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 pitfall.rom in the project root. It is exactly 16384 bytes with this sha256:

4d899d6258a8b06dfae8c91a8c57230fa23ff364136d6a243fafeaf282c8be58

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 18 tests over the listing, no cartridge needed

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  4d899d62...82c8be58
original   : 16384 bytes  4d899d62...82c8be58
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              9467   57.78 %
  datos identificados         6917   42.22 %
  sin explicar                   0    0.00 %
  ==========================================
  explicado                  16384  100.00 %

Without the cartridge

The work is in src/pitfall.asm and the notes: 6,589 lines with 337 routines and tables named, 988 line comments anchored to their address and 130 ranges of data with their explanation next to them. The 18 tests run without the binary.

How it is organised

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

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

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

The .entries file is long because the cartridge declares one single entry point —the BIOS reads the header and calls 0x8013— and everything else arrives by paths no static tracer can follow: the interrupt hook (0x80F7), the handlers that travel inside templates copied to RAM, the four dispatch tables and the sound vectors, declared one by one with the instruction that writes them noted alongside.

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 -one eight-byte pattern per row, four bytes per animation record, defw for a table of pointers- and where a pointer lands on a block that has a name, that name is written next to it.

The tools

In tools/, each with its own header:

   
z80trace.py follows the flow from the entry points
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
busca_autoescritura.py looks for writes into the ROM
mapa_escenas.py reproduces the ring of 255 scenes from the cartridge
busca_escaleras.py which scenes have a way down to the underground
ruta_optima.py the shortest route that collects the 32 treasures
monta_mapa_guion.py the 255 scenes on a grid, labelled
dibuja_guion.py that route drawn, one cell per screen
omsx_*.tcl the openMSX harnesses: boot, captures and walking the world

The cartridge

16384 bytes. No loader, no blocks: the MSX maps the cartridge at 0x8000-0xBFFF —page 2— 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 13 80 00 00 00 00 00 00 00 00 00 00 00 00
'A' 'B'  \_ INIT = 0x8013

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

At 0x8010 there are three bytes that are neither header nor ever executed here: C3 F7 80, a ready-made jp 0x80F7. The boot code copies them to the interrupt hook, and that is where they run.

What the boot does

INIT (0x8013):

That empty loop is the main program. The whole game runs inside the interrupt, fifty or sixty times a second.

Where the state lives

Everything the game keeps lives in RAM from 0xE000. The most-read addresses:

   
0xE012 lives left
0xE05F what is being pressed, in joystick format
0xE1D0-0xE1D4 the clock, already stored as tile numbers
0xE1D6-0xE1DB the six digits of the score
0xE1E6-0xE1ED the four sound vectors
0xE20E-0xE21B the copy of the fourteen sound chip registers
0xE21D-0xE220 the 32 bits of treasures already taken
0xE221 whether the demo is running
0xE222 the screen register: the whole world fits here
0xE224 / 0xE225 the scene's variant and kind, taken from 0xE222
0xE229-0xE246 the ten collision boxes
0xE247 how many live objects, then one pointer per object
0xE26A-0xE2BD the sprite attribute table, uploaded whole every frame

Behind that come the object structures, packed with no gap: 0xE2BF, 0xE2D5 (the player), 0xE2ED, 0xE303, 0xE319 and 0xE32F. The player's takes 24 bytes and the other five 22, closing the chain at 0xE345.

The screen, and the three registers the game never touches

The cartridge writes five of the video chip's eight registers: 0 and 1 (mode and screen), 3 and 4 (colour and pattern bases) and 7 (the border, 0x11: black, at 0x8036).

Registers 2, 5 and 6 are never written: they are the ones that say where the name table, the sprite attributes and the sprite patterns live, and the game writes to 0x1800, 0x1B00 and 0x3800-0x3FFF trusting the bases the BIOS left.

   
0x0000 the tile patterns (register 4 = 0x00, at 0xB79A)
0x1800 the name table
0x1B00 the sprite attributes
0x2000 the colours (register 3 = 0x80, at 0xB794)
0x3800 the sprite patterns

Register 1 holds 0xE2 (0x8047): 16 KB, screen and interrupt on, 16x16 sprites.

The intro runs in one screen mode and the game in another: 0xB6B2 sets register 0 to 0x02 —graphics mode 2— for the entry banner, and 0xB788 puts it back to 0x00, graphics mode 1. That is why the game's colour table is 32 bytes and not 6144: in the game's mode colour goes per group of eight tiles.

How the drawings are stored

There is a 58-byte decompressor at 0xB142 that writes straight to the video port. The format: two bytes of destination address, then one-byte tokens with the counter in the low six bits —bit 7 skip N positions, bit 6 copy N literals, neither repeat the next byte N times, zero ends—.

