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 is not distributed with this repository: you need your own copy, named timepilot.rom in the project root. It is exactly 16384 bytes with this sha256:

183e80262301b18d41762d64a2fc326f4a4bef17832109225637e184d54a70d9

With any other dump the listing will not reassemble. make comprueba tells you in one line.

The commands

make          # trace, generate the listing and check everything
make verify   # assemble the listing and compare its sha256 with the cartridge
make sanity   # what reassembly cannot catch
make test     # the tests on the listing, which do not need the cartridge
make imagenes # redraw the reconstructed pictures
make web      # the pictures and these pages

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

  ensamblado : 16384 bytes  183e8026...4d54a70d9
  original   : 16384 bytes  183e8026...4d54a70d9
OK: reproducible byte a byte

What is in each folder

   
src/timepilot.asm the commented listing, generated; never edited by hand
src/timepilot.notes the annotations: labels, comments, headers and data ranges, anchored to addresses
src/timepilot.entries the entry points the trace cannot deduce, each with its justification
src/timepilot.nocode the zones the tracer must not read as code
tools/ the tracer, the listing generator, the checks and the drawing tools
tests/ the tests on the listing and the notes
medidas/ the raw output of the openMSX measurements
docs/ this site
work/ what make produces along the way

How to read the listing

Every routine has an uppercase name and, where it matters, a comment saying what it does. Data blocks are labelled DATA_<use>, with the width of their structure and an explanation of what they are and how that is known. Addresses are the real ones of the cartridge in page 1: 0x4000-0x7FFF.

To change anything, edit src/timepilot.notes and run make again: the listing is regenerated and the checks say whether it still holds.

How it was done

The tracer (tools/z80trace.py) follows the flow from the header's entry point and from what timepilot.entries declares. What cannot be deduced on its own here is eight dispatch tables: Time Pilot does not put the table right after the call, the way other cartridges do; it loads it with a ld hl,nn and jumps through a jp (hl) that lives somewhere else. Each one is declared, with the instruction that loads it alongside.

Whatever is not code is left as a gap, and every gap is closed by finding the instruction that reads it (tools/quien_apunta.py) and checking that the format matches what the consuming code does with it.

What cannot be read is drawn. tools/graficos.py uploads to a make-believe video memory exactly what the cartridge uploads, at the same addresses, and builds the screen from its own label lists: if the reading were wrong, it would come out as noise.

The tools that came out of this

   
tools/densidad.py measures, routine by routine, how much of the listing carries a comment, and lists the ones below the bar
tools/valida_c.py catches comments anchored mid-instruction, and duplicates
tools/quien_toca_la_ram.py for each RAM byte, which instructions name it and from where. This is what caught the errors in the RAM map
tools/quien_apunta.py who reads a hole in the data, which is how holes get closed
tools/marca_konami.py looks for Konami's hidden mark at the end of the ROM
tools/graficos.py rebuilds video memory and draws the screens
tools/omsx_*.tcl the measurements in openMSX, told in In the emulator

Reproducibility

The cartridge

The header and the machine

The first eighteen bytes are the header the BIOS reads:

4000  41 42        "AB", a cartridge signature
4002  03 42        INIT = 0x4203
4004  00 00 00 00 00 00   STATEMENT, DEVICE and TEXT set to zero
400A  00 x8        padding

With the header at 0x4000 the BIOS maps the cartridge into page 1 (0x4000-0x7FFF) and jumps to INIT once it has finished booting. INIT writes jp 0x4037 into the H.KEYI hook (0xFD9A), clears the RAM from 0xE000 to 0xE7FE and leaves the stack right behind it, at 0xE7FF. From there the work is split: the main program runs the game —scoreboard, lives, change of era— and the interrupt moves everything you can see.

With 0xE000-0xE7FF of RAM, a 16 KB MSX is enough.

Video memory

SCREEN 2, with the eight registers at 0x4D05:

register value what it says
R0 0x02 graphic 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 0xE1 ink 14 on background 1

In SCREEN 2 the screen is split into three thirds of eight rows, and each third has its own 256 patterns and its own colours. Time Pilot loads all three the same: it uploads the first and then copies it twice with COPIA_VRAM_A_VRAM (0x4C97), which reads and writes byte by byte with a 0x800 step. Since the second third is already written when it gets there, the copy propagates to the third one on its own.

The play area is the first 24 columns and 24 rows; the scoreboard goes in the top row and from column 24 on.

The characters

