## Basic Alien Collision Detection

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Coordinate Checking

Coordinate checking should be self-explanatory to most programmers, but is included here for the sake of completeness.  It is also the next step in the development of our Centipede game.

The simplest type of collision detection would be something like this, used to detect if two UDGs have collided:

```       ld a,(playx)        ; player's x coordinate.
cp (ix+1)           ; compare with alien x.
ret nz              ; not the same, no collision.
ld a,(playy)        ; player's y coordinate.
cp (ix+2)           ; compare with alien y.
ret nz              ; not the same, no collision.
jp collis           ; we have a collision.```

Okay, so that’s pretty simple but most games don’t use single-cell character graphics.  What if the aliens are four character cells wide by two high, and the player’s character is three squares high by three wide?  We need to check if any part of the alien has collided with any part of the player, so we need to check that the coordinates are within a certain range.  If the alien is less than two squares above the player, or less than 3 below him then the vertical coordinates match.  If the alien is also less than four squares to the left of the player, and less than three squares to the right then the horizontal position also matches and we have a collision.

Let’s write some code to do this.  We can start by taking the player’s vertical coordinate:

`       ld a,(playx)        ; player's x coordinate.`

Then subtract the alien’s vertical position:

`      sub (ix+1)          ; subtract alien x.`

Next, subtract one from the player’s height, and add it.

`      add a,2             ; player is 3 high, so add 3 - 1 = 2.`

If the alien is within range the result will be less than the combined height of the player and alien, so we perform the check:

```       cp 5                ; combined heights are 3 + 2 = 5.
ret nc              ; not within vertical range.```

Similarly, we can follow this with the code for the horizontal check:

```       ld a,(playy)        ; player's y coordinate.
sub (ix+2)          ; subtract alien y.
add a,2             ; player is 3 wide, so add 3 - 1 = 2.
cp 7                ; combined widths are 3 + 4 = 7.
ret nc              ; not within horizontal range.
jp collis           ; we have a collision.```

Of course, this method doesn’t just work for character-based graphics, it works perfectly well with sprites too, but more of those later. It’s time to finish our game with some collision detection. As our graphics are all single-character UDGs we don’t need anything fancy, a quick x=x and y=y check are all we need.

``` We want a black screen.

ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23693),a        ; set our screen colours.
xor a               ; quick way to load accumulator with zero.
call 8859           ; set permanent border colours.

; Set up the graphics.

ld hl,blocks        ; address of user-defined graphics data.
ld (23675),hl       ; make UDGs point to it.

; Okay, let's start the game.

call 3503           ; ROM routine - clears screen, opens chan 2.

xor a               ; zeroise accumulator.
ld (dead),a         ; set flag to say player is alive.

; Initialise coordinates.

ld hl,21+15*256     ; load hl pair with starting coords.
ld (plx),hl         ; set player coords.
ld hl,255+255*256   ; player's bullets default.
ld (pbx),hl         ; set bullet coords.

ld b,10             ; number of segments to initialise.
ld hl,segmnt        ; segment table.
segint ld (hl),1           ; start off moving right.
inc hl
ld (hl),0           ; start at top.
inc hl
ld (hl),b           ; use B register as y coordinate.
inc hl
djnz segint         ; repeat until all initialised.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player base symbol.

; Now we want to fill the play area with mushrooms.

ld a,68             ; green ink (4) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary colours.
mushlp ld a,22             ; control code for AT character.
rst 16
call random         ; get a 'random' number.
and 15              ; want vertical in range 0 to 15.
rst 16
call random         ; want another pseudo-random number.
and 31              ; want horizontal in range 0 to 31.
rst 16
ld a,145            ; UDG 'B' is the mushroom graphic.
rst 16              ; put mushroom on screen.
djnz mushlp         ; loop back until all mushrooms displayed.

; This is the main loop.

mloop  equ \$

; Delete the player.

call basexy         ; set the x and y positions of the player.
call wspace         ; display space over player.

; Now we've deleted the player we can move him before redisplaying him
; at his new coordinates.

ld bc,63486         ; keyboard row 1-5/joystick port 2.
in a,(c)            ; see what keys are pressed.
rra                 ; outermost bit = key 1.
push af             ; remember the value.
call nc,mpl         ; it's being pressed, move left.
pop af              ; restore accumulator.
rra                 ; next bit along (value 2) = key 2.
push af             ; remember the value.
call nc,mpr         ; being pressed, so move right.
pop af              ; restore accumulator.
rra                 ; next bit (value 4) = key 3.
push af             ; remember the value.
call nc,mpd         ; being pressed, so move down.
pop af              ; restore accumulator.
rra                 ; next bit (value 8) reads key 4.
push af             ; remember the value.
call nc,mpu         ; it's being pressed, move up.
pop af              ; restore accumulator.
rra                 ; last bit (value 16) reads key 5.
call nc,fire        ; it's being pressed, move up.

; Now he's moved we can redisplay the player.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player.

; Now for the bullet.  First let's check to see if it's hit anything.

call bchk           ; check bullet position.

call dbull          ; delete bullets.
call moveb          ; move bullets.
call bchk           ; check new position of bullets.
call pbull          ; print bullets at new position.

; Now for the centipede segments.

ld ix,segmnt        ; table of segment data.
ld b,10             ; number of segments in table.
censeg push bc
ld a,(ix)           ; is segment switched on?
inc a               ; 255=off, increments to zero.
call nz,proseg      ; it's active, process segment.
pop bc
ld de,3             ; 3 bytes per segment.
add ix,de           ; get next segment in ix registers.
djnz censeg         ; repeat for all segments.

halt                ; delay.

ld a,(dead)         ; was the player killed by a segment?
and a
ret nz              ; player killed - lose a life.

; Jump back to beginning of main loop.

jp mloop

; Move player left.

mpl    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
and a               ; is it zero?
ret z               ; yes - we can't go any further left.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
dec b               ; look 1 square to the left.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

dec (hl)            ; subtract 1 from y coordinate.
ret

; Move player right.

mpr    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
cp 31               ; is it at the right edge (31)?
ret z               ; yes - we can't go any further left.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
inc b               ; look 1 square to the right.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

inc (hl)            ; add 1 to y coordinate.
ret

; Move player up.

mpu    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 4                ; is it at upper limit (4)?
ret z               ; yes - we can go no further then.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
dec c               ; look 1 square up.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

dec (hl)            ; subtract 1 from x coordinate.
ret

; Move player down.

mpd    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 21               ; is it already at the bottom (21)?
ret z               ; yes - we can't go down any more.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
inc c               ; look 1 square down.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

inc (hl)            ; add 1 to x coordinate.
ret

; Fire a missile.

fire   ld a,(pbx)          ; bullet vertical coord.
inc a               ; 255 is default value, increments to zero.
ret nz              ; bullet on screen, can't fire again.
ld hl,(plx)         ; player coordinates.
dec l               ; 1 square higher up.
ld (pbx),hl         ; set bullet coords.
ret

bchk   ld a,(pbx)          ; bullet vertical.
inc a               ; is it at 255 (default)?
ret z               ; yes, no bullet on screen.
ld bc,(pbx)         ; get coords.
call atadd          ; find attribute here.
cp 68               ; mushrooms are bright (64) + green (4).
jr z,hmush          ; hit a mushroom!
ret

hmush  ld a,22             ; AT control code.
rst 16
ld a,(pbx)          ; bullet vertical.
rst 16
ld a,(pby)          ; bullet horizontal.
rst 16
call wspace         ; set INK colour to white.
kilbul ld a,255            ; x coord of 255 = switch bullet off.
ld (pbx),a          ; destroy bullet.
ret

; Move the bullet up the screen 1 character position at a time.

moveb  ld a,(pbx)          ; bullet vertical.
inc a               ; is it at 255 (default)?
ret z               ; yes, no bullet on screen.
sub 2               ; 1 row up.
ld (pbx),a
ret

; Set up the x and y coordinates for the player's gunbase position,
; this routine is called prior to display and deletion of gunbase.

basexy ld a,22             ; AT code.
rst 16
ld a,(plx)          ; player vertical coord.
rst 16              ; set vertical position of player.
ld a,(ply)          ; player's horizontal position.
rst 16              ; set the horizontal coord.
ret

; Show player at current print position.

splayr ld a,69             ; cyan ink (5) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,144            ; ASCII code for User Defined Graphic 'A'.
rst 16              ; draw player.
ret

pbull  ld a,(pbx)          ; bullet vertical.
inc a               ; is it at 255 (default)?
ret z               ; yes, no bullet on screen.
call bullxy
ld a,16             ; INK control char.
rst 16
ld a,6              ; 6 = yellow.
rst 16
ld a,147            ; UDG 'D' is used for player bullets.
rst 16
ret

dbull  ld a,(pbx)          ; bullet vertical.
inc a               ; is it at 255 (default)?
ret z               ; yes, no bullet on screen.
call bullxy         ; set up bullet coordinates.
wspace ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,32             ; SPACE character.
rst 16              ; display space.
ret

; Set up the x and y coordinates for the player's bullet position,
; this routine is called prior to display and deletion of bullets.

bullxy ld a,22             ; AT code.
rst 16
ld a,(pbx)          ; player bullet vertical coord.
rst 16              ; set vertical position of player.
ld a,(pby)          ; bullet's horizontal position.
rst 16              ; set the horizontal coord.
ret

segxy  ld a,22             ; ASCII code for AT character.
rst 16              ; display AT code.
ld a,(ix+1)         ; get segment x coordinate.
rst 16              ; display coordinate code.
ld a,(ix+2)         ; get segment y coordinate.
rst 16              ; display coordinate code.
ret

proseg call segcol         ; segment collision detection.
ld a,(ix)           ; check if segment was switched off
inc a               ; by collision detection routine.
ret z               ; it was, so this segment is now dead.
call segxy          ; set up segment coordinates.
call wspace         ; display a space, white ink on black.
call segmov         ; move segment.
call segcol         ; new segment position collision check.
ld a,(ix)           ; check if segment was switched off
inc a               ; by collision detection routine.
ret z               ; it was, so this segment is now dead.
call segxy          ; set up segment coordinates.
ld a,2              ; attribute code 2 = red segment.
ld (23695),a        ; set temporary attributes.
ld a,146            ; UDG 'C' to display segment.
rst 16
ret
segmov ld a,(ix+1)         ; x coord.
ld c,a              ; GP x area.
ld a,(ix+2)         ; y coord.
ld b,a              ; GP y area.
ld a,(ix)           ; status flag.
and a               ; is the segment heading left?
jr z,segml          ; going left - jump to that bit of code.

; so segment is going right then!

segmr  ld a,(ix+2)         ; y coord.
cp 31               ; already at right edge of screen?
jr z,segmd          ; yes - move segment down.
inc a               ; look right.
ld b,a              ; set up GP y coord.
cp 68               ; mushrooms are bright (64) + green (4).
jr z,segmd          ; mushroom to right, move down instead.
inc (ix+2)          ; no obstacles, so move right.
ret

; so segment is going left then!

segml  ld a,(ix+2)         ; y coord.
and a               ; already at left edge of screen?
jr z,segmd          ; yes - move segment down.
dec a               ; look right.
ld b,a              ; set up GP y coord.
cp 68               ; mushrooms are bright (64) + green (4).
jr z,segmd          ; mushroom to left, move down instead.
dec (ix+2)          ; no obstacles, so move left.
ret

; so segment is going down then!

segmd  ld a,(ix)           ; segment direction.
xor 1               ; reverse it.
ld (ix),a           ; store new direction.
ld a,(ix+1)         ; y coord.
cp 21               ; already at bottom of screen?
jr z,segmt          ; yes - move segment to the top.

; At this point we're moving down regardless of any mushrooms that
; may block the segment's path.  Anything in the segment's way will
; be obliterated.

inc (ix+1)          ; haven't reached the bottom, move down.
ret

; moving segment to the top of the screen.

segmt  xor a               ; same as ld a,0 but saves 1 byte.
ld (ix+1),a         ; new x coordinate = top of screen.
ret

; Segment collision detection.
; Checks for collisions with player and player's bullets.

segcol ld a,(ply)          ; bullet y position.
cp (ix+2)           ; is it identical to segment y coord?
jr nz,bulcol        ; y coords differ, try bullet instead.
ld a,(plx)          ; player x coord.
cp (ix+1)           ; same as segment?
jr nz,bulcol        ; x coords differ, try bullet instead.

; So we have a collision with the player.

killpl ld (dead),a         ; set flag to say that player is now dead.
ret

; Let's check for a collision with the player's bullet.

bulcol ld a,(pbx)          ; bullet x coords.
inc a               ; at default value?
ret z               ; yes, no bullet to check for.
cp (ix+1)           ; is bullet x coord same as segment x coord?
ret nz              ; no, so no collision.
ld a,(pby)          ; bullet y position.
cp (ix+2)           ; is it identical to segment y coord?
ret nz              ; no - no collision this time.

; So we have a collision with the player's bullet.

call dbull          ; delete bullet.
ld a,22             ; AT code.
rst 16
ld a,(pbx)          ; player bullet vertical coord.
inc a               ; 1 line down.
rst 16              ; set vertical position of mushroom.
ld a,(pby)          ; bullet's horizontal position.
rst 16              ; set the horizontal coord.
ld a,16             ; ASCII code for INK control.
rst 16
ld a,4              ; 4 = colour green.
rst 16              ; we want all mushrooms in this colour!
ld a,145            ; UDG 'B' is the mushroom graphic.
rst 16              ; put mushroom on screen.
call kilbul         ; kill the bullet.
ld (ix),a           ; kill the segment.
ld hl,numseg        ; number of segments.
dec (hl)            ; decrement it.
ret

; Simple pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning
; the contents of the byte at that location.

random ld hl,(seed)        ; Pointer
ld a,h
and 31              ; keep it within first 8k of ROM.
ld h,a
ld a,(hl)           ; Get "random" number from location.
inc hl              ; Increment pointer.
ld (seed),hl
ret
seed   defw 0

; Calculate address of attribute for character at (dispx, dispy).

atadd  ld a,c              ; vertical coordinate.
rrca                ; multiply by 32.
rrca                ; Shifting right with carry 3 times is
rrca                ; quicker than shifting left 5 times.
ld e,a
and 3
ld d,a
ld a,e
and 224
ld e,a
ld a,b              ; horizontal position.
ld e,a              ; de=address of attributes.
ld a,(de)           ; return with attribute in accumulator.
ret

plx    defb 0              ; player's x coordinate.
ply    defb 0              ; player's y coordinate.
pbx    defb 255            ; player's bullet coordinates.
pby    defb 255

; UDG graphics.

blocks defb 16,16,56,56,124,124,254,254    ; player base.
defb 24,126,255,255,60,60,60,60     ; mushroom.
defb 24,126,126,255,255,126,126,24  ; segment.
defb 0,102,102,102,102,102,102,0    ; player bullet.

; Table of segments.
; Format: 3 bytes per entry, 10 segments.
; byte 1: 255=segment off, 0=left, 1=right.
; byte 2 = x (vertical) coordinate.
; byte 3 = y (horizontal) coordinate.

segmnt defb 0,0,0          ; segment 1.
defb 0,0,0          ; segment 2.
defb 0,0,0          ; segment 3.
defb 0,0,0          ; segment 4.
defb 0,0,0          ; segment 5.
defb 0,0,0          ; segment 6.
defb 0,0,0          ; segment 7.
defb 0,0,0          ; segment 8.
defb 0,0,0          ; segment 9.
defb 0,0,0          ; segment 10.```

