Einstein 256 Primer cover: Graphics 7's 256 colours on the Einstein 256's screen
← Back to Modules
module
20

A Bigger Engine

Introduction

River Run (S19) does all its video work in the gap between one picture and the next. That works because there isn't much of it: a line of river, the sprites, sometimes the numbers. When GAME OVER tried to draw sixteen characters at once, the gap wasn't long enough, the line interrupt came late, and a picture lost its panel.

A bigger game draws more: bullets, dust, debris, explosions drawn into the bitmap, not just sprites over it. This section builds an engine that can draw more than fits in the gap, and still never shows the player anything half done. Canyon flies you up a long canyon. Drones come down at you; shoot one and it leaves a scorch mark on the canyon floor, and the mark is still there when the canyon comes round again.

Every part of it is something you've met. What's new is how the parts are arranged, and why.

The Shape of It

There are three pieces of work every frame, and each has its own place.

The early part of onframe, straight after the picture, with interrupts off. Only what must be in place before the next picture starts: which page is shown, its scroll, the split's line, the sprites. It's short.

The late part of onframe, with interrupts on. All the drawing, into a page nobody is looking at. It can run on into the next picture, because the line interrupt can cut in and put the panel in place on time.

The main loop, which never touches the video chip. It reads the joystick, moves things, checks for hits, and builds the sprite list.

onframe:
        ...             ; still drawing the last frame? show it again
        CALL regs       ; early: page, scroll, split, sprites on
        CALL sprites    ;        the sprite list, sent
        CALL clip       ;        no sprite row on the panel's top line
        ...             ; the next frame: the other page, a line on
        EI
        CALL undo       ; late: this page's moving things, gone
        CALL domark     ;       scorch marks still to do
        CALL feed       ;       the next line of canyon, both pages
        CALL digits     ;       the panel's changed digits
        CALL drawb      ;       the bolts
        CALL Z, drawd   ;       the dust, if there's time
        DI
        RET

Two Pages Under a Scroll

S13 flipped between two pages so the player never sees a picture being drawn. S14 scrolled one page with R#23. Canyon does both at once, with page A at 10000h and page B at 18000h.

Each frame the early part shows the page drawn last frame, at the scroll it was drawn for. Then the late part draws the other page for the next frame's scroll, one line further on. River Run scrolls down by taking one off R#23 each frame, and so does Canyon, so the next picture's R#23 is this one's less one. onframe keeps it in dscroll, and everything drawn into the hidden page is placed with it: a thing at screen line y goes on ring line y + dscroll.

        LD A, (drawn)
        LD (shown), A   ; show the page drawn last frame...
        LD A, (dscroll)
        LD (scroll), A  ; ...at the scroll it was drawn for
        CALL regs

Two pages that take turns raise a question a single page never did: what goes into which page? There are two kinds of drawing, and they're handled differently.

Lasting Things and Moving Things

A lasting thing goes into both pages. The scenery is lasting: each frame feed copies the next line of canyon onto ring line dscroll of page A and of page B. On the page being shown that line is R#23 - 1, out of sight, as in S19; on the page being drawn it is the top line of the next picture.

A scorch mark is lasting too, but it lands on the screen where the player can see it. Draw it into the page being shown and it would appear in the middle of a picture. So a mark is queued, and each page gets it the next time that page is drawn. Each entry in marks has three bits still to do: page A, page B, and the strip (below). domark does whatever is due for the page in hand and clears that bit.

A moving thing is drawn into one page at a time, and each page has to forget where it drew it last. The bolts and the dust move every frame. When plot draws a byte for them, it first reads the byte that was there and keeps it, with its address, on an undo list for that page. The next time the same page comes round to be drawn - two frames later - undo puts those bytes back, last first, and the page is clean scenery again before anything is drawn on it.

plot:   ...             ; the ring line, R#14 and the address
        CALL vpage
        CALL vrad
        IN A, (8)       ; the old byte
        LD (old), A
        AND D
        OR C            ; the new one: (old AND D) OR C
        ...             ; written back, and the old byte on the undo list

Two lists, UNDOA and UNDOB, because the two pages were drawn in different frames with the things in different places. The order of the late part matters here: undo first, then the scorch marks, then the moving things. A mark always goes onto a page that has just been cleaned, so a later undo never puts back a byte from before the mark.

A dot of dust is half a byte, so plot keeps the other half: D is the mask that keeps it and C the dot's colour. A bolt is two dots, a whole byte, so its mask is 0.

Scenery From the Expansion

S19's river was made a line at a time by the CPU, so it never repeated. A game with designed scenery wants it drawn in advance, and much longer than a screen. The 64K expansion holds 512 lines of Graphics 4, twice a page, and the player never sees it (S17).

canyon fills it once, at the start, through R#45 bit 6 (S5): 512 lines of grey walls edged with dark red, an 85-byte black floor that wanders left and right, and every 64 lines a girder across it in one of four colours, so you can see which quarter of the strip you're in. Then two HMMMs with MXS copy lines 256-511 into both pages to start with.

From then on each frame copies one line, from the expansion straight onto the ring line about to come into view, in both pages:

feed:   LD HL, (dstrip)
        LD (fcmd+2), HL ; SY: the strip line
        LD A, (dscroll)
        LD (fcmd+6), A  ; DY: the ring line...
        LD A, 2
        LD (fcmd+7), A  ; ...in page A (512 on)
        LD HL, fcmd
        CALL blit
        ...             ; and again with DY's high byte 3: page B

The command engine counts the expansion as Y 0-511, and dstrip counts down through it one line a frame, wrapping from 0 to 511. So the canyon is 512 lines long and comes round again every 512 frames - and the CPU never copies a byte of it.

Because the strip is in video memory the command engine can draw on it too. A scorch mark is an LMMM with TIMP from the panel's page: once into each page, and once into the strip itself, with MXD (ARG = 20h) so the destination is the expansion:

        LD A, 20h
        LD (mcmd+13), A ; ARG: MXD, into the expansion

TIMP leaves the mark's colour-0 corners out, so the floor shows round it. The mark in the pages is gone once the ring comes round; the mark in the strip is fed back in, with the canyon, 512 frames later. Debris, craters, wreckage - anything the player does to the scenery can stay done.

mark doesn't queue a mark that would run past the last line of a page or of the strip. Splitting it in two would cost two commands for a rare case; dropping it costs nothing.

A Frame Handler in Two Halves

The late part runs with interrupts on, and that changes how it talks to the video chip.

Short DIs. The video chip has one address register, and S7 protected it with DI. A DI around the whole late part would hold up the line interrupt, and the panel would come late. So the late part shuts interrupts off only for the two bytes of each port 9 write - an address, or a register - and turns them on again straight after:

vwad:   DI
        LD A, L
        OUT (9), A
        LD A, H
        OR 40h
        OUT (9), A
        EI
        RET

The line interrupt may come between any two such pairs. It writes registers and reads S#1, and none of that disturbs the late part: an address, once both its bytes are sent, survives a register write, or the status reads a handler makes, before the data. Port 8 then reads or writes where you left it. Only the two bytes of an address, or of a register write, must never be split.

