Under the hood

Getting started

To reproduce this disassembly you need Python 3, GNU make and Pasmo. The cartridge image does not travel in this repository: everyone supplies their own.

synth.rom    32,768 bytes
sha256       a2eea29f13662acea8d0cbe2b4e95201a29e82e0c3c38dd24e910e9325a90a0d

With the file in the root of the repository:

make            # listing, verification, consistency and tests

What each step does

command what it does
make comprueba checks the ROM's sha256
make listado traces the flow and generates src/synth.asm from the binary and the notes
make verify reassembles the listing and compares its sha256 with the ROM's
make sanity checks that no data comes out as code and that not a byte is left unassigned
make test the tests
make densidad how many instructions carry a comment, routine by routine
make imagenes draws the website's pictures from the ROM
make coteja checks those screens against the openMSX dumps
make web generates the HTML pages and checks the links

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

Where everything is

file what it is
src/synth.notes the names, the comments and the data blocks, anchored to addresses
src/synth.entries the entry points the trace cannot deduce, each with its reason
src/synth.nocode the areas that are not code even if the trace could enter them
src/synth.asm the commented listing, generated
tools/z80trace.py, tools/mkasm.py the tracer and the listing generator
tools/flujos.py the RLE the cartridge writes the VRAM with
tools/motor.py SYN written anew: the synthesizer, sample by sample
tools/sonido.py PLAY: the tones played the way the cartridge plays them, as WAV
tools/timbres.py the table of the sixteen factory tones
tools/pantallas.py, tools/imagenes.py build every screen from the tables and draw it
tools/coteja_todo.py, tools/coteja_dac.py the check against the openMSX dumps and against what reaches the DAC
tools/omsx_*.tcl, tools/lanza_*.sh the openMSX probes: see In the emulator

The pages: The program, The cartridge, The code, Findings, In the emulator and Open questions.

The cartridge

32 KB with no mapper, in pages 1 and 2 (0x4000-0xBFFF), and the digital-to-analogue converter: the DAC is any write to 0x4000, 8 bits unsigned with rest at 0x80. The cartridge has no RAM: the sample lives in the computer's RAM, at 0x8000-0xBFFF, and the program switches that page between the RAM (0x4105) and the cartridge (0x410D) with ENASLT, depending on whether it has to play or to draw.

Start-up

0x4000: "AB" and INIT at 0x4054. INIT notes the slots of pages 1 and 2 (0x40D4) and copies to H.STKE an rst 0x30 to 0x406E: the program starts when BASIC has finished starting. Then it clears the variables, sets H.KEYI and, before letting interrupts in, fills the sample with silence.

The first interrupt routine (0x4118) does nothing but raise the DAC from 0 to 0x80, one step per interrupt, 129 times; then it hands the hook to the per-frame routine (0x4015). That way the speaker does not give the power-on thump.

The ROM map

from to what
0x4012 0x4D04 start-up, the intro, the screens, the keyboard, the PSG effects
0x4D04 0x55E8 FILE: the tape, the disk code and the printer
0x55E8 0x5D3A SYN: the synthesizer
0x5D3A 0x7135 PLAY: the 39 routines that play the sample
0x7200 0x8000 the fourteen oscillator waves, 256 bytes each
0x8000 0xA3AA the graphics of every screen, in RLE
0xA3AA 0xA5AA the sixteen factory tones, 32 bytes each
0xA5AA 0xBB6D TONE EDIT, WAVE EDIT, the copy, the list and FILE
0xBB6D 0xBEA6 LAYOUT and the intro's demo
0xBEA6 0xBF62 the PSG effects and their lists
0xBF62 0xC000 RC741 86 5 31, padding and Konami's hidden mark

The whole split, byte by byte, is in src/synth.notes: the code is 16,229 bytes and the data 16,539, and none is left unexplained.

The RAM

