Under the hood

Getting started

A commented disassembly of Nemesis / Gradius, Konami's RC-742 for the MSX: a 128 KB MegaROM with the Konami mapper without SCC. It reassembles into the exact ROM, byte for byte, all sixteen banks, and every one of its 131,072 bytes is accounted for.

The cartridge is not here

No repository ships the game. Put your own dump in the root as nemesis.rom, 131072 bytes, sha256

3210f8a0f2309dd4b9a89fc2b24d0f178ce4393a0a1f2854fbce545c361261bc

make comprueba checks it.

What each command does

make            trace the flow, build the listing, reassemble it, run the tests
make verify     the test that decides: reassembling has to give the ROM back
make sanity     that not one byte is left unexplained, and no data reads as code
make densidad   how much is commented, routine by routine, bank by bank
make reconoce   measures the mapper on the bytes: Konami4, no SCC
make marca      the hidden Konami mark at the end of bank 3
make web        rebuild this website from the ROM and the notes

Sixteen banks, sixteen listings

A 16 KB cartridge is one listing. This one is not. Bank 0 is fixed at 0x4000-0x5FFF and the other three windows are chosen by writing the bank number to 0x6000, 0x8000 and 0xA000, so the same address means different code depending on what is mapped. tools/paginas.py fixes the one org each bank actually executes at, and everything else is done bank by bank:

A D range only counts for the bank whose notes it is in: 0x6000 is the start of bank 1, of bank 4 and of bank 7.

What the numbers mean

make sanity prints the budget: 25,471 bytes of traced code and 105,601 bytes of data in named ranges, 0 unexplained, 131,072 in total. make densidad prints the comment density per bank: 12,959 instructions, 3,027 line comments, 23.4%, and not one routine below 10% out of 1,540.

Every figure on this site comes from those two commands, not from an estimate.

The pictures are drawn, not captured

There is no emulator capture on this site. tools/graficos.py runs the cartridge's own routines in Python — the run-length decompressor at 0x49B9, the character loader at 0x42FC, the mirror lists, and the column builder at 0x46AE — and draws the twelve stage maps and the title screen straight from the bytes. make web regenerates them.

The cartridge

RC-742, 128 KB, sixteen banks of 8 KB, with the Konami mapper without SCC (Konami4). tools/reconocimiento.py measures that on the bytes rather than assuming it: there is not one write to 0x5000, 0x7000, 0x9000 or 0xB000, which are the registers of the other Konami mapper, the one with the SCC.

How the banks are mapped

There is no register for 0x4000-0x5FFF: bank 0 is nailed there. The other three windows take the bank number written to 0x6000, 0x8000 and 0xA000, and the cartridge keeps a copy of the current split in 0xF0F1..0xF0F3 so an interrupt can put it back.

window banks that go there
0x4000 0 (fixed)
0x6000 1, 4
0x8000 2, 5, 7, 9, 11
0xA000 3, 6, 8, 10, 12

Each bank has exactly one address it executes at, which is why the listing can be split into sixteen files with sixteen orgs.

What is in each bank

bank code what it holds
0 3,158 instr. the loop, the scroll, the sprites, the VDP, the modes
1 3,231 instr. the enemy engines, the collisions, the boss
2 2,849 instr. the ship, the bosses, the aiming tables
3 3,074 instr. the power-up meter, the laser, twenty of the enemy types
4, 5, 6 — the stage graphics, compressed
7 420 instr. the sound player
8 — the music and the effects
9, 10 227 instr. the fixed screens, and a small sprite engine of their own
11, 12 — the map pieces and the stage scripts
13, 14, 15 — 0xFF, end to end

Only six of the sixteen banks carry code.

How much of the chip is empty

48,219 bytes — 47.1 KB, 36.8% of the cartridge — are declared as 0xFF filler. Four banks are filler end to end: 6, 13, 14 and 15. The others end with a tail: 7,202 bytes in bank 8, 4,311 in bank 10, 2,487 in bank 12 and 1,052 in bank 5. The odd one out is a hole of 399 bytes inside bank 3, between the last data and the eleven bytes of the hidden Konami mark; the other 47,820 are tails.

