Under the hood

Getting started

This repository holds the commented disassembly of Gremlin Graphics's Trailblazer for the MSX (1986, tape). It does not hold the tape: the .cas image is not distributed.

What you need

It is exactly 38,458 bytes and its fingerprint is:

sha256  779b662747fdd04b641fbf898124611562d9913e570f5050b5d73580ce63cf50

To check it:

make cinta

Reproducing the whole thing

make

That chains the four things that matter:

step what it does what it proves
make listados builds the five .asm from the trace and the notes that the listings are not hand-written
make verify reassembles the five pieces, wraps them back up and compares the sha256 that the listing is the tape
make sanity checks how the bytes are accounted for that not one byte is left unexplained
make test 40 checks that what gets published holds up against the bytes

The one that decides is make verify, and here it has two steps: first each piece has to assemble and give exactly its bytes, and then, wrapping them the way the .cas wraps them — the eight-byte sentinel aligned to a multiple of eight, the MSX BIN header for the first, the loader's own little headers for the other four, the sync byte and the alignment padding — the whole file has to come out with its sha256.

The first step only says the listings are consistent with what we fed them. The second says what we fed them is the tape.

The five pieces

piece      loads at   takes        what it is
cargador   0xD800       385 bytes  the tape loader
slots      0x9000       257 bytes  hunts for RAM across the slots
portada    0x8800      7169 bytes  the loading screen
datos      0x8800     10240 bytes  the fourteen tracks
juego      0x80E8     20248 bytes  the game

tools/cinta.py pulls them out and puts them back together, and says so if the model of the tape does not add up.

The numbers

make densidad

0 routines below 10 %, out of 355
in total: 3637 instructions, 1120 comments, 30.8 %

The pictures

make imagenes

Draws into work/gfx/ the loading screen, all fourteen tracks in full, the table of rows, the sprite patterns and the font. None of them is a capture: they are built by running in Python the same steps the Z80 runs.

The emulator

make emulador

Loads the tape into openMSX, goes into the menu, starts a game, and dumps VRAM, all 64 KB of RAM and the eight VDP registers. That is where the screen geometry the pictures use came from — measured, not deduced.

There are three more tools for asking the machine things when static analysis runs out of anything to look at:

tools/omsx_quien_lee.tcl     who touches a memory area, and from where
tools/omsx_quien_llama.tcl   who enters a routine
tools/omsx_muestrea.tcl      which parts of the loader actually run

The first found who reads the perspective table, which is written in no instruction anywhere. The third turned up a loader routine that never runs.

The tape

file       trailblazer.cas
size       38,458 bytes
sha256     779b662747fdd04b641fbf898124611562d9913e570f5050b5d73580ce63cf50
machine    MSX1
publisher  Gremlin Graphics, 1986
original   Mr Chip Software (Shaun Southern, Commodore 64)

How it is made

A .cas does not store the modulation: it stores the bytes the MSX would have read, and marks where each block starts with an eight-byte sentinel, 1F A6 DE BA CC 13 7D 74, always aligned to a multiple of 8. That alignment is what makes the splitting reliable: the same sequence inside the data, if it lands unaligned, is not a separator.

This tape has two layers, and that is where all the fun is.

The outer one is the MSX's. The first block is an ordinary BIN header — ten 0xD0 bytes and six of name, Trail! — and the second its body, which loads at 0xD800 and runs there. That is the only thing the BIOS loads its own way.

The inner one the game invents. The eight remaining blocks carry no MSX header: they go in pairs, and the first of each pair is an eight-byte little header:

0xFE   load address (2, LE)   length (2, LE)   sum   00 00

And the second starts with another loose byte, 0xFF, before the content.

Those two bytes are not data: they are the sync

