Under the hood

Getting started

This repository does not carry the cartridge. It carries what you need to regenerate the listing from your own copy and check that it produces the same binary, byte for byte.

What you need

The four commands

make comprueba   # that your ROM is the same one: compares the sha256
make listado     # traces the flow and writes src/gamemaster.asm
make verify      # reassembles and compares with the original, byte for byte
make             # all of the above, plus sanity and the tests

make verify is the test that decides whether the disassembly is trustworthy. If the reassembled binary does not give the same sha256, the listing is lying somewhere.

What reassembling does NOT check

Getting the same binary says nothing about whether the bytes are read correctly: graphics marked as code give you the same file, because the bytes do not change — only what we say about them does. That is what make sanity is for, and it runs four checks:

The pictures

make imagenes    # draws the screens from the ROM
make vram        # checks them against openMSX's VRAM, byte for byte

Not one picture on this site is a capture. They all come from running in Python the same steps the Z80 runs: the decompressor at 0x62F7, the rectangle painter at 0x7852 and the label painter at 0x6294. make vram lets the cartridge run in the emulator, dumps the 16 KB of VRAM and compares them one by one with the ones we build.

How it is laid out

src/gamemaster.entries   the entry points for the trace, each with its why
src/gamemaster.nocode    the ranges that are NOT code
src/gamemaster.notes     the annotations: labels, comments and data ranges
src/gamemaster.asm       the listing, GENERATED — not edited by hand
tools/                   the tracer, the generator and the checks
tests/                   what keeps the documentation from lying

The listing is generated. Anything you want to change goes into the .notes, which anchors every comment to an address: that way they survive a retrace.

The tools this cartridge needed

tools/rle.py          the decompressor at 0x62F7, redone in Python
tools/pantallas.py    decodes the label blocks and shows them laid out
tools/cadenas.py      walks the messages and measures where they end
tools/menus.py        derives the thirteen menus by measuring on the code
tools/graficos.py     builds the screens from the ROM
tools/coteja_vram.py  compares them with the emulator's

The cartridge

16 KB in page 1 (0x4000–0x7FFF), MSX1. The AB header declares only INIT (0x4010); STATEMENT, DEVICE and TEXT are all zero.

The problem it has to solve

And it is the whole design of the cartridge.

The Game Master lives in page 1. The game it is cheating lives in page 1 as well, in another slot. The two cannot be switched in at once: the moment you switch the slot to read something from the game, the Game Master vanishes from the memory map — and with it the instruction that was executing.

Everything else follows from that:

  1. Six blocks of code are copied into RAM and executed there. Switching the slot from ROM would be sawing off the branch you are sitting on.
  2. Whatever lives in ROM and has to be called with the other slot in is called through CALSLT (0x001C) with the slot in IY, or through the RST 30h template that 0x6AAA assembles at 0xD33E.
  3. To read a byte from the neighbour it uses RDSLT (0x000C). 0x4CA6 is the inter-slot LDIR: it copies BC bytes one at a time, because there is no other way.

When reading the listing, bear in mind that the absolute addresses inside those blocks point at where they run, not where they are stored. A jp 0xDA4C in the first block is a jump to its own byte 0x4C. The tracer has a directive, !reubica, that gives it that mapping; with it the trace went from 52.9% to 60.0%, and with the CALSLT entries it uncovered, to 64.2%.

The six blocks:

in the listing runs at what it is
0x40DE–0x419A 0xDA00 the slot sweep looking for a cartridge
0x41A8–0x422F 0xC000 the one that also recognises the disk ROM
0x4264–0x426F 0xC800 eleven bytes: the way back from the game
0x426F–0x4272 0xFEDA three bytes: a jp 0xC800 in a BIOS hook
0x4CCA–0x4D17 0xD814 where the patch planted in the game jumps to
0x4D17–0x4E61 0xD170 the interrupt hook

The VRAM map

It is not the usual one, and reading it wrong throws the whole screen out. The eight registers are at 0x6284 and 0x6273 writes them:

register value what it places
R0 0x02 graphics mode 2
R1 0xE2 display on, 16x16 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 the border

R3 and R4 are not addresses: they are base and mask, and they come out the opposite way round from what they look like. Colour ends up at the bottom, at 0x0000, and patterns at the top, at 0x2000. That is what places the four compressed blocks of the menu background, and it is confirmed against the registers the emulator actually holds.

Behind those eight there are four more bytes (0x628C: 0E 00 18 06) that nobody reads: the loop at 0x6279 stops at eight, and searching for their addresses across all 16,384 bytes of the cartridge turns up not one reference.

The RAM

