Under the hood

Where to start

This is the commented disassembly of Colt 36 (Topo Soft, MSX, 1987), a four-level shooting gallery —EL ALMACÉN (the warehouse), EL CAÑÓN (the canyon), LA MINA (the mine) and EL SALOON (the saloon)— released on cassette tape. It explains, byte by byte, what is recorded on it.

The Colt 36 title screen

One thing is worth knowing from the outset, because it shapes everything else: Colt 36 is not written in assembly. The game is a 63-line MSX-BASIC program, tokenised (the interpreter does not store the program text, but a compressed form in which each keyword takes up one byte). Of Z80 code there are 309 bytes —two routines that copy blocks —one to video memory, the other RAM to RAM— and a music player— plus the 45 that get the interpreter going.

What is here

src/ holds the published listings; tools/ the tools that extract the tape, disassemble, draw and verify; tests/ the tests that check the documentation against the binary; docs/ this documentation and its images. The Makefile is the entry point. extracted/, work/, dump/ and build/ are generated by make and are not versioned.

The tape is not here

The tape image is not distributed: you have to supply your own, named colt36.tsx and placed in the root of the project. TSX is a cassette image format: it stores the blocks exactly as they are read from the audio. The copy this work was done with is 34 722 bytes and this sha256:

4f3090407ff22826a0ce1281908c497396cda972fe10dd0af694330cd62ebe13

If it is missing, make stops and says so; if the sha256 does not match, it warns and carries on, because the listings might not correspond. Inside there are 34 239 bytes of content in five blocks.

Without it you can still do a fair amount. The listings in src/ are in the repository and can simply be read. And of the 66 tests, 25 pass on a fresh clone: the ones that check the game listing against what the documentation says —that nobody jumps to line 730, or that the published addresses of the machine-code routines are the ones the game declares with DEFUSR—. The other 40 skip themselves and state why.

What make does

make extracts the blocks, regenerates the listings that come out of the tools, runs the budget check, runs the tests and finishes with three reproducibility checks. It needs Python 3 and pasmo, a command-line Z80 assembler.

The budget accounts for every byte of the tape against what the documentation says it is: today, 34 239 explained out of 34 239. It is kept separate because it catches a failure that reproducibility cannot see: if the tracer took graphics for code, the bytes would still be the same and the binary would come out identical, but the listing would be lying.

The three reproducibility checks decide whether any of this is worth anything. First: src/colt36.bas, retokenised, has to give the 3935 bytes of the original program. Second: the four assembly listings have to give their exact binaries —colt36_arranque.asm 342 bytes, colt36_topo.asm 4254, colt36_scr.asm 7100 and colt36_cm2.asm 18 352—. Third: the two halves of the game block, concatenated, their 4277 bytes.

As long as that stays green, any claim in the documentation can be checked against the binary, and any change in behaviour after touching the game can be put down to what was touched and not to a misreading. Without that check, all of this would be an opinion about a pile of bytes.

On their own: make extract, make test, make clean and make ram, which starts openMSX with the real tape and dumps its RAM as the game begins, to compare it with the memory image the project reconstructs.

Retokenising instead of reassembling, and the role of #

In a normal disassembly the proof is reassembling. Here, since the game is BASIC, the equivalent is tokenising it again. And that raises a problem: a BASIC program cannot take explanations without changing the bytes, because a REM takes up room in memory. Two files would be needed —one commented for reading and one faithful for checking— and the commented one would end up out of date. This is avoided by reserving the character # at the start of a line for the project's comments: it is not MSX-BASIC syntax, the tokeniser ignores it, and so the published file is exactly the one that gets checked. Of the 460 lines of src/colt36.bas, 357 are our comments; 40 are blank and the remaining 63 are the game's program, as it is.

The files in src/

How the data blocks are laid out

Every data range declared in the notes comes out as a block of its own: its own heading saying what it is for, its own label, and the dump aligned to its first byte. An optional line gives the block the row width of its real structure, and that is what makes the four levels readable in the listing itself: 32 bytes per row is one row of the board, so the walls, the doors and the objects can be seen in place. The 256 cell shapes and their colours go eight bytes at a time -one cell per row-, the hiding places four -one per record-, and the note table comes out as defw.

The tools you will want to use

tools/basic_detok.py does and undoes MSX-BASIC tokenisation: detok pulls the text out of a binary, tok turns it back, check and verify test the round trip. The internal pointers of a BASIC program are absolute, so it has to be given the load address; here, 0x8000.

