Under the hood

Getting started

A commented disassembly of Hole in One, the golf game HAL Laboratory released for the MSX in 1984: a 16 KB cartridge mapped into page 2 (0x8000-0xBFFF). It reassembles into the exact ROM, byte for byte, and every one of its 16,384 bytes is accounted for.

The cartridge is not here

No repository distributes the game. Put your own dump in the root as holeinone.rom, 16384 bytes, sha256

14311cc241c53b6ebbf3b0d18101405a5584a259ab0fa265391abe2b791b9eab

make comprueba checks it.

Nor is the tape

The three extra courses come from the Hole In One Extension Course tape (HAL, 1985), which is not distributed either. Put your two sides in the root as extension-course-a.cas and extension-course-b.cas, sha256

A  14ddc079e3bf4c6acf6fb55d0583ccc271de792fe2cab0ccb88d8bd9fbd0224f
B  1e69119111e290d309f16ab6186c32942ead098f32117b94edc8fccf3a46facf

tests/test_cinta.py checks them, and make web redraws the three courses from them. Without the tape those tests fail on purpose: a test that skips checks nothing.

What each command does

make            traces the flow, builds the listing, reassembles it and runs the tests
make verify     the test that decides: reassembling must give the ROM back
make sanity     that not one byte is left unexplained, and no data comes out as code
make densidad   how much is commented, routine by routine
make web        rebuilds this site, drawing the images from the ROM

How it is put together

The listing is not edited by hand: tools/mkasm.py builds it from a flow trace and a file of notes anchored to addresses, so the comments survive a re-trace. The tracer follows the flow from the entry points; the ones it cannot work out by itself - the interrupt hook and the BASIC command hook - are declared in src/holeinone.entries, each with its reason.

The images are not captures either: tools/graficos.py draws them by running in Python the same decompressors and the same script interpreter the Z80 runs. If a range or a format were wrong, noise would come out instead of the course.

The cartridge

Hole in One is a 16 KB HAL Laboratory cartridge from 1984, mapped into page 2 (0x8000-0xBFFF), with no bank switching.

The header declares two hooks, not one

The AB header at 0x8000 puts INIT at 0x8028 and, on top of that, STATEMENT at 0x8010. The second is the hook the BIOS calls when the BASIC interpreter meets a statement it does not recognise, and here it compares the four bytes of the incoming command against the text GOLF, stored right between the two hooks, at 0x8024.

So the cartridge registers a command: from BASIC you type GOLF and the game starts without having to power-cycle the machine.

And it only starts if you are not pressing anything

The first thing INIT does is check row 7 of the keyboard with SNSMAT. If that key is down it returns without initialising anything, and the machine stays in BASIC with the GOLF command already registered.

The screen

SCREEN 2 set up by the BIOS with INIGRP, with the tables where they usually go: patterns at 0x0000, names at 0x1800, sprite attributes at 0x1B00, colour at 0x2000 and sprite patterns at 0x3800. The eight VDP registers read off the emulator confirm it: 02 E2 06 FF 03 36 07 01.

The game hooks its own routine into H.TIMI, writing a jp into 0xFD9F by hand, and from there it flags each new frame and steps the sound along.

How the 16 KB break down

  bytes  
traced code 6,065 37.02 %
identified data 10,319 62.98 %
unexplained 0 0.00 %

The code

Two sibling decompressors

The cartridge stores everything it draws compressed, in two formats that look alike but are not the same.

The run one (0x983C) looks at each byte's high nibble: if it is 0xA it is a run - the low nibble plus two says how many times, the next byte says what value

Both stop on the destination pointer, not on the source: the caller leaves in BC the VRAM address to stop at. That is what lets one table serve three different destinations, and also what makes the short tables overrun.

The pair format's low nibble would be an outer counter, but across the 128 blocks of the cartridge's three tables it is zero, so the block never repeats: there is no data here that tells apart the two possible readings of that loop.

Three script interpreters

Neither the scoreboard, nor the title, nor the course is drawn with code: all three are walked as little languages.

One instruction with two entry points

0x9505 is or 0AFh - the bytes F6 AF - and some callers come in on its first byte and some on its second. Through the first, the or runs and the Z flag comes out clear; through the second, the operand runs, which on its own is xor a, and Z is set. That flag is what the six calls behind it read: long tables, or the wordmark's.

How it is traced

The tracer follows the flow from INIT, but there are two places it cannot reach on its own, declared by hand in src/holeinone.entries: the BASIC hook at 0x8010, which is only entered by typing GOLF, and the interrupt handler at 0x94CB, which the game installs itself by writing a jp into H.TIMI.

Everything else comes out of the trace. Of the 16,384 bytes, 6,065 are code actually reached and 10,319 are data declared one by one, with its format and its proof.

In the emulator

The images on this site are not captures, but the emulator does get used: to check them. The difference matters. A capture proves the game runs; dumping the VRAM and comparing it against what tools/graficos.py draws proves the format has been understood.

How the dumps are taken

Two Tcl scripts for openMSX, without a single breakpoint: the dumps go by emulated clock, so they repeat.

tools/omsx_vram.tcl    six dumps of the start-up, out to 12 s
tools/omsx_juego.tcl   eight dumps with the game running, from 14 to 45 s

Each one saves the VRAM's 16,384 bytes, the eight VDP registers and a screenshot.

What comes out of the comparison

python3 tools/graficos.py holeinone.rom 0x8000 comprueba work/omsx

On the title screen, of the 13,664 bytes in the four tables 13,655 match. The nine that differ are the scoreboard digits on rows 1, 2, 3, 6 and 7 - the TOP and the stroke counts - which the game writes as it plays and which come from no table in the cartridge.

From the game itself come six dumps in which the 480 cells of the course are identical to the ones the script interpreter draws, hole by hole: 2,880 cells in a row without one difference. And with the game running, the pattern and colour tables are still exactly the title's, all 12,288 bytes.

The VDP registers

02 E2 06 FF 03 36 07 01

The sixth cleared one thing up: R6 = 0x07 puts the sprite patterns at 0x3800, so the 608 bytes 0xBE0D decompresses there are the game's nineteen 16x16 sprites.