The loader enters its read routine with the byte it expects already in the accumulator — ld a,0feh at 0xD80D for the little header and ld a,0ffh at 0xD81D for the body — and stores nothing until it has seen it (0xD87D). Besides, the little header is read by asking for exactly four bytes at 0xD8ED (ld de,00004h), so of the block's eight only the address and the length count; the fifth is a checksum and the last two, padding.

It adds up perfectly, and that is what confirms the reading: every body block measures 1 + length + padding, and the padding is exactly what is needed for the next sentinel to land on a multiple of eight.

header               loads at           and takes      the block measures
FE 00 90 01 01 6E    0x9000..0x9100       257 bytes     264 = 1+257+6
FE 00 88 01 1C 6B    0x8800..0xA400      7169 bytes     7176
FE 00 88 00 28 5E    0x8800..0xAFFF     10240 bytes     10248
FE E8 80 18 4F C1    0x80E8..0xCFFF     20248 bytes     20250

Without discounting that byte, the first piece starts with an rst 38h that does nothing sensible. Discounting it, it starts with di, which is what it ought to do. All four start with clean code under this reading and under no other.

The five pieces

piece loads at takes what it is
cargador 0xD800 385 the tape loader
slots 0x9000 257 hunts for RAM across the slots
portada 0x8800 7,169 the loading screen
datos 0x8800 10,240 the fourteen tracks
juego 0x80E8 20,248 the game

Two load at the same address, and the fourth walks over both. It is not a mistake: the loading screen is on show while the rest is read, and of the tracks piece the only thing that survives is its top 0x2000 bytes, copied to 0x6000 before anyone treads on them.

The RAM finder

The second piece, the one at 0x9000, is 82 bytes that do one thing: walk the four slots and their four subslots trying to write at 0x4000 — read, invert all eight bits, write and read back — and the moment they find RAM they leave in 0xFFFE the value to poke into the PPI to have it mapped in.

That 0xFFFE is the first thing the other three pieces read: ld a,(0fffeh) / out (0a8h),a. The game needs RAM in every page, so it cannot leave the BIOS ROM visible.

The VDP registers

Measured on the machine, not deduced: make emulador dumps all eight.

reg value what it says
R0 0x02 mode 2 (SCREEN 2)
R1 0xE2 16 KB, display on, interrupt enabled, 16x16 sprites
R2 0x06 names at 0x1800
R3 0xFF colour: base 0x2000, mask 0x1FFF
R4 0x03 patterns: base 0x0000, mask 0x1FFF
R5 0x36 sprite attributes at 0x1B00
R6 0x07 sprite patterns at 0x3800
R7 0x01 black border

It is the ordinary SCREEN 2 geometry. Worth measuring rather than assuming: the first reading of this disassembly took the dumps to 0x1800 for the colour table, and they are the name table.

How the 38,299 bytes break down

  bytes %
traced code 6,556 17.12
identified data 31,743 82.88
unexplained 0 0.00
total 38,299 100.00

The file's other 159 bytes are the .cas wrapping: sentinels, headers, sync bytes and alignment padding.

The code

The routine that draws the track rewrites itself

This is the heart of the game and deserves telling slowly.

The track in perspective is drawn nowhere at all. The engine that paints it — 0x8C11 going up and 0x8C65 going down — is a run of identical blocks:

ld a,e / out (099h),a      ; the VRAM address
ld a,d / out (099h),a
ld a,000h                  ; <- the pixel byte
out (098h),a               ; and out it goes

That ld a,000h does not read the byte from anywhere: it gets written into it. For each row, 0x8BAE pulls five bytes out of the table at 0x7800, passes them through two 256-entry lookup tables — the ones 0x890F and 0x8920 build at 0xFB00 and 0xFC00 by repeating eight bytes thirty-two times — and pokes the result into the six operands at 0x8C2D, 0x8C42, 0x8C56, 0x8C81, 0x8C95 and 0xC8A9.

There is a seventh hand-written operand, at 0x8C01, choosing the page of the perspective table. 0x8998 writes it when the track starts.

The perspective table appears in no instruction