tools/render_niveles.py reconstructs what the game shows from the tape data, following the lines of the program that paint each screen. These are not screenshots: it is a check. If the block were carved up wrongly —if what we call drawings were the colours— it would come out as noise. That the title and the four backdrops come out legible is the strongest proof that the tables are where we say they are.

python3 tools/render_niveles.py work/CM2.raw docs/imagenes

The first level, EL ALMACÉN, with its scoreboard

The whole backdrop of the same level, taller than the screen: that is where you move

tools/omsx_juega.tcl loads the tape in openMSX, leaves the game running and takes screenshots. Loading it takes a while —blocks at normal speed, no fast loading— so as soon as the game starts it saves a state, colt36_arranque, which you get back to in a second with openmsx -savestate colt36_arranque. The tape and the output are given to it in COLT36_TSX and COLT36_OUT.

The tape

colt36.tsx is 34,722 bytes and its sha256 is 4f3090407ff22826a0ce1281908c497396cda972fe10dd0af694330cd62ebe13. Inside are five blocks recorded with the standard MSX tape encoding: no turbo loader, no home-made tape-reading routine, nothing faster than what the machine came with from the factory. It really is slow: from the moment the tape starts playing to the moment the logo animation starts running, 69.7 seconds go by, measured in the emulator.

block type load end start size
COLT36 ASCII — — — 256 B
topo BIN 0x9470 0xA50D 0x9470 4254 B
scr BIN 0x9C40 0xB7FB 0xB798 7100 B
CM2 BIN 0x9B91 0xE340 0x9BBA 18,352 B
CM1 BIN 0x83E8 0x949C 0x948F 4277 B

That is 34,239 bytes of content. The CM1 block also drags a padding byte of 0xFF along behind the data, and with it the tape ends.

What a BIN on tape is

Each block is preceded by a sixteen-byte header: ten identical bytes saying what type it is, and six with the name. In COLT36 those ten bytes are 0xEA, which means "text"; in the other four they are 0xD0, which means "binary".

A BIN on tape carries nothing resembling a relocation table or a symbol list. The only extra it brings is three two-byte addresses at the start of the data block: load, end and start. BLOAD"cas:" dumps the bytes between the load address and the end address, exactly as they are, untouched; add ,R and it jumps to the start address when it finishes. That is all. The block has to know where it is going to land, and if the machine has that memory occupied, so much the worse for the machine.

The ASCII block works differently: it is a BASIC program saved as plain text, and it is loaded with RUN"cas:", which reads it and runs it. It takes up exactly 256 bytes because text files on tape are written in records of that size, padded with 0x1A until they are full.

The loader

The 256 bytes of the first block are 117 of text, one 0x1A end-of-file byte and 138 more of padding. The text is this:

10 COLOR 1,1,1:SCREEN 2
20 BLOAD"cas:",R
30 BLOAD"cas:",R
40 CLEAR200,39824!
50 BLOAD"cas:",R
60 BLOAD"cas:",R

Line 10 sets ink, background and border to the same colour — 1 is black — and switches to the 256×192 graphics mode. The screen goes dark. Line 20 brings in topo, the company logo, which runs at its own load address, does its animation and hands control back to BASIC. Line 30 brings in scr, the loading screen, whose start address at 0xB798 paints it in 0.27 seconds: it appears all at once, you never see it being drawn.

The Colt 36 loading screen, signed CANO

Line 50 brings in CM2, the 18,352 bytes of artwork, scenery and music, and line 60 the game itself.

That leaves line 40, which is the interesting one. CLEAR200,39824 reserves 200 bytes for strings and lowers the interpreter's memory ceiling to 39824, that is 0x9B90, exactly one byte below where CM2 is going to land. And it cannot go just anywhere. It has to come after line 20, because once the CLEAR has been done the interpreter's stack and file buffer move to 0x98A0..0x9B90, which is inside the range of topo (0x9470..0xA50D): the BLOAD on line 20 would be writing over its own stack and over the buffer it reads the tape with. And it has to come before line 60, so that when the game starts the ceiling is already in place and neither the stack nor the strings ever reach the data.

The proof that it was saved with SAVE",A"

In 39824! there is an exclamation mark nobody typed. In MSX-BASIC an integer only goes up to 32767; 39824 does not fit, so the interpreter stores it as a single-precision number, and when a LIST writes it back out it adds the ! suffix that marks that type. In other words: the text on the tape is literally the output of a LIST, and the loader was saved with SAVE"cas:",A from the interpreter itself — it was not typed by hand in an editor.

