Under the hood

Getting started

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

hinotori.rom     131,072 bytes
sha256           d4d443c18203f1a463b4d0b356a95c5dc578e24e47e5ea596c773b63ad20fba0

With the file in the root of the repository:

make            # listing, verification, consistency and tests

What each step does

target what it does
make comprueba checks the ROM's sha256
make reconoce measures the header, the mapper and the bank → address rule
make semillas traces the whole cartridge and gets the entry points of each bank
make listado generates 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 one byte is left unassigned to code or data
make test the tests
make densidad how many instructions carry a comment, routine by routine
make imagenes draws the pictures of the web from the ROM
make coteja compares them with the openMSX dumps
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.

Where everything is

The cartridge

Hinotori: Hōōhen – Gaō no Bōken (火の鳥 鳳凰編 我王の冒険), by Konami, 1987, catalogue number RC-747. It is for the MSX2: the first thing it does (p00:4D4D) is wait for the V9938 to finish a command by reading the CE bit of status register 2, and on an MSX1 it stays there.

The mapper

It is 128 KB in sixteen 8 KB banks, with Konami's mapper without SCC (Konami4): bank 0 is fixed at 0x4000 and the other three are chosen by writing to 0x6000, 0x8000 and 0xA000. tools/reconocimiento.py measures it on the ROM: not one write to the registers of the SCC mapper (0x5000, 0x7000, 0x9000, 0xB000).

As in King Kong 2, every bank always goes to the same slot, and it is the remainder of dividing by three: banks 1, 4, 7, 10 and 13 at 0x6000; 2, 5, 8, 11 and 14 at 0x8000; 3, 6, 9, 12 and 15 at 0xA000. They are switched three at a time by p00:5408 (A, A+1 and A+2, with a copy at 0xF0F1–0xF0F3), which has five doors (5420, 5405, 5425, 542A, 542F); p00:53E9 puts 1, 2 and at 0xA000 whatever 0xC10C says (3 nearly always, 6 or 9 for a moment), and p00:5434 / 543D put a single one.

banks what is there
0 the framework: start-up, interrupt, states, VDP, sheet, map, texts
1, 2, 3 Gao, the enemies (58 types), the things on the way, the shots
4, 5 the tile lists of every stage and their sources; the palettes
6 Gao's sprites, the pause, the passwords, the ending
7, 8 the sprites of what appears in each area; 18 strips nobody uses
9 what appears in each area, the 18 gates, saving and loading King Kong 2
10, 11, 12 blocks, superrows and maps of the 24 areas
13 letters and the title
14, 15 the sound player and its 120 tracks

Start-up

INIT (0x40B8) clears the RAM, looks at the other slots (p00:5DFF), puts the interrupt on H.TIMI (jp 0x4048) and stays on jr $: the whole game runs from the interrupt. Every frame, the interrupt plays the sound with banks 14 and 15 in, puts them back and calls the game (p00:4200), which branches on the state 0xC100.

If there is a King Kong 2 in another slot, Hinotori does not start: King Kong 2 starts, with a hook of Hinotori's (see Findings).

What belongs to nobody

After the AB and INIT, the header carries a second header at 0x4010: 'C', 'D' and a list of addresses in this game's RAM (the stage at 0xC161, the lives at 0xC160…). No code in this cartridge reads it: it is for another cartridge placed next to it. King Kong 2 carries its own in the same place, and that is exactly what Hinotori looks for in it.

The code

How the listing was made

A MegaROM cannot be traced bank by bank: address 0x8123 says nothing unless you know which bank is in. tools/bancos.py traces the whole cartridge keeping track of the three slots and their copies in RAM (0xF0F1–0xF0F5 and 0xC10C), and models the routines that switch banks. That gives the entry points of each bank (src/pNN.entries) and the dispatcher tables (src/pNN.nocode). Then tools/z80trace.py traces each bank with those entries and tools/mkasm.py writes the listing.

Two measured rules the tracer needs:

make verify reassembles the sixteen banks with Pasmo and the ROM comes out byte for byte. make sanity checks that not one byte is left unassigned: every data range carries its explanation, and most of them come from tools/bloques.py walking through the format as the routine that reads it does (tile lists, palettes, maps, superrows, blocks, sound tracks, enemy records, what each area holds, the gates).

The framework (bank 0)