The 3,570 bytes from 0xC20E to the end of the piece are pointed at by no ld hl,0Cxxxh. The address is computed: 0x8C14 does

ld a,e / and 007h / add a,a / or 0c0h / ld h,a

that is, 0xC0 plus twice the low three bits of e, which gives the eight even pages 0xC0, 0xC2 … 0xCE.

Since static analysis had nothing left to look at, we asked the machine: an openMSX watchpoint over 0xC20E..0xCFFF with the game running (tools/omsx_quien_lee.tcl) says it is read by exactly seven places, 0x9632 and the six in the drawing routine.

And 0x9629 uses those same eight even pages for something else: it copies them 256 at a time into RAM — skipping two pages at a time with the double inc h at 0x9639 — and dumps the 0x800 into VRAM from there. The same bytes serve as table and as patterns.

Five bands at five rates

Advancing the track computes no projection at all. 0x8AD3 splits the screen into five bands and repaints each at a different rate:

band rows repainted
the nearest 32 twice per step
the second 16 once
the third 16 one time in two
the fourth 16 one in four
the horizon 16 one in eight

Five counters with their mask — and 001h, and 003h, and 007h — and the road recedes. No division, no perspective table, not one multiply.

And the ball's height is turned into a track row by the same reasoning, in reverse: 0x8E31 compares against 0xB3, 0x8E, 0x64 and 0x3E and returns 4, 3, 2, 1 or 0.

Fourteen tracks that store no tiles

The fourteen add up to 3,494 rows of five tiles, which would be 17,470 bytes. They take 3,494.

The tracks piece carries 0x2000 bytes into RAM at 0x6000. Of those:

A 219-row track takes 219 bytes because rows repeat and naming them is enough.

The interrupt, in mode 2

0x88E4 sets it up, and it is textbook with a neat detour:

  1. 0xFD00..0xFDFF is filled with 0xFE — 257 bytes in one go, with the ld (hl),0feh and the overlapping LDIR at 0x88EE — and ld i,a with a = 0xFD puts the vector table there;
  2. the vector that comes out of that table is the word at 0xFDFE, which is 0xFEFE;
  3. and at 0xFEFE it writes… 0xFB18, which as an instruction is jr $-5;
  4. and at 0xFEFB it writes 0xC3 and behind it 0x9134: jp 09134h.

The detour is the classic trick for making a 257-byte vector table out of one repeated byte. The real destination is 0x9134, and the whole game hangs off it: the music, the controls, the ball's movement, the track's advance and the painting.

The stars

There are two kinds, and neither is drawn as a sprite.

The background ones are the whole screen shifting: 0x9BB1 moves a row one pixel left with thirty-two rl (hl) written one after another, no loop, with the carry passing from one byte to the next. Every eighth there is a dec hl instead of a dec l, which is the jump to the band above. And they only move one frame in eight.

The foreground ones are seven dots with their own speed and acceleration (0x8F65), spreading towards the edges as if coming head-on: each has its acceleration added every two frames, and when one goes off the edge it comes back to the centre with fresh parameters.

Each dot's colour comes from register R, the DRAM refresh counter, shifted into the high nibble (0x90DE). It is not decided: it is taken.

The dice

0x908E has no table. It takes the seed, puts it in the high byte, subtracts the same value from it twice, crosses the two bytes and mixes in register R. Since R changes with every instruction executed, two games do not come out the same even from an identical start.

One routine for all eighty keys

0xBF62 takes a key code. Out of its low three bits it pulls the bit number, shifts it into place and ors in a 0x47, which is the opcode for bit 0,a. And then it writes that into 0xBF85, the instruction it will execute two lines later.

No table, no loop, no eight branches.

0x9695 does the same with the colour of a line of text: it writes an or with the colour over a nop (0x9808), so the same loop paints lit or unlit with no comparison at all.

The text

Every string carries its end marked in bit 7 of the last letter. That is why dumped raw they look glued together.

