Under the hood

Getting started

The repository ships the listing already generated, but what matters is that it can be rebuilt from the cartridge and that the result is the ROM byte for byte. That is what makes the notes believable.

What you need

The cartridge

It does not travel with the repository. Put it in the root as soccer.rom, 32,768 bytes exactly:

b9536809a1784afb2e49a43d80ba9c6c8c15cd31ecbd6c284e9bf62c6927dcd4

To check it is the same one:

make comprueba

Rebuilding everything

make

That chains the four things that matter:

step what it does
make listado traces the flow from the entry points and writes src/soccer.asm
make verify reassembles with pasmo and compares the sha256 against the cartridge
make sanity what reassembling CANNOT catch (see below)
make test the 39 tests

Why make verify is not enough

Reassembling proves the bytes are the same, not that what we say about them is true. A range of graphics read as code reassembles identically —the bytes do not change, only how they are read— and inflates the coverage on the way. That is why make sanity runs three more checks:

check what it watches
check_trace.py that the ranges in .nocode were not traced as code
check_datos_como_codigo.py that none of the 125 declared data ranges comes out as code
check_entradas.py that no entry point falls inside a data range
presupuesto.py that code and data add up to the 32,768 bytes, with no gaps

The rest

make densidad     # how many instructions carry a comment, routine by routine
make imagenes     # draws the five plates from the ROM
make vram         # checks them byte for byte against openMSX's VRAM
make web          # regenerates this site

Where everything is

file what it is
src/soccer.asm the listing, generated
src/soccer.notes the annotations: names, comments and data ranges
src/soccer.entries the entry points that cannot be deduced, each one justified
src/soccer.nocode the ranges the tracer must not step into
tools/ the tracer, the listing generator, the decompressor and the drawing tools
tests/ the 39 tests, which run without the cartridge

The tests do not need the ROM: they rebuild the data bytes by reading the defb rows of the listing itself, which carry their address in the comment.

The cartridge

The machine

An MSX1: Z80 at 3.58 MHz, TMS9918 VDP, AY-3-8910 sound generator, 16 KB of VRAM and 8 KB of RAM. The cartridge brings 32 KB of ROM and carries neither a mapper nor RAM of its own.

The two pages

At 32 KB the cartridge takes two pages of the memory map, from 0x4000 to 0xBFFF. The working RAM starts at 0xE000.

range what is there
0x4000-0x4010 the "AB" header and the entry point
0x4010-0x4025 a second "AB" header this cartridge never reads
0x4025-0x4C19 the skeleton: scenes, VDP, VRAM, sound, labels
0x4C19-0x5645 the presentation, the menu and the sound interpreter
0x5645-0x651E setting up the match, the frame, the clock and the goal
0x651E-0x6BD8 the penalty shoot-out
0x6BD8-0x6D18 the logo scene
0x6D18-0x853E the graphics and their loaders
0x853E-0x8787 the pitch map, compressed
0x8787-0x9408 the pieces, the ball physics and the aim
0x9408-0xA219 the pass, the shot, the goal lines and the restarts
0xA219-0xB4C3 the tactics and the intelligence
0xB4C3-0xB9F9 offside and the final screen
0xB9F9-0xBFF3 the trajectory and sprite tables
0xBFF3-0xC000 Konami's hidden mark

The header

The first four bytes say everything the BIOS needs:

41 42 70 40    "AB", and INIT at 0x4070

The other three entries —STATEMENT, DEVICE and TEXT— are zero, and so are the six reserved bytes. This is a single-entry-point cartridge.

0x4070 hooks the interrupt at H.KEYI and settles into the jr $ at 0x40BE. The whole game hangs off the interrupt: there is no main loop.

The second header, the one this cartridge does not read

At 0x4010 there is another "AB", with the 0x07 of RC-7xx and the 0x32 of RC-732 inside. Nothing here reads it. It is the header of the Konami Game Master, the cheat cartridge that plugs into the other slot and looks there for the addresses it may touch. Only four of Konami's cartridges carry it.

The VRAM, the wrong way round

The eight VDP registers come from the table at 0x4889, and they are

02 E2 0E 7F 07 76 03 E4
register value what it sets
R0 0x02 SCREEN 2
R1 0xE2 16K, screen on, interrupt, 16x16 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 border and backdrop

