Andrew Hewson looks at the position of the ZX-81 screen display and shows how to produce headerless files on the Spectrum
I HAVE NOTICED a decrease in the number of questions from ZX-81 owners in the past year and so I have tended to concentrate on the Spectrum. This month I redress the balance.
The first question, from Mr Dave Harris, introduces the theme: "Can you explain the ZX-81 display file?"
The area at the bottom of RAM between 16384 and 16508 holds the system variables and is followed by the program area, starting at 16509. The display file is next but as programs can vary in length, the display file does not start at a fixed address. The ZX-81 keeps track of it by storing the current value of the starting address in D-FILE.
If you look at page 178 of the manual you will see that the value of D-FILE is stored at address 16396 and so you might infer that you have only to look at the contents of 16396 to find the value of D-FILE.
Unfortunately, that is not true. Remember that the value of D-FILE is an address and that addresses are whole numbers, like 16384 and 17407 and 32767. A single location can only hold a number between 0 and 255 and so two adjacent locations are used to store large numbers. The value of D-FILE is given by: value in 16396 + 256*value in 16397. Any whole number between 0 and 65535 inclusive can be stored using that system. The value held at an address can be found by PEEKing at it and so you can PRINT the value of DFILE by entering:
PRINT PEEK 16396 + 256*PEEK 16397
You may know that the contents of the first location in the display file is always 118 and you can show that by entering:
PRINT PEEK (PEEK 16396 + 256*PEEK 16397)
Similarly, the address of the end of the display is held in VARS - or more correctly is one less than the value in VARS - and can be PRINTed by entering:
PRINT PEEK 16400 + 256*PEEK 16401 - 1
Each line displayed on the screen is terminated by a byte containing decimal 118 and there is one more byte at the beginning of the file also containing 118. As there are 24 lines in the display, there are 25 bytes containing 118 and it is essential to the correct functioning of the display software that all 25 bytes are present.
With an unexpended ZX-81 the display file consists of those 25 bytes and no more unless a LIST, PRINT or PLOT command has been executed since the last CLS or RUN command. That is a device to keep the display file as small as possible.
With a memory expansion pack of 4 K or more the ZX-81 pads out the display file with 32 bytes per line each containing zero - i.e., blanks.
Mark Wilson writes: "Could you please tell me how to scroll the ZX-81 screen up, down, left and right?"
To scroll the screen downwards or from side to side it is necessary to rearrange the contents of the display file in the appropriate logical fashion. There is one rule which must be obeyed whilst such rearrangements are in progress. It is that whilst running in SLOW mode the ZX-81 display file must contain at least 25 bytes holding 118 at all times. If that rule is broken the consequences are dire - the machine crashes and the only remedy is to pull out the plug and start again.
Tables 1 and 2 consist of programs to scroll the display from side to side and downwards respectively. A few simple alterations as shown to the program in table 1 will change the direction of scrolling from right to left to left to right as required. Similar changes could be made to the program in table 2 to scroll the screen upwards but I have not shown them because it is easier to use the SCROLL command.
The first program works as follows. Variables A and B are set to the values of D-FILE and VARS less one, that is to the addresses of the beginning and end of the display file respectively. Then each location in the display file is inspected in turn using variable I. If it does not contain 118 then the value in it is POKEd into the location to the left or to the right - depending on the value set for D in line 120 - and the routine loops to line 160 to inspect the next location.
If a location contains 118 the value from the beginning of the line - store in variable - is POKEd into the adjacent location. That is the way in which a wraparound effect is achieved. A test is then made - line 210 - to see if the job is completed. The second program works in an analogous fashion.
Adrian Neilson writes: "Please explain how to get a scan going across the screen which makes up a picture and the name of the game and then fills in with colour."
The technique is very straight forward and is explained in the Spectrum Manual but I have received several letters on the topic. The current Spectrum display can be SAVEd at any time using either of the commands:
SAVE "name" SCREEN$ SAVE "name" CODE 16384,6912
The display can be LOADed by entering any of the following:
LOAD "name" SCREEN$ LOAD "name" CODE 16384,6912 LOAD"name"CODE
A particular problem with SAVEing and LOADing the screen is that the various commands and messages are also displayed on the screen. In doing so they prevent the programmer from having full flexibility in the way in which he constructs his display.
A way around the problem is to reserve space RAMTOP for a copy of the display by entering CLEAR 58623 and to use the area to maintain a copy of the required display. A display which is SAVEd from the upper area will automatically LOAD into the display file if the SCREEN$ qualifier is used, i.e. the following pair of instructions are mutually compatible:
SAVE "name" CODE 58623,6912 LOAD "name" SCREEN$
Lindsey Round has written to say: "How do you create headerless files and save and load them on the Spectrum?"
In normal operation all Spectrum cassette files are SAVEd and LOADed in two parts. The first part is very short and specifies the name, type, length, and so on of the main part of the file. On reading the header the Spectrum checks that the name and type match that which it is looking for and displays them on the screen. The remaining data are used by the main loading routine so that it knows where to place the incoming data and how much there will be of it.
The main saving and loading routines in ROM can be called directly from a machine code routine provided the correct parameters are passed in the appropriate Z80 registers. In that way headerless files may be written and read to and from cassette.
Table 3 lists the various parameters and their interpretations and table 4 lists two routines which SAVE and LOAD the screen in that fashion. The routines can be loaded into the Spectrum using an assembler or using the simple decimal loader listed in table 5.