use super::*; use prometeu_drivers::{AudioCommand, AudioTransport}; struct Harness { runtime: VirtualMachineRuntime, vm: VirtualMachine, platform: TestPlatform, log: LogService, fs: VirtualFS, fs_state: FsState, memcard: MemcardService, files: HashMap, next: u32, } impl Harness { fn new(code: &str, name: &str, args: u16) -> Self { let mut runtime = VirtualMachineRuntime::new(None); let mut vm = VirtualMachine::default(); let mut log = LogService::new(4096); let program = serialized_single_function_module( assemble(code).unwrap(), vec![SyscallDecl { module: "audio".into(), name: name.into(), version: 1, arg_slots: args, ret_slots: 1, }], ); runtime .initialize_vm(&mut log, &mut vm, &cartridge_with_program(program, caps::AUDIO)) .unwrap(); Self { runtime, vm, log, platform: TestPlatform::new(), fs: VirtualFS::new(), fs_state: FsState::Unmounted, memcard: MemcardService::new(), files: HashMap::new(), next: 1, } } fn tick(&mut self) -> Option { self.runtime.tick( &mut self.log, &mut self.fs, &mut self.fs_state, &mut self.memcard, &mut self.files, &mut self.next, &mut self.vm, &InputSignals::default(), &mut self.platform, ) } fn transport(&self) -> AudioTransport { self.platform.local_hardware().audio.transport() } fn step(&mut self) -> Option { self.runtime.debug_step_instruction( &mut self.log, &mut self.fs, &mut self.fs_state, &mut self.memcard, &mut self.files, &mut self.next, &mut self.vm, &mut self.platform, ) } } #[test] fn guest_audio_status_matrix_and_type_traps() { for (name, args, inputs, expected) in [ ("stop", 1, "PUSH_I32 0", 0), ("stop", 1, "PUSH_I32 15", 0), ("stop", 1, "PUSH_I32 -1", 1), ("stop", 1, "PUSH_I32 16", 1), ("set_volume", 2, "PUSH_I32 0\nPUSH_I32 0", 0), ("set_volume", 2, "PUSH_I32 0\nPUSH_I32 255", 0), ("set_volume", 2, "PUSH_I32 0\nPUSH_I32 256", 3), ("set_pan", 2, "PUSH_I32 0\nPUSH_I32 -1", 3), ("set_pan", 2, "PUSH_I32 16\nPUSH_I32 -1", 1), ("set_pan", 2, "PUSH_I32 15\nPUSH_I32 255", 0), ("set_pitch", 2, "PUSH_I32 0\nPUSH_I32 1", 0), ("set_pitch", 2, "PUSH_I32 0\nPUSH_I32 0", 3), ("set_pitch", 2, "PUSH_I32 0\nPUSH_I32 -1", 3), ( "play", 7, "PUSH_I32 256\nPUSH_I32 0\nPUSH_I32 0\nPUSH_I32 255\nPUSH_I32 0\nPUSH_I32 1\nPUSH_I32 0", 6, ), ] { let mut h = Harness::new(&format!("{inputs}\nHOSTCALL 0\nFRAME_SYNC\nHALT"), name, args); assert!(h.tick().is_none(), "{name}: {inputs}"); assert_eq!(h.vm.operand_stack_top(1), vec![Value::Int64(expected)], "{name}: {inputs}"); let batch = h.transport().try_take().unwrap().frame; assert_eq!(batch.map_or(0, |v| v.len()), usize::from(expected == 0)); } for name in ["set_volume", "set_pan", "set_pitch"] { let mut h = Harness::new("PUSH_I32 -1\nPUSH_BOOL 1\nHOSTCALL 0\nFRAME_SYNC\nHALT", name, 2); assert!(matches!(h.tick(), Some(CrashReport::VmTrap { .. }))); assert!(h.transport().try_take().unwrap().frame.is_none()); } } #[test] fn frame_publication_backpressure_abort_and_debug_steps() { let mut h = Harness::new( "PUSH_I32 0\nHOSTCALL 0\nFRAME_SYNC\nPUSH_I32 1\nHOSTCALL 0\nFRAME_SYNC\nHALT", "stop", 1, ); assert!(h.tick().is_none()); let pc = h.vm.pc(); assert!(h.tick().is_none()); assert_eq!(h.vm.pc(), pc, "pending batch stalls guest"); let batch = h.transport().try_take().unwrap().frame.unwrap(); assert!(matches!(batch[0], AudioCommand::Stop { voice_id: 0 })); assert!(h.tick().is_none()); assert!(matches!( h.transport().try_take().unwrap().frame.unwrap()[0], AudioCommand::Stop { voice_id: 1 } )); for end in ["HALT", "PUSH_BOOL 1\nHOSTCALL 0\nHALT"] { let mut h = Harness::new(&format!("PUSH_I32 0\nHOSTCALL 0\n{end}"), "stop", 1); let _ = h.tick(); h.platform.audio_mut().finish_frame(); assert!(h.transport().try_take().unwrap().frame.is_none(), "abort cancels admitted stop"); } let mut h = Harness::new("PUSH_I32 0\nHOSTCALL 0\nFRAME_SYNC\nHALT", "stop", 1); assert!(h.step().is_none()); assert!(h.step().is_none()); assert!(h.transport().try_take().unwrap().frame.is_none()); // Switching from stepping to running must preserve previously staged commands. assert!(h.tick().is_none()); assert_eq!(h.transport().try_take().unwrap().frame.unwrap().len(), 1); } #[test] fn guest_quota_is_deterministic_and_resets_on_next_frame() { let repeated = "PUSH_I32 0\nHOSTCALL 0\nPOP_N 1\n".repeat(1024); let mut h = Harness::new( &format!( "{repeated}PUSH_I32 0\nHOSTCALL 0\nFRAME_SYNC\nPUSH_I32 0\nHOSTCALL 0\nFRAME_SYNC\nHALT" ), "stop", 1, ); assert!(h.tick().is_none()); assert_eq!(h.vm.operand_stack_top(1), vec![Value::Int64(7)]); assert_eq!(h.transport().try_take().unwrap().frame.unwrap().len(), 1024); assert!(h.tick().is_none()); assert_eq!(h.vm.operand_stack_top(1), vec![Value::Int64(0)]); assert_eq!(h.transport().try_take().unwrap().frame.unwrap().len(), 1); } #[test] fn guest_play_validates_raw_bank_loop_and_preserves_both_play_forms() { use prometeu_drivers::SoundBankPoolInstaller; use prometeu_hal::{sample::Sample, sound_bank::SoundBank}; for (bank, loop_mode, expected) in [(0, 0, 0), (0, 1, 0), (0, -1, 3), (0, 2, 3), (256, 0, 6), (-256, 0, 6), (16, 0, 6)] { let mut h = Harness::new( &format!( "PUSH_I32 {bank}\nPUSH_I32 0\nPUSH_I32 0\nPUSH_I32 255\nPUSH_I32 127\nPUSH_I32 1\nPUSH_I32 {loop_mode}\nHOSTCALL 0\nFRAME_SYNC\nHALT" ), "play", 7, ); let banks = Arc::new(MemoryBanks::new()); banks.install_sound_bank( 0, Arc::new(SoundBank::new(vec![Arc::new(Sample::new(48_000, vec![1, 2, 3]))])), ); h.platform = TestPlatform::new_with_memory_banks(banks); assert!(h.tick().is_none()); assert_eq!(h.vm.operand_stack_top(1), vec![Value::Int64(expected)]); let batch = h.transport().try_take().unwrap().frame; assert_eq!(batch.map_or(0, |v| v.len()), usize::from(expected == 0)); } let mut h = Harness::new( "PUSH_I32 0\nPUSH_I32 0\nPUSH_I32 255\nPUSH_I32 127\nPUSH_I32 1\nHOSTCALL 0\nFRAME_SYNC\nHALT", "play_sample", 5, ); let banks = Arc::new(MemoryBanks::new()); banks.install_sound_bank( 0, Arc::new(SoundBank::new(vec![Arc::new(Sample::new(48_000, vec![1]))])), ); h.platform = TestPlatform::new_with_memory_banks(banks); assert!(h.tick().is_none()); assert_eq!(h.vm.operand_stack_top(1), vec![Value::Int64(0)]); assert!(matches!( h.transport().try_take().unwrap().frame.unwrap()[0], AudioCommand::Play { loop_mode: prometeu_hal::LoopMode::Off, .. } )); } #[test] fn normal_entry_return_is_a_successful_implicit_audio_frame() { let mut h = Harness::new("PUSH_I32 0\nHOSTCALL 0\nPOP_N 1\nRET", "stop", 1); for _ in 0..2 { assert!(h.tick().is_none()); assert_eq!(h.transport().try_take().unwrap().frame.unwrap().len(), 1); } let mut h = Harness::new("PUSH_I32 0\nHOSTCALL 0\nPOP_N 1\nRET", "stop", 1); for _ in 0..4 { assert!(h.step().is_none()); } assert_eq!(h.transport().try_take().unwrap().frame.unwrap().len(), 1); }