Under the hood

Getting started

This repository does not ship the game: it ships the way to build it back from your own copy and to check that what comes out is exactly the cartridge.

What you need

Your own dump of the cartridge, in the root and with this name:

   
file hyperolympic1.rom
size 16,384 bytes
sha256 0cd8a7928e10a8551d7f6fd68943a6d4a008e04a254d258413c120dda18b792e

Plus pasmo, z80dasm and Python 3. For the emulator measurements, openMSX.

The one command that does it all

make comprueba   # is this the right dump?
make             # trace, generate the listing, reassemble it and run the tests

make finishes green or it does not finish. The line that matters is this one:

  ensamblado : 16384 bytes  0cd8a792...a18b792e
  original   : 16384 bytes  0cd8a792...a18b792e
OK: reproducible byte a byte

The other commands

make verify     # the test that decides: reassembling has to give the ROM back
make sanity     # what reassembling CANNOT catch
make densidad   # how much is commented, routine by routine
make imagenes   # draw blocks of the ROM so you can look at them
make test       # the listing tests
make web        # rebuild this website

Why verify is not enough

Reassembling and getting the same bytes proves the listing is faithful, not that it is correct. If some graphics were being read as instructions the bytes would come out identical all the same: the only thing lying would be the listing.

That is why make sanity runs three more checks, and those three are the ones that really earn their keep:

And a fourth one, born in this project for a specific reason: no annotation may cite an address from the sibling cartridge. Hyper Olympic 1 and 2 are nearly the same program, so the annotations are ported from one to the other with porta_notas.py, which lives in the sibling's repository because that is the one that received the port; that puts them at the right address but does not change what they say. tools/repasa_el_porte.py checks that every address cited inside a comment —or inside a page of this website— is the start of an instruction or falls in a data range of this cartridge.

What is inside

   
src/hyperolympic1.asm the listing; generated, not hand-edited
src/hyperolympic1.notes the annotations, anchored to addresses
src/hyperolympic1.entries the entry points, each one justified
src/hyperolympic1.nocode the areas the tracer is forbidden to enter
medidas/ what was measured in openMSX, with the raw tables
tools/ the tracer, the listing generator and the data walkers
docs/ this website

The comments live apart from the listing, anchored to the address they describe. That way they survive a re-trace: if tomorrow the tracer splits the binary differently, the comments still land where they belong.

What is not here

The cartridge image. Neither this one nor any other: it belongs to Konami and is not distributed. See LEGAL-NOTICE.md.

The cartridge

Sixteen kilobytes on page 1, without a single bank switch. Everything the game does fits between 0x4000 and 0x7FFF.

The header

The first sixteen bytes are the header the BIOS reads: the AB signature and four pointers. Only INIT has a value (0x4081); STATEMENT, DEVICE and TEXT are all zero. So the cartridge adds no commands to BASIC and does not declare itself as a device: it boots and keeps the machine.

What INIT does, and why it never returns

INIT sets up house and leaves:

  1. hooks the interrupt at H.KEYI (0xFD9A) with a jp to 0x4010;
  2. clears 0xE000-0xE3FE and puts the stack right above it, at 0xE3FE;
  3. silences the PSG by zeroing its registers 8, 9 and 10, which are the three volume ones: not the tone ones, which at zero would silence nothing;
  4. sets the VDP write address to 0x81A2 —and writes nothing behind it; see Open questions— and puts PSG register 15, the output port, at 0xCF;
  5. turns off the CAPS lamp;
  6. copies the four world records from 0x5174 to 0xE040;
  7. puts the menu arrow on the first line.

From then on the main program and the interrupt split the work. The interrupt carries the clock, the controls and anything that has to go at screen rate; the main program, the game's state machine.

The memory map

Working RAM is 0xE000-0xE3FE, with the stack at the very top. It is laid out in zones, and each one has its explanation in the listing:

