Timer control (TAC)
| Bits | Meaning |
|---|---|
| 1-0 | Counter rate: 0 = 4096 Hz, 1 = 262144 Hz, 2 = 65536 Hz, 3 = 16384 Hz. |
| 2 | Interrupt type: 0 = v-blank, 1 = timer. |
| 6-3 | Reserved, 0. |
| 7 | 1 = the Game Boy Color’s double speed; the counter rates double. |
The PLAY rate
With bit 2 clear, PLAY is called at every v-blank: once per frame of 70224 cycles of the 4.194304 MHz clock, about 59.73 times a second, and TMA and bits 1-0 do not matter.
With bit 2 set, a counter counts at the chosen rate from TMA up to 256 and then raises the interrupt and starts again at TMA. So PLAY is called at
counter rate / (256 − TMA)
times a second. Examples: TAC 4 (4096 Hz) with TMA 192 gives 4096 / 64 = 64 Hz; TAC 5 (262144 Hz) with TMA 0 gives 1024 Hz. A tune can write TMA and TAC again to change its tempo while it plays.
Double speed
On the Game Boy Color the CPU can run at 8.388608 MHz. Bit 7 of TAC says the tune expects
that, and doubles the four counter rates, so the same TMA gives a PLAY rate twice as high.
In v-blank mode the rate does not change. The explorer follows this: its sample
timer-double-speed.gbs (TAC $84, TMA 0) calls PLAY 32 times a second,
as the counter at 4096 Hz doubled and divided by 256 does.
How the explorer times it
The explorer follows gbsplay (gbhw.c): the processor runs cycle by cycle, and the
hardware around it is advanced to each access, so every write to a sound register reaches the sound
chip at the clock cycle it was made.
- Interrupts. An interrupt is taken between instructions when IME is set and
its flag and enable bits are both set; the dispatch takes five machine cycles (20 clock
cycles): two idle, the high and the low byte of the return address pushed, and the jump
to
$40(v-blank) or$50(timer). A halted processor wakes on the flag and continues with the same dispatch. - Divider and timer. The 16-bit divider counts clock cycles; DIV is its high byte, and a write clears it. TIMA counts at the TAC rate from the divider’s bits, reloads from TMA on overflow and raises the timer interrupt. The sound chip’s frame sequencer (length, envelope, sweep) takes its clock from the same divider.
- Line counter. A frame is 154 lines of 456 clock cycles (70224 in all);
$FF44(LY) and the mode in$FF41follow them: lines 144 to 153 are v-blank, which raises the v-blank interrupt at the start of line 144. A tune that waits for a line therefore waits as long as on a Game Boy. The player starts at the beginning of line 144 (gbsplay’s start); a Game Boy started by its boot ROM is at another phase of its frame, and the sound it makes can differ by a fraction of a frame from the explorer’s. - Modes of a line. Each visible line starts with mode 2 (sprite-table scan, 80
cycles), then mode 3 (pixel transfer), then mode 0 up to cycle 456. Mode 3 lasts 172 cycles plus
the low three bits of
SCX(the first tile’s discarded pixels), 6 cycles when the window starts on the line, and 6 cycles for each of up to ten sprites on the line plus, for the first sprite on each background tile, 5 minus its pixel offset in that tile when that is positive (11 for a sprite atX= 0; Pan Docs “Mode 3 length”); the explorer takes the registers and the sprite table the program has written, soSTAT, the sprite-table and video-RAM blocking ($FE00to$FE9Fduring modes 2 and 3,$8000to$9FFFduring mode 3) and the STAT interrupt follow the length. No pixels are drawn, since no register reports one. All 128 test configurations (scroll, window, sprites) give the sameSTATas SameBoy, and the cycle at whichSTATleaves mode 3 matches it as well. The test programs aremode3.gbsandm3probe.gbsin the samples, compared byscripts/gbs/mode3-check.mjsandm3probe-check.mjs; the source for the rules is Pan Docs “STAT modes” and SameBoy’sCore/display.c.
Song length
A GBS file has no song lengths: a player plays until stopped. The explorer plays the number of seconds in the Length option (60 by default).