Andrew Hewson explains UDGs from A to Z
CHRIS FOWLER of Sheffield has been using the Big Gen program, published in Sinclair User a while ago, to create new user-defined graphics characters. He writes: This is fine as far as it goes but as you know we are limited to 21 UDGs. Is there any way I can use the full alphabet, because I want to create a handwriting font?
Indeed there is. There are 96 characters in the ordinary Spectrum character set. The set starts with character code 32 the space or blank character and ends with the copyright symbol - code 127. They are defined by the contents of a table held permanently in ROM at address 15616. Each definition is held in eight bytes and so the whole table is 768 bytes long.
The method of encoding and decoding the eight bytes can be understood with the help of some knowledge of binary numbers and an imaginary 8x8 grid. Each element in the grid is called a pixel and each pixel can be set to either the Ink or the Paper colour. It is the precise arrangement of Ink and Paper coloured pixels in the 8x8 grid which creates each character.
Each of the eight bytes devoted to a character defines the setting of one horizontal line of eight pixels. The contents of a byte - which necessarily lies in the range 0 to 255 in decimal - is read as an eight digit binary number so that there is a one-to-one correspondence between pixels and binary digits. A binary number consists of zeros and ones only. All pixels for which the corresponding binary digit is zero are set to the Paper colour, whereas all pixels for which the corresponding digit is one are set to the Ink colour.
Very often, the first and last bytes of the group of eight controlling a given character are zero. Those two bytes determine the top and bottom of the character respectively, and a zero setting ensures that all the corresponding pixels are set to the Paper colour. Thus when the character appears on the screen it is well separated from other items o n the lines above and below.
For a similar reason each byte usually contains an even number which is also less than 128. As a result, all pixels at the right and left are also set to the Paper colour so that the character is easily distinguished from its fellows on either side.
The contents of the character table, being held in ROM, cannot be changed. The address of the beginning of the table is 256 more than the value held in the CHARS system variable, which is located at 23606 and 23607, and the table is only accessed by the Spectrum via CHARS. Hence the Spectrum can be forced to use a new, user-defined, character set simply by changing the values stored in CHARS.
Creating a new character set from scratch is a complicated task because the shape of each letter or digit must be worked out in detail. lt is also best done one character at a time.l recommend the following. Move RAMTOP down by 768 bytes to make room for a copy of the entire ROM character set to be stored above RAMTOP. Copy the character set byte by byte above RAMTOP and then reset CHARS to point to the base of the new table. This task is accomplished by the program listed in table one.
The program listed in table two displays the 96 characters in the set assuming they are stored at the top of memory. It then allows you to select one character, enter eight new values to be POKEd into the character set and displays the result for comparison. Just to get the ball rolling, I have also listed in table three the codes for the Greek letters alpha, beta and gamma in both upper and lower case. Try using them in the program in table two to see the effect.
|
|
|
|
Code booksMACHINE CODE programming continues to attract Sinclair users. Sadly it also continues to be a source of frustration and difficulty to some. Take this plea for help from James Broad of Oxford, for example. He writes: I'm baffled. I'm learning machine code and finding it an awful struggle, although your column is proving invaluable. What books did you use when you were learning? It is something that we all have to go through and there are not many people who take to the black art of machine code programming without putting in a lot of effort first. The books which I used are not relevant because the Z80 chip was not around then. These days I would recommend purchasing something cheap and cheerful, like Mastering Machine Code on your ZX Spectrum from Melbourne House, as a taster. lf you find that you understand the basics then move onto something more sophisticated. I recommend Z80 and 8080 Assembly Language Programming by Kathe Spracklen, published by Hayden ISBN 0-8104-5167-0. If you get stuck then go back to a Spectrum specific book. For those who really get the bit between their teeth then Ian Logan's books from Melbourne House, Understanding Your Spectrum and The Complete Spectrum ROM Disassembly, ISBN numbers 0-86161-111 and 0-86161-116, are both very useful. The most authoritative book which is also widely available in the UK is How to Program the Z80 by Rodney Zaks, ISBN number 0-89588-057-1, but don't be tempted to get it unless you have the others under your belt already. Rest assured that everyone finds the learning business tricky and those that deny it probably haven't learnt as much as they think they have. If it was easy it wouldn't be interesting. Perhaps the most inspiring way to learn is to study a few commercial games and then remind yourself that most of the people who are writing the highly sophisticated code which drives today's games knew little about machine code and nothing about the Spectrum just a few short years ago. |