But wait, why are there two tests for collision detection instead of one? Well, imagine the player’s gunbase is one character cell to the left of a centipede segment. The player is moving right and the segment is moving left. In the next frame the segment would move into the cell occupied by the player, and the player would move into the position occupied by the segment in the previous frame – player and centipede segment would move straight through each other and a single collision detection check would fail to pick this up. By checking for a collision after the player moves, and then again after the centipede segments have moved we can avoid the problem.

Collisions Between Sprites

Fair enough, most Spectrum games use sprites rather than UDGs so in the next chapter we shall see how sprites may be drawn.  For collision detection, the same principle of coordinate checking can be used to detect collisions between sprites.  Subtract the first sprite’s coordinates from those of the second, examine the difference and if it’s within the size range of the two sprites combined we have a collision on that axis.  A simple collision check for two 16×16 pixel sprites might look something like this:

```; Check (l, h) for collision with (c, b), strict enforcement.

colx16 ld a,l              ; x coord.
sub c               ; subtract x.
cp 31               ; within x range?
ret nc              ; no - they've missed.
ld a,h              ; y coord.
sub b               ; subtract y.
cp 31               ; within y range?
ret                 ; carry flag set if there's a collision.```

There is a drawback with this method. If your sprites don’t entirely fill their 16×16 pixel boundaries then the collision detection will appear to be too strict, and collisions will happen when sprites are close together but not actually touching. A slightly less sensitive check would involve clipping the corners of the sprites into a more octagonal shape, particularly if your sprites have rounded corners. The routine below works by adding the x and y coordinate differences and checking that they are below a certain limit. For a collision between two 16×16 sprites the maximum coordinate distances are 15 pixels for each axis, so by checking that the x and y differences are 25 or less we are effectively shaving a 5x5x5 pixel triangle from each corner.

```; Check (l, h) for collision with (c, b), cutting corners.

colc16 ld a,l              ; x coord.
sub c               ; subtract x.
jr nc,colc1a        ; result is positive.
neg                 ; make negative positive.
colc1a cp 16               ; within x range?
ret nc              ; no - they've missed.
ld e,a              ; store difference.

ld a,h              ; y coord.
sub b               ; subtract y.
jr nc,colc1b        ; result is positive.
neg                 ; make negative positive.
colc1b cp 16               ; within y range?
ret nc              ; no - they've missed.

cp 26               ; only 5 corner pixels touching?
ret                 ; carry set if there's a collision.```

## How To Write ZX Spectrum Games – Chapter 6

Tables

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Aliens Don’t Come One at a Time

Let us say, for the sake of example, we were writing a Space Invaders game featuring eleven columns, each containing five rows of invaders.  It would be impractical to write the code for each of the fifty-five aliens in turn, so we need to set up a table.  In Sinclair BASIC we might go about this by defining three arrays of fifty-five elements – one for the invaders’ x coordinates, one for y coordinates, plus a third status byte.  We could do something similar in assembler by setting up three tables of fifty-five bytes each in memory, then adding the number for each alien to the start of each table to access the individual element.  Unfortunately, that would be slow and cumbersome.

A far better method is to group the three data elements for each invader into a structure, and then have fifty-five of these structures in a table.  We can then point hl to the address of each invader, and know that hl points to the status byte, hl plus one points to the x coordinate, and hl plus two points to the y coordinate.  The code to display an alien might look something like this:

```       ld hl,aliens        ; alien data structures.
ld b,55             ; number of aliens.
loop0  call show           ; show this alien.
djnz loop0          ; repeat for all aliens.
ret
show   ld a,(hl)           ; fetch alien status.
cp 255              ; is alien switched off?
jr z,next           ; yes, so don't display him.
push hl             ; store alien address on the stack.
inc hl              ; point to x coord.
ld d,(hl)           ; get coord.
inc hl              ; point to y coord.
ld e,(hl)           ; get coord.
call disply         ; display alien at (d,e).
pop hl              ; retrieve alien address from the stack.
next   ld de,3             ; size of each alien table entry.
add hl,de           ; point to next alien.
ret                 ; leave hl pointing to next one.```

Using the Index Registers

The drawback with this routine is that we have to be very careful where hl is pointing to all the time, so it might be an idea to store hl in a two-byte temporary memory location before calling show, then restoring it afterwards, adding three at the end of the main loop, then performing the djnz instruction.  If we were writing for the Nintendo GameBoy with its cut-down Z80 this would probably represent our best option.  On machines with more advanced processors such as the Spectrum and CPC464 we can use the index registers, ix, to simplify our code a little.  Because the ix register pair allows us to displace our indirect addressing, we can point ix to the beginning of an alien’s data structure and access all elements within it without the need to change ix again.  Using ix our alien display routine might look like this:

```       ld ix,aliens        ; alien data structures.
ld b,55             ; number of aliens.
loop0  call show           ; show this alien.
ld de,3             ; size of each alien table entry.
add ix,de           ; point to next alien.
djnz loop0          ; repeat for all aliens.
ret
show   ld a,(ix)           ; fetch alien status.
cp 255              ; is alien switched off?
ret z               ; yes, so don't display him.
ld d,(ix+1)         ; get coord.
ld e,(ix+2)         ; get coord.
jp disply           ; display alien at (d,e).```

Using ix means we only ever need to point to the beginning of an alien’s data structure, so ix will always return the status for the current invader, ix+1 the x coordinate, and so on.  This method enables the programmer to use complex data structures for his aliens of up to 128 bytes long, without getting confused as to which bit of the structure our registers are pointing at any given time as with the hl example earlier.  Unfortunately, using ix is a little slower than hl, so we shouldn’t use it for the more intensive processing tasks such as manipulating graphics.

Let us apply this method to our Centipede game.  Firstly, we need to decide how many segments are needed, and what data to store about each segment.  In our game the segments will need to move left or right until they hit a mushroom, then move down and go back the other way.  So it seems we will need a flag to indicate the particular direction a segment is travelling in, plus an x or y coordinate.  Our flag can also be used to indicate that a particular segment has been destroyed.  With this in mind we can set up a data structure of three bytes:

```centf  defb 0              ; flag, 0=left, 1=right, 255=dead.
centx  defb 0              ; segment x coordinate.
centy  defb 0              ; segment y coordinate.```

If we choose to have ten segments in our centipede, we need to reserve a table space of thirty bytes. Each segment needs to be initialised at the beginning, then deleted, moved and redisplayed during the game.

Initialising our segments is probably the simplest task, so we can use a simple loop incrementing the hl register pair for each byte before setting it.  Something like this will usually do the trick:

```       ld b,10             ; number of segments to initialise.
ld hl,segmnt        ; segment table.
segint ld (hl),1           ; start off moving right.
inc hl
ld (hl),0           ; start at top.
inc hl
ld (hl),b           ; use B register as y coordinate.
inc hl
djnz segint         ; repeat until all initialised.

```

Processing and displaying each segment is going to be slightly more complicated, so for that we will use the ix registers.  We need to write a simple algorithm which manipulates a single segment left or right until it hits a mushroom, then moves down and switches direction.  We’ll call this routine proseg (for “process segment”), and set up a loop which points to each segment in turn and calls proseg.  Providing we get the movement algorithm correct we should then see a centipede snaking its way through the mushrooms.  Applying this to our code is straightforward – we check the flag byte for each segment (ix) to see which way the segment is moving, increment or decrement the horizontal coordinate (ix+2) accordingly, then check the attribute at that character cell.  If it’s green and black we increment the vertical coordinate (ix+1), and switch the direction flag (ix).

Okay, there are one or two other things to consider, such as hitting the sides or bottom of the screen, but that’s just a case of checking the segment’s coordinates and switching direction or moving to the top of the screen when we need to.  The segments also need to be deleted from their old positions prior to being moved, the redisplayed at their new positions, but we have already covered the steps required to perform those tasks.

Our new code looks like this:

```; We want a black screen.

ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23693),a        ; set our screen colours.
xor a               ; quick way to load accumulator with zero.
call 8859           ; set permanent border colours.

; Set up the graphics.

ld hl,blocks        ; address of user-defined graphics data.
ld (23675),hl       ; make UDGs point to it.

; Okay, let's start the game.

call 3503           ; ROM routine - clears screen, opens chan 2.

; Initialise coordinates.

ld hl,21+15*256     ; load hl pair with starting coords.
ld (plx),hl         ; set player coords.

ld b,10             ; number of segments to initialise.
ld hl,segmnt        ; segment table.
segint ld (hl),1           ; start off moving right.
inc hl
ld (hl),0           ; start at top.
inc hl
ld (hl),b           ; use B register as y coordinate.
inc hl
djnz segint         ; repeat until all initialised.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player base symbol.

; Now we want to fill the play area with mushrooms.

ld a,68             ; green ink (4) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary colours.
mushlp ld a,22             ; control code for AT character.
rst 16
call random         ; get a 'random' number.
and 15              ; want vertical in range 0 to 15.
rst 16
call random         ; want another pseudo-random number.
and 31              ; want horizontal in range 0 to 31.
rst 16
ld a,145            ; UDG 'B' is the mushroom graphic.
rst 16              ; put mushroom on screen.
djnz mushlp         ; loop back until all mushrooms displayed.

; This is the main loop.

mloop  equ \$

; Delete the player.

call basexy         ; set the x and y positions of the player.
call wspace         ; display space over player.

; Now we've deleted the player we can move him before redisplaying him
; at his new coordinates.

ld bc,63486         ; keyboard row 1-5/joystick port 2.
in a,(c)            ; see what keys are pressed.
rra                 ; outermost bit = key 1.
push af             ; remember the value.
call nc,mpl         ; it's being pressed, move left.
pop af              ; restore accumulator.
rra                 ; next bit along (value 2) = key 2.
push af             ; remember the value.
call nc,mpr         ; being pressed, so move right.
pop af              ; restore accumulator.
rra                 ; next bit (value 4) = key 3.
push af             ; remember the value.
call nc,mpd         ; being pressed, so move down.
pop af              ; restore accumulator.
rra                 ; next bit (value 8) reads key 4.
call nc,mpu         ; it's being pressed, move up.

; Now he's moved we can redisplay the player.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player.

; Now for the centipede segments.

ld ix,segmnt        ; table of segment data.
ld b,10             ; number of segments in table.
censeg push bc
ld a,(ix)           ; is segment switched on?
inc a               ; 255=off, increments to zero.
call nz,proseg      ; it's active, process segment.
pop bc
ld de,3             ; 3 bytes per segment.
add ix,de           ; get next segment in ix registers.
djnz censeg         ; repeat for all segments.

halt                ; delay.

; Jump back to beginning of main loop.

jp mloop

; Move player left.

mpl    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
and a               ; is it zero?
ret z               ; yes - we can't go any further left.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
dec b               ; look 1 square to the left.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

dec (hl)            ; subtract 1 from y coordinate.
ret

; Move player right.

mpr    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
cp 31               ; is it at the right edge (31)?
ret z               ; yes - we can't go any further left.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
inc b               ; look 1 square to the right.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

inc (hl)            ; add 1 to y coordinate.
ret

; Move player up.

mpu    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 4                ; is it at upper limit (4)?
ret z               ; yes - we can go no further then.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
dec c               ; look 1 square up.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

dec (hl)            ; subtract 1 from x coordinate.
ret

; Move player down.

mpd    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 21               ; is it already at the bottom (21)?
ret z               ; yes - we can't go down any more.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
inc c               ; look 1 square down.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

inc (hl)            ; add 1 to x coordinate.
ret

; Set up the x and y coordinates for the player's gunbase position,
; this routine is called prior to display and deletion of gunbase.

basexy ld a,22             ; AT code.
rst 16
ld a,(plx)          ; player vertical coord.
rst 16              ; set vertical position of player.
ld a,(ply)          ; player's horizontal position.
rst 16              ; set the horizontal coord.
ret

; Show player at current print position.

splayr ld a,69             ; cyan ink (5) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,144            ; ASCII code for User Defined Graphic 'A'.
rst 16              ; draw player.
ret

wspace ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,32             ; SPACE character.
rst 16              ; display space.
ret

segxy  ld a,22             ; ASCII code for AT character.
rst 16              ; display AT code.
ld a,(ix+1)         ; get segment x coordinate.
rst 16              ; display coordinate code.
ld a,(ix+2)         ; get segment y coordinate.
rst 16              ; display coordinate code.
ret

proseg ld a,(ix)           ; check if segment was switched off
inc a               ; by collision detection routine.
ret z               ; it was, so this segment is now dead.
call segxy          ; set up segment coordinates.
call wspace         ; display a space, white ink on black.
call segmov         ; move segment.
ld a,(ix)           ; check if segment was switched off
inc a               ; by collision detection routine.
ret z               ; it was, so this segment is now dead.
call segxy          ; set up segment coordinates.
ld a,2              ; attribute code 2 = red segment.
ld (23695),a        ; set temporary attributes.
ld a,146            ; UDG 'C' to display segment.
rst 16
ret
segmov ld a,(ix+1)         ; x coord.
ld c,a              ; GP x area.
ld a,(ix+2)         ; y coord.
ld b,a              ; GP y area.
ld a,(ix)           ; status flag.
and a               ; is the segment heading left?
jr z,segml          ; going left - jump to that bit of code.

; so segment is going right then!

segmr  ld a,(ix+2)         ; y coord.
cp 31               ; already at right edge of screen?
jr z,segmd          ; yes - move segment down.
inc a               ; look right.
ld b,a              ; set up GP y coord.
cp 68               ; mushrooms are bright (64) + green (4).
jr z,segmd          ; mushroom to right, move down instead.
inc (ix+2)          ; no obstacles, so move right.
ret

; so segment is going left then!

segml  ld a,(ix+2)         ; y coord.
and a               ; already at left edge of screen?
jr z,segmd          ; yes - move segment down.
dec a               ; look right.
ld b,a              ; set up GP y coord.
cp 68               ; mushrooms are bright (64) + green (4).
jr z,segmd          ; mushroom to left, move down instead.
dec (ix+2)          ; no obstacles, so move left.
ret

; so segment is going down then!

segmd  ld a,(ix)           ; segment direction.
xor 1               ; reverse it.
ld (ix),a           ; store new direction.
ld a,(ix+1)         ; y coord.
cp 21               ; already at bottom of screen?
jr z,segmt          ; yes - move segment to the top.

; At this point we're moving down regardless of any mushrooms that
; may block the segment's path.  Anything in the segment's way will
; be obliterated.

inc (ix+1)          ; haven't reached the bottom, move down.
ret

; moving segment to the top of the screen.

segmt  xor a               ; same as ld a,0 but saves 1 byte.
ld (ix+1),a         ; new x coordinate = top of screen.
ret

; Simple pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning
; the contents of the byte at that location.

random ld hl,(seed)        ; Pointer
ld a,h
and 31              ; keep it within first 8k of ROM.
ld h,a
ld a,(hl)           ; Get "random" number from location.
inc hl              ; Increment pointer.
ld (seed),hl
ret
seed   defw 0

; Calculate address of attribute for character at (dispx, dispy).

atadd  ld a,c              ; vertical coordinate.
rrca                ; multiply by 32.
rrca                ; Shifting right with carry 3 times is
rrca                ; quicker than shifting left 5 times.
ld e,a
and 3
ld d,a
ld a,e
and 224
ld e,a
ld a,b              ; horizontal position.
ld e,a              ; de=address of attributes.
ld a,(de)           ; return with attribute in accumulator.
ret

plx    defb 0              ; player's x coordinate.
ply    defb 0              ; player's y coordinate.

; UDG graphics.

blocks defb 16,16,56,56,124,124,254,254    ; player base.
defb 24,126,255,255,60,60,60,60     ; mushroom.
defb 24,126,126,255,255,126,126,24  ; segment.

; Table of segments.
; Format: 3 bytes per entry, 10 segments.
; byte 1: 255=segment off, 0=left, 1=right.
; byte 2 = x (vertical) coordinate.
; byte 3 = y (horizontal) coordinate.

segmnt defb 0,0,0          ; segment 1.
defb 0,0,0          ; segment 2.
defb 0,0,0          ; segment 3.
defb 0,0,0          ; segment 4.
defb 0,0,0          ; segment 5.
defb 0,0,0          ; segment 6.
defb 0,0,0          ; segment 7.
defb 0,0,0          ; segment 8.
defb 0,0,0          ; segment 9.
defb 0,0,0          ; segment 10.
```

