Under the hood

Getting started

To reproduce this disassembly you need Python 3 (with Pillow for the pictures), GNU make and Pasmo. The cartridge image is not in this repository: bring your own.

goemon.rom    131,072 bytes
sha256        a436addc241d977ba38081463322e7e611d6bab4d10cd06305fe5a71f02a549c

With the file at the root of the repository:

make            # listing, verification, consistency and tests

What each step does

command what it does
make comprueba checks the sha256 of the ROM
make reconoce the header and the 49 writes to the mapper, with their bank
make listado builds the sixteen .asm files from the binary, the notes and the seeds
make verify reassembles each bank and the whole ROM, and compares the sha256
make sanity checks that no data is read as code and that no byte is left unassigned
make test the tests
make densidad how many instructions carry a comment, bank by bank
make imagenes draws the PNGs in docs/imagenes/ from the ROM
make coteja checks the pictures against the openMSX dumps in work/
make web builds the HTML pages and checks the links

make verify is the one that decides: at the end it must print OK: la ROM entera reproducible byte a byte.

make coteja needs the openMSX dumps, which are not in the repository. They are made with the launchers in In the emulator; the neighbour one also needs the Q*bert and Game Master images.

Where everything is

The cartridge

   
title Ganbare Goemon! Karakuri Dōchū (Konami, 1987)
number RC-748
machine MSX2 (V9938), SCREEN 5
size 131,072 bytes: 16 banks of 8 KB
mapper Konami without SCC (Konami4)
sound PSG, three channels
sha256 a436addc241d977ba38081463322e7e611d6bab4d10cd06305fe5a71f02a549c

The header is AB with INIT at 0x4097, and the interrupt comes in at 0x4045 through H.TIMI. At 0x4010 there is a second header, CD, which is the one the Game Master looks for.

The mapper

Bank 0 is fixed at 0x4000-0x5FFF. The other three slots are chosen by writing the bank number to 0x6000, 0x8000 and 0xA000. There is not a single write to 0x5000, 0x7000, 0x9000 or 0xB000, the registers of the SCC mapper: that is why it is Konami4.

There are 49 writes to the mapper (tools/reconocimiento.py): 8 to 0x6000, 10 to 0x8000 and 31 to 0xA000. Banks are loaded three at a time by five routines, p00:4206, 4220, 4238, 4250 and 4268 (1-2-3, 4-5-6, 7-8-9, 10-11-12 and 13-14-15). With that, each bank has its slot and the listing splits into sixteen modules, each with its own org:

slot banks
0x4000 (fixed) 0
0x6000 1, 4, 7, 10, 13
0x8000 2, 5, 8, 11, 14
0xA000 3, 6, 9, 12, 15

There is one exception: p01:7DEC puts bank 14 at 0xA000 to read the objects of each cell, and the listing declares it (tools/paginas.py). Of the 31 writes to 0xA000, 21 are single ones: routines that read a table and change only that slot (to bank 6, 9, 12, 14 or 15). Twenty go back to the 1-2-3 trio when they finish. The other one, p01:7DA1, leaves early if the area has no special exits: in 14 areas it leaves bank 9 in place until the next return to the trio, with no visible effect (all 14 match openMSX). The interrupt calls the sound with banks 10, 11 and 12 (p00:404F) and on the way out puts them back from their copies in RAM (0xF0F1-0xF0F3).

What is in each bank

bank code what it holds
0 6,703 B start-up, the interrupt, the game states, the data readers (rle, patterns, letters, HMMC, palettes, labels), building and painting the screen, the status bar; the figures' colour lists
1 7,439 B the game: loading the area, the player and his collisions, the pause, the password, the endings; the Game Master and Q*bert signatures, the demo script
2 6,087 B the figures (creating, moving, their sprites), the interiors and their prices (0x9371), the passages, the end of the stage and its texts
3 7,181 B each figure type, the purchases, the dice, the secret passages, the pause words and the keywords; Konami's mark
4, 5 — the characters of the six graphics sets, in rle
6 — the links of the 49 areas, the keyboard tables, the title picture
7 — the patterns shared by every set and those of the status bar
8 — the sprites of the 20 poses of Goemon and Ebisumaru
9 — the exits of each area, the secret passages (0xA575 and 0xA86D), the palettes, the figure poses, each area's graphics set (0xB8EC) and the money of each type (0xB86D)
10 1,314 B the sound: 28 tunes and 32 effects, and the routine that plays them
11 — figure patterns, the 9 instruments and the end of a score from bank 10
12 — 114 labels (0xA9C0), the figure (0xA830) and pose records, the ending sentences (0xBCD3)
13 — the grids of the 49 areas, the screens and the blocks of each set
14 — which figures appear in each cell (0x9BF0), the objects of each cell (0x981D), more blocks
15 — the fixed objects (0xA08E), the passages of each area, the texts after clearing an area (0xBC16)

