Under the hood

Getting started

This repository does not contain the game. It contains the tools that, from your own image of the cartridge, rebuild the commented listing, reassemble it and check that the ROM comes out byte for byte.

What you need

What to run

make comprueba     # the ROM's sha256
make               # trace, generate the listing, reassemble, run the tests
make densidad      # how many instructions carry a comment, bank by bank
make sanity        # the budget: not one byte unassigned

make verify reassembles the sixteen banks and compares them with the ROM: it must say OK: la ROM entera reproducible byte a byte.

Where things are

The pictures

python tools/dibuja.py mapas work/dibujos --numeros --actores
python tools/dibuja_sprites.py tipos work/dibujos/sprites --escala 3
python tools/dibuja_sprites.py snake work/dibujos/snake.png
python tools/dibuja.py transceptor work/dibujos/transceptor.png

The cartridge

The machine

MSX2: the VDP is a V9938 and the game works in SCREEN 5 (256 × 212, 16 colours, CHGMOD at p01:7C6C). VRAM page 0 is the screen and page 1 holds the tiles, the item drawings, the font and the backgrounds that get covered; almost everything is painted by copying rectangles from page 1 to page 0 with the VDP commands (p00:4FCD HMMM, p00:5011 LMMM with TIMP, p00:4F91 HMMV).

The mapper

A 128 KB MegaROM with the Konami mapper without SCC: 16 banks of 8 KB. Bank 0 is fixed at 0x4000; the other three are chosen by writing to 0x6000, 0x8000 and 0xA000. Bank 3k+1 goes to 0x6000, 3k+2 to 0x8000 and 3k+3 to 0xA000, and they are set in trios (p00:4248 1-2-3, p00:424E 4-5-6, p00:426A 7-8-9, p00:4270 10-11-12, p00:4276 13-14-15), with their copy at 0xF0F1-0xF0F3.

The code in banks 6, 9, 12 and 15 runs at 0xA000 with 1 and 2 below: these are the zone banks (p00:4913-4925), noted at 0xF0F6. To call a routine that switches banks and come back with their own in place they use the trampolines at p00:4050-4195: each one calls the routine and ends at p00:4198, which sets 1-2-3 and puts the bank in 0xF0F6 back at 0xA000.

The interrupt

p00:41AC hangs from H.TIMI. Every frame it sets banks 4 and 5 and calls the sound driver (bank 4, 0x6000); then, if the previous frame has finished (0xC005), it calls the frame's game (p01:6672).

Sound

Four channels of 0x20 bytes from 0xC010 over the PSG (p04:6000). There are 95 sounds (index p04:6892): below 0x29 they are effects on the fourth channel, from 0x29 to 0x5B three-channel music and from 0x5C on four-channel (p04:67FD). The lists have two formats and five instruments of 12 notes (p04:652B).

Sprites

V9938 sprite mode 2, 16 × 16. There are two attribute tables (0xF600 and 0xF200) swapped every frame with R#5 (p15:B8BC), and the order in which the sprite groups are written changes every frame (p15:B8DE), so that with more than eight on a line it is not always the same one that disappears.

The RAM

Address What it is
0xC000 / 0xC001 the game state and its step
0xC002 bit 6: being played (the demo makes no sound)
0xC006 / 0xC007 the new keys and the ones held
0xC112 / 0xC113 the new function keys and the ones held
0xC130 the room
0xC131 / 0xC137 the life and its maximum
0xC132 the CLASS (0-3)
0xC134 / 0xC135 the selected weapon and item
0xC138 the transceiver frequency (BCD)
0xC151 the game mode
0xC180 Snake's state
0xC181-0xC184 Snake's Y and X (16 bits)
0xC200 the 6 projectiles (0x40-byte records)
0xC500 / 0xC530 the weapons and items carried
0xCA00 the saved game (0x300 bytes)
0xD000 the 16 actors (0x80-byte records)
0xE000 the built room (32 × 24 tiles)
0xEA00 the copy of the sprite attributes

The code

The numbers

How it is organised

The frame's game (p01:6672) dispatches on the game state (0xC000): the logo and the title, the wait, the demo, PLAY START, the new game, the game, GAME OVER, the pause and the ending.

Inside the game (p01:6F18) the mode rules (0xC151, table p01:6F7A): play, room change, the weapons and equipment windows, the transceiver, the lorry, the lift, opening a door, the telescope, Snake dead, the text window, the capture and room 0x85.

Snake has his own state (0xC180, table p01:6001) and his pose mode (0xC18B, table p01:6508).

The records

IX points to three kinds of record, and tools/anotaciones.py comments their fields wherever IX is surely one of them:

Actors

Each type has two handlers: creation (p00:575F, table p00:5762) and every frame (p01:722C, table p01:722F, from the loop at p01:71E5). In the listing they are called crea_tipo_N and cuadro_tipo_N. The frame's collisions are at p02:88EF: projectiles against actors, Snake against actors, the tiles and rooms that hurt, doors and items.

Texts

159 texts (p12:A37D) with 92 shared pieces (p12:B8A7): a byte from 0xA1 on inserts a piece. The window works in steps (p00:5D03) and writes a letter every 4 frames with its sound.

The names

The names and the automatic comments come from tools/anotaciones.py, each with the routine where it is read; tools/anota.py writes them into the notes. The per-type handlers are named on their own from the two dispatch tables, and the bank 0 trampolines after the routine they call.

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, 38 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)
Hinotori 54 886 copia_logica (p00:5011) = lmmm
King Kong 2: Yomigaeru Densetsu 54 853 copia_logica (p00:5011) = lmmm
Vampire Killer 40 672 copia_logica (p00:5011) = copia_con_operacion
Ganbare Goemon! Karakuri Dōchū 38 638 copia_logica (p00:5011) = lmmm
The Treasure of Usas 35 572 copia_logica (p00:5011) = copia_con_operacion
The Pro Yakyuu Gekitotsu Pennant Race 29 442 copia_logica (p00:5011)
Konami’s Game Master 2 26 428 copia_logica (p00:5011) = vdp_lmmm
Hai no Majutsushi - Mahjong 2 21 221 patrones_con_destino (p00:4DCF) = vuelca_lista_vram
Gekitotsu Pennant Race 2 18 259 rellena (p00:4F91) = borra_bloque_vram
Q*bert 16 237 mezclador (p04:64F2) = pon_el_mezclador

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.