Chapter 20: File storage

BASin

Chapter 20: File storage

Summary

LOAD, SAVE, VERIFY, MERGE

Loading and saving work differently in BASin than on a real ZX Spectrum. See Loading and saving files.

It is also recommended that you read the sections on LOAD, SAVE, VERIFY and MERGE. These sections will acquaint you with how those commands work.

When you have read them, then come back here for more information as to the things you can do with these commands.

Working with MERGE

We have seen that LOAD deletes the old program and variables in the computer before loading in the new ones from disk; there is another command, MERGE, that does not. MERGE only deletes an old program line or variable if it has to because there is a new one with the same line number or name. Type in the 'dice' program in Chapter 11 and save it on disk, as "dice". Now enter and run the following:

 1 PRINT 1
 2 PRINT 2
10 PRINT 10
20 LET x=20

and then proceed as for the verification, but replacing VERIFY "dice" with

MERGE "dice"

If you list the program you can see that lines 1 and 2 have survived, but lines 10 and 20 have been replaced by those from the dice program. x has also survived (try PRINT x).

You have now seen simple forms of the four statements used with the file system:

SAVE stores the program and variables in a file.

VERIFY checks the program and variables in a file against those already in the computer.

LOAD clears the computer of all its program and variables, and replaces them with new ones read in from a file.

MERGE is like LOAD except that it does not clear out an old program line or variable unless it has to because its line number or name is the same as that as that of a new one from the file.

In each of these, the keyword is followed by a string: for SAVE this provides a name for the program on disk, while for the other three it tells the computer which program to load. There are a couple of twists to all this.

You can provide the empty string instead of a filename; then the computer prompts you to browse for a file on disk. (On a real ZX Spectrum, the empty string indicates the next file on the cassette tape - regardless of its filename - and cannot be used with SAVE.)

A variant on SAVE takes the form

SAVE string LINE number

A program saved using this is recorded in such a way that when it is read back by LOAD (but not MERGE) it automatically jumps to the line with the given number, thus running itself.

So far, the only kinds of information we have stored on disk have been programs together with their variables. There are two other kinds as well, called arrays and bytes.

Arrays are dealt with slightly differently:

You can save arrays on disk using DATA in a SAVE statement by

SAVE string DATA arrayname()

String is the name that the information will have on disk and works in exactly the same way as when you save a program or plain bytes.

The array name specifies the array you want to save, so it is just a letter or a letter followed by $. Remember the brackets afterwards; you might think they are logically unnecessary but you still have to put them in to make it easier for the computer.

Be clear about the separate roles of string and array name. If you say (for instance)

SAVE "Bloggs" DATA b()

then SAVE takes the array b from the computer and stores it on disk under the name "Bloggs". When you type

VERIFY "Bloggs" DATA b()

the computer will look for a number array stored on disk under the name "Bloggs" and check it against the array b in the computer.

LOAD "Bloggs" DATA b()

finds the array on disk, and then - if there is room for it in the computer - deletes any array already existing called b and loads in the new array from disk, calling it b.

You cannot use MERGE with saved arrays.

You can save character (string) arrays in exactly the same way. When you load in a character array, the computer will delete not only any previous character array with the same name, but also any string with the same name.

Byte storage is used for pieces of information without any reference to what the information is used for - it could be television pictures, or used-defined graphics, or something you have made up yourself. It is shown using the word CODE, as in

SAVE "picture" CODE 16384,6912

The unit of storage in memory is the byte (a number between 0 and 255), and each byte has an address (which is a number between 0 and 65535). The first number after CODE is the address of the first byte to be stored on disk, and the second is the number of bytes to be stored. In our case, 16384 is the address of the first byte in the display file (which contains the television picture) and 6912 is the number of bytes in it, so we are saving a copy of the television screen - try it. The name "picture" works just like the names for programs.

To load it back, use

LOAD "picture" CODE

You can put numbers after CODE in the form

LOAD name CODE start,length