The cartridge clears the 8 KB from 0xC000 to 0xDFFF at boot and uses them as workspace. The variables that matter most:

address what it holds
0xD12E my slot · 0xD12F the neighbour's
0xD130 the game's own interrupt hook, saved by the patch
0xD136 the operation code: high nibble what, low nibble on what
0xD300–0xD31x the header of the game that was recognised
0xD31A, 0xD31B the stage and the lives the user asked for
0xD317 which cheats are pending

And one that does two jobs: 0xD12D holds the PSG's channel C volume and is also the low byte of the IY used for inter-slot calls. It works because CALSLT only looks at IYh.

The hidden mark

In the last 22 bytes, 0x7FEA–0x7FFF: RC-735 and 19 katakana bytes reading 10バイタノシムカートリッジ — "the cartridge for ten times the fun" — which was the slogan.

The format was discovered by Manuel Pazos (@ManuelPazosMSX): the title reversed, its length, the catalogue number in BCD and a closing 0xAA. As a bonus, this mark confirms that 0xBA is the ー lengthener, which had been left open until now.

Before the mark there are 272 filler bytes of 0xFF (0x7EDA–0x7FEA): what was left over of the cartridge.

The code

10,522 bytes of traced code (64.2%) and 5,862 of data (35.8%), adding up to the cartridge's 16,384: 100% explained, zero bytes unassigned. 663 routines, 22.1% of instructions carrying a comment and not one routine below 10%.

How it gets inside the game

This is the central idea, and it lives at 0x4C76.

  1. It copies the game's first 256 INIT bytes to 0xD861, reading them from the other slot with RDSLT.
  2. It looks in that copy for the sequence 9B FD, which is the operand of ld (0xFD9B),hl — the instruction a game uses to install its interrupt routine into the BIOS's H.KEYI hook.
  3. It replaces it with the five bytes at 0x4CB9. With the 22 opcode already in front, the instruction goes from

    22 9B FD        ld (0xFD9B),hl
    

    to

    22 30 D1        ld (0xD130),hl   ; the game's hook, saved
    CD 14 D8        call 0xD814      ; and control to the Game Master
    
  4. And it runs the patched copy, not the original.

So it does not patch the game: it patches the RAM copy of its start-up. From then on it runs once per frame inside the game and can poke its variables.

It works across Konami's MSX1 catalogue because all those cartridges start the same way. Checked over the 67 cartridge ROMs in the collection: of the 60 by Konami, 53 carry that instruction in the 256 bytes of their INIT. The seven that do not are the six MSX2 cartridges (Vampire Killer, King Kong 2, Hinotori, Ganbare Goemon, Metal Gear and The Treasure of Usas) and the European version of the Game Master itself.

How it knows which game is next to it

Two routes, and the order matters (0x5E64):

1. The second header. It reads two bytes from 0x4010 of the neighbour and compares them with AB and CD. If it is AB it takes 19 bytes as they come; if CD, 21, and it distributes them according to a flag byte: each rra pulls out one bit saying whether the next field is present, and any field that is missing keeps the default from 0x6034. Of the 67 cartridge ROMs in the collection, 17 carry that header: 16 games (Soccer and Football are the same binary under another label).

2. The signature table. For everything else — which is most of the catalogue — 0x5EF5 adds up the 256 bytes from 0x5000 to 0x50FF of the neighbour's ROM and looks that 16-bit sum up in the table at 0x5F3A: 62 four-byte entries pointing at 29 stand-in headers of 19 bytes, for 28 games. It is 29 and not 28 because Antarctic Adventure needs two, one per pair of its own builds, and they differ in a single byte.

Either way the result ends up at 0xD300 onwards in the same format: the two BCD pairs of the catalogue number, then pointers to the game's variables in its RAM.

The cheats

The table at 0x5B02 has twenty-one three-byte entries — catalogue number and routine — and it is where whatever each game needs in particular lives. Nearly all of them end by jumping to 0x5D13, the universal three-instruction cheat.

What changes is the arithmetic. 0x5E0F divides by C through subtraction and shifts, and that C is how many stages the game has:

game by what it writes
RC-701 Antarctic Adventure 10 the track, at 0xE0E2, with two tables
RC-710 / RC-711 Hyper Olympic 4 the event, and eight bytes per cartridge
RC-713 Magical Tree 9 the level and the starting score
RC-717 Hyper Sports 2 3 the event
RC-718 Hyper Rally 13 the stage and its pair of bytes
RC-721 Sky Jaguar 8 not a stage: a position along the run
RC-727 King's Valley 15 the room, the lap and a pair from the table
RC-730 Road Fighter 6 the course, and it clears the screen

