Under the hood

Getting started

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

qbert.rom    32,768 bytes
sha256       bd253f3285b3bf31501cd34f593f35ed3b9adfc6eb44c360c6fd59f8ab2c3684

With the file at the root of the repository:

make            # listing, verification, consistency and tests

What each step does

command what it does
make comprueba checks the ROM's sha256
make listado traces the flow and generates src/qbert.asm from the binary and the notes
make verify reassembles the listing and compares its sha256 with the ROM's
make sanity checks that no data comes out as code and that no byte is left unassigned
make test the tests
make densidad how many instructions carry a comment, routine by routine
make imagenes draws the web's pictures from the ROM
make coteja checks those screens against the openMSX dumps
make web generates the HTML pages and checks the links

make verify is the one that decides: it has to end with OK: reproducible byte a byte.

Where everything is

file what it is
src/qbert.notes the names, comments and data blocks, anchored to addresses
src/qbert.entries the entry points the trace cannot deduce, each with its reason
src/qbert.nocode the areas that are not code even if the trace could enter them
src/qbert.asm the commented listing, generated
tools/z80trace.py, tools/mkasm.py the tracer and the listing generator
tools/graficos.py the cartridge's two formats: the VRAM RLE and the text scripts
tools/sonido.py walks the sound data the way the engine reads it
tools/pantallas.py, tools/imagenes.py build every screen from the tables and paint it
tools/coteja.py, tools/coteja_todo.py the check against the openMSX dumps
tools/omsx_*.tcl, tools/lanza_*.sh the openMSX probes: see In the emulator

The pages: The game, The cartridge, The code, Findings, In the emulator and Open questions.

The cartridge

32 KB with no mapper, in pages 1 and 2 (0x4000-0xBFFF). INIT puts page 2 in the same slot as page 1 (0x405E), and writes 1, 2 and 3 to 0x6000, 0x8000 and 0xA000, the bank writes of Konami's mappers, which do nothing here.

The headers

0x4000: "AB" and INIT at 0x405E. 0x4010: the Konami Game Master header, "CD" and RC-746, with seven fields: the scene variable and the level menu scene (0xE000, 3), the stage the Game Master picks and how many there are (0xE114, 50), the lives (0xE110), the high score (0xE105), the score (0xE10B), a second score the game never reads (0xE108) and the mode bits (0xE002).

The ROM map

from to what
0x4025 0x50A6 the framework, the scenes, the score line, the controls, the font, the logos and the sound engine
0x50A6 0x61DD the sound data: two instruments, 93 entries and the sequences
0x61DD 0x9093 the game: the stage, the jumps, the cubes, the creatures, the duel, the intro
0x90A7 0x97DB the graphics of the duel and of rock, paper, scissors
0x97DB 0xA7AD the 50 boards of 9 × 9
0xA7AD 0xA7FE the bonus board
0xA7FE 0xAABC the cubes: 3 styles of 26 cubes of 3 × 3 cells
0xAABC 0xAFBC the sprites of the creatures and of Q*bert
0xAFBC 0xB09A the tiles of the spinning cubes and the finished cube
0xB09A 0xBFE7 the intro and the menus, in RLE
0xBFF6 0xC000 Konami's hidden mark

The whole split, byte by byte, is in src/qbert.notes: the code is 11,743 bytes and the data 21,025, and not one is left unexplained.

The RAM

address what
0xE000 / 0xE001 the scene and its step
0xE002 the mode: bit 6 game, bit 5 duel, bit 0 bonus
0xE003 the frame counter
0xE008 / 0xE009 joystick 1: just pressed and held
0xE010-0xE08F the four sound channels
0xE105-0xE107 the high score, in BCD
0xE10B-0xE10D the score, in BCD
0xE110 / 0xE111 the lives and the stage, in BCD
0xE200-0xE2BF 24 objects of 8 bytes: the two Q*berts (two sprites each) and the creatures
0xE2C0 the ten slots of the spinning cubes
0xE32F / 0xE330 joystick 2
0xEB00 the 26 cubes of the stage's style
0xEC00 the 9 × 9 board; bit 6 marks the first player's finished cubes and bit 5 the second's
0xEC51 the time, in BCD
0xED00-0xEFFF the copy of the name table

