Under the hood

Getting started

This repository holds the commented disassembly of Konami's The Goonies for the MSX (RC-734, 1986). It does not hold the cartridge: the ROM image is not distributed.

What you need

It is exactly 32,768 bytes and its fingerprint is:

sha256  2ba602b1a17e4797da1588afe6e65640331c15746bf5959f4778b404be929dde

To check it:

make comprueba

Reproducing the whole thing

make

That chains the four things that matter:

step what it does what it proves
make listado builds src/goonies.asm from the trace and the notes that the listing is not hand-written
make verify reassembles and compares the sha256 that the listing is the cartridge
make sanity checks how the bytes are accounted for that not one byte is left unexplained
make test 44 checks that what gets published holds up against the bytes

The one that decides is make verify. If the reassembled sha256 is not the one above, the listing is lying somewhere.

The numbers

make densidad

0 routines below 10 %, out of 1176
in total: 8774 instructions, 3236 comments, 36.9 %

The pictures

make imagenes

Draws into work/gfx/ the title screen, the twenty-five level maps with their creatures and hazards, the five round minimaps, the player's figures in colour, the tile sheet and the twenty-three inventory icons. None of them is a capture: they are built by running in Python the same steps the Z80 runs.

Checking against the emulator

make vram

Boots openMSX with the cartridge, lets it run from cold and dumps VRAM at five instants; then it subtracts that VRAM from the one tools/vram.py builds. No playing and nothing to load: the intro, the title and the demo come up on their own and are deterministic, because the demo is a recorded game.

Latest measurement, on the latest build:

screen           colour       spr patt     patterns     names
title            0/6144       0/2048       0/6144       0/768
the nine         0/6144       0/2048       0/6144
level 1 room 1   0/6144       0/2048       0/6144
level 1 room 2   0/6144       0/2048       0/6144
level 1 room 3   0/6144       0/2048       0/6144

How the commenting works

Batches of comments are written into work/coment/tanda-NN-whatever.notes, with lines C 0xADDR text, L 0xADDR name and B 0xADDR HEADING, and then:

python3 tools/aplica_comentarios.py src/goonies.asm src/goonies.notes \
        work/coment --escribe && make listado && make verify

There are forty-two batches.

The cartridge

file       goonies.rom
size       32,768 bytes
sha256     2ba602b1a17e4797da1588afe6e65640331c15746bf5959f4778b404be929dde
catalogue  RC-734
machine    MSX1

Where it lives

A 32 KB cartridge in pages 1 and 2, 0x4000..0xBFFF. No paging, no mapper: all 32 KB are there at once and the Z80 sees the lot.

The two headers

The first is the usual one: at 0x4000 sit the "AB" signature and the address of INIT, which here is 0x406A. STATEMENT, DEVICE and TEXT are zero, and so are the six reserved bytes.

The second, at 0x4010, is not an MSX thing: it is the Konami Game Master header, for the house's cheat cartridge that plugs into the other slot. It starts with "CD", then 07 34 — the 0x07 of the RC-7xx range and the 0x34 of RC-734 — and then eleven bytes with the variables the Game Master wants to touch.

This cartridge never reads it: tools/quien_lee.py gives zero references to 0x4010..0x401E. It is there for the cartridge next door to read.

Eight of the Konami games here carry it, and all eight are disassembled in this series: Konami's Soccer and Football (RC-732), this one, Konami's Boxing (RC-736), Yie Ar Kung-Fu II (RC-737), Knightmare (RC-739), Twin Bee (RC-740), Nemesis (RC-742) and F-1 Spirit (RC-752). The marker in front goes by year, not by catalogue number: AB on the 1985 ones and CD from 1986 on.

The hidden mark

At the end of the cartridge, behind the 0xFF padding, sit the title in katakana written backwards and the catalogue number:

グーニーズ   34   0xAA

It is read with the house code: index = byte − 0x80, and indices 0 to 44 are the gojūon in order. The finding is Manuel Pazos's, who uncovered it in 2021; without him nobody would know to look there.

This cartridge also pins down a character that was still unconfirmed: 0xBA, index 58, is the long-vowel mark ー. グーニーズ only works out that way.

The VDP registers

0x470F pushes eight bytes into registers 0 to 7, one at a time:

reg value what it says
R0 0x02 mode 2 (SCREEN 2)
R1 0xE2 16 KB, display on, interrupt enabled, 16x16 sprites
R2 0x0E names at 0x3800
R3 0x7F colour: base 0x0000, mask 0x1FFF
R4 0x07 patterns: base 0x2000, mask 0x1FFF
R5 0x76 sprite attributes at 0x3B00
R6 0x03 sprite patterns at 0x1800
R7 0xE4 dark blue border

Careful with R3 and R4: in mode 2 they are not an address, they are base and mask. Reading them as an address gives you correct shapes with banded colours, which is the classic symptom.

The geometry that comes out of it is upside down from the usual:

colour             0x0000..0x17FF   three 0x800 banks
sprite patterns    0x1800..0x1FFF   64 patterns
patterns           0x2000..0x37FF   three 0x800 banks
names              0x3800..0x3AFF   768 tiles
sprite attributes  0x3B00..0x3B7F   32 sprites

The colour table sits below the pattern table. An ld hl,00008h in this cartridge does not point at patterns: it points at colour.

How the 32,768 bytes break down

  bytes %
traced code 17,422 53.17
identified data 15,346 46.83
unexplained 0 0.00
total 32,768 100.00

Counted by tools/presupuesto.py, and checked by make sanity on every build.

Konami's framework

The table dispatcher — pop hl / add a,a / call ... / ld e,(hl) / inc hl / ld d,(hl) / ex de,hl / jp (hl) — is the same in eleven of the house's cartridges. Searching for its tail (5e 23 56 eb e9) across the 47 ROMs to hand turns it up exactly once in each: Sky Jaguar (RC-721), Yie Ar Kung-Fu (both builds), Hyper Rally (RC-718), Road Fighter (RC-730), Konami's Ping Pong (RC-731), Konami's Soccer and Football (RC-732), Hyper Sports 3 (RC-733), this one (RC-734), Nemesis (RC-742) and F-1 Spirit (RC-752).

In this cartridge it is used twenty-six times, and all twenty-six tables sit immediately behind their call.

The code

The main loop

0x40BF counts the frame, checks pause and dispatches by scene. (0xE000) is the scene and (0xE001) the subscene, and every scene splits into its subscenes with a chain of djnz: register b arrives holding the subscene and counts down until it lands on the right piece.

The nine scenes are in the table at 0x40F6:

scene what it does
0 the intro: the sign coming down, and the caption
1 wait, touches no VRAM
2 the title, the nine walking, and the game
3 the countdown
4 the level starts
5 the game
6 a life lost
7 game over
8 round cleared

The table dispatcher

Twenty-six times over, this shows up:

call 04060h
defw target_0, target_1, ...

0x4060 does a pop hl to recover its own return address — which is the start of the table — then add a,a, and jumps to entry number a. The pop also removes the return, so the target finishes on its own account.

How many entries each table has is not estimated: they are closed, in this order, by the ld hl,<nnnn> / push hl that manufactures the return address, by the fit that no entry points inside the table itself, and in one case by a call. All twenty-six are in src/goonies.entries with their arithmetic.

The three drawing interpreters

The cartridge does not store screens: it stores scripts, and it has three ways of reading them.

0x4682, literal script. [vramAddr][bytes...], with 0xFE to change address and carry on and 0xFF to close. Written byte by byte with WRTVRM.

0x4699, the same script but erasing. It is the same routine with c = 0 instead of 0xFF, so the and c at 0x4692 turns every byte into a zero. The same script paints and erases, with nothing stored on the side.

0x469D, RLE script, straight through the VDP port:

0x00        end
0x80        change address and carry on
0x01..0x7F  the next byte, repeated n times
0x81..0xFF  n & 0x7F literal bytes

And it reads backwards from what it looks like. What separates the two cases is where the djnz jumps back to: the one at 0x46BA returns to ld a,(de) / inc de — so it re-reads, and that is why bit 7 set is the literal — and the one at 0x46C4 returns only to out (c),a, so bit 7 clear is the run.

That the reading is right is proved by the fit: every script ends exactly where the next one begins, with not a byte to spare. tools/formatos.py walks them and tools/cobertura.py checks the fit.

