OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

Hot-swapping APU programs at runtime — apuReset() in action. More...

#include <snes.h>
#include <snes/apu.h>
#include <snes/input.h>

Macros

#define SPC_BASE   0x0200

Functions

int main (void)
static void start_song (u8 song)
 Upload and start one of the two APU programs.

Variables

u8 current_song
 Active program: 0 = drums, 1 = cello (probe oracle).
u8 spc_cello []
u8 spc_cello_end []
u8 spc_drums []
u8 spc_drums_end []

Detailed Description

Hot-swapping APU programs at runtime — apuReset() in action.

The apu module's upload path (apuWaitBoot/apuUpload/apuExecute) runs once, at boot. This example adds the missing capability: replacing the running APU program at ANY time. The method is krom (Peter Lemon)'s PlayTwoSong protocol, productized: the 65816 writes APU_RESET_MAGIC to I/O port 0 (apuReset()); the SPC700 program polls for it in its wait loops (the APU_CHECK_RESET idiom), silences the DSP, and jumps back to the IPL boot ROM — ready for the next upload.

Press A for the play_noise drum kit, B for the pitch_mod vibrato cello. Both APU programs are this repo's own — no external assets beyond the cello sample already attributed.

ROM mode: LoROM (project default).

SNES Concepts
  • Cooperative APU reset: CPU→APU messaging over $2140 after boot
  • The IPL ROM is re-entrant — jump to $FFC0 with the ROM re-enabled and the full boot handshake ($AA/$BB, $CC upload) works again
  • Two independent APU programs in one ROM, uploaded on demand
What to Observe
Drums start immediately. Press B: the kit cuts, the cello blooms. Press A: back to the drums. The backdrop tracks the active program (red = drums, amber = cello). Swap latency is a few frames — the SPC polls for the reset request every millisecond.
Modules Used
console, apu, input
See also
https://github.com/PeterLemon/SNES — PlayTwoSong, the original protocol demo (its songs use samples ripped from commercial games, so this port swaps between two OpenSNES-native programs instead)
drums.spc700.asm, cello.spc700.asm — the two APU programs

Macro Definition Documentation

◆ SPC_BASE

#define SPC_BASE   0x0200

Function Documentation

◆ main()

int main ( void )

< A button

< B button

◆ start_song()

void start_song ( u8 song)
static

Upload and start one of the two APU programs.

Variable Documentation

◆ current_song

u8 current_song

Active program: 0 = drums, 1 = cello (probe oracle).

◆ spc_cello

u8 spc_cello[]
extern

◆ spc_cello_end

u8 spc_cello_end[]

◆ spc_drums

u8 spc_drums[]
extern

◆ spc_drums_end

u8 spc_drums_end[]