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 worth believing.

What you need

The cartridge

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

6d36c9e9b6a6b93f642722dcd314834d01e519ab3a98d7e4ca257468bcaf29e1

The other build, needed only for make coteja, goes in as roadfighter_dificil.rom:

070fc231f979a04bb4652f7c5e585f6445758af32ffcbf60757deb1f7b5856e7

make comprueba verifies both.

What make does

make            traces, generates the listing, verifies and runs the tests
make trace      follows the flow from the entry points
make listado    writes src/roadfighter.asm
make verify     THE PROOF: reassembles and compares against the ROM
make sanity     what reassembly cannot catch
make densidad   how much is commented, routine by routine
make test       the tests
make web        generates this site

make verify is what decides whether the disassembly can be trusted. Until it is green, anything said about the cartridge is said blind.

The tests run without the cartridge

The tests do not need the ROM. tests/test_listado.py rebuilds the data bytes by reading the defb and defw rows of the listing itself, which carry their address in the comment. That way the decompressor, the sign interpreter, the patch paster and the road generator can all be executed in a bare clone, with no cartridge and no make.

How it is laid out

src/roadfighter.asm       the listing, generated
src/roadfighter.notes     WHAT IS UNDERSTOOD: the comments come from here
src/roadfighter.entries   the entry points that cannot be deduced
src/roadfighter.nocode    what is not code even though it looks like it
tools/                    the tools
tests/                    what keeps this from decaying

The .asm is regenerated: it is not edited. What gets edited is the .notes, which is why the comments survive a re-trace.

The cartridge

RC-730, 16,384 bytes, in page 1 of the MSX. It is a plain ROM cartridge with no mapper: the whole thing is visible from 0x4000 to 0x7FFF and there is nothing to switch.

The header

The first sixteen bytes are the header the MSX looks for at boot: "AB", the INIT address (0x404F), and STATEMENT, DEVICE and TEXT at zero, plus six reserved bytes that are zero too.

Everything hangs off the interrupt

INIT does not start a game loop. What it does is hook H.KEYI with 0x4013 and settle into a jr $. From there on the whole game runs inside the interrupt hook, once per frame.

The hook carries its own reentry latch at (0xE005), because a slow frame can catch the previous one still running.

The memory map

0xE000   the scene, and 0xE001 the step within it
0xE002   the game flags (bit 6 is "game in progress")
0xE003   the game clock, one per frame
0xE043   the stage, 1 to 6      0xE042  the lap
0xE046   where the road ring is being read from
0xE049   the car's state        0xE04B..0xE04C  its Y in fixed point
0xE04D..0xE04E  its x           0xE04F  the speed
0xE058..0xE06D  THE NEW ROW: 22 columns
0xE078   distance covered in the stage, 16 bits
0xE083   the fuel
0xE098..0xE0AF  the verge trail: one cell per ring row
0xE0E5   the road objects, 16 bytes each
0xE10E..0xE185  the RAM copy of the thirty sprite attributes
0xE186..0xE395  THE RING: 24 rows of 22 columns

That 0xE10E plus 0x78 lands exactly on 0xE186 is no accident: the sprite attribute copy ends precisely where the ring begins, which is why the two uploads the cartridge makes to VRAM -0x34 bytes and 0x78- fit with no gap.

The screen

SCREEN 2, with the tables somewhere unusual. The eight bytes the cartridge loads into the VDP registers, from the table at 0x46A9, read 02 E2 0E 7F 07 76 03 E4:

register value what ends up where
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 0xE4 on the title screen; 0xE0 in play, with a black backdrop

Colours below patterns, the wrong way round. R3 and R4 are not addresses but a base and a mask, and reading them as if they were gives a misleading result: the shapes come out fine and the colours come out in stripes.

The dispatcher

The cartridge hands out work through a nine-instruction dispatcher at 0x4045 that eats its own return address:

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

The pop hl recovers the address it was going to return to, which is exactly where the table starts, because the table sits right behind the call. There are six such tables: 0x4099 (the scenes), 0x50C9 (scenery per stage), 0x5526 (road per stage), 0x570C (the roadside scenery), 0x6A80 (the car's states) and 0x70C9 (an object's states).

None of them can be deduced by following jumps, so they are declared by hand in src/roadfighter.entries, each with its justification written alongside.

The nine scenes

The index is (0xE000) and the step, (0xE001). Every scene opens with a row of chained djnz: the step is not compared against anything, it is spent.

scene what it is
0 boot and title screen
1 the wait
2 winding up a game
3 the longest: the menu, building the road and starting up
4 the countdown, with the road already rolling
5 the game
6 the crash
7 GAME OVER
8 back to the title screen

The table's tenth word would be 0x1310, which does not land in the ROM: those two bytes are already the djnz at 0x40AB, which happens to be the table's own first destination. That fit is what pins down the end of the table without having to assume it.

The hidden mark