Commands one register at a time. S16 sent a command's 15 registers under one DI. Here blit sends them one register at a time, each pair under its own DI. The line handler may come between two of them; it writes other registers, and the command's keep what you wrote. cewait checks CE the way S16 taught, one read at a time, with the port 9 bytes written straight out so its DI is as short as it can be.

What else can come in. With interrupts on, any interrupt can arrive, not only the line. The frame interrupt is the subject of the next heading. The other is MOS's clock tick, once a second, through its own entry in the table. MOS's tick handler pages the ROM in and out and touches no other port: none of the video chip's. So it can come in the middle of the late part and do no harm. (Setup runs with interrupts on for the same reason.)

The end of the late part turns interrupts off again before RET. The code after onframe in s15-lineint.asm reads S#2, and it must not be interrupted while R#15 is 2.

When a Frame Runs Long

Two things can run long, and each has a guard. Neither guard needs to know how long anything takes: each just asks whether a piece of work has finished.

The late part still drawing when the next frame comes. The late part is interrupted by the frame interrupt like anything else, and onframe is called again. It checks busy, which the late part sets at its start and clears at its end:

onframe:
        LD A, (busy)
        OR A
        JP NZ, regs     ; still drawing the last frame: show the same again

The hidden page is half drawn, so it mustn't be shown. regs puts the registers back for the page already on screen - the line interrupt left the panel's in place - and returns, and the interrupted late part carries on. The player sees the same picture twice. The next frame flips as usual. Nothing tears, and the panel never moves.

The main loop still busy when the frame comes. The main loop sets done when it has finished its frame's work, and onframe takes it and clears it. If it wasn't set, the main loop is behind - the bolts and drones haven't all moved yet - and onframe sets behind. The late part then leaves out what the game can do without: the dust. Leaving it out gives the main loop that time back, so a busy moment doesn't feed on itself.

        LD A, (behind)
        OR A
        CALL Z, drawd   ; the dust, unless the main loop is behind

Decide what your game can drop - dust, distant stars, a glow - and drop it first.

The Panel

The panel is S19's: 32 lines of the page at 8000h, switched in at screen line 160 by online, which writes R#2 and R#23 first, then SPD to hide the sprites. clip blanks the one sprite row the late SPD would show on line 160, as S19's does. Two things are new, both because the late part runs with interrupts on.

R#19 back to its own line. After online changes R#23 to 96, the R#19 left in place - scroll + 159 - would be counted against the new scroll: the line scroll + 63. At some scrolls that line lies below the picture, in the blanking, and its line flag would interrupt the late part and run online there, leaving the panel's page and scroll in force for the next picture (S15). So online writes R#19 = 255: under R#23 = 96 that is line 159, the line it is on, and a line already reached raises no flag that frame.

S#1 read at the end. Between the R#23 write and the R#19 write, for a moment, the chip has the new R#23 and the old R#19. At one scroll in 256, that pair names the very next line, and online was interrupted again as soon as it returned. A second online writes the same values and does no harm, but it is work the frame doesn't need. R#15 is still 1 inside online, so one IN A, (9) reads S#1 and clears the flag.

Sprites Built in the Main Loop

The page drawn in the late part is shown at the next frame. The late part draws the bolts and the dust where the main loop last put them, so the main loop's positions are for that page's picture: the picture after next, counting from when the main loop runs. The sprites have to match, or a bolt would reach a drone a frame before or after the drone does.

So the main loop works out everything for the picture after next. Its scroll is dscroll less one; the main loop calls it ahead. addspr turns each screen line into the chip's Y for that scroll, and steps round 216, as S19's sprites did:

addspr: PUSH HL
        LD HL, ahead
        DEC A
        ADD A, (HL)     ; Y = line - 1 + R#23 of that picture
        POP HL
        CP 216
        JR NZ, ad1
        INC A           ; a line lower instead

The list goes into one of two buffers, lista and listb, and is handed over at the end by changing one pointer, rlist - a single LD (rlist), HL the interrupt can't split. Then the next pass builds into the other buffer. onframe sends pend, the list handed over a frame earlier - the one for the page it is now showing - and takes the new one as pend for next time.

Sending is now a copy from RAM: 21 bytes, no decisions. All the thinking happened in the main loop, where it can take as long as it likes.

Digits by Blitter

S19 drew each digit with the CPU, eight font reads and 32 bytes written, and spread GAME OVER across frames because it was slow. Canyon draws the ten digits once, at the start, in lines 64-71 of the panel's page, which are never shown. From then on a digit is one HMMM: 8 dots by 8 lines from the glyph to its place in the panel.

digits keeps what each of the nine places shows in have, works out what it should show in want, and copies only the ones that differ. Most frames nothing changes; every eighth frame the distance's last digit does: one command. The scorch mark lives in the same hidden lines, at line 80, and the sprite tables in the last ones, from line 224. One page holds everything the panel and the blitter need.

The Code