address what it does
0x40B8 INIT: RAM to zero, other cartridges, hook, jr $
0x4048 the interrupt: sound (banks 14 and 15) and one game frame
0x4200 the frame: keys and branching on the state 0xC100
0x40AE the dispatcher: jumps to entry A of the table stuck behind the call
0x5408 banks A, A+1 and A+2
0x54CC the tile lists to the sheet (eight formats)
0x5900, 0x59E5 the map: superrows, blocks and the 32 × 32 screen table
0x57B8 scrolling: one more 8-pixel row and the map commands
0x4B7A the 32 sprites, in two alternating halves
0x4E0B–0x4E8B the V9938's HMMV, HMMM and LMMM commands
0x4F87, 0x4FBE texts: [x][y] and letters, 0xFE another place, 0xFF ends
0x5DFF looks for King Kong 2, Q*bert or the Game Master in the other slots

The game states (0xC100): 1 the title, 2 the demo, 4 the area starts, 5 playing, 6 a life is lost, 9 the menu, 0x0A the pause and the passwords, 0x0C the ending, 0x0D the POWER UP window (F2), 0x0E and 0x0F the maps (F4 and F5), 0x11 ITEM INFORMATION (F3).

The enemies and the things

The enemy records are 0x80 bytes long (six at 0xD000); ix+0 is the type, ix+1 the step (every type branches on it through the dispatcher), ix+3 and ix+5 y and x, ix+0x70–0x73 the collision box. The common routines in p01 (getting close to Gao, copying pieces of a record, counting frames) are used by all; the rest belongs to each type, in banks 2 and 3.

The numbers

Measured with make sanity and make densidad: the whole cartridge assigned, code and data, and the percentage of instructions with a line comment. The comments that come from tables (the RAM, the record fields, the data each instruction points at) are put in by tools/anota.py; the names of the routines nobody had christened come from what they do.

In the emulator

Everything checked by playing was done in openMSX with the C-BIOS_MSX2_JP machine and -romtype Konami, one emulator at a time, with the probes in tools/. The dumps go to work/, which does not travel in the repository: they are made again with these commands.

probe what it does
lanza_vuelca.sh <dir> "<seconds>" ["<spaces>"] dumps VRAM, palette, RAM and registers at those moments
lanza_bp.sh <dir> <address> [time] dumps when the Z80 goes through there
lanza_guion.sh <dir> "<script>" waits for the game (state 5) and runs a script: keys, text, dumps
lanza_lecturas.sh who reads each byte of the cartridge (a read watchpoint)

The three that take extra arguments pass them on to openMSX: that is how another cartridge goes into slot B (-cartb kingkong2.rom -romtype Konami).

What has been checked

what how result
the logo lanza_bp.sh work/logo 0x66F9 0 bytes different
the title lanza_vuelca.sh work/v4 "23" 0 bytes and the 16 colours
the menu with Q*bert lanza_bp.sh work/menu_qbert 0x4735 120 "…" 120 -cartb qbert.rom 0 bytes
the sheet and the palette lanza_bp.sh work/area_1 0x5D6E 1 and 2 areas 0 and 1: 0 tiles and 0 colours different
the map on screen a dump of the demo and work/area_1, area_2 the screen rows, consecutive and in order in the map (25/25, 31/31, 31/31)
the room bosses lanza_guion.sh work/salaNN with tools/guion_salaNN.txt, one boot per room (the gate to 0xC486 and 0xC485 = 0x80) each room, its whole boss in the VRAM; none of the unloaded figures
the passwords lanza_guion.sh with F1, HOME, HOME, the text and RETURN 15 with their change in RAM
King Kong 2 next to it lanza_guion.sh … -cartb kingkong2.rom King Kong 2 boots; F4 → SAVE MODE

make coteja repeats the checks with whatever dumps there are in work/.

Worth knowing

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, 41 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,051 lmmm (p00:4E8B) = copia_con_operacion
Ganbare Goemon! Karakuri Dōchū 55 878 lmmm (p00:4E8B)
Metal Gear 54 886 lmmm (p00:4E8B) = copia_logica
King Kong 2: Yomigaeru Densetsu 43 678 lmmm (p00:4E8B)
The Treasure of Usas 41 682 lmmm (p00:4E8B) = copia_con_operacion
The Pro Yakyuu Gekitotsu Pennant Race 36 501 lmmm (p00:4E8B) = copia_logica
Konami’s Game Master 2 28 451 lmmm (p00:4E8B) = vdp_lmmm
Gekitotsu Pennant Race 2 25 311 hmmv (p00:4E0B) = borra_bloque_vram
Hai no Majutsushi - Mahjong 2 20 204 rle_con_destino (p00:4A87) = vuelca_lista_vram
Q*bert 14 163 pon_mezclador_del_canal (p14:993D) = 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.