No play routine
A HES file has no play address. The tune is a program for the whole console: it sets itself up in the init routine, then lets interrupts (the timer, or the vertical blank of the video chip) run its player. A HES player therefore has to behave like the console: it runs the processor, raises the interrupts at the right rate and mixes what the PSG produces.
Starting a song
This is what Game_Music_Emu does (start_track_), and the explorer does the same:
- Clear the 8 KB of RAM (and the SuperGrafx pages) to zero. Reset the PSG, the timer and the VDC state.
- Load MPR0 to MPR7 from the header.
- Set A to the song number (the start song, or the song the listener chose), X and Y to 0, the interrupt-disable flag on. The stack pointer is
$FD. - Push a return address on the stack that points at
$1FFF; the byte there is the idle loop (a jump to itself in the player’s own memory), so when the init routine ends with RTS the processor lands on it. - Set the program counter to the init address and run.
- When the processor returns to the idle address, clear the interrupt-disable flag. From now on the interrupts drive the tune.
The init routine may take as long as it needs; some do all the setting up of their song and enable the timer or the
vertical-blank interrupt themselves, others rely on the player to do it. NEZplug++ does it a little differently: it puts a
JSR init and a jump-to-self in a six-byte stub at $1FF0, runs for up to five seconds of console time until the
stub has finished, and starts with the song number in A but the stack pointer at $FF.
The timer
The timer is a 7-bit down counter, written at the hardware page offset $0C00 (the reload value, bits 0 to 6) and
started or stopped by bit 0 of $0C01. Reading $0C00 gives the counter. It counts the console’s
7,159,091 ticks per second divided by 1,024: the period of one count is 1,024 ticks, so a reload value of n raises
an interrupt every (n + 1) counts, about 6,991 per second divided by (n + 1). When the timer interrupt is taken the
handler must acknowledge it by writing any value to $1403; otherwise it is raised again at once.
Game_Music_Emu’s load of a started timer is (n + 1) × 1,024 + 1 ticks; the explorer uses the same value.
Interrupts
| Source | Mask bit ($1402) | Vector | Rate | Acknowledge |
|---|---|---|---|---|
| Timer | 4 | $FFFA | Set by the timer reload value | Write to $1403 |
| IRQ1: VDC vertical blank | 2 | $FFF8 | 262 lines of 455 ticks: 119,210 ticks, 60.05 per second | Read $0000 (the VDC status) |
| IRQ2 and BRK | 1 | $FFF6 | Never raised by a player; BRK raises it | |
| NMI | none | $FFFC | Never raised by a player |
A mask bit that is set disables its source; the tune clears the bits it wants. The VDC interrupt is raised only when
bit 3 of VDC register 5 is set (the tune writes it with ST0 #5 and ST1 / ST2). When both the timer and the VDC are
pending the timer is taken first. Taking an interrupt costs 8 cycles, sets the interrupt-disable flag, and clears the
decimal and T flags.
Length and tempo
Neither the file nor the init routine says how long a song lasts, so a player needs a length of its own: Game_Music_Emu uses a default of a few minutes and lets the user change it. The explorer renders 90 seconds unless the options bar says otherwise (up to 600) and has a tempo option that scales the clock the interrupts run on.
In the explorer
The Tune tab describes the start in the order above. The Trace tab shows the first 48 instructions the processor runs, with A, X, Y, the flags, the stack pointer and the tick count, a line for each interrupt entry, and the first 40 PSG register writes. The Playback tab plays the tune.
Divergence from the specification
There is no specification. The explorer follows Game_Music_Emu for the start (the idle loop at $1FFF and the stack
pointer $FD) and the timer, the VDC and the priority between them. NEZplug++ differs in the stack pointer, in
running the init routine for up to five seconds before the interrupts are enabled, and in treating the timer counter and its reload
separately (COUNTER-- reaching zero), so a song that reads the timer back sees slightly different values.
Mednafen emulates the console and has no HES start at all.
Sources
- Game_Music_Emu 0.6.1,
Hes_Emu.cpp(start_track_,run_until_,irq_changed,cpu_read_,cpu_write_) andHes_Cpu.cpp: the start state, the timer, the VDC period and the masks. - NEZplug++,
m_hes.c(reset,timer_event,vsync_event): the second implementation and the differences above. - The HuC6280 manual (Hudson Soft) for the interrupt vectors and the mask register, as summarised by Mednafen’s
huc6280.cpp.