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

ad011203f1295bf75fc30423cead68e12f96169ffeb9f13474303d9a553e41f7

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 28 tests on the listing, which do not need the cartridge
make imagenes # redraw the reconstructed pictures
make medir    # measure what the scroll costs, in openMSX
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  ad011203...553e41f7
  original   : 16384 bytes  ad011203...553e41f7
OK: reproducible byte a byte

What is in each folder

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

How to read the listing

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

To change anything, edit src/pippols.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 pippols.entries declares: the interrupt and the targets of the dispatchers, which jump through tables. 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, tools/refs.py) and checking that the format matches what the consuming code does with it.

What cannot be read is measured. tools/graficos.py rebuilds the background in Python —the strips, the generator of the 192 characters, the map decompressor and the name table dump— and draws the screen: if the reading were wrong, it would come out as noise. And tools/omsx_scroll.tcl measures in openMSX, with the emulated Z80's clock, what each frame costs.

Reproducibility

The cartridge

The header and the machine

The first sixteen bytes are the header the BIOS reads:

4000  41 42        "AB", a cartridge's signature
4002  4A 40        INIT = 0x404A
4004  00 00 00 00 00 00   STATEMENT, DEVICE and TEXT set to zero
400A  00 x6        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. From there the program never comes back: INIT writes jp 0x4010 into the H.KEYI hook (0xFD9A) and drops into a two-byte jr $ at 0x4074. The whole game runs inside the interrupt, one step per frame.

RAM is used from 0xE000 up, with the stack at 0xF000, so a 16 KB MSX is enough.

Video memory

SCREEN 2, with the eight registers at 0x43FD:

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 thirds of eight rows, and each third has its own 256 patterns. Pippols loads all three the same, so a character number means the same thing whatever row it is on; without that, the background would break as it crossed row 8 or row 16.

The 256 characters are shared out like this:

characters what they are
0x00-0x0F plain background and fixed decorations (0x00 and 0x0C are the empty pair)
0x09-0x0A the ground, which changes on every screen
0x10-0x39 the font: ten digits and twenty-six letters
0x40-0xFF the 192 of the scrolling background

Those 192 are the point of the whole cartridge: eight copies of the same set of 24 characters, each one a pixel lower. They are explained in The pixel scroll.

What goes to VRAM is compressed with a three-order RLE (0x43AE): a command byte, and if it is 0 it ends, if bit 7 is clear it repeats the next byte that many times, and if it is set it copies that many bytes as they are. 0x80 starts another block with a new address. The font, for example, takes 264 bytes in the cartridge and 336 in VRAM; the KONAMI logo patterns, 151 and 216.

The sprites

The 52 common patterns are decompressed in one go into 0x1880 (0x5E67), and the player's are loaded separately: there are 26 blocks in the table at 0x643A, and each frame whichever one matches the current sprite set —walking, sitting on the throne, dying— is sent up to 0x1800. That is how the little fellow is animated without burning spare patterns.

The attribute table holds 32 sprites: the boss, four for the player, ten creatures, seven objects, two of your shots and three of theirs. The order they are written in alternates from one frame to the next (0x7F83), creatures first on one frame and objects first on the next, so that the MSX's limit of four sprites per line does not always hide the same ones.

The cartridge's sprites

The RAM map

address what it holds
0xE000/0xE001 the program's state and substate
0xE005 the interrupt's lock
0xE010-0xE039 the three sound channels, 14 bytes each
0xE043-0xE048 high score and score, three BCD bytes each
0xE050 lives
0xE060-0xE0EF the name table buffer: 24 rows of 6 bytes
0xE100/0xE101 the position within the stage, in pixels
0xE103 the background stage (0-7)
0xE11C the offset of 0 to 7 pixels within the character
0xE120-0xE126 the player: state, direction, animation, Y, X and sprite set
0xE132 which world screen you are on (0-17)
0xE1B0-0xE1C7 three enemy shots of 8 bytes
0xE1E0-0xE1EF the player's two shots
0xE200-0xE29F the ten creatures, 16 bytes each
0xE400-0xE42F the seven objects, 6 bytes each
0xE500-0xE91F the 44 map pieces, 24 rows each
0xEA00-0xEABF the four background strips and their four colour strips

A creature's slot is 16 bytes: type, substate, animation, counter, Y, X, pattern, colour, the Y and X fractions, the Y and X velocities in 8.8 fixed point, and the target. A negative type means it is dying.

What it is made of

  bytes  
code 9099 55.5 %
data 7285 44.5 %
unidentified 0  

The data, from the inside: 1201 bytes are the music and effect tracks, 1485 the compressed common sprite patterns and 883 the player's 26 blocks, 647 the whole map (the dictionary, the sequence, the stretches and the plans), 409 the background strips with their colour, 408 the KONAMI logo and the title, 358 the object lists of the six roads and 264 the font. The rest are tables of a few dozen bytes each: which creatures each stage gets, the sideways movement curves, the jump steps and the corners each creature comes in through.

At the end of the cartridge there are 45 padding bytes of 0xFF and, behind them, nine bytes nobody reads: Konami's hidden mark, the title ピポルス backwards and the 29 of RC-729. It is in Findings.

The code

Nine thousand bytes of Z80, with 152 routines somebody calls by name and a frame Konami reused across its cartridges of those years: the interrupt as the program, a dispatcher that jumps through tables and a three-channel sound player.

The interrupt is the program

