Introduction
A GBS file holds music from the Game Boy and the Game Boy Color. It does not store notes or
audio: it stores the original SM83 machine code that played the tune, cut out of a game’s ROM,
plus a small header saying where to load it and how to call it. A player puts the code into
memory, calls the tune’s INIT routine with the number of the song, and then calls its
PLAY routine over and over, at the rate of the screen’s v-blank (about 59.7 times a
second) or of the Game Boy’s timer. Each PLAY writes to the sound registers
$FF10 to $FF3F, and a player has to emulate the Game Boy’s sound chip
to hear the result.
The format is a sibling of NSF (the same idea for the NES) and PSID. The programs usually keep their own music engine and song tables, so a GBS file of a whole game can have dozens of songs and sound effects, each chosen by a number.
Structure
The file starts with the three bytes G B S. The rest of the 112-byte header is
little-endian: the version (always 1), the number of songs, the first song (counted from 1),
the load, init and play addresses, the stack pointer, the timer modulo and timer control bytes,
and three 32-byte strings for the title, the author and the copyright. The program and its
data follow, up to the end of the file.
0Magic and version GBS and the version 14 bytes4Songs the number of songs and the first one2 bytes6Addresses load, init and play, then the stack pointer8 bytes14Timer TMA and TAC: the PLAY rate2 bytes16Text title, author, copyright96 bytes112Code and data the SM83 program and the music, from the load address onvaries
The header page gives every field. Pages and RST says how the data is cut into the 16 KB pages of a ROM and how the restart instructions reach the program, INIT and PLAY what a player does before and after calling them, timing how often PLAY is called, and sound registers what the tune writes to the sound chip.
What the explorer shows
The explorer reads the whole file: every byte of the header is in the structure tree, and the
data is shown as the pages it makes up; a field that breaks a rule of the format (a version
other than 1, a load address outside $0400 to $7FFF, a first song that
does not exist, reserved timer bits) is listed as a problem on the field. It then runs the tune.
An SM83 processor (the Game Boy’s, not a Z80) is emulated cycle by cycle, one bus access per
machine cycle, with the pages of the ROM, the RAM and the timer registers around it; INIT is
called with the song, and PLAY at the rate the header gives. The four channels of the sound chip
(two pulse channels, the wave channel and the noise channel) are mixed in stereo, played and
drawn as a waveform. The program is also disassembled as SM83 code, page by page, with the
names of the hardware registers and the INIT and PLAY entry points. The song, the length and
the channels to silence are set in the options bar. The sprite table has the original Game
Boy’s corruption bug: while the picture unit scans it (the first 80 cycles of a line) and a
program reads or writes it, or puts an address in $FE00 to $FEFF on the bus
with INC, DEC, PUSH, POP, CALL,
RST, JR, LD SP,HL or an interrupt, the row it is scanning
is corrupted as SameBoy corrupts it (all 768 cases of a test with six operations at 128 cycles
of a line agree byte for byte).
The player is a Game Boy, in gbsplay’s model: it sets TMA, TAC and the bank, enables the
v-blank and timer interrupts, runs INIT with the song in A and the address
$FFFF as its return address, then halts there. The hardware raises the
interrupts (the timer from DIV and TAC, v-blank every 70,224 cycles) and
the vectors at $40 and $50 call PLAY, so a program that reads the
divider, the timer, LY or STAT, uses its own interrupt handler or waits with
HALT sees what a Game Boy would show it. A GBS file
does not say how long a song lasts, so the length is the Length option (60 seconds by default).
The picture, the joypad and the link cable
A GBS player draws no picture, because nothing in a sound program depends on the pixels: what a
program can learn about the picture unit it learns through LY, STAT, the
STAT interrupt, the sprite table, video RAM and the HDMA of the Game Boy Color, and all of those are
modelled. The length of mode 3 (the line’s pixel transfer) is 172 cycles plus the low three bits of
SCX, 6 for a visible window and the per-sprite penalties, as Pan Docs “Mode 3 length”
describes; the sprite table and SCX, WX, WY, LCDC
a program has written decide it, so STAT leaves mode 3, the sprite table and video RAM unblock and an
HDMA block of the Game Boy Color can start (as far as Pan Docs and SameBoy’s source say) at the
matching cycle; 128 configurations of scroll, window and sprites agree with SameBoy on STAT,
on the cycle at which STAT leaves mode 3 and on the sprite-table accesses. What cannot be observed from a sound
program is the colour of a pixel, because no register reports it.
The joypad register $FF00 is modelled: the Joypad options hold any of the eight keys
down for the whole song, a program selecting buttons, directions or both reads them as the Game Boy
does (opposite directions are masked, the lines read 1 when released), the selection takes effect
12 or 24 cycles after the write as on an original Game Boy, and a key line going low raises the joypad
interrupt (IF bit 4). The serial port is modelled too: a transfer started with the internal
clock shifts one bit every 256 cycles, takes 3841 to 4096 cycles depending on when the divider’s bit
falls, and ends with the serial interrupt; the Link cable option chooses what comes in
(nothing connected: every bit reads 1; a partner sending 0; the sent bits coming back), and a transfer
on the external clock never ends. 240 joypad cases and 64 serial cases per partner, run in the explorer and
in SameBoy, give the same bytes in every case. The Game Boy Color’s fast serial clock (every 8
cycles) and its double-speed clocking are taken from SameBoy’s source (Core/timing.c) and
Pan Docs.
Sources
- GBS File Specification 1.02 by Scott Worley (OverClocked ReMix wiki), checked against the VGMPF page and the source of gbsplay (
gbs.c,gbhw.c,mapper.c). - Audio Registers and Audio details in Pan Docs, for every sound register.
- The processor and the hardware around it (divider, timer, line counter, interrupt flags, speed switch) are checked with Blargg’s
gb-test-roms:cpu_instrs(11 ROMs),instr_timing,mem_timing(3 ROMs),halt_bugandinterrupt_time, all 17 pass (interrupt_timeis a Game Boy Color ROM that switches to double speed). The register values and read masks follow SameBoy (Core/memory.c,timing.c,apu.c) and Pan Docs; the interrupt model is gbsplay’s (gbhw.c,gbs.c). - The sound chip is the core of the VGM viewer (a port of libvgm’s SameBoy sound code); the whole player is compared with Game_Music_Emu on built tones: the pitch agrees to under 0.5 Hz, the spectrum correlates 0.82 to 1.00 (the level is 1.3 times Game_Music_Emu’s, whose mixer differs, and 64 / 34 = 1.88 times SameBoy’s, because libvgm’s
MAX_CH_AMPis0x1E00and SameBoy’s0x0FF0). The same tune run as a Game Boy ROM in SameBoy gives a correlation of 0.995 to 0.999 on the tone channels. - The sprite-table corruption follows SameBoy (
Core/memory.c,Core/display.c): a test of six operations at 128 cycles of a video line, run in the explorer and in SameBoy, leaves the same 160 bytes of the table in all 768 cases. - The length of mode 3 follows Pan Docs “STAT modes” and “Mode 3 length” and SameBoy (
Core/display.c); the joypad follows SameBoy (Core/joypad.c,Core/memory.c) and Pan Docs “Joypad Input”; the serial port follows SameBoy (Core/timing.cGB_serial_master_edge,Core/memory.c) and Pan Docs “Serial Data Transfer”. Test GBS files written for the site (mode3.gbs,m3probe.gbs,joypad.gbs,serial.gbs) run in the explorer and in SameBoy give the same results: 0 differences in 128 mode 3 cases, 240 joypad cases for each of nine key sets and 64 serial cases for each of three partners. - The sample files are gbsplay’s
nightmode.gbs, the hUGEDriver example song built with its driver (hUGETracker and hUGEDriver are dedicated to the public domain) and tunes built for the site with rgbds.
Links
Software
- gbsplay – command-line GBS player that runs real Game Boy interrupts (Linux, macOS)
- MAME sound devices – source of sound chip emulations
Reference
- GBS File Specification 1.02 – the original document by Scott Worley
- GBS – the Video Game Music Preservation Foundation page
- Pan Docs: Audio – the Game Boy sound hardware