Under the hood

Getting started

What you need

pasmo and z80dasm to assemble and disassemble, and Python 3 for the tools. Nothing else.

The cartridge does not travel with this repository: put your own, named f1spirit.rom, in the root of the project. It is exactly 131072 bytes, with this sha256:

3b9afd12b29680abb655f05d24e0970954c38e48c5ccb24227d6c710474396a7

With any other dump the listing will not assemble back. make comprueba says so in one line.

What is different from a 16 KB cartridge

This is a 128 KB MegaROM with a Konami SCC mapper: sixteen 8 KB pages, each with its fixed window in the Z80 map. The disassembly treats every page as an independent module —its own org, its own trace, its own notes and its own listing— and make verify reassembles all sixteen and concatenates them: the whole ROM has to come out, byte for byte.

The commands

make          # traces, generates the sixteen listings and checks everything
make verify   # assembles the sixteen pages and compares the whole ROM
make sanity   # what reassembling cannot catch
make test     # the 31 tests on the listings, which do not need the cartridge
make externos # the calls each page makes outside itself
make web      # these pages

make chains the first four. If all is well, the line that matters is this one:

  ensamblado : 131072 bytes  3b9afd12...74396a7
  original   : 131072 bytes  3b9afd12...74396a7
OK: la ROM entera reproducible byte a byte

What make sanity checks

Reassembling proves the bytes are the same, but not that they were read correctly: data read as code produces exactly the same binary. Hence a second round of checks:

What is in each folder

   
src/f1spirit_pNN.asm the commented listings, generated; never edited by hand
src/pNN.notes each page's annotations: labels, comments, headers and data ranges, anchored to addresses
src/pNN.entries the entry points the trace cannot deduce, each with its justification
src/pNN.nocode the ranges the tracer must not read as code
tools/ the tracer, the listing generator, the checks, the track editor and the drawing tools
tests/ 31 tests on the listings, the annotations and the site
docs/ this site and the track editor
work/ whatever make leaves along the way

How to read the listings

Every line carries its address at the end, in the window where that page runs. The comments do not live in the .asm —which is regenerated— but in the matching .notes, anchored to addresses, so that they survive a re-trace.

Calls between pages come out as bare addresses (call 04b28h) because a page's listing does not know what is in the others. To find out where each one goes, make externos groups them by destination.

The cartridge

A 128 KB MegaROM

F-1 Spirit is not a 16 KB cartridge like the 1984 Konami titles. It is 131,072 bytes, sixteen 8 KB pages that the Z80 cannot see at once: the cartridge carries its own circuit, the Konami mapper with SCC, and the program keeps asking it to place the page it needs in each window.

There are four windows, and every page has a fixed one, the one its number gives it:

pages Z80 window
0 0x4000 (always mapped)
1, 4, 7, 10, 13 0x6000
2, 5, 8, 11, 14 0x8000
3, 6, 9, 12, 15 0xA000

Page 0 is the only fixed one: it holds the header, the interrupt, the bank switching routines, the joystick reading and everything the other pages need within reach at all times. The rest take turns.

That each page has only one possible window is not a convention of this disassembly: it is what the hardware does, and the project checks it on every build. make sanity walks every place where the program asks for a page to be mapped and verifies the destination matches the window it belongs to.

The header

4000  41 42        "AB", a cartridge signature
4002  E4 40        INIT = 0x40E4
4004  00 00 00 00 00 00   STATEMENT, DEVICE and TEXT at zero
400A  00 x6        padding
4010  43 44 07 52 FF      "CD" 07 "R" FF, Konami's signature
4015  C3 2E BE     jp 0xBE2E

The BIOS sees the "AB" at 0x4000, maps the cartridge and jumps to INIT. From there the program never returns: INIT installs a jump to 0x4018 in the H.KEYI hook and sits in an endless two-byte loop. The whole game runs inside the interrupt, just like the 1984 Konami cartridges.

RAM runs from 0xE000 to 0xF0FF —INIT clears all of it— with the stack at 0xF0F0, and F0F1, F0F2 and F0F3 hold the mapper shadow: which page is in each window. It is needed because the interrupt can fire at any moment and has to be able to leave the mapping exactly as it found it.

The interrupt, with two locks and a brake

The interrupt does the sound and calls the game logic. The problem is that the logic can take longer than one frame, and the bank switching routines end in ei, so the interrupt fires again while the logic is still running. Hence three mechanisms:

That last one is a fixed clock: the race advances one step every three interrupts no matter how long the code takes.

The screen

SCREEN 2, with the eight registers written by 0x4317 from the table at 0x4328:

register value what it says
R0 0x02 graphics mode 2
R1 0xE2 16 K, screen and interrupt 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 0xE4 ink 14 on background 4

In SCREEN 2 the screen is split into three eight-row thirds and each third has its own 256 patterns. The cartridge's tile loader loads all three at once: a list record carries three bits —one per third— and the same drawing is decompressed as many times as thirds are asked for.

The name table is not written directly. The game keeps two 1 KB buffers in RAM, one per player (0xE400 and 0xEC00), which are 32 × 32 circular maps: the track is painted there and then dumped to VRAM. Being circular is what lets the scroll move nothing at all: only the row it starts reading from changes.

