Machine code colour graphics Robert Erskine shows you how to speed up your colour graphics on the ZX Spectrum Although Spectrum BASIC is fairly fast, there are many occasions when it is not nearly fast enough; particularly when you need to print large or multiple images to the screen. The most efficient way of solving this problem is to use machine code routines to do the job, for the speed of the Spectrum's microprocessor is such that several graphics, or even the entire screen, can be printed in a small fraction of a second. Although there is a routine in the ROM which enables you to do this (using an RST 10 instruction) the process can be rather long winded unless each character in the graphic follows the previous one on the screen. Fur- thermore, since this routine draws its graphics symbols from existing memory, you will always be confined to using the standard character set and user defined graphics. What would be ideal would be a program which enables you to 'read' every byte of your most complex graphic master- pieces and store them away in RAM for instant recall to any position on the screen. The two programs in this article, Grafcode and Grafprint, are designed to do exactly that. Dynamic duo Grafprint is a machine code program which builds up a graphic image on the screen, including attributes, from a data file held in memory. It can either be called from within a BASIC program or incorporated in a larger machine code program where it can be used to greater effect in generating laser fire or swooping aliens. The Demo program illustrates Grafprint at work, printing a large green moon lander at the top left of the screen. By altering lines 20 and 30 which hold the low and high byte values of the first attribute position on the screen, you will see that the image can be printed anywhere, in spite of the peculiar design of the Sinclair display file. Line 90 of Demo holds the machine code data of Grafprint and line 100 holds the data for the graphic. The Grafprint program is designed to be loaded from ad- dress 32300 to address 32411 and therefore RAMtop should be at 32299 or less. The first eight bytes are used as tempo- rary pigeon holes by the main program, which starts at 32308. Although Grafprint can be moved elsewhere in memory, it contains references to these first eight bytes and these would therefore have to be changed. Grafcode is a BASIC program which generates data files of graphics for Grafprint. To use it, first create your graphic image (or text) anywhere on the screen, preferably from the top left-hand corner where you can find the start address easily. The image may be created in the normal way, using BASIC colour commands, user defined graphics and so on. Then, when it is ready, activate Grafcode by entering the command GO TO 9600. On no account RUN the program or your splendid new handiwork will disappear without trace. What's in store? You will then be prompted to input the address from which you want the graphics data to be stored, the /first/ attri- bute address currently occupied by the graphic and the number of character squares in the graphic. You will then be asked to input each of the displacements between the characters. These refer to attribute file displacements so that one square to the right = 1, one square below = 32, and so on. You may then sit back and hum a little tune for a few seconds whilst Grafcode translates your handiwork into a sequential data file which can be accessed by Grafprint. Having loaded Grafprint and prepared your data file, the next step is to load the start address if the data file into 32302/3 and the attribute address of the chosen screen location into 32300/1. Obviously in a moving graphics program, this would be done in machine code. To print the graphic, simply enter the command RANDOMIZE USR 32308. Grafprint works by moving a pointer around the attri- butes file of the Spectrum and loading the attributes of the current character into an address, followed by the corresponding eight display file bytes for each character. The data file which it reads is formatted so that each character is represented by 10 bytes. The first byte holds the displacement between the current character and the one before, the second holds the attributes code and the remaining eight hold the graphic image. The data for the first character in a graphic also uses 10 bytes but instead of starting with a displacement value, it holds the total number of characters in the graphic. The program incorporates a routine which calculates which of the three screen zones the current character will occupy and selects the corresponding display addresses accordingly. This ensures that if the graphic crosses a border between zones then the relative positions of the characters are maintained. The sky's the limit There is no limit to the size of the data file which can be used by Grafprint, other than the size of your machine's memory, and if files are built up section by section, there is no limit to the different shapes you can store. Having used 21 user defined graphics in a picture you can convert it to data using Grafcode, store it temporarily on tape and continue with a new set of graphics, bringing the whole lot together in one file when you have finished. Graphics in- volving circles and other shapes can equally be stored. If you intend to move the graphics rapidly around the screen using Grafprint, it will be necessary to erase each preceding image before printing the next. One way of doing this is to use a machine code equivalent of PRINT OVER, say by holding a blank graphic of the same size and shape as the original and using Grafprint to print it over the top. In some cases, it will be sufficient to clear the screen between each printing because the speed of machine code is such that the illusion of continuous action will be main- tained. Screen clearing can be achieved by filling all the display file addresses with zeros and all the attribute addresses with an appropriate code. If the background is complex and you don't wish to clear it between moves, you can store a complete copy of the screen above RAMtop using a block move routine in machine code and reprint it to the screen between moves by means of a similar routine. Since this technique uses up nearly seven thousand bytes of RAM it is hardly worth contemplating on a 16K Spectrum, unless the rest of your program is entirely in machine code.