In all, 28,724 bytes of code and 102,348 of data: 100 % of the cartridge accounted for (make sanity).

The VRAM

The game runs in SCREEN 5 (p00:4994), with 16 colours out of a palette of 512.

Sprites are mode 2 (16 × 16, colour per line). Their patterns go to 0xF800. The table and the colours are rebuilt in RAM (0xEE00 and 0xEC80) and p01:685F copies them on odd frames. That is why a dump can catch the copy from one frame earlier, and the comparisons allow for it.

The RAM that matters

address what it is
0xC000 / 0xC001 the game state and its step
0xC002 game flags (bit 7 player 2, bit 6 playing)
0xC00B / 0xC00C F1-F3 just pressed
0xC257 the score (BCD)
0xC260 the lives
0xC265-0xC266 the money, in BCD
0xC270-0xC27D the status-bar items
0xC27F continue allowed
0xC280 / 0xC281 / 0xC288 the area, the cell and the stage
0xC289 the area's graphics set
0xC480 / 0xC481 maximum life and life
0xC4B0 the timer (BCD)
0xC600 the 8 figures, 0x80 bytes each
0xCDB1 inside a secret passage
0xEF00 0xFF if the neighbour is there
0xEF80 the secrets: bits 0-1 the words, 2-5 the keywords, 6 the menu

The code

Everything runs in the interrupt

INIT (0x4097) clears the game RAM (0xC000-0xF0EF), looks for the neighbour in the other slots (p01:7EAB), switches to SCREEN 5 and hooks H.TIMI. From then on the main program sits in a jr to itself (p00:40E5), and the whole game runs in p00:4045, once per frame:

  1. If the neighbour is there, first the STOP pause (p00:40E7, 0xEF01).
  2. The sound, with banks 10, 11 and 12 in place (p10:6000). Then the game's banks come back from 0xF0F1-0xF0F3.
  3. If the previous frame has finished (the semaphore at 0xC005), the state machine (p00:5DBB). Interrupts are enabled before it, so the sound does not wait for the game.

The dispatcher

p00:408D is Konami's dispatcher: the table of destinations sits right after the call. It pops the return address, which is the base of the table, and jumps to destination number A. The listing marks each of those tables as data and names their destinations.

The game states

0xC000 is the state and 0xC001 its step (table at 0x5DCF):

state what it is
0 the Konami logo and the title
1 the title menu
2 the demo
3 the game starts
4 entering the area
5 playing
6 a life is lost
7 out of lives: continue or game over
8 area cleared: time to points and the next area
9 leaving the cell
0x0A the pause
0x0B stage cleared
0x0C the neighbour's menu
0x0D F2 in the pause: the password
0x0E the password screen
0x0F the end of the game

In states 0 to 2, any key leads to the title (p01:62FE).

The player

p01:6D38 runs the player for one frame: it reads the controls and dispatches on his state (0xC490, table at 0x6D4C), where 0 is on the ground, 1 jumping and 2 dying. The controls go to 0xC006/0xC007 and F1-F3 to 0xC00B/0xC00C (p00:49D2). p01:781F says whether the player can stand somewhere: it returns carry if he cannot.

One cell

On entering a cell, p01:66F6 does, in order:

  1. its links and its special exits;
  2. clears the figures;
  3. builds the screen at 0xD800 (p00:51ED) and paints it (p00:534E);
  4. on top, the fixed objects (p02:90C0);
  5. puts the player at the entry point;
  6. uploads the patterns of the cell's figures (p00:5413);
  7. paints the status bar and sets the colours.

The figures

There are 8 figures at 0xC600, 0x80 bytes each: type (ix+0), step (ix+1), y (ix+3), x (ix+5), pose (ix+0x0A)… p02:8334 creates one in the first free slot. Each type has two entries in its tables: the start-up one (p02:8427) and the per-frame one (p02:871C), which in turn dispatches on the figure's step. Its sprites come from the pose (0xB6D2) and its colours from the lists at 0x554E.