The last fifteen bytes are read by nobody. They are RC-730 and the title in katakana, ロードファイター, written backwards and closed with the length, the two RC digits in BCD and an 0xAA. The format of that signature was discovered by Manuel Pazos.

The code

7,797 bytes of code and 8,587 of data. The two add up to the cartridge's 16,384, and that sum is the check: not one byte unaccounted for.

The ring, which is the whole idea

The road does not scroll. What moves is where you start reading.

At 0xE186 there is a ring of twenty-four rows by twenty-two columns. The routine at 0x5716 steps the pointer at (0xE046) back twenty-two places -one row- and drops the row just built into the gap. When the pointer falls below 0xE170 it goes back to 0xE380, which plus twenty-two gives 0xE396, the end of the ring.

The limits fit on their own, which is why they can be trusted: 0xE186 minus 22 is exactly 0xE170, and 0xE380 plus 22 is exactly 0xE396.

Then 0x573A dumps the twenty-four rows into the name table starting at 0x3801 and skipping 0x20 per row, because the screen is thirty-two columns wide and the road only takes twenty-two. The ones left over on the right are the dashboard.

One new row, step by step

0x551D  the road engine
  0x592A  fills the twenty-two column row with the background tile
  ...     THIS stage's branch paints the road on top
  0x58AF  every eight rows, pastes a road segment
  0x56F7  the roadside scenery, through one of three doors
  0x590E  records this row's verge in the trail
  0x5944  the finish sign, if it is due
  0x54AF  the loose pieces from the scenery script
  0x5716  and the row goes into the ring

The six roads

The table at 0x5526 is indexed with (0xE043) without subtracting one, and the stages run 1 to 6: entry 0 is used by nobody and therefore repeats stage 1's.

stage what its branch does
1 just the lane markings
2 pastes four columns from a cyclic list
3 pulls the whole row out of a sixteen-bit word, and the bridge
4 stores seven columns and computes the mirror by adding 0x0D
5 the same with four columns and another table
6 like 3, and also redraws the markings on top

All six converge on the same ending, which finishes with a pop hl at 0x5543: it eats the return address so as to exit straight to whoever called the dispatcher, skipping a level.

Stage 3's symmetrical road

The routine at 0x56CC pulls tiles out of a sixteen-bit word, one per bit, starting from the most significant. It is asked for twelve columns and then ten more.

But the second call enters at 0x56CE, which reloads the word from the pointer, and the pointer has not moved. So columns 12 to 21 repeat the pattern of the first ten. The road comes out symmetrical without spending one extra byte.

The mirror in stages 4 and 5

Here half the road is stored and the other half computed. The seven bytes of the left side are read again, 0x0D is added to each -that being the jump from a tile to its mirrored version in the pattern table- and they are written backwards from the end of the row.

Stage 3's bridge

Four columns from the script, seven of tile 0xD0 in the middle and another four from the script, from column 4 to 18. The seven in the middle are not written one by one: 0xD0 goes into the first and an ldir that overlaps itself drags it to the end. That is why there are seven and not the six the ld c,006h says.

And two rows in four carry a 0xD1 in column 11, which is the dashed centre line.

The verge trail

At 0xE098 there are twenty-four cells, one per ring row, each holding the verge margin in the top six bits and the scenery mode in the bottom two. Every new row shifts them all along one place and pushes its own in at the top.

It is the memory of where the road was at each height of the screen. Without it, an object twenty rows into its fall would not know where the edge is at its height, and the car itself would not know where to reappear after a crash.

The car

0xE049          the state, and a table of eight branches at 0x6A80
0xE04B..0xE04C  the Y, in 8.8 fixed point
0xE04D..0xE04E  the x, in the same format
0xE04F          the speed, capped at 0xD7

The eight states: rolling, wrecked and burning, burning fuel, leaving the screen, skidding, spinning, waiting, and bouncing off the verge.

The Y's high byte is, as it stands, the sprite's Y in VRAM. That is not a guess: across nine hundred frames of the attract mode the two matched without a single exception.

The objects

An object is sixteen bytes, and two are live at a time. Spawning goes by segments: a pointer walks the sixteen-byte row belonging to the stage in the plan at 0x537D, and each value indexes a table of fifteen records. Each record carries six bytes, that is two objects of three fields, which are assembled in two slots and copied whole from there into the first free hole.

All access to an object goes through two routines: 0x72F6, which fetches field C, and 0x72E7, which adds DE to the word ending at field C. Hence the ld c,006h -the Y- and the ld c,009h -the speed- that turn up everywhere.

The decompressor

The graphics are packed with an RLE that writes straight into VRAM, with out (c),a on the VDP data port:

0x00           closes the block
0x01..0x7F     the byte that follows, repeated that many times
0x80           the next two bytes are a new VRAM address
0x81..0xFF     copy verbatim the (command & 0x7F) bytes that follow