range what it is
0xE000-0xE001 frame counter and wait in units of 32
0xE002-0xE017 the state of the game: who plays, with what, round and event
0xE019-0xE01C the menu: where the arrow is and which line is picked
0xE01D-0xE01E the interrupt's two semaphores
0xE020-0xE02E the state of the attempt and the event, bit by bit
0xE040-0xE04B the four world records, three BCD bytes each
0xE051-0xE052 this round's mark to beat
0xE060-0xE09F the scoreboards as painted
0xE0A0-0xE0FF the two athletes: speed, angle, mark and pose
0xE120-0xE15F four 16-byte actor records
0xE160-0xE190 the sound player's three channels
0xE200-0xE22F the state of the distance events
0xE230-0xE2CF the scratch area where figures are assembled before upload

The interrupt's two semaphores

They deserve their own section, because they are the reason this does not fall apart.

The second one is the fine trick. Setting an address in the VDP is two writes to the control port, and if the interrupt slips in between them —and it writes to the VDP too— whatever comes next gets written in the wrong place. Rather than forbid the interrupt, you let it through and check afterwards.

The screen

SCREEN 2, with its three thirds:

table address
names 0x3800
patterns 0x2000
colour 0x0000
sprite attributes 0x3B00
sprite patterns 0x1800, 16x16

The eight VDP registers come from registros_del_vdp (0x4E73).

In the listing, VRAM addresses appear with bit 14 set (0x4000 added) when they are about to be written, which is how the VDP wants them: that is why 0x7800 is the name table and 0x5800 the sprite patterns. Worth keeping in mind when reading a dump, or the numbers will not add up.

Five tables indexed from one

A pattern that repeats and that misleads when reading the listing: there are tables whose recorded address falls two bytes ahead of the first useful slot, because the index starts at one and the code advances 2*index before reading.

table what the code says where it really starts
event labels 0x4F33 0x4F35
event screens 0x4F3B 0x4F3D
event scoreboards 0x4F43 0x4F45
melody pointers 0x6D26 0x6D28
actor records 0x7B44 0x7B46

It is not a mistake: slot zero is never used. And the label table's slot zero does double duty: the two bytes at 0x4F33 are the FF FF that closes the last menu message. Two bytes with two jobs.

The actors shove each other

The four actor records live back to back at 0xE120, 0xE130, 0xE140 and 0xE150, sixteen bytes each. And that is no accident: bit 2 of a record makes that actor bump the counter of the one 0x11 bytes behind it, and bit 3 the one 0x0F ahead.

That is how the scenery drags itself along in a chain with nobody keeping a list of who pushes whom: the relationship is the distance between the records.

What it does not carry

Konami's hidden mark is not there. Many cartridges from the house hide their RC-7xx catalogue number and the title in katakana at the end of the ROM, behind the filler; it was Manuel Pazos (@ManuelPazosMSX) who found that out. Here the last useful byte is sound player code and only one 0xFF of filler is left. It was checked with tools/marca_konami.py, which in the same run does find it in another cartridge from the house, so the method works.

The code

Sixteen kilobytes split into 9,335 bytes of code and 7,049 of data. There is no bank switching and no loading trick: the whole program is in plain sight between 0x4000 and 0x7FFF, and this page is about how it is put together.

Two threads sharing the work

The cartridge has a main program and an interrupt, and the line between them is sharp.

The interrupt (INTERRUPCION, 0x4010) comes in through the H.KEYI hook that INIT fills with a jp, and does everything that has to run at screen rate: it reads the controls, moves the sound player, counts the clock and calls the motor of whichever event is being played. It is where the game happens.

The main program carries the state machine: set up the scenery, wait for the attempt to end, hand out points, move to the next round. Most of the time it is waiting for the interrupt to set a bit.

The split has one nice consequence: stop the main program and the athlete keeps running.

The life of an event

MONTA_LA_PRUEBA (0x4234) heads everything that happens between two events. The route is always the same:

  1. MONTA_EL_DECORADO (0x4253) fetches this event's screen script and interprets it;
  2. MONTA_LOS_ACTORES_DE_LA_PRUEBA (0x7B2C) copies the actor records from the ROM table into RAM;
  3. EMPIEZA_EL_INTENTO (0x4296) places the athletes and releases the semaphore;
  4. BUCLE_DE_LA_PRUEBA (0x4396) sits watching 0xE02E until the interrupt says the attempt is over;
  5. PUNTUA_EL_INTENTO (0x4F4D) turns the mark into points;
  6. MIRA_SI_SE_ACABO_LA_PRUEBA (0x45A1) decides whether there is another attempt, another player, another round, or the game is over.

