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 does not travel in this repository: bring your own.

kingkong2.rom    131,072 bytes
sha256           9c42a0b4d4219176e0e894c434deb0b479cde3ce9edbd9e99395a8baae6c01d6

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 semillas traces the whole cartridge and gets the entry points of each 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 not a single byte is left unassigned to code or data
make test the tests
make densidad how many instructions carry a comment, routine by routine
make web builds the HTML pages and checks the links

make verify is the one that decides: it has to end with OK: la ROM entera reproducible byte a byte.

The pictures in docs/imagenes/ are drawn from the ROM by the tools in tools/: titulo.py, mapa.py and marcas.py, figuras.py, jefes.py and objetos.py. coteja.py compares them with the openMSX dumps, which do not travel in the repository: they are made with the launchers in In the emulator.

Where everything is

The cartridge

   
title King Kong 2: Yomigaeru Densetsu (キングコング2 甦る伝説, Konami, 1986)
number RC-745
machine MSX2 (V9938), SCREEN 5
size 131,072 bytes: 16 banks of 8 KB
mapper Konami without SCC (Konami4)
sound PSG, three channels
sha256 9c42a0b4d4219176e0e894c434deb0b479cde3ce9edbd9e99395a8baae6c01d6

It is MSX2 even if it does not look like it at first: p00:46DD waits for the CE bit of status register 2 and p00:4706 issues VDP commands (LINE, HMMV, HMMM, LMMM, PSET). On an MSX1 machine it keeps waiting in that loop.

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, and here always three at a time: p00:42C6 has five gates (1-2-3, 4-5-6, 7-8-9, 10-11-12 and 13-14-15). That is why every bank always goes to the same slot, which is decided by the remainder of its number divided by three:

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

The copies of what is mapped live at 0xF0F1-0xF0F3, and 0xF0F6 keeps a reserve bank for 0xA000: 76 bridges in bank 0 are call X / jp 0x4215, and 0x4215 puts that bank back when done.

What each bank holds

Nine banks carry code; seven are data only.

bank what it holds
0 start-up, the interrupt, the VDP commands, how a screen is built, the sprites, the enemy records, the bosses uploaded raw
1 the game states, keyboard and joystick, Mitchel, the items, each area's music, who talks
2 the conversation engine, the changing scenery, the figure that follows Mitchel and half of the enemies
3 the other half of the enemies, what they throw and what they drop when they die
4 the sound player and its tracks; the planes of the split screens
5 the enemies' sprite patterns (RLE) and the letter font (p05:9F99)
6 the arrival on the island, the three endings, the title, the passages and the alternate planes
7, 8 which tile set each screen uses, the palettes, the blocks of 4 × 4 tiles and the item icons (p07:630A)
9 hits and damage, experience, each screen's neighbours (p09:B1C5) and the scenery that can be removed
10, 11 the outdoor map (25 blocks per screen, p10:6000) and 8 × 8 tiles
12 what each conversation line does, their texts and the item window
13 Mitchel's poses, the enemies of each screen and the graphics of the sprite bosses
14 the figures of the bosses painted by the VDP
15 the names of items and enemies, the scenery that dissolves dot by dot

The VRAM

SCREEN 5, 256 × 212 in 16 colours. Page 0 is the one on view; page 1 is the store:

The RAM that matters

address what it is
0xC100 / 0xC101 the game state and its step
0xC125 the screen
0xC131 the plane (0, outdoors; on split screens, the alternate one)
0xC127 DAYS
0xC129 LIFE
0xC12B EXP
0xC12E LEVEL
0xC12F MP
0xC135 GOLD
0xC140 / 0xC141 SHOT1 (the weapon) and SHOT2 (the item)
0xC16E how many times you have continued
0xC300-0xC306 Mitchel: state, y, x, direction
0xC400 the items on the ground (4 records)
0xC500 the scenery that can be removed (3 records)
0xC600 the figure that follows Mitchel
0xC800 the thing that can be pushed
0xD000 the 13 enemy records, 0x80 bytes each
0xDE00, 0xDF00 the dead bosses and the dead enemies around

The code

Everything runs in the interrupt

The cartridge hooks H.TIMI to p00:4229, which runs once per frame: first the sound, with banks 4 and 5 mapped (p04:6228), and then the game (p01:65C2): it bumps the frame counter (0xC103) and branches on the state.

The dispatcher

p00:425F is Konami's dispatcher: the table of destinations is glued right after the call. It pops the return address, which is the base of the table, and jumps to destination number A. p00:4260 does the same with the table in HL. The listing marks each of those tables as data and names their destinations.

The game states

0xC100 is the state and 0xC101 its step (table at p01:65D6): the Konami logo being uncovered, the demo, the title, the game, GAME OVER with its CONTINUE (F5), the pause and the ending (p06:A6CF). The game itself (p00:5C05) branches again on 0xC201: playing, changing screen, in a conversation, the durian…

A screen

On entering a screen (0xC125), p00:4A7B chooses its tile set (a nibble per screen at p07:6009, or per plane at p07:6000) and uploads to the page 1 sheet what its list says. The map is 5 × 5 blocks of 4 × 4 tiles (p10:6000, or p04:7E76 on the alternate planes); p00:4EE7 copies the 20 × 20 tiles to page 0 with HMMM from (8, 8). Then the palette, the screen's sprites (p00:4CB3), the enemies (the list at p13:6000) and what is painted on top: the statue, the bosses, the scenery that can be removed.

