Under the hood

Getting started

What you need

pasmo and z80dasm to assemble and disassemble, and Python 3 for the tools. And for screenshots, and for the harnesses that pull data out of the running game, openmsx.

The tape isn't distributed with this repository, only the documentation work. So you'll need your own copy, named stardust.tsx in the root of the project, and it has to give this sha256:

8f4fb3840e5ad043d8d694faeaa86a6e4a5cd2cabe5dd99fec08e5cf0a7dbb13

The commands

make          # extracts the blocks, generates the listings and checks everything
make test     # the tests only
make web      # rebuilds the site under docs/

make on its own runs the whole cycle, and it fails if anything doesn't add up: if a listing stops reproducing its block byte for byte, if the tracer wanders into a data zone, if an entry point sits inside a range declared as data, or if a single byte of the tape is left unaccounted for.

Without the tape

You can still read the listings in src/ and the notes, which is where the work actually lives. And the tests that don't depend on the binary pass all the same, needing nothing else: a handful of them just do arithmetic on the published figures.

How it is laid out

Each block on the tape has three files that govern its disassembly:

   
src/X.entries the entry points: where tracing starts
src/X.nocode the zones that are NOT code, and how that is known
src/X.notes the annotations, and the data ranges with their explanation

Those produce the src/stardust_X.asm files, and those never get touched by hand: they're generated. If you want to change a comment, it goes in the .notes file instead.

That separation is exactly what keeps the listing and its verification from drifting apart over time: the file that gets published is the same one that gets checked.

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, so where one table ends and the next begins is visible at a glance. An optional line gives the block the row width of its real structure: the 59 characters of the charset eight bytes at a time -one glyph per row-, the level tiles four -one row of a 32x32 tile-, the sentinels three, the final scene's picture eighteen (its own row length), the instrument table fifteen bytes per instrument, and the tables of counter-plus-records with the counter alone on the first row. Tables of pointers come out as defw, with the destination named next to them where it has a name.

The tools you'll reach for most

   
tools/perfil.py X-rays a binary and says where the nature of the data changes
tools/clasifica_huecos.py classifies a range by measuring it, and says which measurement the label came from
tools/check_entradas.py refuses any entry point that falls inside a range declared as data
tools/coteja_spectrum.py cross-checks this binary against the Spectrum version's
tools/render_graficos.py draws tiles, sprites and charset straight from the tape
tools/omsx_*.tcl openMSX harnesses: they pull out of the running game what the binary won't say
tools/omsx_replay.tcl replays a recorded playthrough and samples the program counter

If you want to pull on a thread

No bytes are left unowned, and no routines uncommented — but there are still loose ends, like the 149 bytes of music nothing names, or the noise mode no score ever switches on. They're counted, figures and all, in Open questions, and everything needed to look into them is right there in the repository.

The tape

It's 94,228 bytes of TSX file, which open up into 93,861 bytes of content spread across seven blocks. And the odd part shows up right at first glance: three of them are recorded the way the MSX expects, and the other four aren't.

The seven blocks

  block bytes goes to what it is
1 STARDU 256 — the BASIC loader, plain text
2 TOPO 4254 0x9470 the publisher's animated logo
3 LOADER 351 0xD2F0 the turbo loader
4 — 12,468 0x9B8C the loading screen, and it executes
5 — 46,663 0x47A0 the ship game, zones 1 to 7
6 — 8 — an eight-byte descriptor
7 — 29,861 0x61D0 the second part, the one on foot

The first three are KCS blocks, the MSX's own tape format, with the usual header and filename. The four that follow are ZX Spectrum blocks: a flag byte, the data, and an XOR check byte at the end. All four carry that XOR correctly, verified one by one.

The loading chain

It starts in the BASIC loader, which is just five lines:

10 COLOR 1,1,1:SCREEN 2
20 BLOAD"cas:",R          ' the logo, which runs by itself
30 BLOAD"cas:"            ' the turbo loader, which does NOT run
40 CLS:COLOR 0,0,0:SCREEN 2
50 DEFUSR=54000!:A=USR(0) ' 54000 = 0xD2F0: now it does

