Under the hood

Getting started

A commented disassembly of Yie Ar Kung-Fu, Konami's RC-725 for the MSX, a 16 KB cartridge in page 1 (0x4000–0x7FFF). It reassembles into the exact ROM, byte for byte, and all 16,384 of its bytes are accounted for.

The cartridge is not here

No repository distributes the game. Put your own dump in the root as yiear.rom, 16384 bytes, sha256

644c78c7d20bb30802888b513b384cf0ce4f9d8b6fd490f30e6211ac92274848

make comprueba checks it.

Two dumps of this cartridge circulate, which the archives call easy and hard. The listing is the easy one, the hash above. If you have the other one too, put it in as yiear_hard.rom and tools/coteja_builds.py will compare them; the result is in Findings.

What each command does

make            traces the flow, builds the listing, reassembles it and runs the tests
make verify     the test that settles it: reassembling must give the ROM back
make sanity     that not a byte is left unexplained, and no data comes out as code
make densidad   how much is commented, routine by routine
make imagenes   draws the graphic data blocks, so you can look at them
make web        rebuilds this site from the ROM and the notes

How it is put together

The listing is not edited by hand: tools/mkasm.py generates it from a flow trace and a file of address-anchored notes, so the comments survive a retrace. The tracer follows the flow from the entry points; the ones it cannot work out on its own — the interrupt hook and the fifteen dispatch tables — are declared in src/yiear.entries, each with its justification.

The pictures on the front page are not captures. tools/pantallas.py draws them by running in Python the cartridge's own steps: the script interpreter at 0x442C, the tile-list one at 0x44A6, the fills and the mirror at 0x4C46. And they are not taken on trust: tools/omsx_vram.tcl dumps openMSX's VRAM during a game and it is compared byte for byte. The colour table comes out with zero differences across 6,144 bytes and the pattern table with zero across 6,144.

The tools

tools/z80trace.py        the flow trace, with the tables declared
tools/mkasm.py           the listing, from the trace and the notes
tools/guiones.py         the script interpreter at 0x442C, in Python
tools/textos.py          the tile alphabet and the list interpreter
tools/pantallas.py       the screens, built step by step
tools/cierra_datos.py    where each block ends, by running its interpreter
tools/apunta_a.py        who points into a range, searched across all 16 KB
tools/coteja_builds.py   the two builds, collated by instruction
tools/presupuesto.py     how the 16,384 bytes are accounted for
tools/densidad.py        how much is commented, routine by routine

The cartridge

16,384 bytes in page 1, from 0x4000 to 0x7FFF. The BIOS reads the AB header and calls INIT.

The header

The first ten bytes are 41 42 10 40 00 00 00 00 00 00: it declares only INIT, and here INIT sits right against the header, at 0x4010. No STATEMENT, no DEVICE, no TEXT.

How the 16,384 bytes are accounted for

  bytes  
traced code 7,494 45.7%
identified data 8,890 54.3%
unexplained 0 0.0%

tools/presupuesto.py counts it, crossing the flow trace with the ranges declared in the notes. The check is not that the numbers add up: it is that every range has a name and an explanation.

The VRAM map

The eight values at 0x4503, R0 to R7, are 02 E2 0E 7F 07 76 03 E0, and the whole map comes out of them:

  where from which register
COLOUR 0x0000 R3 = 0x7F
SPRITE PATTERNS 0x1800 R6 = 0x03
PATTERNS 0x2000 R4 = 0x07
NAMES 0x3800 R2 = 0x0E
SPRITE ATTRIBUTES 0x3B00 R5 = 0x76

R3 and R4 are not addresses: they are a base bit and a mask. What they say is colour below and pattern above, the reverse of the usual layout. And R1 = 0xE2, with bit 1 set and bit 0 clear, means 16x16 sprites, unmagnified.

These eight values are the same as two other cartridges of the same family except for R7, the border colour.

The two drawing interpreters

The cartridge does not store VRAM dumps: it stores short programs that get interpreted. There are two, and it pays not to mix them up.

SCRIPTS (the interpreter at 0x442C). They write through the VDP's data port, byte by byte, with two commands:

0x00           end
0x80           chain: the next two bytes are a new VRAM address,
               LOW BYTE FIRST
n (bit 7 clear)  fill: the next byte, repeated n times
n (bit 7 set)    raw copy of (n and 0x7F) bytes

TILE LISTS (the one at 0x44A6). They write into the name table with WRTVRM, one tile per byte, passing each through a mask that decides whether it is drawn or erased:

0xFF   end
0xFE   chain, the same as the other one's 0x80
rest   the byte is a tile

And every list begins with its address, because 0x449E — the normal door — only sets the mask and falls into 0x44A0, which is the body of command 0xFE: the first thing it does is read two bytes. The only entry that skips that is 0x449A.

The alphabet

It is not ASCII. A is tile 0x21 and on from there to Z at 0x3A; the digits run 0x10 to 0x19, the space is 0x20 — and 0x00, the blank tile, does the same job — and the copyright circle is 0x1A.