; S20: Canyon - a bigger engine for the 256.
;
; You fly up a long canyon. Its 512 lines of scenery are kept in the 64K
; expansion and fed onto the screen a line a frame by the command engine.
; Two pages take turns: one is shown while the other is drawn for the next
; frame, scrolled one line further on. Steer with joystick A and fire.
; Shoot a drone for 10 points: it leaves a scorch mark on the canyon floor,
; and the mark is still there when the canyon comes round again. Q quits.
;
; What it uses, and where it was taught:
;   Graphics 4, two pages flipped by R#2                           (S13)
;   R#23 scrolls them; a new line fed in each frame                 (S14)
;   a line interrupt switches to the panel at line 160              (S15)
;   HMMM, LMMM with TIMP, MXS and MXD, CE                     (S16, S17)
;   sprite mode 2, a colour a line; Y with R#23 added, 216     (S8, S9, S19)
;   joystick A on ports 30h and 20h                                 (S10)
;
; Pages (command Y):  the panel's page 8000h (256-511, R#2 = 3Fh), with the
;   digit glyphs, the scorch mark and the sprite tables in its hidden lines;
;   page A 10000h (512-767, R#2 = 5Fh); page B 18000h (768-1023, R#2 = 7Fh).
; Memory: frame.asm uses 9000h-9203h and s15-lineint.asm 9140h-; this
; program keeps its undo lists at 9300h and a row buffer at 9500h.

SHIPY:  EQU 136         ; the ship's top line on screen
PLAY:   EQU 160         ; the playfield's height, above the panel
WAIT:   EQU 160         ; drones at lines 160-239 are out of sight, waiting
GLYPHY: EQU 256+64      ; the digit glyphs: the panel's page, line 64
MARKY:  EQU 256+80      ; the scorch mark: line 80
UNDOA:  EQU 9300h       ; page A's undo list: its end, then 4 bytes an entry
UNDOB:  EQU 9380h       ; page B's
ROW:    EQU 9500h       ; one line of the canyon, built before it is sent

        ORG 256

        ; --- Graphics 4, the display off while the scenery is made.
        ;     Interrupts stay as MOS left them until irqon: its clock tick
        ;     never touches the video chip.
        LD A, 06h
        LD B, 0
        CALL setreg     ; R#0: Graphics 4
        LD A, 02h
        LD B, 1
        CALL setreg     ; R#1: display off, 16x16 sprites
        LD A, 5Fh
        LD B, 2
        CALL setreg     ; R#2: page A
        XOR A
        LD B, 23
        CALL setreg     ; R#23 = 0

        ; --- the sprite tables, in the panel's page from line 224:
        ;     patterns 0F000h, colours 0F800h, attributes 0FA00h
        LD A, 3
        LD B, 14
        CALL setreg     ; R#14 = 3: 0C000h-0FFFFh
        LD HL, 3000h
        CALL vwrite
        LD HL, pats
        LD BC, 64*256+8         ; 2 patterns of 32 bytes
        OTIR
        LD HL, 3800h
        CALL vwrite
        LD HL, cols
        LD BC, 80*256+8         ; 5 sprites of 16 lines
        OTIR
        LD HL, 3A00h
        CALL vwrite
        LD A, 216               ; no sprites until the game starts
        OUT (8), A
        LD A, 0F7h
        LD B, 5
        CALL setreg     ; R#5 = F7h, R#11 = 01h: attributes 0FA00h
        LD A, 01h
        LD B, 11
        CALL setreg
        LD A, 1Eh
        LD B, 6
        CALL setreg     ; R#6 = 1Eh: patterns 0F000h

        ; --- the canyon into the expansion, the panel, then both pages
        CALL canyon
        CALL panel
        LD HL, fill1
        CALL blit       ; page A: strip lines 256-511
        LD HL, fill2
        CALL blit       ; page B: the same
        CALL cewait
        LD HL, UNDOA+2  ; both undo lists empty
        LD (UNDOA), HL
        LD HL, UNDOB+2
        LD (UNDOB), HL

        ; --- both interrupts (S15)
        CALL irqon
        CALL lineon
        LD A, 159
        LD B, 19
        CALL setreg     ; the split: line 159 + R#23
        LD A, 16h
        LD B, 0
        CALL setreg     ; R#0: Graphics 4, line interrupt on
        LD A, 0E2h
        LD B, 1
        CALL setreg     ; R#1: display, frame interrupt, 16x16 sprites
        EI

; --- the main loop: it works out the picture after next
main:   CALL waitf
        RST 8
        DEFB 181        ; MOS: a key, or 0
        CP 'Q'
        JR Z, quit
        LD A, (dscroll) ; the picture after next: one line further on
        DEC A
        LD (ahead), A
        LD HL, (dstrip)
        DEC HL
        LD A, H
        AND 1
        LD H, A
        LD (astrip), HL
        CALL steer      ; the joystick
        CALL fire       ; fire starts a bolt
        CALL climb      ; the bolts climb
        CALL drones     ; the drones fly down
        CALL hits       ; bolts against drones
        CALL specks     ; the dust falls
        CALL far        ; the distance flown
        CALL build      ; the sprite list for that picture
        LD A, 1
        LD (done), A    ; this frame's work done
        JR main

; --- back to MOS: line interrupt off first (S15), then MOS's screen
quit:   DI
        LD A, 06h
        LD B, 0
        CALL setreg     ; R#0 without IE1
        XOR A
        LD B, 45
        CALL setreg     ; R#45: no expansion
        LD A, 03h
        LD B, 6
        CALL setreg     ; R#6: MOS's sprite patterns, 1800h
        LD A, 0C0h
        CALL irqoff
        CALL tomos
        RET

; --- steer: joystick A, two dots a frame (bits 2 left, 3 right; 0 = held)
steer:  IN A, (30h)
        LD C, A
        LD HL, shipx
        BIT 2, C
        JR NZ, se1
        LD A, (HL)
        OR A
        JR Z, se1       ; at the left edge
        SUB 2
        LD (HL), A
se1:    BIT 3, C
        RET NZ
        LD A, (HL)
        CP 240
        RET NC          ; at the right edge
        ADD A, 2
        LD (HL), A
        RET

; --- fire: a bolt from the ship's nose, at most one every 8 frames
fire:   LD HL, cool
        LD A, (HL)
        OR A
        JR Z, fi1
        DEC (HL)
        RET
fi1:    IN A, (20h)
        BIT 1, A        ; joystick A's fire: 0 while pressed
        RET NZ
        LD HL, bolt
        LD B, 3
fi2:    LD A, (HL)
        CP 255
        JR Z, fi3       ; a free one
        INC HL
        INC HL
        DJNZ fi2
        RET             ; all three in flight
fi3:    LD (HL), SHIPY-6
        INC HL
        LD A, (shipx)
        ADD A, 6        ; two dots wide, at the ship's middle
        LD (HL), A
        LD A, 8
        LD (cool), A
        RET

; --- climb: each bolt climbs 4 lines a frame; off the top, it's gone
climb:  LD HL, bolt
        LD B, 3
cb1:    LD A, (HL)
        CP 255
        JR Z, cb3
        SUB 4
        JR NC, cb2
        LD A, 255
cb2:    LD (HL), A
cb3:    INC HL
        INC HL
        DJNZ cb1
        RET

; --- drones: down 2 lines a frame, drifting sideways. Off the bottom
;     they wait out of sight, then come in at the top somewhere new.
drones: LD HL, drone
        LD B, 4
dr1:    LD A, (HL)
        ADD A, 2
        CP WAIT         ; just gone off the bottom?
        JR NZ, dr2
        PUSH HL
        LD HL, seed
        CALL rnd
        POP HL
        AND 62
        ADD A, WAIT+2   ; wait a while
dr2:    LD (HL), A
        CP 240          ; 16 lines above the top: coming in
        CALL Z, launch
        INC HL
        LD A, (HL)      ; X
        INC HL
        ADD A, (HL)     ; + its drift, 1 or -1
        CP 8
        JR C, dr3
        CP 233
        JR C, dr4
dr3:    LD A, (HL)      ; at an edge: turn round
        NEG
        LD (HL), A
        DEC HL
        JR dr5
dr4:    DEC HL
        LD (HL), A
dr5:    INC HL
        INC HL
        DJNZ dr1
        RET

; --- launch: a new X for the drone at HL
launch: PUSH HL
        LD HL, seed
        CALL rnd
        POP HL
        AND 127
        ADD A, 48
        INC HL
        LD (HL), A
        DEC HL
        RET

; --- hits: each bolt against each drone in sight, by coordinates
hits:   LD HL, bolt
        LD B, 3
ht1:    LD A, (HL)
        CP 255
        JR Z, ht5       ; not in flight
        PUSH BC
        LD D, A         ; D = the bolt's Y
        INC HL
        LD E, (HL)      ; E = its X
        DEC HL
        PUSH HL
        LD HL, drone
        LD B, 4
ht2:    LD A, (HL)
        CP WAIT
        JR NC, ht4      ; out of sight
        LD A, D         ; their middles' lines, apart
        SUB (HL)
        SUB 5
        LD C, 10
        CALL near
        JR NC, ht4
        INC HL
        LD A, E         ; and across
        SUB (HL)
        DEC HL
        SUB 7
        LD C, 9
        CALL near
        JR NC, ht4
        CALL boom       ; a hit
        POP HL
        LD (HL), 255    ; the bolt is gone too
        PUSH HL
        JR ht6
ht4:    INC HL
        INC HL
        INC HL
        DJNZ ht2
ht6:    POP HL
        POP BC
ht5:    INC HL
        INC HL
        DJNZ ht1
        RET

; --- near: carry if the difference A is less than C, either way
near:   OR A
        JP P, nr1
        NEG
nr1:    CP C
        RET

; --- boom: the drone at HL is hit: 10 points, and a scorch mark where it was
boom:   PUSH HL
        PUSH DE
        LD A, (score+1) ; one more, in BCD (S19)
        ADD A, 1
        DAA
        LD (score+1), A
        LD A, (score)
        ADC A, 0
        DAA
        LD (score), A
        LD A, (HL)
        ADD A, 4
        LD C, A         ; C = the mark's top line on screen
        INC HL
        LD B, (HL)      ; B = its X
        DEC HL
        LD (HL), WAIT   ; the drone goes
        CALL mark
        POP DE
        POP HL
        RET

; --- mark: queue a scorch mark at screen line C, X B of the picture
;     after next. It is lasting: each page gets it when it is next drawn,
;     and the strip gets it too, so it comes round again.
mark:   LD A, (ahead)
        ADD A, C        ; its ring line
        CP 249
        RET NC          ; it would run past the page's last line: none
        LD (mring), A
        LD HL, (astrip)
        LD A, L
        ADD A, C
        LD L, A
        JR NC, mk1
        INC H
mk1:    LD A, H
        AND 1
        LD H, A         ; its strip line, 0-511
        JR Z, mk2
        LD A, L
        CP 249
        RET NC          ; past the strip's last line: none
mk2:    EX DE, HL       ; DE = the strip line
        LD HL, marks
        LD C, 4
mk3:    LD A, (HL)
        OR A
        JR Z, mk4       ; a free entry
        INC HL
        INC HL
        INC HL
        INC HL
        INC HL
        DEC C
        JR NZ, mk3
        RET             ; all four still being drawn: none
mk4:    INC HL
        LD A, (mring)
        LD (HL), A      ; ring line
        INC HL
        LD (HL), B      ; X
        INC HL
        LD (HL), E      ; strip line
        INC HL
        LD (HL), D
        DEC HL
        DEC HL
        DEC HL
        DEC HL
        LD (HL), 7      ; to do: page A (1), page B (2), the strip (4)
        RET

; --- specks: the dust falls 3 lines a frame, faster than the canyon
specks: LD HL, dust
        LD B, 6
sk1:    LD A, (HL)
        ADD A, 3
        CP PLAY
        JR C, sk2
        SUB PLAY        ; off the bottom: in again at the top
        PUSH AF
        PUSH HL
        LD HL, seed
        CALL rnd
        POP HL
        INC HL
        LD (HL), A      ; somewhere new
        DEC HL
        POP AF
sk2:    LD (HL), A
        INC HL
        INC HL
        DJNZ sk1
        RET

; --- far: the distance, one more every 8 frames, in BCD
far:    LD HL, farc
        INC (HL)
        LD A, (HL)
        AND 7
        RET NZ
        LD A, (dist+1)
        ADD A, 1
        DAA
        LD (dist+1), A
        LD A, (dist)
        ADC A, 0
        DAA
        LD (dist), A
        RET

; --- build: the sprite list for the picture after next - the ship, then
;     the drones - then hand it over. onframe sends it a frame later, with
;     the page drawn for the same picture.
build:  LD DE, (blist)
        LD A, (shipx)
        LD B, A
        LD C, 0         ; pattern 0: the ship
        LD A, SHIPY
        CALL addspr
        LD HL, drone
        LD C, 4         ; pattern 4: a drone
        LD A, 4
bu1:    PUSH AF
        LD A, (HL)
        INC HL
        LD B, (HL)
        INC HL
        INC HL
        CALL addspr
        POP AF
        DEC A
        JR NZ, bu1
        LD HL, (blist)  ; hand it over...
        LD (rlist), HL
        LD DE, lista    ; ...and build the other one next time
        OR A
        SBC HL, DE
        LD HL, listb
        JR Z, bu2
        LD HL, lista
bu2:    LD (blist), HL
        RET

; --- addspr: screen line A, X B, pattern C to the list at DE. The chip wants
;     Y = line - 1 + R#23, and Y = 216 would end the list (S19).
addspr: PUSH HL
        LD HL, ahead
        DEC A
        ADD A, (HL)
        POP HL
        CP 216
        JR NZ, ad1
        INC A           ; a line lower instead
ad1:    LD (DE), A
        INC DE
        LD A, B
        LD (DE), A
        INC DE
        LD A, C
        LD (DE), A
        INC DE
        RET

; --- onframe: the frame interrupt's work (frame.asm calls it).
onframe:
        LD A, (busy)
        OR A
        JP NZ, regs     ; still drawing the last frame: show the same again
        LD A, (done)
        XOR 1
        LD (behind), A  ; 1: the main loop didn't finish the last frame
        XOR A
        LD (done), A
        ; --- the early part, interrupts off: the registers, the sprites
        LD A, (drawn)
        LD (shown), A   ; show the page drawn last frame...
        LD A, (dscroll)
        LD (scroll), A  ; ...at the scroll it was drawn for
        CALL regs
        CALL sprites
        CALL clip
        ; --- the next frame: the other page, one line further on
        LD A, (shown)
        XOR 1
        LD (drawn), A
        LD HL, UNDOA
        LD BC, 4*256+2  ; page A: R#14 4 and 5, command Y 512 on
        OR A
        JR Z, of1
        LD HL, UNDOB
        LD BC, 6*256+3  ; page B: R#14 6 and 7, command Y 768 on
of1:    LD (ulist), HL
        LD A, B
        LD (base), A
        LD A, C
        LD (yhigh), A
        LD HL, dscroll
        DEC (HL)
        LD HL, (dstrip)
        DEC HL
        LD A, H
        AND 1
        LD H, A
        LD (dstrip), HL
        ; --- the late part, interrupts on: the line interrupt may cut in
        LD A, 1
        LD (busy), A
        EI
        CALL undo       ; this page's moving things, gone
        CALL domark     ; scorch marks still to do on this page
        CALL feed       ; the next line of canyon, into both pages
        CALL digits     ; the panel's changed digits
        CALL cewait
        CALL drawb      ; the bolts
        LD A, (behind)
        OR A
        CALL Z, drawd   ; the dust, unless the main loop is behind
        LD HL, (ulist)  ; where this page's undo list ends
        LD DE, (undop)
        LD (HL), E
        INC HL
        LD (HL), D
        DI
        XOR A
        LD (busy), A
        RET

; --- regs: the playfield's registers for the top of the picture
regs:   LD A, (shown)
        OR A
        LD A, 5Fh       ; page A...
        JR Z, rg1
        LD A, 7Fh       ; ...or page B
rg1:    LD B, 2
        CALL setreg
        LD A, (scroll)
        LD B, 23
        CALL setreg
        LD A, (scroll)
        ADD A, 159
        LD B, 19
        CALL setreg     ; the split stays at line 160
        LD A, 08h
        LD B, 8
        JP setreg       ; R#8: sprites on for the playfield

; --- online: line 159 - the panel from the next line, with no sprites (S19)
online: LD A, 3Fh
        OUT (9), A
        LD A, 128+2
        OUT (9), A      ; R#2: the panel's page
        LD A, 96
        OUT (9), A
        LD A, 128+23
        OUT (9), A      ; R#23: screen line 160 = panel line 0
        LD A, 0Ah
        OUT (9), A
        LD A, 128+8
        OUT (9), A      ; R#8 bit 1 (SPD): sprites off
        LD A, 255
        OUT (9), A
        LD A, 128+19
        OUT (9), A      ; R#19: this line again - no more this frame
        IN A, (9)       ; S#1 (R#15 is 1 here): clear a line flag these
        RET             ; writes raised on their way through

; --- sprites: the list handed over a frame ago, to the attribute table
sprites:
        LD A, 3
        LD B, 14
        CALL setreg
        LD HL, 3A00h
        CALL vwrite     ; 0FA00h
        LD HL, (pend)
        LD B, 5
sp1:    LD A, (HL)
        OUT (8), A      ; Y
        INC HL
        LD A, (HL)
        OUT (8), A      ; X
        INC HL
        LD A, (HL)
        OUT (8), A      ; pattern
        OUT (8), A      ; the fourth byte, unused
        INC HL
        DJNZ sp1
        LD A, 216
        OUT (8), A      ; the end of the list
        RET

; --- clip: as S19's. The SPD write lands a line late, so line 160 shows
;     the sprite rows on scrolled line 0 (160 + 96 = 256). Rewrite the
;     colour table with that row of each sprite in colour 0, not drawn.
clip:   LD HL, 3800h
        CALL vwrite     ; 0F800h: the colour table
        LD HL, cols
        LD DE, (pend)   ; the Y each sprite was sent with
        LD B, 5
cl1:    LD A, (DE)
        INC A
        NEG
        LD C, A         ; C = the row on scrolled line 0, if 0-15
        PUSH DE
        LD E, 0         ; E = the row
cl2:    LD A, E
        CP C
        LD A, (HL)
        JR NZ, cl3
        XOR A           ; that row: colour 0
cl3:    OUT (8), A
        INC HL
        INC E
        BIT 4, E
        JR Z, cl2       ; 16 rows a sprite
        POP DE
        INC DE
        INC DE
        INC DE
        DJNZ cl1
        LD HL, (rlist)  ; next frame: the list just handed over
        LD (pend), HL
        RET

; --- undo: put back, last first, the bytes this page's moving things
;     changed when it was last drawn
undo:   CALL cewait
        LD HL, (ulist)
        LD E, (HL)
        INC HL
        LD D, (HL)      ; DE = the list's end
        INC HL
        LD (undop), HL  ; its start: drawing fills it again
        EX DE, HL
ud1:    LD DE, (undop)
        LD A, L
        CP E
        JR NZ, ud2
        LD A, H
        CP D
        RET Z           ; back at the start: done
ud2:    DEC HL
        LD C, (HL)      ; the old byte
        DEC HL
        LD D, (HL)
        DEC HL
        LD E, (HL)      ; its address
        DEC HL
        LD A, (HL)      ; its R#14
        PUSH HL
        CALL vpage
        EX DE, HL
        CALL vwad
        LD A, C
        OUT (8), A
        POP HL
        JR ud1

; --- domark: each scorch mark still to do on this page or the strip:
;     LMMM with TIMP, from the panel's page, so the floor shows through
domark: LD HL, marks
        LD B, 4
ms1:    LD A, (HL)
        OR A
        JR Z, ms4       ; a free entry
        PUSH BC
        PUSH HL
        LD A, (drawn)
        INC A           ; 1 for page A, 2 for page B
        AND (HL)
        JR Z, ms2       ; not this page, or done
        CPL
        AND (HL)
        LD (HL), A      ; this page done
        INC HL
        LD A, (HL)
        LD (mcmd+6), A  ; DY: the ring line...
        LD A, (yhigh)
        LD (mcmd+7), A  ; ...in this page
        INC HL
        LD A, (HL)
        LD (mcmd+4), A  ; DX
        XOR A
        LD (mcmd+13), A ; ARG: main video memory
        LD HL, mcmd
        CALL blit
ms2:    POP HL
        PUSH HL
        BIT 2, (HL)
        JR Z, ms3       ; the strip's done
        RES 2, (HL)
        INC HL
        INC HL
        LD A, (HL)
        LD (mcmd+4), A  ; DX
        INC HL
        LD A, (HL)
        LD (mcmd+6), A
        INC HL
        LD A, (HL)
        LD (mcmd+7), A  ; DY: the strip line
        LD A, 20h
        LD (mcmd+13), A ; ARG: MXD, into the expansion
        LD HL, mcmd
        CALL blit
ms3:    POP HL
        POP BC
ms4:    LD DE, 5
        ADD HL, DE
        DJNZ ms1
        RET

; --- feed: the strip line for the next picture's top, from the
;     expansion into ring line R#23 - 1 of both pages (S14, S17)
feed:   LD HL, (dstrip)
        LD (fcmd+2), HL ; SY: the strip line
        LD A, (dscroll)
        LD (fcmd+6), A  ; DY: the ring line...
        LD A, 2
        LD (fcmd+7), A  ; ...in page A (512 on)
        LD HL, fcmd
        CALL blit
        LD A, 3
        LD (fcmd+7), A  ; ...and in page B (768 on)
        LD HL, fcmd
        JP blit

; --- digits: the score and the distance; a glyph copied for each digit
;     that has changed since it was last shown
digits: LD HL, want
        LD A, (score)
        CALL two
        LD A, (score+1)
        CALL two
        LD (HL), 0      ; points go up in tens
        INC HL
        LD A, (dist)
        CALL two
        LD A, (dist+1)
        CALL two
        XOR A
dg1:    LD (dgi), A
        LD E, A
        LD D, 0
        LD HL, want
        ADD HL, DE
        LD A, (HL)      ; the digit wanted here
        LD HL, have
        ADD HL, DE
        CP (HL)
        JR Z, dg2       ; already showing
        LD (HL), A
        ADD A, A
        ADD A, A
        ADD A, A
        LD (dcmd), A    ; SX: its glyph, 8 dots each
        LD HL, places
        ADD HL, DE
        LD A, (HL)
        ADD A, A
        ADD A, A
        ADD A, A
        LD (dcmd+4), A  ; DX: its column
        LD HL, dcmd
        CALL blit
dg2:    LD A, (dgi)
        INC A
        CP 9
        JR NZ, dg1
        RET

; --- two: the BCD byte A as two digits at HL
two:    PUSH AF
        RRCA
        RRCA
        RRCA
        RRCA
        AND 0Fh
        LD (HL), A
        INC HL
        POP AF
        AND 0Fh
        LD (HL), A
        INC HL
        RET

; --- drawb: each bolt, 6 lines of two light yellow dots
drawb:  LD HL, bolt
        LD B, 3
db1:    LD A, (HL)
        INC HL
        CP 255
        JR Z, db3       ; not in flight
        PUSH BC
        PUSH HL
        LD E, (HL)      ; X
        LD B, 6
db2:    PUSH AF
        PUSH BC
        PUSH DE
        LD D, 0         ; the whole byte...
        LD C, 0BBh      ; ...two dots of light yellow
        CALL plot
        POP DE
        POP BC
        POP AF
        INC A
        DJNZ db2
        POP HL
        POP BC
db3:    INC HL
        DJNZ db1
        RET

; --- drawd: each speck of dust, one white dot
drawd:  LD HL, dust
        LD B, 6
dd1:    LD A, (HL)      ; its line
        INC HL
        LD E, (HL)      ; its X
        INC HL
        PUSH BC
        PUSH HL
        LD D, 0Fh       ; an even X: the high nibble
        LD C, 0F0h
        BIT 0, E
        JR Z, dd2
        LD D, 0F0h      ; an odd X: the low one
        LD C, 0Fh
dd2:    CALL plot
        POP HL
        POP BC
        DJNZ dd1
        RET

; --- plot: on screen line A of the next picture, the byte holding dot E
;     becomes (old AND D) OR C, in the page being drawn, and the old byte
;     goes on its undo list
plot:   LD HL, dscroll
        ADD A, (HL)     ; the ring line
        SRL A
        LD H, A         ; ring / 2; carry = its bit 0
        LD A, E
        RRA
        LD L, A         ; address low: (ring AND 1) x 80h + X / 2
        LD A, H
        RLCA
        RLCA
        AND 1           ; the ring line's bit 7...
        PUSH HL
        LD HL, base
        ADD A, (HL)     ; ...picks R#14: base or base + 1
        POP HL
        RES 6, H        ; address high: (ring AND 127) / 2
        LD (pg), A
        LD (uad), HL
        CALL vpage
        CALL vrad
        IN A, (8)       ; the old byte
        LD (old), A
        AND D
        OR C
        LD C, A         ; the new one
        CALL vwad
        LD A, C
        OUT (8), A
        LD HL, (undop)  ; the undo entry: R#14, address, old byte
        LD A, (pg)
        LD (HL), A
        INC HL
        LD DE, (uad)
        LD (HL), E
        INC HL
        LD (HL), D
        INC HL
        LD A, (old)
        LD (HL), A
        INC HL
        LD (undop), HL
        RET

; --- vpage, vrad, vwad: R#14 = A; the read or write address HL. Each pair
;     of port 9 bytes under its own DI: the line interrupt may come between
;     pairs, never inside one.
vpage:  DI
        OUT (9), A
        LD A, 128+14
        OUT (9), A
        EI
        RET
vrad:   DI
        LD A, L
        OUT (9), A
        LD A, H
        OUT (9), A
        EI
        RET
vwad:   DI
        LD A, L
        OUT (9), A
        LD A, H
        OR 40h
        OUT (9), A
        EI
        RET

; --- blit: the 15-byte command at HL into R#32-R#46, once the last one
;     is done. One register at a time, each pair under its own DI.
blit:   CALL cewait
        LD B, 32
bl1:    LD A, (HL)
        DI
        OUT (9), A
        LD A, B
        OR 128
        OUT (9), A
        EI
        INC HL
        INC B
        LD A, B
        CP 47
        JR NZ, bl1
        RET

; --- cewait: until the command engine is done, one read at a time (S16),
;     the port 9 bytes written directly to keep each DI as short as it can be
cewait: DI
        LD A, 2
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 2: port 9 reads S#2
        IN A, (9)
        LD C, A
        XOR A
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 0
        EI
        BIT 0, C        ; CE: 1 while a command runs
        JR NZ, cewait
        RET

; --- vwrite: VRAM write address HL in the page R#14 selects (setup and
;     the early part only: interrupts are off there)
vwrite: LD A, L
        OUT (9), A
        LD A, H
        OR 40h
        OUT (9), A
        RET