R3 and R4 are not addresses: they are base and mask. Here they leave the COLOUR table low and the PATTERNS high, exactly the opposite of the usual layout. From that comes a rule that holds for all ten graphics batches: the colour of a pattern dump sits 0x2000 bytes lower, at the same address. The patterns at 0x2828 have their colour at 0x0828, the ones at 0x2998 at 0x0998, and so on.

R7 changes with the scene: 0xE4 at start-up and 0x00 during the match, so the pitch's backdrop is black, not blue.

The sound

An interpreter of its own at 0x4E50, with 54 sequences pointed at by the table at 0x50DA. The tones come from twelve bytes at 0x50CE, one per semitone, and the octave is got by shifting left.

Konami's hidden mark

The last thirteen bytes of the cartridge, from 0xBFF3 to 0xBFFF:

... 0A 32 AA

Ten bytes with the title in katakana —コナミのサッカー— written backwards, its length (0x0A), the 0x32 of RC-732 and the closing 0xAA.

That this mark exists and what shape it has was discovered by Manuel Pazos, and this comes from him: all that was done here was checking that this cartridge carries it and that the number inside is its own.

The code

Everything hangs off the interrupt

0x4070 hooks H.KEYI and then sits still in a jr $. From there on, the whole program is what runs every fiftieth of a second. 0x40C8 dispatches the scene (0xE000) names with the nine-entry table at 0x40E3, and (0xE001) dispatches the step inside it with a chain of djnz.

variable what it is
(0xE000) the scene
(0xE001) the step within the scene
(0xE003) the frame counter, which doubles as randomness
(0xE004) the wait count
(0xE005) the reentrancy lock

The dispatcher with the table right behind

Almost the whole program is a state machine, and the mechanism repeats thirteen times: a call 0x4066 with the pointer table stuck right behind the call. The routine takes the return address off the stack, uses it as the base of the table, indexes with A and jumps.

How many entries each table has is written nowhere. It is fixed by a fit: no entry can point inside its own table, so the first one marks where it ends. That fit corrected two that looked like 24 and 29 entries and are 9 and 4.

The one with the push bc / ret

There is a fourteenth dispatcher that looks like none of the others. 0xB2CC takes the destination from the table at 0xB2DC, pushes it and does push bc / ret: a jp (bc), which the Z80 does not have, written by hand. To a tracer that is the end of a routine, and behind it sat 283 bytes nobody reached.

The decompressor

Almost all the graphics are compressed, in a one-command-per-byte format that writes straight to VRAM. L_47B7 does it:

command what it does
0x00 closes the block; it is the only thing marking where it ends
0x01-0x7F the byte that follows, repeated that many times
0x80 the two bytes that follow are a new VRAM address
0x81-0xFF copies the next (command & 0x7F) bytes as they are

Since the size is written nowhere, running the decompressor is the only honest way to know where each block ends. That is what tools/rle.py does, and it is where the limits of the 125 declared data ranges come from.

On top of that format there are two additions, and both save ROM:

The dumping doors, all in the same stretch:

address what it does
0x4680 LDIRVM, an uncompressed block
0x4684 FILVRM
0x4687 FILVRM in all three thirds
0x4698 decompresses into all three thirds
0x46A8 the same, mirrored
0x46B8 the same, changing the colour
0x47AF the destination is in the block's first two bytes

The match frame

0x5805 is what runs on every interrupt while the game is on, and the order matters: each routine relies on what the previous one left.

the referee and the rules        0x5818
the three offside sub-scenes     0x581B
the restart countdown            0x5828
which zone the ball is in        0x582B
switching player with the button 0x582E
who holds which position         0x5831
the machine picks a team-mate    0x5834
each pad's highlighted player    0x5837
the player-change beep           0x583A
the collision sub-state          0x583D
a frame of the tactics           0x5840
the two goalkeepers' logic       0x5843
everybody faces the ball         0x5846

And the drawing, afterwards:

save the side's background       0x5882
stamp the side onto the map      0x5885
put the other side in sprites    0x5888
dump the pitch to VRAM           0x588F
put the background back          0x5892

The match variables

variable what it is
(0xE100) the twelve pieces, 32 bytes each
(0xE280) the match sub-state
(0xE281) the ball sub-state
(0xE2A1) (0xE2A5) the ball's height and horizontal position
(0xE2AB) its vertical speed
(0xE2C1) where the pitch window sits
(0xE410) the position list of the twelve pieces
(0xE527) the side the camera follows
(0xE528) who has the ball
(0xE52C) (0xE52D) each pad's highlighted player
(0xE533) which restart is under way, from 1 to 10
(0xE600) the pitch map, 80 by 23

