Under the hood

Getting started

A commented disassembly of Cabbage Patch Kids (Konami, RC-716, 1984), the 16 KB MSX cartridge. Everything here comes out of the binary and can be generated from it again.

What you need

It goes in the root as cabbagepatch.rom, 16,384 bytes, sha256

114945c770531db3ae61eb330d7ed870a3c00d3a90db2a29a4c134135c2c0707

and make comprueba checks it.

The test that decides

make verify

This reassembles src/cabbagepatch.asm with pasmo and compares the result, byte for byte, with the cartridge. If it says OK: reproducible byte a byte, the listing has invented nothing.

What reassembly cannot catch

A listing can reassemble perfectly and still be wrong: if drawings are read as instructions the bytes do not change, only what is said about them. So three more checks run alongside, all in make sanity:

Everything you can run

make            # trace, listing, verify and the tests
make listado    # regenerates src/cabbagepatch.asm from the annotations
make sanity     # the checks reassembly does not cover
make densidad   # how much is commented, routine by routine
make imagenes   # redraws the PNGs in docs/imagenes from the ROM
make web        # regenerates this site
make test       # the tests

And one more that is specific to this cartridge:

python3 tools/repasa_el_porte.py

The annotations were ported from another cartridge — see Findings — and that tool is the guardian for it: it checks that no address cited in a comment is a leftover, that the other game is not named where it should not be, and it lists every comment that is identical to the sibling's but sits on a different instruction.

What is in here

The cartridge

16,384 bytes with the standard header: AB at 0x4000, INIT at 0x404F, and STATEMENT, DEVICE and TEXT at zero. The BIOS maps it into page 1 (0x4000-0x7FFF) and jumps to INIT when it has finished booting.

The memory map

   
0x4000-0x7FFF the cartridge
0xE000-0xE3FF the game's RAM, which INIT clears
0xE400 the stack

Of the cartridge, 7,981 bytes are code (4,038 instructions) and 8,403 are data, in 143 named ranges. Nothing is left over.

What the VDP is told

The eight registers live at 0x44F1 and 0x44D7 copies them to 0xE038 before sending them:

register value what it means
R0 0x02 SCREEN 2
R1 0xE2 16K, screen on, interrupts on, 16×16 sprites
R2 0x0E name table at 0x3800
R3 0x7F colour table at 0x0000
R4 0x07 pattern table at 0x2000
R5 0x76 sprite attributes at 0x3B00
R6 0x03 sprite patterns at 0x1800
R7 0xE1 black border

How the drawings are stored

Three ways, and the listing uses all three to walk the data and find where each block ends:

Run-length, unpacked by RLE_A_VRAM (0x4D24). A count byte: zero ends the stream, a count with bit 7 set copies that many bytes as they are, and one without it repeats the next byte that many times. Entered at 0x4D24 the stream carries its own two-byte VRAM address in front; entered at 0x4D28 the address comes in DE.

Straight copies, 0x4502, with HL, DE and BC.

Mirrored copies, 0x6DC1: the same, but each byte goes in with its eight bits reversed, so one drawing serves for a thing and for the same thing facing the other way. It counts with B, and one of the calls loads ld bc,0x0018, which leaves B at zero: that copy is 256 bytes, not 24.

The font

48 glyphs of eight bytes at 0x4A37. The tile code is the ASCII, so they run from 0x30 (0) to 0x5F, and 0x4635 repeats them across the three thirds of the screen so text can be written anywhere.

The font

After the Z there are five slots that are not letters: the word with that the menu needs for «1PLAYER with JOYSTICK», and the two arrows of the menu cursor.

The lists that draw

Two formats, both of them lists of tile codes:

The code

Everything happens inside the interrupt

INIT (0x404F) sets the stack at 0xE400, clears 0xE000-0xE3FF, writes jp 0x402C into the H.KEYI hook, sets up the VDP and the PSG, loads the font, and falls into jr $ at 0x4080. It never leaves.

From there the interrupt is the program. Every frame it:

  1. plays the sound —always, even if the frame before ran long—,
  2. reads the controls, if there is a game in play,
  3. runs one step of the state that 0xE000 names.

0xE005 is the latch. While a step is running it is set, and if the next interrupt arrives before the step has finished it only moves the music along and leaves (0x404C). The game slows down; it does not trip over itself.

The dispatcher, and the table behind the CALL

DESPACHA (0x4082) is Konami's: it is called with the index in A and finds its table by itself.

add a,a
pop hl          ; the return address IS the table
call HL_MAS_A
ld e,(hl) / inc hl / ld d,(hl)
ex de,hl
jp (hl)