address what
0x8000-0xBFFF the sample: 16 KB of 8-bit samples, silence at 0x80
0xC000-0xCFFF the sixteen user waves, 256 bytes each
0xD000-0xD1FF the sixteen tones, 32 bytes each
0xD200-0xD22F the LAYOUT: the note of each of the 48 keys
0xD230-0xD2AF per key, the PLAY routine and its delay
0xD800-0xDEFF the variables: the phase, the keys, the screen, the sprite copy
0xD854 / 0xD855 the slots of the RAM and of the cartridge in page 2
0xDB00 the current tone
0xDC00-0xDC18 the tone SYN is synthesizing
0xDC19 SYN's write pointer
0xDCFD / 0xDCFE the loop page and the length of the sample
0xDD00-0xDDFF the user wave prepared for listening
0xDF00 the stack, downwards

The VRAM

SCREEN 2, with the registers at 0x4295: colours at 0x0000, patterns at 0x2000, names at 0x3800, sprite attributes at 0x3B00 and sprite patterns at 0x1800. Tiles 0 to 15 are flat squares of the sixteen colours, the font runs from 0x10 (the digits) and from 0x21 (the letters), and every screen brings its patterns, colours and names in RLE from page 2 of the cartridge (0x4358): a control byte below 0x80 repeats the next one, 0x80 + n copies n bytes, 0x80 brings another address and 0 ends.

The PSG

Only for the effects: three channels (0xD810, five bytes each) with note lists at 0xBEFE. The cursor makes effect 1, a finished operation 2, a screen change 5 and the intro 8 (0xBEBF).

The code

8,637 instructions in 1,187 routines, all with a name or a comment; 3,767 instructions carry a comment, 43.6%. The listing is generated from src/synth.notes and reassembles the ROM byte for byte.

Every frame

H.KEYI (0x4015) drives the program: the PSG effects, the keys (0x4817: the first one pressed as row × 8 + bit + 1, with repeat after 40 frames) and the screen's phase. In the intro, 0xD800 is the phase and 0xD801 the step, and every phase is a chain of djnz; in the program, 0xD8F4 is the screen and 0x4805 the table of the nine. SYN and PLAY disable interrupts and enable them when they finish.

SYN, the synthesizer

0x55F0 prepares and 0x5767 takes one turn per sample: the LFO, the three envelopes, the oscillator, the filter, the amplitude, and the byte to RAM.

tools/motor.py is this code written anew in Python, with its 8- and 16-bit arithmetic and its roundings; tests/test_motor.py checks that the sixteen tones come out byte for byte the same as what the Z80 writes in openMSX.

PLAY, the pitch

Three tables of thirteen routines (0x5E8B with a loop page, 0x6709 without, 0x6EBB for listening in WAVE EDIT). Every key has in 0xD230 a routine r and a delay D (0x5DA9, and 0x5E29 per note), and the routine is always the same thing: the byte to the DAC, D turns of a wait loop, a look at whether the key is still down, and padding of instructions that do nothing (nop, cp 0, neg, inc ix…).

Counted instruction by instruction, within each routine every path costs the same per sample, and routine r takes 59 + r + 14 × D cycles of the Z80's data sheet: the thirteen routines are a one-cycle fine adjustment. On an MSX every opcode fetch carries a wait state, and what is measured in openMSX is 67 + 16 × D for routine 0 (In the emulator). While the tail of the sample plays, the keyboard is scanned row by row, and another key starts another note: PLAY is monophonic.

FILE: the tape

On tape (0x4D04 saves, 0x4D8C loads), four blocks through the BIOS, each after its header:

  1. the type sixteen times: 0x21 TONE, 0x22 LAYOUT, 0x23 SAMP;
  2. the seven letters of the name, in the font's code (0x21 = A);
  3. the length in two bytes and, only in SAMP, the loop page;
  4. the data: TONE to 0xC000 (4,608 bytes), LAYOUT to 0xD200 (176), SAMP to 0x8000 (0x4E7A).

When loading, with a blank name any file of the type will do; otherwise it looks for the one with that name.

