Under the hood

Getting started

This repository does not ship the cartridge. It ships the commented listing, the tools that generate it and the checks that hold it up. With the ROM dump next to it, everything is reproducible with make.

What you need

bomberman.rom
eab39bab547089facf9df3021e729b65fba2b263c505325d034ed1b6e3df8eea
8192 bytes

make comprueba verifies the sha before anything else.

The one command

make all

It chains four steps, and the order matters:

step what it does
make listado traces the flow from INIT and generates src/bomberman.asm
make verify reassembles with pasmo and compares the sha256 with the original
make sanity checks that not one byte of the cartridge is left unexplained
make test the 64 checks on the listing and on what it claims

make verify is the one that decides whether the disassembly is trustworthy: if the 8192 reassembled bytes do not give the same sha256, the listing is lying somewhere.

The other commands

make densidad     # how many instructions carry a comment, routine by routine
make imagenes     # draws docs/imagenes from the ROM, capturing nothing
make cotejo       # boots openMSX, dumps its VRAM and subtracts ours from it
make alcance      # measures on the machine how far a blast breaks bricks

How it is laid out

src/bomberman.asm       the listing, generated
src/bomberman.notes     the comments, anchored to addresses
src/bomberman.entries   the entry points the tracer cannot work out
src/bomberman.nocode    the ranges that are NOT code, with the reason
tools/                  the tracer, the generator and the checks
tests/                  64 checks that do not need the cartridge
docs/                   this site, in both languages

The listing is not edited by hand. The comments live in src/bomberman.notes, each one anchored to an address, and they survive a re-trace: if the tracer changes its mind about where a routine starts, the comments still land where they should. make listado glues them on.

Why there are three annotation files and not one

Because each answers a different question, and keeping them apart puts in writing what is known and how it is known:

The cartridge

8 KB in page 1 (0x4000–0x5FFF), MSX1. It is the smallest cartridge in the series, and the first Hudson Soft one.

The header is code

An MSX cartridge starts with AB, the INIT address, and then STATEMENT, DEVICE, TEXT and six reserved bytes. Here INIT is 0x4004, which is exactly where those fields should begin. Read as instructions:

4000  41 42        "AB"
4002  04 40        INIT = 0x4004
4004  31 00 F0     ld sp,0F000h
4007  CD D1 4F     call 4FD1h        the RAM's starting values
400A  CD 62 50     call 5062h        the VDP
400D  CD 4E 40     call 404Eh        the PSG, with its table right behind

Only the first four bytes of the cartridge are really a header. The other nine are the first four instructions, and there is not one byte left for STATEMENT, DEVICE or TEXT.

The memory map

where what
0x4000–0x5FFF the cartridge
0xE000–0xE2FF the screen, 768 bytes: what is seen
0xE300–0xE389 the variables
0xE38A–0xE689 the board, another 768 bytes: what stays
0xF000 the stack

The two RAM screens are what organises the whole game, and they are explained in The code.

The VDP

The eight registers come from the table at 0x5144 and monta_el_vdp (0x5062) writes them out:

register value what it says
R0 0x02 SCREEN 2
R1 0xC2 16 KB, display on, 16×16 sprites
R2 0x06 name table at 0x1800
R3 0x7F colour at 0x0000, three banks of 0x800
R4 0x07 patterns at 0x2000, three banks
R5 0x38 sprite attributes at 0x1C00
R6 0x07 sprite patterns at 0x3800
R7 0xF0 white ink on transparent background

The sprite registers are declared and never used. Right after writing them, 0x507C puts a 0xD0 into the first attribute — the "no more sprites" marker — and the cartridge never touches the list again. Everything that moves in this game is a name-table tile.

No interrupt and no sync

monta_el_vdp puts a ret into H.TIMI (0xFD9F): the BIOS stops interfering on every interrupt. And the game does not wait for the display sweep: the pace is set by an empty loop of 0x500 turns at the end of cierra_el_cuadro (0x46C8). The screen is dumped when it is due, not when the VDP is safe.

The three graphics blocks

The format used by carga_los_graficos_en_los_tres_tercios (0x5088) is:

2 bytes   destination in VRAM; the third is added to the high byte
1 byte    how many tiles, and ZERO means 256
n*8       the patterns, which LDIRVM dumps in one go
pairs     (colour, how many tiles) for FILVRM, closed by 0xFF