; --- rnd: A = a random byte, from the 16-bit seed at HL (S19)
rnd:    PUSH DE
        LD E, (HL)
        INC HL
        LD D, (HL)
        SRL D
        RR E
        JR NC, rn1
        LD A, D
        XOR 0B4h
        LD D, A
rn1:    LD (HL), D
        DEC HL
        LD (HL), E
        LD A, E
        POP DE
        RET

; --- canyon: the 512-line strip, into the expansion by the CPU (S5)
canyon: LD A, 40h
        LD B, 45
        CALL setreg     ; R#45 bit 6: the CPU writes the expansion
        LD DE, 0        ; DE = the strip line, 0-511
cy1:    CALL crow       ; build it in ROW
        LD A, D         ; R#14 = line / 128
        ADD A, A
        LD C, A
        LD A, E
        RLCA
        AND 1
        OR C
        LD B, 14
        CALL setreg
        LD A, E         ; address (line AND 127) x 128
        RRCA
        AND 80h
        LD L, A
        LD A, E
        AND 7Fh
        SRL A
        LD H, A
        CALL vwrite
        LD HL, ROW
        LD BC, 128*256+8
        OTIR
        INC DE
        LD A, D
        CP 2
        JR NZ, cy1
        XOR A
        LD B, 45
        JP setreg       ; the CPU back to main video memory