Inside a piece, the offsets that get used most:

+N what it is
+0x03 the heading, 1 to 8
+0x04 the height
+0x06 +0x07 the horizontal position, in 16 bits
+0x0A +0x0B the on-screen position; 0xE0 in the height means "not visible"
+0x0C the drawing group
+0x0D the drawing within the group; 4 is on the ground
+0x15 the piece number, 0 to 11
+0x16..+0x18 where it has to go
+0x1A the frames left before it reacts

The eight headings

They are used at every turn, and always the same way:

6  7  8
5  ·  1
4  3  2

The first side —pieces 0 to 5— attacks to the left and the second —6 to 11— to the right. It checks out three independent ways: where the receiver of a pass has to be for offside, which way the taker of a free kick faces, and which way he walks while getting into place.

In the emulator

The emulator here is not for taking captures: it is for checking that what we say is true. The pictures on this site are drawn by Python running the cartridge's own steps, and the emulator is what says whether those steps are read right.

Starting it

openmsx -machine Philips_VG_8020 -cart soccer.rom

It is a 32 KB cartridge with an "AB" header and a single entry point, so it starts on any MSX1 with 8 KB of RAM.

Dumping the VRAM

make vram

That does three things:

  1. launches openMSX with tools/omsx_vram.tcl, which sets not one breakpoint: the dumps are scheduled on emulated time, which is the only thing that does not choke the emulator;
  2. at seven moments dumps the 16 KB of VRAM, the eight VDP registers, the 4 KB of working RAM and a screenshot;
  3. compares the dumps against what tools/graficos.py builds, byte for byte, with tools/coteja_vram.py.

The script has a 240-second watchdog on real time: a broken script cannot leave the emulator hanging.

What the comparison says

titulo   COLOUR 0    PATTERNS 0    NAMES 0
campo    COLOUR 0    PATTERNS 0    SPR PATT 0
         MAP at 0xE600, 80x23:   2 of 1840
         WINDOW of 23x32:       44 of 736, and 42 are player patches

The title screen comes out identical across all three tables. On the pitch, the three graphics tables too, and the differences that remain are explained:

The comparison deliberately leaves out the sprite attribute table —it changes every frame— and tiles 0x5C to 0x63, which the match rewrites as it runs to stamp the patches. That the pitch map uses none of them —its tiles run from 0x01 to 0x58— is what makes it possible to draw the whole pitch without playing the game, and there is a test watching it.

The eight registers, checked

The table at 0x4889 says 02 E2 0E 7F 07 76 03 E4, and the emulator returns exactly that on the start-up screen. During the match only R7 changes, to 0x00: the pitch's backdrop is black.

Looking at the RAM

The dump includes the 4 KB from 0xE000 to 0xEFFF, and the whole state of the match can be read there: the two kit palettes at 0xE051 and 0xE056, the twelve pieces from 0xE100, the pitch map at 0xE600, and the four strides per frame at 0xE06A-0xE071. At level 1 they come out as 0x0100 and 0x0150 for the human's side and 0x00E0 and 0x0130 for the machine's, which is what the table at 0x57EF says.

One caveat

tools/omsx_vram.tcl crosses the menu by pressing the space bar at fixed times. If Konami changed its mind, or if a slower machine is used, the dumps can land on a different scene: each info_NN.txt carries the scene and the sub-state written down, so it shows up straight away.

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)
Knightmare 26 305 lee_el_mando_1 (0x48B9) = lee_mando_y_teclado
Yie Ar Kung-Fu II 25 276 latido (0x402E) = cada_cuadro
The Goonies 24 301 lee_el_mando_1 (0x48B9) = lee_mando_y_teclado
Hyper Sports 3 23 232 escribe_un_rotulo (0x46CE) = pinta_guion_de_tiles
Nemesis / Gradius 22 209 escribe_un_rotulo (0x46CE) = escribe_caracteres
Road Fighter 22 272 lee_el_mando_1 (0x48B9) = lee_el_mando
Hyper Rally 20 262 lee_el_mando_1 (0x48B9) = LEE_MANDOS
Konami's Ping Pong 20 221 escribe_un_rotulo (0x46CE) = pinta_rotulo
King's Valley 17 212 escribe_el_periodo_y_el_volumen (0x4FDE) = tono_con_periodo_explicito
Pippols 16 176 escribe_el_periodo_y_el_volumen (0x4FDE) = PISTA_NOTA

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.