Bank 6 is the odd one: the routine that maps 4/5/6 for graphics does put it in 0xA000, so it is mapped for real — there is simply nothing in it to read.

The VRAM map is upside down

The eight bytes at 0x575A go to VDP registers 0 to 7: 0x02, 0xE2, 0x0E, 0x7F, 0x07, 0x76, 0x03, 0xE4. They do not leave anything where the BIOS leaves it.

table where the BIOS puts it where this cartridge puts it
patterns 0x0000 0x2000
colours 0x2000 0x0000
names 0x1800 0x3800
sprite attributes 0x1B00 0x3B00
sprite patterns 0x3800 0x1800

The name table ends at 0x3AFF and the sprite attributes start at 0x3B00, right behind it.

How the graphics are stored

Everything the cartridge draws goes through one small decompressor at 0x49B9, and its format is written the other way round from what you would expect: bit 7 marks the literal copy, not the repeat.

command what it does
0x00 ends the block
0x01..0x7F the next byte, N times
0x80 the next two bytes are a new VRAM address
0x81..0xFF N-0x80 bytes copied as they come

With that, the size of a block is not estimated: it is counted. That is what lets every graphics range be declared with a D directive without missing a byte or eating the next one. tools/rle.py measures any of them.

The stage characters are loaded by records of six bytes: which thirds to fill, where the pattern stream is, at which character to start, and where the colour stream is. Then two more lists mirror part of what was just loaded — by bits for a horizontal flip, by bytes for a vertical one — so half the terrain characters are made out of the other half instead of being stored twice.

The hidden mark

At the end of bank 3, at offset 0x07FFF of the dump, sit the cartridge code RC-742 and eight katakana characters that read グラディウス, Gradius. It is the signature Konami hid in its cartridges; the person who found it and documented it is Manuel Pazos. make marca reads it back out of the ROM.

The code

The frame

The interrupt hook does the work. Every frame it puts the sound banks in place, calls the player, gives the scroll its steps, runs the objects and pushes the sprite buffer out. What is left for the main loop is the mode machine: title, demo, play, stage screen, game over.

The map is in RAM

The twenty-two rows of thirty-two cells live at 0xED00, not in VRAM. Every scroll step:

  1. 0x469D shifts the whole thing one column to the left with twenty-two ldirs of 0x1F bytes.
  2. 0x46AE works out which column comes in on the right and 0x46E1 writes it.
  3. 0x47FE pushes all 704 bytes to the name table with outi, without going through the BIOS.

The incoming column comes from the stage's script: the distance travelled minus the start of the script range, divided by four, indexes rows of six piece numbers, and each piece is four by four characters. Five pieces give twenty rows and the sixth gives the last two. Outside the script range the column is sky: one star, on the row the table at 0x478E gives.

Two sets of pieces: the one at 0x8000 of bank 11 and the one at 0x8FF0. Stages 5, 9, 10 and 12 use the second.

The objects

Twelve slots of 32 bytes at 0xE300, plus eight of 0x20 counted backwards at 0xE460 for the aimed shots, ten at 0xE500, five at 0xEB00, and eight of 8 bytes at 0xE700 for the background. Inside a slot:

byte what it is
0 the type, 1 to 0x1F; zero means the slot is free
1 which step of its own machine it is on
3, 4 the row, with its fraction
5, 6 the column, with its fraction
7, 8 the vertical speed
9, 10 the horizontal speed
11 drawn with characters instead of a sprite
12, 13 the drawing and its colour
0x17..0x1A the two accelerations
0x1B flags: whether it can be shot, whether it is drawn

saca_un_objeto (0x6A72) is the most called routine in bank 1: give it the type, where it goes and one adjustment byte, and it finds a free slot, fills it from the four-byte record at 0x6BA3 and bumps the live count. Three types break the rule: 0x1E needs three consecutive slots, 0x0E goes to the other table, and the rest go to 0xE300.

Then the type drives two twin tables: p00:0x5DFD says who moves it and p01:0x6B46 says who finishes building it. Twenty of the thirty-one types have at least one of the two in bank 3, and sixteen have both, which is why 0xA7B9..0xB537 is the longest stretch of code in the cartridge.

