Under the hood

Getting started

All you need to reproduce this disassembly is Python 3, GNU make and Pasmo. The cartridge image does not travel in this repository: bring your own.

penguinadventure.rom    131,072 bytes
sha256                  525608aa990e1a19285edc98dec3f0aa11339d8a17641c89df3966845d10cc6a

With that file at the root of the repository:

make            # listing, verification, consistency and tests

What each step does

target what it does
make listado generates the sixteen .asm files from the binary, the notes and the seeds
make verify reassembles each bank and the whole ROM, and compares the sha256
make sanity checks that not one byte is left unassigned to code or data
make test the 30 tests
make densidad how many instructions carry a comment, bank by bank
make cifras rewrites the numbers in both READMEs by counting over the tree
make imagenes draws the 35 PNGs from the ROM
make web generates the HTML pages and checks the links

make verify is the one that decides. It must print OK: reproducible byte a byte seventeen times: once for each of the sixteen banks and once for the whole ROM. If one fails, the listing is lying.

Where everything is

Why the notes live outside the listing

Because the listing is regenerated whole every time. If the comments lived inside the .asm, any change to the tracing would lose them. Living in the .notes, anchored to their address, they survive the listing being rebuilt.

And out of that comes a check that is not cosmetic: tools/valida_c.py verifies that every comment lands on the first byte of an instruction. One anchored mid-instruction never appears in the listing, and nobody notices it was lost.

The cartridge

128 KB with the Konami mapper WITHOUT SCC (Konami4): sixteen 8 KB banks. There is no register for 0x4000-0x5FFF —bank 0 is fixed there— and the other three windows are chosen by writing the bank number to 0x6000, 0x8000 and 0xA000.

tools/reconocimiento.py measures this on the bytes themselves: not a single write to 0x5000, 0x7000, 0x9000 or 0xB000 —the registers of the mapper with SCC— and all 512 writes to the Konami4 registers carry a bank that obeys the rule, without one exception.

Banks are swapped in groups of three

Always the same dance, and every step has its reason:

di                  while the memory map is half done the interrupt, which
                    reads from the cartridge, must not come in
ld hl,0xF0F1        the three RAM mirrors, one per window and consecutive
ld a,N              the first bank of the trio
ld (0x6000),a       to the mapper register
ld (hl),a           and noted in its mirror
inc a / inc hl      the next bank and the next mirror
ei                  interrupts can come back

The mirrors at 0xF0F1, 0xF0F2 and 0xF0F3 exist because the mapper cannot be read: writing to 0x6000 sets the bank, but there is no way to ask which one is there. Anyone who needs to put it back has to have written it down.

What is in each bank

banks what they hold
0 the core: interrupt, decompressor, painters, the bridges to the data
1, 2, 3, 9, 14 code
4 to 8, 10 to 13, 15 data: characters, backdrops, stage scripts, sound

The data banks never execute an instruction: they are mapped, read and put back. p00:479D is the clean example —it maps banks 12 and 13 only to read four bytes of the table at 0xACBA— and from 0x47E2 onwards there is a long row of bridges doing exactly that. They live in the fixed bank because it is the only place from which you can switch banks without pulling the floor out from under yourself.

What INIT does

The header at 0x4000 is AB with INIT at 0x406A, and that routine does four things before never returning —it ends in a jr to itself—:

  1. sets the trio 1-2-3 and notes it in the three mirrors;
  2. reads the primary slot register with RSLREG and keeps the bits of page 1, which is where the cartridge is; with ENASLT it puts that same slot into page 2, so the cartridge spans 0x4000 to 0xBFFF;
  3. hooks its interrupt into H.KEYI (0xFD9A) with a jp;
  4. wipes the 0x10EF bytes from 0xE000 to 0xF0F0 in one go, plants the stack right there and releases the semaphore at 0xE005.

The second header: the Konami Game Master

At 0x4010 there is another header, CD, which is the one the Konami Game Master reads. It declares thirteen stages —which are not the twenty-four the game has— and gives it the addresses of the things the Game Master knows how to touch: the game data, the score and the routine to jump into.