## How To Write ZX Spectrum Games – Chapter 5

Simple Background Collision Detection

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Finding Attributes

Anyone who ever spent time programming in Sinclair BASIC may well remember the ATTR function. This was a way to detect the colour attributes of any particular character cell on the screen, and though tricky for the BASIC programmer to grasp, could be very handy for simple collision detection. The method was so useful in fact that it its machine language equivalent was employed by a number of commercial games, and it is of great use to the novice Spectrum programmer.

There are two ways to find the colour attribute settings for a particular character cell on the Spectrum. A quick look through the Spectrum’s ROM disassembly reveals a routine at address 9603 which will do the job for us, or we can calculate the memory address ourselves.

The simplest way to find an attribute value is to use a couple of ROM routines:

```       ld bc,(ballx)       ; put x and y in bc register pair.
call 9603           ; call ROM to put attribute (c,b) on stack.
call 11733          ; put attributes in accumulator.```

However, it is much faster to do the calculation ourselves. It is also useful to calculate an attribute’s address, and not just its value, in case we want to write to it as well.

Unlike the Spectrum’s awkward pixel layout, colour cells, located at addresses 22528 to 23295 inclusive, are arranged sequentially in RAM as one would expect. In other words, the screen’s top 32 attribute cells are located at addresses 22528 to 22559 going left to right, the second row of colour cells from 22560 to 22591 and so on. To find the address of a colour cell at print position (x,y) we therefore need only to multiply x by 32, add y, then add 22528 to the result. By then examining the contents of this address we can find out the colours displayed at a particular position, and act accordingly. The following example calculates the address of an attribute at character position (b,c) and returns it in the HL register pair.

```; Calculate address of attribute for character at (b, c).

atadd  ld a,b              ; x position.
rrca                ; multiply by 32.
rrca
rrca
ld l,a              ; store away in l.
and 3               ; mask bits for high byte.
add a,88            ; 88*256=22528, start of attributes.
ld h,a              ; high byte done.
ld a,l              ; get x*32 again.
and 224             ; mask low byte.
ld l,a              ; put in l.
ld a,c              ; get y displacement.
ld l,a              ; hl=address of attributes.
ld a,(hl)           ; return attribute in a.
ret```

Interrogating the contents of the byte at hl will give the attribute’s value, while writing to the memory location at hl will change the colour of the square.

To make sense of the result we have to know that each attribute is made up of 8 bits which are arranged in this manner:

```d0-d2		ink colour 0-7,			0=black, 1=blue, 2=red, 3=magenta,
4=green, 5=cyan, 6=yellow, 7=white
d3-d5		paper colour 0-7,		0=black, 1=blue, 2=red, 3=magenta,
4=green, 5=cyan, 6=yellow, 7=white
d6		bright,				0=dull, 1=bright
d7		flash,				0=stable, 1=flashing```

The test for green paper for example, might involve:

```       and 56              ; mask away all but paper bits.
cp 32               ; is it green(4) * 8?
jr z,green          ; yes, do green thing.```

while checking for yellow ink could be done like this:

```       and 7               ; only want bits pertaining to ink.
cp 6                ; is it yellow (6)?
jr z,yellow         ; yes, do yellow wotsit.```

Applying what we Have Learned to the Game

We can now add an attribute collision check to our Centipede game. As before, the new sections are underlined.

```; We want a black screen.

ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23693),a        ; set our screen colours.
xor a               ; quick way to load accumulator with zero.
call 8859           ; set permanent border colours.

; Set up the graphics.

ld hl,blocks        ; address of user-defined graphics data.
ld (23675),hl       ; make UDGs point to it.

; Okay, let's start the game.

call 3503           ; ROM routine - clears screen, opens chan 2.

; Initialise coordinates.

ld hl,21+15*256     ; load hl pair with starting coords.
ld (plx),hl         ; set player coords.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player base symbol.

; Now we want to fill the play area with mushrooms.

ld a,68             ; green ink (4) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary colours.
mushlp ld a,22             ; control code for AT character.
rst 16
call random         ; get a 'random' number.
and 15              ; want vertical in range 0 to 15.
rst 16
call random         ; want another pseudo-random number.
and 31              ; want horizontal in range 0 to 31.
rst 16
ld a,145            ; UDG 'B' is the mushroom graphic.
rst 16              ; put mushroom on screen.
djnz mushlp         ; loop back until all mushrooms displayed.

; This is the main loop.

mloop  equ \$

; Delete the player.

call basexy         ; set the x and y positions of the player.
call wspace         ; display space over player.

; Now we've deleted the player we can move him before redisplaying him
; at his new coordinates.

ld bc,63486         ; keyboard row 1-5/joystick port 2.
in a,(c)            ; see what keys are pressed.
rra                 ; outermost bit = key 1.
push af             ; remember the value.
call nc,mpl         ; it's being pressed, move left.
pop af              ; restore accumulator.
rra                 ; next bit along (value 2) = key 2.
push af             ; remember the value.
call nc,mpr         ; being pressed, so move right.
pop af              ; restore accumulator.
rra                 ; next bit (value 4) = key 3.
push af             ; remember the value.
call nc,mpd         ; being pressed, so move down.
pop af              ; restore accumulator.
rra                 ; next bit (value 8) reads key 4.
call nc,mpu         ; it's being pressed, move up.

; Now he's moved we can redisplay the player.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player.

halt                ; delay.

; Jump back to beginning of main loop.

jp mloop

; Move player left.

mpl    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
and a               ; is it zero?
ret z               ; yes - we can't go any further left.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
dec b               ; look 1 square to the left.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

dec (hl)            ; subtract 1 from y coordinate.
ret

; Move player right.

mpr    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
cp 31               ; is it at the right edge (31)?
ret z               ; yes - we can't go any further left.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
inc b               ; look 1 square to the right.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

inc (hl)            ; add 1 to y coordinate.
ret

; Move player up.

mpu    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 4                ; is it at upper limit (4)?
ret z               ; yes - we can go no further then.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
dec c               ; look 1 square up.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

dec (hl)            ; subtract 1 from x coordinate.
ret

; Move player down.

mpd    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 21               ; is it already at the bottom (21)?
ret z               ; yes - we can't go down any more.

; now check that there isn't a mushroom in the way.

ld bc,(plx)         ; current coords.
inc c               ; look 1 square down.
cp 68               ; mushrooms are bright (64) + green (4).
ret z               ; there's a mushroom - we can't move there.

inc (hl)            ; add 1 to x coordinate.
ret

; Set up the x and y coordinates for the player's gunbase position,
; this routine is called prior to display and deletion of gunbase.

basexy ld a,22             ; AT code.
rst 16
ld a,(plx)          ; player vertical coord.
rst 16              ; set vertical position of player.
ld a,(ply)          ; player's horizontal position.
rst 16              ; set the horizontal coord.
ret

; Show player at current print position.

splayr ld a,69             ; cyan ink (5) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,144            ; ASCII code for User Defined Graphic 'A'.
rst 16              ; draw player.
ret

wspace ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,32             ; SPACE character.
rst 16              ; display space.
ret

; Simple pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning
; the contents of the byte at that location.

random ld hl,(seed)        ; Pointer
ld a,h
and 31              ; keep it within first 8k of ROM.
ld h,a
ld a,(hl)           ; Get "random" number from location.
inc hl              ; Increment pointer.
ld (seed),hl
ret
seed   defw 0

; Calculate address of attribute for character at (dispx, dispy).

atadd  ld a,c              ; vertical coordinate.
rrca                ; multiply by 32.
rrca                ; Shifting right with carry 3 times is
rrca                ; quicker than shifting left 5 times.
ld e,a
and 3
ld d,a
ld a,e
and 224
ld e,a
ld a,b              ; horizontal position.
ld e,a              ; de=address of attributes.
ld a,(de)           ; return with attribute in accumulator.
ret

plx    defb 0              ; player's x coordinate.
ply    defb 0              ; player's y coordinate.

; UDG graphics.

blocks defb 16,16,56,56,124,124,254,254    ; player base.
defb 24,126,255,255,60,60,60,60     ; mushroom.```