The adjustment byte is the enemy's mark. The sixteen bytes at 0xA5C7 are read round and round: three out of four come out with the mark at zero, one in four comes out with a 1 and one in sixteen with a 2. A marked enemy is painted colour 8, the red one, and leaves something different behind when it dies.

How the enemies move

Every type is a tiny machine of its own, and none of them uses trigonometry. Some of the shapes:

The sound

Bank 7 is the player and bank 8 the data; the interrupt maps both and calls 0x8063. Three channels, each a card of 0x11 bytes at 0xE010, 0xE021 and 0xE032, each walking its own stream of commands. Everything reaches the PSG through the BIOS, WRTPSG. The table at 0x8328 holds eighty pointers, one per sound; entry 0 is not an address at all, so sound 0 does not exist, and the last three point at the first filler byte of bank 8, that is, at silence.

Sprite priority is rotated on purpose

The MSX draws four sprites per line and drops the rest, always the last ones in the table. So 0x47CE does not upload the buffer in one go: it uploads it in thirty-two pieces of four bytes, starting each frame at a different place — 0xE17F goes up by 0x1C and wraps at 0x7C — and stepping 0x0C at a time. Each object lands in a different slot of the attribute table every frame, and the flicker is shared out.

In the emulator

Some of what is written in this listing cannot be settled by reading. This page is what was checked with the cartridge actually running, and how.

What was measured, and what came out

The keyboard cheats. tools/omsx_claves.tcl boots the cartridge, starts a game, presses GRAPH to pause, types the word letter by letter, presses RETURN and writes down the bytes of the ship's card before and after. With OPTION, 0xE1E8 fills with 4F 50 54 49 4F 4E and 0xE20B goes from 00 to 02. With BAKA, the lives at 0xE060 go from 02 to 00 and the demo flag at 0xE05F goes out.

The first attempt failed, and the failure was the finding: typing the word while playing does nothing at all. The code only reads the keyboard with the game paused, so the script had to press GRAPH first.

The maps. The twelve maps on the front page are drawn from the ROM, not captured — but they were checked against the real thing. A script lets the demo play, dumps the 704 bytes of the map buffer at 0xED00 together with the distance in 0xE063, and the same columns are generated in Python. Of 704 cells, 6 differed at distance 138 and 9 at distance 201. Those are the twinkling stars, which the cartridge picks with the R register, and the characters the shot writes over the map.

The title screen. The whole VRAM was dumped from the emulator and compared, byte for byte, against the one the tool builds from the compressed blocks. They match everywhere the artwork lives; the only bytes that differ are characters 0xC0 to 0xFF, which the game keeps rewriting, and the sprite attribute table.

That comparison is what caught a bug — a mask of 0x17FF where it should have been 0x1FFF, which quietly dropped the 0x800 bit and drew the middle third of every screen with the first third's characters.

The traps, already paid for

Running one yourself

NEM_OUT=<directory> NEM_CLAVE=option openmsx -machine Philips_VG_8020 \\
    -carta nemesis.rom -script tools/omsx_claves.tcl

The script writes its own log next to the screenshots, with the emulated time on every line, so a run that goes wrong can be read afterwards instead of guessed at.

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, 48 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)
Nemesis 2 76 1,025 mide_la_distancia_a_la_nave (p01:66D5) = angulo_a_f000
Parodius 54 690 descomprime_con_destino (p00:49B3) = rachas_con_destino
Salamander - Operation X 43 579 descomprime_con_destino (p00:49B3) = rachas_con_destino
Nemesis 3 - The Eve of Destruction 39 469 descomprime_con_destino (p00:49B3) = rle_con_direccion
The Maze of Galious 28 380 baja_el_caracter_a_la_ram (p00:4450) = lee_caracteres
Yie Ar Kung-Fu II 23 228 descomprime_con_destino (p00:49B3) = guion_rle
Konami’s Soccer 22 209 escribe_caracteres (p00:4998) = escribe_un_rotulo
Hyper Sports 3 19 207 descomprime_con_destino (p00:49B3) = descomprime_a_vram
Twin Bee 18 188 pon_la_mezcla (p07:802A) = pon_el_mezclador
The Goonies 16 167 descomprime_con_destino (p00:49B3) = guion_rle

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.