Machine Code Programming (part 5)

See all articles from Sinc-Link v2 n5
Author(s): Stan Piotrowski
Publication: Sinc-Link
Pub Details: Sinc-Link v2 n5 (Volume 2, Issue 5)
Pages: 4-8

On the last page you will find several piecs of information about the Z-80 chip which is the chip used in the ZX-81. They are all grouped in order that you may see the correlation between common M/C commands. There are few more things to learn before we can actually begin programming a game (or anything) in M/C.

As you learned in the last issue of our Newsletter, you can print any printable character to the screen by calling a special Print Routine within the Z-80 commands. This is fine for printing messages but it will not work for moving graphics around on the screen. There is another way and it, too, can be used to print messages but is more cumbersome and would take a great deal more patience and bytes.

Mr. Sinclair set up the ZX-81 so that 2 special addresses hold the location of the Display File (or D-File in the manual). In some computers, the display file is MEMORY MAPPED which means that always and forever, the same addresses hold the location of the screen positions. For example, in memory mapped computers, the display file may begin at location 2000 onwards to a total of the number of print positions on the screen. If you wanted to print the letter “A” in the middle of the screen, then you would add the number of characters across (in the ZX-81 it is 32) times the number of rows down. In the ZX-81, the middle is about 10 lines down. Therefore, you would add 10*32 or 320 to 2000 to print the letter “A” in the middle. But not so with the ZX-81. As I stated above, the ZX-81 is NOT memory mapped but floats around in memory and since it is a “floater”, it is a “SYSTEM VARIABLE”. If you check your manual on the System Variables, you will find that the addresses 16396 and 16397 hold the location of the beginning of the display file. If we PEEK these addresses at different times during programming, you will find that they are different each time. In actual fact, we don’t really care where the D-File is located; all we want to do is print a character somewhere on the screen but in a place decided by us. (Which means doing some mathematics).

Oh yes, there is one more hitch in the floating D-File; at the beginning, there is always a character code of 118 and at the end of each line, there is a code of 118 which means that each line (except the first line) has actually 33 spaces instead of the normally 32 we have been accustomed to. We must take this into consideration each time we print something to the screen or check the character of what’s on a particular screen location. Try this:

PRINT PEEK 16396 + 256 * PEEK 16397.

This will give you the address location of the start of D-File. If, for example, you PEEKed the above addresses and the result was 32000; PRINT PEEK 32000 and you will get 118 which means the start of D-File.

Enter this short program to check how D-File works:

10 CLS
20 PRINT "A"
30 LET X=PEEK 16396 + 256 * PEEK 16397
40 LET X=X+1
50 PRINT X

RUN

Running through the program, the screen is cleared and the letter “A” is printed at the top left corner. In line 30, “X” is the address of the beginning of the location of the display file. Since the first location is always code 118, in line 40 we add 1 to the address to get the first location of the printable part of the D-File. In line 50, we PEEK this address contained in “X” which will be code 38. If you check in your manual, the code for 38 is the letter “A”.

You should now see how this address holds the location of D-File and the fact that we let the computer look after the actual location. We don’t really need to know where the D-File is located in memory but simply PEEK the variable addresses 16396 and 16397 to find it.

Now, if we can PEEK these addresses, we can also POKE a printable character into the D-File location. Don’t forget, we don’t POKE into the addresses 16396 & 16397 but only into their PEEK value.

Therefore, let’s modify the above program slightly to POKE the letter “B” into the top left corner of our screen:

DELETE LINE 10
40 POKE X,39

RUN

The letter “B” will have appeared at the top left corner of the screen. The whole program above is in Basic so is rather slow but if it is done in machine language, it is done very quickly. In fact, it moves so fast that we will actually slow the routine down by about 10000 cycles in order that we can use the routine in a game!!! Notice, 10000 cycles. That’s like using in Basic:

FOR I = 1 TO 10000
NEXT I

I showed you this routine before but now you should see the logic in it. First of all, we find the start of the loaction of D-File, then POKE a character code into it, add 1 to the address, go back and do it again. The big thing about this program is that it will crash and you have to unplug your computer to get out of it since it doesn’t take into account all codes 118 and the end of D-File; in fact it will go on forever POKEing the codes into memory. All I’m trying to show you is how fast this routine is in comparison to the previous machine code printing routine. But, this routine is used the most in machine language programming. There is one thing more to remember and it is important: the register HL must be used whenever the above routine is being used and normally DE is used to hold the screen position. (e.g. the ZX-81 has 32 horizontal positions by 24 vertical positions or 768 possible positions that can be displayed on the screen.

Enter a REM line with 12 characters then enter the following codes:

10 REM 123456789012

42, 12, 64 :LD HL (16396)
17, 1, 0 :LD DE, 1
25 :ADD HL,DE
54, 38 :LD (HL), 38
35 :INC HL
24, 251 :JR -5

RAND USR 16514

Immediately the screen was filled with the letter “A” – so fast that you were unable to see the start or finish.

The last area at the present time before starting our “Space Game” is that of K-Scan or keyboard scan routine. We must be able to control our “Lasers” or the “Spaceship’s” movements and in order to do this, we must be able to use the keyboard, obviously.

There are many subroutines in ROM that we make extensive use of which means we don’t “REWRITE” the ROM routines and one of the routines is K-Scan. When we call this routine (in machine language: CALL 688), it returns with the value of the last key pressed in the HL register. If we Compare the values we are looking for with the values in the HL register, we can do something and if the values are not what we want, then do nothing. For example, if we wish to have the “Y” key to move our Spaceship up, we compare the value in the HL register pair to the value of the letter “Y”.

Note: it is NOT the character value of “Y” but the keyboard section where the letter “Y” is contained. If you have a copy of Toni Baker‘s book “Mastering Machine Code On Your ZX-81” (an excellent book on machine code), beginning on page 88, she shows you what the sections are about. Basically, the keyboard is divided up into several sections (hardware and software).

The top row of key is one section; the next another section, etc. Then the sections are divided vertically as well beginning with 1, Q, A and SHIFT. This is a very brief explanation of it and if anyone has added an external keyboard, this was actually done in hardware. According to Toni Baker’s book, we can use whole sections to move the spaceship. For example, the keys 1, 2, and 3 could be one type of movement. But if we wanted to get specific and have only the key labelled 1 to do something, then we must examine both the “H” and the “L” of the hl register pair. In the above sections, we need only to compare the “L” register.

I’ll let you find out what the returned value is of each key pressed in the following Basic program. You can print them out for yourself or jot them down somewhere. Generally, you will only be concerned with 5 specific keys for most games: the left, right, up, and down movements and a fire-the-missile key.

10 SCROLL
20 IF INKEY$ = "" THEN GOTO 20
30 LET X = PEEK 16421
40 LET Y = PEEK 16422
50 LET A$ = INKEY$
60 PRINT A$;": LOBYTE=";X, "HIBYTE=";Y
70 GOTO 10

What happens in the above program is that the “LOBYTE” will be what is returned in the “L” register and the “HIBYTE” will be what is returned in the “H” register. In machine code, you would CALL K-SCAN and then compare the HL register with what your expected results would be and then continue execution of the machine code program

Well that’s it for this Newsletter for Machine Code programming. In the next issue, we will begin to actually program a Space Invaders game.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.