A SID file’s code expects a C64 in a known state. The
header says where to load the code and how to
call it, and this page gives the rules for doing so, from
SID_file_format.txt.
Loading
- If the header’s loadAddress is
$0000, the first two data bytes give the load address, little-endian, and are not loaded. - If loadAddress is not 0, loading starts with the first data byte. Any address prefix in the data must be removed, or everything loads two bytes out.
- For RSID, the effective load address must be at least
$07E8.
initAddress
The accumulator holds the song number minus one when init is called.
| Case | Value |
|---|---|
| PSID with the MUS player flag | Must be $0000: the external player supplies the entry point. |
| RSID with the BASIC flag | Must be $0000: the BASIC program is started. |
| RSID without the BASIC flag | At least $07E8, inside the payload, and not in ROM or I/O ($A000–$BFFF, $D000–$FFFF). |
Otherwise, $0000 | Means the effective load address. |
playAddress
| Case | Value |
|---|---|
| PSID without the MUS player flag | $0000 means the program installs its own interrupt handler. |
| PSID with the MUS player flag | Must be $0000: the external player’s entry point is used. |
| RSID | Must be $0000. |
RSID rules
An RSID file must be rejected unless all of these hold:
- The version is 2, 3, 4 or 5.
- loadAddress, playAddress and speed are 0.
- The effective load address is at least
$07E8. - With the BASIC flag set, initAddress is
$0000; without it, initAddress meets the rule above.
An RSID tune uses the default C64 power-on environment and sets up the hardware
itself. With the BASIC flag, the song number (0 for song 1) is put at
$030C.
Initial environment
| Item | PSID | RSID |
|---|---|---|
$02A6 | 1 for PAL, 0 for NTSC. | |
| VIC | IRQ set to any raster value below $100. Enabled when the song’s speed bit is 0, disabled otherwise. | IRQ set to raster $137, not enabled. |
| CIA 1 timer A | Set to 60 Hz with the counter running. IRQs are on when the speed bit is 1, off otherwise. | Set to 60 Hz with the counter running and IRQs on. |
| Other timers | Disabled and loaded with $FFFF. | |
| Bank register | Written on every init and play call, by address. | $37. |
For a PSID tune with CIA timing, players read $02A6 to set the CIA timer:
$4025 on PAL, $4295 on NTSC. When the tune’s video standard
differs from the machine, a CIA timer may stand in for VBI timing:
$3FFB for an NTSC tune on PAL and $5021 for a PAL tune on
NTSC.
Bank register (PSID)
| Address called | Bank register |
|---|---|
Below $A000 | $37 |
$A000 to below $D000 | $36 |
$D000 to below $E000 | $34 |
$E000 and above | $35 |
MUS tunes and BASIC tunes
With the MUS player flag set, the file holds music data for Compute!’s Sidplayer and no player code; a player must merge in its own. With the BASIC flag set on an RSID file, the file is a C64 BASIC program.
How a player starts a tune
The format document gives the state a tune can rely on. The open players (libsidplayfp, VICE) turn it into a start-up routine of their own; the explorer’s player does the same steps, in this order, before the first instruction of the tune:
- The tune’s data are copied to memory at the load address, and the byte at
$02A6is set to 1 (PAL) or 0 (NTSC). - All interrupts are switched off and cleared: the VIC-II mask (
$D01A) and flags ($D019), and the interrupt control registers of both CIAs. - The SID master volume is set to 15, and CIA 1 timer A gets its 60 Hz latch:
$4025(16,421 cycles plus one) for PAL,$4295for NTSC. - The VIC-II raster compare is set to line 0 for a PSID, or to line
$137(311) for an RSID whose clock matches the machine. - A PSID song whose speed bit is 0 gets the VIC raster interrupt; one whose bit is 1 gets the CIA 1 timer A interrupt. An RSID gets the CIA 1 timer A interrupt, as a C64 does after it starts.
- The memory bank register (
$01) is set from the init address ($37,$36,$35or$34, above;$37for an RSID), the accumulator is the song number minus one, and init is called as a subroutine. A PSID is called with its interrupt flag set; an RSID with it clear. - When init returns, a PSID with a play address has its bank register set to
$37and interrupts enabled, and the processor idles. On every interrupt the KERNAL’s IRQ entry (the code at$FF48, which pushes the registers and jumps through$0314) reaches a small routine that saves$01, writes the bank value for the play address, calls play with the accumulator at 0, restores$01, acknowledges the VIC and CIA interrupts, pulls the registers and returns. - A PSID without a play address, and an RSID, has no such routine: the tune’s own interrupt handler (set through
$0314, or through$FFFEin the RAM under the ROM when the bank register hides the KERNAL, and through$FFFAfor the NMI) runs on the interrupts the tune has enabled.
The routine needs a free page of memory; it goes into the pages the header’s start page and page length name,
or into the first page the tune’s data do not use (from $0400, never in $A000–$BFFF).
What the explorer emulates of the C64
- The 6510 processor, instruction by instruction, with the documented cycle counts, the stable undocumented
opcodes, and the two side effects tunes use: a read-modify-write instruction writes the unchanged byte back first
(so
ASL $D019andDEC $D019acknowledge a raster interrupt), and an instruction with no operand still reads the byte after it (a tune can runRTIfrom$DD0Cto acknowledge the CIA 2 NMI by the read of$DD0D). - The memory banking of the processor port, with RAM under the ROMs and the I/O area. There is no BASIC or character ROM,
and the KERNAL is a stand-in with the interrupt and NMI entries, the routines tunes jump back into
(
$EA31,$EA7E,$EA81,$FE47), the vectors, and a return at every jump-table entry. - Both CIAs: timers A and B with latches, one-shot and continuous modes, timer B counting timer A, the interrupt control registers, and a time-of-day clock. No keyboard or joystick: they read as nothing pressed.
- The VIC-II raster counter and raster interrupt, for PAL (312 lines of 63 cycles) and NTSC (263 lines of 65). No bad lines, no sprites, no stolen cycles.
- One to fifteen SID chips (several SID chips).
The processor was checked with Klaus Dormann’s 6502 functional test, which exercises every documented instruction, flag and addressing mode, decimal mode included, and passes.