What overwrites what

The four binary blocks occupy overlapping ranges, because they arrive and evict each other in turn:

Of the logo's 4254 bytes, 1780 are left untouched, those from 0x949D to 0x9B90. And 0x9B90 is exactly the ceiling the CLEAR set: what survives of the logo falls precisely in the area the interpreter uses as its stack and string space. The corpse lasts longer than you would think, because the game spends little stack: measured 80 seconds into a game, 1629 of those bytes were still intact, and 893 of them consecutive, from 0x949D to 0x9819.

An ,R that starts nothing

All four BLOADs carry ,R, but the one for CM2 sets nothing running. Its start address, 0x9BBA, holds a single byte 0xC9, which is the Z80's RET instruction: the jump goes in and comes straight back out. That byte is the end of the routine beginning at 0x9B91, the one that initialises the music player.

And it had to be that way on purpose. BSAVE lets you leave out the start address, but in that case it takes the load address, which here is 0x9B91: the ,R would have fired up the music player with the game not yet loaded. Pointing the start address at a RET is how a block of pure data gets along with a loader that always uses ,R.

The CM1 relocator

The last block loads at 0x83E8 and its start address is 0x948F. There sit five instructions:

LD HL,0x83E8
LD DE,0x8000
LD BC,0x10B5
LDIR
JP 0x9088

LDIR copies BC bytes from HL to DE: here, all 4277 of the whole block, a thousand bytes further down. Since the destination is below the source, copying forwards never bites its own tail. The routine copies itself too, and when it is done it jumps to 0x9088, which is by then already in place.

The reason for the whole detour is that the game is an MSX-BASIC program, and on the MSX, BASIC programs live from 0x8001 onwards, which is exactly where CM1 has to end up. But while the tape is loading, the program in charge is the loader, which is also BASIC and is also at 0x8001, running the BLOAD on line 60. Loading the game straight into its place would mean writing over the program that is reading the tape. So the block lands a thousand bytes higher up, on free ground, and only comes down to its home once the loader is no longer needed.

From there on there is no tape left: the 20 bytes at 0x9088 set VARTAB — the pointer where the interpreter builds its variable table — to 0x8F60 and jump to the routine that runs programs. Before that they write five bytes over 0x8000 that renumber the game's first line, which on tape is 65535: see A line numbered 65535.

The Colt 36 title screen

How Colt 36 is put together

Colt 36 is written in MSX-BASIC. Not a BASIC loader that fires up an assembler game: the whole game — the main loop, hit detection, the scoreboard, the bandit logic — is 63 lines of MSX-BASIC, 3935 bytes in tokenised form (the compressed internal representation the interpreter stores a program in). It is a commercial 1987 action game and it plays perfectly well. What follows explains how that is possible.

The title screen

Who does what

The big block on the tape, CM2, takes up 18,352 bytes and is almost all data: the tile shapes, the four backdrops, the tunes, the sprites. Machine code accounts for 309 bytes, 1.7% of the block, in three pieces:

USR3 and USR4 are not game routines. Line 500 declares them as DEFUSR3=&H41 and DEFUSR4=&H44: they are DISSCR and ENASCR, two BIOS entry points for switching the screen off and on while drawing.

Add another 45 bytes to those 309, living in the other block and run only once, at startup.

The trick: the game doesn't draw, it writes tile numbers

In this screen mode the MSX divides the picture into 32 × 24 cells of 8 × 8 pixels, and what sits in video memory is not pixels but a table of 768 numbers, one per cell, saying which tile goes where. Colt 36 keeps a copy of that board in ordinary RAM, from 0xDA00 onwards, and always works on the copy.

So the entire game boils down to reading and writing numbers:

When it is time to show the result, USR1 dumps all 512 bytes of the visible window (16 rows) into video memory in one go. Writing 512 bytes one at a time from BASIC would be hopeless; in machine code it is instant.

Level 1, EL ALMACÉN (the warehouse), with its scoreboard

Parameters at a fixed address, and scrolling for free

USR1 and USR2 take no arguments. They read their three parameters from six fixed memory locations, 0xD9E1–0xD9E6: how many bytes to copy, where to, and where from, two bytes each. What puts them there is BASIC subroutine 20, which splits each 16-bit number into low and high halves by dividing by 256, because the interpreter has no operators to do it.

The interesting part is that neither routine updates those parameters when it finishes, and the program exploits that in the two places where it matters.