Each object: state, speed, state again, arc step, Y, X, pattern and colour. The states are at 0x6D35.

The VRAM

SCREEN 2 with the registers at 0x46F0: names at 0x3800, patterns at 0x2000, colour at 0x0000, sprite attributes at 0x3B00 and sprite patterns at 0x1800, with 16 × 16 sprites.

Tiles 0 to 15 have a blank pattern and colour 0x00-0x0F: each one is a solid block of that colour, and they are the cubes' faces. Tiles 0x40-0x87 all carry the same triangle (0x6286) with two colours each (0x628E): they are the edges. A 3 × 3 cube is a combination of faces and edges.

The formats

tools/graficos.py and tools/sonido.py read all three, and every data block in the listing ends exactly where its format ends.

The code

The listing is src/qbert.asm: 5,939 instructions, 4,636 of them commented (78.1%), 788 routines (444 with their own name), no routine below 10% and no call target without a name. It reassembles byte for byte.

The framework

INIT leaves a jp in H.KEYI and sits in a jr $: the whole game runs in the VDP interrupt (0x4025). Every frame the sound plays; the scene only runs if the previous frame finished (0xE005), with interrupts enabled so the sound is never lost.

The framework is the one in The Goonies and Yie Ar Kung-Fu II: tools/porta_nombres.py finds here, by instruction signature, their script, RLE and font routines. The sound engine and the game belong to this cartridge.

The scenes

0x40C5 dispatches on 0xE000 with the table at 0x40E3:

scene what
0 the Konami logo, uncovered line by line
1 the intro: Q*bert at the arcade machine, the title and 1PLAYER/2PLAYERS
2 the demo
3 the level menu
4 the start of a stage: LEVEL, STAGE or READY, and building it
5 the game
6 TIME OVER or GAME OVER
7 GAME OVER, with the F5 CONTINUE
8 stage cleared: the next one, or the bonus stage

Each scene is a chain of djnz on its step (0xE001). Since djnz with B=0 gives 0xFF and jumps, step 0 is the last block of the chain: every scene starts with the block that sets it up.

Without a game, the scene returns to 0x441E, which checks for a key press: on the logo or the demo it jumps to the title; on the title, the directions change option and the trigger starts.

The game

0x68E6 is the game frame: the sprite table (with the planes rotating every frame to share the flicker when there are more than four on a line), a quarter of the name table, one of the four line checks (rows, columns and both diagonals, one per frame), the controls of both Q*berts, the collisions, the movement of the 24 objects, the spinning cubes, the hidden life and, if there is no green ball, the creatures' release, their decisions and the time.

Objects move according to their state (0x6D35): still, jumping left or right, coming in from the top, falling off a side, going down to the row at 0xE29C, running away or falling dead. The jump follows one of the five arcs at 0x6F9E (normal, long and falling dead) and moves X by one and two pixels in turn: one column of cubes every 16 steps.

When Q*bert lands, 0x708E spins the cube: it takes one of the ten animation slots, paints nine tiles with the colours of the faces taken from two templates (0x727A, 0x729F) and eight frames later 0x715A leaves the new rotation and checks whether it now matches the model.

The sound

Four channels of 0x20 bytes: the three of the PSG and an effects one that, when it plays, takes over the third one's registers. Sounds 0x01 to 0x16 are one-channel effects; from 0x17 on, three-channel tunes that use three consecutive entries of the table at 0x52E2. 0x56 is the pause: before playing it saves the channels, and they come back when the pause ends.

The code that never runs

Twelve pieces nobody reaches, declared as such in src/qbert.entries and run in openMSX (see In the emulator): a pattern viewer (0x6757), two copies of the key reading (0x705E, 0x7068), the VRAM read (0x4643), a byte swap (0x45FD), three duel leftovers (0x821A, 0x8275, 0x833C) and four more. And a dead loop at 0x44A5: the Game Master stage checked right after being cleared.

In the emulator

The pictures are built from the ROM, not captured. openMSX is used for two things: to check those pictures against the real VRAM and to test what the code says about the game.

Everything with openMSX and the C-BIOS_MSX1_JP machine, one emulator at a time.

The check

