Under the hood

Getting started

This repository does not ship the game. It ships the commented disassembly of Mopi Ranger (Konami, 1985, RC-728) and the tools that reproduce it.

What you need

The check that decides

make comprueba      # that the dump is the same one
make                # trace, build the listing, reassemble and check

make ends with this, and it is the only line that matters:

OK: reproducible byte a byte

It means that the listing in src/mopiranger.asm, assembled, gives back the same 16,384 bytes as the cartridge. If that fails, nothing else counts.

What each target does

target what it does
make trace follows the flow from the entry points in src/mopiranger.entries
make listado writes src/mopiranger.asm from the trace and the notes
make verify assembles and compares the sha256 with the original
make sanity what reassembly does NOT cover (see below)
make test the tests
make densidad how much of the listing is commented
make mapas draws the zones by unpacking them
make web builds these pages

Why verify is not enough

A listing can reproduce the ROM byte for byte and still be telling lies: bytes do not change because you read them wrong. What catches that is make sanity:

The files

src/mopiranger.entries   the entry points, each with its reason
src/mopiranger.nocode    the ranges that are NOT code
src/mopiranger.notes     what is understood: data blocks and comments
src/mopiranger.asm       the listing, generated
tools/                   the tracer, the generator and the drawing tools

The .asm is generated: what you edit is the .notes, because the comments are anchored to addresses and so survive a re-trace.

The cartridge

Mopi Ranger is a 16 KB MSX1 cartridge, Konami catalogue number RC-728, from 1985. It maps into page 1, 0x4000 to 0x7FFF.

The header

The first ten bytes are the header the BIOS reads:

41 42 10 40 00 00 00 00 00 00
A  B  INIT  STATEMENT DEVICE  TEXT

The AB is the signature that says a cartridge is there. Of the four addresses, only INIT is filled in, at 0x4010; the other three are zero.

Everything hangs off the interrupt

INIT does not start the game: it hooks it and then stops.

di / im 1
ld a,0c3h / ld (0fd9ah),a       a `jp` into H.KEYI
ld hl,04043h / ld (0fd9bh),hl   and the handler behind it
ld sp,0e400h                    the stack
ld hl,0e000h ... ldir           clears the kilobyte of variables
...
ei
jr $                            and here it stays forever

From that jr $ on, the whole game runs from the interrupt. That matters when disassembling it: with only INIT declared as an entry point, the trace stops at 1.3% of the cartridge.

The memory map

where what
0x4000-0x400F the header
0x4010-0x5C75 the code
0x5C76-0x5DB7 the text and the attract-mode script
0x5E0E-0x5FB7 patterns, compressed
0x6035-0x64B4 the fifty zones: pointers and data
0x6522-0x6EF2 the twenty-five maps, compressed
0x6EF3-0x71F6 the 193 metatiles
0x722B-0x7AD3 patterns and colours, compressed
0x7C9D-0x7FC8 the sound scripts
0x7FC9-0x7FF3 padding
0x7FF4-0x7FFF Konami's hidden mark

In RAM, the variables live from 0xE000 to 0xE3FF with the stack just above.

The screen

The cartridge loads its eight VDP registers from a table at 0x45F7, which reads 02 E2 0E 7F 07 76 03 E0:

register value what it says
R0 0x02 SCREEN 2
R1 0xE2 display on, interrupt enabled, 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 0xE0 border and background

The colours sit below the patterns, the other way round from usual. R3 and R4 are not addresses but base and mask, and reading them backwards gives correct shapes with striped colours.

Konami's hidden mark

The last twelve bytes are the mark Manuel Pazos (@ManuelPazosMSX) found in 2021: Konami hid the catalogue number and the title in katakana in many of its MSX cartridges. The format, reading forwards:

[title, N bytes, IN REVERSE ORDER] [N] [the two RC digits in BCD] [0xAA]

Here:

BA B1 B7 8B AC A9 B8 9A A2   09   28   AA

Nine title bytes, the 0x09 saying how many, the 0x28 of RC-728 and the 0xAA that closes it. The title, read back, is モピレンジャー -Mopi Renjaa-, with the handakuten and the dakuten as separate characters.

This cartridge does carry it, and that cannot be assumed: of the twenty or so cartridges by the same company disassembled in this series, fewer than half have it. It has to be hunted for across the WHOLE ROM and not just at the end, because it can end up sitting in front of a block of data.

The code

Of the cartridge, 7,789 bytes are code and 8,595 are data. That is 3,802 instructions across 512 named routines.

The shape of the program

Everything hangs off the interrupt. Each frame comes in at 0x4043, which carries its own latch so it cannot re-enter if a frame takes longer than it lasts, and from there goes out to the scene dispatch.

interrupt (0x4043)
  +- scene dispatch (0x40AC), on (0xE000)
       +- 9 scenes: title, attract mode, play, start zone,
          lose a life, end of zone, tally, game over, wait

The dispatcher, and why the tables are never loaded

The index dispatch for the whole cartridge is this routine:

despacha_por_indice:
    pop hl          ; the return address: the TABLE itself
    call 0x50F2     ; A times two, HL += A, DE = word[HL]
    ex de,hl
    jp (hl)         ; and jump

The pop hl recovers the return address, which is the table sitting right behind the call. That is why this game's dispatch tables are never loaded into a register: they are written inline, in the gap between the call and the code that follows.

For the disassembly that matters: a jp (hl) cannot be followed statically. Without declaring those two tables -the nine scenes at 0x40DC and the five substates at 0x4735- the trace stops at 5.7%.