The first is the vertical scroll. There is no scrolling of any kind: each time round the loop it dumps the whole window again from a different address. Moving the backdrop one row means adding 32 to the source address. That is why line 620 only rewrites two bytes — the source — and calls USR1: the how-many and the where-to are still set from the previous pass. (The crosshair, incidentally, never moves up or down: it is nailed to row 7, and up and down move the backdrop.)

The second is the pan on line 746: when the bandit that shoots you is off-window, the program walks the backdrop up to him rewriting only those same two source bytes. And there is a third place where the same source gets reused, though there the BASIC does go through subroutine 20 again: loading the tiles. The screen is split into three bands of 2048 bytes that can have different tile sets, up to 768 in total. Lines 520, 530 and 535 change only the destination and call USR1 three times, so they upload the same set of 256 tiles to all three bands. The 768 possible tiles are given up, and in exchange any cell is valid anywhere on the screen, which is exactly what makes moving the backdrop around freely possible.

The full backdrop for level 3, LA MINA (the mine): 32 columns by 64 rows

Startup, and why the program cannot be listed

Tape block CM1 is 4277 bytes loaded at 0x83E8. Its start address, 0x948F, lands on a five-instruction relocator that copies itself and everything else 1000 bytes lower down:

LD HL,0x83E8 / LD DE,0x8000 / LD BC,0x10B5 / LDIR / JP 0x9088

It loads at 0x83E8 rather than straight to 0x8000 because while the game is loading the one in charge is the tape loader, which is itself a BASIC program and lives from 0x8001 onwards: loading over it would trample the very program running the BLOAD.

Once in place, the startup at 0x9088 does three things: it copies five bytes over the beginning of the program, points VARTAB (0xF6C2, the pointer where the interpreter builds its variable table) at 0x8F60, and jumps to 0x73AC, the interpreter routine that starts executing.

Those five bytes are the protection: they renumber the program's first line, which on tape is 65535. See A line numbered 65535.

And the first thing that freshly numbered line does finishes the job: POKE &HFBB1,1. That address is BASROM, which the system uses to decide whether CTRL+STOP can interrupt. Set to 1, the game can no longer be stopped; and if it cannot be stopped, it cannot be listed.

The music, and why it only plays on the title screen

The player is minimal: one byte per channel per step, no durations, no envelopes. 0xFE means go back to the start, 0xFF means silence, and any other value is a note number that gets doubled and used as an index into a table of 96 16-bit periods. USR5 initialises; USR6 plays a single step and returns. It writes to the sound chip through its ports, 0xA0 and 0xA1, in six bytes, without going near the system routine.

Since nothing is hooked to the video interrupt, the music only advances if somebody calls USR6 over and over, and the only place in the program that does is line 582, inside the title screen loop. The tune's entire tempo control is the FOR H%=1TO35 sitting right behind it. The moment the game starts nobody calls it again, and from there on the sound comes from BASIC's own PLAY and SOUND.

One detail about the period table: the first entry is a dead-on C1 (32.71 Hz), but the player does not start there. Its base is the seventh entry, so note 0 in the tunes is an F#1 and the first six are never reached.

The odd-looking text in the listing

Reading the program you come across things like DATA "0000EL0CA[ON" or A$="00MUSICA^GOMINOLAS". These are not typos. The game uses its own character set, and the tile for a letter is its ASCII code plus 60. Which is why '0' (48 + 60 = 108) is the blank tile and doubles as a space, '[' is the ñ, '^' is the colon and '_' is the exclamation mark. The j, k and l on line 2010 are not letters either: they are the tiles for the bullet and the two halves of the bandit icon the scoreboard is refilled with. Digits go their own way, using tile 156 plus the figure.

How it was done

There is no turbo loading here: the tape is made of standard blocks, so the bytes come straight out of the tape file and no emulator is needed to decode them. What the emulator is used for is proof. The game's memory image is rebuilt by hand — one block relocates itself on startup — and then openMSX loads the real tape and its RAM is dumped at the exact instant the game starts. The two match, byte for byte.

From there, code is traced by following control flow from the known entry points, never by disassembling linearly: 18 KB of graphics decoded as instructions would throw everything out of alignment. Regions known to be data are declared off-limits, because one bad seed sends the tracer into the artwork and inflates coverage with nonsense.

Two separate checks guard the result. Reproducibility: every listing must give back the original binary — reassembled for the machine code, re-tokenised for the BASIC. And a budget: every byte must be either traced code or a data range named in the notes. The second exists because the first cannot see misinterpretation — if graphics get marked as code, the bytes still come out identical and only the listing lies.