; --- crow: strip line DE: grey walls, edged dark red, and an 85-byte
;     black floor between them, wandering left and right over the 512
;     lines. Every 64 lines a girder crosses it, its colour by quarter.
crow:   LD A, E         ; t = the line, or 511 - the line in the second half
        BIT 0, D
        JR Z, cr1
        CPL
cr1:    SRL A
        SRL A
        SRL A
        ADD A, 6
        LD C, A         ; C = the left wall, 6-37 bytes
        LD HL, ROW
        LD B, A
cr2:    LD (HL), 0EEh   ; grey rock
        INC HL
        DJNZ cr2
        DEC HL
        LD (HL), 0E6h   ; its edge: grey, dark red
        INC HL
        LD A, E
        AND 63
        CP 2
        LD A, 11h       ; the floor: black...
        JR NC, cr3
        PUSH HL         ; ...or a girder: line / 128 picks its colour
        LD A, D
        ADD A, A
        LD L, A
        LD A, E
        RLCA
        AND 1
        OR L
        LD L, A
        LD H, 0
        PUSH BC
        LD BC, girder
        ADD HL, BC
        POP BC
        LD A, (HL)
        POP HL
cr3:    LD B, 85
cr4:    LD (HL), A
        INC HL
        DJNZ cr4
        LD (HL), 6Eh    ; the right wall's edge
        INC HL
        LD A, 42        ; and the rest of it: 43 - left - 1 bytes
        SUB C
        LD B, A
