Under the hood

Getting started

What you need

pasmo and z80dasm to assemble and disassemble, and Python 3 for the tools. No other dependencies.

The cartridge is not distributed with this repository: you need your own copy, named athletic.rom in the project root. It is exactly 16384 bytes with this sha256:

7bd280ae4147a5bf5676b15fde310a8106887786c78873344d97a1cd81285485

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

The commands

make          # trace, build 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 17 tests over the listing, no cartridge needed
make web      # the images and these pages

make fails if the listing stops reproducing the cartridge byte for byte, if the tracer walks into an area declared as data, if an entry point falls inside one, or if a single byte of the 16384 is left unaccounted for.

The proof that decides

A disassembly is trustworthy if assembling it gives back the original. That is make verify:

ensamblado : 16384 bytes  7bd280ae...81285485
original   : 16384 bytes  7bd280ae...81285485
OK: reproducible byte a byte

The second proof

A listing can reassemble perfectly and still be wrong: if drawings are being read as instructions, the bytes do not change —only what is said about them does—. make sanity crosses the data ranges against the trace and ends with the split:

  codigo trazado              7448   45.46 %
  datos identificados         8936   54.54 %
  sin explicar                   0    0.00 %
  ==========================================
  explicado                  16384  100.00 %

The third proof, for the graphics

Neither of those two can catch a tile table read one byte out of step: the bytes still assemble and the range is still declared. So the graphics are checked by drawing them. tools/graficos.py rebuilds video memory by replaying the cartridge's own copy calls —same order, same addresses, mirrors and colour substitutions included— and tools/imagenes_web.py then repeats the specific calls that paint each thing. Every picture on this site comes out of that: the title logo, the scenery, the nine vine drawings, the eighteen water-jet blocks, the tile set and the sprites. A mislabelled range comes out as noise.

Without the cartridge

The work is in src/athletic.asm and the notes: 6,882 lines with 296 routines and tables named, 348 line comments anchored to their address and 166 ranges of data with their explanation next to them. The 17 tests run without the binary.

How it is organised

The listing is never edited by hand: it is generated, governed by three files.

   
src/athletic.entries the entry points: where tracing starts
src/athletic.nocode the areas that are NOT code, and how that is known
src/athletic.notes the names, the comments and the data ranges

From those, src/athletic.asm. Every note is anchored to its address, so it survives a retrace.

The .entries file holds 48 entry points because the cartridge declares one single entry point —the BIOS reads the header and calls 0x4077— and everything else arrives by paths no static tracer can follow: the interrupt hook (0x4038), which is where the whole game runs, and the five dispatch tables that the routine at 0x40A9 reads from behind its own call, declared one by one with the instruction that reaches them noted alongside.

It also carries a directive of its own, !skip 0x5F65 6: the scenery engine takes six bytes of parameters that sit after its call, so the tracer has to step over them instead of executing them. Those 19 blocks of six bytes come out of the trace as data, and each one is declared in the notes.

The .nocode file is short —5 ranges— and holds the five dispatch tables, which the tracer would otherwise walk straight into.

How the data blocks are laid out

Every data range declared in the .notes comes out as a block of its own: its own heading saying what it is for, its own label, and the dump aligned to its first byte, so where one table ends and the next begins is visible at a glance. An optional line gives the block the row width of its real structure, and that is what makes the tables readable in the listing itself:

   
F 0x48CE 8 the 48 font glyphs, one glyph per row
F 0x7608 8 the tile patterns, one pattern per row
F 0x6B8A 4 the fourteen player poses, the four sprite patterns of each on its row
F 0x6C06 5 the death poses, five patterns each
F 0x5AAF 4 sprite attributes, one record —Y, X, pattern, colour— per row
F 0x5701 19 the eighteen water-jet blocks: 6×3 tiles and the plank's height, one block per row
F 0x700D 6 and F 0x707C 3 tile blocks laid out at their true width, six and three columns
F 0x5E54 3,1 the three tiles of the rock on the first row and the 0xFF end marker on its own
F 0x4149 w1 a table of pointers as defw, one per row
F 0x445E w3 the six parameter bytes of a scenery call: three words, which is what they are

Where a pointer lands on a block that has a name, that name is written next to it: the twenty entries of the state table come out reading -> ESTADO_00_ARRANCA, -> ESTADO_01_LOGO and so on, and the six that are 0x0000 are visible for what they are.

The tools

In tools/, each with its own header:

   
z80trace.py follows the flow from the entry points, stepping over inline parameters
mkasm.py builds the listing with the notes anchored
presupuesto.py the split of the 16384 bytes, and what is left unowned
refs.py which instructions point into a range, without inventing pointers
quien_apunta.py for each gap, who reads it from traced code
graficos.py rebuilds video memory the way the cartridge loads it, and draws it
imagenes_web.py the pictures on this site, each one a routine replayed
check_datos_como_codigo.py no declared data range may come out as code
check_entradas.py no entry point may fall inside one
omsx_*.tcl the openMSX harnesses used to measure who reads what

The cartridge

16384 bytes. No loader, no blocks: the MSX maps the cartridge at 0x4000-0x7FFF —page 1— and that is the whole picture of memory, with no overlays: no address means two different things at two different times.

Where it enters

The first sixteen bytes are the header the BIOS reads:

41 42 77 40 00 00 00 00 00 00 00 00 00 00 00 00
'A' 'B'  \_ INIT = 0x4077

The two letters mark an executable cartridge; of the four vectors only INIT is set —STATEMENT, DEVICE and TEXT, the BASIC ones, are zero—.

What the boot does

INIT (0x4077), in order:

Those two bytes are the main program. The whole game runs inside the interrupt, fifty or sixty times a second, and the program never comes back.

The lock

Because the game is the interrupt, a frame that overruns would re-enter itself. 0xE005 stops that: the hook (0x4038) reads the video chip's status to drop the request, plays the sound —always, before anything else—, and only then looks at the lock. If it is set, that is all this interrupt does (0x4067): the music carries on and the frame that is still running is left alone.

Where the state lives

Everything the game keeps lives in the 1 KB from 0xE000, with the stack on top of it at 0xE400.

   
0xE000 / 0xE001 the game state, 0 to 19, and its sub-state
0xE002 the options: keyboard, two players, game in progress, whose turn
0xE005 the lock: the interrupt is inside
0xE008 / 0xE009 what was pressed last frame and this one, in joystick format
0xE010-0xE02D the three sound channels, ten bytes each
0xE038-0xE03F the copy of the eight video chip registers
0xE040-0xE048 the high score and the two players' scores, in BCD
0xE050-0xE06F the player in play: lives, stage, SCENE, clock, direction…
0xE080-0xE09F the other player's copy of that, swapped on each turn
0xE0B0-0xE12F the 32 sprite attribute records, uploaded whole every frame
0xE130-0xE14F the frame counters and the phases of the vines and water jets
0xE134-0xE13C the player: Y, X, pose, which way he faces, his state
0xE150-0xE155 the six parameter bytes the scenery engine copies out of the ROM
0xE156 / 0xE158 this screen's fixed obstacles and its moving ones
0xE18C-0xE1AE the obstacles' X positions and what each is worth
0xE200-0xE205 the jump: which curve, which way, how many frames

The screen: colours below, patterns above

The video chip is set from an eight-byte table at 0x4545, copied to 0xE038 and sent out register by register (0x4528):

register value what it says
0 0x02 graphics mode 2
1 0xE2 16 KB, screen on, interrupts on, 16×16 sprites
2 0x0E the name table at 0x3800
3 0x7F the colour table at 0x0000
4 0x07 the pattern table at 0x2000
5 0x76 the sprite attributes at 0x3B00
6 0x03 the sprite patterns at 0x1800
7 0xE1 the border

Colours below and patterns above is the other way round from the usual layout, and it is why every address in this listing looks displaced if you read it expecting the common one.

In this mode the screen is three thirds, each with its own pattern and colour bank, and the game uses that on purpose: the font is loaded once and then copied into the other two thirds (0x4583 and 0x4594), while the scenery tiles are loaded only into the third where they are going to be drawn. The vine is the clearest case: its top row is drawn with tiles 0xB0-0xB6, which are loaded into the first third, and everything below it with tiles 0x60-0x9B, loaded into the middle one.

How the drawings are stored

Three ways, and each one is used for exactly what it suits:

Two of the smallest calls, replayed: the strip of grass on row 15 (0x5E71) and the ground of rows 16 to 19 (0x5CEA). Between them, twelve bytes of parameters and two short lists.

The strip of grass on row 15

The ground of rows 16 to 19

The left-facing halves are not stored at all. 0x5A4B reverses the bits of a tile and 0x5A09 does the same across the two halves of a 16×16 sprite, and both run at load time: the cartridge carries 23 sprites and puts 46 in video memory.

The full split

