The three steps
- Load. The data goes into memory from the load address (Pages and RST): page 0 in bank 0, page 1 in bank 1.
- INIT. Once per song. The player sets up the registers and the RAM, calls the init address, and waits for the routine to return.
- PLAY. Over and over, at the rate the header gives (Timing), until the song is stopped. Each call writes the next step of the music to the sound registers.
INIT
Before calling INIT, a player:
- initialises the sound registers and the other hardware registers as after a reset, and clears the RAM;
- puts the song number in register A, counted from 0 (the header’s first-song field counts from 1: song 1 is A = 0);
- sets the stack pointer to the header’s value, and pushes the address
$FFFF, so that INIT ends withRETto it; - writes TMA and TAC from the header, and selects the pages as after loading;
- enables the v-blank and timer interrupts (
IE = $05) but leaves the interrupt master flag (IME) clear: INIT itself runs with interrupts off.
INIT must end with RET. It may use the other registers freely and does not need to
keep any of them. When it returns to $FFFF the player executes HALT with
interrupts enabled, and from then on the tune only runs inside interrupt handlers.
PLAY
PLAY is called from the interrupt handler of the timer or v-blank interrupt, whichever the header
chooses, and ends with RET to the handler’s RETI. The routine has to be short enough to end before the next call. It decides what the
music does on this step (a new note, a slide, a volume change) and writes it to the sound
registers (Sound registers). It may also select a
page and read data from it.
Players differ
The specification describes PLAY as an interrupt service routine. gbsplay runs real interrupts
as a Game Boy does, and the explorer follows it: an SM83 processor model runs INIT, then halts at
$FFFF, and each timer or v-blank interrupt costs five machine cycles (two idle, the
return address pushed in two writes, the jump to the vector) before the vector’s code runs. So a
tune that depends on interrupts, on HALT, on the timer registers, on the line counter
or on the cycle a write reaches the sound chip behaves as on a Game Boy. The vectors are in the
player’s own ROM below the load address, one of three layouts:
| Header | $40 (v-blank) | $50 (timer) |
|---|---|---|
| TAC bit 2 set (timer) | RETI | CALL PLAY, RETI |
| TAC bit 2 clear | CALL PLAY, RETI | RETI |
TAC & $78 = $40 | CALL load+$40, CALL PLAY, RETI | CALL load+$48, RETI |
The other interrupt vectors ($48, $58, $60) hold
RETI, and the eight restart addresses $00 to $38 jump to the
load address plus the same offset, as Pages and RST
describes. The hardware a tune can read or write (the divider and timer, the line counter and LCD
status, the serial port, the joypad, the sprite table with its DMA, video RAM) is modelled in the
explorer’s hardware.js, so a tune that polls the line counter waits as long as it
would on a Game Boy. Game_Music_Emu instead calls PLAY from outside with interrupts off, and
starts the other registers at 0.
The explorer’s options say what the file cannot: the eight Joypad options hold keys down
for the whole song (P1, $FF00: bit 5 low selects the buttons, bit 4 low the directions,
both selected read ORed, a pressed key reads 0; opposite directions are masked; the selection takes
effect 12 or 24 cycles after the write, and a line going low sets IF bit 4), and the
Link cable option says what a serial transfer shifts in (SB $FF01,
SC $FF02: nothing connected gives 1 bits and SB $FF; a partner
that sends 0 bits; or the sent bits returning). An internal-clock transfer shifts a bit on each
falling edge of divider bit 7 every other time, so a byte takes 3841 to 4096 cycles; writing
SC restarts the count; a transfer on the external clock never ends. These follow SameBoy
(Core/joypad.c, Core/timing.c, Core/memory.c) and Pan Docs
(“Joypad Input”, “Serial Data Transfer”); 240 joypad cases and 64 serial cases for each partner give
the same bytes in the explorer and in SameBoy running the same file.