cr5:    LD (HL), 0EEh
        INC HL
        DJNZ cr5
        LD HL, cseed    ; a grey speck on the floor, now and then
        CALL rnd
        CP 120
        RET NC
        AND 63
        ADD A, C
        ADD A, 8
        LD L, A
        LD H, ROW/256
        LD (HL), 1Eh
        RET

; --- panel: lines 0-127 of the panel's page (R#14 = 2): a white line,
;     then black, with the words; the digit glyphs at line 64, the scorch
;     mark at line 80 - out of sight, under the panel's 32 lines
panel:  LD A, 2
        LD B, 14
        CALL setreg
        LD HL, 0
        CALL vwrite
        LD A, 0FFh
        LD B, 128
pn1:    OUT (8), A
        DJNZ pn1
        LD DE, 127*128
pn2:    LD A, 11h
        OUT (8), A
        DEC DE
        LD A, D
        OR E
        JR NZ, pn2
        LD D, 12
        LD E, 1
        LD HL, words1
        CALL ptext
        LD E, 20
        LD HL, words2
        CALL ptext
        LD D, 64        ; the glyphs 0-9, columns 0-9, drawn once
        LD E, 0
        LD HL, nums
        CALL ptext
        LD HL, scorch   ; the scorch mark, a line at a time
        LD DE, 80*128
        LD A, 8
