Under the hood

Getting started

A commented disassembly of Konami's Tennis, Konami's RC-720 for the MSX, a 16 KB cartridge mapped into page 1 (0x4000–0x7FFF). It reassembles into the exact ROM, byte for byte, and every one of its 16,384 bytes is accounted for.

The cartridge is not here

No repository distributes the game. Put your own dump in the root as tennis.rom, 16384 bytes, sha256

68bec8172d816025a21dd47482e9413fd8bd726b2cafb49236ca58a65c73e07f

make comprueba checks it.

What each command does

make            traces the flow, builds the listing, reassembles it and runs the tests
make verify     the test that decides: reassembling must give the ROM back
make sanity     that not one byte is left unexplained, and no data comes out as code
make densidad   how much is commented, routine by routine
make web        rebuilds this site from the ROM and the notes

How it is put together

The listing is not hand-edited: tools/mkasm.py builds it from a flow trace and a file of address-anchored notes, so the comments survive a re-trace. The tracer follows the flow from the entry points; the ones it cannot deduce on its own —the interrupt hook above all— are declared in src/tennis.entries, each with its reason.

The pictures on the front page are not captures. tools/graficos.py draws them by running in Python the cartridge's own steps: the two screen interpreters of tools/guiones.py, the VRAM-to-VRAM copy at 0x442D and the sprite decompressor at 0x5932. And they are not taken on trust: graficos.py --comprueba dumps the emulator's VRAM and compares it byte for byte.

The cartridge

16 KB in page 1, 0x4000–0x7FFF. The AB header declares only INIT, and puts it at 0x409B — the first ten bytes are 41 42 9B 40 00 00 00 00 00 00.

What INIT does

It hooks the interrupt before anything else: it writes a 0xC3 into H.KEYI (0xFD9A) and the address 0x4010 behind it, so the whole game hangs off the interrupt and a static trace cannot reach it on its own. That is why 0x4010 is declared in src/tennis.entries.

Then it clears 0xE000–0xE3FE with an overlapping ldir, leaves the stack just above it, silences the PSG, and loads the VDP's eight registers from the table at 0x45C9.

The screen

SCREEN 2, with those eight registers: 02 E2 0E 7F 07 76 03 E1.

R2 = 0x0E   name table ............. 0x3800
R3 = 0x7F   colours ................ 0x0000
R4 = 0x07   patterns ............... 0x2000
R5 = 0x76   sprite attributes ...... 0x3B00
R6 = 0x03   sprite patterns ........ 0x1800

R3 and R4 are the ones worth care: in SCREEN 2 the TMS9918 reads them as a base bit and a mask, not as an address, so this cartridge ends up with the colour table underneath the pattern table — the reverse of the usual layout.

The map of the ROM

0x4000  cartridge header
0x4010  interrupt handler
0x409B  INIT
0x430F  four-bit direction table
0x45C9  the VDP's eight registers
0x45D1  the title screen's tile lists
0x4686  the title screen script
0x489D  35 eight-byte patterns, asked for by index
0x49B5  the court script
0x4FF1  the court's tile list
0x5528  the ball's five sizes
0x5961  58 pose entries, 37 descriptions, 189 patterns
0x6AA7  the shot's effect table
0x6E61  three difficulty curves
0x715D  the umpire's three faces
0x763A  the match messages and the scores
0x776C  the ball boy's five groups
0x7C5D  the twelve semitones and 25 tunes
0x7FE9  23 bytes of 0xFF filler

The hidden mark

Konami hid its catalogue number and the title in katakana at the end of many of its cartridges — a find by Manuel Pazos (@ManuelPazosMSX). This one does not carry it. All 16,384 positions were scanned with tools/busca_marca_konami.py, a finder first validated against cartridges of the same family that do carry it.

The code

7,882 bytes of code and 8,502 of data. 492 named routines, 1,368 anchored comments, 32.4% of the listing commented and no routine under 10%.

Everything hangs off the interrupt

0x4010 is a whole frame of the game. It reads the VDP status, marks 0xE01D so nobody else touches the VDP behind its back, reads the controls, runs the sound — which plays on every frame, whatever else happens — and then, if there is a match on, the nine tasks of the frame, in order, from 0x4070:

0x6D12  swap the doubles pairs
0x555D  give each player his frame
0x6E97  rotate the pairs between points
0x6B25  play the opponent
0x678D  decide his shot
0x51FE  move the ball
0x6ED2  the umpire's eyes
0x7932  award the point
0x7F56  put everyone back in place

