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 hyperolympic2.rom
size 16,384 bytes
sha256 f254764f4bd1634f50abcdabe5813d133d719197216071772845983ffabad22d

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  f254764f...abad22d
  original   : 16384 bytes  f254764f...abad22d
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, which matters here more than anywhere

No annotation may cite an address from the sibling cartridge, or name its events.

This cartridge and Hyper Olympic 1 are nearly the same program: 83.4 % of that one's instructions turn up here. Making use of that is the sensible thing to do, and the annotations for the shared code are brought over with tools/porta_notas.py, which lines both ROMs up instruction by instruction with the sixteen-bit operands zeroed. But that puts each comment at the right address without changing what it says: a comment that is true over there can be a lie here.

tools/repasa_el_porte.py is the guard. The rule is exact and has no heuristic: every address cited inside a comment —or inside a page of this website— has to be the start of an instruction or fall in a data range of this cartridge, and no annotation may name the other one's events. It forgives three things, each with its reason written into the code: the zero slots of the tables indexed from one, sentences that are deliberately comparing with the sibling, and the two comparison pages, whose subject is precisely the other binary.

It is not a decorative guard. It caught nine cited addresses that do not exist here —six off by one byte and three pointing at a different routine— and a block heading that still titled the flight with the name of the sibling's event, the hammer, which this cartridge does not have.

And there is a second guard, tools/cifras_portadas.py, which watches what the first one cannot see: if a comment is identical to one in the sibling, the instruction it annotates has to be the same too, once the sixteen-bit operands are zeroed. It caught the two comments at 0x4389, which said "32 sprites" when the ld bc,01afeh here loads 26.

What is inside

   
src/hyperolympic2.asm the listing; generated, not hand-edited
src/hyperolympic2.notes the annotations, anchored to addresses
src/hyperolympic2.entries the entry points, each one justified
src/hyperolympic2.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 0x517B 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 javelin and the high jump
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 (0x4E5C), and they are byte for byte the same as in the sibling cartridge.

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.

Twenty-six sprites, not thirty-two

SUBE_LOS_COLORES_DE_LOS_SPRITES (0x4388) uploads colores_de_los_sprites (0x5161) into the attribute table with the row at 0xD1, that is, hidden off screen, and there are twenty-six of them: ld bc,01afeh. In Hyper Olympic 1 the same routine uploads thirty-two.

And the pose table (posturas_del_atleta, 0x713C) has eighteen entries against the twenty-five over there. These four events need fewer figures than those.

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 0x4F1C 0x4F1E
event screens 0x4F24 0x4F26
event scoreboards 0x4F2C 0x4F2E
melody pointers 0x6E62 0x6E64
event actor records 0x7B6B 0x7B6D

It is not a mistake: slot zero is never used.

The hurdle is drawn on two sheets

A normal actor assembles its figure at 0xE230 and uploads it in one go. The hurdle does not fit in one row: 0x7AB3 assembles the top half at 0xE230 and the bottom half at 0xE270, and uploads them separately, each to its own row of the name table. It is the only actor in the cartridge that does this.

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.

Twelve bytes nobody reads

At 0x4E64, right next to the VDP registers, there are three groups of four bytes shaped like rows of the sprite attribute table: 00 70 38 70 / 00 A0 38 70 / 00 F0 38 F0. No pointer in the ROM lands there and nobody reads them.

They are also in Hyper Olympic 1, byte for byte identical and just as dead as here. Both cartridges inherited the same corpse.

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 two bytes are 0xFF and in front of them there is sound player code. 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,872 bytes of code and 6,512 of data. There is no bank switching and no loading trick: the whole program is in plain sight between 0x4000 and 0x7FFF.

And there is one thing worth knowing before reading any of it: this cartridge is, for the most part, the same program as Hyper Olympic 1. Lining both ROMs up instruction by instruction, 4,195 of that one's 5,032 instructions turn up here: 83.4 %. What follows holds for both except where it says otherwise.

Two threads sharing the work

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 four motors, and where they come from

This is the interesting part of this cartridge. The four events are new, but only two of the motors are:

event motor where it comes from
110 hurdles and 1500 m MOTOR_DE_LA_CARRERA (0x5187) the same one as the sibling's races
javelin MOTOR_DE_LA_JABALINA (0x680A) written here, on the jump's mould
high jump MOTOR_DEL_SALTO_DE_ALTURA (0x6A2C) the hammer motor, reused

That third one is the one that says most. The motor that in the sibling cartridge span the hammer —wind up, mark the angle with the button and let go— is exactly the one that lifts the high jumper here. What changes is what gets drawn, not the mechanism.

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

What this cartridge adds: 2,227 bytes

Lining both ROMs up with porta_notas.py, the bytes of code in this cartridge with no counterpart in the sibling are 2,227, spread over 247 runs. The four largest are, all four of them, the two genuinely new events:

run bytes what it is
0x682A 434 the javelin motor
0x5A1B 172 the hurdle jump
0x7AB3 159 MONTA_LA_VALLA, the two-sheet actor
0x5AD7 109 EL_ATLETA_SE_LEVANTA, after knocking one over

The high jump is not on that list, and that is the point: its motor is the sibling's hammer motor, whole. The rest are small runs, nearly all one or two loose instructions where an address or a constant changes.

The hurdle jump

While the athlete runs, bit 1 of 0xE0C5 asks to jump. The jump lasts ten frames, counted at 0x5A34, and while it lasts the pose is 13; when it ends, 6. Then 0x5A5B compares the athlete's column with the column of the hurdle that is his and, if they are less than eight columns apart, sets bit 4 of 0xE028 or 0xE029.

That bit is the fall: 0x5AA5 gives him pose 14, wipes his four speed bytes and gives him 0x14 frames of getting back up. Knocking a hurdle over is not a collision: it is a comparison of columns at one specific instant.