That covers the tile and sprite patterns (0x909E-0x95FE), the nine sprite blocks of the scene handlers (0x981E-0x99AF) and the six of the intro (0xBAB2-0xBC61). Stored raw: the four 16-tile scenery sets (0x95FE-0x97FE) and seven loose sprite patterns (0x97FE-0x981E and 0x99AF-0x9A6F).

Every compressed block ends exactly where the next one starts, and that pins its size without guessing.

The typography fits in twelve tiles

There is not one string of text. The only font is twelve tiles: the ten digits (0xB8-0xC1), the colon (0xC2) and the blank (0xC3), just enough for the score and the clock. Everything else that looks like text is drawing.

The full split

Not one byte unaccounted for: 9,467 of code reached by the tracer and 6,917 of data, each inside a declared range with the instruction that reads it noted alongside.

   
0x8000-0x8010 the header
0x8010-0x8013 the three bytes copied to the interrupt hook
0x8013-0x8A69 boot, the player, collisions, the jump, the score and the lives
0x8A69-0x8AFF RAM initialisers, the class table, the jump curve and the walk scripts
0x8AFF-0x8E1B screen and sprite loading, and scene drawing
0x8E1B-0x8F06 the floor strips and the six sinking patterns
0x8F06-0x9090 the scenery cell scripts
0x9090-0x909E fourteen raw tile numbers: row 23
0x909E-0x95FE the five big blocks of compressed graphics
0x95FE-0x97FE the four 16-tile scenery sets
0x97FE-0x9A6F the sprites: nine compressed blocks and seven raw patterns
0x9A6F-0xA086 the frame, the objects, the screen change, the score and the clock
0xA086-0xA43A the scenery tables, the four scene layouts and the 14x18 one
0xA43A-0xA61A the vine
0xA61A-0xA69E the 33 phases the vine is traced with
0xA69E-0xAE90 the object handlers and the eight scene routines
0xAE90-0xAED4 the four dispatch tables
0xAED4-0xB113 cell scripts, object templates, animation scripts and the colours
0xB113-0xB393 the video layer, the input and the sound upload
0xB393-0xB6B1 the sound table and the nine routines it installs
0xB6B1-0xB9E4 the intro and the demo
0xB9E4-0xBC6D the intro's tables and graphics
0xBC6D-0xC000 the padding up to 16 KB: 915 bytes, all zero

The code

Everything happens inside the interrupt

The main program is two bytes: a jump to itself, at 0x80F5. The game runs in the H.KEYI hook, which the BIOS calls on every screen refresh and which always does the same thing (0x80F7-0x8100):

B249   both joysticks, through the sound chip
B26E   keyboard row 8, remapped into joystick format
B35B   the chain of sound vectors
9A6F   the frame: the screen and the objects

and after that, at 0x8103, the 0x54 bytes at 0xE26A go to video memory 0x1B00: the whole sprite attribute table, once per frame.

And it never returns to the BIOS

The end of the hook is not a normal ret: it is eleven pops in a row (0x810F-0x8122) that unstack what the BIOS saved on entering the interrupt —including the alternate registers, after the ex af,af' and exx at 0x8118— with an in a,(099h) in the middle (0x811A) that reads the video chip status and acknowledges the interrupt.

The first pop hl eats the return address to the BIOS, and the ei / ret at 0x8123 hands control straight back to the interrupted program. The rest of the BIOS interrupt routine never runs.

The frame

0x9A6F, in order:

  1. the game clock (0x9DB8);
  2. the screen edge (0x9CBE): if the player reached it, the scene changes and the frame ends there;
  3. if 0xE221 is non-zero the demo is running, and the input is replaced by the recording at 0xE259 (0x9A7E);
  4. the walk over the live objects (0x9A88);
  5. the two sprites above the player (0x9D03);
  6. the close (0x9AE0): system keys and the idle timer.

The character is three sprites, one per colour

An MSX1 sprite is one colour, so a three-colour player is three sprites. 0x9D03 builds the other two from the main one: same X, 16 pixels higher, with patterns (P-0x20)+0x68 and (P-0x20)+0xB0. The colours are written by the scene setup: 0x0C at 0xE2A5, 0x06 at 0xE2A9 and 0x0F at 0xE2AD. reaparece zeroes them to hide the character (0x8247) and turns them back on at different times (0x82D4-0x82E1): that is the flicker.

Harry is 16x32 and the layers are not exact overlays: the two upper sprites overlap each other —two colours in the top half— and the main one sits below, one colour for the legs. Rebuilt from the dumped video memory (tools/render_jugador.py), this is the strip of the twelve frames named by the cartridge's own animation scripts:

The twelve frames of the main character