tools/omsx_vuelca.tcl dumps the VRAM, the RAM from 0xE000 to 0xFFFF and the VDP registers at the given seconds, pressing space where needed. tools/omsx_fase.tcl starts the game on any stage: it writes the stage to 0xE111 at the call monta_la_fase at 0x4209, only once, and dumps a few seconds later.

tools/coteja_todo.py builds each screen with tools/pantallas.py and compares it with its dump: the whole name table, the pattern and colour of every tile in use, the visible sprites and their patterns. The time, the lives, the objects and the sprite turn of that frame come from the dump's RAM.

screen differences
Konami logo 0
title 0
stage 1 0
stage 11 0
stage 21 0
stage 41 0
stage 50 0
duel, stage 31 0
bonus after stage 3 0
0x6757 viewer over stage 1 0
0x6757 viewer over the title 0

In both viewers the tiles each screen builds are compared; the rest are leftovers from earlier ones. The title, besides, is built on top of the Konami logo's VRAM: nine of its tiles keep the colour the logo left them.

Two measurement traps. The sprite table is written at the start of the frame and the objects move afterwards: a dump taken mid-frame has the RAM's Y one step ahead of the VRAM's. And the copy of the name table is sent a quarter at a time, one per frame: a digit of the time can be one frame behind.

The tests

tools/omsx_prueba.tcl reads a list of timed orders (write a byte, press a key, dump) and tools/lanza_prueba.sh runs it. The five in tools/pruebas/:

test what is done what happens
linea.txt on stage 1, five cubes on row 4 marked as finished stage cleared (game steps 2, 4 and 5) and the time is paid: 430 points for 43 seconds
vida.txt the streak of three set to 1, 1, 1 the object appears at Q*bert's Y, crosses, and the lives go from 2 to 3
continue.txt no lives and the time at 1 GAME OVER; with F5, the game again with two lives in reserve
pausa.txt F1 twice the time stays at 99 until the second F1
bonus.txt stage 3 and stage cleared bit 0 of the mode turns on and the bonus stage starts

The twelve pieces nobody calls

tools/omsx_huerfanos.tcl runs them in a game, one per frame: at 0x40C5 it stacks the return, jumps to the piece with the registers it needs and records what it did. The last one is the viewer, and the emulator stays paused with it on screen (tools/lanza_huerfanos.sh).

piece what it does
0x640D with stage 0x23 it returns A=09: the minimum of the stage minus one and 9
0x45FD swaps 4 bytes between HL and DE
0x4643 sets up a VRAM read and leaves port 0x98 in C'
0x705E stores joystick 2: with A=05, 0xE32F=05 and 0xE330=05
0x7068 reads E, S, F and C: with E held, A=01 (up)
0x722C the cube under Q*bert against the model: not equal
0x77CC writes 0x5A into fourteen objects in a row
0x833C the other player is not falling (with one player there is no other)
0x9032 the life object against the second sprite: they do not touch, and it moves one pixel
0x821A takes a life (from 2 to 1) and draws the duel score line
0x8275 makes Q*bert fall dead: state 12 and the falling arc
0x6757 the pattern viewer

Running it

tools/lanza_vuelca.sh work/v1 "6 12" "10"
tools/lanza_fase.sh "11 0 0 3"
tools/lanza_prueba.sh tools/pruebas/vida.txt
tools/lanza_huerfanos.sh
make coteja

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, 49 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 22 241 guion_rle (0x46A0)
Penguin Adventure 20 232 destapa_una_linea_del_logotipo (0x494E) = destapa_una_columna
The Goonies 18 213 guion_rle (0x46A0)
Konami's Ping Pong 18 181 guion_rle (0x46A0) = descomprime
Konami's Synthesizer 18 195 guion_rle (0x46A0) = rle_a_vram
Konami’s Game Master 2 17 208 lee_cursores_espacio_y_select (0x472D) = th_teclas_1
Nemesis 2 17 204 destapa_una_linea_del_logotipo (0x494E) = logo_aparece
Hyper Sports 3 16 173 guion_rle (0x46A0) = descomprime_a_vram
Metal Gear 16 237 pon_el_mezclador (0x506D) = mezclador
Nemesis / Gradius 16 187 pon_el_mezclador (0x506D) = pon_la_mezcla

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.