There are two doors: through 0x4611 the destination arrives in HL, and through 0x460B it lives inside the block itself, in its first two bytes. And 0x45E0 calls the decompressor three times adding 0x800 to HL, which is the geometry of SCREEN 2: one block fills all three thirds.

The bounds of the twenty-two blocks are not estimated: tools/rle.py measures them by running that same decompressor, and each one closes on its own 0x00.

Three overlapping blocks

Three of those blocks do not end where the next one starts: they end after. The cartridge reuses the tail of one as the body of the one behind it.

block closes at what it shares
0x5DEC 0x5F45 its last 32 bytes are the head of the block at 0x5F25
0x601D 0x6022 it swallows the block at 0x601F whole
0x602D 0x603E it swallows the block at 0x6031 whole

This is not a misreading: all three close on their own 0x00 and all three match the emulator's VRAM.

In the emulator

Looking at a picture is not enough to call it right. What settles it is dumping the machine's memory and comparing it, byte for byte, against what the notes claim. Everything this site says about what the cartridge does while running is measured that way.

The scripts

tools/ holds four openMSX scripts. None of them sets a breakpoint except the one that needs to: the rest work off emulated time, which is the only thing that does not choke the emulator.

tools/omsx_vram.tcl       dumps the 16 KB of VRAM and the VDP registers
tools/omsx_coche.tcl      records the car's motion, frame by frame
tools/omsx_carretera.tcl  dumps the road generator's state
tools/omsx_etapa.tcl      the same, forcing which stage is played

Run them like this:

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

The graphics, against VRAM

tools/graficos.py rebuilds the screens by running the cartridge's own steps in Python: the decompressor, the sign interpreter, the mirrors and the patch paster. Then it is compared against what the VDP actually holds.

TITLE SCREEN     colour, sprites, pattern and names: 0 differences in all four
GAME SCREEN      colour 0/6144   sprites 0/2048   pattern 0/6144
                 names 2/528

The two cells that do not match are the finish arch, row 4, columns 6 and 7, which is animated: it alternates 0x46/0x47 with 0x48/0x49. So the two that fail, fail because they have to.

The road, step by step

This is the check that costs the most and is worth the most. The road is procedural, so comparing a lone dump says nothing: there is no way to know where in the script it was.

What does work is this. tools/omsx_carretera.tcl records the entire state of the generator -the variables from 0xE040 to 0xE0C0 and the 528-byte ring- on consecutive frames. Then, in Python, the state from frame N is loaded, one step of the rebuilt engine is run, and the result is compared against frame N+1.

The result, with each of the six stages forced in turn:

stage steps compared identical
1 44 44
2 44 44
3 44 44
4 44 44
5 44 44
6 43 43

Identical means the entire ring, all 528 bytes, and every generator variable. Across the 263 steps, not one difference.

That check turned up three misreadings that only show this way: that the routine at 0x5784 begins with an ldi of its own, that the bridge's ldir overlaps itself and drags seven cells rather than six, and that the alternation between the two scripts was the wrong way round.

Forcing a stage

The attract mode picks the stage using the R register, and on a clean openMSX boot the draw always comes out the same: stage 2. To see the other five it has to be forced, and where matters.

tools/omsx_etapa.tcl puts a breakpoint at 0x504C, the first instruction of prepara_la_etapa, and writes (0xE043) before the routine builds anything. That way the scenery, the scripts and the lists are all chosen for the forced stage and the state stays coherent.

Forcing it mid-game does not work: the scenery stays as the previous stage's and the screen fills with garbage. Reading the segment plan does still work -it depends only on (0xE043) and the pointer- but the captures do not.

The car

tools/omsx_coche.tcl records, nine hundred times, the car's Y alongside the accelerator and the speed. Three things come out of it:

How to play it

Any MSX1 emulator will do. With openMSX:

openmsx -machine Philips_VG_8020 -cart roadfighter.rom

And to see the other build, the same command with roadfighter_dificil.rom. The difference is in stage 5, and it is one segment.

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)
Konami's Ping Pong 22 258 descomprime_con_destino_dentro (0x460B) = descomprime
Konami’s Soccer 22 272 lee_el_mando (0x46C3) = lee_el_mando_1
Yie Ar Kung-Fu II 21 245 descomprime_con_destino_dentro (0x460B) = guion_rle
Knightmare 20 244 lee_el_mando (0x46C3) = lee_mando_y_teclado
The Goonies 19 247 lee_el_mando (0x46C3) = lee_mando_y_teclado
Hyper Sports 3 19 215 descomprime_con_destino_dentro (0x460B) = descomprime_a_vram
King's Valley 17 218 init_del_cartucho (0x404F) = arranca_el_juego
Yie Ar Kung-Fu 17 162 escribe_rotulo (0x45F0) = dibuja_la_lista
Nemesis / Gradius 16 199 descomprime_con_destino_dentro (0x460B) = descomprime_con_destino
Konami's Boxing 15 143 escribe_rotulo (0x45F0) = pinta_texto

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.