Chapter Ten

Tokenized Program Save and Load

The tokenized program can be saved to and reloaded from a peripheral device, such as a disk or a cassette. The primary statement for saving the tokenized program is SAVE. The saved program is reloaded into RAM with the LOAD statement. The CSAVE and the CLOAD statements are special versions of SAVE and LOAD for use with a cassette. Saved File Format The tokenized program is completely contained within the Variable Name Table, the Variable Value Table, and the Statement Table. However, since these tables vary in size, we must also save some information about the size of the tables. The SAVE file format is shown in Figure 10-1. The first part consists of seven fields, each of them two bytes long, which tell where each table starts or ends. Part two contains the saved program's Variable Name Table (VNT), Variable Value Table (VVT), and Statement Table (ST). The displacement value in all the part-one fields is actually the displacement p?us 256. We must subtract 256 from each displacement value to obtain the true displacement. The VNT starts at relative byte zero in the file's second part. The second field in part one holds that value plus 256. The DVVT field in part one contains the displacement, minus 256, of the VVT from the start of part two. The DST value, minus 256, gives the displacement of the Statement Table from the start of part two. The DEND value, minus 256, gives the end-of-file displacement from the start of part two. 81


Chapter Ten Figure 10-1. SAVE File Format PART 1 0 +----------+ | 0 | 2 +----------+ | 256 | <--> The displacement of the VNT from 4 +----------+ the beginning of part two, plus 256. | Not Used | 6 +----------+ | DVVT | <--> The displacement of VNT from the 8 +----------+ beginning of part two, plus 256. | DST | <--> The displacement of ST from the 10+----------+ beginning of part two, plus 256. | Not Used | 12+----------+ | DEND | <--> The displacement of the end of the ======14+==========+ file from the beginning of part two. PART 2 0| VNT | <--> Variable Name Table DVVT-256+----------+ | VVT | <--> Variable Value Table DSNT-256+----------+ | ST | <--> Statement Table DEND-256+----------+ XSAVE($BB5D) The code that implements the SAVE statement starts at the XSAVE ($BB5D) label. Its first task is to open the specified output file, which it does by calling ELADVC. The next operation is to move the first seven RAM table pointers from $80 to a temporary area at $500. While these pointers are being moved, the value contained in the first pointer is subtracted from the value in each of the seven pointers, including the first. Since the first pointer held the absolute address of the first RAM table, this results in a list of displacements from the first RAM table to each of the other tables. These seven two-byte displacements are then written from the temporary area to the file via 103. These are the first fourteen bytes of the SAVE file. (See Figure 10-1.) The first RAM table is the 256-byte buffer, which will not be SAVEd. This is why the seven two-byte fields at the beginning of the SAVEd file hold values exactly 256 more than the true 82
Chapter Ten displacement of the tables they point to. (The LOAD procedure will resolve the 256-byte discrepancy.) The next operation is to write the three needed RAM tables. The total length of these tables is determined from the value in the seventh entry in the displacement list, minus 256. To write the three entries, we point to the start of the Variable Name Table and call 104, with the length of the three tables. This saves the second part of the file format. The file is then closed and XSAVE returns to Execution Control. XLOAD ($BAFB) The LOAD statement is implemented at the XLOAD label located at $BAFB. XLOAD first opens the specified load file for input by calling ELADVC. BASIC reads the first fourteen bytes from the file into a temporary area starting at $500. These fourteen bytes are the seven RAM table displacements created by SAVE. The first two bytes will always be zero, according to the SAVE file format. (See Figure 10-1.) BASIC tests these two bytes for zero values. If these bytes are not zero, BASIC assumes the file is not a valid SAVE file and exits via the ERRNSF, which generates error code 21 (Load File Error). If this is a valid SAVE file, the value in the pointer at $80 (Low Memory Address) is added to each of the seven displace— ments in the temporary area. These values will be the memory addresses of the three RAM tables, if and when they are read into memory. The seventh pointer in the temporary area contains the address where the end of the Statement Table will be. If this address exceeds the current system high memory value, the routine exits via ERRPTL, which generates error code 19 (Load Program Too Big). If the program will fit, the seven addresses are moved from the temporary area to the RAM table pointers at $80. The second part of the file is then loaded into the area now pointed to by the Variable Name Table pointer $82. The file is closed, CLR is executed, and a test for RUN is made. If RUN called XLOAD, then a value of $FF was pushed onto the CPU stack. If RUN did not call XLOAD, then $00 was pushed onto the CPU stack. If RUN was the caller, then an RTS is done. 83
Chapter Ten If XLOAD was entered as a result of a LOAD or CLOAD statement, then XLOAD exits directly to the Program Editor, not to Execution Control. CSAVE and CLOAD The CSAVE and CLOAD statements are special forms of SAVE and LOAD. These two statements assume that the SAVE/LOAD device is the cassette device; CSAVE is not quite the same as SAVE "C:". Using SAVE with the "C:" device name will cause the program to be saved using long cassette inter-record gaps. This is a time waster, and CSAVE uses short inter-record gaps. CSAVE starts at XCSAVE ($BBAC). CLOAD starts at XCLOAD ($BBA4). 84

<-Chapter 09Chapter 11->