The sound

Two chips at once: the machine's PSG and the cartridge's own SCC, which adds five more channels with programmable waveforms. The player lives in page 13 and runs once per interrupt.

Writing to the SCC is not free: you have to map page 0x3F at 0x8000, write, and put page 2 back. That is why the player does not write the fifteen values of the five channels on every interrupt: it compares them with a copy it keeps in RAM and only sends the chip the ones that changed.

The code

What is in each page

Of the sixteen pages, six carry code and ten are data:

page what it is
0 fixed at 0x4000: header, interrupt, mapper, joysticks, tile loader, track, pits, title screen
1 menus, car cards, ranking, HUD, starting grid, password
2 car physics, states, engine sound, objects, ending
3 the rivals: grid, overtakes, collisions, types
4 tile and sprite lists
5, 7, 10, 11, 14 graphics and metatiles
6, 8, 9, 12 drawings, texts and tables
13 the sound player
15 title-screen streams and one recorded game

The disassembly produces one listing per page, each with its own org, and make verify reassembles all sixteen and concatenates them: the whole ROM has to come out, byte for byte.

The track

Each circuit's track is a sequence of pieces. Each entry in the sequence is two bytes —the piece number and some flags— and a 0xFF closes the lap.

A piece is 24 bytes: three rows of eight metatiles. Each metatile is 4 × 4 tiles, i.e. 16 bytes, so a piece is twelve tile rows tall and takes the full width of the screen.

The position along the track is a 32-bit counter that goes down —the track comes towards the player— and every 0x800 is one eight-pixel row. After twelve rows the piece runs out and the next one begins.

Two details that show up on reading the code:

The signs and roadside props are not part of the pieces' drawing: they live in a separate per-circuit list, ordered by position, three bytes per entry —the piece in the top twelve bits, the row in the low four, and the drawing—. Because it is ordered, a single index that moves forward and never back is enough.

The tile loader

Nothing is copied to VRAM as-is. All the graphics go through a loader that walks lists in page 4. A list record is six bytes:

flags | tile | pointer to the patterns | pointer to the colours

and the flags say where the drawing comes from and what to do with it:

bits what they say
2-1 page group where the drawing lives: 4, 7, 0x0A or 0x0D
3 flip the tile vertically
4 mirror it horizontally
5, 6, 7 which screen thirds it goes to

On the other side of the pointers there are no loose bytes: there is an RLE decompressed straight into VRAM, eight bytes at a time, with the working tile in RAM. The codes are 00 ends, 01-7F repeats the next byte n times, 80 does nothing and 81-FF copies n bytes verbatim. Mirroring and flipping happen there, with the tile already assembled and before letting it go: they come for free.

Besides records, a list can carry orders:

order what it does
0x10 copies the 16 bytes behind it into a variable
0x11, 0x15, 0x19, 0x1D write a word into one of four slots
0x12 nothing (and without advancing the pointer: no list uses it)
0x13 nests one list inside another
0x17 loads a resource from the table of 85

And there is a row mode: loading one pixel row of each tile at a time, with a wait in between, makes the drawing appear top to bottom instead of all at once. That is the panel's entrance effect.

The sprites

Attributes are not written directly: there is a RAM copy of 32 sprites × 4 bytes dumped to the VDP on every repaint. On odd frames it is dumped backwards, from 29 to 0, with the last two apart: that is the priority rotation that makes the sprites beyond the four-per-line limit flicker instead of vanish.

Each car is two sprites in the same place: the pattern and the pattern plus four, in different colours. On a TMS9918 that is how you get a two-ink figure.

The car's frame comes from the angle, split into sixteen sectors. From F3 upwards the cars carry five extra frames —the wheels turning— and the lower categories do not: those cars never show them.

The rivals

Each player has five rivals, 0x38-byte records, and each one is served one frame in eight: its turn is written in its own record, so the five never fall on the same frame.

A rival looks at the track ahead —at 0xE8 and at 0x98 pixels— reading three cells of the row, and gets two things from it: whether a corner is coming, and where there is room. If a corner is coming, one of three lists is subtracted from its base speed, from the gentlest braking to the hardest.

Car-to-car collisions are resolved as a half-elastic impact: each car's new speed is the average of the two, plus or minus a quarter of the difference along the long axis and an eighth along the short one.

On the grid player 1 starts third and player 2 fourth, but the classification is longer than the six cars you can see: the number of cars that are ahead unseen in that race is added to the grid slot — 20 in the first race. That is why you start 23rd.

The sound

The page 13 player runs once per interrupt and serves two chips: the machine's PSG and the cartridge's SCC.

From the PSG it writes the three channels with their period and volume, the noise period, the three envelope registers and the mixer. From the SCC, the five channels, their 32-byte waveforms and its own mixer.

The expensive part of the SCC is reaching it: map page 0x3F at 0x8000, write, put page 2 back. That is why the player keeps a RAM copy of the fifteen values and compares them one by one; only the changed ones are flagged and only those get written. It is fifteen near-identical seven-instruction blocks, written out by hand with no loop: longer, but with no index arithmetic.

The effects in the scores are software envelopes: two volume phases, a four-stage vibrato and a note slide, all written into the score itself as orders.