The ROM also carries disk routines through the Disk BASIC's BDOS (0xF37D: NAME.T, .K and .S, and a directory of up to 35 names, 0x4F4D), and a DISK option in the menu that is only drawn if there is a drive (H.PHYD, 0x438F). They have not been tested.

The tape that came with the cartridge carries seven SAMPs on its two sides: PIANO, BRASS and ORCHE of 16,384 bytes, TRUMPET of 12,288, SAMISEN of 7,792, DOG of 3,072 and CRASH of 16,384.

The printer

SHIFT F3 (0x52B7) dumps the screen as dots through LPTOUT, in 64 bands of four rows and 192 columns (0x5419), with the sequences at 0x54B6; from WAVE EDIT it also prints the table of the wave, 64 signed samples in eight rows (0x5516). From the code: it has not been tried on a printer.

In the emulator

The pictures are built from the ROM, not captured. openMSX is for checking those pictures against the real VRAM, and for measuring what the code says about the sound.

All with openMSX and the Philips_VG_8020 machine, one emulator at a time.

Checking the screens

tools/omsx_dibujo.tcl follows a key script and dumps the VRAM, the RAM from 0xC000 up and the VDP registers when told; tools/omsx_tone16.tcl does not go by the clock: it waits for the intro to be at the title, starts the program, opens TONE EDIT and dumps one screen per tone, going from one to the next with INS. tools/coteja_todo.py builds every screen with tools/pantallas.py and compares it with its dump, byte for byte in everything it writes and dot for dot in the picture.

screen differences
Konami logo 0
title 0
TONE EDIT, all sixteen tones 0
WAVE EDIT 0
SYN 0
PLAY 0
FILE MODE 0

Twenty-three screens. In seven of the TONE EDITs the dump catches the cursor in the other phase of its blink (colour 12 instead of 8, 0xA5AA): it is the only different byte and it is accepted.

The synthesizer

tools/omsx_sintesis.tcl notes every byte SYN writes at 0x57B0; tests/test_motor.py compares the sixteen tones with what tools/motor.py gives: byte for byte the same.

What reaches the DAC

tools/omsx_dac.tcl notes every byte the Z80 writes to 0x4000 with its instant: it synthesizes tone 1, gives the Z key routine 9 and delay 11 (A3) and holds it for half a second. tools/coteja_dac.py rebuilds what has to arrive and compares:

   
bytes to the DAC 22,273: the rest value, the whole 16 KB and 23 more turns of loop page 0x84
different from motor.py 0
period between writes 254.37 cycles on average (254 and 255); sonido.py uses 254.4

tools/omsx_ritmo.tcl is the measurement the thirteen routines' cycles in sonido.py come from: each one with D = 32 and D = 64, 3,000 samples.

The sample in RAM

With a breakpoint after the write at 0x57B0: during SYN the slot register reads 0xF4 (page 2 is slot 3, the RAM) and the byte read back after writing it is the one written.

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, 48 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)
Konami’s Game Master 2 18 206 escribe_lista (0x4337) = th_pinta_texto
Q*bert 18 195 rle_a_vram (0x4352) = guion_rle
Konami’s Game Master 15 177 nombre_del_punto (0x5405) = posicion_a_vram
The Goonies 15 165 rle_a_vram (0x4352) = guion_rle
Yie Ar Kung-Fu II 15 180 byte_de_teclas (0x42B7) = lee_las_filas_7_y_8_del_teclado
Hyper Sports 3 14 176 byte_de_teclas (0x42B7) = teclado_del_jugador_1
The Maze of Galious 13 177 rle_a_vram (0x4352) = rle_con_destino
Konami's Ping Pong 13 159 byte_de_teclas (0x42B7) = lee_teclado_1
F-1 Spirit — The Way to Formula 1 12 128 rle_a_vram (0x4352) = RLE_SIGUE_PUNTERO
Nemesis / Gradius 12 134 rle_a_vram (0x4352) = descomprime_con_destino

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.