The title and menu characters

Characters are uploaded with a list of blocks (0x4BF8): two bytes with the VRAM address, one with how many are coming and the data behind. The title and the menu are written with characters 0xC3 on, and the game font —the letters and the digits— with 0xDC on.

The odd part is that they are the same bytes: 0x798B is uploaded twice, once as the end of the menu block and once as the start of the font (0x427A), and the same happens with the digits at 0x79D3 (0x45E9). In SCREEN 2 colour goes per character, so having the same drawing under two numbers is what allows the same alphabet in two colours without taking up twice the room.

The first sixteen characters are not letters: the first eight are the eight drawings of the player's shot, the ninth is the little ship the lives are painted with (0x6A85), and then come the mark for an enemy still to go —different in each era (0x6AD0)— and the cell underneath the plane (0x6AF0).

The first sixteen characters

The sprites

The attribute table is 32 sprites, and the working copy lives in RAM (0xE380): the interrupt uploads it whole to VRAM on every frame (0x40CA). At the start of each era, seven groups of three bytes (how many, pattern, colour) hand out the 21 sprites needed —the plane, the enemies, the shots and the labels (0x4DEB)— and park them all off screen, at Y = 0xD1.

The plane is the only one that changes drawing: its sixteen patterns are at 0x6F2B and only the one in use goes up to VRAM.

The sound

Three channels of eight bytes at 0xE020, 0xE028 and 0xE030, and a player (0x4160) that gives each of them one step per frame. A sound program is a string of three-byte notes: the fine period, the coarse period with the volume in the top nibble and bit 3 for noise, and the duration. With bit 7 of the duration set, the note fades out on its own, step by step. An 0xFF ends the program.

A channel is quiet when the high byte of its pointer is zero, and to shut it up at once it is enough to point it at an 0xFF: that is exactly what 0x5C8F does with the one that closes the program at 0x7E40.

The RAM map

address what it holds
0xE000/0xE001 game mode and which player is playing
0xE002/0xE003 each player's lives
0xE005/0xE006 next extra-life step
0xE00B-0xE013 both players' scores and the record, in BCD
0xE018 the wait: the interrupt counts it down once every 32 frames
0xE019 the frame counter everything hangs off
0xE01F the phase of the interrupt's cycle (0-5)
0xE020-0xE037 the three sound channels
0xE120/0xE122 enemies still to shoot down, per player
0xE121/0xE123 how many are on screen right now
0xE126/0xE127 how many have come out since the life started
0xE130-0xE134 when the next group comes out, and where from
0xE145/0xE146 the plane's state and the direction it flies in (0-15)
0xE180-0xE183 each player's era and round
0xE200-0xE20F the end-of-era big machine
0xE210-0xE22F the nine clouds
0xE230-0xE24F the eight shots, four bytes each
0xE260-0xE29F the era 1 bombs and the enemy shots
0xE2A0-0xE2CF the missiles and the passenger
0xE2D0-0xE2FF the seven enemy slots, sixteen bytes each
0xE380-0xE3FF the RAM copy of the sprite table
0xE400-0xE4A0 the workshop where characters are shifted

The player's plane is not on this list, and that is not an oversight: it has no slot because it does not move. It is nailed at Y = 0x5C, X = 0x54 (0x5408); what moves is everything else.

What it is made of

  bytes  
code 8911 54.4%
data 7473 45.6%
unidentified 0  

The data, from the inside:

  bytes  
the seventeen sound programs 1,179 they take up almost all the end of the cartridge
the big machine of the five eras 1,113 its drawings, the character map and the smoke
each era's own characters 926 the sprite patterns of its enemies
the title, the menu and the font 753  
the common sprite patterns 617 the ones that work for every era
the sixteen drawings of the plane 512 32 bytes each
the clouds 542 the drawings, their shifted copies and the tables
tables and label lists 1,605 the years, the collisions, the directions, the dispatches
padding at the end 226 0xFF from 0x7F1E to the last byte

That padding at the end has its point: it is exactly where other Konami cartridges hide their mark —the catalogue number and the title in katakana—, a detail documented by Manuel Pazos. Time Pilot does not carry it: from 0x7F1E to 0x7FFF there is nothing but 0xFF.

The code

Almost nine thousand bytes of Z80 with 593 labels and the framework Konami kept reusing in its cartridges: the interrupt carrying whatever moves, a dispatcher that jumps through tables, and a three-channel sound player.

The interrupt splits the work

