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

Splitting the Screen

Introduction

S14's scroll moves the whole screen. A game almost always wants something that doesn't move: the score, the lives, the fuel gauge. On the TC-01 the video chip draws every line the same way, and all your program hears from it is the frame flag, once a frame (the assembly course's S26).

The 256's chip will interrupt your program at a line you name. At that moment you can change the registers that decide what the rest of the picture looks like: which page is shown, where it is scrolled to, the backdrop colour. The top of the screen is drawn one way and the bottom another. And it will interrupt again further down the same picture, several times over, so the screen can be cut into bands that each scroll at their own speed.

The Line Interrupt

Three things set it up:

R#19the line to interrupt at
R#0 bit 4 (IE1)the line interrupt on. Graphics 4 with it is R#0 = 16h
S#1 bit 0 (FH)the line flag: set when that line is reached, cleared when you read S#1

It comes through the same IM 2 vector as the frame interrupt, FFh, so one handler gets both. It tells them apart by reading S#1. If FH is set it is the line; if not, it is the frame, and frame.asm's handler takes over as usual, reading S#0.

Two rules about where the split lands:

R#19 counts lines of the scrolled picture, not lines of the screen. With R#23 = 16, R#19 = 100 interrupts at screen line 84, where picture line 100 is showing. So when the playfield scrolls, add R#23 to R#19 every frame, or the split scrolls with it.

A change made in the line handler shows from the next line. So ask for the line above the one you want. To start the panel at screen line 160, write R#19 = 159 + R#23.

lineint.asm

The handler is short, and you will want it again in S19, so it is a file of its own that you INCLUDE after frame.asm. Save it as s15-lineint.asm.

; s15-lineint.asm - adds the line interrupt to frame.asm, for any program
; that INCLUDEs both. (S15 explains every line.)
;
; Both of the video chip's interrupts come through the same IM 2 vector,
; FFh. This file puts its own entry stub on that vector. The stub reads
; S#1: if the line flag (FH, bit 0) is set, it is the line interrupt and
; it calls the program's online; if not, it is the frame interrupt, and
; frame.asm's frame does the usual work (and calls onframe). After the
; frame, if the picture has not begun yet, it reads S#1 again to clear a
; line flag raised outside the picture.
;
; Your program must define, as well as onframe:
;   online    - its work at the line in R#19. Called from the interrupt;
;               it may use AF only (save anything else it needs), must
;               not read port 9 (R#15 is 1 while it runs), must not call
;               MOS, and should be quick: the picture is being drawn.
; and use, in order:
;   irqon, then lineon - both tables' entries in place (interrupts off)
;   set R#19, R#0 with bit 4 (IE1) on, R#1 with bit 5 on, then EI
;   on the way out: R#0 with bit 4 off, under DI, BEFORE irqoff

LENTRY: EQU 9140h       ; this stub's home: above 8000h, clear of frame.asm's

; --- lineon: copy the stub up and point vector FFh at it
lineon: LD HL, lstub
        LD DE, LENTRY
        LD BC, lstubend-lstub
        LDIR
        LD HL, LENTRY
        LD (TABLE+255), HL
        RET

; --- lstub: copied to LENTRY. As frame.asm's stub: if the interrupt came
;     during a MOS call, page the ROM out first, and back in after.
lstub:  PUSH AF
        IN A, (26h)
        BIT 1, A        ; 1 = the ROM is in
        JR Z, ls1
        OUT (24h), A    ; ROM out
        CALL vdpint
        OUT (24h), A    ; ROM back in
        POP AF
        EI
        RETI
ls1:    CALL vdpint
        POP AF
        EI
        RETI
lstubend:

; --- vdpint: which interrupt is it? S#1 bit 0 (FH) says the line.
;     The line's work goes first, before anything else: the picture does
;     not wait. R#15 is still 1 while online runs.
vdpint: LD A, 1
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 1
        IN A, (9)       ; S#1 - reading it clears FH
        RRCA            ; FH into carry
        JR NC, notln
        CALL online     ; the line
        XOR A
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 0 again
        RET
notln:  XOR A
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 0 again, before frame reads S#0
        CALL frame      ; the frame: frame.asm reads S#0 and calls onframe
        LD A, 2         ; still outside the picture? (S#2 bit 6, VR)
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 2
        IN A, (9)       ; S#2
        AND 40h
        JR Z, nl1       ; no: the picture has begun, leave FH alone
        LD A, 1
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 1
        IN A, (9)       ; S#1: a line flag raised outside the picture, cleared
nl1:    XOR A
        OUT (9), A
        LD A, 128+15
        OUT (9), A      ; R#15 = 0
        RET

lineon replaces frame.asm's entry on vector FFh with its own. Like frame.asm's, the stub is copied above 8000h: an interrupt that arrives during a MOS call finds the ROM over the bottom 32K, where the rest of your program lives, so the stub pages it out first (port 26h bit 1 says it is in, port 24h swaps it) and back in afterwards.

Then vdpint reads S#1 with R#15 = 1. If the line flag is set it calls your online straight away, before it even puts R#15 back. The picture doesn't wait for your handler: the chip is drawing the next line while the Z80 is still working out which interrupt it has. A handler that does a few more things first makes its changes land late, and a register written second can miss the line the first one made. So online does only the visible change, writes registers directly with OUT, and returns.

Only then does vdpint set R#15 back to 0. On the frame path that has to happen before frame reads port 9, or frame reads S#1 instead of S#0, the frame flag is never cleared, and the interrupt comes straight back.

After frame returns there is one more step. The chip's line count doesn't stop at the bottom of the picture. If the line R#19 asks for, less R#23, falls below the picture's last line, the line flag rises after the picture. If that happens while frame is still running with interrupts off, the flag waits, and the line interrupt arrives the moment the stub's EI comes: still outside the picture. online would then make its change for the whole of the next picture, not just the bottom of it. The panel later in this section runs into this once every 256 frames, because online changes R#23 part-way down. So vdpint reads S#2 with R#15 = 2. Its bit 6 (VR) is 1 from the frame interrupt until the picture starts. If VR is still 1, any line flag set now was raised outside the picture, and reading S#1 clears it. If the picture has begun, the flag is left alone. Then R#15 goes back to 0.

On the way out, turn IE1 off first. frame.asm's irqoff puts MOS's table back, and vector FFh in MOS's table lands in the middle of MOS. A line interrupt still enabled then throws your program back to MOS, as an enabled frame interrupt did in S3. Write R#0 without bit 4, under DI, before irqoff.

A Colour Split

The simplest split changes one colour. In Graphics 4 every dot of colour 0 shows the backdrop, R#7. So draw sky and sea in colour 0, set R#7 to cyan in the frame interrupt and to blue in the line interrupt, and the screen is cyan above the line and blue below it. Move R#19 each frame and the waterline rises and falls.

The frame handler sets the colour for the top of the next picture. The backdrop also colours the border, so the border above the picture is cyan, and so is the border below it, apart from a line or two of blue just under the picture.

The Code: A Tide

A green rock stands in the sea. The tide comes in and goes out: the waterline climbs from line 163 to line 100 and back, a line a frame.

; S15: a colour split. One picture, two backdrop colours: sky above a
; line, sea below it - and the line rises and falls, like a tide.
;
; Everything in the picture that is colour 0 shows the backdrop, R#7.
; The frame interrupt sets R#7 to cyan for the top of the picture; the
; line interrupt, at the line in R#19, sets it to blue. Moving R#19 each
; frame moves the waterline. Runs for 500 frames (10 seconds).

        ORG 256

        DI
        ; --- Graphics 4, showing the page at 10000h
        LD A, 06h
        LD B, 0
        CALL setreg
        LD A, 5Fh
        LD B, 2
        CALL setreg

        ; --- sprites out of the way (attribute table at 1FA00h: Y = 216)
        LD A, 7
        LD B, 14
        CALL setreg
        XOR A
        OUT (9), A
        LD A, 3Ah + 40h
        OUT (9), A
        LD A, 216
        OUT (8), A
        LD A, 0F7h
        LD B, 5
        CALL setreg
        LD A, 03h
        LD B, 11
        CALL setreg

        ; --- the picture: all colour 0, with an island of colour 12
        LD A, 4
        LD B, 14
        CALL setreg     ; 10000h
        XOR A
        OUT (9), A
        LD A, 40h
        OUT (9), A      ; offset 0, writing: the run carries on in G4
        LD D, 0         ; D = line
pic1:   LD E, 0         ; E = byte across
pic2:   XOR A           ; colour 0 ...
        LD C, A
        LD A, D
        CP 60           ; ... except lines 60-191, bytes 40-87
        JR C, pic3
        LD A, E
        CP 40
        JR C, pic3
        CP 88
        JR NC, pic3
        LD C, 0CCh      ; the island
pic3:   LD A, C
        OUT (8), A
        INC E
        BIT 7, E
        JR Z, pic2      ; 128 bytes a line
        INC D
        LD A, D
        CP 192
        JR NZ, pic1

        ; --- both interrupts
        CALL irqon
        CALL lineon
        LD A, 120
        LD B, 19
        CALL setreg     ; a line to start with
        LD A, 16h       ; R#0: Graphics 4 with IE1, the line interrupt
        LD B, 0
        CALL setreg
        LD A, 0E0h      ; R#1: display on, frame interrupt on
        LD B, 1
        CALL setreg
        EI

main:   CALL waitf
        LD HL, (TICKS)
        LD DE, 500
        OR A
        SBC HL, DE
        JR C, main

        ; --- put the machine back: the line interrupt off first
        DI
        LD A, 06h       ; R#0 without IE1
        LD B, 0
        CALL setreg
        LD A, 4         ; R#7: MOS's backdrop, colour 4
        LD B, 7
        CALL setreg
        LD A, 0C0h
        CALL irqoff
        CALL tomos
        RET

; --- onframe: sky for the top of the next frame; move the waterline
onframe:
        LD A, 7         ; R#7 = colour 7, cyan
        LD B, 7
        CALL setreg
        LD A, (TICKS)   ; t = frames mod 128, folded: 0-63, then back down
        AND 7Fh
        CP 64
        JR C, of1
        CPL
        AND 7Fh
of1:    ADD A, 100      ; the waterline: lines 100-163
        DEC A           ; ask for the line above: the change shows a line late
        LD B, 19
        JP setreg

; --- online: the waterline - the backdrop turns blue from here down
online: LD A, 4         ; colour 4, blue
        OUT (9), A
        LD A, 128+7
        OUT (9), A
        RET

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

Build and run

Save it as s15-colour-split.asm next to frame.asm and s15-lineint.asm. Build and run it as in S2.

What you should see

A cyan sky, a dark green rock in the middle, and blue sea below a straight waterline right across the screen. The waterline moves a line every frame, falling for a little over a second and then rising, again and again. Below the picture the border is cyan, after a line or two of blue. After ten seconds MOS's screen and the prompt.

A green rock, cyan sky above, blue sea below a straight waterline

A Panel That Stays Still

The colour split changed one register. The score panel changes two:

  • R#2, to show a different page below the split: the panel lives in the page at 18000h (R#2 = 7Fh), the playfield in the page at 10000h (5Fh).
  • R#23, so the panel starts from its own line 0. The split shows from screen line 160, and line 160 + R#23 must be 256, which wraps to 0: so R#23 = 96.

The frame interrupt puts both back for the top of the next frame (R#2 = 5Fh, R#23 = the scroll), asks for the next line interrupt at 159 + the scroll, and feeds the playfield's next line as in S14. The playfield shows only 160 lines now, but its page still loops through all 256, so nothing else in S14's scroll changes.

What it costs: the panel's 32 lines take 4K of the page at 18000h; the rest of that page is free. The handlers cost two interrupts a frame instead of one.

The panel's text comes from MOS's own font, which is still in video memory at 1800h while your program runs: 8 bytes a character, one byte a row, a set bit for each dot of ink. glyph reads a character's 8 bytes and turns each into four Graphics 4 bytes.

The Code: River and Panel

; S15: a still score panel under a scrolling playfield.
; The top 160 lines are S14's river, scrolling up a line a frame. The
; bottom 32 lines are a panel that never moves. Runs for 500 frames.
;
; The playfield is the page at 10000h (R#2 = 5Fh), scrolled by R#23.
; The panel is 32 lines at the top of the page at 18000h (R#2 = 7Fh).
; At line 159 the line interrupt switches R#2 to the panel's page and
; R#23 to 96, so that screen line 160 shows panel line 0 (160 + 96 =
; 256, which wraps to 0). The frame interrupt puts both back for the
; top of the next frame, and asks for the next line interrupt.

        ORG 256

        DI
        ; --- Graphics 4 first, so VRAM runs carry on past 16K
        LD A, 06h
        LD B, 0
        CALL setreg
        LD A, 5Fh
        LD B, 2
        CALL setreg
        XOR A
        LD B, 23
        CALL setreg

        ; --- sprites out of the way (attribute table at 1FA00h: Y = 216)
        LD A, 7
        LD B, 14
        CALL setreg
        XOR A
        OUT (9), A
        LD A, 3Ah + 40h
        OUT (9), A
        LD A, 216
        OUT (8), A
        LD A, 0F7h
        LD B, 5
        CALL setreg
        LD A, 03h
        LD B, 11
        CALL setreg

        ; --- the panel, before the river: its text comes from MOS's
        ;     font, still in VRAM at 1800h
        CALL panel

        ; --- the river: rows 0-255 into lines 0-255
        LD HL, 0
first:  LD A, L
        CALL drawrow
        INC HL
        LD A, H
        OR A
        JR Z, first

        ; --- both interrupts
        CALL irqon
        CALL lineon
        LD A, 159       ; the first split: line 159, no scroll yet
        LD B, 19
        CALL setreg
        LD A, 16h       ; R#0: Graphics 4 with IE1
        LD B, 0
        CALL setreg
        LD A, 0E0h      ; R#1: display on, frame interrupt on
        LD B, 1
        CALL setreg
        EI

main:   CALL waitf
        LD HL, (TICKS)
        LD DE, 500
        OR A
        SBC HL, DE
        JR C, main

        ; --- put the machine back: the line interrupt off first
        DI
        LD A, 06h
        LD B, 0
        CALL setreg
        LD A, 0C0h
        CALL irqoff
        CALL tomos
        RET

; --- online: line 159 - from the next line down, show the panel
online: LD A, 7Fh       ; the panel's page
        OUT (9), A
        LD A, 128+2
        OUT (9), A
        LD A, 96        ; screen line 160 = panel line 0
        OUT (9), A
        LD A, 128+23
        OUT (9), A
        RET

; --- onframe: the top of the next frame - the playfield, scrolled on
onframe:
        LD A, 5Fh
        LD B, 2
        CALL setreg     ; the playfield's page
        LD HL, (TICKS)
        LD A, L
        LD B, 23
        CALL setreg     ; R#23 = the scroll
        LD A, L
        ADD A, 159      ; R#19 counts lines after the scroll: add it
        LD B, 19
        CALL setreg
        DEC HL          ; feed the line just scrolled off, as in S14
        LD A, L
        INC H
        JP drawrow

; --- panel: lines 0-31 of the page at 18000h. Line 0 white, the rest
;     black (colour 1), with the text on lines 12-19.
panel:  LD A, 6
        LD B, 14
        CALL setreg     ; 18000h
        XOR A
        OUT (9), A
        LD A, 40h
        OUT (9), A
        LD A, 0FFh      ; line 0: white
        LD B, 128
pn1:    OUT (8), A
        DJNZ pn1
        LD DE, 31*128   ; lines 1-31: black
pn2:    LD A, 11h
        OUT (8), A
        DEC DE
        LD A, D
        OR E
        JR NZ, pn2
        LD HL, text     ; then the text, a character at a time
        LD E, 4         ; E = the character column (8 dots each)
pn3:    LD A, (HL)
        OR A
        RET Z
        CALL glyph
        INC HL
        INC E
        JR pn3

; --- glyph: character A at column E, lines 12-19 of the panel. HL, E kept.
;     Each of the font's 8 bytes is 8 dots, 1 = ink; in Graphics 4 that
;     is 4 bytes, two dots each, the left dot in the high nibble.
glyph:  PUSH HL
        PUSH DE
        LD L, A         ; the font byte address: 1800h + code * 8
        LD H, 0
        ADD HL, HL
        ADD HL, HL
        ADD HL, HL
        LD BC, 1800h
        ADD HL, BC
        LD D, 12        ; D = panel line
gl1:    XOR A
        LD B, 14
        CALL setreg     ; MOS's font is in the first 16K
        LD A, L
        OUT (9), A
        LD A, H
        OUT (9), A      ; read address (no 40h)
        IN A, (8)
        LD C, A         ; C = the glyph's row
        LD A, 6
        LD B, 14
        CALL setreg     ; the panel's 16K
        LD A, D         ; offset = line * 128 + column * 4
        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
        LD B, 4         ; 4 bytes of 2 dots
gl2:    LD A, 11h       ; both dots black ...
        SLA C
        JR NC, gl3
        OR 0E0h         ; ... left dot colour 15 (1 OR E = F)
gl3:    SLA C
        JR NC, gl4
        OR 0Eh          ; right dot colour 15
gl4:    OUT (8), A
        DJNZ gl2
        INC HL
        INC D
        LD A, D
        CP 20
        JR NZ, gl1
        POP DE
        POP HL
        RET

text:   DEFB "SCORE 001230    LIVES 3", 0

; --- drawrow: river row HL on VRAM line A of the page at 10000h (S14)
drawrow:
        PUSH HL
        PUSH AF
        RLCA
        AND 1
        ADD A, 4
        LD B, 14
        CALL setreg
        POP AF
        AND 7Fh
        RRCA
        LD C, A
        AND 80h
        OUT (9), A
        LD A, C
        AND 3Fh
        OR 40h
        OUT (9), A
        POP HL
        PUSH HL
        LD A, L
        AND 7Fh
        CP 64
        JR C, dr1
        CPL
        AND 7Fh
dr1:    ADD A, 8
        LD D, A
        LD A, L
        AND 1Fh
        LD E, 44h
        JR NZ, dr2
        LD E, 0FFh
dr2:    LD C, 128
        LD A, 0CCh
        LD B, D
dr3:    OUT (8), A
        DEC C
        DJNZ dr3
        LD A, E
        LD B, 48
dr4:    OUT (8), A
        DEC C
        DJNZ dr4
        LD A, 0CCh
        LD B, C
dr5:    OUT (8), A
        DJNZ dr5
        POP HL
        RET

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

Build and run

Save it as s15-split-screen.asm next to frame.asm and s15-lineint.asm. Build and run it as in S2.

What you should see

S14's river, scrolling up a line every frame, in the top 160 lines. Below it, starting with one white line, a black panel 32 lines deep reading SCORE 001230 LIVES 3. The river scrolls; the panel doesn't move at all, and the join stays on the same line in every frame. After ten seconds, MOS's screen and the prompt.

The scrolling river above a still black panel reading SCORE 001230 LIVES 3

Where R#19 Points Now

There is one thing the panel's online doesn't do. It changes R#23, and leaves R#19 alone. The chip goes on comparing R#19 with the line it is drawing, but it counts that line with the new R#23. So after the split, the line it waits for is R#19 - 96, which is the scroll + 63, not the scroll + 159.

Where that falls depends on the scroll. If it is a line of the panel still to come, the chip interrupts again there, online runs a second time and writes the same two values, and nothing on the screen changes. If it falls below the picture, the line flag rises outside the picture: that is the case lineint.asm's clear deals with.

So here is the rule. Whenever a line handler changes R#23, work out where R#19 now points. Either that is harmless, as in the panel, or the handler writes R#19 again: to the line of the next split, counted with the new scroll, or, if there is no next split, to its own line counted with the new scroll. That line has already gone by, so the chip has nothing more to interrupt for in this frame. The frame interrupt sets R#19 afresh for the next picture.

Several Splits a Frame

A line handler that writes R#19 is interrupted again at that line, in the same picture. Each time, it can change R#23. That cuts the screen into bands, each scrolled by its own amount.

It is how a game shows depth. Distant scenery moves slowly and near scenery quickly, so a stack of bands, each scrolling a little faster than the one above, reads as a landscape stretching away from you. It's called parallax.

The program below has four bands, 48 lines each:

BandScreen linesShowsScroll
00-47the sky, still0
148-95the far fieldsdown 1 line a frame
296-143nearerdown 2 lines a frame
3144-191the nearestdown 4 lines a frame

The frame interrupt sets up band 0: R#23 = 0 and R#19 = 47, for the first join at line 48. At the foot of each band online sets up the next: its R#23, then R#19 for the next join. The new R#19 is counted with the new scroll, so for the join at line 96 it is 95 + band 1's R#23. The last band writes R#19 = 143 + its own R#23: its own line, under its own scroll, so there are three line interrupts a frame and no more.

All four bands come from one page, at 10000h. The sky is on its lines 0-47 and the fields on lines 64-191. The fields repeat every 64 lines, so a field band scrolled by n lines looks exactly as it does scrolled by n + 64. Each band's scroll is kept to 64 different values, so that the top of the band always shows a page line from 64 to 127. Band 1's top is screen line 48, so its R#23 is 16 + (0 to 63); band 2's is 96, so its R#23 is (0 to 63) - 32. R#23 wraps within the page, so a scroll of -32 (224) is fine.

Sprites Over the Bands

In Graphics 4, R#23 moves the sprites with the picture: a sprite shows at screen line Y + 1 - R#23. That is no trouble with one scroll. With bands, each line of the screen moves its sprites by that line's R#23. A balloon whose top half is in band 0 and bottom half in band 1 is cut at the join, and its bottom half is drawn shifted by the difference between the two scrolls. Worse, every band whose scroll brings a sprite's Y into its own lines shows a copy of it, a ghost. Here is the program below with a single sprite table:

Balloons cut at the joins and copies of them in the wrong bands

The cure is a sprite table for each band. S8 showed that R#5 and R#11 say where the attribute table is, with the colour table 512 bytes below it. Four tables need four places; this program uses R#11 = 3 with R#5 = F7h, E7h, D7h and C7h, which put the tables at 1FA00h, 1F200h, 1EA00h and 1E200h. online writes the next band's R#5 along with its R#23, so each band draws its sprites from its own table.

Every frame, every sprite goes into every table, with the Y that puts it on the right screen line under that band's scroll:

Y = screen line - 1 + the band's R#23

Each band then draws only the rows of each sprite that fall in its own lines, and the rows join up exactly. If Y comes out at 216, which would end the list, use 217: that sprite is a line lower in that band for one frame.

The order matters. Write R#23 first, then R#5, then R#19. With R#5 written before R#23, the last line of a band can show the next band's sprites, still at the old scroll.

What it costs: four tables of 4 sprites is 16 attribute entries to write each frame, and in general bands x sprites. The three extra interrupts a frame are three short handlers.

Write First, Check After

online has three jobs: write the three registers, check whether there is another band to come, and if so fetch its values. The writes go first, from three bytes already waiting in NEXT, so they reach the chip as soon as possible. Only then does online look at BANDN, the count of bands left, and copy the next band's three values into NEXT.

That check is the guard. Once the bands are used up, online copies nothing, and NEXT keeps the last band's values. So if a line interrupt does come after the last band, online writes the last band's R#23, R#5 and R#19 again, and the picture doesn't change.

Put the guard and the fetch first and the writes come later. R#23 still makes its line, but R#5 misses it: the first line of a band can show the band above's sprites, at the new band's scroll.

The Code: Parallax

A still sky with hills on the horizon, and below it fields and a road rushing towards you, faster the nearer they are. Two balloons rise and sink through every band, and two birds fly along the joins.

; S15: parallax - four bands, each with its own scroll and its own sprites.
;
; The top band is a still sky with hills on the horizon. Below it, three
; bands show the same fields and road, moving down the screen at 1, 2 and
; 4 lines a frame: the nearer the ground, the faster it goes. Two balloons
; rise and sink through every band, and two birds fly along the joins.
; Runs for 500 frames (10 seconds).
;
; One page, at 10000h (R#2 = 5Fh): the sky on lines 0-47, the fields on
; lines 64-191. The fields repeat every 64 lines, so each band's scroll
; only ever needs to go 63 lines past the top of its window. The frame
; interrupt sets up the sky band; a line interrupt at the foot of each band
; sets up the next, writing R#23 (its scroll), then R#5 (its own sprite
; table), then R#19 (the line for the next one).
;
; Sprites: patterns at 1C000h; four attribute tables, one a band, at
; 1FA00h, 1F200h, 1EA00h, 1E200h (R#5 = F7h, E7h, D7h, C7h, R#11 = 3),
; each with its colour table 512 bytes below.
;
; Memory: frame.asm uses 9000h-9203h and s15-lineint.asm 9140h-; this
; program builds a line of scenery at 9400h.

ROW:    EQU 9400h       ; one line of scenery, built before it is sent
NSPR:   EQU 4           ; sprites

        ORG 256

        DI
        ; --- Graphics 4 first, so VRAM runs carry on past 16K
        LD A, 06h
        LD B, 0
        CALL setreg
        LD A, 5Fh
        LD B, 2
        CALL setreg
        XOR A
        LD B, 23
        CALL setreg

        ; --- the scenery: lines 0-191 of the page at 10000h
        LD A, 4
        LD B, 14
        CALL setreg     ; 10000h
        XOR A
        OUT (9), A
        LD A, 40h
        OUT (9), A      ; offset 0, writing
        LD D, 0         ; D = line
draw:   LD A, D
        CP 48
        JR NC, dw1
        CALL skyrow     ; lines 0-47: the sky
        JR dw3
dw1:    CP 64
        JR NC, dw2
        XOR A           ; lines 48-63: never shown
        CALL fill
        CALL send
        JR dw3
dw2:    CALL fieldrow   ; lines 64-191: the fields
dw3:    INC D
        LD A, D
        CP 192
        JR NZ, draw

        ; --- the sprites: patterns, then the four colour tables (all
        ;     alike), then every attribute table empty (Y = 216)
        LD A, 7
        LD B, 14
        CALL setreg     ; 1C000h-1FFFFh
        LD HL, 0000h
        CALL vwrite
        LD HL, pats
        LD BC, 64*256+8 ; 2 patterns of 32 bytes, to port 8
        OTIR
        LD HL, 3800h
        CALL coltab
        LD HL, 3000h
        CALL coltab
        LD HL, 2800h
        CALL coltab
        LD HL, 2000h
        CALL coltab
        LD HL, 3A00h
        CALL notab
        LD HL, 3200h
        CALL notab
        LD HL, 2A00h
        CALL notab
        LD HL, 2200h
        CALL notab
        LD A, 38h
        LD B, 6
        CALL setreg     ; R#6 = 38h: patterns at 1C000h
        LD A, 0F7h
        LD B, 5
        CALL setreg     ; R#5 = F7h, R#11 = 3: the sky's table
        LD A, 3
        LD B, 11
        CALL setreg

        ; --- both interrupts
        XOR A
        LD (BANDN), A   ; nothing to set up until the first frame
        CALL irqon
        CALL lineon
        LD A, 47
        LD B, 19
        CALL setreg     ; the first join, until the frame takes over
        LD A, 16h       ; R#0: Graphics 4 with IE1, the line interrupt
        LD B, 0
        CALL setreg
        LD A, 0E2h      ; R#1: display, frame interrupt, 16x16 sprites
        LD B, 1
        CALL setreg
        EI

main:   CALL waitf
        LD HL, (TICKS)
        LD DE, 500
        OR A
        SBC HL, DE
        JR C, main

        ; --- put the machine back: the line interrupt off first
        DI
        LD A, 06h       ; R#0 without IE1
        LD B, 0
        CALL setreg
        LD A, 03h
        LD B, 6
        CALL setreg     ; R#6: MOS's sprite patterns, 1800h
        LD A, 0C0h
        CALL irqoff
        CALL tomos
        RET

; --- online: the foot of a band - set up the next one, from NEXT. AF only
;     (anything else is saved); R#15 is 1 here, so no reading port 9.
;     The writes go first, straight away: R#23, then R#5, then R#19.
online: LD A, (NEXT)    ; the band's scroll
        OUT (9), A
        LD A, 128+23
        OUT (9), A
        LD A, (NEXT+1)  ; its sprite table
        OUT (9), A
        LD A, 128+5
        OUT (9), A
        LD A, (NEXT+2)  ; the line for the next interrupt
        OUT (9), A
        LD A, 128+19
        OUT (9), A
        LD A, (BANDN)   ; the guard: any bands left?
        OR A
        RET Z           ; no: NEXT keeps the last band, so a spare
        DEC A           ; interrupt writes the same values again
        LD (BANDN), A
        PUSH HL         ; yes: the next band's three values into NEXT
        PUSH DE
        PUSH BC
        LD HL, (BANDP)
        LD DE, NEXT
        LD BC, 3
        LDIR
        LD (BANDP), HL
        POP BC
        POP DE
        POP HL
        RET

; --- onframe: the top of the next frame. Set up the sky band, list the
;     other three for online, and write the sprites into all four tables.
onframe:
        LD A, (TICKS)   ; t = frames, 0-255
        LD C, A
        XOR A           ; each field band: its speed x -t, kept to 0-63,
        SUB C           ; plus where its window starts (the top of the
        AND 63          ; band shows page line 64 + 0-63)
        ADD A, 16
        LD (SC+1), A    ; band 1: 1 line a frame; 48 + 16 = 64
        XOR A
        SUB C
        SUB C
        AND 63
        SUB 32
        LD (SC+2), A    ; band 2: 2 lines a frame; 96 - 32 = 64
        XOR A
        SUB C
        SUB C
        SUB C
        SUB C
        AND 63
        SUB 80
        LD (SC+3), A    ; band 3: 4 lines a frame; 144 - 80 = 64
        ; the sky band, written now: R#23, R#5, R#19
        XOR A
        LD B, 23
        CALL setreg     ; the sky: not scrolled
        LD A, 0F7h
        LD B, 5
        CALL setreg     ; its sprite table
        LD A, 47        ; join 1: line 48 - 1
        LD B, 19
        CALL setreg
        ; the list for online: scroll, table, next line - a band each.
        ; Band 1 goes straight into NEXT; bands 2 and 3 wait in BANDS.
        LD HL, NEXT
        LD A, (SC+1)
        LD (HL), A
        INC HL
        LD (HL), 0E7h
        INC HL
        ADD A, 95       ; join 2: line 96 - 1, under band 1's scroll
        LD (HL), A
        LD HL, BANDS
        LD A, (SC+2)
        LD (HL), A
        INC HL
        LD (HL), 0D7h
        INC HL
        ADD A, 143      ; join 3: line 144 - 1, under band 2's scroll
        LD (HL), A
        INC HL
        LD A, (SC+3)
        LD (HL), A
        INC HL
        LD (HL), 0C7h
        INC HL
        ADD A, 143      ; the last band: its own join, under its own
        LD (HL), A      ; scroll - so no more line interrupts this frame
        LD HL, BANDS
        LD (BANDP), HL
        LD A, 2
        LD (BANDN), A
        ; where the sprites are on the screen this frame
        LD A, C         ; u = t folded: 0-127, then back down
        CP 128
        JR C, of1
        CPL
of1:    LD B, A
        ADD A, 20
        LD (SPS), A     ; balloon 0: lines 20-147 and back
        LD A, 147
        SUB B
        LD (SPS+1), A   ; balloon 1: the other way
        LD A, C
        ADD A, A
        LD (SPX+4), A   ; bird 2: 2 dots a frame to the right
        LD A, C
        CPL
        LD (SPX+6), A   ; bird 3: 1 dot a frame to the left
        ; the four attribute tables, each with its band's scroll
        LD A, 7
        LD B, 14
        CALL setreg
        LD HL, 3A00h
        LD A, (SC)
        CALL table
        LD HL, 3200h
        LD A, (SC+1)
        CALL table
        LD HL, 2A00h
        LD A, (SC+2)
        CALL table
        LD HL, 2200h
        LD A, (SC+3)
        JP table

; --- table: every sprite into the attribute table at offset HL of page 7,
;     for a band scrolled by A: Y = screen line - 1 + scroll.
table:  LD C, A
        CALL vwrite
        LD HL, SPS
        LD DE, SPX
        LD B, NSPR
tb1:    LD A, (HL)      ; its line on the screen
        DEC A
        ADD A, C        ; + the band's scroll
        CP 216          ; 216 would end the list: use 217, a line lower
        JR NZ, tb2
        INC A
tb2:    OUT (8), A      ; Y
        LD A, (DE)
        OUT (8), A      ; X
        INC DE
        LD A, (DE)
        OUT (8), A      ; pattern
        OUT (8), A      ; (unused in this mode)
        INC DE
        INC HL
        DJNZ tb1
        LD A, 216       ; the end of the list
        OUT (8), A
        RET

; --- vwrite: VRAM write address HL in R#14's 16K
vwrite: LD A, L
        OUT (9), A
        LD A, H
        OR 40h
        OUT (9), A
        RET

; --- coltab: the colour table at offset HL of page 7
coltab: CALL vwrite
        LD HL, cols
        LD BC, 64*256+8
        OTIR
        RET

; --- notab: an empty attribute table at offset HL of page 7
notab:  CALL vwrite
        LD A, 216
        OUT (8), A
        RET

; --- skyrow: line D of the sky. Light blue to line 15, cyan below, and
;     dark green hills: in each 64 dots the hill's top steps up from line
;     46 to 39 and back down. Line 47 is all hill.
skyrow: LD A, 55h       ; colour 5, light blue
        LD E, A
        LD A, D
        CP 16
        LD A, E
        JR C, sr1
        LD A, 77h       ; colour 7, cyan
sr1:    CALL fill
        LD HL, ROW
        LD E, 0         ; E = byte across
sr2:    LD A, E         ; p = E mod 32, folded: 0-15, then back down
        AND 31
        CP 16
        JR C, sr3
        CPL
        AND 31
sr3:    SRL A
        NEG
        ADD A, 46       ; the hill's top here: 46 - p/2
        LD C, A
        LD A, D
        CP C
        JR C, sr4       ; above the hill
        LD (HL), 0CCh   ; colour 12, dark green
sr4:    INC HL
        INC E
        BIT 7, E
        JR Z, sr2
        JR send

; --- fieldrow: line D of the fields. Fields 32 lines deep, light and mid
;     green in turn, a hedge along the top of each, two trees in each, and
;     a grey road down the middle with white edges and a dashed centre.
fieldrow:
        LD A, D
        AND 31
        CP 3
        LD A, 0CCh      ; lines 0-2 of a field: the hedge, dark green
        JR C, fr2
        LD A, D
        AND 32
        LD A, 33h       ; colour 3, light green
        JR NZ, fr2
        LD A, 22h       ; colour 2, mid green
fr2:    CALL fill
        LD A, D         ; lines 8-19 of a field: the trees
        AND 31
        CP 8
        JR C, fr4
        CP 20
        JR NC, fr4
        LD HL, ROW+16   ; light fields: trees at bytes 16 and 98
        LD BC, ROW+98
        BIT 5, D
        JR NZ, fr3
        LD HL, ROW+34   ; mid fields: at bytes 34 and 84
        LD BC, ROW+84
fr3:    CALL tree
        LD H, B
        LD L, C
        CALL tree
fr4:    LD HL, ROW+56   ; the road: bytes 56-71, dots 112-143
        LD (HL), 0FEh   ; white edge, then grey
        INC HL
        LD B, 14
fr5:    LD (HL), 0EEh   ; colour 14, grey
        INC HL
        DJNZ fr5
        LD (HL), 0EFh   ; grey, then the white edge
        LD A, D
        AND 8
        JR NZ, send     ; lines 8-15 of every 16: no dash
        LD A, 0FFh
        LD (ROW+63), A  ; the dash, 4 dots of white
        LD (ROW+64), A
        ; fall into send

; --- send: the 128 bytes at ROW to the next VRAM line
send:   LD HL, ROW
        LD BC, 128*256+8
        OTIR
        RET

; --- fill: all 128 bytes of ROW with A
fill:   LD HL, ROW
        LD B, 128
fl1:    LD (HL), A
        INC HL
        DJNZ fl1
        RET

; --- tree: 6 bytes (12 dots) of dark green at HL
tree:   LD A, 0CCh
        LD (HL), A
        INC HL
        LD (HL), A
        INC HL
        LD (HL), A
        INC HL
        LD (HL), A
        INC HL
        LD (HL), A
        INC HL
        LD (HL), A
        RET

; --- the sprites. SPS: each one's top line on the screen.
;     0 and 1 are balloons, 2 and 3 birds flying along the first and
;     third joins (their wings cross lines 48 and 144). SPX: each one's
;     X and pattern (0 the balloon, 4 the bird).
SPS:    DEFB 20, 147, 42, 138
SPX:    DEFB 64, 0, 176, 0, 0, 4, 255, 4

SC:     DEFB 0          ; each band's scroll this frame (the sky's is 0)
        DEFS 3
NEXT:   DEFB 0, 0F7h, 47 ; the next band: R#23, R#5, R#19 for online
BANDS:  DEFS 6          ; bands 2 and 3, the same three each
BANDP:  DEFW BANDS      ; online's place in BANDS
BANDN:  DEFB 0          ; bands left in BANDS this frame

; sprite mode 2 colours, a byte a line: red and yellow stripes, black
; ropes, a dark red basket; the bird black
cols:   DEFB 8, 8, 10, 10, 8, 8, 10, 10, 8, 8, 10, 1, 1, 6, 6, 6    ; balloon
        DEFB 8, 8, 10, 10, 8, 8, 10, 10, 8, 8, 10, 1, 1, 6, 6, 6    ; balloon
        DEFB 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1         ; bird
        DEFB 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1         ; bird

pats:
; the balloon (pattern 0): top-left
        DEFB 00000111b
        DEFB 00011111b
        DEFB 00111111b
        DEFB 01111111b
        DEFB 01111111b
        DEFB 11111111b
        DEFB 11111111b
        DEFB 11111111b
; bottom-left
        DEFB 01111111b
        DEFB 00111111b
        DEFB 00011111b
        DEFB 00001000b
        DEFB 00000100b
        DEFB 00000011b
        DEFB 00000011b
        DEFB 00000000b
; top-right
        DEFB 11100000b
        DEFB 11111000b
        DEFB 11111100b
        DEFB 11111110b
        DEFB 11111110b
        DEFB 11111111b
        DEFB 11111111b
        DEFB 11111111b
; bottom-right
        DEFB 11111110b
        DEFB 11111100b
        DEFB 11111000b
        DEFB 00010000b
        DEFB 00100000b
        DEFB 11000000b
        DEFB 11000000b
        DEFB 00000000b
; the bird (pattern 4): top-left
        DEFB 00000000b
        DEFB 00000000b
        DEFB 11000000b
        DEFB 01100000b
        DEFB 00110000b
        DEFB 00011000b
        DEFB 00001100b
        DEFB 00000110b
; bottom-left
        DEFB 00000011b
        DEFB 00000001b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
; top-right
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000011b
        DEFB 00000110b
        DEFB 00001100b
        DEFB 00011000b
        DEFB 00110000b
        DEFB 01100000b
; bottom-right
        DEFB 11000000b
        DEFB 10000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b
        DEFB 00000000b

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

Build and run

Save it as s15-parallax.asm next to frame.asm and s15-lineint.asm. Build and run it as in S2.

What you should see

A light blue and cyan sky over dark green hills, and below it three bands of fields: green and light green, with hedges, trees, and a grey road with a dashed white line down the middle. The fields move down the screen, slowly in the top band and fastest at the bottom, so the hedges and dashes slide past each other at the joins. The sky doesn't move. The two balloons, red and yellow with a dark red basket, rise and sink through every band in one piece, and the two black birds stay whole as they fly along the first and third joins. After ten seconds, MOS's screen and the prompt.

The sky and three bands of fields, two balloons and two birds

Half a second later: each band has moved its own distance; a balloon crosses the first join

Another half second on

Change One Thing

  • In s15-split-screen.asm, change ADD A, 159 in onframe to ADD A, 160. Which line of the screen does the panel start on now, and which of its lines will you never see?
  • Take the LD A, L and ADD A, 159 out of onframe, and write LD A, 159 instead, so R#19 no longer allows for the scroll. What happens to the join?
  • In s15-colour-split.asm, take DEC A out of onframe. Where does the waterline land, compared with the line number in the comment?
  • In s15-parallax.asm, change the 0E7h, 0D7h and 0C7h in onframe to 0F7h, so every band uses the sky's sprite table. What happens to a balloon as it crosses a join, and what appears in the other bands?
  • In onframe, give the last band band 2's R#19 instead of its own: just before the last ADD A, 143, add LD A, (SC+2). Once band 3 has started, which line is the chip waiting for now? Why does the picture still look the same?

When It Goes Wrong

SymptomCause
One line at the join shows the wrong thingThe line handler is too slow, so its second register write lands a line after the first. Do the visible changes first, with direct OUTs
The panel starts one line low and its top line never showsR#19 set to the join's line, not the line above it
Once every few seconds the playfield vanishes for one frameA line flag raised below the picture, while the frame handler ran, was taken as soon as it finished, so online switched to the panel for the whole next picture. After frame, clear it: if S#2 bit 6 (VR) is still 1, read S#1
Sprites are cut at a band's join, the lower part out of line, and copies of them show in other bandsOne sprite table for all the bands. Give each band its own table, every sprite in each at that band's scroll, and write R#5 in the line handler
The first line of a band shows the band above's sprites, out of line by the difference in scrollThe handler checks and fetches before it writes, so R#5 lands a line late. Write the registers first, then check

Summary

You can interrupt your program at a line of your choice with R#19 and R#0 bit 4, tell the line interrupt from the frame interrupt by S#1's FH bit, and change the picture from the next line down: a backdrop colour, or R#2 and R#23 for a still panel under a scrolling playfield. Ask for the line above, add the scroll, keep the line handler short, and turn IE1 off before you give MOS its table back. After the frame's work, while S#2's VR bit says the picture has not started, read S#1 to clear any line flag raised outside the picture.

For several splits a frame, each line handler writes the next R#19 counted with its own new scroll, and the last one writes its own line, so you always know where R#19 points. Sprites over the bands need a table each: R#23, then R#5, then R#19, and every sprite in every table at its band's scroll.

Next

S16, The Blitter: Fill, Line, Point: the command engine S14 borrowed for its copy, taught properly.

Get the Newsletter

New guides, disk images and community finds, every couple of weeks. No spam, we promise, this isn't Tatung's marketing department.
Read Past Issues
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.