Under the hood

Getting started

You need Python 3, GNU make, Pasmo and your own image of the cartridge as dunkshot.rom at the root of the repository (32,768 bytes; make comprueba checks the sha256). The ROM is not distributed.

make            # listing, reassembly, checks and tests
make imagenes   # the website pictures, from the ROM
make web        # the bilingual website in docs/
make parche     # the MSX2 patch: work/dunkshot_msx2.rom and parche/dunkshot_msx2.ips

make generates src/dunkshot.asm from the binary and the notes (src/dunkshot.notes), reassembles it with Pasmo and checks that the exact ROM comes out; then it runs the checks that reassembly does not cover: that no data is read as code, that no entry point falls inside a data area and that not a single byte is left unassigned.

The tools

The notes

The listing is not edited by hand: every comment is a directive in the notes file anchored to an address (C 0x5F33 text), every named routine an L, and every data block a D with its measurement. make listado applies them and tools/valida_c.py warns if one lands mid-instruction or is repeated.

The cartridge

32 KB with no mapper in pages 1 and 2 (0x4000-0xBFFF), an AB header with INIT at 0x4010 and the other three pointers at zero. INIT enables the slot in page 2, hooks H.TIMI to 0x606E and enters the loop at 0x4044.

What is inside

range what it is
0x4010-0x8ED4 code (20,642 bytes in all, with the final stretch)
0x8ED5-0x8F7D the sound: 64 periods, the mixer, 13 pointers and 13 priorities
0x8F7E-0x9664 the sound pieces and their shared phrases
0x9665-0x9724 the net and the ball, six sprite patterns
0x9725-0xA38D 196 sprite patterns of the players, compressed
0xA38E-0xA74D the poses: 40 layouts and 160 poses
0xA74E-0xABA7 the 250 patterns of the court, the menus and the font
0xABA8-0xAEBF three colour tables, one per court
0xAEC0-0xB967 colours, plays, name tables, the logo and the court map
0xB968-0xBFD3 code: controls, dispatcher and the computer's intelligence
0xBFD4-0xBFFF 44 bytes of 0xFF

And scattered through the code, the small tables: the texts (0x7C55), the foul labels (0x796F), the look pairs (0x80E4), the tip-off positions (0x5255), the directions (0x693D, 0xBA7B) and the team-mates (0xBF1E).

The 250 court patterns

Four compressors

Every block ends exactly where the next one starts and measures what the code copies afterwards; the tests check it.

The VRAM

SCREEN 2 as INIGRP leaves it: patterns at 0x0000 (the court's 250 in all three thirds and the logo at 0xB0-0xFF), names at 0x1800, sprite attributes at 0x1B00 and 0x1F00, colours at 0x2000 (the chosen court's table in all three thirds, with 0xAEC0 on top at 0x2100) and sprite patterns at 0x3800: each player has four at 0x3800 + n × 128 that 0x5FD1 copies from RAM when the pose changes; the net at 0x3B00 and the ball, the shadow and the arrow at 0x3B60.

The 80 logo patterns

The RAM

Six 0x30-byte player records at 0xE500 (0x4DBF indexes them; +1 is Y, the depth, and +3 is X along the court, signed from the centre), the ball at 0xE620, the shadow of the sprite attributes at 0xE650, the two teams at 0xE851 and 0xE93D, the plays at 0xEA89, the sound variables at 0xEFE1-0xEFE8 and the window column at 0xE830. At 0xC000-0xC2FF are the perspective tables that 0x5A58 builds when a match starts, and 0xC400 is the buffer everything is decompressed into.

The tape