The hurdle is drawn on two sheets

A normal actor assembles its figure in the scratch area at 0xE230 and uploads it in one go. The hurdle does not fit in one row, so 0x7AB3 assembles the top half at 0xE230 and the bottom half at 0xE270, and uploads them separately, each to its own row of the name table. It is the only actor in the cartridge that does this.

The only real piece of arithmetic

CALCULA_EL_TIRO (0x7D38) takes the running speed and the angle and gets from them the advance and the height for each frame, and finally the mark. 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 (0x7E53): one byte per degree. No real-time approximations.

The points

PUNTUA_EL_INTENTO (0x4F36) turns the mark into points, and the arithmetic flips sign depending on the event: the points come from the difference between the mark and a fixed per-event reference (marca_de_referencia, 0x5022). In timed events the reference is on top and the mark is subtracted from it; 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.

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.

The long script (GUION_LARGO, 0x4CF2) 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, 0x4AFD) is for loose text, like the menu's. The label (LLENA_EL_PAPEL, 0x4C4C) prepares text that is later stretched into large letters. And the figure (MONTA_LA_FIGURA, 0x79A5) expands an actor's description into character codes.

The scenery lists come in pairs: the hurdles and the 1500 metres share theirs (0x655A) and the javelin and the high jump the other (0x661F).

The large letters are manufactured

A large-letter label is not drawn anywhere: it is assembled on the fly. MONTA_UN_ROTULO (0x4C32) copies the normal 8x8 glyphs into a scratch area at 0xE230, and then ESTIRA_LAS_LETRAS (0x4C94) 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.

Writing to the VDP without the interrupt stepping on it

There are four routines devoted to this, and they all do the same thing (0x4DE5 and the ones below it).

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.

The solution is not to forbid the interrupt but to notice: clear 0xE01D, set the address, and 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 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. A sound does not interrupt another one of higher priority.

The strings live in melodias (0x6EAC): 0xFF ends and 0xFE starts over. The thirty-six pointers are at 0x6E64, and a sound with several voices takes consecutive entries.

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: bit 2 of the flags makes the actor push the one ahead and bit 3 the one behind, adding or subtracting 0x11 from the pointer. That is how the scenery drags itself along in a chain with nobody keeping a list of who pushes whom.

The athlete is seven sprites

FICHA_DEL_QUE_JUEGA (0x7F51) and what sits under it assemble the runner. He is not a sprite: he is seven, and every pose places them by hand. 0x7F85 uploads to VRAM whatever patterns the pose needs, and then it writes the seven rows of the attribute table, adding to each the row and column the athlete is at.

There are eighteen poses (0x713C), seven fewer than in the sibling cartridge.

How to read it

The listing is generated; it is not hand-edited. The comments live apart, in src/hyperolympic2.notes, anchored to the address they describe.

And here there is an extra caution, because 86.9 % of this cartridge's comments are identical to one in the sibling: they were ported, and a ported comment says what the instruction did over there. Two guards watch for it, and both have caught something real:

It is written up in detail in Getting started.

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 0x422E —right after 0x4216 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 110 HURDLERS 12 SEC 93
2 JAVELIN THROW 96 M 72
3 HIGH JUMP 02 M 36
4 1500 METER 03 min 31

All four records match byte for byte the table at 0x517B, which INIT copies to 0xE040: 00 12 93 / 00 96 72 / 00 02 36 / 00 03 31. So what is on screen and what the binary says are the same thing, checked from both ends.

The capture confirms the typo, but it is not the proof

On the first event's screen you can read 110 HURDLERS, with an extra R. A capture on its own would prove nothing: it could be the emulator, the crop or the resolution.

The proof is in the data. The rotulo_110_vallas label starts at 0x64B1, and behind its header byte it carries the glyph string

0A 01 01 00 0A 12 1E 1B 0E 16 0F 1B 1C 0A

and in the cartridge's font (0x6197) those indices read ` 110 HURDLERS `. The typo is written into the ROM. The capture only shows the result.

The clock was worked out for 60 Hz

SUMA_AL_RELOJ (0x56D7) 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.

It is the same routine as the sibling cartridge's, and over there it is measured by running the event mashing the space bar and sampling the counter once per emulated second:

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 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 110 hurdles timed at 13.00 by the game took 15.6 real seconds. The qualifying marks at 0x500A and the world records at 0x517B 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 menu

Four lines, the same as in the sibling cartridge: one player or two, and joystick or keyboard. Whichever you pick leaves a number from 1 to 4 in 0xE01B, and from that come the number of players (0xE010) and the controller (0xE004).

How to repeat it

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

The second one takes all four captures in a single pass, and the title one too. The tables and PNGs that come out land in work/omsx/. The ones in medidas/ and docs/imagenes/ 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 1 124 2,194 SUBE_LOS_PATRONES (0x7F9B)
Konami's Tennis 22 312 COPIA_VRAM_A_VRAM (0x4C00) = replica_vram
Konami's Golf 19 227 COPIA_VRAM_A_VRAM (0x4C00) = replica_los_tercios
Konami's Baseball 18 209 COPIA_VRAM_A_VRAM (0x4C00) = replica_el_tercio_en_los_otros_dos
Hyper Sports 1 9 105 MONTA_UN_CANAL (0x6C3D) = SUMA_BONUS_7BE3
Konami’s Mahjong Dojo 5 122 NOTA_DE_UN_BYTE (0x6DE6) = lee_una_nota_de_musica
Road Fighter 5 52 TOCA_LA_NOTA_DE_UN_BYTE (0x6E36)
Yie Ar Kung-Fu 5 40 ADELANTA_LA_CAIDA (0x6DD2) = 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.