INIT hooks jp 0x4037 into H.KEYI and from then on there are two programs running: the main one runs the game —the scoreboard, the lives, the change of era, the GAME OVER— and the interrupt moves everything you can see.

The interesting part is that it does not do the same thing on every frame. 0xE01F counts from 0 to 5 and the six-entry table at 0x4118 says what is due:

frame what moves
0 the shots, the clouds, the plane and the collisions
1, 3 and 5 the enemies
2 the scrolling background
4 the big machine and the stage count

Three frames out of six go to the enemies, which are what there is most of. And at the end of the interrupt the VDP status is read again: if another one has arrived in the meantime, it takes one more step without leaving (0x410B), so a long frame does not swallow the next one.

That split has been measured in the emulator and it comes out even: between 8.8 and 11.1 milliseconds per phase. The numbers are in In the emulator.

The dispatchers

Time Pilot does not put the table behind the call, the way other cartridges from the same house do. The pattern here is:

	ld hl,<table>
	call 0x5961        ; hl = table[A]
	call 0x415E        ; jp (hl)

0x5961 is five instructions —rlca, add A to HL, read the word— and 0x415E, 0x554E and 0x6696 are three loose jp (hl) that act as a call (hl). There are eight tables like this: the six entries of the interrupt split, the sixteen directions of flight, the sixteen of the shot, the eight enemy behaviours, the eight starts, and three more of eight steps.

Since the table is loaded with an ld hl,nn and the jump is somewhere else, a static trace cannot follow them: all eight are declared by hand in src/timepilot.entries, each with the instruction that loads it.

The sixteen directions

Everything that flies carries a direction from 0 to 15 —0 is up, 4 is right— and a two-component speed. Moving is jumping to the routine for its direction (0x52D9 on): each one adds or subtracts the two components to the position, in BCD, with a daa after each addition and the carry passing to the cell next door.

The player's plane is the only one that turns gradually: the controls ask for a direction and 0x53BB walks it there one step at a time, the short way round. The rest change course all at once.

The actors

where what it is how many bytes per slot
0xE200 the end-of-era big machine 1 16
0xE210 the background clouds 9 —
0xE230 the player's shots 8 4
0xE260 the era 1 bombs 4 3
0xE270 the enemy shots 6 7
0xE2A0 the missiles of eras 3, 4 and 5 4 9
0xE2CE the passenger 1 2
0xE2D0 the enemies 7 16

The enemies, the enemy shots, the bombs and the missiles move as sprites; the clouds, the player's shots and the big machine, by writing characters into the name table. That is the important difference in this cartridge: on the MSX only four sprites fit on a line, and drawing the eight shots and the big machine with characters saves nine.

The player's plane is not on the list because it has no slot: it does not move.

The eight behaviours

Each enemy slot carries in its byte 1 which of the eight behaviours it has been given, and the dispatcher at 0x606C jumps through the table at 0x605C. Two of them fly a trajectory that is written down in a table:

All of them, whenever they change course, go through 0x63A2, which is where the two flags in the state are checked: with bit 1 the actor fires at the plane, and with bit 3 it drops a missile. Both are cleared as they are used, so each flag is good for one go.

The sound

Three channels of eight bytes and a player that gives each one a step per frame (0x4160). A channel's program is a string of three-byte notes —fine period, coarse period with the volume and the noise bit, and duration— and an 0xFF at the end. If the duration has bit 7 set, the note fades out on its own.

When a new sound starts it is compared with the number of the one already playing on that channel: if the one playing is worth more, it keeps its place.

The collisions

Everything is compared against the same rectangle, taken from a table of six (0x4E64): the four bytes are the minimum Y, the maximum Y, the minimum X and the maximum X, and each class of collision has its own. The shots, which live in name-table cells, are converted to pixel coordinates first by multiplying by eight (0x5C2A).

The attract mode

When nobody touches anything, the game is played by 0x546B, and that is the strangest thing in the cartridge: the attract mode's controller is not a recorded script nor a number generator, but the code itself. It takes the R register, adds it to 0x5399, and whatever byte is there goes into 0xE009 as if it came from the joystick.

In the emulator

Everything else on these pages comes from reading the binary. This page is the opposite: these are measurements, taken with the cartridge running in openMSX, and each one says which window it was taken over. What has not been measured is not here.

How it was measured