That routine is arranca_en_la_fase_pedida (p00:40F7), and not one instruction of this cartridge calls it. Its first instruction says whose it is: it reads 0xD31A, which is Game Master RAM —where the stage the user asked for in its MODIFY MODE is written—, and a value of this game would have no business there.

The VRAM map, which is upside down

The eight bytes p00:44AE writes from register 7 down to 0 give a layout that is not the one almost any MSX1 game uses:

where what
0x0000-0x17FF colour table (R3 = 0x7F)
0x1800-0x1FFF sprite patterns (R6 = 0x03)
0x2000-0x37FF pattern table (R4 = 0x07)
0x3800-0x3AFF name table (R2 = 0x0E)
0x3B00-0x3B7F sprite attributes (R5 = 0x76)

Colours below patterns. R3 and R4 are not addresses but base and mask, and reading them the wrong way round gives correct shapes with striped colours.

Konami's hidden mark

At the end of the ROM is the mark Manuel Pazos documented: the catalogue number —RC-743— and the title in katakana, written with a syllabary of its own. tools/marca_konami.py extracts it and checks it. The finding is his; here it is only verified on this cartridge.

The code

The state machine

Sixteen states and, inside each one, as many substates as needed. The trick is the ld bc,(0xE000) at p02:800F: one instruction loads both —the state in C and the substate in B— and then the house dispatcher jumps on C while each state walks B down with djnz until it reaches the substate due. Leaving a substate is as short as inc (hl) on 0xE001.

And a detail at p02:800B: in states 0, 1 and 2 the address 0x8A55 is pushed onto the stack before dispatching, so that when the state does ret it does not go back to its caller but there. It is a way of chaining without spending a call.

The attract cycle is 0 → 1 → 2 → 0. State 3 —the title screen with PUSH SPACE KEY— is only entered by pressing something.

The house dispatcher

call despacha (p00:4060) with the index in A, and the pointers sit glued right behind the call itself: the routine pops the return address off the stack, uses it as the base of the table and jumps. It spends no register passing the table around, which is why in the listing the defws appear immediately after the call.

The three script formats

The cartridge does not have one decompressor: it has three ways of emitting bytes, and all three are run in Python by tools/graficos.py to draw the images on this site.

The compressed one (p00:418C to RAM, p00:4386 to VRAM). A destination word and then orders:

byte what it does
0x00 done
bare 0x80 another destination word follows
bit 7 set the low seven are how many bytes go through as they are
bit 7 clear the low seven are how many times the next byte repeats

Each byte also goes through p00:43F9, which is where it gets mirrored —a mirrored character is the same byte read backwards, p00:4402— or has its colours swapped in pairs (p00:440D).

The masked one (p00:42BC and p00:42BE). No compression at all: a destination word, 0xFF ends it, 0xFE opens another run and everything else is a byte as it is. The interesting part is the and c at p00:42C9: with the mask at zero what gets written is zeros, so the same script both paints a caption and erases it. That is what the shop does when it closes.

The block one (p00:43B3). Uncompressed bytes sixteen at a time, and bit 0 of H rules: if it is set, each column is painted twice, the second one mirrored. That is why the ROM holds half a figure and not the whole one.

The objects, and the three coordinates

There are three sets of slots, and the first one explains how the game is built.

0xE310: three slots of 0x20 bytes. Each object has three coordinates, not two, each with its own 16-bit speed:

+0x00  what class of object; a zero means a free slot
+0x02  the screen column        +0x03  the row
+0x06  X, 16 bits               +0x0C  its speed
+0x08  Y, 16 bits               +0x0E  its speed
+0x0A  Z -the DEPTH-            +0x10  its speed
+0x12  whether it moves by itself

The Z is what makes things come towards you: p09:A966 works out the screen column by subtracting the depth from the X and keeping the high byte, and that is what gives the perspective. And p09:AA18 picks one of four drawings from the high byte of the X: the same creature from larger to smaller.