The timings are the cartridge's too: walking is five frames of three ticks each (0x8AE1) and climbing two frames of one tick (0x8AF9). Walking left uses patterns 0x44-0x54 (0x8AED), the exact mirror of 0x20-0x30, checked pixel by pixel. The two standing patterns, 0x34 and 0x58, come from no script: se_para sets them by hand (0x8970 and 0x896B), which is why they are not in the strip.

On the TMS9918 the lower-numbered sprite is in front, so the one at 0xE2A6 covers the one at 0xE2AA. It matters in the two climbing frames, the only ones where the two upper layers overlap: five pixels each, which with the order reversed would come out white instead of red.

The speed: anda (0x88ED) writes 0x00C8 —200/256, 0.78 pixels per frame— and 0xFF38 the other way, the same number negated.

The emulator dump agrees: three sprite entries at 0xE2A2 with the same X, Y 109 for the main one and 93 for the other two, patterns 0x34, 0x7C and 0xC4 — exactly 0x34, (0x34-0x20)+0x68 and (0x34-0x20)+0xB0—.

The objects: six bytes per axis, and one counter each

0xE247 says how many objects are alive, followed by one pointer per object. Each object's counter (+0x11) is decremented and only when it hits zero is it reloaded with the period (+0x10) and the object served (0x9A99): every object runs at its own pace from a single loop.

The structure, read from the walk at 0x9A88:

   
+0x00 to +0x05 flags, 16-bit speed, the two limits and the fraction — one axis
+0x06 to +0x0B the same for the other axis
+0x0C / +0x0D the animation script
+0x0E / +0x0F the animation counter and the current frame
+0x10 / +0x11 the period and its count
+0x12 / +0x13 its handler
+0x14 / +0x15 where its sprite attributes are

That the two movement blocks are identical is visible in the code: 0x9AB3 calls the move routine, 0x9AB9 adds six to IX, and 0x9ABD calls the same routine again.

The position is not in the structure: the integer part lives directly in the sprite attribute (0x9B24), and only the fraction stays in the structure, at +0x05. Moving an object is adding its speed to a 16-bit number whose high byte is what the video chip sees.

The per-axis flags (0x9B1F):

   
bit 0 it moves
bit 1 on reaching a limit it bounces instead of stopping (0x9B5A)
bit 2 it has limits, at +0x03 and +0x04
bit 5 it is animated (0x9AEC)
bit 6 it has its own handler (0x9AC2)

Four dispatchers, all with the same trick

Jumps to computed addresses stack the return by hand: ld de,<back> / push de / jp (hl) (0x9AD2, and 0x9F89 the same). The return addresses are declared as tracer entry points.

What it picks Where Index Table
an object's handler 0x9AD6 the object itself, +0x12/+0x13 none
the kind of scene 0x9F81 0xE225, 0 to 7 0xAEB4
the scene's variant 0xA99F 0xE224, 0 to 7 0xAE94, 0xAEA4 or 0xAEC4, per caller
what happens on contact 0x873B the box class, 0 to 10 0x8AA0

The variant one takes the table in HL: the same routine serves three tables, and the caller decides which. The one at 0xAEA4 is only loaded by 0xAE2E, and only with scene kind 5.

The collision boxes

0xE229-0xE246 are ten three-byte records: class, left X and right X. The scene routines write them when the screen is set up, and 0x84EF, 0x8529 and 0x8589 walk them with the player's X plus eight —his centre—.

The first seven belong to the surface and those from 0xE241 on to the underground (0x853A). The class goes straight to the dispatcher at 0x873B:

     
0 off jumps nowhere
1 log 0x8640: knocks down and drains points
2 ground 0x8751: the way down to the underground
3 pool 0x8361: sinks you
4 crocodile 0x8361: the same place
5 vine 0x8162: hooks you
6 kills 0x8221
7 — points to 0x874B, and nobody ever writes it
8 treasure 0x878C: adds it up
9 kills 0x8221
10 bounce 0x85EF: reverses your run and pushes back. Not the ladder

Classes 2, 3 and 4 also push the player sideways out of the box (0x8555-0x8578): what is adjusted is his X; nothing touches the Y there.

The world is not stored: it is computed

The scene you are in is the content of 0xE222, an 8-bit feedback shift register. 0xB68F steps it forward and 0xB69F steps it back with the exact inverse function: leaving left undoes what right did. All eight bits are used: 6-7 the scenery (0x9F91), 3-5 the kind (0x9EFB), 0-2 the variant (0x9EEF).

The detail, with why there are exactly 255 screens, is in Findings.

Drawing a scene

0x9EE6 is the only path that builds a screen: poking 0xE222 repaints nothing. It reads the scenery, kind and variant from the register, then builds:

The floor strips save two bytes per strip: the layout byte is at once offset into the table, copy length and video memory advance (0x8DA7). Each strip copies from tabla+N to tabla+2N.