Twelve screens are split: p06:BEC5 shows one plane or the other depending on which side Mitchel is on (0xC131).

Mitchel

His state is 0xC300 (table at p01:6D3C): 0 still, 1-4 walking up, down, left and right, 5 the hit, 6 spinning, 7 throwing. He walks from one 8-dot tile to the next (p01:6F17); in the water of tile set 4, without the clogs, at half step (p01:6F00). He is two overlaid 16 × 16 sprites; his pose comes every frame from an RLE at p13:7D9B.

The enemies

Thirteen records of 0x80 bytes at 0xD000. The type goes in ix+0: p01:6885 says which routine runs every frame and p00:5B1E what it does when created. Nearly all of them go by steps (ix+1) through the dispatcher. From ix+0x12 on, each type uses the record its own way; tools/anota.py knows what each field is in each enemy's code and comments it.

What gets thrown (stones, balls, the boomerang) are other records, the figures 0x81-0x91 (p01:7077). On dying, p03:B9E6 decides what each type drops, with its own count at 0xDFC0-0xDFDB.

The sound

p04:6032 receives the sound: below 0x80, an effect, which uses the PSG's channel C and takes it from the music while it plays; from 0x80 to 0x8D, one of the 14 pieces, with one track per channel (p04:66D8). 0xFD pauses (and plays a little tune), 0xFE brings the music back and 0xFF fades it out. The tracks are bytes of note, rest, tempo, octave, volume, envelope, jumps, calls and repeats (p04:638A).

How the listing is made

tools/bancos.py traces the whole cartridge following the bank switches, the dispatcher and the bridges. tools/mkasm.py writes each bank with its org, its labels and its comments, taken from src/pNN.notes. 100% of the 131,072 bytes is assigned to code (37,983) or data (93,089), and 40.1% of the 18,093 instructions carry a comment, with no routine below 10%.

In the emulator

What is drawn on this website is checked against openMSX with a Japanese MSX2 machine (C-BIOS_MSX2_JP, and -romtype Konami after the -cart). The probes are Tcl scripts that put the cartridge where it is needed by touching the RAM and dump the VRAM, the RAM, the palette and the VDP registers. They are run one at a time.

The probes

probe what it does launcher
omsx_vuelca.tcl dumps at the given seconds: the Konami logo and the title lanza_vuelca.sh
omsx_pantallas.tcl puts each screen in 0xC125 with 0xC201 = 1 and dumps the VRAM lanza_pantallas.sh
omsx_salidas.tcl leaves Mitchel against an edge and notes which screen he reaches lanza_salidas.sh
omsx_enemigos.tcl the 13 records, sprites and colours, sample by sample, screen by screen lanza_enemigos.sh
omsx_lecturas.tcl who reads each byte of the cartridge, with which bank mapped lanza_lecturas.sh
omsx_truco.tcl the level trick: TAB and an F key at the start lanza_truco.sh
KK_OUT=work/truco KK_FILA=7 KK_MASCARA=0x01 \
openmsx -machine C-BIOS_MSX2_JP -cart kingkong2.rom -romtype Konami \
        -script tools/omsx_truco.tcl

The checks

tools/coteja.py compares what is drawn from the ROM with the dumps. Current result:

check what it compares result
titulo the Konami logo (170 × 56 of page 1) and the title (the whole of page 0), with the 16 colours 0 dots different
pantallas the 160 × 160 dots of each screen and its palette 94 of 140 identical; in nearly all the others the difference is what the game paints on top of the map (items, bosses, the boat); on eight a palette colour changes, and on four (0x48, 0x4A, 0x5F and 0x6F) almost nothing matches and it is still to be looked at
objetos the 38 icons that p00:4AED uploads to 0xB800 222 dumps: 182 identical and 40 with the same box of page 1 overwritten by something else (see Open questions)
tools/jefes.py what p00:4BD0 uploads on the seven boss screens 0 bytes different
exits the neighbours at p09:B1C5, walking Mitchel out through the edges 9 out of 9
trick LEVEL with TAB+F1 and TAB+F4 10 and 40, as p01:67F3 says

Tips for seeing 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, 40 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 68 1,003 lmmm (p00:481B) = copia_con_operacion
Ganbare Goemon! Karakuri Dōchū 65 970 lmmm (p00:481B)
Metal Gear 54 853 lmmm (p00:481B) = copia_logica
The Treasure of Usas 52 752 lmmm (p00:481B) = copia_con_operacion
Hinotori 43 678 lmmm (p00:481B)
The Pro Yakyuu Gekitotsu Pennant Race 30 460 lmmm (p00:481B) = copia_logica
Konami’s Game Master 2 26 429 lmmm (p00:481B) = vdp_lmmm
Gekitotsu Pennant Race 2 21 282 hmmv (p00:479B) = borra_bloque_vram
Hai no Majutsushi - Mahjong 2 18 202 rle_con_destino (p00:45E9) = vuelca_lista_vram
The Maze of Galious 17 197 el_sonido_desde_cero (p04:6000) = sonido_a_cero

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.