0xE0D9 spreads that work over frames: not all of it fits in one.

The two screen interpreters

The cartridge does not store screens as VRAM dumps. It stores scripts.

Tile lists (0x43EA) are the simple one: each entry starts with its VRAM address in two bytes and then bytes that go to the data port, with 0xFE n b to repeat and 0xFF to close. Two 0xFF in a row close the list.

Scripts (0x445F) start with a block count, and each block carries a command:

0    raw bytes, with 0x11 n b to repeat
1    patterns by index into the table at 0x489D, eight bytes each
3    one address and then pairs of count and value
*    one address, a repeat count, and a pointer to an eight-byte pattern

And a rule that runs through all of it: any counter at zero means 256, because the Z80 decrements before it tests.

The figures

A pose is five stacked 16x16 sprites. 0x5961 is a table of 58 entries; each points at a twelve-byte description — five pattern pointers plus one pointer to the five (y,x) pairs. 0x58A9 loads the patterns, 0x58D4 places them.

The sprite decompressor is 0x5932 and always writes 32 bytes, with one rule: 0x00 n writes n zero bytes, everything else goes through as it is.

The arithmetic

There is no multiply or divide on a Z80, so the cartridge builds its own: 0x54AA shifts and subtracts eight times, 0x54CA shifts and adds sixteen, and 0x547E divides by counting how many times the divisor fits. The ball's flight is built out of those three.

The sound

Three channels, eleven bytes of state each from 0xE211. A tune is read by the high nibble of each byte: 0x1x duration, 0x2x volume, 0xDx vibrato step, 0xEx octave, 0xFx vibrato width, 0xFE up an octave, 0xFF end. Anything else is a note — high nibble the semitone, low nibble the duration — and the period comes from the twelve-entry scale at 0x7C5D, doubled once per octave.

In the emulator

The pictures on the front page are drawn from the ROM, not captured. Looking at them is not enough to call them right, so they are checked against the VRAM the VDP really holds.

The dump

"C:/Program Files/openMSX/openmsx.exe" -machine Philips_VG_8020 \
    -cart tennis.rom -script tools/omsx_vram.tcl

tools/omsx_vram.tcl lets the game run and, at twelve moments, writes out the 16 KB of VRAM, the VDP's eight registers and the working variables that say what was being drawn. It sets no breakpoints: the dumps go by emulated clock, which is the only thing that does not choke the emulator.

The first ones are taken at nine seconds, not at three: before that the cartridge is still halfway through painting the title and the VRAM is not comparable with anything.

The comparison

python tools/graficos.py --comprueba tennis.rom 0x4000 work/omsx

12 dumps, 137,260 bytes compared, 0 different

Two things are left out of the comparison, both with a reason. Dumps taken before INIT has loaded the VDP registers, because the screen is not built yet. And the 32 colour bytes of tiles 0xA5 to 0xA8, which are the fault message: 0x746F alternates two colour sets over them every seven frames, so their value depends on the exact frame the dump was taken on.

What that comparison found

It is what caught the pattern and colour tables being read the wrong way round. The shapes came out fine — you could read "Konami's Tennis" — but the colours arrived in bands. The VRAM matched byte for byte; the mistake was in reading it.

And it is what showed that the court script does not carry the font. Building the court on an empty VRAM left 1,514 bytes different, all of them letters and digits. The cartridge does not clear the patterns when it goes from the title to the court: it only clears the name table, at 0x4234. Starting the court from the title's VRAM, as the cartridge does, took those 1,514 differences to zero.

Following the ball boy

work/sigue_sprites.tcl follows 0xE03A, 0xE03B and 0xE03C and the sprite attributes at 0x3B3C. In two minutes of demo the ball boy never comes out: he needs 0xE0C0 or 0xE044 set, and neither happens. Writing 0xE0C0 by hand from the debugger brings him out, and his four sprites move with him — which is how "he is made of sprites" stopped being a reading of the code and became something seen.

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, 33 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 Baseball 32 518 manda_de_la_melodia (0x7BE8) = lee_una_nota_larga
Konami's Golf 25 405 manda_de_la_melodia (0x7BE8) = corre_el_canal_nota_larga
Hyper Olympic 1 22 312 replica_vram (0x442D) = COPIA_VRAM_A_VRAM
Hyper Olympic 2 22 312 replica_vram (0x442D) = COPIA_VRAM_A_VRAM
Konami's Ping Pong 5 49 enciende_canal (0x7AD6) = 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.