Under the hood

Getting started

The repository ships the listing already generated, but what really matters is that it can be rebuilt from the cartridge and that the result is the ROM byte for byte. That is what makes the notes believable.

What you need

The cartridge

It does not travel with the repository. Put it in the root as pingpong.rom, exactly 16,384 bytes:

8e0f57b2c52e85505505c1d4bb4643eb7311213676f0ecfe21dd06571fc0bbd5

To check it is the same one:

make comprueba

Rebuilding everything

make

That chains the four things that matter:

step what it does
make listado traces the flow from the entry points and writes src/pingpong.asm
make verify reassembles with pasmo and compares the sha256 against the cartridge
make sanity what reassembly CANNOT catch (see below)
make test the 36 tests

Why make verify is not enough

Reassembly proves the bytes are the same, not that what we say about them is true. If a data table were read as code, the binary would still come out identical: the only thing that would change is the lie in the listing.

So make sanity checks three more things:

The numbers

make densidad

It gives the two that get published: how many instructions carry a comment, and whether any routine is left below 10 %.

The pictures

make imagenes

It draws the screens from the bytes of the ROM, running in Python the same steps the Z80 runs. There is not one screen capture in this repository.

And to check them against the real machine:

"C:/Program Files/openMSX/openmsx.exe" -machine Philips_VG_8020 \
    -cart pingpong.rom -script tools/omsx_vram.tcl
python3 tools/coteja_vram.py pingpong.rom work/omsx/vram_01.bin logotipo

That dumps the emulator's 16 KB of VRAM at several moments and compares them byte for byte against what tools/graficos.py builds.

The files written by hand

Only three, and all three carry the reasoning behind every decision:

Everything else comes out of those.

The cartridge

The facts

   
Game Konami's Ping Pong
Publisher Konami, 1985
Reference RC-731
Size 16,384 bytes
Machine MSX1
Page 1 (0x4000..0x7FFF)
sha256 8e0f57b2c52e85505505c1d4bb4643eb7311213676f0ecfe21dd06571fc0bbd5

The header

The first sixteen bytes are the header every MSX cartridge carries:

41 42 6C 40 00 00 00 00 00 00 00 00 00 00 00 00
"A" "B"  INIT     STATEMENT  DEVICE   TEXT   reserved

"AB" is the signature, and behind it the address of INIT: 0x406C. The other three entries are zero, so the cartridge adds no BASIC statements and declares no device.

INIT starts no loop

What 0x406C does fits in twenty instructions:

  1. disables interrupts and sets mode 1
  2. writes a jp (0xC3) at 0xFD9A and the address 0x4019 at 0xFD9B, which is the H.KEYI hook
  3. puts the stack at 0xE400
  4. clears the 0x400 bytes of variables from 0xE000
  5. sets the (0xE005) latch, builds the screen, clears it
  6. enables interrupts and sits on jr $

And there it stays forever. The whole game hangs off the interrupt, once per frame, off H.KEYI.

How the 16 KB break down

  bytes  
code reached 7,676 46.85 %
data identified 8,708 53.15 %
unexplained 0 0.00 %

The screen

The eight VDP registers come from the table at 0x47D6, which 0x47C5 pours into R0..R7:

02 E2 0E 7F 07 76 03 E4
reg value what it says
R0 0x02 SCREEN 2
R1 0xE2 16K, display on, interrupt on, 16x16 sprites
R2 0x0E NAME table at 0x3800
R3 0x7F COLOUR: base 0x0000, mask 0x03FF
R4 0x07 PATTERNS: base 0x2000, mask 0x03FF
R5 0x76 sprite attributes at 0x3B00
R6 0x03 sprite patterns at 0x1800
R7 0xE4 ink 14 on background 4

R3 and R4 are not addresses: they are a base and a mask. Decoded, they lay out VRAM the other way round from the usual, with COLOUR low and PATTERNS high:

   
0x0000..0x17FF COLOUR
0x1800..0x1FFF sprite patterns
0x2000..0x37FF PATTERNS
0x3800..0x3AFF NAMES
0x3B00..0x3B7F sprite attributes

Reading them as addresses gives the layout swapped, and with it the shapes come out right and the colours in stripes. There is no need to take the reading on trust here: the measured destinations of every compressed block fall inside this layout and not the other, and the cartridge itself confirms it twice by filling 0x0080 with 0xF0 and 0x1600 with 0x70 -values that only make sense as colour-.

