Under the hood

Getting started

Just reading

The listings in src/*.asm are commented and need nothing built to be read. Where to start, depending on what you're after:

I want to see... look at
How the game boots alehop_game_nucleo.asm, at 0xC000
The engine: physics, collision, scrolling alehop_game_extra.asm
The music player alehop_game_sonido.asm
The turbo tape loader alehop_slots.asm
The maps and graphics alehop_game_datos.asm

The comments are in Spanish, matching the game's own language.

Rebuilding it

make

That extracts the tape, traces the code, regenerates the five game listings plus the other three modules, and verifies everything. Green means four things:

  1. Every listing reassembles to the original binary, byte for byte.
  2. The five game chunks, concatenated in load order, reproduce the whole turbo block.
  3. No data region has been mistaken for code.
  4. Not one byte of the 42645 is unexplained.

Then the 36 tests run.

You'll need:

The .tsx tape image isn't distributed with this repository. Without it, make says what it can't do rather than pretending it went fine.

The images

make imagenes

Draws the six levels from the game's own data: the full strip for each one (2048x64) and a real screen with its three bands. These aren't screen captures — they're generated by decompressing the graphics and assembling the maps, which is the best evidence that the format is properly understood.

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 levels readable in the listing itself: 32 bytes per row, which is exactly the 32 columns the camera shows, so a whole screen of that row of the strip is one line. The presentation, the intro, the scoreboard and the title screen come out the same way -one row of the screen per line-, the fixed sprites four bytes at a time -one attribute record each- and the graphics table as defw pairs: source and video destination side by side.

The emulator

ALEHOP_TSX="$PWD/alehop.tsx" ALEHOP_OUT="$PWD/dump" \
  openmsx -machine Philips_VG_8020-20 -script tools/omsx_load.tcl

This loads the original tape using the game's own loader as the decoder and dumps RAM at each stage. It produces dump/full_recolocado.bin, the 64 KB image the code is traced against: addresses only mean anything once the game has been relocated in memory.

To watch it play and take screenshots:

ALEHOP_TSX="$PWD/build/alehop_rebuild.tsx" ALEHOP_OUT="$PWD/build/capturas" \
  openmsx -machine Philips_VG_8020-20 -script tools/omsx_juega.tcl

Loading takes about seven minutes of emulated time, same as it did in 1988.

Modifying the game

Comments go in src/*.notes, anchored to addresses so they survive a full re-trace; the .asm files are generated from them, so a comment written into an .asm is lost on the next trace.

To change the code itself, edit the .asm and then:

python3 tools/build_tape.py build build/my_alehop.tsx

That assembles, rebuilds the blocks with their headers and checksums, and produces a loadable tape. As long as make was green before you touched anything, any change in behaviour can be pinned on what you changed rather than on a misreading of the binary.

The tape

Ale Hop! shipped on cassette. The preserved file is a TSX, an MSX-oriented format that stores the tape's blocks already demodulated rather than as audio: the bytes come out exactly, with no conversion guesswork.

60680 bytes in 12 blocks. Three are metadata, one is the catalogue entry, and the remaining eight hold the game.

What's on it

block what size
4-5 ALEHOP — the BASIC loader, as text 256 B
6-7 TOPO — the Topo Soft logo 4254 B
8-9 SLOTS — RAM finder and turbo loader 753 B
10 Turbo block 1: the loading screen 12388 B
11 Turbo block 2: the entire game 42645 B

The first three files travel in KCS blocks, the MSX standard: slow, with headers. The last two are turbo blocks, and that's where Topo Soft went off the beaten path.

The turbo format

A turbo block is trivial once you know it:

[0x00] [ ......... data ......... ] [checksum]

A zero sync byte, the data, and a checksum byte chosen so that the XOR of the whole block comes to zero. Verified on both blocks, and one of the project's tests.

What makes it fast isn't the format but how it's read: the SLOTS loader doesn't use the BIOS cassette routine at all. It times the pulse widths by hand, reading bit 7 of port 0xA2, which lets it push the tape speed far past what the BIOS allows.

The loading chain

Typing RUN"CAS:" kicks off a five-stage sequence. Verified in the emulator with a breakpoint at each stage: all of them are hit, in order, in 435 seconds of emulated time — the seven-odd minutes anyone who played this in 1988 will remember.

0x9470  TOPO      The Topo Soft logo, shown while the rest loads
0xC58F  SLOTS     Hunts for RAM in every slot, then relocates itself
0x88B8  turbo 1   Loads the cover screen and runs it: dumps to VRAM, returns
0x0000  turbo 2   Loads the whole game... into page 0
        Moves three chunks to high RAM and patches six vectors
0xC000  The game starts

The odd part: loading on top of the ROM

A 64 KB MSX doesn't have 64 KB within reach. Memory is seen through 16 KB windows called pages, and the bottom one — page 0, from 0x0000 to 0x3FFF — is normally occupied by the BIOS ROM. There is RAM underneath, but it's covered.

The usual thing for a tape game is not to bother and load from 0x4000 up. Ale Hop! does the opposite: it puts all 42645 bytes into page 0, on top of where the BIOS sits. To pull that off it first has to find that RAM and know how to switch it in, which is exactly what the SLOTS finder is for: it walks 4 primary by 4 secondary slots writing and reading back, and records two memory configurations — the normal one and the all-RAM one — so it can alternate between them during play.

Once loaded, it moves three chunks with LDIR:

from (load) to (runtime) bytes what
0x8A41 0xB000 2201 The sound player
0x92DA 0xBD00 2112 The core and title screen
0x9B1A 0xD000 2938 The game engine

And what stays down below — 35393 bytes of graphics and maps — gets covered by the ROM again. The game runs with the BIOS mapped in so it can call it, and only uncovers the RAM underneath for a moment each time it loads a level, between a call 0xF00F and a call 0xF000.

That's why the disassembly is five listings rather than one: each chunk is assembled at the address where it actually runs, and concatenating them in tape order reproduces the original block byte for byte.

The round trip

The format is understood completely, and there are two proofs:

  1. tools/tsx_parse.py and tools/tsx_build.py take the TSX apart and put it back together with the same sha256. Not one byte of the container is left uninterpreted.
  2. tools/build_tape.py assembles the commented .asm files, rebuilds the blocks with their headers and checksums, and produces a tape identical to the original (499d74c7...). That tape loads in openMSX and the game runs.

The second one is what makes the disassembly useful: change something in the code and you can still get back to a tape that boots.

Inside the code

The 42645 bytes of the game block break down like this:

  bytes  
Traced code 4588 10.8 %
Identified data 38057 89.2 %
Unexplained 0  

Only 10.8 % being code doesn't mean there's disassembly left to do: it means Ale Hop! is mostly graphics. An MSX game of this era fits in very few instructions; what takes up room is the artwork.

Five listings, not one

The loader relocates three chunks before starting the game, so tape addresses and runtime addresses don't line up. To have the listing read with the real addresses and still reassemble to the original binary, the game is split into five modules:

listing runs at on the tape at bytes
alehop_game_datos.asm 0x0000 0x00000 35393
alehop_game_sonido.asm 0xB000 0x08A41 2201
alehop_game_nucleo.asm 0xBD00 0x092DA 2112
alehop_game_extra.asm 0xD000 0x09B1A 2938
alehop_game_cola.asm 0xA694 0x0A694 1

Concatenated in that order they come to exactly 42645 bytes. tools/split_trace.py does the splitting and a test checks the result.

Living with the ROM

The game starts at 0xC000, and the first thing it does after setting the stack is configure memory through three calls into a slot switcher the loader left at 0xF000:

ld sp,0xF37F
ld (0xC52E),a      ; 0 = normal boot, non-zero = returning from a finished game
call 0xF000        ; page 0 -> BIOS ROM
call 0xF014        ; page 1 -> RAM
call 0xF019        ; page 2 -> RAM

That switcher has six entry points doing the same job with different parameters: three put a page into the BASIC configuration and three into the all-RAM one, covering pages 0, 1 and 2.

The practical consequence, worth keeping in mind while reading the listing: any address below 0x4000 appearing in a CALL or JP is the BIOS, not game data. The 35 KB of graphics sitting underneath are only visible inside the level loader.

The frame routine

The engine has no conventional main loop. Instead there's a 16-bit task mask at 0xDB69. The frame routine waits for the vertical blank and then checks it bit by bit:

ld ix,0xDB69
halt
bit 7,(ix+0) / call nz,BLIT_MAP
bit 6,(ix+0) / call nz,BLIT_BACKGROUND
bit 5,(ix+0) / call nz,ANIMATE_TILES
bit 4,(ix+0) / call nz,APPLY_SPEED
bit 3,(ix+0) / call nz,ADVANCE_CAMERA
bit 2,(ix+0) / call nz,APPLY_HEIGHT
bit 1,(ix+0) / call nz,FRAME_PATTERN
bit 0,(ix+0) / call nz,DRAW_PLAYER
bit 7,(ix+1) / call nz,COLLISION
bit 6,(ix+1) / call nz,JUMP
bit 5,(ix+1) / call nz,COUNTDOWN

Change the mask and you change the game's state without touching the flow: 0xE0FF during normal play, smaller values while the player is dying or the stage is ending. The main loop puts it back every time round:

MAIN_LOOP:
    ld sp,0xF37F        ; the stack is reset: collision handlers jump without unwinding
    call FRAME
    ld hl,0xE0FF
    ld (0xDB69),hl
    jr MAIN_LOOP

The fast interrupt

The game hooks itself into H.TIMI (0xFD9F), the hook the BIOS calls on every video interrupt. But it doesn't return the way it should:

in a,(0x99)         ; acknowledge the VDP interrupt
pop hl              ; THROW AWAY the return address into the BIOS handler
di
ld hl,0xDB75
inc (hl)            ; frame counter
call SOUND_FRAME
pop ix / pop iy / pop af / pop bc / pop de / pop hl
ex af,af' / exx
pop af / pop bc / pop de / pop hl
ei
ret

It discards the way back into the BIOS and unwinds by hand the registers the BIOS had just pushed, skipping the rest of the system interrupt routine. In a game this tight on time, that's a lot. There are three copies of this same skeleton in the binary: the title screen's, the gameplay one, and a third that never runs.

Sound

Each of the PSG's three channels gets a 46-byte state block — the code says so itself: ld de,0x002E — holding its melody pointer twice over (one advances, the other is there to loop), the remaining note duration, volume, period, and offsets that get added every frame. Those offsets are two software envelopes: one for volume with two phases and one for period with three, each with its own step counts, signed increment and delay. They are not vibrato: they run once through, and would only start over if command 0x8A turned their bits on, which never happens in the title music — the only 0x8A across the three voices carries a 0.

A melody is a byte stream: values under 0x80 are notes, 0x80 and above are commands dispatched through a 12-entry jump table at 0xB486.

All three channels write into an 11-byte shadow buffer at 0xB4A0, laid out in the chip's own register order, and a single routine flushes the lot at the end of the frame.

The player doesn't look like code written for this game so much as a library dropped into it. The tell is in the binary: it comes with its own interrupt handler, ready to hook into the system — and the game never uses it, because it has its own.

Graphics

Compressed with a 16-bit token RLE:

0xFFFF        end of block
bit 15 set    repeat the next byte (n & 0x7FFF) times
bit 15 clear  copy n literal bytes

Eleven blocks, each decompressing to exactly 2048 bytes — one screen third of patterns or colours, or the sprite patterns — chaining without a single spare byte from 0x4800 to 0x884D.

There are two graphics sets, one for gameplay and one for the title screen, sharing the third screen band and the sprites. Their tables live at 0xDB17 and 0xDB35 and are lists of (compressed source, VRAM destination) pairs terminated by 0x0000.

How it was done

The game's own loader was used as the decoder: rather than reimplementing Topo Soft's turbo tape format, the original tape is loaded in openMSX and RAM is dumped at each stage. What comes out is exactly what the game sees in memory — and it matches the bytes extracted from the tape file, byte for byte.

From there the code is traced by following control flow from the entry points, never by disassembling linearly: 35 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 reassembles to the original binary, byte for byte. And a budget: every byte must be either traced code or a data range named in the notes. The second one exists because the first can't see misinterpretation — if graphics get marked as code, the bytes still come out identical and only the listing lies.