0xE370: three slots of 0x10 bytes, with two coordinates and their speeds.

0xE3A0: three slots of 0x10 bytes for what flies through the air. Each one carries two sprites, not one: row, column, drawing and colour for the first, and row, column and drawing for the second, which inherits its colour. That is the house way of painting a two-coloured figure on an MSX1, where a sprite has one colour.

What you control

Four sprites in a two-by-two block, placed by p03:A8DB from (0xE204, 0xE205) —row and column— and the pose set by p03:A8F5 from the table at 0xA91D: thirteen poses of eight bytes, four (pattern, colour) pairs each.

The state of what you control is 0xE203, and it is not an animation counter: it is what nearly everything hangs off. States 3, 4, 8 and 10 are the ones where nothing collides; p02:9751 sets it to 1 or 2 on a jump, p03:B33B to 15, and p02:925D to 0x15 or 0x16 on losing. p00:42FB only consults it to decide where to split the sprite table on upload: in state 4 it moves sprites 4 and 5 to the front, and in 16 the eight at 0xEEE0, which on a VDP that paints only four per line is giving them priority.

The screen mirror

The RAM from 0xEBA0 to 0xEEFF is an exact copy of the VRAM from 0x3820 to 0x3B7F, 0x4C80 apart: 0xEBA0 is row 1, 0xEBE0 row 3 —where the play area starts— and 0xEE80 the sprite attribute table. The screen is built in RAM and pushed in one go with outi, which is the only way to fill the VRAM at frame rate.

And mind the loop count at p00:4270: ld b,d comes in with 0x40, but each turn drops B twice —once the outi, which is "output and decrement", and once the djnz— so it moves 32 bytes per turn and not 64. And 32 is exactly one row.

Real trigonometry

The curves are written in the ROM and read back: a 24-step circle and a 32-step parabola. But to aim at the player, bank 9 computes:

The angle comes out in the house's 256 units of a circle, and p09:AD27 breaks it back into its two components by reading the 65-entry curve at 0xAD77 twice, at B and at 0x40 − B.

Randomness

There is no generator. The randomness comes from the Z80's R register, the memory refresh counter, which goes up by one for every instruction the CPU fetches. p00:5630 uses it to pick one of four backgrounds, p09:AADA for a creature's side and shove, and p01:79A1 for the gambling machine's reels.

It is cheap and it is not random: R is a counter, and two reads separated by a fixed piece of code differ by a fixed amount.

In the emulator

Almost everything on this site comes from reading the binary and running its routines in Python. But some things are only settled by watching them run, and the two keyboard codes are one of them.

The machine matters

With Philips_VG_8020 this cartridge does not start: the MSX sits on the BASIC boot screen and never runs. With C-BIOS_MSX1_EU it does. You can tell in two places:

If you look at the screen and see BASIC, this is why. And if you check those two addresses before starting, it saves you an afternoon.

Checking the two codes

PA_OUT=work/omsx PA_CLAVE=NORIKO openmsx -machine C-BIOS_MSX1_EU \
    -carta penguinadventure.rom -script tools/omsx_claves.tcl

The probe boots, presses space to get into the title screen, types the word letter by letter on the key matrix and reads 0xF0F7 and the queue at 0xF0F8. With PA_CLAVE=KAZUMI it tries the other one.

What comes out, and it is what the listing predicted byte for byte:

pulsada N (fila 4 mascara 08) -> cola 00 00 00 00 00 04
pulsada O (fila 4 mascara 10) -> cola 00 00 00 00 04 05
pulsada R (fila 4 mascara 80) -> cola 00 00 00 04 05 06
pulsada I (fila 3 mascara 40) -> cola 00 00 04 05 06 01
pulsada K (fila 4 mascara 01) -> cola 00 04 05 06 01 02
pulsada O (fila 4 mascara 10) -> cola 04 05 06 01 02 05
0xF0F7 DESPUES = FE
VERDE: 0xFE, o sea NORIKO