And it carries on in the turbo loader, which does all this in order: hunts for RAM and maps it into pages 1 and 2, to get a flat 48K from 0x4000 to 0xFFFF, like the Spectrum's; copies 300 bytes of itself to 0x4000 and jumps there; saves 94 bytes from 0xDAC0 to 0xFDE8, the patch mailbox, and sets the stack; loads the loading-screen block and executes it; loads the game block right on top, wiping out the previous one; applies the patches from the mailbox, if there are any; and finally jumps to 0xBD85, which is the game.

The eight-byte descriptor and the last block aren't loaded by it at all: the game itself asks for them later, on clearing the last zone.

And the eight-byte descriptor isn't data: it's code

Those eight bytes are the prettiest thing on the whole tape. The game asks for them at 0xF77D, and that address is no ordinary mailbox: it is the exact spot where the two instructions that will load the next block live, in the listing itself. The header lands on top of them and they are executed right afterwards.

On tape, the eight bytes are:

02 DD 21 D0 61 11 A5 74

The 02 is the type, and the game checks it before anything else. The other seven are, as they stand, ld ix,061D0h and ld de,074A5h: the destination address and the length, opcodes included. That is why the jump at 0xF77B is a jr $+3, which skips the type byte and lands right on them.

It adds up on all four sides: the next block is 0x74A5 bytes long, 0x61D0 + 0x74A5 = 0xD675, and the loader's final jump, to 0xA279, falls inside what has just been loaded.

And the same 2 does three jobs: it is the header's type, it is the last byte of the data block —the mark that it arrived whole— and it is the constant the two are compared against. The game does not keep that constant anywhere separate: it reads it from the operand of its own cp 002h, at 0xF76E. Three uses, one single byte.

The blocks tread on each other

And this is exactly what forces you to look at the tape differently. In memory, the blocks don't coexist at all:

loading screen   0x9B8C - 0xCC3F
ship game        0x47A0 - 0xFDE6    <- wipes out the whole loading screen
second part      0x61D0 - 0xD674    <- lands inside the game

So there's no single 64K image holding the whole game, just three photographs of memory at different moments. Each listing carries the org of where it really runs, and the byte budget gets summed over the tape's blocks rather than the memory map — summing over memory would count the overlapping bytes wrong, some twice and others not at all.

And this isn't a technicality: an address without an instant attached to it means nothing here, because several programs share the same addresses at different moments. Sampling the program counter of a real playthrough turns up 122 addresses that look like code executing inside the tileset and aren't: 66 belong to the TOPO logo, 9 to the loading screen, and 56 sit below 0x8000, which is the BASIC ROM until the loader maps RAM over it.

How much came across from the Spectrum

The cross-check tool (tools/coteja_spectrum.py) aligns the two binaries whole: it indexes the windows of the Spectrum binary, counts which offsets keep coming up, and from the dominant ones extracts the maximal runs of identical bytes. Every stretch it calls shared comes with its own evidence — where it starts in each binary, how long it is, and at what offset. Locating each section just by its first 32 bytes and keeping the first match doesn't work at all here: since both versions share the artwork, those needles land inside the tileset.

And what comes out of it reframes quite a bit of how you picture this conversion:

25,015 bytes identical to the Spectrum      53.6% of the block
   of those, CODE:       81 bytes            0.3%
   of those, DATA:   24,934 bytes           99.7%

The code isn't shared. The artwork and the data were carried across byte for byte — and to the same address, at offset zero: the graphics occupy 0x6037–0xA55F and 0xA561–0xBD84 on both machines — but the code got rewritten from scratch. The only two stretches of code that do match, 55 and 24 bytes, are unrolled runs of adc hl,hl: they come out identical because they're the same instruction repeated many times over, not because anyone actually copied them.

And that fits what the game's own credits screen says, crediting the conversion to Carlos Arias while the graphics stay with the Arévalo brothers, same as the original.