The secret passages

p00:5969 builds the area's passage at 0xD800, one byte per cell and 28 per row, from a grid of bits (1 wall, 0 path) with some marks on top (p00:5A10). While inside, 0xCDB1 is not zero, and each frame goes to p02:9881: walking (p03:B8C3), picking up whatever is there (p03:BC5C), the map (p03:BAF1) or the exit (p03:BD06).

The sound

It all lives in bank 10, and the interrupt calls it every frame. There are 28 tunes (0x6592, six bytes each: one pointer per PSG channel) and 32 effects (0x6550). p10:61F1 reads the scores: notes, rests, tempo, octave, jumps and commands 0xD0-0xFF (p10:6341). The pitch of each note comes from the lowest octave (0x6303). The 9 instruments and the end of one score are in bank 11. There is a half-made tune 0x9C, whose first pointer is the silence, and right behind it effect 0x20 already begins (0x663A).

How the listing is made

Density: 42.5% of the 14,355 instructions carry a comment, and all 1,678 routines are above 10 % (make densidad).

In the emulator

Everything drawn on this website is checked against openMSX with a Japanese MSX2 machine (C-BIOS_MSX2_JP). The probes are Tcl scripts that take the cartridge wherever it needs to be, by poking RAM or pressing real keys, and dump the whole VRAM (128 KB), the palette and the RAM. One emulator runs at a time.

The probes

probe what it does launcher dumps
omsx_konami.tcl the Konami logo, when its sweep ends lanza_konami.sh 1
omsx_vuelca.tcl the demo, at each change of cell or state lanza_vuelca.sh —
omsx_zonas.tcl the entrance of each area lanza_zonas.sh 49
omsx_interiores.tcl the 21 interiors, closed and open lanza_interiores.sh 21 + 8
omsx_vecino.tcl the title with Q*bert and with the Game Master in slot B lanza_vecino.sh 2
omsx_claves.tcl the four password keywords lanza_claves.sh 4
omsx_secretos.tcl the menu, the pause words, F2, F5 and the money on dying lanza_secretos.sh 1 game
omsx_pasadizos.tcl the 42 secret passages, the view and the map lanza_pasadizos.sh 84
omsx_entrada_pasadizo.tcl entering the passage by paying in interior 16 lanza_entrada_pasadizo.sh 1

To force an area, omsx_zonas.tcl puts the stage in 0xC288, the area in 0xC280 and 0xC268 to 0, and goes back to state 4, step 1: the game loads the area as if it had been reached on foot.

The comparisons

make coteja compares what is drawn from the ROM with the dumps. Current result:

test what it compares result
coteja.py the 49 area entrances: 0xD800, the screen, the characters on page 1 and the palette 49 dumps, 0 different
titulo.py the logo, the title and the neighbour's menu 0 pixels different
figuras.py Goemon's pose in each dump: patterns, sprites and colours 49 dumps, 0 different
enemigos.py each figure in the dumps and the lists of each cell 107 figures and 49 lists, 0 different
interiores.py the 21 interiors, the whole of page 0 0 bytes different
pasadizos.py the 42 passages: the grid at 0xD800, the map and the palette 42, 0 different

Tricks to see it

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, 43 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)
Vampire Killer 90 1,495 hmmc (p00:47B2)
King Kong 2: Yomigaeru Densetsu 65 970 lmmm (p00:4803)
The Treasure of Usas 62 987 hmmc (p00:47B2)
Hinotori 55 878 lmmm (p00:4803)
Metal Gear 38 638 lmmm (p00:4803) = copia_logica
The Pro Yakyuu Gekitotsu Pennant Race 36 547 hmmc (p00:47B2) = rectangulo_desde_ram
Konami’s Game Master 2 30 500 hmmc (p00:47B2) = vdp_hmmc
The Maze of Galious 23 312 cuadro_del_efecto (p10:6454) = paso_de_envolvente
Hai no Majutsushi - Mahjong 2 23 241 rle_con_destino (p00:4533) = vuelca_lista_vram
Gekitotsu Pennant Race 2 18 254 hmmv (p00:4732) = borra_bloque_vram

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.