pn3:    PUSH AF
        PUSH HL
        EX DE, HL
        CALL vwrite
        LD BC, 128
        ADD HL, BC
        EX DE, HL
        POP HL
        LD BC, 8*256+8
        OTIR
        POP AF
        DEC A
        JR NZ, pn3
        RET

; --- ptext: the text at HL (ending 0) at column E, line D
ptext:  LD A, (HL)
        OR A
        RET Z
        CALL glyph
        INC HL
        INC E
        JR ptext

; --- glyph: character A at column E, lines D to D+7 of the panel's page,
;     white on black, from MOS's font (S15)
glyph:  PUSH HL
        PUSH DE
        LD L, A
        LD H, 0
        ADD HL, HL
        ADD HL, HL
        ADD HL, HL
        LD BC, 1800h
        ADD HL, BC      ; MOS's font: 1800h + code x 8
        LD C, 8
gl1:    PUSH BC
        XOR A
        LD B, 14
        CALL setreg
        LD A, L
        OUT (9), A
        LD A, H
        OUT (9), A      ; read address
        IN A, (8)
        LD C, A         ; the glyph's row
        LD A, 2
        LD B, 14
        CALL setreg
        LD A, D
        RRCA
        AND 80h
        LD B, A
        LD A, E
        ADD A, A
        ADD A, A
        OR B
        OUT (9), A
        LD A, D
        SRL A
        OR 40h
        OUT (9), A      ; write address: line D, byte E x 4
        LD B, 4
gl2:    LD A, 11h
        SLA C
        JR NC, gl3
        OR 0F0h
gl3:    SLA C
        JR NC, gl4
        OR 0Fh
gl4:    OUT (8), A
        DJNZ gl2
        POP BC
        INC HL
        INC D
        DEC C
        JR NZ, gl1
        POP DE
        POP HL
        RET

; --- the commands: SX, SY, DX, DY, NX, NY (two bytes each), CLR, ARG, CMR
fill1:  DEFW 0, 256, 0, 512, 256, 256   ; HMMM, strip lines 256-511 to page A
        DEFB 0, 10h, 0D0h               ;   (ARG 10h: MXS, from the expansion)
fill2:  DEFW 0, 256, 0, 768, 256, 256   ; the same to page B
        DEFB 0, 10h, 0D0h
fcmd:   DEFW 0, 0, 0, 0, 256, 1         ; HMMM: one strip line, fed
        DEFB 0, 10h, 0D0h
mcmd:   DEFW 0, MARKY, 0, 0, 16, 8      ; LMMM TIMP: the scorch mark
        DEFB 0, 0, 98h
dcmd:   DEFW 0, GLYPHY, 0, 256+12, 8, 8 ; HMMM: a digit to the panel
        DEFB 0, 0, 0D0h

; --- the engine's state
shown:  DEFB 0          ; the page on screen: 0 A, 1 B
scroll: DEFB 0          ; its R#23
drawn:  DEFB 0          ; the page being drawn, for the next frame...
dscroll: DEFB 0         ; ...and its R#23 there
dstrip: DEFW 256        ; the strip line at the top of that picture
base:   DEFB 4          ; the drawn page's R#14s: 4 and 5, or 6 and 7
yhigh:  DEFB 2          ; its command Y's high byte: 2 (512), 3 (768)
ulist:  DEFW UNDOA      ; its undo list
undop:  DEFW UNDOA+2    ; the list's next free entry
busy:   DEFB 0          ; 1 while onframe's late part runs
done:   DEFB 1          ; 1 once the main loop has finished its frame
behind: DEFB 0          ; 1 if it hadn't, at this frame
pend:   DEFW lista      ; the sprite list to send next frame
rlist:  DEFW lista      ; the list just handed over
blist:  DEFW listb      ; the list being built
pg:     DEFB 0          ; plot's R#14...
uad:    DEFW 0          ; ...address...
old:    DEFB 0          ; ...and old byte
dgi:    DEFB 0          ; digits' counter
want:   DEFB 0, 0, 0, 0, 0, 0, 0, 0, 0
have:   DEFB 255, 255, 255, 255, 255, 255, 255, 255, 255
places: DEFB 7, 8, 9, 10, 11, 25, 26, 27, 28   ; the digits' columns

; --- the game's state (the main loop's)
ahead:  DEFB 0          ; R#23 of the picture after next
astrip: DEFW 0          ; and the strip line at its top
shipx:  DEFB 120
cool:   DEFB 0          ; frames until the next bolt
score:  DEFB 0, 0       ; BCD, in tens
dist:   DEFB 0, 0       ; BCD
farc:   DEFB 0
mring:  DEFB 0
seed:   DEFW 0ACE1h     ; random numbers for the game...
cseed:  DEFW 1D2Bh      ; ...and for the canyon
bolt:   DEFB 255, 0, 255, 0, 255, 0     ; Y (255: none), X
drone:  DEFB WAIT, 64, 1                ; Y, X, drift
        DEFB WAIT+20, 100, 255
        DEFB WAIT+40, 140, 1
        DEFB WAIT+60, 180, 255
dust:   DEFB 10, 40, 40, 90, 70, 150, 100, 200, 130, 120, 150, 60  ; Y, X
marks:  DEFB 0, 0, 0, 0, 0      ; to do (bits: A, B, strip), ring, X, strip
        DEFB 0, 0, 0, 0, 0
        DEFB 0, 0, 0, 0, 0
        DEFB 0, 0, 0, 0, 0
words1: DEFB "SCORE", 0
words2: DEFB "DIST", 0
nums:   DEFB "0123456789", 0
girder: DEFB 0AAh, 0CCh, 088h, 055h     ; dark yellow, dark green, red, light blue

; two sprite lists: Y, X, pattern for the ship and 4 drones
lista:  DEFB 216, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
listb:  DEFB 216, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

; the scorch mark: 16 x 8 dots - a grey rim, dark red inside, black
; specks - with colour 0 (transparent) round it
scorch: DEFB 000h, 000h, 0EEh, 0E0h, 00Eh, 0EEh, 000h, 000h
        DEFB 000h, 0EEh, 066h, 0EEh, 0EEh, 06Eh, 0E0h, 000h
        DEFB 00Eh, 066h, 066h, 066h, 066h, 066h, 06Eh, 000h
        DEFB 0E6h, 061h, 066h, 066h, 066h, 061h, 066h, 0E0h
        DEFB 0E6h, 066h, 066h, 066h, 066h, 066h, 066h, 0E0h
        DEFB 00Eh, 066h, 061h, 066h, 066h, 066h, 06Eh, 000h
        DEFB 000h, 0EEh, 066h, 066h, 06Eh, 0EEh, 0E0h, 000h
        DEFB 000h, 000h, 0EEh, 0EEh, 0E0h, 000h, 000h, 000h