The points deserve a note, because the arithmetic flips sign depending on the event: they come from the difference between the mark and a fixed per-event reference (0x5020). In timed events the reference is on top and the mark is subtracted from it —the less you take, the more points—; in distance events it is underneath and the mark is what beats it. Then it is multiplied by sixteen, and in odd-numbered events doubled once more.

Four motors, and one does double duty

Each event has its motor, and the interrupt calls whichever applies:

event motor what it does
100 and 400 metres MOTOR_DE_LA_CARRERA (0x5180) running, the gap between the two and the finish
long jump MOTOR_DEL_SALTO (0x668F) the run-up, the take-off, the flight and the landing
hammer MOTOR_DEL_MARTILLO (0x68B3) the spins, the angle and the flight

The race motor covers both timed events as it stands, and on top of that the long jump reuses its run-up: what belongs to the jump is the take-off and what happens after.

And there is a fourth piece that is not a motor but behaves like one: AVANZA_EL_TIRO (0x7D2C), which computes the flight. Both the jump and the hammer use it, and it is the only part of the cartridge that does real arithmetic.

The only real piece of arithmetic

CALCULA_EL_TIRO (0x7D39) takes the running speed and the angle and gets from them the advance and the height for each frame, and finally the mark in metres. For that there are three arithmetic routines written by hand, because the Z80 brings none:

And the sine comes from a table, EL_SENO_DEL_ANGULO (0x7E54): one byte per degree. No real-time approximations.

The rest of the cartridge does not compute: it looks things up in tables or adds in BCD directly, which is how the marks and the scoreboards are carried without having to convert anything to paint it.

The cartridge does not store screens: it stores scripts

Not one screen is drawn in the ROM as such. What there is are scripts, and four different interpreters that read them, each with its own format:

The long script (GUION_LARGO, 0x4D09) is the big one: a byte with the number of orders and then the orders, each starting with its type.

type what it does
0 bytes to VRAM, with 0x11 as the run marker (RLE)
1 strings of font glyphs, uploaded as patterns
2 large-letter labels
3 fills of one byte
4+ repeat an eight-byte pattern N times

The short script (GUION_CORTO, 0x4AFE) is for loose text, like the menu's. The label (LLENA_EL_PAPEL, 0x4C63) prepares text that is later stretched into large letters. And the figure (MONTA_LA_FIGURA, 0x7A1F) expands an actor's description into character codes.

The 2.5 KB running from 0x5C96 to 0x668E are entirely scripts and script data, and a walk from the roots covers them without leaving a byte loose.

The large letters are manufactured

A large-letter label is not drawn anywhere: it is assembled on the fly. MONTA_UN_ROTULO (0x4C49) copies the normal 8x8 glyphs into a scratch area at 0xE230, and then ESTIRA_LAS_LETRAS (0x4CAB) makes three passes pulling the top two bits out of each byte and pushing them into the byte eight positions earlier.

With that, a row of eight glyphs yields four rows of pattern: the letter comes out twice the size without taking twice the ROM. The price is written up in Open questions: three labels declare twenty-four bytes of length and only carry sixteen of their own, so they take eight from the block behind.

Writing to the VDP without the interrupt stepping on it

This is the finest detail in the cartridge, and there are four routines devoted to it (FIJA_ESCRITURA_CON_CANDADO 0x4DFC, FIJA_LECTURA 0x4E14, FIJA_ESCRITURA 0x4E25, and the ones that use them).

The problem: setting an address in the VDP is two consecutive writes to the control port, and the interrupt writes to the VDP too. If it slips in between the two, the address left standing is its own, and whatever comes next gets written in the wrong place.

The solution is not to forbid the interrupt but to notice:

  1. clear 0xE01D;
  2. set the address;
  3. look at 0xE01D: if the interrupt went through in the middle it will have set it, and then you do it again.