R7 is 0xE4 -blue background- while the company logo is up, and 0x4B4B changes it to 0xE0 -black- before painting the player selection.

RAM

The game uses 0x400 bytes from 0xE000, and clears them all at startup. The addresses that come up most:

address what it holds
0xE000 / 0xE001 the scene and the step within it
0xE002 the mode: bit 5 two players, bit 6 in play
0xE003 the frame clock, never reset
0xE004 the subscene countdown
0xE005 the interrupt reentry latch
0xE008 / 0xE009 player 1's control: edges and raw state
0xE048 / 0xE049 player 2's
0xE010.. the three sound channels, fifteen bytes each
0xE070.. the sprite attribute buffer, 0x80 bytes
0xE100.. the ball: position, velocity and state
0xE130 the phase of the rally
0xE132 / 0xE133 the two scores, in BCD
0xE160.. / 0xE180.. the two players

The hidden mark

The last fifteen bytes:

AC B8 9D AC B8 9A 00 98 00 9F 94 89 0C 31 AA

Twelve of title in katakana -コナミのピンポン-, the length 0x0C and the 0x31 of RC-731. Before them sit 70 bytes of 0xFF, whatever was left over of the cartridge.

That mark was discovered by Manuel Pazos, and the finding is his.

The code

One whole frame

Everything hangs off H.KEYI, so the program is whatever fits between two interrupts. The heartbeat at 0x4019 always does the same:

call RDVDP              acknowledge the interrupt
di / call 0x5195 / ei   the sound, which cannot miss a frame
bit 0,(0xE005)          the reentry latch
  inc (hl)              set it
  call 0x46AC           check whether ends must be swapped
  call 0x47E3           read the controls
  call 0x409F           run whichever scene is due
  ld (0xE005),0         and clear it
call RDVDP / call m,0x5195   one more sound pass

The latch matters: if a frame takes longer than a frame lasts, the next one is skipped whole instead of running half-way through.

Dispatch, on two levels

The scene is dispatched by 0x4062, the dispatcher this family of Konami cartridges shares:

add a,a / pop hl / call 0x4058 / ld e,(hl) / inc hl / ld d,(hl)
ex de,hl / jp (hl)

The pop hl retrieves its own return address, and that is the table: it sits glued right behind the call. How many entries it has is not estimated; it is pinned by the fact that its first entry points just past the table. Both tables match to the byte:

0x40C9 + 8*2  = 0x40D9, and its first entry is 0x40D9
0x6225 + 12*2 = 0x623D, and its first entry is 0x623D

The subscene uses no table at all. It is picked by walking down a chain of djnz:

L_40D9: djnz L_40EE      <- subscene 0
        ...
L_40EE: djnz L_40FA      <- subscene 1
        ...

Each step is two bytes, and the block that runs is the one where B hits zero. Twenty-five steps in fifty bytes, no comparisons and no table.

To move from one step to the next there are two endings repeated all over the listing: 0x4260 bumps (0xE001), and 0x425D also leaves A frames of wait in (0xE004).

The interpreters

The cartridge carries three, and all three read a script out of the ROM.

The labels, and their four doors

The format is [VRAM destination, word] [pattern indices...], with 0xFE to jump to another destination and 0xFF to finish. The pattern index is the ASCII code minus 0x20, so the font is laid out like ASCII from the space up.

The neat part is that one loop covers four things, depending on which door you come in by:

entry header mask
0x4764 read from the script 0xFF, paints
0x476C already in HL whatever it carries
0x475C already in HL 0xFF, paints
0x4760 read from the script 0x00, erases

The fourth is the clever one: with the mask at zero the and c leaves the byte at zero, so the very script that wrote the label erases it.

The decompressor

0x00           closes the block
0x01..0x7F     the byte that follows, repeated that many times
0x80           new VRAM destination (two bytes)
0x81..0xFF     literal of (command & 0x7F) bytes

It writes to the VDP port with one out (c),a per byte, keeping the port in C' so it does not disturb the caller's registers.

And on top there are two loops that repeat it: 0x4735 with B=2 and 0x4739 with B=3, raising the destination by 0x800 each pass. The push de / pop de returns DE to the start of the block, so it is the same block being painted into all three thirds of SCREEN 2.

The second decompressor

0x5E9B has the same commands minus the 0x80 -it cannot change destination mid-way- and, above all, it is not handed the block in DE: it fetches it from a pointer. That is what links each frame with its patterns, and it is told in Findings.