; colour bytes, 16 lines a sprite (S9)
cols:   DEFB 15, 15, 7, 7, 7, 7, 5, 5           ; 0, the ship
        DEFB 5, 5, 2, 2, 2, 2, 11, 10
        DEFB 7, 7, 15, 15, 8, 8, 15, 8          ; 1-4, the drones
        DEFB 6, 6, 14, 14, 14, 0, 0, 0
        DEFB 7, 7, 15, 15, 8, 8, 15, 8
        DEFB 6, 6, 14, 14, 14, 0, 0, 0
        DEFB 7, 7, 15, 15, 8, 8, 15, 8
        DEFB 6, 6, 14, 14, 14, 0, 0, 0
        DEFB 7, 7, 15, 15, 8, 8, 15, 8
        DEFB 6, 6, 14, 14, 14, 0, 0, 0

; 16x16 patterns, a quarter at a time: top-left, bottom-left, top-right,
; bottom-right. 1 = a dot.
pats:
; the ship (pattern 0): top-left
        DEFB 00000001b
        DEFB 00000011b
        DEFB 00000011b
        DEFB 00000111b
        DEFB 00000111b
        DEFB 00001111b
        DEFB 00011111b
        DEFB 00111111b
; the ship (pattern 1): bottom-left
        DEFB 01111111b
        DEFB 11111111b
        DEFB 11110111b
        DEFB 11100011b
        DEFB 00000011b
        DEFB 00000001b
        DEFB 00000011b
        DEFB 00000001b
; the ship (pattern 2): top-right
        DEFB 10000000b
        DEFB 11000000b
        DEFB 11000000b
        DEFB 11100000b
        DEFB 11100000b
        DEFB 11110000b
        DEFB 11111000b
        DEFB 11111100b
; the ship (pattern 3): bottom-right
        DEFB 11111110b
        DEFB 11111111b
        DEFB 11101111b
        DEFB 11000111b
        DEFB 11000000b
        DEFB 10000000b
        DEFB 11000000b
        DEFB 10000000b
; a drone (pattern 4): top-left
        DEFB 00000011b
        DEFB 00001111b
        DEFB 00011110b
        DEFB 00111100b
        DEFB 01111111b
        DEFB 11111111b
        DEFB 11011011b
        DEFB 11111111b
; a drone (pattern 5): bottom-left
        DEFB 01111111b
        DEFB 00111111b
        DEFB 00011000b
        DEFB 00110000b
        DEFB 01100000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
; a drone (pattern 6): top-right
        DEFB 11000000b
        DEFB 11110000b
        DEFB 01111000b
        DEFB 00111100b
        DEFB 11111110b
        DEFB 11111111b
        DEFB 01101011b
        DEFB 11111111b
; a drone (pattern 7): bottom-right
        DEFB 11111110b
        DEFB 11111100b
        DEFB 00011000b
        DEFB 00001100b
        DEFB 00000110b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b

        INCLUDE "frame.asm"
        INCLUDE "s15-lineint.asm"

Build and run

Save it as s20-canyon.asm next to frame.asm and s15-lineint.asm. Build and run it as in S2. Joystick A steers and fires; Q quits.

What you should see

The screen is blank for a moment while the canyon is made. Then grey canyon walls, edged in dark red, with a black floor between them, scattered with grey specks and falling white dust. The whole picture moves down a line every frame, and the canyon swings left and right as it comes in at the top. Girders cross the floor every 64 lines, dark yellow, dark green, red or light blue depending on which quarter of the canyon you're in. Your ship sits near the bottom. Below, after a white line, the panel reads SCORE 00000 and DIST, counting up.

These two pictures are one frame apart, one from each page. The canyon in the second is exactly one line lower; only the ship, the bolts and the dust differ:

The canyon at the start: grey walls, a black floor with girders, the ship near the bottom, the panel underneath

One frame later, from the other page: everything one line lower

Hold fire and light yellow bolts climb from the ship's nose. Drones - red and white saucers with a cyan dome - fly down the canyon, drifting from side to side. Hit one and it vanishes, the score goes up by 10, and a scorch mark is left on the floor where it was:

The canyon with drones, bolts and two scorch marks on the floor, SCORE 00050

Magnified, a mark is a grey rim round dark red, with black specks, and the floor shows round its edges:

A scorch mark, magnified

512 frames later the same stretch of canyon comes round again, with the same marks in the same places - and any new ones:

512 frames later: the same walls and girders, the two earlier scorch marks in the same places and two new ones, SCORE 00100

Press Q and MOS's screen comes back, with the register display and the prompt:

MOS's screen after the program: the register display and the prompt

Change One Thing

  • In onframe, after CALL digits, add a loop that wastes time: LD BC, 1500, then wz1: DEC BC, LD A, B, OR C, JR NZ, wz1. Now the late part runs past the panel's line. Where does the panel appear? Watch its white line closely: is it always white, and why not?
  • Make the loop LD BC, 3500. What happens to the speed of the canyon, and to the bolts and drones against it? Does any picture show a half-drawn page?
  • Put the same loop in the main loop instead, after CALL build, with LD BC, 4000 (call its label wz2). What happens to the dust, and why only the dust?
  • In mark, change LD (HL), 7 to LD (HL), 3. Shoot a few drones, then wait for the canyon to come round. What is missing, and why are the marks still there in the meantime?

When It Goes Wrong

SymptomCause
Rows of the ship or a drone on the panel's white line, now and thenThe sprites were hidden with an empty attribute table switched in by R#5 (S15's tables for bands) instead of SPD. In this handler R#5 is written after R#2 and R#23, and lands too late for line 160. Use SPD and clip, as S19
The panel's white line broken in almost every pictureR#5 (or SPD) written before R#2 and R#23 in online. The panel's two registers go first (S15)
With the late part made long, the panel's first lines show canyon in most picturesThe late part ran with interrupts off throughout - no EI at its start, none after each pair - so the line interrupt waited until it ended. Turn interrupts on for the late part and keep each DI to one port 9 pair
The program stops after a few secondsCALL undo left out: nothing empties the undo lists, and plot writes on past their end, over the program's other memory
The assembler stops with duplicate definition of labelOne of your labels is also used in frame.asm or s15-lineint.asm (st1, ls1, wf1 ...). Rename yours

Summary

You can now draw more than fits between two pictures. The early part of the frame sets the registers and the sprites with interrupts off; the late part draws the hidden page with interrupts on, one port 9 pair at a time, and the line interrupt cuts in when it must. Lasting things go into both pages, each when it is next drawn; moving things go into one, with an undo list per page. Scenery lives in the expansion and comes in a line a frame by HMMM, and LMMM with MXD can change it for good. The main loop works for the picture after next and hands its sprite list over by one pointer. And two flags, busy and done, tell the engine when to show a picture again and what to leave out.

Next

S21, Where You Go From Here: what the primer leaves out - including how much the blitter can do in one frame - and what to try next.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.