Not one byte unaccounted for: 7,448 of code reached by the tracer and 8,936 of data, each inside a declared range with the instruction that reads it noted alongside.

   
0x4000-0x4010 the header
0x4010-0x4038 the video chip primitives, and four 0xFF of padding
0x4038-0x4077 the interrupt
0x4077-0x4149 INIT, the two-byte main loop, the dispatcher, the controls, one game step
0x4149-0x4171 the table of twenty states
0x4171-0x4468 the twenty states: title, menu, demo, play, death, game over, stage cleared, screen change
0x4468-0x44C8 the credits, in tiles
0x44C8-0x4626 new game, video and sound setup, the copy routines and the wipe
0x4626-0x4795 the font loader, the score, the extra lives, the high score, the scoreboard and the lives
0x4795-0x47FB the title screen and its columns
0x47FB-0x48CE the ATHLETIC LAND logo, compressed
0x48CE-0x4A4E the 48 font glyphs
0x4A4E-0x4B52 the scoreboard labels and the four menu options
0x4B52-0x4BD8 the KONAMI logo loader and the run-length decompressor
0x4BD8-0x4C6B the KONAMI logo, compressed
0x4C6B-0x503B the game's tiles and their colours
0x503B-0x555B the 64 sprites
0x555B-0x56DD the vine: nine pointers and nine drawings
0x56DD-0x5857 the water jets: eighteen pointers and eighteen blocks
0x5857-0x5C32 the frame, the demo's input, the graphics loader, the mirrors, and building a screen
0x5C32-0x5C72 the two tables of thirty-two screens
0x5C72-0x5F65 the scenery: the skies, the hedge, the ground, the pond, the obstacles and the player's entrance
0x5F65-0x5FD2 the scenery engine
0x5FD2-0x66A6 the counters and the eleven moving things, the clock and the scoring
0x66A6-0x6DD4 the player: seventeen states, the collisions, his sprites and the jump curves
0x6DD4-0x70A5 the scenery lists and the tiles they use
0x70A5-0x714F the loader for the patterns and colours
0x714F-0x77F8 the colour lists, the colours and the tile patterns
0x77F8-0x77FE six bytes with no use, between the patterns and the sound
0x77FE-0x7960 the sound: the request, the player and the two formats
0x7960-0x7C3E the note periods, the table of 34 tracks, and the tracks
0x7C3E-0x8000 the padding up to 16 KB: 962 bytes, all 0xFF

The code

Everything happens inside the interrupt

The main program is two bytes: jr $ at 0x40A7. The game runs in the H.KEYI hook, which the BIOS calls on every screen refresh, and which always does the same three things (0x4038):

785D   the sound, always, even if the last frame never finished
40B3   the controls, only with a game in progress
412A   one step of the game: the state in 0xE000

and nothing else. There is no main loop to come back to.

The call that never comes back

Jumps to computed addresses are a routine of their own here, 0x40A9, and it uses a trick that shows up all over the cartridge: the table sits right behind its own call.

DESPACHA:
    add a,a                   ; 40a9
    pop hl                    ; 40aa   the return address IS the table
    call HL_MAS_A             ; 40ab
    ld e,(hl)                 ; 40ae
    inc hl                    ; 40af
    ld d,(hl)                 ; 40b0
    ex de,hl                  ; 40b1
    jp (hl)                   ; 40b2

The pop hl takes the return address, which is where the table starts, indexes it and jumps. It never returns to whoever called it. Five tables are used that way:

Table How many What it picks
0x4149 20 the state of the game
0x41F8 2 the two steps of the menu
0x5C7F 4 which of the four skies is drawn
0x5E85 4 which combination of treetops goes on top
0x66C2 17 what the player is doing

Because the table is data sitting in the middle of the code path, a static tracer walks straight into it. That is what src/athletic.nocode is for: five ranges, one per table.

One routine draws all the scenery

0x5F65 is the only thing that puts a layout on screen, and it takes its arguments the same way: six bytes after the call, two pointers and a video address.

MOTOR_DE_ROTULOS:
    pop hl                    ; 5f65   the six bytes of parameters
    ld de,0e150h              ; 5f66
    ld bc,00006h              ; 5f69
    ldir                      ; 5f6c
    push hl                   ; 5f6e   carry on after them

The list is a run of counts: n copies n tiles from the tile list, n with bit 7 set fills n positions with a single tile, a lone 0x80 says a new video address follows, and a zero ends. Nineteen calls, and four of them write the colour table instead of the name table (0x70F0 onwards): the same engine, pointed at 0x0000 rather than 0x3800.

That is why the tracer needs the !skip 0x5F65 6 directive: those six bytes are not instructions, and executing them would derail everything after the call.

The frame

0x585E, in order: the 128 bytes of sprite attributes go to video memory in one copy —all 32 sprites, every frame—; at exactly the twenty-fourth step the moving obstacles are switched on; and then, only while the player is alive, the clock, the counters and the eleven things that move, followed by the player himself.

The counters are the small clever thing. 0xE130 counts frames, and on top of it four more tick every 8, 9, 10 and 11 frames (0x5FD2). Those four are the phases: the two vines take the 8 and the 10, and the four water-jet planks take one each, so the four planks never rise and fall together. And they are the only thing that survives the screen wipe (0x5A64): walk into a new screen and the vines are wherever they were.

The jump is a table read forwards and then backwards

There is no vertical speed anywhere. A jump is a list of deltas between a 0xFE and a 0xFF, walked in one direction subtracting —going up, less and less each frame— and in the other adding (0x61B7). Hitting the 0xFF turns it round; coming back to the 0xFE ends the jump.