It never returns to the call. Five tables use it:

table entries what it picks
0x4144 20 the state of the game (0xE000)
0x41F8 6 the steps of the menu (0xE001)
0x6016 17 the state of the player (0xE138)
0x785D 4 the action on the kid-picking screen
0x7960 4 the same on the naming screen

Each one closes exactly against its lowest destination, and that is what gives its size. They are declared in src/cabbagepatch.nocode so the tracer does not walk into them: read as instructions, they would give false coverage.

And what runs after the state

Since the dispatcher does not come back, anything that has to run behind the state has to be left on the stack first. PASO_DEL_JUEGO (0x4128) loads HL with 0x4735 when there is a game in play and with 0x40C1 when there is not, and pushes it: the state's ret lands there.

The states of the player

Seventeen, in the table at 0x6016, with six holes that are 0x0000 and are never used. Walking, in the air, on the swing, on the trampoline, on the log, the goal, the stage cleared, sinking, hit. The one for being hit (LE_HAN_DADO, 0x6328) turns off four bits of the pattern table —the face changes— and moves to state 16 with its sound.

Jumps and falls come out of a table of deltas read forwards and backwards: the same list, walked one way subtracting and the other adding, is the rise and the fall, and the bytes 0xFE and 0xFF are what turn it around and what end it (0x5BC4 and friends).

The sound

The player is Konami's three-channel one, at 0x7AA0, with eleven bytes per channel from 0xE010, 0xE01B and 0xE026. SONIDO (0x7A08) asks for a number: below 0x0D it is an effect and takes one channel; from 0x0F up it is music and takes three. The number is also the priority: a channel already playing something with an equal or higher number is not interrupted (0x7A2F).

Notes are a nibble each, with 0xFn changing octave and 0xDn setting the volume, and the twelve periods of one octave live at 0x7BFF; lower octaves are that period doubled, once per octave (add hl,hl).

The instruction the Z80 does not have

DESPLIEGA, the drawing unpacker, needs ld (IX+A),B —a displacement that changes— and the Z80 only takes a fixed one, inside the opcode. So the cartridge writes the byte as it goes: seven bytes copied into RAM at 0xE5F8 that do ld (0xE5FD),a and then ld (ix+00h),b, where 0xE5FD is exactly the operand of that second instruction. It is the only code in the cartridge that does not run from ROM.

In the emulator

The pictures in the gallery are drawn from the ROM, without an emulator. This page is the other thing: what was checked by running the cartridge, which is the only way to know that a screen exists and looks the way the code says it should.

The harness is tools/omsx_capturas.tcl, on a Philips VG-8020 —a real MSX1 of the period— with the cartridge given on the command line:

CPK_OUT=work/shots CPK_TECLAS="16 BARRA;19 ABAJO;23 FOTO" \
  openmsx -machine Philips_VG_8020 -cart cabbagepatch.rom \
          -script tools/omsx_capturas.tcl

CPK_TECLAS is a list of second key pairs, and FOTO takes a screenshot. Keys are pressed on the key matrix, not typed: type would write the word.

What it settled

Two traps that cost a capture each

And one thing the emulator would not give

Getting to the naming screen by pressing keys does not work if the option chosen on the menu is a joystick one, because then the game reads the joystick and the keyboard is ignored. Rather than fight it, the menu step is pushed on by hand —CPK_POKE="30 0xE001 4" writes that byte at that second—, which is the same thing the game does when it moves along, only without waiting for the right key.

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, 47 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)
Athletic Land 72 1,114 COBRA_PUNTOS (0x5FA3)
Magical Tree 25 298 LEE_EL_TECLADO (0x409C) = lee_el_teclado
Circus Charlie 23 255 LEE_EL_TECLADO (0x409C) = lee_el_teclado
Comic Bakery 20 206 LEE_EL_TECLADO (0x409C) = lee_el_teclado
Konami’s Mahjong Dojo 18 214 RELLENA_VRAM (0x4522) = rellena_la_vram
Hyper Sports 1 17 210 LEE_EL_TECLADO (0x409C) = LEE_TECLADO
Yie Ar Kung-Fu 12 155 LEE_EL_TECLADO (0x409C) = lee_el_teclado
Monkey Academy 10 115 PINTA_BCD (0x4718) = PINTA_BYTES_BCD
Super Cobra 10 97 SONIDO_SIGUIENTE_VUELTA (0x7A5D) = SONIDO_ENCADENADO
Konami’s Billiards 8 97 PINTA_FASE (0x4710) = pinta_el_numero_de_fase

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.