How 64,000 tiles fit in 2,983 bytes

A hundred rooms of 32x20 tiles: 64,000 tiles. And they take this:

  bytes
the 25 maps, 80 bytes each 2,000
the definitions of the 108 blocks 717
the block pointer table 216
the map pointer table 50
total 2,983

Which is twenty-one tiles per byte. Three layers make it possible:

One. A level is eighty bytes: four columns by twenty rows of 8x4 tile blocks. That gives 32 columns by 80 rows, which is the same as saying the four rooms one after another. They are built in RAM at 0xE600, 0xE880, 0xEB00 and 0xED80 — 640 bytes each — and 0x5F13 dumps whichever one is current into the name table from row 2 on, since the top two rows are the scoreboard.

Two. Bit 7 of the byte asks for the block mirrored, and that is used 615 times out of 2,000. Mirroring a block is free: it is read backwards.

Three. The 108 blocks share tails. When two end with the same rows, the second one's pointer starts halfway into the first and those bytes serve both. Counting each byte once they take 717 instead of 841. Block 9, for instance, is two bytes of its own (0x39 0x03) with the whole of block 10 tucked in behind.

And on top of that each block is compressed. 0x5A05 decompresses it onto 0xE1F0 and stops when the pointer reaches 0xE210, that is at exactly 32 tiles:

0x0n        n zeroes
0x1n        n times 0x40
0x2n <b>    n times <b>
0x3n <b>    the pair factory at 0x5A50
anything    that tile, as is

All 108 blocks are used. Not one is spare.

Two twin tables of 19 rows by 4 columns, at 0x52DB and 0x5327. 0x52CD enters with (0xE06A)*4 + the current room: the row is picked by the level's room layout and the column by the room you are in.

The 19 rows are not an estimate. The 25 bytes at 0x9D67 — one per level, the ones that go into (0xE06A) — top out at 18, so 19 rows are needed and not one more. And 19×4 = 76 bytes is exactly what there is from 0x52DB to 0x5327, and as much again from 0x5327 to 0x5373.

Which of the two gets consulted depends on which side you leave by: at 0x5290, if the column drops below 0x15 it goes to the first; at 0x5296, if it reaches 0xC0, to the second. One is the left-hand exit and the other the right.

The sprites

0x586A assembles three sprites in one go from a nine-byte entry: y offset, x offset and pattern number, three times over. The entry is picked by (ix+005h) times nine.

And there is a rotation so the same ones do not always flicker. Sprite attributes are copied into VRAM starting each frame at a different spot in the buffer at 0xE0AC. Eleven positions, and each entry brings where it starts and how many bytes go before wrapping: 0xE0AC+8n and 0x54-8n, with n from 0 to 10. Those 0x54 bytes are 21 sprites.

But the first eleven sprites are out of the rotation: they always go in the same place, because they are the scoreboard and the player, which cannot be allowed to flicker.

The sound

The engine lives at 0xB585..0xB80A and plays through the PSG with WRTPSG. You ask for a piece with ld a,<n> / call 0B590h, and the and 03fh at 0xB59F says the number fits in six bits. Before playing it checks the priority of whatever is already sounding (cp e / ret c), so a weak effect does not trample the music.

Each voice is a pointer, and the voices of a piece are consecutive in the table: 0x5BCB does add a,a and takes 2 or 3 entries from 0xB814 + n*2. That is why the table is not split into pieces: it is a bag of 47 pointers. The last three are 0xBFEE, which is where the padding starts: they are empty voices.

And mind where that table begins, which very nearly slipped through. It looks like it runs from 0xB814 to 0xB874 with 48 entries, and then the first one is 0x393C, which is nowhere near the music. It does not. The period table has twelve bytes, not ten — it is a whole chromatic octave, and the twelve come out in a ratio of 1.059 to each other, which is the twelfth root of two — so it reaches 0xB816 and the pointers are 47. That the sum is 0xB814 + n*2 only means the first piece is number 1: there is no piece 0.

In the emulator

Everything on this page is done by booting the cartridge from cold. No playing, no savestate, no breakpoint: the intro, the title and the demo come up on their own and always the same, because the demo is a recorded game.

Where each screen is

