Personal Computer News


Microdrive Data Files

 
Published in Personal Computer News #051

Data storage on microdrives can be made more efficient using Gavin Monk's programs.

Microdrive Data Files

Data storage on microdrives can be made more efficient using Gavin Monk's programs

ZX Microdrives are at last available to most Spectrum owners, but most users are still unable to make the most of their Microdrive systems because of the very poor manual. The LOAD* and SAVE* commands are fairly well explained, but data files are explained very poorly. This is a great pity, because using this type of file allows the maximum potential of the system to be exploited. The simple and very useful routine described below illustrates the use of data files.

The Catalogue To z$ Routine

This routine reads the catalogue from a cartridge and places it into z$. This allows a Basic program to examine z$, and hence see if a file is on a cartridge or check the space on a cartridge. A very crude way of doing this would be to first clear the screen then do a CAT command and use the SCREEN$ function to read the details from the screen.

This is the method used by Sinclair in the production of its demo cartridge, but although it works fine it has two main drawbacks. First, all information on the screen is lost, and second, if the cartridge contains more than twenty files, the screen will scroll up and the routine will give very false results.

A much better method is to store the catalogue in a data file on the cartridge then read it into the string and erase the file. To do this the format of the data returned from the CAT command must be known. It is as follows: The first ten bytes contain the cartridge name, the next two are both newlines (i.e. CHR$ 13), then the cartridge contents follow in alphabetical order. Each filename is ten characters long followed by a newline, and lastly another newline followed by one or two bytes containing the number of kilobytes left unused on the cartridge, followed by yet another newline.

The routine is placed at the front of a program so that it operates faster. Stream four is used to store the catalogue as a datafile named 'index.d' on the cartridge. First stream four is closed, just in case it was already open, then it is opened again and attached to a Microdrive channel in drive d with the name 'index.d'.

Next the catalogue of the cartridge in drive d is sent to the file-line 110. The file is then closed for writing and opened for reading - lines 120 and 140. Line 150 reads data from the file until a newline code appears and places it into r$. If two newline codes appear adjacent in the file then r$ will be a null string.

This will happen twice, once after the cartridge name and again before the amount of space left on the cartridge. This therefore can be used to detect the end of the file 'index.d'. Line 160 checks to see if r$ is a null string, and if this is so the end of file counter, eof is incremented by one. If eof = 2, then this means that the next item on the file is the remaining space left on the cartridge. Then r$ is added to z$, and the program loops back to read the next item from the file unless eof = 2, indicating that the end of the file has been reached.

If eof = 2 then the file is closed and erased by lines 190 and 200. This leaves z$ containing the cartridge catalogue. The format of z$ is as follows. The first ten characters are the cartridge name. This is followed by the filenames in alphabetical order (each ten characters long), and lastly one or two characters containing the amount of spare space remaining on the cartridge.

The routine can be tested by the following short Basic program:

   10 LET d=1:REM drive 1
   20 GOSUB 100:REM call CAT to z$ routine
   30 CLS:PRINT z$
   40 STOP

This short program will, after about 40 seconds, print z$ on the screen. It should look like a chain of names followed by a number.

The programs presented here show uses of this routine. They are a ZX Microdrive indexed loader, and a simple Microdrive database program.

ZX Microdrive Indexed Loader

This program requires approximately 5K of cartridge space, although this can be reduced considerably by the removal of REM statements and use of multi-statement lines. The program creates a program file named 'run' which allows a file from a cartridge to be loaded, without remembering its name, and by pressing only three keys.

The program relies on the fact that identifiers are included within the file names. This is performed by the use of a dot suffix similar to the system used in CP/M. The required identifiers are:

.P - Program file
.c - Code file
.d - Data file

In addition to this the option to just list some files from the catalogue has been included. This is performed by the filename beginning with an upper case letter. This is very useful if the cartridge contains, say, a program file which when run loads a machine code file. For example, if a program called Snake loaded some machine code called snake code when run, then if the two files were called Snake.p and snake.c respectively, the index would just include the filename Snake.p and not snake.c as well.

Type in the program as shown in Listing 1 and type RUN 990 (ENTER). Now you should be presented with a menu asking you which of the three options is required. Select the option required and you will not be prompted for the drive number. The program will read the catalogue from the selected drive and place it into z$. Now z$ is spliced so that t$ contains the cartridge title and f$ contains the list of files as previously selected.

The program is then saved onto the cartridge with the filename 'run'. Now type RAND USR 0 to reset the Spectrum and type RUN (ENTER). After a short delay you will be presented with an index of the files on the cartridge.

Select the file that you require to be loaded by pressing the letter which occurs before that filename on the screen. The file will now be loaded by the program unless it is a code file. If this is so you will be asked to input a value for RAMtop as required. If this is not required then just press Enter. The file will now be loaded by the program. Also if key 1 is pressed the index will be updated. This should be performed after new files have been added to the cartridge. If key 2 is pressed the menu program will be loaded from the next drive.

Simple Microdrive Database Program

This is a simple database program which allows you to store and sort dual file lists. Possible examples of its use are catalogues of magazine articles, software lsits and telephone numbers.

Type in the program shown in listing 2 and type RUN 500 (ENTER). You will be presented with a menu of the available options, which are as follows:

1. Add to File

Enter Name
Enter Details

You will then be asked if name and details are OK. At this point pressing y allows more data to be entered, n deletes the last entry and allows the correct one to be added, and s stops adding and sorting data then returns to main menu.

2. Delete From File

Enter name to be deleted, and the name and details will be deleted.

3. Search and Print Items

Enter string to be searched for in the name list, and all items containing the string will be listed.

4. Print File

Prints the whole file.

5. Printer On/Off

This is a toggle to switch data output to screen or printer.

6. Save or Load Data

This allows the program and its current data to be saved to cassette or Microdrive. If the Microdrive option is chosen then the required cartridge is checked to see if the file already exists, and that there is enough spare space on the cartridge. The Cat to z$ routine is used to do this and hence errors are trapped.

In its current form this program can be used only for simple types of data storage. However, the program is very user-friendly and includes a very powerful search routine. The program could be further improved by adding a faster sort algorithm and increasing the number of data fields.

Microdrive Clinic

Microdrives can take a bit of getting used to, and this isn't helped by the fact that, since their launch, Sinclair has been carrying out various modifications to deal with problems that have arisen in use.

But there are a couple of common problems you can look out for. If you try to load a program or data file from Microdrive when there isn't enough memory for it you get an 'out of memory' error, the computer hangs up but the tape keeps whizzing round. Even though the manual warns you not to switch off while the tape is turning, it's the only way out of this one.

This kind of crash is most likely to happen when you have a program that lowers RAMtop with CLEAR. Using RANDOMISE USR 0 before loading the program helps avoid this.

If you have a ZX Printer connected as well as Interface 1, you may find it impossible to load programs from tape. The only way out of this is to unplug the printer, with consequent wear and tear on the edge connector.

There are other bugs which sometimes appear when Interface 1 and the ZX Printer are connected conerning INKEY$ and BEEP. These sometimes cause the machine to hang up, but at other times it may take four BEEPs before this happens. They can be demonstrated with:

10 BEEP .1,50

This makes the computer display thin black lines in the border, then it hangs up.

10 IF INKEY$="a" THEN GO TO 10

This works properly if the first key you press is 'a', but if you press any othey key first the program won't stop when you press 'a', or when you press BREAK.

Gavin Monk