(0xA41E) is the pointer to the character sheet the painting routine uses (0x97D3). The startup code leaves it at 0xBA2E, the ordinary font, and 0x9957 and 0x9973 swap it for another sheet for one caption and put it back.

In the emulator

Here the emulator is not decoration: it is a measuring instrument, and three things in this disassembly would not have been settled without it.

Loading the tape and dumping everything

make emulador

Puts the tape into openMSX, types BLOAD"CAS:",R, waits until the PC has spent eight seconds inside 0x80E8..0xCFFF and dumps VRAM, all 64 KB of RAM and the eight VDP registers. Then it goes into the menu, starts a game, and dumps again.

It takes about 190 emulated seconds: that is 38 KB of tape at 1200 baud, though emulated at full tilt.

The keyboard is not read through the BIOS

And that matters for automating it. openMSX's type injects the keypress where BASIC reads it, and this game reads the keyboard by hand through the PPI (0xBF17 and 0xBF62). The keypress BASIC injects lasts less than its scan and never registers.

The way out is to press the key in the matrix and hold it half a second:

keymatrixdown 8 0x01          ; space
after time 0.5 {keymatrixup 8 0x01}

The game itself says what to press: Press any key for options first, and once in the menu, 3 : Play the game.

That is where the screen geometry came from

R0 = 0x02   mode 2 (SCREEN 2)
R1 = 0xE2   16 KB, display on, interrupt on, 16x16 sprites
R2 = 0x06   names               at 0x1800
R3 = 0xFF   colour    base 0x2000, mask 0x1FFF
R4 = 0x03   patterns  base 0x0000, mask 0x1FFF
R5 = 0x36   sprite attributes   at 0x1B00
R6 = 0x07   sprite patterns     at 0x3800
R7 = 0x01   black border

Measuring rather than assuming is not fussiness: the first reading of this disassembly took the dumps to 0x1800 for the colour table, and they are the name table. With the wrong geometry the pictures come out right in shape and wrong in place, and you cannot see that by looking at them.

Who touches a memory area

tools/omsx_quien_lee.tcl

Puts a watchpoint over a range and notes the PC of every read or write. It is for when static analysis runs out of anything to look at: when the address is computed and appears in no instruction.

This is what settled the perspective table. The 3,570 bytes from 0xC20E to the end are pointed at by no ld hl,0Cxxxh, and there is no way to find them by reading. Watching the area with the game running turned up exactly seven places: 0x9632 and the six in the drawing routine.

Who enters a routine

tools/omsx_quien_llama.tcl

Breakpoint on the routine, then look at who has just left the return address on the stack. It was used on six routines nobody visibly calls: they appear in no call, jp or ld hl,, and no relative jump from traced code lands in them.

Of the six, none ran in ninety seconds of play. Either they belong to screens the test never reached — game over, the high score table — or they are dead code. The cheat mode is no longer a candidate: it cannot be reached at all, and that is measured in Findings. The six are in Open questions.

It also served the opposite purpose: confirming that 0x8E4B and 0x92D1, reached by a ld hl,nn / push hl rather than a call, do run.

Which parts of the loader actually run

tools/omsx_muestrea.tcl

Four thousand samples of the PC while the whole tape loads. Not as exact as a breakpoint, but plenty for telling live code from code that is never touched.

This is how we found that the routine at 0xD8D5 — the one reading the tape bit by hand through port 0xA2 — never runs at any point: the actual loading calls the BIOS's TAPIN. It is a turbo loader left inside.

And a claim that had to be withdrawn

Before that measurement, this disassembly said the loader "builds its own read routine on the stack and pulls the bit off port 0xA2 by hand". Half of that was true — the bridge on the stack is there — and the other half false: the bridge does not read bits, it calls the BIOS, and the routine that does read bits goes unused.

It was corrected as soon as the machine said so.

Tcl traps already paid for in this series