mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 17:06:04 +00:00
compiler: audio driver, u16 arithmetic, multi-scanline, slot recycling
Five language features and optimizations from the planned-work backlog: - **Minimal audio driver**: `play`/`start_music`/`stop_music` now generate APU pulse-1/pulse-2 writes from a builtin SFX/music name table, and the NMI handler gains a `JSR __audio_tick` splice (via the linker's `__audio_used` marker lookup) that ages an SFX countdown counter and mutes pulse 1 when the tone expires. Programs that never trigger audio pay zero ROM cost. - **u16 arithmetic and comparisons**: new IR ops `LoadVarHi`, `StoreVarHi`, `Add16`, `Sub16`, and six `Cmp*16` variants. The lowering context tracks variable types via the analyzer's symbol table and routes expressions through the 8-bit or 16-bit path based on operand width. Add16 emits `CLC;ADC;ADC` with carry propagating naturally into the high byte; compares dispatch high-byte-first with a short-circuit low-byte fallback. Fixes a silent miscompile where `big += 1` on a u16 var only incremented the low byte. - **Multi-scanline handlers per state**: `gen_scanline_irq` now dispatches on `(current_state, ZP_SCANLINE_STEP)` and reloads the MMC3 counter with the delta to the next scanline in the same state. `gen_scanline_reload` resets the step counter at the top of each NMI so a state with multiple handlers fires them in ascending line order. Previously only the first handler per state ever fired. - **IR temp slot recycling**: `build_use_counts` pre-scans each function to count per-temp uses; `retire_op_sources` decrements the counts after each op and pushes dead slots back onto `free_slots` for later allocation. `bitwise_ops.ne` used to crash (debug) or miscompile (release) once it hit 128 concurrent temps; with recycling the same function now uses ~4 slots instead of 136. - **INC/DEC peephole fold + improved dead-load elimination**: `fold_inc_dec` collapses `LDA addr; CLC; ADC #1; STA addr` into a single `INC addr` (and the SEC/SBC variant into `DEC addr`), saving 5 bytes and 5 cycles per increment. The fold is suppressed when the next instruction reads carry. `remove_dead_loads` now walks past INC/DEC/STX/STY (which don't touch A) to find the actual next A-use, catching more dead loads. Tests: 331 unit + 39 integration (up from 313 + 37), including new guards for audio, u16, multi-scanline, and slot recycling. https://claude.ai/code/session_01A8qk3gw2jWSzdiXBZPZSFE
This commit is contained in:
parent
9ebf58f7db
commit
9a539ea068
12 changed files with 2108 additions and 144 deletions
|
|
@ -25,6 +25,15 @@ pub const ZP_INPUT_P2: u8 = 0x08;
|
|||
/// the oldest slot gets overwritten — the classic NES flicker
|
||||
/// fallback.
|
||||
pub const ZP_OAM_CURSOR: u8 = 0x09;
|
||||
/// Pulse-1 SFX countdown frames. `play SfxName` sets this to the
|
||||
/// SFX duration and writes the tone registers; the NMI audio tick
|
||||
/// decrements it every frame, silencing pulse 1 when it reaches 0.
|
||||
pub const ZP_SFX_COUNTER: u8 = 0x0A;
|
||||
/// Pulse-2 music countdown frames. `start_music TrackName` sets
|
||||
/// this to `$FF` (infinite sustain) and writes a pulse-2 tone;
|
||||
/// `stop_music` zeros it and mutes pulse 2. `$FF` skips the NMI
|
||||
/// decrement so music plays until explicitly stopped.
|
||||
pub const ZP_MUSIC_COUNTER: u8 = 0x0B;
|
||||
|
||||
/// Generate the NES hardware initialization sequence.
|
||||
/// This runs at RESET and sets up the hardware before user code.
|
||||
|
|
@ -48,9 +57,29 @@ pub fn gen_init() -> Vec<Instruction> {
|
|||
out.push(Instruction::new(STA, AM::Absolute(PPU_CTRL)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(PPU_MASK)));
|
||||
|
||||
// Disable DMC IRQs
|
||||
// Disable DMC IRQs momentarily (will re-enable the square
|
||||
// channels below so `play`/`start_music` can make sound).
|
||||
out.push(Instruction::new(STA, AM::Absolute(APU_STATUS)));
|
||||
|
||||
// Enable pulse 1 and pulse 2 channels for the minimal audio
|
||||
// driver. SFX runs on pulse 1, music on pulse 2. We leave
|
||||
// triangle / noise / DMC disabled — the engine is deliberately
|
||||
// simple and those channels would go unused anyway.
|
||||
out.push(Instruction::new(LDA, AM::Immediate(0x03)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(APU_STATUS)));
|
||||
// Pre-silence both channels: `$30` on the volume register sets
|
||||
// constant-volume envelope with volume 0 and halts the length
|
||||
// counter, which is the canonical "silent but armed" state.
|
||||
out.push(Instruction::new(LDA, AM::Immediate(0x30)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(0x4000)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(0x4004)));
|
||||
// Clear sweep units so the channel tone doesn't auto-slide.
|
||||
out.push(Instruction::new(LDA, AM::Immediate(0x08)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(0x4001)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(0x4005)));
|
||||
// Restore the zero we need for the subsequent RAM clear below.
|
||||
out.push(Instruction::new(LDA, AM::Immediate(0x00)));
|
||||
|
||||
// Wait for first vblank
|
||||
// vblankwait1:
|
||||
out.push(Instruction::new(NOP, AM::Label("__vblankwait1".into())));
|
||||
|
|
@ -227,6 +256,46 @@ pub fn gen_multiply() -> Vec<Instruction> {
|
|||
out
|
||||
}
|
||||
|
||||
/// Generate the per-NMI audio tick that ages the SFX counter and
|
||||
/// silences pulse 1 when the counter hits zero. Music on pulse 2
|
||||
/// uses the sentinel `$FF` for infinite sustain and is never
|
||||
/// decremented here — `stop_music` handles mute explicitly.
|
||||
///
|
||||
/// The linker splices a `JSR __audio_tick` into the NMI handler
|
||||
/// whenever user code contains any audio ops, so programs that
|
||||
/// never call `play`/`start_music`/`stop_music` pay zero cost.
|
||||
///
|
||||
/// Contract:
|
||||
/// - Input: `ZP_SFX_COUNTER` = remaining frames for pulse 1's tone
|
||||
/// - Effect: decrements the counter; on 0→transition mutes $4000
|
||||
/// - Clobbers: A (which the NMI handler restores via PLA)
|
||||
pub fn gen_audio_tick() -> Vec<Instruction> {
|
||||
let mut out = Vec::new();
|
||||
|
||||
out.push(Instruction::new(NOP, AM::Label("__audio_tick".into())));
|
||||
|
||||
// SFX counter check: if 0, nothing to do.
|
||||
out.push(Instruction::new(LDA, AM::ZeroPage(ZP_SFX_COUNTER)));
|
||||
out.push(Instruction::new(
|
||||
BEQ,
|
||||
AM::LabelRelative("__audio_tick_done".into()),
|
||||
));
|
||||
out.push(Instruction::new(DEC, AM::ZeroPage(ZP_SFX_COUNTER)));
|
||||
// If still non-zero, leave the tone alone.
|
||||
out.push(Instruction::new(
|
||||
BNE,
|
||||
AM::LabelRelative("__audio_tick_done".into()),
|
||||
));
|
||||
// Counter just hit 0: silence pulse 1 (volume envelope = mute).
|
||||
out.push(Instruction::new(LDA, AM::Immediate(0x30)));
|
||||
out.push(Instruction::new(STA, AM::Absolute(0x4000)));
|
||||
|
||||
out.push(Instruction::new(NOP, AM::Label("__audio_tick_done".into())));
|
||||
out.push(Instruction::implied(RTS));
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
/// Generate 8 / 8 -> 8 software divide routine (restoring division).
|
||||
///
|
||||
/// Input: A = dividend, zero-page $02 = divisor
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue