Pascal Disk Handler for the Larken

Author(s): David Solly; Larry Kenny
Pub Details: Time Designs Magazine v4 n5 (Volume 4, Issue 5)
Date: July/August 1988
Pages: 20-23

Access to the Larken Disk Drive System from within a compiled HiSoft(TM) Pascal program is now possible thanks to the procedures developed by David Solly and Larry Kenny. This article demonstrates how to install these procedures and gives a practical demonstration on how they may be used within a simple directory program. The procedures described in this article are valid for both the Timex Sinclair 2068 and the ZX Spectrum versions of HiSoft Pascal.

I have owned HiSoft Pascal, (henceforth Pascal), for the Timex Sinclair 2068 and the ZX Spectrum for a number of years now but the drawback with Pascal, as it is implemented on Sinclair computers, has been that there was no way to access a DOS from within a compiled program. It seemed ridiculous to put the effort into creating programs which ran like machine code once the source code was debugged, compiled and transfered to disk only to be forced to go back to a plodding tape operating system, (henceforth TOS), when I needed to save or enter data from within the compiled program. Finally I sat down and did some serious study of the problem, some head scratching, some question asking (especially of Larry Kenny, who is the creator of the Larken DOS, and Ken Schieman) and some experimentation. The result of this is the creation of the Pascal disk handler described below.

The needs of LKDOS are the same as for the TOS. You are required to provide the name of your program, the starting address of the program and the lenght of the program. This is exactly what happens when you type within Basic:

SAVE “Myprogram” CODE start, length.

Pascal also passes the same information to the TOS when it saves out a variable through the use of the procedure:

TOUT (‘Myprogram’, ADDR (the name of the variable being saved), SIZE (the name of the variable being saved));

The crux of the problem is that, although both languages generate identical information, LKDOS can extract it only from within the Basic environment. Therefore, additional programming is need in order to transfer the save or load parameters to LKDOS from within the Pascal environment.

The first steps towards transfering the required information form Pascal to LKDOS are to store the name, the start address, and the length of the variable to be saved or loaded in a safe area of the RAM. This is a simple matter thanks to the very versatile POKE() function in Pascal. Unlike the POKE command available in Basic, this Pascal function can DOKE a number or POKE the contents of a whole character array starting at a given address. The first fifteen bytes of the printer buffer proved to be the safest area to which all the transfers can be achieved with the following lines:

POKE (23300, 'Myprogram');

POKE (23311, ADDR (name of variable));

and

POKE (23313, SIZE (name of variable));

You can see these lines in a modified form in the demonstration program within the procedures GETNAME, DISKSAVE and DISKLOAD.

Upon completion of these steps the next step is to copy the information from its storage place in the RAM to the appropriate routines in the LKDOS. Again to HiSoft includes within their implementation of Pascal the procedure INLINE() wich allows us to embed Z80 machine code within a Pascal procedure to accomplish our ends.

The following is a disassembly of the machine code used in the procedure DOUT

00010 NAME      EQU 23300
00020 PROGNM EQU 8226
00030 TEMP4 EQU 8243
00040 TEMP2 EQU 8241
00050 NMIF EQU 8194
00060 ADDR EQU 23311
00070 SIZE EQU 23313
00080 SV1 EQU 0204
00090 SV2 EQU 0207
00100 ORG 40000
00110 DI ; Disable interrupt
00120 CALL 98 ; Turn on LKDOS cartridge
00130 LD HL,NAME ; Transfer file name
00140 LD DE,PROGNM ; to prognm
00150 LD BC,09
00160 MOVE LD A,(HL) ; Loop to catch any
00170 CP 0 ; occurance of CHR$ 0
00180 JR NZ,NZERO
00190 LD (HL),32 ; & replace with a space
00200 NZERO LDI
00210 LD A,B
00220 OR C
00230 JR NZ,MOVE
00240 LD A,11
00250 LD (NMIF),A
00260 CALL SV1 ; Save name
00270 LD HL,(ADDR) ; Retrieve start address
00280 LD (TEMP4),HL ; Set start address
00290 LD HL,(SIZE) ; Retrieve length of save
00300 LD (TEMP2),HL ; Set length of save
00310 CALL SV2 ; Second save data
00320 LD A,(100) ; Exit cartridge
00330 EI ; Enable interrupt