## How To Write ZX Spectrum Games – Chapter 4

Random Numbers

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Generating random numbers in machine code can be a tricky problem for a novice programmer.

First of all, let’s get one thing straight. There is no such thing as a random number generator. The CPU merely follows instructions and has no mind of its own, it cannot simply pluck a number out of thin air based on a whim. Instead, it needs to follow a formula which will produce an unpredictable sequence of numbers which do not appear to follow any sort of pattern, and therefore give the impression of randomness. All we can do is return a false – or pseudo – random number.

One method of obtaining a pseudo-random number would be to use the Fibonacci sequence, however the easiest and quickest method of generating a pseudo-random 8-bit number on the Spectrum is by stepping a pointer through the ROM, and examining the contents of the byte at each location in turn. There is one small drawback to this method – the Sinclair ROM contains a very uniform and non-random area towards the end which is best avoided. By limiting the pointer to, say, the first 8K of ROM we still have a sequence of 8192 “random” numbers, more than enough for most games. In fact, every game I have ever written with a random number generator uses this method, or a very similar one:

```; Simple pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning
; the contents of the byte at that location.

random ld hl,(seed)        ; Pointer
ld a,h
and 31              ; keep it within first 8k of ROM.
ld h,a
ld a,(hl)           ; Get "random" number from location.
inc hl              ; Increment pointer.
ld (seed),hl
ret
seed   defw 0```

Let’s put our new random number generator to use in our Centipede game. Every Centipede game needs mushrooms – lots of them – scattered randomly across the play area, and we can now call the random routine to supply coordinates for each mushroom as we display them. The bits underlined are those we need to add.

```; We want a black screen.

ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23693),a        ; set our screen colours.
xor a               ; quick way to load accumulator with zero.
call 8859           ; set permanent border colours.

; Set up the graphics.

ld hl,blocks        ; address of user-defined graphics data.
ld (23675),hl       ; make UDGs point to it.

; Okay, let's start the game.

call 3503           ; ROM routine - clears screen, opens chan 2.

; Initialise coordinates.

ld hl,21+15*256     ; load hl pair with starting coords.
ld (plx),hl         ; set player coords.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player base symbol.

; Now we want to fill the play area with mushrooms.

ld a,68             ; green ink (4) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary colours.
mushlp ld a,22             ; control code for AT character.
rst 16
call random         ; get a 'random' number.
and 15              ; want vertical in range 0 to 15.
rst 16
call random         ; want another pseudo-random number.
and 31              ; want horizontal in range 0 to 31.
rst 16
ld a,145            ; UDG 'B' is the mushroom graphic.
rst 16              ; put mushroom on screen.
djnz mushlp         ; loop back until all mushrooms displayed.

; This is the main loop.

mloop  equ \$

; Delete the player.

call basexy         ; set the x and y positions of the player.
call wspace         ; display space over player.

; Now we've deleted the player we can move him before redisplaying him
; at his new coordinates.

ld bc,63486         ; keyboard row 1-5/joystick port 2.
in a,(c)            ; see what keys are pressed.
rra                 ; outermost bit = key 1.
push af             ; remember the value.
call nc,mpl         ; it's being pressed, move left.
pop af              ; restore accumulator.
rra                 ; next bit along (value 2) = key 2.
push af             ; remember the value.
call nc,mpr         ; being pressed, so move right.
pop af              ; restore accumulator.
rra                 ; next bit (value 4) = key 3.
push af             ; remember the value.
call nc,mpd         ; being pressed, so move down.
pop af              ; restore accumulator.
rra                 ; next bit (value 8) reads key 4.
call nc,mpu         ; it's being pressed, move up.

; Now he's moved we can redisplay the player.

call basexy         ; set the x and y positions of the player.
call splayr         ; show player.

halt                ; delay.

; Jump back to beginning of main loop.

jp mloop

; Move player left.

mpl    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
and a               ; is it zero?
ret z               ; yes - we can't go any further left.
dec (hl)            ; subtract 1 from y coordinate.
ret

; Move player right.

mpr    ld hl,ply           ; remember, y is the horizontal coord!
ld a,(hl)           ; what's the current value?
cp 31               ; is it at the right edge (31)?
ret z               ; yes - we can't go any further left.
inc (hl)            ; add 1 to y coordinate.
ret

; Move player up.

mpu    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 4                ; is it at upper limit (4)?
ret z               ; yes - we can go no further then.
dec (hl)            ; subtract 1 from x coordinate.
ret

; Move player down.

mpd    ld hl,plx           ; remember, x is the vertical coord!
ld a,(hl)           ; what's the current value?
cp 21               ; is it already at the bottom (21)?
ret z               ; yes - we can't go down any more.
inc (hl)            ; add 1 to x coordinate.
ret

; Set up the x and y coordinates for the player's gunbase position,
; this routine is called prior to display and deletion of gunbase.

basexy ld a,22             ; AT code.
rst 16
ld a,(plx)          ; player vertical coord.
rst 16              ; set vertical position of player.
ld a,(ply)          ; player's horizontal position.
rst 16              ; set the horizontal coord.
ret

; Show player at current print position.

splayr ld a,69             ; cyan ink (5) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,144            ; ASCII code for User Defined Graphic 'A'.
rst 16              ; draw player.
ret

wspace ld a,71             ; white ink (7) on black paper (0),
; bright (64).
ld (23695),a        ; set our temporary screen colours.
ld a,32             ; SPACE character.
rst 16              ; display space.
ret

; Simple pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning
; the contents of the byte at that location.

random ld hl,(seed)        ; Pointer
ld a,h
and 31              ; keep it within first 8k of ROM.
ld h,a
ld a,(hl)           ; Get "random" number from location.
inc hl              ; Increment pointer.
ld (seed),hl
ret
seed   defw 0

plx    defb 0              ; player's x coordinate.
ply    defb 0              ; player's y coordinate.

; UDG graphics.

blocks defb 16,16,56,56,124,124,254,254    ; player base.
defb 24,126,255,255,60,60,60,60     ; mushroom.```

Once run this listing looks more like a Centipede game than it did before, but there’s a major problem. The mushrooms are distributed in a random fashion around the screen, but the player can move straight through them. Some form of collision detection is required to prevent this happening, and we shall cover this in the next chapter.

# Loudspeaker Sound Effects

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

The Loudspeaker

There are two ways of generating sound and music on the ZX Spectrum, the best and most complicated of which is via the AY38912 sound chip in the 128K models. This method is described in detail in a later chapter, but for now we will concern ourselves with the 48K loudspeaker. Simple it may be, but this method does have its uses especially for short sharp sound effects during games.

Beep

First of all we need to know how to produce a beep of a certain pitch and duration, and the Sinclair ROM has a fairly accessible routine to do the job for us at address 949, all that is required is to pass the parameters for pitch in the HL register pair and duration in DE, call 949 and we get an appropriate “beep”.