The basis is a real game, played and recorded on a Philips VG-8020 with tools/omsx_graba.tcl: 170 seconds, era 1 from t=10 to t=134, era 2 up to t=164 and game over at t=166. The whole replay is saved, so the measurements can be repeated as many times as needed over exactly the same game.

tools/omsx_repasa.tcl walks it second by second reading the game's RAM and says what state it is in at each point —attract mode or game, era, round, lives, enemies still to go— and the window is chosen from that: from t=30 to t=90, sixty straight seconds of play in era 1, with no attract mode in between.

Then tools/omsx_mide.tcl sets two breakpoints, one at the entry of the cartridge's hook (0x4037) and one at its exit (0x4112), and times the emulated time between them.

What the interrupt takes

   
ROM interrupts in 60 emulated seconds 2,980
frames per second 49.67
time inside the hook 30.07 s out of 60
percentage of the frame 50.11%
per interrupt 10.09 ms out of a 20.1 ms frame
the shortest / the longest 0.07 ms / 15.15 ms

So half the machine's time goes on the cartridge's interrupt, and the other half is everything left for the main program.

The figure is checked with a second method that shares nothing with the first: sampling the program counter every millisecond and counting how many samples land with the interrupt running. That gives 30,048 samples out of 59,910, or 50.16% (tools/omsx_control_pc.tcl).

What each phase of the six-frame cycle costs

The interrupt does not do the same thing on every frame: 0xE01F counts from 0 to 5 and each phase moves one thing. Measuring per phase, over the same window:

phase what it moves average ms
0 the shots, the clouds, the plane and the collisions 10.18
1 the enemies 10.14
2 the scrolling background 11.10
3 the enemies 10.17
4 the big machine and the stage count 8.81
5 the enemies 10.17

Between the dearest phase and the cheapest there are 2.3 milliseconds: the split is genuinely balanced, not just a way of ordering the work.

To the VDP, per frame

   
writes to port 0x98 (data) 360.6
writes to port 0x99 (address) 106.6

Of the 360.6 data writes, 96 are always the same ones: the whole sprite attribute table, which the interrupt uploads every frame from its RAM copy (0x40CA) without checking whether anything has changed.

The more there is in the air, the more it costs

The 50% above is a quiet window. Measuring two more, with the same method:

window what was going on % of the frame
replay, t=30 to t=90 in play, era 1 50.11%
30 s being piloted in play, era 1, from 25 to 17 enemies to go 59.04%
60 s of attract mode the demo, which fires non-stop 71.32%

All three are measurements, not estimates. What is not measured is how that difference breaks down: all that can be said without going past what has been checked is that the dearest window is the one with most things in the air, and that the attract mode, which presses the button on every frame, is the worst case of the three.

The trap that cost an afternoon

The first attempt at measuring the cost was to sample the program counter and count how many samples landed between 0x4037 and 0x4117, which is the piece of cartridge where the hook lives. That gave 1.20%.

That figure measures nothing: the hook calls routines spread all over the cartridge, so most of the time the program counter is outside that range even though the interrupt is working. What has to be counted is not where the PC is, but whether it has gone in and not yet come out: a flag that the entry breakpoint sets and the exit breakpoint clears.

Both figures are in medidas/control_pc.txt, side by side, in case the warning is of use to anyone. That folder, medidas/, holds the raw output of all these tools.

One number that comes out the same measured as read

Reading the code, 0xE120 starts each stage at 25 and goes down by one per enemy shot down; when it reaches zero the big machine comes out and the count is left at 5. Reading that same address second by second in the replay gives exactly that: 25 at the start, going down, and 5 as soon as the machine appears. That is what you want from a measurement: for it to confirm the reading or knock it down.

The tools

   
tools/omsx_graba.tcl records the game into a replay
tools/omsx_repasa.tcl walks the replay second by second and says what was there
tools/omsx_mide.tcl the measurement: interrupt, phases and VDP
tools/omsx_control_pc.tcl the independent control, by sampling the PC

The recorder carries an after quit { salva cierre }: without it, everything played since the last automatic save is lost when the window is closed. That was learnt by losing a game.

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, 6 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)
Frogger 16 236 CANAL_PON_VOLUMEN (0x41B6)
Hyper Olympic 1 2 14 PINTA_LA_PANTALLA_FIJA (0x4A62) = PINTA_EL_MARCO_DEL_MENU
Hyper Olympic 2 2 14 PINTA_LA_PANTALLA_FIJA (0x4A62) = PINTA_EL_MARCO_DEL_MENU

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.