Of the names in the Spectrum's control file, 20 out of 138 are backed by identical bytes. Among them the data for the seven zones, the tile geometry — "111 tiles at 4x32 bytes per tile", exactly matching what this project measures on its own — and the DEMO message, in the same string.

About the on-foot part, the cross-check can't say a thing, and it's not the tool's fault: the authors of the Spectrum disassembly warn in their own README that "the entire on-foot second stage of the game also fell outside the scope". They simply never disassembled it, so it isn't in the snapshot being compared against here.

The code

The tape's seven blocks come to 93,861 bytes, and 20,076 of them are code the tracer reaches by following the flow. The rest is graphics, tables, buffers and padding — all of it named, though.

That figure is deliberately low. It only counts code genuinely arrived at by following calls and jumps from a known entry point, and there's more code on the tape than that covers — but until it's known how it gets entered, it doesn't get counted, because taking it as traced is exactly the mistake that had to be undone here in the first place.

How the screen gets drawn

Here's the big difference from the original. The Spectrum writes straight into its screen memory, which is ordinary RAM; the MSX keeps its video memory behind the graphics chip, so it has to be sent out through a port. Which is why this version carries an intermediate buffer the original never needed at all.

The buffer runs from 0x4000 to 0x4EFF, 3,840 bytes, 24 wide by 160 tall, and the dump moves it to VRAM in three bands — 0x4000 with 56 rows, 0x4540 with 64 and 0x4B40 with 40, contiguous and closing to the byte — one per call, each into its own third of SCREEN 2. And each band gets walked by columns, not by rows (here's the third call, the 40-row one):

f3f2: ld de,04b40h     ;       the band: its buffer, its target and its height
f3ff: ld c,018h        ; 24  <- OUTER loop: steps the buffer one byte at a time
f401:   call 0EE24h    ;        sets the video memory address
f408:   ld de,00018h   ; 24  <- the STRIDE within the buffer
f40b:   ld a,(hl) / out (098h),a / add hl,de
f40f:   djnz           ; 40  <- INNER loop, with B=0x28

The axes are worth dwelling on, because they're easy to read backwards. The ld b,028h looks like it's saying "40 columns" and it isn't: it's the inner loop, and it walks the buffer in steps of 24 — so it's actually collecting 40 bytes from a single column. The one really counting columns is the outer loop, stepping the buffer one byte at a time, 24 times over.

Drawing it settles the question: split the buffer 24 at a time and the high-score table comes out legible, halftone and all; split it 40 at a time and it's noise. And it matches what you see while playing: 24 bytes are 192 pixels, narrower than the screen's 256 — hence the fixed frame down the sides — and the surplus sits vertically, exactly where it scrolls.

That routine racked up 3,252,480 writes in two minutes of play, which makes it by far the hardest-working code in the whole game. Watching the port video memory arrives through turns up seventeen separate routines that write to it — and that method, watching who writes instead of guessing what the data is, is exactly what let the buffer get pinned down to the byte.

Sprites, the Spectrum way

The Spectrum has no hardware sprites, so they have to be drawn by hand: the image gets shifted bit by bit to the exact position, a hole gets opened in the background with AND, and it gets painted with OR. And to avoid paying for the shift loop every time, the instruction gets written out N times and entered halfway through:

c4c5: ld a,h / ld h,l / ld l,0ffh
c4c9: jp 0c4e1h          ; the jump that vaults over the whole run
c4cc: adc hl,hl / adc a,a / adc hl,hl / adc a,a / ...

That technique is exactly what throws the tracer off: the jp skips the whole run, and the adcs get entered through a computed jump that isn't anywhere in the binary. The only reason we know they run at all is that the emulator caught them running, halfway down the run.

And the MSX does have hardware sprites, even though they're not used for this: the conversion brought the source machine's method across as it was, without adapting it.

A script interpreter

The sound isn't written as code at all, but as scripts a small virtual machine interprets. Bytes below 0x80 are notes; from 0x80 up they're commands:

e230: ld a,(bc) / cp 080h / jp c,0e231h
      sub 080h / ld hl,0e7a3h / call 0e5c0h / jp (hl)

with 0xE5C0 doing "HL = table + A×2, HL = (HL)". There are fifteen commands in total, and that jump table is also the first trap in tracing: left undeclared as data, the tracer walks straight in and starts disassembling addresses as though they were instructions.

Every object carries its own routine

The ship part settles entity behaviour like this: each one keeps a pointer to its governing routine right in its own 8-byte structure, and the game jumps there with jp (hl). Those structures arrive all 0xFF on the tape and only get filled in while playing, so the destinations can't be read cold from the binary — they have to be caught with the game running. Doing that turns up eight routines, with the captured IX values stepping 8 at a time.

The same measurement in the part on foot steps 46 at a time. But that jp (hl) — the one at 0xC544 — doesn't dispatch entities at all: it dispatches the sound interpreter's commands, and the 46-byte objects are its three channels. The routine that starts a sound takes the channel number and multiplies it by 46 (ld de,0002eh) to reach its state. The whole story is in Findings.

The on-foot enemies, on the other hand, carry no routine pointer at all: they live in 5-byte tables — walkers at 0xACE4, flyers at 0xACF9, the turret's two shots at 0xAD04 — and fixed loops move them, one per species.

Code that writes itself

There's a dispatcher that patches its own calls:

f000: ld (0f016h),hl    ; the operand of a CALL gets written
f003: ld (0f019h),de    ; and the operand of another
...
f015: call 0000h        ; that 0000 isn't real: HL fills it in
f018: call 0000h        ; and DE this one

Read cold, that looks like call 0, which would reset the machine if it ever actually ran. Where it really calls isn't in the binary at all — it's in the registers of whoever invokes it at the time.

What only a full playthrough shows

Both of the things above — the unrolled runs and the self-patching dispatcher — are really the same problem seen twice: a static tracer just can't follow them. The only way past it is watching the machine run, and for that a recorded playthrough beats any scripted harness, because it reaches screens a script never does.

Replaying a complete 38-minute run and sampling the program counter, of the 1,489 addresses the ship game actually executed, the tracer already reached 1,444 on its own. The 45 it missed became entry points, each with its sample count beside it.

In the second part, far less explored because you have to clear seven zones to get there, 159 addresses turn up untraced. Two are worth telling apart, because they show the trap in sampling the program counter when several addresses on the tape serve different programs at different times:

0xD48C   139,323 samples (window set wrong, see below)
0xC865    27,928 samples

0xD48C is not code. It disassembles to nop / rst 38h / nop / rst 38h, the disassembler gives up partway through with an illegal sequence, and nothing in the listing calls it. Sampling the program counter over 130 seconds of play with the window set right — after the tape load has actually finished, not when it should have — not one sample lands there: 259,149 samples across two windows, zero at that address. Its 489 bytes are simply data: 21 distinct values, 199 of them 0xFF, and the rest bytes with few bits set, arranged in pairs — 40 ff / 01 ff / 41 ff / 00 ff / 40 f9 — exactly what artwork with a mask looks like in a conversion that has to shift its sprites by hand.

What actually takes care here is the window. At t=1775 — "once the load finishes" — the load hasn't actually finished: the program counter sits at 0xF87E, 0xF88D, 0xF887, still inside the tape loader, across only 45 distinct addresses. The tape's still turning, and half the memory from 0x61D0 up is still the ship game underneath it. Five programs occupy the same addresses at different moments — the TOPO logo, the loading screen, the BASIC ROM, the ship game and the second part — so the samples have to be split into windows with a clear reference point — the loader's jump to the game at 0xBD85, or the second load — getting both the start and the end of the window right.

0xC865, meanwhile, isn't a real address at all: it's the second byte of the instruction that starts at 0xC864, which is why a breakpoint set at 0xC865 never fires — breakpoints only trigger where an instruction actually starts. 0xC864 is code: the handler for the sound interpreter's 0x8C command, the one that calls a phrase of the music. With 208 appearances in the score, it's by far the commonest command, so its 27,928 samples add up just fine.