No need to assume it: KONAMI, PERFECT and GAME OVER read out on their own with that arrangement and no other alignment gives them.

The fifteen dispatch tables

The cartridge dispatches two ways. The first, 0x4071, has the table written behind the call itself: pop hl takes it off the stack, A doubled indexes into it, and whatever pointer comes out is executed with jp (hl). Since the return address has been popped, that does not come back.

The second, 0x7375, indexes a table of tables by the opponent and dispatches whichever comes out.

Without declaring all fifteen the static trace falls well short: the six of the per-opponent dispatch alone hide 315 bytes of code.

The two builds

There are two different Yie Ar Kung-Fu cartridges, and they do not play the same. This is what is in each:

  easy hard
sha256 644c78c7… 61c86ad4…
how often the opponent reacts always 1 frame in 32 1 in 32, 16 or 8, by level
CHEN's long weapon lasts 18 frames, grows to 8 lasts 22, grows to 10
the three height thresholds three inline cps a three-byte table, one value changed
Konami's hidden mark does not carry it carries it: RC-725 and the title in katakana
this repository this is the one disassembled collated instruction by instruction

At the highest level the hard cartridge's opponent reacts four times as often as the easy one's, and on the easy cartridge that never happens at any level. Everything else is five constants and one change of form.

Byte for byte the two differ in 15,618 of 16,384, 95.3%. That figure means nothing — both are relocated. By instruction they share 97.0%. The detail of every difference is in Findings, and tools/coteja_builds.py produces it.

The code

Everything happens inside the interrupt

INIT (0x4010) hooks the handler by hand: it puts a 0xC3 — a jp — into H.KEYI (0xFD9A) with the address 0x4043 behind it, leaves the stack at 0xE400, clears 0xE000–0xE3FF with an ldir, loads the graphics and settles into a jump to itself. From there on the whole game runs inside the interrupt.

The handler at 0x4043 reads the VDP status, gives the sound engine a frame, and with the latch at 0xE006 open reads the controls and does the game frame. On the way out it looks at the status again: if another interrupt is pending, it gives sound another frame.

The scene dispatch

0xE000 is the scene — 0 to 8, the table at 0x409F — and 0xE001 the step within it. Every scene starts with a chain of djnz, which is the shortest way there is to write a switch: B comes in holding the step, and each djnz takes one off it and jumps if it has not reached zero yet.

Scene 0 is the title screen: step 0 builds the whole screen, step 1 draws VIDEO CARTRIDGE, step 2 erases it and step 3 paints the logo and the PUSH SPACE KEY.

The controls

The joystick and the keyboard are read separately and joined with an or, so it does not matter which you play with. The point is that both end up in the same bit layout, the PSG's port A:

bit 0  up           bit 3  right
bit 1  down         bit 4  trigger A
bit 2  left         bit 5  trigger B

Getting there from the keyboard takes five chained rotations, which is what 0x453F does. That all five line up is not a guess: with the MSX matrix's normal arrangement — row 8 with SPACE at bit 0, LEFT at 4, UP at 5, DOWN at 6 and RIGHT at 7 — each and lands exactly where it should.

With bit 6 of 0xE002 clear nobody is playing and 0x4564 makes the controls up: that is how the attract sequence runs, stepping through the list at 0x55DE one position every 0x20 frames.

How the player is built

The player is made of sprites. 0x4FBE spreads the six patterns at 0x50A6 across the pattern field of the first six sprites — two at a time, so as not to touch coordinates or colour — and then runs whichever recipe 0xE154 picks from the table at 0x50AC.

A recipe is a run of fields with the high nibble as the marker:

marker what it does
0x7- the low nibble goes to 0xE174, the bitmap the strikes consult
0x8- the low nibble goes to 0xE172, the pose the strike is checked with; and mid-loop, it closes the list
0xF- the low nibble is used to work out the strike's X, 0xE171
rest sprite bytes: the first adds into the Y and the second into the X

And there are two bodies, not one. Facing right it uses 0x5064, which adds straight. Facing left, 0x5021 reads a pointer to another recipe out of the data itself and runs 0x5028, which also mirrors the second coordinate with ld a,010h / add a,(hl) / neg / add a,b.

How the opponent is built

The opponent is made of tiles. 0x5374 writes tile numbers into the name table: it walks down to the row 0xE182 says, places the column with 0xE183 and copies the block row by row with LDIRVM.

What is unusual about it are the sums from 0x5387 to 0x53C1, which clip: if the figure runs off the left or the right of the thirty-two columns, the width is shortened and the source moved on, rather than drawing over the next row.

And the poses come compressed. 0x541D decompresses them into 0xE280 with three commands — 0xF- for consecutive tiles, 0xE- for gaps and any other byte for a single tile — and, if the pose is odd, 0x5454 writes them out again backwards into 0xE2D0, swapping each tile for its twin.

The hit boxes