Here length is just a safety measure; when the computer has found the bytes on disk with the right name, it will still refuse to load them in if there are more than length of them - since there is obviously more data than you expected it could otherwise overwrite something you had not intended to be overwritten. It gives the error report R File loading error. You can miss out length, and then the computer will read in the bytes however many there are.

Start shows the address where the first byte is to be loaded back to - this can be different from the address it was saved from, although if they are the same you can miss out the start in the LOAD statement.

CODE 16384,6912 is so useful for saving and loading the picture that you can replace it with SCREEN$ - for instance:

SAVE "picture" SCREEN$
LOAD "picture" SCREEN$

BASin supports a number of different file formats. You can indicate the format you wish to use by providing a filename extension.

The RAM Disk

BASin provides emulation of the Spectrum 128k and +2's Silicon Disk (or RAM Disk). Be aware that using the Silicon Disk has it's own requirements related to the use of other 128k commands. See the section on 128k commands in BASin for more information.

Anything you can do with SAVE, LOAD or MERGE for files saved from BASin, you can do with the silicon disk that's built into the 128k Spectrum models. This acts like the usual cassette, or hard disk in BASin (with a couple of extra commands), with the exception that it's about 64kb in size, very fast, and loses its contents when BASin is reset or quit (However, it does survive the NEW command). You use the commands in exactly the same way you would in normal operation - simply add an exclamation mark ! between the command and its associated string. So where you would type...

SAVE "squares"

...to save normally, you may instead use...

SAVE !"squares"

...to save to the silicon disk.

There are two extra commands for use with the silicon disk. The first one is...

CAT !

...which gives you a list of all the programs or data files that are stored on the disc.

The second one is...

ERASE !"filename"

...to get rid of an unwanted program or data.

Perhaps the most obvious use of the silicon disc is to store chunks of BASIC program which can be merged (using MERGE !) into a smaller program, in sequence. This makes it possible to write about 90kb of BASIC program, and hold it in the emulated memory (to do this, the program structure has to be well defined).

One of the more interesting uses of the silicon disk is in animation, where a series of pictures can be defined by a 'slow' BASIC program and stored in the silicon disk, then called back to the screen at high speed. The following program gives a taste of this; doubtless you can do better...

 10 INK 5: 
    PAPER 0: 
    BORDER 0: 
    CLS
 20 FOR f=1 TO 10
 30 CIRCLE f*20,150,f
 40 SAVE !"ball"+STR$(f) CODE 16384, 2048
 50 CLS
 60 NEXT f
 70 FOR f=1 TO 10
 80 LOAD !"ball"+STR$(f) CODE 
 90 NEXT f
100 BEEP 0.01,0.01
110 FOR f=9 TO 2 STEP -1
120 LOAD !"ball"+STR$(f) CODE
130 NEXT f
140 BEEP 0.01,0.01
150 GO TO 70
160 REM use GO TO 160 to clear pictures from disc
170 FOR f=10 TO 1 STEP -1
180 ERASE !"ball"+STR$(f)
190 NEXT f

Note that in line 40 of this program, the two numbers following CODE are the address in memory of the start of the screen, and the length of the top third of it. By only saving and loading the top third, the overall speed is maintained. Lines 160 to 190 are there if you break (using ESC) out of the program, modify the circle drawing bit, and try to save a new set of pictures. So before doing that, type GO TO 160 to clear out the silicon disc. (Always try to delete files backwards so the last file to be saved will be the first to be deleted. This saves the computer a lot of juggling about, and is much faster).

Summary

Below is a complete summary of the four statements used in this chapter. Name stands for any string expression, and refers to the name under which the information is stored on disk. It should consist of ASCII printing characters. Unlike the original ZX Spectrum, BASin permits filenames to be longer than 10 characters.

There are four sorts of information that can be stored on disk: program and variables (together), number arrays, character arrays and straight bytes.

When you provide the empty string instead of a filename, BASin prompts you for the file to be opened or saved.

SAVE