It is called three times with A = 0, 8 and 0x10, which added to the destination's high byte give the three thirds of SCREEN 2.

block destination tiles what it brings
0x514C 0x0000 256 the whole title screen
0x5B26 0x0080 10 the letters of SCORE and BONUS
0x5B7C 0x0100 64 the digits, the monsters and the bomber

The three tile without a gap: 0x514C ends at 0x5B26, which ends at 0x5B7C, which ends at 0x5E38. That is what proves the format is read correctly: if the tile count or the colour-run count were off by one byte, the next block would not start where it starts.

The font changes halfway through

The big block loads the title screen's 256 tiles. When the game starts, 0x4074 and 0x407A load the two small ones on top: where the title screen has the big BOMBER MAN wordmark, the game puts the monsters.

Everything from tile 0x90 up is never overwritten, and that is where the H, D, F, A, M and I needed by the bottom label come from. So the in-game caption is written with tiles from three different blocks, and that is why there is no way to read a text out of this cartridge without drawing it.

The title screen's tiles

The in-game tiles

The sound

Two formats, both made of (register, value) pairs closed by 0xFF.

The short one is embedded right behind the call. vuelca_la_tabla_del_psg (0x404E) starts with a pop hl: it does not pick up a datum, it picks up the return address, which is where the table starts because the table is glued behind the call. When it is done it does push hl / ret and carries on executing after it. There are three such places, and they are the three ranges in .nocode.

The long one is kept with one pointer per voice, at (0xE305) and (0xE307), and sigue_los_dos_guiones_de_sonido (0x4025) advances it one frame per turn. Here each 0xFF closes the frame, not the script: the script ends with a double 0xFF. There are five: the door fanfare, the prize picked up, the enemy blowing up, the bomber's death and the footstep.

The code

3,557 bytes of code and 4,635 of data, 200 routines, and one idea that explains everything: the game has no map data structure. It reads the tile number.

Two screens in RAM, not one

where what
0xE000–0xE2FF the screen: what is seen. Every frame it is dumped whole to VRAM with an LDIRVM and left blank
0xE38A–0xE689 the board: what stays. The bricks, the flames while they last, the prize and the door once uncovered

Both have the same shape — 24 rows of 32 tiles — and the same cursor arithmetic: pon_el_cursor (0x5128) gives the address in the screen and pon_el_cursor_en_el_tablero (0x4F6F) the same tile in the board, by adding 0x38A.

That is where it comes from that the wall and the pillars cannot be broken: they only exist in the screen, pinta_el_tablero (0x4DB7) repaints them every frame, and they are not where the flames write. And the bricks only exist in the board.

What each tile means

tiles what they are
0x00–0x09 the digits
0x0A, 0x0E the prize and the door, two by two
0x10–0x19 the letters of SCORE and BONUS
0x20 empty tile
0x22–0x2F the enemy blowing up, seven frames
0x60–0x7B the bomb: four sizes times two of blink
0x80–0x87 the brick crumbling, eight frames
0x88, 0x89 the border wall and the fixed pillar
0x8A–0x8D the bomber standing
0xA0–0xBF the bomber walking: four directions times two steps
0xC0–0xCF the enemy alive, four frames
0xE0–0xEF the flame

And out of that come the three comparisons that repeat all over the listing:

The main loop is the game's index

bucle_principal (0x40CC): twenty calls in a row, one per system, in the order in which they paint over one another.

cierra_el_cuadro                 the counters, the dump and the sound
copia_el_tablero_a_la_pantalla   the board on top of the screen
pinta_el_tablero                 the frame and the pillars
avanzan_las_bombas               the countdown
pinta_las_bombas_y_las_llamas
pon_una_bomba                    reads the trigger
avanza_el_bombero                walks, or dies
mueve_los_enemigos
pinta_el_premio / pinta_la_puerta
pinta_los_enemigos
descubre_el_premio / descubre_la_puerta
pinta_al_bombero                 on top of everything
saca_los_enemigos_de_castigo
pisa_la_puerta_o_el_premio
descuenta_el_bonus
pinta_el_marcador
pinta_el_aviso_de_bomba_automatica