0x52F5 takes four bytes from the opponent's recipe, adds the X at 0xE16E to each and writes a pair into 0xE198: the value, and the value plus 10, plus 8, plus 6 and plus 6. Four horizontal bands, each with its start and its end, from widest to narrowest.

And 0x4A0F is what uses them: it takes the strike's X from 0xE171 and sees if it falls inside one. Before that there are three cut-offs — the bit of 0xE173 that matches the pose, the opponent's pose not being 0x0E or above, and the player's Y having reached 0x72.

The strike's height is kept separately, in the table at 0x4B54: three values, 0x84, 0x91 and 0x9F — high, middle and low.

The sound engine

Three voices, with the channel table in RAM and eight bytes per channel. There are two areas: 0xE01A for music and 0xE032 for effects, and the tune number decides which — below 8 it is an effect, from 8 up music, and from 0x0C up it uses three channels instead of two.

The number's six low bits index the pointer table at 0x7DB6, which has twenty-one entries. The first points nowhere, so the useful range runs 1 to 20, and those twenty go to sixteen distinct tunes: several share the one at 0x7FEF, which is a single 0xFF — the tune that does not sound.

There are two tune formats. The short one carries duration, envelope and notes. The long one, used when bit 7 of the number is set, adds the duration unit, the decay, the octave and the semitone.

The notes. The table at 0x7DAC holds ten values: 106, 100, 95, 89, 84, 80, 75, 71, 67 and 63. The ratio between each and the next is 1.06, which is exactly the semitone's (2 to the power 1/12 = 1.0595): they are ten consecutive semitones. And they are a PSG divider, not a speed and not a frequency: the lower the value, the higher the note. The octave is applied by doubling the divider, which drops it an octave each time.

Priority. When 0x7BCC is entered with D at zero — which is how the whole game enters — the tune plays, no questions asked. But from inside the engine itself it is entered with D = 1, and then 0x7BF3 compares against what is already playing on that channel and does not overwrite it if the current one is equal or higher.

In the emulator

The pictures on this site are not captures: tools/pantallas.py builds them by running in Python the cartridge's own steps. Looking at them is not enough to take them on trust, so they are compared byte for byte with the real VRAM.

How the real VRAM is taken

tools/omsx_vram.tcl lets the game run in openMSX and, at several points, dumps the 16 KB of VRAM, the eight VDP registers and the variables that say what was being drawn:

"C:/Program Files/openMSX/openmsx.exe" -machine Philips_VG_8020 \
    -cart yiear.rom -script tools/omsx_vram.tcl

It sets no breakpoints. The whole game runs inside the interrupt at 0x4043 and a bp there drowns the emulator, so the dumps go by emulated clock. It also carries a real-time watchdog: a broken script cannot hang the emulator indefinitely.

The result

Comparing the VRAM pantallas.py builds with the one openMSX holds 120 seconds into a game:

table bytes differing
COLOUR (0x0000–0x17FF) 6,144 0
PATTERNS (0x2000–0x37FF) 6,144 0
NAMES (0x3800–0x3AFF) 768 43
SPRITE PATTERNS (0x1800–0x1FFF) 2,048 520

The two big tables come out exact. What does not match is what the game rewrites every frame and the build has no business placing:

The buffer trap

The sprite patterns and part of the tiles are reused several times per frame. If a dump comes out "all different except one thing", the fault is not the format: it is the moment it was taken.

That is why the dump worth comparing is one taken with the fight running and settled, not one from the startup or from a transition between scenes.

A mistake only the comparison caught

The mirror loop at 0x4C46 was read at first as though the source advanced a third each turn. The renders came out plausible — a temple showed up — and only the VRAM comparison brought it down: the source does not advance, and each turn the cartridge does pop de / ld hl,00800h / add hl,de / ex de,hl / pop hl, which adds 0x800 to the destination and recovers the starting HL.

With that fixed, the pattern table went from 776 differing bytes to zero.

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, 52 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)
Yie Ar Kung-Fu II 35 370 lee_el_teclado (0x453F) = lee_las_filas_7_y_8_del_teclado
Sky Jaguar 26 356 un_cuadro_de_juego (0x407B) = PASO_DE_ESTADO
The Goonies 21 208 dibuja_la_lista (0x449E) = pinta_guion
Knightmare 19 206 interrupcion (0x4043) = cada_cuadro
Mopi Ranger 18 247 suma_a_la_puntuacion (0x4319) = suma_al_marcador
Hyper Sports 3 17 205 lee_el_teclado (0x453F) = teclado_del_jugador_1
Konami's Ping Pong 17 185 lee_el_teclado (0x453F) = lee_teclado_1
Road Fighter 17 162 dibuja_la_lista (0x449E) = escribe_rotulo
Hyper Sports 2 16 217 empieza_tapando (0x43AC) = PINTA_CIFRAS_SIN_CEROS
Konami’s Soccer 16 165 dibuja_la_lista (0x449E) = escribe_un_rotulo

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.