The input: two joysticks and a keyboard in the same byte

0xB249 reads both joystick ports through the sound chip into 0xE05F and 0xE061 —with a cpl at 0xB257, because the chip delivers them inverted—. 0xB26E takes keyboard row 8 —space bar and cursors— and remaps it bit by bit: three rotates and an and 0x1F (0xB28F) leave bit 0 up, 1 down, 2 left, 3 right, 4 fire.

Both sources end in the same place with the same shape, so the rest of the game cannot tell which you use. The demo exploits it: it writes the recorded input into 0xE05F (0x9A7E).

The sound never touches the chip: it touches fourteen bytes of RAM

0xE20E-0xE21B is a copy of sound chip registers 0 to 13, and 0xB37B uploads it whole with an outd loop, once per frame. Outside it, port 0xA1 is written in only two places (0xB24F, 0xB261), and not for sound: it is register 15, the joystick port selector.

On top sit four vectors at 0xE1E6-0xE1ED. Requesting a sound is calling 0xB32E with a number from 0 to 10; the table at 0xB393 —[slot][pointer] records— says which slot it installs into and with which routine, and the slot fixes the channel. Every frame, 0xB35B calls whatever is installed in the first three.

There are two engines and three hand-written effects. The one at 0xB3F0 reads eight-byte scripts and sweeps pitch and volume together, with a fractional part at 0xE1FC of which only the high byte comes out (0xB471); the one at 0xB5EA reads four-byte scripts and holds each note still. The three without a script —0xB49F, 0xB4E6, 0xB52E— carry the sweep in the code: 19 frames raising the period by 0x14, three frames lowering it by 0x7F —or 0x80: the sbc at 0xB528 takes the carry and nothing clears it— and one frame of noise.

A detail you cannot hear playing: sounds 0 and 1 are mute twice over. Their entry points to 0xB392 —a lone ret— and they install into slot 3, the only one the chain at 0xB35B never walks.

The vine is computed, not stored

The vine is painted every frame, and it is the code that paints it: what the cartridge does not carry is a drawing of it. On each step, 0xA471 traces a 16-point straight line onto a 0x40-byte bitmap in RAM (0xE18A) and uploads it to video memory as a sprite pattern (0xA594). The slope comes from the table at 0xA61A —33 four-byte records— indexed by the phase of the swing, 0xE1CB, which runs back and forth between 1 and 0x20 (0xA5EF).

The four bytes of each phase:

byte what it is from vertical to the tip
1 the slope, in 1/256 pixel per row 0x00 → 0xC9
2 the object's period (0xA48F) 1 → 9 frames
3 where the rope ends and you grab it (0xA529) 0x10 → 0x06
4 which step of the jump curve the fall starts at on letting go (0xA546) 0x00 → 0x10

Since the period grows towards the tips, the swing rushes through the middle and brakes at the ends, like a pendulum: no physics, one column of a table. Half a swing is the 32 periods added up, 75 frames.

The rope is not the 48 rows of its three sprites: the third one carries pattern 0x60, which 0xA551 builds by copying from the first only as many rows as the third byte says, so the rope ends at the grab point and shortens as it leans: 48 rows vertical, 38 at the tip.

Reproducing the tracer with the same data (tools/render_liana.py) gives the 33 phases:

The 33 phases of the vine on top of each other

The bottom edge is each phase's grab point, which is why the outer ones end higher. Against an emulator capture, the rope lands 0.28 pixels on average from where the model puts it (the matching phase is 0x1C); even the steps repeat, because the machine restarts the accumulator on each sprite and drops the fraction every 16 rows.

While you hang, the vine writes your X and Y (0xA5F9), with a drawing of its own: pattern 0x40, or 0x64 facing left (0x819A). Letting go is pressing down (0x81A7).

The jump is a table, not a speed

The jump starts at 0x87E9 and the step is kept at +0x17 of the player's structure. In the air (0x8820) that number counts from 0x1F to 0 and indexes the curve at 0x8AB6, read from the end backwards: 0xFF goes up one pixel, 0x01 down one, 0x00 holds (0x8835). There is no vertical speed anywhere: there is a table.

From 0x1F to 0x11 the curve rises ten pixels and from 0x10 to 1 it drops them ever closer together: 31 frames, 15 up and 16 down. Letting go of the vine uses the same curve but does not start at 0x1F: it starts at the fourth byte of the swing phase (0x821A), which never goes past 0x10 —exactly the falling half—, so letting go only ever falls. At the vertical it is 0 and you land at once (0x882E); at the tip it is 0x10, the whole descent.

Turning in the air is only possible in the first three steps (0x8896), and doing so recovers the ground lost by changing direction (0x88C3).