Alas, the way in which we work out the parameters required is a little tricky as it needs a little calculation. We need to know the Hertz value for the frequency of note to emit, essentially just the number of times the loudspeaker needs to be toggled each second to produce the desired pitch. A suitable table is located below (# stands for ‘sharp’):

Middle C 261.63

C# 277.18

D 293.66

D# 311.13

E 329.63

F 349.23

F# 369.99

G 392.00

G# 415.30

A 440.00

A# 466.16

B 493.88

For each octave higher, simply double the frequency, to go an octave lower halve it. For example, to produce a note C one octave higher than middle C we take the value for Middle C – 261.63, and double it to 523.26.

Once the frequency is established we multiply it by the number of seconds required and pass this to the ROM routine in the DE register pair as the duration – so to play the note at middle C for one tenth of a second the duration required would be 261.63 * 0.1 = 26. The pitch is worked out by first dividing the 437500 by the frequency, subtracting 30.125 and passing the result in the HL registers. For middle C this would mean a value of 437500 / 261.63 – 30.125 = 1642.

In other words:

DE = Duration = Frequency * Seconds

HL = Pitch = 437500 / Frequency – 30.125

So to play note G# one octave above that of middle C for one quarter of one second:

```; Frequency of G sharp in octave of middle C = 415.30
; Frequency of G sharp one octave higher = 830.60
; Duration = 830.6 / 4 = 207.65
; Pitch = 437500 / 830.6 - 30.125 = 496.6

ld hl,497           ; pitch.
ld de,208           ; duration.
call 949            ; ROM beeper routine.
ret```

Of course, this routine isn’t just useful for musical notes – we can use it for a variety of effects as well, one of my favourites being a simple pitch bend routine:

```       ld hl,500           ; starting pitch.
ld b,250            ; length of pitch bend.
loop   push bc
push hl             ; store pitch.
ld de,1             ; very short duration.
call 949            ; ROM beeper routine.
pop hl              ; restore pitch.
inc hl              ; pitch going up.
pop bc
djnz loop           ; repeat.
ret```

Have a play with the above routine – by fiddling with it it’s pretty easy to adjust the pitch up and down, and to change the starting frequency and pitch bend and length producing a number of interesting effects. One word of warning though – Don’t go too crazy with your pitch or duration values or the beeper routine will get stuck and you won’t be able to regain control of your Spectrum without resetting it.

White Noise

When using the loudspeaker we don’t even have to stick with the routines in the ROM, it is easy enough to write our own sound effects routines, especially if we want to generate white noise for crashes and bangs. White noise is usually a lot more fun to play with.

To generate white noise all we need is a quick and simple random number generator (a Fibonacci sequence might work, but I’d recommend stepping a pointer through the first 8K of ROM and fetching the byte at each location to get a reasonably random 8-bit number). Then write this value to port 254. Remember this port also controls the border colour so if you don’t want a striped multicolour border effect we need to mask off the border bits with AND 248 and add the number for the border colour we want (1 for blue, 2 for red etc.) before performing an OUT (254) instruction. When we’ve done this we need to put in a small delay loop (short for high pitch, long for lower pitch) and repeat the process a few hundred times. This will give us a nice “crash” effect.

This routine is based on a sound effect from Egghead 3:

```noise  ld e,250            ; repeat 250 times.
ld hl,0             ; start pointer in ROM.
noise2 push de
ld b,32             ; length of step.
noise0 push bc
ld a,(hl)           ; next "random" number.
inc hl              ; pointer.
and 248             ; we want a black border.
out (254),a         ; write to speaker.
ld a,e              ; as e gets smaller...
cpl                 ; ...we increase the delay.
noise1 dec a               ; decrement loop counter.
jr nz,noise1        ; delay loop.
pop bc
djnz noise0         ; next step.
pop de
ld a,e
sub 24              ; size of step.
cp 30               ; end of range.
ret z
ret c
ld e,a
cpl
noise3 ld b,40             ; silent period.
noise4 djnz noise4
dec a
jr nz,noise3
jr noise2```

## Tutorial: ZX Spectrum Machine Code Game in 30 Minutes!

This tutorial by Jon Kingsman (bigjon) originally appeared in a thread on WoSF. Reproduced with permission.

Roadrace game by bigjon, incorporating suggestions from Dr Beep, skoolkid and Matt B

Hi folks, I’m a machine code novice who coded a very small roadrace game to help me learn.

I reckon you can learn the basics of machine code in half an hour by coding this game step by step.

This tutorial assumes you have a working knowledge of ZX Spectrum basic, and the ZX Spin emulator.

Make yourself a large cup of tea – by the time you’ve drunk it, you be able to program in machine code!

CHAPTER 1 – Create a machine code function that returns the score to BASIC
Machine code programs are a series of bytes in the Spectrum’s memory.
In this chapter we will

• – Use the Spin assembler to write a few bytes into the memory generating a score for our game.
• – Write a BASIC program to run the machine code and print the score we have obtained from it.

Open ZX Spin.  Select Tools -> Z80 Assembler.
To run our roadrace game, we need to execute the following steps:

```MAIN 	;label for main section of program as opposed to graphics data etc

;arrange to put our machine code at free, fast-running (over 32768) and memorable address in RAM
org 33000

;initialise score

PRINCIPALLOOP ;label for the loop in the game that will execute over and over
;set new carposition
;crash? if so, go to GAMEOVER.
;print car
;jump back to PRINCIPALLOOP

GAMEOVER ;label for the cleaning up that needs to be done before returning to BASIC
;return score to BASIC```

Copy and paste the paragraph above into the Spin Assembler.It will appear as 15 lines of mainly grey text.

The text is grey because text after a ; is a comment. The assembler ignores it but it’s there for our benefit.

You can TAB comments over towards the right-hand side of the assembler page to make your code more readable.

The labels are in pink.The assembler won’t put anything in RAM for them but will use them as entry points to jump to.

In the assembler, do File -> Save as and type something like mc30mintut.asm into the save box.
We’ll do the first and last of these steps in this chapter, starting with the last one.

The assembly language instruction for ‘return to calling program’ (in our case a BASIC routine) is ‘ret’.

Click on the end of line 15, press enter to create line 16 and type ret
The word ret‘ appears in blue. This is Spin’s colour code for an instruction.

When the Spin assembler gets to the instruction ret it writes the byte 201 into the memory at an address we choose.

The computer knows that the first byte it meets will be an instruction byte.

It does something different for each byte from 0 to 255. There’s a list in Appendix A of the Spectrum Manual.

Some instruction bytes, like 201 for ret, are complete as is – no further info is needed to complete the action.

Some instruction bytes need one or two bytes of data afterwards for the computer to know what to do.

Some other instruction bytes need a further instruction byte to clarify the action required.

Now we’ll initialise the score, ready for the mc program to report it back to BASIC at the end of the game.

The computer does most of its work using 7 temporary byte-sized addresses called registers.

The first register deals with single bytes only (numbers 0 to 255), the other six are in pairs to deal with 0 to 65535.

The first (single-byte) register is called the A register, sometimes also referred to as the accumulator.

The other three register pairs are called BC, DE, and HL (H is for High byte, L is for Low byte)

Any machine code function called from basic will return the value from 0 to 65535 in the BC register.

We will write the value 0 into the BC register, ready to increase it by 1 each time the game goes round its principal loop.

At the beginning of line 4, type ld bc,0. ld is the instruction for load a value into a register or register pair.

The instruction byte for ld is different for each register or register pair that is loaded.

The instruction byte for ld bc is 1. The computer then expects 2 data bytes to give it a number from 0 to 65535.

In our case the two data bytes will be 0,0.  So the assembler will write 1,0,0,201 at address 33000 in RAM.

We’ll assemble this code now. Do File -> Save, then File -> Assemble. Type 33000 into the Start Address box and click OK.

At the bottom window of the assembler you should see a report that says “No errors in 16 lines. 4 bytes generated”.

You can see the four bytes are now at memory address 33000 by clicking in the main Spin display window on Tools -> Debugger.

To run these four bytes of machine code, enter this one-line program in the main Spin display window:
10 PRINT AT 0,0; “Your score was “; USR 33000

Now RUN the program. Did you get “Your score was 0”? Congratulations – you have coded your first machine code program!

Do File -> Save in the main Spin display window and save as something like mc30mintut.sna. Here ends Chapter 1!

CHAPTER 2 – Display material on the screen.
There are two areas of the Spectrum’s memory which have a direct effect on the screen display.
The complicated way is the display file, from addresses 16384 to 22527, which stores a dash of 8 pixels per byte.

Try POKE-ing 255 into bytes within this range to see the funny order in which this memory area is mapped onto the screen.

The simple way is the attribute file, from 22528 to 23296, which affects an 8×8 pixel block per byte, in logical order.

In this chapter we will

• Draw our ‘car’ by changing the paper colour of one character square to blue.
• Draw our ‘road’ by using a loop to create two vertical stripes of black paper colour down the screen.

In the spin assembler line 5, delete the word ‘road’ in the comments.

At the beginning of line 5, type ld hl,23278. This points HL to the middle of the bottom row in the display file.

Insert line 6, ld a,8. This puts a blue PAPER colour into the A register. Why 8? See the BASIC manual chapter 16.

Insert line 7, ld (hl),a. The brackets round hl mean the load will be to the address in RAM that hl is pointing to.

Insert line 8, ld (32900),hl ;save car posn. We’ll store the attribute file address of the ‘car’ in some free bytes in RAM.

Now for the road. Insert line 4, ld hl,22537 ;initialise road. This points to a third of the way along the top line.

To save the road position, which we’ll need frequently, we’ll let the computer choose where to store it, on its ‘stack’.

Chapter 24 of the manual has a diagram showing where the machine stack is in the RAM.
To write to the stack we use push. To write from the stack we use pop. What goes on the stack first will come off last.

Insert line 5, push hl ;save road posn. Insert line 21, pop hl ;empty stack

To print a black road we need 0 in the accumulator (ch16 of the BASIC manual).

We could do ld a, 0 but this takes 2 bytes whereas xor a takes only one. Insert line 6, xor a.
xor compares the chosen register to the A register and puts a 1 in the A register for each bit that is different.

We’ll print the top line of the road. Two double squares of black with a 9-square gap between them.

Insert line 7, then copy and paste the following code:

```	ld (hl),a
inc hl ;inc increases the register by one, dec decreases it by one.
ld (hl),a
ld de,9 ;for a 9-space gap in the road.
ld (hl),a
inc hl
ld (hl),a```

To get hl to point to the left hand verge on the next line, we need to move 21 bytes further in the attribute file.

Insert line 15, ld de, 21 ;point to left verge on next line

To fill the screen with the road we will use machine code’s equivalent of a FOR-NEXT loop, djnz.
djnz stands for Decrement then Jump if Not Zero. We load the b register with the number of times we want to loop.

Insert line 7, ld b,24 ;print road verge on 24 lines.
Insert line 8, fillscreen – this is the label for our loop to jump back to.
Insert line 19, djnz fillscreen.

Because our routine will continue from the loop when b=0, we no longer need to initialise b as well as c to 0 in the next line.

Change line 20 ld bc, 0 to ld c,b.  This is one byte shorter.

Assemble and save. If you want to see the blue ‘car’, you’ll need to add something like 20 PAUSE 0 to your basic program.

CHAPTER 3 – move the car, test for collision.
Time to start playing the game! First we need to erase the car ready to move it if the player wants to.

Insert line 26, then copy and paste the following code:

```	ld hl,(32900) ;retrieve car posn
ld a,56 ;erase car
ld (hl),a```

Before we read the keyboard we will lock the keyboard for most of the game, and unlock it only when we want to read the keys.

The instruction to lock the keyboard is di = ‘disable interrupts. Its opposite is ei = ‘enable interrupts’.

Replace line 3 with di. Insert line 29, ei. Insert line 31, di. Insert line 41, ei

To read the keys we use the IN ports – see ch23 of the BASIC manual – to read the left and right half of the bottom row.

We load bc with the port number and use the instruction cp (compare) to see if the number has dropped to show a keypress.

Delete line30 and replace with the following code:

```	ld bc,65278 ;read keyboard caps to v
in a,(c)
cp 191
jr nz, moveright
inc l
moveright
ld bc,32766 ;read keyboard space to b
in a,(c)
cp 191
jr nz, dontmove
dec l
dontmove```

jr nz stands for jump relative if not zero. It skips over the instruction to increment / decrement the car position.

Replace line 43 with the following to see if we bump into the oncoming road 32 bytes (1 screen) down the attribute file:

```	ld (32900),hl ;store car posn
ld de, 32 ;new carposn
xor a  ;set carry flag to 0
sbc hl,de
ld a,(hl) ;crash?
or a
jr z,gameover
ld a,8  ;print car
ld (hl),a```

We’d like to sub hl,de but there’s no such instruction so we use sbc, subtract with carry, and set the carry flag to zero.

or compares the register to the a register bit by bit and leaves a 1 in the a register for each bit that is 1 in either.

If all the digits are zero, then the zero flag will be set, so we can use or a to test for a black paper colour.

Delete line 53. Delete line 53 again!

To clean up the score at GAMEOVER insert line21, push bc; save score. Replace line 57 with pop bc;retrieve score

To cycle round the game before GAMEOVER change line 55 to jp PRINCIPALLOOP.

Assemble, save, and run. You’ll need to deliberately crash to get out!

CHAPTER 4 – scroll and move the road, keep score, adjust speed.
To scroll the road down the screen we copy the screen attribute bytes to the line beneath 736 times.

We use the instruction lddr, which stand for LoaD ((hl) to (de)),Decrement (hl and de) and Repeat (until bc is zero).

Replace line 53 with the following:

```	ld hl,23263 ;scroll road
ld de,23295
ld bc,736
lddr
pop bc  ;retrieve score```

To add 1 to the score and save it ready for GAMEOVER, insert the following into line 59:

inc bc ;add 1 to score
push bc ;save score

To move the road randomly left or right on the top line we use the following algorithm –
Choose a location in ROM where the are 256 random looking bytes and add the low byte of the score in bc to it.

If it is odd, lower the road position in hl by one. If it is even, increase by one.

(To test the last bit for odd and even we use ‘and 1’ which “masks” the last bit and sets the zero flag if it is 0).

Check to see if the road has reached the edge of the screen and bump it away if it has.
Print the new road top line like we did in chapter 2.

Replace line 58 with the following hefty chunk of code:

```	pop hl  ;retrieve road posn
ld (hl),a
inc hl
ld (hl),a
ld de,9
ld (hl),a
inc hl
ld (hl),a
ld hl,14000 ;source of random bytes in ROM
ld d,0
ld e,c
ld a,(hl)
dec hl  ;move road posn 1 left
and 1
inc hl
inc hl
ld a,l  ;check left
cp 255
jr nz, checkright
inc hl
inc hl
checkright
ld a,l
cp 21
dec hl
dec hl
ld (hl),a
inc hl
ld (hl),a
ld de,9
ld (hl),a
inc hl
ld (hl),a```

The last thing we need to do to have a playable game is slow down our blindingly fast machine code.

Insert the following into line 106 (as extension material, you could adjust the figure in bc with a keypress to ‘brake’):

```;wait routine
ld bc,\$1fff ;max waiting time
wait
dec bc
ld a,b
or c
jr nz, wait```

Save, assemble, and run – and that’s it! Has your tea gone cold yet?

```main
org 33000
di
xor a
ld b,24
fillscreen
ld (hl),a
inc hl
ld (hl),a
ld de,9
ld (hl),a
inc hl
ld (hl),a
ld de,21
djnz fillscreen
ld c,b  ;initialise score
push bc  ;save score
ld hl,23278 ;initialise car
ld a,8
ld (hl),a
ld (32900),hl ;save car posn
principalloop
ld hl,(32900) ;retrieve car posn
ld a,56  ;erase car
ld (hl),a
ei
ld bc,65278 ;read keyboard caps to v
in a,(c)
cp 191
jr nz, moveright
inc l
moveright
ld bc,32766 ;read keyboard space to b
in a,(c)
cp 191
jr nz, dontmove
dec l
dontmove
di
ld (32900),hl ;store car posn
ld de, 32 ;new carposn
xor a  ;set carry flag to 0
sbc hl,de
ld a,(hl) ;crash?
or a
jr z,gameover
ld a,8  ;print car
ld (hl),a
ld de,23295
ld bc,736
lddr
pop bc  ;retrieve score
ld (hl),a
inc hl
ld (hl),a
ld de,9
ld (hl),a
inc hl
ld (hl),a
ld hl,14000 ;source of random bytes in ROM
ld d,0
ld e,c
ld a,(hl)
dec hl  ;move road posn 1 left
and 1
inc hl
inc hl
ld a,l  ;check left
cp 255
jr nz, checkright
inc hl
inc hl
checkright
ld a,l
cp 21
dec hl
dec hl
ld (hl),a
inc hl
ld (hl),a
ld de,9
ld (hl),a
inc hl
ld (hl),a
inc bc  ;add 1 to score
push bc  ;save score
;wait routine
ld bc,\$1fff ;max waiting time
wait
dec bc
ld a,b
or c
jr nz, wait
jp principalloop
gameover
pop bc  ;retrieve score
pop hl  ;empty stack
ei
ret; game and tutorial written by Jon Kingsman ('bigjon', 'bj'). electronic mail gmail.com - atsign - jon.kingsman (reversed)```

## 64 Column print

The source code for 64 column printing was originally provided by Andrew Owen in a thread on WoSF. Reproduced with permission.

BASIC normally provides only 32 columns for printing text. This machine code routine, which can be called from BASIC, effectively doubles the number of columns horizontally available for printing.

In BASIC, do a CLEAR 49999 so that BASIC won’t overrun the machine code in memory.

Assemble or load the following routine at address 50000. Do a RANDOMIZE USR 50000, to run the routine and perform the magic for 64 column printing.

To print in 64 columns, use stream 4, for eg: PRINT #4;AT 0,53;”Hello World”

```; ---------------
; 4x8 font driver, (c) 2007 Andrew Owen
; ---------------

org	50000		;

; --------------------------------
; CREATE CHANNEL AND ATTACH STREAM
; --------------------------------
;
; Based on code by Ian Beardsmore from Your Spectrum issue 7, September 1984.

c_chan:	ld	hl,(\$5c53)	; a channel must be created below basic
; so look at the system variable PROG
dec	hl		; move hl down one address

ld	bc,\$0005	; the new channel takes 5 bytes
call	\$1655		; call the MAKE_ROOM routine
inc	hl		; move HL up one address

ld	bc,chan_4	; could write the bytes directly but
; then code would be non-relocatable

ld	(hl),c		; low byte of the output routine
inc	hl		; move HL up one address
push	hl		; save this address for later

ld	(hl),b		; high byte of the output routine
inc	hl		; move HL up one address

ld	bc,\$15c4	; address of input routine

ld	(hl),c		; low byte of the input routine
inc	hl		; move HL up one address

ld	(hl),b		; high byte of the input routine
inc	hl		; move HL up one address

ld	(hl),'P'	; channel type; 'K', 'S', 'R' or 'P'

; attach stream

pop	hl		; the first address plus one of the
; extra space stored earlier
ld	de,(\$5c4f)	; store the contents of CHANS in DE
and	a		; clear the carry flag before
; calculation
sbc	hl,de		; the difference between the start of
; the channels area and the start of the
; extra space becomes the offset, stored
; in HL
ex	de,hl		; store the offset in DE

ld	hl,\$5c10	; store the contents of STRMS in HL
ld	a,\$04		; stream number 4
add	a,\$03		; take account of streams -3 to -1
add	a,a		; each of the seven default streams has
; two bytes of offset data
; the total number of bytes occupied,
; held in a, forms the offset for the
; new stream
ld	b,\$00		; set b to hold \$00
ld	c,a		; set the low byte of the offset
; address to give the correct location
; in the streams table to store the
; offset
ld	(hl),e		; the low byte of the offset
inc	hl		; move HL up one address
ld	(hl),d		; the high byte of the offset
ret			; all done

; -----------------
; CHANNEL #4 OUTPUT
; -----------------
;
; Based on code by Tony Samuels from Your Spectrum issue 13, April 1985.
; A channel wrapper for the 64-column display driver.

chan_4:	ld	b,a		; save character
ld	a,(atflg)	; value of AT flag
and	a		; test against zero
jr	nz,getrow	; jump if not
ld	a,b		; restore character

atchk:	cp	\$16		; test for AT
jr	nz,crchk	; if not test for CR
ld	a,\$ff		; set the AT flag
ld	(atflg),a	; next character will be row
ret			; return

getrow:	cp	\$fe		; test AT flag
jr	z,getcol	; jump if setting col
ld	a,b		; restore character
cp	\$18		; greater than 23?
jr	nc,err_b	; error if so

ld	(row),a		; store it in row
ld	hl,atflg	; AT flag
dec	(hl)		; indicates next character is col
ret			; return

getcol:	ld	a,b		; restore character
cp	\$40		; greater than 63?
jr	nc,err_b	; error if so
ld	(col),a		; store it in col
xor	a		; set a to zero
ld	(atflg),a	; store in AT flag
ret			; return

err_b:	xor	a		; set a to zero
ld	(atflg),a	; clear AT flag
rst	08h		;
defb	\$0a		;

crchk:	cp	\$0d		; check for return
jr	z,do_cr		; to carriage return if so
call	pr_64		; print it

ld	hl,col		; increment
inc	(hl)		; the column
ld	a,(hl)		;

cp	\$40		; column 64?
ret	nz		;

do_cr:	xor	a		; set A to zero
ld	(col),a		; reset column
ld	a,(row)		; get the row
inc	a		; increment it
cp	\$18		; row 24?
jr	z,wrap		;

zend:	ld	(row),a		; write it back
ret

wrap:	xor	a		;
jr	zend		;

; ------------------------
; 64 COLUMN DISPLAY DRIVER
; ------------------------

pr_64:	rra			; divide by two with remainder in carry flag

ld	h,\$00		; clear H
ld	l,a		; CHAR to low byte of HL

ex	af,af'		; save the carry flag

ld	de,font-\$80	; offset to FONT
; character map in FONT
push	hl		; save font address

; convert the row to the base screen address

ld	a,(row)		; get the row
ld	b,a		; save it
and	\$18		; mask off bit 3-4
ld	d,a		; store high byte of offset in D
ld	a,b		; retrieve it
and	\$07		; mask off bit 0-2
rlca			; shift
rlca			; five
rlca			; bits
rlca			; to the
rlca			; left
ld	e,a		; store low byte of offset in E

ld	a,(col)		; get the column
rra			; divide by two with remainder in carry flag
push	af		; store the carry flag

ld	h,\$40		; base location
ld	l,a		; plus column offset

ex	de,hl		; put the result back in DE

; HL now points to the location of the first byte of char data in FONT_1
; DE points to the first screen byte in SCREEN_1
; C holds the offset to the routine

pop	af		; restore column carry flag
pop	hl		; restore the font address

jr	nc,odd_col	; jump if odd column

even_col:
ex	af,af'		; restore char position carry flag
jr	c,l_on_l	; left char on left col
jr	r_on_l		; right char on left col

odd_col:
ex	af,af'		; restore char position carry flag
jr	nc,r_on_r	; right char on right col
jr	l_on_r		; left char on right col

; -------------------------------
; WRITE A CHARACTER TO THE SCREEN
; -------------------------------
;
; There are four separate routines

; HL points to the first byte of a character in FONT
; DE points to the first byte of the screen address

; left nibble on left hand side

l_on_l:	ld	c,\$08		; 8 bytes to write
ll_lp:	ld	a,(de)		; read byte at destination
and	\$f0		; mask area used by new character
ld	b,a		; store in b
ld	a,(hl)		; get byte of font
and	\$0f		; mask off unused half
or	b		; combine with background
ld	(de),a		; write it back
inc	d		; point to next screen location
inc	hl		; point to next font data
jr	nz,ll_lp	; loop 8 times
ret			; done

; right nibble on right hand side

r_on_r:	ld	c,\$08		; 8 bytes to write
rr_lp:	ld	a,(de)		; read byte at destination
and	\$0f		; mask area used by new character
ld	b,a		; store in b
ld	a,(hl)		; get byte of font
and	\$f0		; mask off unused half
or	b		; combine with background
ld	(de),a		; write it back
inc	d		; point to next screen location
inc	hl		; point to next font data
jr	nz,rr_lp	; loop 8 times
ret			; done

; left nibble on right hand side

l_on_r:	ld	c,\$08		; 8 bytes to write
lr_lp:	ld	a,(de)		; read byte at destination
and	\$0f		; mask area used by new character
ld	b,a		; store in b
ld	a,(hl)		; get byte of font
rrca			; shift right
rrca			; four bits
rrca			; leaving 7-4
rrca			; empty
and	\$f0		;
or	b		; combine with background
ld	(de),a		; write it back
inc	d		; point to next screen location
inc	hl		; point to next font data
jr	nz,lr_lp	; loop 8 times
ret			; done

; right nibble on left hand side

r_on_l:	ld	c,\$08		; 8 bytes to write
rl_lp:	ld	a,(de)		; read byte at destination
and	\$f0		; mask area used by new character
ld	b,a		; store in b
ld	a,(hl)		; get byte of font
rlca			; shift left
rlca			; four bits
rlca			; leaving 3-0
rlca			; empty
and	\$0f		;
or	b		; combine with background
ld	(de),a		; write it back
inc	d		; point to next screen location
inc	hl		; point to next font data
jr	nz,rl_lp	; loop 8 times
ret			; done

; --------------
; TEXT VARIABLES
; --------------
;
; Used by the 64 column driver

atflg:	defb	\$00		; AT flag
row:	defb	\$00		; row
col:	defb	\$00		; col

; -------------------
; half width 4x8 font
; -------------------
;
; 384 bytes

font:
defb	\$00,\$02,\$02,\$02,\$02,\$00,\$02,\$00,\$00,\$52,\$57,\$02,\$02,\$07,\$02,\$00;
defb	\$00,\$25,\$71,\$62,\$32,\$74,\$25,\$00,\$00,\$22,\$42,\$30,\$50,\$50,\$30,\$00;
defb	\$00,\$14,\$22,\$41,\$41,\$41,\$22,\$14,\$00,\$20,\$70,\$22,\$57,\$02,\$00,\$00;
defb	\$00,\$00,\$00,\$00,\$07,\$00,\$20,\$20,\$00,\$01,\$01,\$02,\$02,\$04,\$14,\$00;
defb	\$00,\$22,\$56,\$52,\$52,\$52,\$27,\$00,\$00,\$27,\$51,\$12,\$21,\$45,\$72,\$00;
defb	\$00,\$57,\$54,\$56,\$71,\$15,\$12,\$00,\$00,\$17,\$21,\$61,\$52,\$52,\$22,\$00;
defb	\$00,\$22,\$55,\$25,\$53,\$52,\$24,\$00,\$00,\$00,\$00,\$22,\$00,\$00,\$22,\$02;
defb	\$00,\$00,\$10,\$27,\$40,\$27,\$10,\$00,\$00,\$02,\$45,\$21,\$12,\$20,\$42,\$00;
defb	\$00,\$23,\$55,\$75,\$77,\$45,\$35,\$00,\$00,\$63,\$54,\$64,\$54,\$54,\$63,\$00;
defb	\$00,\$67,\$54,\$56,\$54,\$54,\$67,\$00,\$00,\$73,\$44,\$64,\$45,\$45,\$43,\$00;
defb	\$00,\$57,\$52,\$72,\$52,\$52,\$57,\$00,\$00,\$35,\$15,\$16,\$55,\$55,\$25,\$00;
defb	\$00,\$45,\$47,\$45,\$45,\$45,\$75,\$00,\$00,\$62,\$55,\$55,\$55,\$55,\$52,\$00;
defb	\$00,\$62,\$55,\$55,\$65,\$45,\$43,\$00,\$00,\$63,\$54,\$52,\$61,\$55,\$52,\$00;
defb	\$00,\$75,\$25,\$25,\$25,\$25,\$22,\$00,\$00,\$55,\$55,\$55,\$55,\$27,\$25,\$00;
defb	\$00,\$55,\$55,\$25,\$22,\$52,\$52,\$00,\$00,\$73,\$12,\$22,\$22,\$42,\$72,\$03;
defb	\$00,\$46,\$42,\$22,\$22,\$12,\$12,\$06,\$00,\$20,\$50,\$00,\$00,\$00,\$00,\$0F;
defb	\$00,\$20,\$10,\$03,\$05,\$05,\$03,\$00,\$00,\$40,\$40,\$63,\$54,\$54,\$63,\$00;
defb	\$00,\$10,\$10,\$32,\$55,\$56,\$33,\$00,\$00,\$10,\$20,\$73,\$25,\$25,\$43,\$06;
defb	\$00,\$42,\$40,\$66,\$52,\$52,\$57,\$00,\$00,\$14,\$04,\$35,\$16,\$15,\$55,\$20;
defb	\$00,\$60,\$20,\$25,\$27,\$25,\$75,\$00,\$00,\$00,\$00,\$62,\$55,\$55,\$52,\$00;
defb	\$00,\$00,\$00,\$63,\$55,\$55,\$63,\$41,\$00,\$00,\$00,\$53,\$66,\$43,\$46,\$00;
defb	\$00,\$00,\$20,\$75,\$25,\$25,\$12,\$00,\$00,\$00,\$00,\$55,\$55,\$27,\$25,\$00;
defb	\$00,\$00,\$00,\$55,\$25,\$25,\$53,\$06,\$00,\$01,\$02,\$72,\$34,\$62,\$72,\$01;
defb	\$00,\$24,\$22,\$22,\$21,\$22,\$22,\$04,\$00,\$56,\$A9,\$06,\$04,\$06,\$09,\$06;```