It is cheaper than disabling and re-enabling interrupts on every write, and it does not lose a frame. The other semaphore, 0xE01E, is the real lock: while it is set, the interrupt leaves without touching anything.

The sound player

Three channels, an eleven-byte record each, at 0xE160, 0xE16B and 0xE176:

offset what it is
+0 note countdown
+1 base duration
+2 sound number
+3/+4 pointer to the string
+5 octave
+6 decay
+7 volume
+8 decay countdown
+9 repeats left
+0x0A duration

The interesting part is the sound number, which carries information in its top bits: bit 7 says the note takes a single byte, bit 6 that channel 3 is used as noise, and the bottom six are the priority. MIRA_LA_PRIORIDAD (0x6AE8) is what stops a key beep from wiping out the end-of-event music: a sound does not interrupt another one of higher priority.

DOBLA_LA_MELODIA (0x6B7E) does what it says: it plays the same string an octave above or below, which is how you get two voices out of one.

The actors

An actor is a sixteen-byte record in RAM, and there are four: 0xE120, 0xE130, 0xE140 and 0xE150.

offset what it is
+0 flags
+1 counter
+2 step
+6 column within the figure
+7/+8 VRAM address
+9/+0A the record that relieves it
+0B row of the adjustment table
+0C computed column
+0D/+0E the figure
+0F how many characters get painted

The four records sit back to back in memory, and that is where the trick comes from: MIRA_SI_EMPUJA_AL_DE_DETRAS (0x79CF) looks at bits 2 and 3 of the flags and, depending on which is set, adds or subtracts 0x11 from the pointer to bump the neighbouring record's counter.

That is how the scenery drags itself along in a chain with nobody keeping a list of who pushes whom: the relationship is the distance between the records.

The athlete is seven sprites

FICHA_DEL_QUE_JUEGA (0x7F52) and what sits under it assemble the runner. He is not a sprite: he is seven, and every pose places them by hand.

SUBE_LOS_PATRONES (0x7F9C) uploads to VRAM whatever patterns the pose needs, and then UN_SPRITE_DEL_ATLETA (0x7FD5) writes the seven rows of the attribute table, adding to each the row and column the athlete is at. Changing pose is changing the patterns; moving the athlete is adding to the seven rows.

How to read it

The listing is generated; it is not hand-edited. The comments live apart, in src/hyperolympic1.notes, anchored to the address they describe, and make listado pastes them where they belong. Which means that if tomorrow the tracer splits the binary differently, the comments still land in the right place.

VRAM addresses appear in the listing with bit 14 set (0x4000 added) when they are about to be written, which is how the VDP wants them. That is why the name table, which lives at 0x3800, is seen being written as 0x7800.

In the emulator

Reading the binary tells you what the code does. Some things only tell you what the code is worth once it runs, and that is what openMSX is for. Everything on this page is measured, and the raw tables are in medidas/.

The house rule

The scripts in tools/omsx_*.tcl only read memory. The ROM is never touched: no patches, no changed bytes, nothing forced to execute.

The one exception is declared and is a measurement, not a modification: tools/omsx_prueba_n.tcl stops the machine at 0x4220 —right after PARTIDA_NUEVA (0x4218) sets the round and the event to 1— and writes into 0xE016 the number of the event you want to see. 0xE016 is the game's own variable, and from there on the cartridge runs by itself, with its data and its code. What you save is having to qualify in the three previous events to reach the fourth.

The four events

With that, they were photographed one by one (medidas/pruebas.txt, captures in docs/imagenes/):

0xE016 label that comes up record it paints
1 100 METER DASH 09 SEC 95
2 LONG JUMP 08 M 90
3 HAMMER THROW 83 M 98
4 400 METER DASH 43 SEC 86

All four records match byte for byte the table at 0x5174, which INIT copies to 0xE040: 00 09 95 / 00 08 90 / 00 83 98 / 00 43 86. So what is on screen and what the binary says are the same thing, checked from both ends.

The clock was worked out for 60 Hz

This is the measurement that most changes what you think of the game.