Substates, with no table at all

Inside a scene there is no table: there is a chain of djnz.

escena_de_juego:
    djnz L_41AC        ; if B != 1, on to the next substate
    ...                ; substate 1
L_41AC:
    ...                ; substate 2

Every djnz that does not jump eats one of B, so the code left at the end is the highest substate's. For five or six cases that is shorter than a table, which is why there is not one here.

The house compressor

A single RLE format, with two routines that read it:

[control] bit 7 says literal or run,
          the other seven are the count
[bytes]   the payload: C bytes if literal, one if it is a run
0x00      closes

Neither carries a length: the trailing 0x00 marks it. The honest way to find where a block ends is to run the decompressor, and that is what tools/rle.py does.

The maps: 2x2 metatiles

Each zone is 120 unpacked bytes, and each byte is a metatile: a 2x2 block of cells whose four indices live in the table at 0x6EF3, four bytes per entry. The loop at 0x64CD writes two cells, adds 0x20 -a whole row of the name table- and writes two more.

Twelve metatiles across by ten down. Those two figures do not come from looking at the picture: they are the cp 00ch and the cp 00ah the loop counts with.

Movement, square by square

Everything that moves shares one engine. When something squares up on a cell -both coordinates multiples of eight- the routine at 0x52DC reads off the screen the patterns of the surrounding cells and stores them in its block. Everything after that -can it turn, is there floor, is there a ladder- is answered from those bytes.

Turning ninety degrees requires being squared up; a U-turn is always allowed. And on turning, the position is dragged to the multiple of eight one pixel at a time in the direction of travel rather than rounded: that is why turns feel glued to the grid.

The sound

Three PSG channels, and effects carry a priority: a new one does not get in if what is playing has an equal or higher number. Each effect takes two channels, with its script pulled from the table of 27 pointers at 0x7CA9.

The routine that requests an effect saves every register, IX and IY included, and disables interrupts: it is called from anywhere in the game and the handler touches the PSG too.

How to read this listing

Routine names are descriptive, and the comments are anchored to addresses in src/mopiranger.notes rather than written into the .asm. The listing is commented at 53.0% and no routine is left below 10%.

In the emulator

How to see what this disassembly describes actually running. Everything here uses openMSX, which has a debug console you can use to stop the game at an address and look at memory.

Starting it

openmsx -machine "Philips_VG_8020" -cart mopiranger.rom

Seeing the 5730-point prize

It is the easiest thing to check and the most satisfying. You need to reach a bonus stage -zone 5, 10, 15...- and stand on the exact square. From the console you can go straight there:

debug set_breakpoint 0x4931 {} {puts "5730 points!"}

That breakpoint fires right on the ld de,05730h. If it triggers, the KONAMI 5730 PTS caption is about to appear.

And rather than hunting for the square by hand, you can watch where the protagonist is while you play:

debug read_block memory 0xE130 2

First byte is X, second is Y. You want them at 0x90 and 0x39.

Seeing that the attract mode is a script

Leave the game sitting on the title screen until the attract mode starts, then:

debug read_block memory 0xE342 77

Those 77 bytes are the recorded keypresses. You can follow the pointer:

debug set_watchpoint read_mem 0xE342 {} {puts "reading the script"}

And the sharp test: change a byte of the script live and watch the attract mode do something else.

debug write_block memory 0xE342 0x08

Seeing the VDP registers the other way round

To check first-hand that the colours sit below the patterns:

debug read_block "VDP regs" 0 8

Out come the eight bytes 02 E2 0E 7F 07 76 03 E0. The fourth (R3=0x7F) is the colour table and the fifth (R4=0x07) the pattern table.

Watching the sprite table rotate

debug set_watchpoint write_mem 0xE057 {} {puts [format "%02X" [debug read memory 0xE057]]}

0xE057 is the offset the sprite dump starts from each frame. It climbs by sixteen and wraps at 0x80.

Dumping video memory to check a drawing

This is what separates "the picture looks right" from "the picture IS right". Dump the VRAM and compare it byte for byte against what the tools in tools/ draw:

debug save_memory VRAM vram.bin

On the Python side, tools/mapas.py builds the same VRAM by running what the cartridge does. If the two match, the format is properly understood.

A warning about watchpoints

Putting a watchpoint over a large range with an expensive callback freezes the emulator. What works is to start with a narrow probe, measure the write rate, and only then decide whether the whole range is affordable.

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, 52 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)
The Goonies 18 178 avanza_la_cortinilla (0x46F5) = baja_un_paso
Yie Ar Kung-Fu 18 247 suma_al_marcador (0x43B2) = suma_a_la_puntuacion
Knightmare 16 173 manejador_de_interrupcion (0x4043) = cada_cuadro
Sky Jaguar 16 211 suma_al_marcador (0x43B2) = SUMA_PUNTOS
Yie Ar Kung-Fu II 16 145 avanza_la_cortinilla (0x46F5) = baja_un_paso
Hyper Sports 3 14 154 manejador_de_interrupcion (0x4043)
Konami's Boxing 13 118 avanza_el_guion (0x7B35) = repite_un_trozo
Hyper Rally 12 121 manejador_de_interrupcion (0x4043) = INT
King's Valley 12 141 suma_al_marcador (0x43B2)
Konami's Ping Pong 12 111 avanza_la_cortinilla (0x46F5) = sube_el_rotulo

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.