Saves information on disk under the given name.

  1. Program and variables:
    SAVE (!) name LINE line number

    saves the program and variables in such a way that LOAD automatically follows with

    GO TO line number
     
  2. Bytes:

    SAVE (!)name CODE start, length

    saves length bytes starting at address start.

    SAVE (!)name SCREEN$

    is equivalent to

    SAVE (!)name CODE 16384,6912

    and saves the television picture.
     
  3. Arrays:

    SAVE (!)name DATA letter()
    or
    SAVE (!)name DATA letter$()

    saves the array whose name is letter or letter$ (this need bear no relation to name).
     
  4. Verify:

    Checks the information against on tape against the information already in memory. Failure to verify gives error R File loading error.
     
    1. Program and variables:

      VERIFY name
       
    2. Bytes:

      VERIFY name CODE start,length

      If the bytes name on disk are more than length in number, then gives error R. Otherwise, checks them against those in memory starting at address start.

      VERIFY name CODE start

      checks the bytes name on tape against those in memory starting at the address from which the first disk byte was saved.

      VERIFY name SCREEN$

      is equivalent to

      VERIFY name CODE 16384,6912

      (On an original ZX Spectrum, this will almost certainly fail to verify, because the filename is printed on screen as the file loads from cassette.)
       
    3. Arrays:

      VERIFY name DATA letter()
      or
      VERIFY name DATA letter$()

      checks the array name on disk against the array letter or letter$ in memory.
       

     

LOAD

Loads new information fron disk, deleting old information from memory.

  1. Program and variables:

    LOAD (!)name

    deletes the old program and variables and loads in program and variables name from disk; if the program was saved using SAVE name LINE it performs an automatic jump.

    Error 4 Out of memory occurs if there is no room for the new program and variables. In this case the old program and variables are not deleted.
     
  2. Bytes:

    LOAD (!)name CODE start, length

    If the bytes name from disk are more than length in number then gives error R. Otherwise, loads them into memory starting at address start and overwriting whatever was there previously.

    LOAD (!)name CODE start

    loads the bytes name from disk into memory, starting at address start and overwriting whatever was there previously.

    LOAD (!)name CODE

    loads the bytes name from disk into memory starting at the address from which the first disk byte was saved and overwriting the bytes that were there in memory before.
     
  3. Arrays:

    LOAD (!)name DATA letter()
    or
    LOAD (!)name DATA letter$()

    deletes any array called letter or letter$ (as appropriate) and forms a new one from the array stored on disk.

    Error 4 Out of memory occurs if no room for new arrays. Old arrays are not deleted.
     

MERGE

Loads new information from disk without deleting old information from memory.

  1. Program and variables:

    MERGE (!)name

    merges the program name in with the one already in memory, overwriting any program lines or variables in the old program whose line numbers or names conflict with ones in the new program.

    Error 4 Out of memory occurs unless there is enough room in memory for all of the old program and variables and all of the new program and variables being loaded from disk.
     
    1. Bytes:

      Not possible
       
    2. Arrays:

      Not possible
       

Exercises

  1. Make a disk on which the first program, when loaded, prints a menu (a list of some other programs on the disk), asks you to choose a program, and then loads it.
     
  2. Get the chess piece graphics from Chapter 14, and then type NEW: they will survive this. However, they will not survive having the computer turned off; if you want to keep then, you must save them on disk, using SAVE with CODE. The easiest way is to save all twenty-one user defined graphics by
    SAVE "chess" CODE USR "a",21*8
    

    followed by

    VERIFY "chess" CODE
    

    This is the system of bytes saving that was used for saving the picture. The address of the first byte to be saved is USR "a", the address of the first of the eight bytes that determine the pattern of the first user-defined graphics, and the number of bytes to be saved is 21*8 - eight bytes for each of 21 graphics.

    To load back you would normally use

    LOAD "chess" CODE
    

    However, if you are loading back into a Spectrum with a different amount of memory, or if you have moved the user-defined graphics to a different address (you have to do this deliberately using more advanced techniques), you have to be more careful and use

    LOAD "chess" CODE USR "a"
    

    USR allows for the fact that the graphics must be loaded back to a different address.

Chapter 21

Chapter 19