SUMA_AL_RELOJ (0x5695) adds to the BCD counter at 0xE0A9-0xE0AB once per frame, alternating 0x0167 and 0x0166. If the top byte is seconds and the middle one hundredths, that is 1.67 and 1.66 hundredths: an average of 1.665, which is one sixtieth of a second to four figures.

tools/omsx_reloj.tcl runs the 100 metres mashing the space bar and samples 0xE0A9-0xE0AB once per emulated second. On two machines:

machine Hz game clock per real second
Philips VG-8020 (PAL) 50 0.8325 s — runs 16.75 % short
C-BIOS MSX1 JP (NTSC) 60 0.9990 s — spot on

Both figures come from subtracting two consecutive samples in reloj_pal.txt and reloj_ntsc.txt, and they agree with the arithmetic: 50 × 1.665 = 83.25 and 60 × 1.665 = 99.9.

The consequence: on a European MSX the times the cartridge shows are not seconds. A 100 metres timed at 12.00 by the game took 14.4 real seconds. The qualifying marks at 0x5008 and the world records at 0x5174 are meant for the Japanese machine.

And mind reading it backwards. That is a period, not a speed: the game does not run slower in Europe —the athlete moves across the screen at whatever speed 50 Hz allows— what happens is that the stopwatch counts short. Those are two different claims and only one of them is measured.

The demo: who is really in charge

When nobody touches anything, the cartridge falls into a demo that plays itself. The pilot is INT_CON_MANDOS (0x4027) dropping into 0xE00B a value from ld a,r masked to two bits, once every four frames: random keypresses.

Reading the listing, the switch looks like bit 7 of 0xE029, because 0x5849 sets it and 0x4030 reads it. tools/omsx_demo.tcl samples both situations without touching anything, first letting the demo run and then starting a real game, and this comes out (medidas/demo.txt, abridged):

  0xE002 0xE029
demo 01 0x82
game 00 0x82

0xE029 holds the same value in both. The one in charge is 0xE002, which is the first thing the interrupt looks at: if it is zero it reads the real controls, and if not it makes the keypresses up. In a real game the made-up ones never reach 0xE00B.

It is a good example of why you have to measure: reading the code, the bit 7 hypothesis was reasonable, well built, and false.

The menu, measured on the title capture

Four lines, and what each one leaves in memory:

line 0xE01B
1PLAYER with JOYSTICK 1
2PLAYERS with JOYSTICK 2
1PLAYER with KEYBOARD 3
2PLAYERS with KEYBOARD 4

EMPIEZA_LA_PARTIDA (0x41BC) splits it up: bit 0 of 0xE01B gives the number of players (0xE010) and the value being below 3 gives the controller (0xE004). One variable with both answers inside it.

How to repeat it

openmsx -machine Philips_VG_8020 -cart hyperolympic1.rom \
        -script tools/omsx_reloj.tcl
PRUEBA=3 openmsx -machine Philips_VG_8020 -cart hyperolympic1.rom \
        -script tools/omsx_prueba_n.tcl

The tables that come out land in work/omsx/. The ones in medidas/ are the ones this page was written from.

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, 29 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)
Hyper Olympic 2 124 2,194 SUBE_LOS_PATRONES (0x7F9C)
Konami's Tennis 22 312 COPIA_VRAM_A_VRAM (0x4C17) = replica_vram
Konami's Golf 20 279 SUBE_EL_ROTULO (0x4151) = titulo_espera
Konami's Baseball 19 218 COPIA_VRAM_A_VRAM (0x4C17) = replica_el_tercio_en_los_otros_dos
Hyper Sports 1 9 105 MONTA_UN_CANAL (0x6B01) = SUMA_BONUS_7BE3
Konami’s Mahjong Dojo 5 122 NOTA_DE_UN_BYTE (0x6CAA) = lee_una_nota_de_musica
Road Fighter 5 52 TOCA_LA_NOTA_DE_UN_BYTE (0x6CFA)
Yie Ar Kung-Fu 5 40 ADELANTA_LA_CAIDA (0x6C96) = apaga_poco_a_poco_si_no

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.