The normal jump is these sixteen bytes at 0x6CF5:

4 4 3 3 3 3 2 2 2 2 1 1 1 1 0 0

Sixteen frames up and sixteen down, 32 pixels of rise, and the fall is the rise read the other way: exactly symmetrical, because it is the same bytes. There are four such lists, and everything that leaves the ground uses one:

   
0x6CF5 the normal jump, a bounce with no button, falling off a plank or a post
0x63C9 the high bounce off a trampoline, and the bouncing ball
0x63A5 a ball that rolls in high hops
0x63B7 a ball that rolls almost flat
0x6CE3 sinking, and a ball in between

The player's curve is walked one delta per frame, and while he is in the air the same routine also checks whether he has touched the end of a vine (0x6C3C).

The player is four sprites and a shadow

An MSX1 sprite is one colour, so the boy is four: two on top —head and body, in red and yellow— and two underneath —the legs, in magenta and blue—. The pose 0xE136 indexes the table at 0x6B8A, fourteen records of four patterns: 0 to 6 facing right, 7 to 13 the same facing left.

Under him, sprite 8: the same X, pattern 0xD4, black. It sits at Y 0x8C while he is on the ground or hanging from a vine, disappears while he is in the air, and follows sixteen points under him while he is up on a trampoline, a plank, a post or the log — which is what tells you where you are.

Here are the 64 sprite patterns, rebuilt from video memory as the cartridge loads it. The right-facing 23 are drawn in the cartridge and the next 23 are those same ones mirrored bit by bit at load time; the last 18 are everything else — the fish, the balls, the spider, the shadow, the 50/100/200 labels, the four faces and the fruit. They come out white on black because the colour is not in the pattern: the game writes it into the attribute record.

The 64 sprite patterns of the cartridge

The tiles, three thirds of them

In graphics mode 2 each third of the screen has its own bank, and the cartridge uses them as three different tile sets. This is the pattern table with its colours, rebuilt by replaying the cartridge's own copies: the font sits in all three thirds —it is loaded once and copied twice—, the scenery goes wherever it is going to be drawn, and the middle third carries the vines and the water jets.

The three thirds of the tile table

The water jet is eighteen drawings and a nineteenth byte

The plank that rides the water jet is not moved: it is redrawn. 0x56DD is a table of eighteen pointers, and each one points at nineteen bytes: a block of 6×3 tiles, and then one more byte, the height of the plank — 0x52 at the bottom down to 0x32 at the top, two at a time. 0x60B7 draws the block and returns that byte, which is what the player then stands on.

The phase runs 0 to 31: from 18 upwards it is read backwards, so eighteen drawings make a full up-and-down.

The eighteen blocks of the water jet, rising

The sound never touches the chip more than it must

Three channels, ten bytes of state each, from 0xE010: how many frames of the note are left, its length, which sound is playing, the pointer into its track, how many octaves down, and the volume with its envelope.

Asking for a sound is calling 0x77FE with a number. Under 0x8E it is an effect and takes channel C alone; 0x8E is the in-game music and takes A and B; from 0x90 up it takes all three. The number itself is the priority: a new sound only displaces a lower one (0x781C). The table at 0x796A holds 34 pointers, the channels of each sound side by side.

The player reads two formats, told apart by bit 7 of the sound number:

   
effect 0x2n sets the note length, then two bytes: volume and a 12-bit period
music 0xFD oo sets the octave and the starting volume, then one byte per note: length in the high nibble, note in the low one, 12 meaning silence

0xFE restarts the track —that is how the in-game music loops— and 0xFF switches the channel off. Only the music carries an envelope: the note starts at its volume, drops three steps in three frames, holds, and drops two more at the end.

One sound is a joke on the rest: number 6 is a track that only contains 0xFF. It is asked for when the bee flies off the screen and when you leave a screen, and all it does is switch the effects channel off — the only way to stop the buzzing.

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, 29 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)
Cabbage Patch Kids 72 1,114 COBRA_PUNTOS (0x6642)
Monkey Academy 17 255 SUENA_PASO (0x7880)
Antarctic Adventure 14 222 SUENA_PASO (0x7880) = CANAL_SIGUE
Super Cobra 11 116 MUSICA_PERIODO (0x793A)
Circus Charlie 10 87 COPIA_VRAM_A_VRAM (0x4573) = copia_vram_a_vram
Magical Tree 8 85 PINTA_FASE (0x4707) = pinta_el_numero_de_fase
Konami’s Billiards 6 111 SUENA_PASO (0x7880) = paso_de_un_canal
Konami’s Mahjong Dojo 5 43 COPIA_VRAM_A_VRAM (0x4573) = copia_dentro_de_la_vram

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.