The sound

Three channels of fifteen bytes each from 0xE010. A tune can take one, two or three, and the tune number doubles as its priority: 0x5121 compares the low six bits of what is playing against the new one and will not override it if it is lower.

The tune script is read by nibbles:

command what it does
bare 0x20 envelope: writes registers 11, 12 and 13
0x2x duration
0x1x noise: period to register 6 and the mixer opened
0xDx duration multiplier
0xEx octave
0xFx starting volume
0xFE / 0xFF close
anything else note: the high nibble is the note, 0 to 12

The note becomes a period through the twelve-semitone table at 0x532D, and the octave is applied by doubling the period as many times as (ix+5) says. That they are twelve equal-tempered semitones is not an assumption: the ratio between consecutive notes comes out between 1.050 and 1.071, against the 1.0595 of the twelfth root of two.

The 12 is not a note: it is the rest.

The arithmetic

The Z80 can neither multiply nor divide, so the cartridge does it by hand:

With those three and three fixed factors, 0x6BA1 projects the three table coordinates onto a screen row and column.

How a control is read

The joystick is read through the PSG -0x8F to register 15 to select the port, 14 to read- and the keyboard with SNSMAT. The good bit is that 0x4812 builds from the keys the very same bit map the joystick gives, shuffling each row into place with rrca. From there on the rest of the game cannot tell one from the other.

And 0x47F9 is the filter that turns state into fresh presses: it stores what is current, XORs it with what was there and ANDs with what is current. What is left are the rising edges, and they go one byte below the raw state.

In the emulator

The pictures on this site do not come from the emulator: tools/graficos.py draws them from the bytes of the ROM. The emulator is used for the opposite, which is checking that those pictures are true.

Dumping the real VRAM

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

The script lets the game run and, at seven moments spread over the startup and the match, dumps into work/omsx/:

It sets no breakpoints at all: the dumps are scheduled on emulated time, which is the only thing that does not choke the emulator. And it carries a real-time watchdog, so a broken script cannot leave it hanging.

Comparing

python3 tools/coteja_vram.py pingpong.rom work/omsx/vram_01.bin logotipo
python3 tools/coteja_vram.py pingpong.rom work/omsx/vram_03.bin titulo
python3 tools/coteja_vram.py pingpong.rom work/omsx/vram_04.bin nivel

It compares the four tables -colour, patterns, names and sprite patterns- byte for byte, and says in which and in how many bytes they differ. What comes out today:

scene bytes different out of 16,384
logo 0
table with the menu 0
title 1

The one byte left is the eighth of the small "KONAMI" at 0x3868: the dump catches that animation half-way through, it is not a format error.

Sprite attributes are not compared: they change every frame. What is compared is the scenery, which is what graficos.py claims to reproduce.

What the registers confirmed

info_00  regs 02 E2 0E 7F 07 76 03 E4   scene 0, subscene 2
info_02  regs 02 E2 0E 7F 07 76 03 E0   scene 1, subscene 3
info_04  regs 02 E2 0E 7F 07 76 03 E0   scene 3, subscene 5

Three things at once:

Playing it

With any MSX1 emulator and the cartridge in page 1. With openMSX:

openmsx -machine Philips_VG_8020 -cart pingpong.rom

Player 1's keys are the arrows and the space bar; player 2 has their own keys, spread over four rows of the matrix, and both joystick ports work too.

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, 54 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)
Yie Ar Kung-Fu II 25 284 lee_teclado_1 (0x4812) = lee_las_filas_7_y_8_del_teclado
The Goonies 22 235 descomprime (0x477B) = guion_rle
Road Fighter 22 258 descomprime (0x477B) = descomprime_con_destino_dentro
Hyper Sports 3 21 243 lee_teclado_1 (0x4812) = teclado_del_jugador_1
Knightmare 20 209 pinta_rotulo (0x4764) = pinta_texto
Konami’s Soccer 20 221 pinta_rotulo (0x4764) = escribe_un_rotulo
Konami's Boxing 19 202 pinta_rotulo (0x4764) = pinta_texto
Q*bert 18 181 descomprime (0x477B) = guion_rle
Yie Ar Kung-Fu 17 185 lee_teclado_1 (0x4812) = lee_el_teclado
Nemesis / Gradius 16 170 descomprime (0x477B) = 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.