And three exits, which are the three ways a life ends: dead (0xE37B), door stepped on (0xE316) or stage cleared (0xE318).

The order is the logic. Each thing leaves its trace in the tiles for the next one to read, and that is why the bomber is painted before checking whether he has been killed: as he writes over the tile, he reads what was there.

There are two cut-down versions of the same loop: un_cuadro_sin_mando (0x4231), without anything that attends to the player, and un_cuadro_de_adorno (0x4253), without enemies. They are used in the celebrations and at game over: everything is still seen moving, but you are no longer in charge.

Everything is two by two

pinta_cuatro_casillas (0x4B7B) writes A, A+1 on top and A+0x10, A+0x11 below. The 0x1F in the middle is 32 minus the inc bc already done, and the 0x0F is 0x10 minus the inc a.

That is how the bomber, the monster, the bomb, the flame, the prize, the door, the brick and the pillar are painted: all of them. And the real board, in two-by-two cells, is 15 columns by 11 rows inside a 32-by-24 screen.

How a stage is sown

siembra_la_fase (0x4E35) keeps no map: it writes the bricks into the board and there they stay. The only thing noted separately is where the first one fell — hiding the prize — and where the last one fell — hiding the door — because 0x4EB1 only saves the prize the first time and the door is noted every time.

The rules of the draw, in order:

  1. two or more cells away from the bomber's spot
  2. of the two coordinates, one odd and one even. With both odd it would land on a pillar; with both even, on the cells the game always leaves clear
  3. none of the eight in the table at 0x4EDF, which are the neighbours of the four corners the enemies come out of

It does not check whether there is already a brick there, so two can land on the same cell and fewer than thirty-five come out. In the emulator dump there were 29.

The counter with a ceiling

It is how all the game's time is measured. avanza_un_contador (0x46BE) takes a pointer to a (count, ceiling) pair: it adds one and on reaching the ceiling goes back to zero. There are six pairs in a row from 0xE36F, and anyone who wants to do something "now and then" checks whether their count is zero.

pair ceiling what it measures
0xE36F 4 the bomber's step
0xE371 4 his death throes
0xE373 7 the enemy blowing up
0xE375 5 the enemies' step
0xE377 3 the bomb and the crumbling brick
0xE379 50 the bonus

All six ceilings come from the starting-values table at 0x4FDD and are never touched again. That is the speed of the whole game.

The two lists

Enemies at 0xE31C: eight seven-byte records, closed by the 0xFF at 0xE354.

+0  what it is doing: 0 free, 1 alive, 2..9 blowing up, 0xFF end of list
+1  column       +2  row
+3  drawing frame, counting 3, 2, 1, 0 and round again
+4  countdown to the next frame
+5  where it is heading: 0 left, 1 right, 2 up, 3 down
+6  unused

The +3 does two things at once: it picks the drawing and it decides the behaviour. When it is zero the enemy goes after the bomber, and that is precisely the red frame.

Bombs at 0xE359: five four-byte records.

+0  0 free, 1..4 the fuse, 5..12 the blast, 13 put it out
+1  column       +2  row       +3  count to the next frame

There is no other limit: the game has no "bombs remaining".

The blast, and the unrolled loop

pinta_la_explosion (0x4A8C) paints the centre without asking and walks the four arms with the table at 0x4F8D: eight strips of four tiles, one per side and per each of the two rows — or columns — it occupies. Four screen tiles per side are two board cells.

The loop is unrolled on purpose, one copy per tile, because each stops on something different:

tile stops on which routine it uses
1st the wall only pinta_una_llama_y_gasta_el_ladrillo
2nd brick, wall or pillar pinta_una_llama_y_gasta_el_ladrillo
3rd brick, wall or pillar pinta_una_llama_sin_gastar
4th brick, wall or pillar pinta_una_llama_sin_gastar

The difference that matters: the first two wear the brick down — adding one to its drawing every three frames until it disappears — and the last two return without writing if what they find is a brick. Since the first two are the board cell right next to the bomb and the last two the one after, the result is that the blast reaches two cells but only breaks the bricks in the first.

It is measured on the machine, not deduced: In the emulator.

A register trick worth keeping

si_toca_gasta_el_ladrillo (0x4B4E) has to add one to A and has not one register free. It solves it like this:

push af           A onto the stack
ex (sp),hl        and from the stack into H, which is where A now is
ld a,(0e377h)     the three-frame count
or a
jr nz,...
inc h             one more frame of crumbling
ex (sp),hl        H back onto the stack
pop af            and from there into A, already changed

The randomness

numero_al_azar (0x4EF9) feeds back (0xE384) with sixteen bits and mixes in the Z80's R register. It returns the low byte.

That mixing is not a decoration: if R advanced by the same amount between two draws, the series of cells collapses into three to five values and repeats. A test in the repository checks it with four different steps. On the machine that does not happen, because between two draws a varying number of instructions runs depending on how many times the draw had to be repeated.

In the emulator

Three measurements, all of them on the machine and all reproducible with make. No image in this repository comes from the emulator: the captures are for checking, not for publishing.

Booting it

openmsx -machine Philips_VG_8020 -cart bomberman.rom

The VG-8020's BIOS takes more than five seconds to call INIT. Before that the cartridge has not even copied the starting values into RAM, and a dump taken at three seconds is worthless.

The VRAM comparison

make cotejo

It boots openMSX with no window, lets the title screen run, presses space through the keyboard matrix (row 8, bit 0, which is what GTTRIG reads on input 0) and dumps the VDP's 16 KB twice, plus 1,792 bytes of RAM. Then tools/coteja_vram.py subtracts them from the VRAM that tools/vram.py builds.

==================================================================
 LA PORTADA, contra el volcado de t=8
==================================================================
  color                         0 de  6144  ok
  patrones                      0 de  6144  ok
  tabla de nombres              0 de   768  ok
==================================================================
 LA PARTIDA, contra el volcado de t=18
==================================================================
  color                         0 de  6144  ok
  patrones                      0 de  6144  ok
  marco y pilares               0 de   184  ok
  ladrillos que rompen la regla      0 de    29  ok
  premio y puerta bajo ladrillo      0 de     2  ok
==================================================================
 0 diferencias en todo lo que no decide el azar.

Each line says something different:

And a trap that cost a round trip. Comparing the VRAM's name table with the RAM screen says nothing: vuelca_la_pantalla (0x510E) dumps it and leaves it blank, so at any instant the RAM is half-built while the VRAM holds the last complete frame. The frame and the pillars have to be looked at in the name table.

The twenty differences that turned up on the title screen the first time were all explained, and two of them were findings:

Measuring the blast's reach

make alcance

The listing says the flame reaches two board cells per side but only wears down the bricks in the first. That can be read, but it can also be measured.

tools/omsx_alcance.tcl gets into a game, dumps the board, writes two bomb records by hand into the list at 0xE359 — state 1, column, row and count zero, which is exactly what pon_una_bomba leaves — and dumps the board again eight seconds later.

The two cells were picked by reading the board from an earlier dump: emulation is deterministic, so the same timings give the same deal.

  ladrillos antes  : 29
  ladrillos despues: 27
  bomb A: brick 1 cell  to the right in ( 5, 0) -> FALLS
  bomb A: brick 1 cell  below         in ( 4, 1) -> FALLS
  bomb B: brick 2 cells to the left   in (11, 0) -> STILL STANDING

Checking the auto-bomb mode

make fase4

It forces the stage to 4 by writing (0xE310) and dumps the name table four times in a row, to catch the bottom label in both halves of its blink. This comes out:

a row 23: '#                           @:3#'
b row 23: '#                           @:3#'
c row 23: '#  BOMB AUTO SETTING STAGE  @:3#'
d row 23: '#  BOMB AUTO SETTING STAGE  @:3#'

On every other stage that row carries the signature ` BOMBERMAN HUDSONSOFT 1983` and does not blink.

Tricks for looking inside

Seeing the board, which is where the real map is:

debug read_block memory 0xE38A 768

Skipping stages, which is what make fase4 does:

debug write memory 0xE310 4

Seeing who is hunting: byte +3 of the first enemy record.

debug read memory 0xE31F

When it is zero, that monster is coming for you — and it turns red.

And the golden rule for any dump of this cartridge: the RAM screen (0xE000) is always half-built. What has to be read is the VRAM name table (0x1800), which holds the last complete frame.