And with KAZUMI, the queue 02 00 08 07 03 01 and 0xF0F7 at 0xFF.

The four traps, which cost an afternoon

The state. The code watcher only runs in state 3, which is the PUSH SPACE KEY screen. The attract cycle goes 0 → 1 → 2 → 0 and state 3 is not reached on its own: you have to press something to get in. Typing the word during the demo does absolutely nothing, and you cannot see that by looking at the screen.

type will not do. It holds the key too briefly and the watcher, which only notes a key when it changes from released to pressed, never sees it. You press the matrix by hand with keymatrixdown and keymatrixup, and you have to release between keys: two presses in a row without a release in between are one press.

The first key is lost if it is pressed at the very instant state 3 is entered. Half a second of breathing room and it registers.

Screenshots come out stale with throttle off: the renderer does not refresh and you see the screen from several seconds ago. To take pictures you have to run at real speed.

Starting on whichever stage you like

There is no need to play the whole game. The stage is at 0xE092, from 1 to 24, and it can be written from the emulator console:

debug write memory 0xE092 12

Mind the two tables at p00:4105 and p00:4113, which only cover the first thirteen of the twenty-four: that is one of the open questions.

Checking the drawings against openMSX

The images on this site are drawn by reading the cartridge's tables with our own code. What says they are right is not looking at them: it is comparing them with emulator dumps.

tools/omsx_fases.tcl forces the stage and the level at p00:46E3 —where both the normal set-up and the Game Master one go through— and dumps VRAM, RAM and the VDP registers every so many frames, on every state change and at every cut of the ending; and it logs every object that comes out on the road (p01:6852). To reach the end of the long stages it sets the step period (0xE4C0) to 1 right before walking, at p00:4560, and it keeps 0xE1F1 so the penguin does not die. The scenes, the map, the fight and the shop have their own probes (tools/omsx_escenas.tcl, omsx_mapa.tcl, lanza_pelea.sh and omsx_tienda.tcl).

make coteja compares against those dumps:

what how many differences
each stage's screen 24 stages 0 bytes
the ending: goal and dinosaur 144 cuts 0
the penguin, pose and placement 1,107 frames 0
the creatures, drawing by class and distance 510 objects 0
space 9 screens, 8 fish, 13 meteorites 0
the road objects, LEVEL 1 and 2 5,414 0
the screen mirror, walking the stage 424 dumps 0
the WARP, backdrop 9 21 screens 0
the tree and the two endings 3 scenes 0
what moves in the scenes 1,337 dumps 0
the fight with the dinosaur 3,246 dumps 0
the map before every stage 32 dumps 0
the shop: the three shopkeepers, open and closing 14 dumps 0

Two traps. At the dump point (p00:451C) the VRAM name table lags one frame behind the RAM mirror, so cells are compared in the mirror. And stages 12, 18 and 24 never end without the item at 0xE16C: a late dump is on another pass, with the script indices the same and the slots not.

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, 41 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)
Twin Bee 24 279 pon_la_mezcla (p14:8049) = pon_el_mezclador
Q*bert 20 232 destapa_una_columna (p02:8AB8) = destapa_una_linea_del_logotipo
Hinotori 12 104 pon_la_mezcla (p14:8049) = pon_mezclador_del_canal
Knightmare 12 162 salida_de_la_presentacion (p02:8A55) = mira_si_se_ha_pulsado_algo
Nemesis / Gradius 12 139 pon_la_mezcla (p14:8049)
Metal Gear 11 102 pon_la_mezcla (p14:8049) = mezclador
Konami’s Soccer 11 115 llena_los_tres_tercios (p00:4293) = rellena_los_tres_tercios
Shalom: Knightmare III 10 101 maquina_de_estados (p02:8000) = cuadro
Yie Ar Kung-Fu II 10 97 llena_los_tres_tercios (p00:4293) = rellena_los_tres_bancos
King's Valley II 9 100 mira_hacia_donde_se_pide_ir (p03:A84B)

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.