Under the hood

Rebuilding the ROM

make            # listing, reassembly, checks and tests
make verify     # the 64 reassembled banks give the whole ROM, byte for byte
make sanity     # every byte, explained as code or as data with its reader

You need the cartridge image (metalgear2.rom, sha256 43b7fab533abeb87313919ec1fc7bc732d7b0ebfacede1e4f89d8295160384f2, sha1 af567ea6e27912d6d5bf1c8e9bc18e9b393fd1ab), which is not distributed.

In numbers

   
explained 100% of the 524,288 bytes
code 118,147 bytes, 57,378 instructions
data 406,141 bytes, each block with the routine that reads it
routines 7,673
commented 45.9% of the instructions, no routine below 10%

The cartridge

Metal Gear 2: Solid Snake (Konami, 1990, RC-767): a 512 KB MegaROM, 64 banks of 8 KB with Konami's SCC mapper (registers at 0x5000, 0x7000, 0x9000 and 0xB000; 0x3F at 0x9000 opens the SCC). Each bank always runs in the same window (tools/paginas.py), and no write to the mapper in the whole cartridge contradicts it (tools/reconocimiento.py).

The states

0xCA00 is the state and 0xCA01 its step (p01:72A2):

State  
0 the Konami logo and the round of the intro
1 the intro (p13:8000) and the title (p14:A196)
2 the title goes to white (p14:A299)
3 waits and clears the game
4 the new game
5 the game: the zone's routine and the step at 0xCA01 (0 play, 2 F2, 3 F3...)
6 the end of the game, with its window (p01:7655)
7 the ending (p13:9555)

The RAM

   
0xC000-0xC37F the copies of bank 3 (the interrupt, the call to the sound)
0xC400-0xC69F the sound engine
0xCA00 / 0xCA01 the state and its step
0xCA22 the game phase (the lists at p06:B40C)
0xCA42 the VRAM page being drawn on (0 or 1, alternating)
0xCA4A / 0xCA4B the zone and the room
0xCA53 Snake's life
0xCA5F the alert
0xCA76 the special room
0xCB01 Snake's drawing
0xCE00 the actors
0xD500 the room's doors (16-byte records)
0xD600 / 0xD638 Snake's weapons and equipment
0xD7A0 the transceiver frequencies
0xD7D0 the game flags
0xD800 the boss records (4 x 0x20)
0xD900 the zone map
0xDC00 the sprite attributes

The VRAM

The V9938's SCREEN 5 (p06:B066): 256 x 212 dots, 16 colours, four 32 KB pages. The game draws on page 0 or 1 while it shows the other (0xCA42); page 3 keeps the font and the drawings of the weapons and the equipment, and page 2 the pieces of the rooms, which are copied with HMMM commands. The sprite patterns go at 0xF800 (R#6 = 0x1F). The fight with METAL GEAR splits the screen by changing R#23 on one line (0xCA1C).

What the emulator has to do

For the cartridge running (tools/mg2run.py on top of tools/msx2run.py) to paint the same as an MSX, three things were needed, each checked against openMSX:

What it shares with Metal Gear

Very little. Comparing both listings instruction by instruction, in runs of 16 bytes or more (6 instructions or more) and masking the absolute addresses -a routine moved elsewhere counts the same-, there are 1,484 bytes out of this cartridge's 118,147 bytes of code, 1.3 %: 38 runs identical byte for byte and 53 equal except for the addresses. None is longer than 76 bytes. The game engine is written anew.

What repeats is Konami's MSX2 library:

System Bytes Routines For example
The VDP: commands, VRAM, palette, sprites 863 34 copia_rectangulo (p00:4767), borra_bloque_vram (p00:46FF), lee_estado_vdp (p00:465C), cambia_tablas_de_sprites (p01:67CD)
1 to 3 bit characters turned into 4 208 11 caracter_a_4_bits (p00:48A5), fila_3_bits (p00:4BB1)
Joystick and keyboard 170 8 mira_el_mando (p06:A65C), mando (p06:B0C8)
The Konami logo 105 7 pantalla_de_56 (p13:92D1)
Arithmetic 54 4 por_la_rapidez (p02:8707), multiplica (p02:8719)
Start-up 46 3 slot_del_cartucho (p00:5FF1), arranque_por_el_gancho (p00:4040)
The state loop 38 2 cada_cuadro (p01:728E), with the RAM moved from 0xC0xx to 0xCAxx

389 of those bytes are a second copy of the VDP library in bank 63, the one used by the debug monitor (copia_hmmm, p63:BB99).

What they do not share, not even a 16 byte run: the sound engine (Metal Gear's drives the PSG; this one, the SCC, p16-p17), the bank switching, the texts and the transceiver, and the guards' intelligence. Only the aiming looks alike: velocidad_hacia and angulo_hacia (p02:8667-8725) do the same calculation with the same sine table, but rewritten: only pieces of 3 to 22 instructions match.

The identical data are 578 bytes: the Konami logo (497 bytes, the palette and pieces at p56:AE95), the sine table (p08:BE7D) and the joystick direction table (p06:A6A8). Texts and music, nothing.

On Metal Gear's side it is 1,179 of its 35,620 bytes of code, 3.3 %. With a lower threshold (12 bytes) it comes to 2.3 % of this one and 5.7 % of that one; with a higher one (32 bytes), 0.7 % and 1.6 %.

How it has been done