INIT (0x404A) sets up the VDP, loads the fixed characters, writes jp 0x4010 into the BIOS's H.KEYI hook and drops into a two-byte jr $ at 0x4074. From then on the main loop does absolutely nothing: everything —sound, controls, the player, the creatures, the objects and the scroll— happens inside the interrupt, one step per frame.

The first thing the interrupt does is read the VDP status, which is what lowers the request, and the last thing is read it again in case another arrived meanwhile. In between there is a lock (0xE005): if the previous step has not finished, the new one just gives the sound a step and leaves. That way the music does not break when a frame overruns, which happens here often: the whole interrupt averages 73 % of the frame.

The dispatcher

	call DESPACHA
	defw target_0, target_1, ...

DESPACHA (0x4043) is six instructions: add a,a because the index is in words, pop hl to pick up the return address —which is exactly where the table begins—, read word number A from there and jp (hl). You go in with a call and you never come back.

There is a second, cheaper dispatcher for things indexed by type: a push bc / ret with the table two bytes below its base, so the index can start at 1 instead of 0. The three creature tables (0x6861, 0x6894, 0x68B4) and the per-frame routine table (0x6B58) all work that way.

The eight states

0xE000 says what the program is doing, and 0xE001 is a substate that each state eats away with djnz:

state what it does
0 the KONAMI logo rising three rows every two frames
1 the title on screen, with its countdown
2 the menu: PUSH SPACE KEY blinking
3 starts the demo
4 starts the stage: clear, paint the panel and build it
5 playing: one step of the game per frame
6 end of game, with sound 0x98
7 GAME OVER and back to the title

States 0, 1 and 2 push the return address 0x4184 before dispatching, which is a trick for the three of them to share the menu handling without repeating it: on return, that stretch reads the controls and starts the game if space was pressed.

One step of the game

PASO_DE_PARTIDA (0x5087) is what runs on every frame while you play, in this order: the sprite table, the controls, the player, the creatures, the shots —yours and theirs—, the objects, the scroll and the collisions. At the end, ELIGE_MUSICA (0x5168) looks at what the player is up to and asks for the music that fits; since the highest number wins in each channel, it can ask every frame without cutting off whatever is already playing.

The sound

Three 14-byte channels at 0xE010, and a table of 40 sounds at 0x4AD8 pointing at 36 tracks. The sound number also says how many channels it wants: below 7, only channel C, which is the effects one; from 7 to 14, two; from 15 up, all three. And if a channel is already playing something with a higher number, the new one does not take its place.

A track is a run of bytes: the high nibble is the note and the low one the length, 0x2n changes the length of whatever follows, 0xFF silences the channel and 0xFE is end-or-jump —with a loop count, which is how repeats are done. The notes come from fifteen PSG periods (0x4AC9), one octave and three notes over, each one the previous times 0.944, which is the semitone; reading three bytes earlier drops three semitones at once, and that is what bit 6 of a channel does.

The background and the map

Here is what sets this cartridge apart, and it has its own page: The pixel scroll. In short: the background characters are in VRAM eight times over, each copy a pixel lower, and scrolling the screen means adding 24 to the character number and writing the whole name table again, 24 rows by 22 columns, on every frame.

The road is not stored as screens but on four floors:

position in pixels
  --> stretch (0..12) and row within the stretch (0..23)
        --> plan (0x53A2): 10 plans x 13 stretches
              --> stretch (0x5424): 19 stretches x 6 pieces
                    --> piece (0xE500): 44 pieces of 24 rows

A stage is thirteen stretches of 24 rows, that is 2496 pixels, and each stretch is six pieces of four columns laid side by side. The 44 pieces are built in RAM by gluing groups of three rows from a dictionary of 25, with the index list at 0x550B: 328 bytes for the game's whole map.

The player and the creatures

The player's state (0xE120) comes straight from the controls through a sixteen-entry table (0x5D32), and everything hangs off it: the animation, the sprite that gets loaded and which way the shot goes. Before moving, the character ahead is checked with four probes (0x5B6A), and the comparison is made against 0x40 + 24 × phase because background characters change number with the scroll offset.

The creatures are ten 16-byte slots with 8.8 fixed-point velocity, and twelve routines, one per type. The chase engine (0x6B72) does not turn on a sixpence: it eases the current velocity towards the one that would reach the target, and that is why they trace those curves instead of heading straight for the player.

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, 53 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)
Knightmare 22 249 PISTA_NOTA (0x49D6) = toca_la_nota
Hyper Sports 3 20 229 PISTA_MANDOS (0x4A31) = mandato_de_pista_larga
The Goonies 18 205 PISTA_NOTA (0x49D6) = toca_la_nota
King's Valley 16 203 PISTA_MANDOS (0x4A31) = procesa_comando_de_control
Konami’s Soccer 16 176 PISTA_NOTA (0x49D6) = escribe_el_periodo_y_el_volumen
Hyper Rally 15 158 PISTA_NOTA (0x49D6) = CANAL_TONO_60BA
Yie Ar Kung-Fu II 15 147 ESCRIBE_ROTULO (0x4393) = pinta_guion
Konami's Ping Pong 14 142 ESCRIBE_ROTULO (0x4393) = pinta_rotulo
Yie Ar Kung-Fu 14 153 ESCRIBE_ROTULO (0x4393) = dibuja_la_lista
Konami's Boxing 13 133 ESCRIBE_ROTULO (0x4393) = 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.