And two pairs share a routine: RC-710 with RC-711 — told apart inside by their number — and RC-700 with RC-716, that is Athletic Land and Cabbage Patch Kids.

The menus: three pieces, always the same

Thirteen menus, all built alike:

ld hl,<labels>       and `call 0x6294` paints them
ld hl,<slots>        where the cursor may sit, one row per line
ld b,<N>             how many lines it has
call 0x6581          picks one, and returns it in A
...
call 0x6368          dispatches
<table of N pointers>    ← GLUED to the call, inside the flow

0x6368 does a pop hl to grab the return address — which is the byte after the CALL — and reads word number A from there. The table sits inside the code. There are fourteen of them, and their sizes are not eyeballed: each one comes from the ld b,N that precedes the call 0x6581. tools/menus.py reads them.

The font and the formats

Code nobody reaches

Four fragments — 0x6445, 0x6787, 0x6904 and 0x6938 — disassemble to correct, well-fitted Z80: they start right after a ret and end right where a label that is used begins. But there is not one 16-bit reference nor one relative jump landing on them, searched across all 16,384 bytes.

Plus two two-byte doors (0x5022 and 0x6FB5), each a ld b,N put there to enter the neighbouring routine with a different value, that nobody jumps to.

It is not the mirage of reading an operand as an opcode, because they do not fall inside another instruction. They are leftovers from an earlier build: there are three of this cartridge.

In the emulator

Running it on its own

"C:/Program Files/openMSX/openmsx.exe" -machine Philips_VG_8020 \
    -cart gamemaster.rom

With no game beside it, the cartridge keeps to itself: it builds its title screen and waits. 0x4079 reads row 7 of the keyboard and, if that key is held, it will not enter the game even if there is one.

Running it with a game

Which is what it is for. Two slots:

openmsx.exe -machine Philips_VG_8020 \
    -cart gamemaster.rom -cartb <game>.rom

The Game Master has to sit in the slot before the game's: 0x402A works out the neighbour's by adding 4 to its own if it is in an expanded slot, and 1 if not.

Dumping the VRAM and checking the pictures

This is the check that decides whether the screens on this site are the cartridge's:

make vram

What it does underneath:

GM_SALIDA=work/omsx openmsx.exe -machine Philips_VG_8020 \
    -cart gamemaster.rom -script tools/omsx_vram.tcl
python3 tools/coteja_vram.py gamemaster.rom 0x4000 work/omsx

tools/omsx_vram.tcl sets no breakpoints: it dumps the 16,384 bytes of VRAM and the eight VDP registers at five moments on the emulated clock — at 8, 12, 16, 20 and 25 seconds — because between the BIOS booting and the slot sweep the cartridge performs, the title screen takes a while to appear. The first dump we tried, at 6 seconds, came out entirely black.

tools/coteja_vram.py compares byte for byte, picks whichever dump is closest, checks the VDP registers and splits the differences into two groups: those at animated addresses — declared, with whatever moves them — and the rest. If one turns up that is not on the list, it comes out in red with its address.

The two frames

The title screen is not still, which is why a check cannot always give zero:

Any given dump lands on one of the two frames. What is required is that everything else be zero.

Looking inside while it runs

The variables that say the most, in openMSX's console:

debug read memory 0xD12E    ; my slot
debug read memory 0xD12F    ; the neighbour's
debug read memory 0xD301    ; the catalogue number recognised
debug read memory 0xD317    ; which cheats are pending
debug read memory 0xD31A    ; the stage asked for
debug read memory 0xD31B    ; the lives asked for

If 0xD301 is 0xFF, the cartridge has recognised nothing: either there is no game beside it, or its sum is not in the table.

And to watch the moment it gets in:

debug set_bp 0x4C8F         ; just before the cpir looking for the 0x9B
debug set_bp 0x4C9B         ; and once it has found it

Between those two points, 0xD861 onwards holds the RAM copy of the game's start-up, still unpatched.

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, 30 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 155 2,447 lee_las_teclas_de_truco (0x4F13) = lee_teclas
Konami's Synthesizer 15 177 posicion_a_vram (0x7612) = nombre_del_punto
King's Valley II 9 81 posicion_a_vram (0x7612)
Gekitotsu Pennant Race 2 8 68 0x6957 = abre_fichero
Shalom: Knightmare III 8 80 intercambia_bloques (0x6871) = intercambia_ram
The Pro Yakyuu Gekitotsu Pennant Race 6 53 escribe_en_disco (0x6987)
The Maze of Galious 5 58 0x533B = columna_del_logotipo

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.