A team is saved as a BSAVE block: the cassette BIOS writes ten 0xD0 bytes and a six-letter name (the team's three letters and three 0x7F), then the start, end and execution addresses and the team's 0xEC bytes (0x8C33). Loading (0x8C71) looks for a header whose name ends in those three 0x7F, and VERIFY (0x8C87) compares byte for byte.

LOAD DATA with no tape

The code

Start-up and the loop

INIT (0x4010) sets the stack at 0xF200, enables the slot in page 2, hooks the frame interrupt to 0x606E and starts: 0x5189 clears the RAM, 0x501F sets SCREEN 2 and loads patterns, sprites and colours, 0x7322 leaves the teams ABC and DEF, and the loop at 0x4044 chains title (0x839C), wait (0x8B28), menu (0x83BA), SET-UP (0x812F) and match. The interrupt handles the players' turning, the falls, the net and the buttons (0x60A0-0x62E3).

The sprites and the flicker

Every frame, 0x5C40 works out each player's four sprites: it subtracts the pose offsets (0xA38E) from the position of the feet and adjusts the height with the perspective curves at 0xB59E and 0xB61E (0x5DE4, 0x5DAF). Since the TMS9918 draws no more than four sprites per line, 0x5E1B sorts the seven groups (six players and the fixed ones) by depth, the nearest first, 0x5C0B builds their attributes in that order, 0x5BAB copies them reversed into another buffer and the two go to the two attribute tables (0x1B00, 0x1F00); 0x5B77 flips bit 2 of register 5 every frame to show one or the other: one frame the near players are in front, the next the far ones. That is what makes the players flicker when they bunch up. On an MSX2 the V9938 draws eight per line: The MSX2 patch takes the game there, measured before and after.

The 160 poses

The window over the court

The 56 × 24 map is in RAM (0xC400, decompressed by 0x5F8D). 0x5F33 copies into the name table the 24 rows of 32 bytes starting at the column in 0xE830, with 0x5B07 (SETWRT and outi); 0x5E7C decides when to move it. There is no fine scrolling: the screen jumps one column at a time.

Drawing into VRAM

Three routines do it all: 0x5B07 writes BC bytes from HL to DE, 0x5B50 fills BC bytes with A and 0x5AF7 writes one byte at HL. On top of them go 0x7C1B (a zero-terminated string), 0x7C29 (B rows of C characters), 0x7FC1 (a box with patterns 0xF5-0xF7 and 0x62-0x67) and the number printers at 0x7ACF, 0x7ADA and 0x7AA7, which blank the leading zeros by counting in C the digits already written.

The menu selector

0x8AC9 takes the cursor position, the number of options and the ball pattern (0x6F); it reads with CHGET, moves up and down with 0x1E and 0x1F and returns any other key. The chosen option lives in 0xEFCD, whose bit 7 says whether the right-hand team is being dealt with.

The computer

With the ball, 0xBB14 takes the player through five states (0xF009): pick a point towards the basket with sine and cosine from the tables at 0xC000, run to it, face the basket, go to the shooting spot and shoot, steal or pass according to chance and the +0x1D and +0x1E skills. Without the ball, 0xBD61 (0xF00A) goes for the ball carrier, marks him or stays put, and 0x76AE measures the shooting zone. The plays at 0xEA89 give each player a script of waypoints to go to.

The sound

0x8D42 starts piece A if its priority (0x8F71) is not lower than the one playing; 0x8D8B runs every frame and 0x8DB2 interprets: 0x40-0xFF note (channel in the two high bits, index into the table of 64 periods), 0x20-0x25 mixer, 0x26-0x2F register, 0x33 jump, 0x34/0x35 call and return with its own stack in IX, 0x36/0x37 loop, 0x38-0x3B relative volume, 0x3D silence, 0x00-0x1F wait; a wait of zero ends the piece. Thirteen pieces, ten distinct: the music is piece 1 (and 8, the same pointer) and 4, 5 and 6 are the same one with three priorities.

Chance

The Z80's R register (0x50E9, 0x6F53, 0x6F76): one bit or one byte, as needed.

What nobody runs

Seventeen pieces of code with no callers, declared in src/dunkshot.entries with their reason: alternative entries (0x59BA, 0x59E8, 0x5AFD), loose routines (0x441D negates HL, 0x6F76 gives 1 or 2 at random, 0x8D3B waits for the sound to end), a five-digit counter (0x7AAE) and 0x8B3C, which writes a zero at 0x785B, which is ROM: a leftover from when the program was tested in RAM.

In the emulator

The checks were made with openMSX and the C-BIOS_MSX1_JP machine. The script tools/omsx_vuelca.tcl presses the keys it is told at the seconds it is told and dumps the whole VRAM, the RAM from 0xC000 up and the VDP registers; tools/lanza_vuelca.sh launches it:

sh tools/lanza_vuelca.sh work/v_titulo "6 12"
sh tools/lanza_vuelca.sh work/v_partido2 "16 20 25 30 45" "4 8 0x01 8 8 0x01 12 8 0x01"
sh tools/lanza_vuelca.sh work/v_menus "12 19 26" "4 8 0x01 7 8 0x40 9 8 0x01 14 8 0x40 16 8 0x01"

Keys go as second row mask of the keyboard matrix: space is row 8 with 0x01 and down is row 8 with 0x40. Space at seconds 4, 8 and 12 takes you from the title to the menu, from there to SET-UP and from there to the match.

Then tools/coteja.py compares the built VRAM with the dump: the name table and, for each third, the pattern and colour of the tiles in use. What is left in VRAM from earlier screens and not used does not count.

python3 tools/coteja.py work/partido_12.vram work/v_partido2/t020.vram work/v_partido2/t020.regs

What was checked

At 0 differences: the title (two dumps), the first menu, SET-UP, the team card, LOAD DATA with SURE? and with ERROR!, and the match at the tip-off (two dumps, with the window at column 12). And the 24 sprites of the six players in those two dumps: pose, patterns, colours and the foot position that follows from the four offsets.

The title

The dumps do not travel with the repository; what travels is the sha256 of each built VRAM (tests/test_juego.py), so that a change in the decompressors or in the build shows up in the tests.