(Program provided by Larry Kenny of Larken Electronics, #2 Navan, Ontario, Canada K4B 1H9, Tel: (613)-835-2680) (Listing made using Zeus Assembler)

The procedure DIN contains the same code except that SV1 and SV2 are replaced with LD1 EQU 198 and LD2 EQU 201. The MOVE loop insures that the file name is padded out with the correct number of spaces so that it totals nine characters and spaces otherwise the file name may be corrupted and fail to reload.

The following listing demonstrates how all the procedures described above are used within a complete Pascal program. The object of the program is to create a simple telephone directory which will allow you to store ten names and numbers, read the information stored in the directory, and read and write the information stored in the directory to disk using the Larken LKDOS. There is also a summation of this article contained in the procedure SONGANDDANCE.

Pascal source code listing

  10 {PROGRAM BY:                 }
20 {DAVID SOLLY }
30 {[address omitted]}
40 {OTTAWA, ONTARIO }
50 {[address omitted]}
60 {TEL: [phone number omitted]}
70
80
90 {THIS PORGRAM IS FOR }
100 {DEMONSTRATING HOW THE }
110 {LARKEN DISK DRIVE MAY BE }
120 {ACCESSED FOR STORING AND }
130 {RETRIEVING DATA WITHIN A }
140 {HISOFT(TM) PASCAL PROGRAM. }
150 {THE PROCEDURES DESCRIBED }
160 {IN THIS PROGRAM ARE VALID }
170 {FOR BOTH THE ZX SPECTRUM }
180 {AND THE TIMEX SINCLAIR }
190 {2068 VERSIONS OF HISOFT }
200 {PASCL. }
210
220
230 PROGRAM LARKENDEMO;
240
250 CONST
260 LENGTH = 10;
270 PN = 23300; {ADDRESS WHERE THE NAME FOR THE SAVE/LOAD}
280 {ROUTINES IS STORED}
290 PB = 23311; {CONTAINS THE ADDRESS WHERE THE DATA BEGINS}
300 PS = 23313; {CONTAINS THE NUMBER OF BYTES USED BY THE DATA}
310 CAPSLOCK = 23658; {ADDRESS OF THE CAP SHIFT LOCK CONTROL}
320 ON = 8; {SWITCHES CAPSLOCK ON}
330 OFF = 0; {SWITCHES CAPSLOCK OFF}
340
350
360 TYPE
370 ENTRY = RECORD
380 NAME : ARRAY [1..10] OF CHAR;
390 NUMBER : ARRAY [1..10] OF CHAR;
400 END;
410
420
430 VAR
440 DIRECTORY : ARRAY [1..LENGTH] OF ENTRY;
450 I : INTEGER;
460 FINISHED : BOOLEAN;
470 ANS : CHAR;
480
490
500
510
520 PROCEDURE DOUT; {INVOKES LKDOS WRITE}
530 BEGIN
540 INLINE (#F3, #CD, #62, #00, #21, #04, #5B, #11,
550 #22, #20, #01, #09, #00, #7E, #FE, #00,
560 #20, #02, #36, #20, #ED, #A0, #78, #B1,
570 #20, #F3, #3E, #0B, #32, #02, #20, #CD,
580 #CC, #00, #2A, #0F, #5B, #22, #33, #20,
590 #2A, #11, #5B, #22, #31, #20, #CD, #CF,
600 #00, #3A, #64, #00, #FB);
610 END;
620
630
640 PROCEDURE DIN; {INVOKES LKDOS READ}
650 BEGIN
660 INLINE (#F3, #CD, #62, #00, #21, #04, #5B, #11,
670 #22, #20, #01, #09, #00, #7E, #FE, #00,
680 #20, #02, #36, #20, #ED, #A0, #78, #B1,
690 #20, #F3, #3E, #0B, #32, #02, #20, #CD,
700 #C6, #00, #2A, #0F, #5B, #22, #33, #20,
710 #2A, #11, #5B, #22, #31, #20, #CD, #C9,
720 #00, #3A, #64, #00, #FB);
730 END;
740
750
760 PROCEDURE GETNAME;
770 VAR
780 PROGNM : ARRAY [1..9] OF CHAR;
790
800 BEGIN
810 WRITELN;
820 POKE (CAPSLOCK, OFF);
830 WRITELN ('NAME FOR DISK OPERATION? ');
840 WRITELN;
850 WRITE ('9 CHARACTERS MAXIMUM: ');
860 READLN;
870 READLN (PROGNM);
880 POKE (PN, PROGNM); {STORES THE NAME IN RAM}
890 END;
900
910
920 PROCEDURE DISKSAVE;
930 BEGIN
940 PAGE;
950 WRITELN;
960 WRITELN ('LARKEN DISK SAVE ROUTINE');
970 WRITELN;
980 GETNAME;
990 POKE (PB, ADDR (DIRECTORY)); {START ADDRESS FROM WHERE THE}
1000 {DATA IS TO BE SAVED}
1010 POKE (PS, SIZE (DIRECTORY)); {NUMBER OF DATA BYTES TO SAVE}
1020 DOUT;
1030 WRITELN ('SAVED!');
1040 FOR I := 1 TO 100 DO {PAUSE LOOP}
1050 END;
1060
1070
1080 PROCEDURE DISKLOAD;
1090 BEGIN
1100 PAGE;
1110 WRITELN;
1120 WRITELN ('LARKEN DISK LOAD ROUTINE');
1130 WRITELN;
1140 GETNAME;
1150 POKE (PB, ADDR (DIRECTORY)); {START ADDRESS AT WHICH THE}
1160 {DATA IS TO BE LOADED}
1170 POKE (PS, SIZE (DIRECTORY)); {NUMBER OF DATA BYTES TO LOAD}
1180 DIN;
1190 WRITELN ('LOADED!');
1200 FOR I := 1 TO 100 DO {PAUSE LOOP}
1210 END;
1220
1230
1240 PROCEDURE FILLDIRECTORY;
1250 BEGIN
1260 PAGE;
1270 POKE (CAPSLOCK, OFF);
1280 FOR I := 1 TO LENGTH DO
1290 BEGIN
1300 WITH DIRECTORY [I] DO
1310 BEGIN
1320 WRITELN ('ENTRY NO. ', I, ' OF ', LENGTH);
1330 WRITELN;
1340 WRITE ('NAME PLEASE (10 CHARS) ');
1350 READLN;
1360 READ (NAME);
1370 WRITE ('NUMBER PLEASE (10 CHARS) ');
1380 READLN;
1390 READ (NUMBER)
1400 END
1410 END;
1420 WRITELN ('DIRECTORY FULL ');
1430 WRITELN;
1440 WRITELN ('HIT ANY KEY TO CONTINUE');
1450 READLN
1460 END;
1470
1480
1490 PROCEDURE READDIRECTORY;
1500 BEGIN
1510 PAGE;
1520 POKE (CAPSLOCK, OFF);
1530 FOR I := 1 TO LENGTH DO
1540 BEGIN
1550 WITH DIRECTORY [I] DO
1560 BEGIN
1570 WRITELN (NAME,' ', NUMBER);
1580 WRITELN;
1590 END
1600 END;
1610 WRITELN ('END OF DIRECTORY');
1620 WRITELN ('HIT ANY KEY TO CONTINUE');
1630 READLN
1640 END;
1650
1660
1670 PROCEDURE SONGANDDANCE;
1680 BEGIN
1690 PAGE;
1700 WRITELN ('Larken Disk Access Routine');
1710 WRITELN ('for');
1720 WRITELN ('HiSoft(TM) Pascal');
1730 WRITELN ('for the');
1740 WRITELN ('ZX Spectrum');
1750 WRITELN ('and the');
1760 WRITELN ('Timex Sinclair 2068');
1770 WRITELN;
1780 WRITELN ('by');
1790 WRITELN ('David Solly');
1800 WRITELN ('and');
1810 WRITELN ('Larry Kenny');
1820 WRITELN;
1830 WRITELN ('Hit any key to continue');
1840 READLN;
1850 PAGE;
1860 WRITELN ('Many ZX Spectrum and TS 2068');
1870 WRITELN ('programers have long wanted to');
1880 WRITELN ('do serious programing in other');
1890 WRITELN ('languages than the resident');
1900 WRITELN ('Sinclair Basic. Although such');
1910 WRITELN ('languages as Forth, Logo, C,');
1920 WRITELN ('Prolog and Pascal have long been');
1930 WRITELN ('available to Sinclair users one');
1940 WRITELN ('of the main drawbacks for');
1950 WRITELN ('serious programing in these');
1960 WRITELN ('languages has been the lack of');
1970 WRITELN ('disk I/O routines. This program');
1980 WRITELN ('will demonstrate how the Larken');
1990 WRITELN ('disk drive system may be');
2000 WRITELN ('accessed for storing and ');
2010 WRITELN ('retrieving data within a');
2020 WRITELN ('HiSoft(TM) Pascal program. The');
2030 WRITELN ('procedures described in this ');
2040 WRITELN;
2050 WRITELN ('Hit any key to continue');
2060 READLN;
2070 PAGE;
2080 WRITELN ('program are valid for both the');
2090 WRITELN ('ZX Spectrum and the Timex');
2100 WRITELN ('Sinclair 2068');
2110 WRITELN;
2120 WRITELN ('The two disk drive procedures');
2130 WRITELN ('are found in the procedures DIN,');
2140 WRITELN ('and DOUT. A third procedure,');
2150 WRITELN ('called GETNAME, supplies the');
2160 WRITELN ('above procedures with a name for');
2170 WRITELN ('storing or retrieving from the');
2180 WRITELN ('disk. All these procedures work');
2190 WRITELN ('in conjunction with the resident');
2200 WRITELN ('procedures ADDR(), SIZE(), and');
2210 WRITELN ('POKE()');
2220 WRITELN;
2230 WRITELN ('The authors hope that these');
2240 WRITELN ('procedures will stimulate Pascal');
2250 WRITELN ('programing for the ZX Spectrum');
2260 WRITELN;
2270 WRITELN ('Hit any key to continue');
2280 READLN;
2290 PAGE;
2300 WRITELN ('and the Timex Sinclair 2068 and');
2310 WRITELN ('encourage other Sinclairests to');
2320 WRITELN ('write disk routines for the');
2330 WRITELN ('other languages mentioned');
2340 WRITELN ('above.');
2350 WRITELN;
2360 WRITELN ('David Solly');
2370 WRITELN ('Larry Kenny');
2380 WRITELN;
2390 WRITELN;
2400 WRITELN ('Hit any key to continue');
2410 READLN
2420 END;
2430
2440
2450 BEGIN {BODY OF THE PROGRAM}
2460 REPEAT
2470 PAGE;
2480 POKE (CAPSLOCK, ON);
2490 FINISHED := FALSE;
2500 WRITELN;
2510 WRITELN;
2520 WRITELN ('MENU');
2530 WRITELN;
2540 WRITELN ('SELECT ONE OF THE FOLLOWING');
2550 WRITELN ('OPTIONS ');
2560 WRITELN;
2570 WRITELN;
2580 WRITELN ('1) READ THE INTRODUCTION');
2590 WRITELN ('2) CREATE DATA');
2600 WRITELN ('3) READ DATA');
2610 WRITELN ('4) SAVE DATE TO DISK');
2620 WRITELN ('5) LOAD DATA FROM DISK');
2630 WRITELN ('6) EXIT THE PROGAM');
2640 WRITELN;
2650 WRITELN;
2660 WRITE ('MAKE YOUR SELECTION ');
2670 READLN;
2680 READ (ANS);
2690
2700
2710 CASE ANS OF
2720 '1' : SONGANDDANCE;
2730 '2' : FILLDIRECTORY;
2740 '3' : READDIRECTORY;
2750 '4' : DISKSAVE;
2760 '5' : DISKLOAD;
2770 '6' : FINISHED := TRUE
2780 END;
2790
2800
2810 UNTIL FINISHED = TRUE;
2820
2830
2840 {FINALE}
2850 PAGE;
2860 WRITELN;
2870 WRITELN;
2880 WRITELN ('END OF DEMONSTRATION');
2890 WRITELN;
2900 WRITELN
2910 END.

The Pascal disk handler described in this article and program works only within a compiled Pascal program and, regretfull, can not be used to save Pascal source code. The procedures DIN, DOUT, and GETNAME are completely modular. They may be copied directly from this program into the appropriate section of any Pascal program you care to write which requires access to LKDOS. The procedures DISKSAVE and DISKLOAD may also be used but remember to change the name within the parentheses of ADDR() and SIZE() to the name of the variable within your program that you wish to save or load. The “.C” extension required by data file saves within Basic when using LKDOS is not required by the Pascal disk handler, however, it may be good practice to use the extension “.P” to indicate that the data that has been saved is intended for a Pascal program rather than a Basic program. All the LKDOS error codes are operational and will stop your program without crashing providing that the compiled code is accessed through a Basic loader program, that a PRINT USR is used rather than RANDOMIZE USR and that there is at least one line of Basic after the USR call. Example:

  10 REM TYPICAL COMPILED PASCAL PROGRAM LOADER
20 BORDER 0: PAPER 0: INK 7: CLS
30 PRINT #4: LOAD "MYPROG.C1" CODE 27000
40 CLS: PRINT USR 27000
50 STOP
9000 REM SAVE LOADER TO DISK
9010 PRINT #4: SAVE "LOADER.B1" LINE 1

On behalf of Larry and myself I hope that this Pascal disk handler will prove to be useful to all who wish to do serious programming in Pascal which requires disk access.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.