A commented disassembly of 3D Golf Simulation - Normal Edition, the golf game T&E Soft released for the MSX in 1983: a 16 KB cartridge mapped into page 2 (0x8000-0xBFFF).
Except there is nothing here to disassemble. The cartridge holds not one Z80 instruction: what is inside is a 246-line MSX-BASIC program that the machine's interpreter runs straight out of the ROM. So the listing is the detokenised program, and the proof that it is exact is not reassembling it: it is retokenising it and checking that the same 9,041 bytes come back.
No repository distributes the game. Put your own dump in the root as
3dgolf.rom, 16384 bytes, sha256
95d4e9dd82e60c614a471520d4e22ede94a5440a48f62376df62a74f093a8514
make comprueba checks it.
make builds the listing, retokenises it, cross-checks the data and runs the tests
make verify the test that decides: retokenising must give the ROM back
make verifica the three cross-checks on the course data
make densidad how much of the listing is explained
make sanity what each of the nine holes holds, counted from the data
make graficos draws the nine holes from the cartridge's own bytes
make emulador takes the VRAM dumps from openMSX (slow: see below)
make vram compares what was drawn against those dumps
make web rebuilds this site
src/3dgolf.bas is not edited by hand. tools/monta_bas.py builds it by
joining the program freshly read from the binary with the comments in
tools/comentarios_3dgolf.py. The reason is concrete: the program carries high
bytes - the golfer's stances, the panel's labels - and an ordinary text editor
runs them through UTF-8 and silently replaces them with the replacement
character. It happened the first time the file was touched, and the error
surfaced twenty lines further down.
The images are not captures either: tools/campo.py draws them by running in
Python the program's own subroutines - 4000 for the plan view, 5000 for the 3D
view, 7000 for the scaling - on an emulation of the SCREEN 2 display with its
colour attributes. If the format were misunderstood, noise would come out
instead of a golf course.
The game writes the golfer's stances and the panel's labels with high bytes, which are the right graphics only on a Japanese MSX. On a European machine the course comes out the same - it is geometry - but those labels come out as other symbols. And the cartridge has to be anchored in page 2: see In the emulator.
16,384 bytes, anchored in page 2 (0x8000-0xBFFF). All 16,384 are accounted for, and they break down like this:
| range | bytes | what it is |
|---|---|---|
| 0x8000-0x800F | 16 | the AB header |
| 0x8010-0xA360 | 9,041 | the MSX-BASIC program, tokenised |
| 0xA362-0xAFFF | 3,231 | the interpreter's variable table |
| 0xB000-0xBF9F | 4,000 | the course data: all nine holes |
| 0xBFA0-0xBFFF | 96 | nothing: nobody reads them |
41 42 00 00 00 00 00 00 10 80 00 00 00 00 00 00
A B INIT STATEMENT TEXT=0x8010 DEVICE
INIT zero, STATEMENT zero, DEVICE zero. The only thing the cartridge says is TEXT=0x8010, meaning "there is a BASIC program here, run it". And the interpreter runs it from the ROM itself, without copying it to RAM.
246 lines, 718 statements, 9,041 tokenised bytes. Not one CALL or USR to real
machine code: the only call, USR(0) in line 30000, points at address 0, so
pressing STOP resets the machine.
Between the program and the course data sit 3,231 bytes that are neither code nor authored data: they are the interpreter's variable tables, exactly as they were left in memory during a real session of the game. That is the trick that lets a BASIC cartridge with no INIT routine boot at all: the ROM carries the program and its state already set up.
tools/variables.py reads them. Out come 98 plain variables (0xA362 to 0xA728)
and nine arrays (0xA729 onwards), in the same order the program creates them.
And the session frozen inside was playing hole 2:
H = 2 the hole
ZM = 0xB37A the course read pointer, already on hole 3
HX = 60 hole 2's flag...
HZ = 139 ...with line 117's one-metre adjustment already taken off
P = 3 its par
S8 = 12 twelve strokes in all, eleven of them on hole 1
That those figures agree with the course data is a check on the reading done from outside the program, and it is wired up as a test.
The last array does not fit: C1() starts at 0xAFA1 and would need 25 bytes
more than are left. From 0xB000 on comes the course data, and it eats the rest.
It does not matter: the interpreter rebuilds all those arrays when it runs the
DIM in line 20.
Exactly 4,000 bytes, from 0xB000 to 0xBF9F, with the nine holes packed one behind the other. How they are read is in Findings.
| hole | address | bytes | hole | address | bytes | |
|---|---|---|---|---|---|---|
| 1 | 0xB000 | 532 | 6 | 0xB8BC | 444 | |
| 2 | 0xB214 | 358 | 7 | 0xBA78 | 438 | |
| 3 | 0xB37A | 446 | 8 | 0xBC2E | 408 | |
| 4 | 0xB538 | 452 | 9 | 0xBDC6 | 474 | |
| 5 | 0xB6FC | 448 |
246 lines of MSX-BASIC. The listing is at 100 % coverage: all 246 carry their own comment above them, and that comes to 702 lines of explanation for 718 statements of program.
| lines | what they do |
|---|---|
| 1-3 | STOP, the title screen and the DEFINT D |
| 10-90 | arrays, the thirteen clubs, hole 1's course and the wait |
| 110-130 | builds the hole's view and starts |
| 200-295 | the turn: stance, club, and the shot formula |
| 300-470 | the flight simulation |
| 500-630 | where the ball came to rest, and the camera for the next shot |
| 700-720 | hole finished, and the end of the round |
| 900-1260 | the ball in the view, and the three selection screens |
| 2000-2510 | reading the course out of the ROM |
| 3000-3070 | finding somewhere to drop the ball |
| 4000-4080 | the plan view |
| 5000-5690 | the 3D view engine |
| 6100-6900 | the five sounds and the silence |
| 7000-9900 | scaling, scoreboard, text, panel and compass |
| 10200-30000 | the nine holes' DATA and the STOP handler |
A classic conic perspective, written out by hand three times because there is no shared routine: in 370-372 for the ball, in 900-940 to mark where it came to rest, and in 5000-5690 for the terrain.
Subroutine 5000 makes four passes over the hole's data:
DN().Q1-Q9 matrix. Of its nine terms, six are
constants: the camera turns on one axis only. It never tilts or rolls.The clipping (5190-5590) is the classic one against a 45-degree view frustum: a
point is outside on the left if its x is less than minus its depth, and so on
for all four walls. If both ends are outside the same wall, the whole segment is
out. If not, the outside end is replaced by the exact intersection and the
whole thing starts over, because the moved point may now fall outside another
wall. With four walls, the loop cannot run more than four times.
The projection is two divisions by the depth, scaled to half the window's width and height (71 and 63) and centred on (79,63).
Subroutine 2500 reads one 16-bit value from the ROM. Since MSX-BASIC has no 16-bit PEEK, it builds one:
F1=PEEK(ZM) : F2=PEEK(ZM+1)
F1$=HEX$(F1) : F2$=HEX$(F2) : IF LEN(F2$)=1 THEN F2$="0"+F2$
DI(I)=VAL("&H"+F1$+F2$)
Two PEEKs, two conversions to hexadecimal text, a concatenation and a VAL. The
second piece is padded with a zero when needed; the first is not, because it
sits in the high position where a zero does no harm. And since VAL of a
four-digit hexadecimal returns a signed integer, negative coordinates come
out on their own.
It is desperately slow, and it shows: drawing hole 1 in full takes the interpreter more than five minutes of machine time.
There is no text console. Everything is written by opening the screen as if it were a file:
PSET(AX*8,AY*8),0 : OPEN"GRP:"FOR OUTPUT AS #1 : PRINT#1,E$ : CLOSE#1
The PSET is there to move the graphics cursor to the cell; the text comes out in
COLOR's foreground, which is white. This was checked in the emulator's VRAM:
the panel's cells read 0xF0, ink 15.
And the golfer is not a sprite or a drawing: it is eight strings, J$(0) to
J$(7), written with that same trick using the graphic character set in the
machine's own ROM. That is why a Japanese MSX is needed to see them properly.
There are only two, and they are the other way round from what you would guess: sprite 0, with the solid pattern, is the power meter's bar; sprite 1, with a two-pixel pattern, is the ball, and it moves across the plan view, not the 3D view. In the 3D view the ball is the shadow drawn by lines 390-393 - a filled circle whose radius shrinks with distance - and the dot left by subroutine 900.
openMSX does not guess this cartridge right on its own: it puts it at 0x4000 and the game never starts, because its header declares TEXT at 0x8010 and there is nothing there. You have to tell it. With an extension of your own:
<?xml version="1.0" ?>
<!DOCTYPE msxconfig SYSTEM 'msxconfig2.dtd'>
<msxconfig>
<info>
<name>3D Golf Simulation</name>
<manufacturer>T&E Soft</manufacturer>
<code/><release_year>1983</release_year>
<description>A 16 kB cartridge anchored in page 2.</description>
<type>cartridge</type>
</info>
<devices>
<primary slot="any"><secondary slot="any">
<ROM id="3D Golf Simulation ROM">
<mem base="0x8000" size="0x4000"/>
<rom><filename>.../3dgolf.rom</filename></rom>
<mappertype>Mirrored</mappertype>
</ROM>
</secondary></primary>
</devices>
</msxconfig>
The file is in the repository, at tools/3dgolf_page2.xml: copy it into
share/extensions/ in openMSX's user directory, point it at your own ROM and
start with -ext 3dgolf_page2. slotmap confirms the ROM sits at 0x8000.
The game writes the golfer's stances and the panel's labels with high bytes, which on a Japanese MSX are the graphics and katakana and on a European one are other symbols. On a European machine the course comes out the same - it is pure geometry - but those labels come out wrong. It is not a fault in the cartridge.
Drawing hole 1 takes the interpreter more than five minutes of emulated
time: subroutine 2500 reads the outline value by value with PEEK, HEX$ and
VAL, and the plan's fills are not quick either. tools/omsx_campo.tcl turns the
speed limiter off and waits until second 380 before dumping.
tools/campo.py draws the course by running the program's own subroutines in
Python, on an emulation of SCREEN 2 with its colour attributes. make vram
compares the result with the emulator's dump:
| region | pixels matching | VRAM bytes matching |
|---|---|---|
| plan view | 15,737 of 15,744 (99.96 %) | 4,217 of 4,224 (99.83 %) |
| 3D view | 18,011 of 18,432 (97.72 %) | 4,439 of 4,608 (96.33 %) |
The plan is the one that counts, because it is the one the game reads with POINT to know what the terrain is. The seven pixels that differ are on the edge of two bunkers.
In the 3D view, the ones that differ are edge pixels of the same lines, off by one. Six variants of the line-drawing were tried and none closes the gap entirely; it is in Open questions.
Emulating the display was not optional - the game reads from it - and the obvious model came out wrong. What real VRAM says:
LINE ... BF is not a heap of points: it fills by whole bytes, and
wherever the row of eight pixels ends up fully covered it leaves the pattern at
zero and the colour in the low nibble. The view's ground reads pattern 00
and colour 0C; the sky, 00 and 05.0C becoming FC, instead of dragging the whole cell with them. If
it was all ink, it goes in as background: that is how the white frame slips
into the plan's border, 60 becoming 6F with the pattern going FF to 7F.SCREEN 2 + CLS leaves the colour table at 0x00, not 0xF0.With the naive model the plan matched at 38 %. With this one, at 99.96 %.