tools/omsx_sonda.tcl looks ninety times, once per emulated second, at which scene is running. That is how the instants get chosen without guessing:

second scene what you see
5–11 0 the intro, with the sign coming down
12–15 1 the title with the THE GOONIES lettering
16–35 2.1 the nine characters walking
36– 2.3 the demo playing itself

To run it:

openmsx -machine Philips_VG_8020 -cart goonies.rom \
        -script tools/omsx_sonda.tcl

Checking against VRAM

make vram

Boots openMSX with no renderer and no throttle, dumps the 16 KB of VRAM at five instants — 13, 25, 45, 60 and 75 seconds — and then subtracts that VRAM from the one tools/vram.py builds, which is the Python translation of the cartridge's loading routines.

The first three tables are static: they are built on entering the screen and not touched until it changes. They have to come out at zero.

screen           colour       spr patt     patterns
title            0/6144       0/2048       0/6144      (and names 0/768)
the nine         0/6144       0/2048       0/6144
level 1 room 1   0/6144       0/2048       0/6144
level 1 room 2   0/6144       0/2048       0/6144
level 1 room 3   0/6144       0/2048       0/6144

The trap that cost half the work

The cartridge does not clear patterns or colour when the scene changes. Only the name table.

Building the title screen on its own gave hundreds of bytes of difference that were not a misreading: they were inheritance that was missing. The title screen is painted on top of whatever the intro left behind — the font and the sign that comes down — and the level is painted on top of whatever the nine-characters screen left.

That is why vram.desde_el_encendido() chains the scenes in the order of the table at 0x40F6:

scene 0.0   the font and the sign coming down     (0x412B)
scene 0.2   the THE GOONIES lettering             (0x4123)
scene 1     wait, touches no VRAM                 (0x4139)
scene 2.0   the bottom two rows cleared and the
            nine-characters scenery               (0x4175)
scene 2.3   the level                             (0x41B9)

With the whole chain, all five screens close at zero.

The phase of the water

The water's three tiles stay in whatever phase the last repaint left them, and that depends on bit 2 of the frame counter and on whether the previous room had water. The check tries both and says which one fits; the two batches are different, so getting it right by chance is not possible.

You can see it in the dump: room 0 of level 1 has no water ((0xE345) is zero) and the tiles are as the original script left them; room 1 does have water and they are in the other phase; and room 2, which has none either, inherits the phase room 1 left behind.

The tiles that move

The name table does not check out at zero, and it should not: that is where the game repaints creatures, water, cages and the scoreboard every frame. What gets looked at is how many tiles move and whether they are the ones that should. In level 1 it is 19, 47 and 29 out of the 640 in each room, and they fall on the rows where the creatures are.

On the title screen, though, it does close at zero: all 768 tiles. Nothing moves there.

Tcl traps already paid for in this series

Code shared with other Konami cartridges

This disassembly has been compared, instruction by instruction, with every other Konami one in the series. A block counts when the same instructions, at least six of them from a label to its ret or its unconditional jump, are in both cartridges; the addresses are left aside, because they change from one cartridge to the next. Of the other 57, 52 have at least one block in common with this one; these are the ones that share the most:

Cartridge Blocks Instructions For example (here = there)
Knightmare 31 394 lee_mando_y_teclado (0x4742)
Yie Ar Kung-Fu II 29 319 guion_rle (0x469D)
Hyper Sports 3 24 294 guion_rle (0x469D) = descomprime_a_vram
Konami’s Soccer 24 301 lee_mando_y_teclado (0x4742) = lee_el_mando_1
Konami's Ping Pong 22 235 guion_rle (0x469D) = descomprime
Hyper Rally 21 266 lee_mando_y_teclado (0x4742) = LEE_MANDOS
Yie Ar Kung-Fu 21 208 pinta_guion (0x4682) = dibuja_la_lista
Road Fighter 19 247 lee_mando_y_teclado (0x4742) = lee_el_mando
King's Valley 18 253 toca_la_nota (0xB700) = tono_con_periodo_explicito
Mopi Ranger 18 178 baja_un_paso (0x49DD) = avanza_la_cortinilla

The whole matrix, the 58 cartridges against each other, is on The routines in the series database, along with the method and the program that measures it.