use super::{
decode_double_buffer_samples, decode_mace3_mono_to_u8, decode_mace6_mono_to_u8,
extended80_to_f64, parse_aiff_samples, synth_sys_beep_samples,
GUEST_SND_CHANNEL_CMD_IN_PROGRESS_OFFSET, GUEST_SND_CHANNEL_Q_HEAD_OFFSET,
GUEST_SND_CHANNEL_Q_LENGTH_OFFSET, GUEST_SND_CHANNEL_Q_TAIL_OFFSET, GUEST_SND_CHANNEL_SIZE,
MEM_FULL_ERR, SAMPLED_SYNTH_ID, SOUND_MANAGER_3_SYNTH_VERSION,
};
use crate::cpu::{CpuOps, Register};
use crate::managers::resource::ResourceFork;
use crate::memory::{MacMemoryBus, MemoryBus};
use crate::sound::{
cmd, PendingDoubleBackCallback, PendingSoundCallback, PlaybackKind, SndChannel, SndCommand,
StereoSample,
};
use crate::trap::test_helpers::{setup, TEST_SP};
#[test]
fn sound_dispatch_generated_routes_preserve_exact_long_values() {
assert_eq!(super::SOUND_DISPATCH_OPERATION_ROUTES.len(), 54);
assert!(super::SOUND_DISPATCH_OPERATION_ROUTES
.windows(2)
.all(|pair| pair[0].selector < pair[1].selector));
for (selector, routine_name) in [
(0x0000_000C, "SpeechManagerVersion"),
(0x0000_0010, "MACEVersion"),
(0x0000_0014, "SPBVersion"),
(0x0004_0010, "Comp3to1"),
(0x0008_0010, "Exp1to3"),
(0x000C_0008, "SndSoundManagerVersion"),
(0x000C_0010, "Comp6to1"),
(0x0010_0010, "Exp1to6"),
(0x003C_000C, "SpeechBusy"),
(0x0040_000C, "SpeechBusySystemWide"),
(0x0108_000C, "CountVoices"),
(0x0110_0014, "SPBSignOutDevice"),
(0x0204_0008, "SndPauseFilePlay"),
(0x021C_000C, "DisposeSpeechChannel"),
(0x021C_0014, "SPBCloseDevice"),
(0x0220_000C, "SpeakString"),
(0x0228_0014, "SPBPauseRecording"),
(0x022C_000C, "StopSpeech"),
(0x022C_0014, "SPBResumeRecording"),
(0x0230_0014, "SPBStopRecording"),
(0x0238_000C, "ContinueSpeech"),
(0x0308_0008, "SndStopFilePlay"),
(0x030C_000C, "GetIndVoice"),
(0x030C_0014, "SPBSignInDevice"),
(0x0320_0014, "SPBRecord"),
(0x0418_000C, "NewSpeechChannel"),
(0x0424_0014, "SPBRecordToFile"),
(0x0430_000C, "StopSpeechAt"),
(0x0434_000C, "PauseSpeechAt"),
(0x0440_0014, "SPBMillisecondsToBytes"),
(0x0444_000C, "SetSpeechRate"),
(0x0444_0014, "SPBBytesToMilliseconds"),
(0x0448_000C, "GetSpeechRate"),
(0x044C_000C, "SetSpeechPitch"),
(0x0450_000C, "GetSpeechPitch"),
(0x0460_000C, "UseDictionary"),
(0x0514_0014, "SPBGetIndexedDevice"),
(0x0518_0014, "SPBOpenDevice"),
(0x0604_000C, "MakeVoiceSpec"),
(0x0610_000C, "GetVoiceDescription"),
(0x0614_000C, "GetVoiceInfo"),
(0x0624_000C, "SpeakText"),
(0x0638_0014, "SPBGetDeviceInfo"),
(0x063C_0014, "SPBSetDeviceInfo"),
(0x0654_000C, "SetSpeechInfo"),
(0x0658_000C, "GetSpeechInfo"),
(0x0708_0014, "SndRecordToFile"),
(0x0804_0014, "SndRecord"),
(0x0828_000C, "SpeakBuffer"),
(0x0A5C_000C, "TextToPhonemes"),
(0x0B4C_0014, "SetupAIFFHeader"),
(0x0D00_0008, "SndStartFilePlay"),
(0x0D48_0014, "SetupSndHeader"),
(0x0E34_0014, "SPBGetRecordingStatus"),
] {
let route =
super::sound_dispatch_operation_route(0xA800, selector).expect("SoundDispatch route");
assert_eq!(route.routine_name, routine_name);
}
let midi_stop_time_route = super::sound_dispatch_operation_route(0xA800, 0x0064_0004)
.expect("SoundDispatch MIDIStopTime route");
assert_eq!(midi_stop_time_route.routine_name, "MIDIStopTime");
assert_eq!(
midi_stop_time_route.operation_id,
"selector-operation:_SoundDispatch:0x00640004:d0-long-immediate:32"
);
for (trap_word, selector) in [
(0xA900, 0x003C_000C),
(0xA800, 0x003D_000C),
(0xA800, 0x0010_0008),
(0xA900, 0x0064_0004),
(0xA800, 0x0060_0004),
(0xA800, 0x0068_0004),
(0xA800, 0x0064_0008),
(0xA800, 0x0065_0004),
(0xA800, 0x060C_0018),
(0xA800, 0x0000_203C),
] {
assert!(super::sound_dispatch_operation_route(trap_word, selector).is_none());
}
}
#[test]
fn sound_dispatch_records_exact_identity_and_rejects_wrong_routine_value() {
let (mut disp, mut cpu, mut bus) = setup();
disp.current_trap_word = 0xA800;
cpu.write_reg(Register::D0, 0x003C_000C);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.expect("SoundDispatch arm").is_ok());
assert_eq!(
disp.current_selector_operation,
Some(super::SOUND_DISPATCH_OPERATION_ROUTES[8].operation_id)
);
let sp = TEST_SP + 0x80;
bus.write_word(sp, 0x003B);
bus.write_long(sp + 2, 0x003B_2E3C);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0064_0004);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.expect("SoundDispatch arm").is_ok());
assert_eq!(
disp.current_selector_operation,
Some(super::MIDI_STOP_TIME_OPERATION_ROUTE.operation_id)
);
cpu.write_reg(Register::A7, TEST_SP);
cpu.write_reg(Register::D0, 0x003D_000C);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.expect("SoundDispatch arm").is_ok());
assert_eq!(disp.current_selector_operation, None);
}
fn build_aiff(
form_kind: &[u8; 4],
channels: u16,
sample_size_bits: u16,
sample_rate_extended80: [u8; 10],
ssnd_samples: &[u8],
comm_compression: Option<&[u8; 4]>,
) -> Vec<u8> {
let mut comm = Vec::new();
comm.extend_from_slice(&channels.to_be_bytes());
let num_frames =
(ssnd_samples.len() / (channels as usize * (sample_size_bits as usize / 8))) as u32;
comm.extend_from_slice(&num_frames.to_be_bytes());
comm.extend_from_slice(&sample_size_bits.to_be_bytes());
comm.extend_from_slice(&sample_rate_extended80);
if let Some(cc) = comm_compression {
comm.extend_from_slice(cc);
}
let mut ssnd = Vec::new();
ssnd.extend_from_slice(&0u32.to_be_bytes());
ssnd.extend_from_slice(&0u32.to_be_bytes());
ssnd.extend_from_slice(ssnd_samples);
let mut out = Vec::new();
out.extend_from_slice(b"FORM");
let form_size = 4 + (8 + comm.len()) + (8 + ssnd.len());
out.extend_from_slice(&(form_size as u32).to_be_bytes());
out.extend_from_slice(form_kind);
out.extend_from_slice(b"COMM");
out.extend_from_slice(&(comm.len() as u32).to_be_bytes());
out.extend_from_slice(&comm);
out.extend_from_slice(b"SSND");
out.extend_from_slice(&(ssnd.len() as u32).to_be_bytes());
out.extend_from_slice(&ssnd);
out
}
fn write_minimal_format2_snd_handle(
bus: &mut MacMemoryBus,
snd_handle: u32,
snd_ptr: u32,
format_word: u16,
) {
bus.write_long(snd_handle, snd_ptr);
bus.write_word(snd_ptr, format_word); bus.write_word(snd_ptr + 2, 0); bus.write_word(snd_ptr + 4, 0); }
fn alloc_minimal_format2_snd_handle(
bus: &mut MacMemoryBus,
format_word: u16,
data_size: u32,
) -> (u32, u32) {
let snd_handle = bus.alloc(4);
let snd_ptr = bus.alloc(data_size);
assert_ne!(snd_handle, 0, "sound handle allocation must succeed");
assert_ne!(snd_ptr, 0, "sound data allocation must succeed");
write_minimal_format2_snd_handle(bus, snd_handle, snd_ptr, format_word);
(snd_handle, snd_ptr)
}
#[test]
fn legacy_sound_driver_decodes_supported_synths() {
let (mut disp, _cpu, mut bus) = setup();
bus.write_byte(crate::memory::globals::addr::SD_VOLUME, 7);
let free_form = 0x2F0000;
bus.write_word(free_form, 0);
bus.write_long(free_form + 2, 0x0001_0000);
bus.write_bytes(free_form + 6, &[0x20, 0xE0, 0x20, 0xE0]);
assert_eq!(
disp.write_device_driver(&mut bus, -4, free_form, 10),
Ok(10)
);
let chan = disp
.legacy_sound_driver_channel
.and_then(|ptr| disp.sound_manager.find_channel(ptr))
.expect("legacy sound driver channel allocated");
assert_eq!(
chan.playback_sample_rate(),
Some(crate::sound::RATE_22KHZ_FIXED)
);
let free_form_mix = disp.sound_manager.mix_frame(4);
assert!(free_form_mix.iter().any(|sample| *sample != 0x80));
let free_form_frac = 0x2F1000;
bus.write_word(free_form_frac, 0);
bus.write_long(free_form_frac + 2, 0x0000_2AAA);
bus.write_bytes(free_form_frac + 6, &[0x20, 0xE0, 0x20, 0xE0]);
assert_eq!(
disp.write_device_driver(&mut bus, -4, free_form_frac, 10),
Ok(10)
);
let chan_frac = disp
.legacy_sound_driver_channel
.and_then(|ptr| disp.sound_manager.find_channel(ptr))
.expect("legacy sound driver channel allocated");
let expected_frac_rate = ((u64::from(crate::sound::RATE_22KHZ_FIXED) * 0x2AAA) >> 16) as u32;
assert_eq!(chan_frac.playback_sample_rate(), Some(expected_frac_rate));
let square = 0x300000;
bus.write_word(square, (-1i16) as u16);
bus.write_word(square + 2, 14_243); bus.write_word(square + 4, 255); bus.write_word(square + 6, 1); bus.write_word(square + 8, 0); bus.write_word(square + 10, 0);
bus.write_word(square + 12, 0);
assert_eq!(disp.write_device_driver(&mut bus, -4, square, 14), Ok(14));
let square_mix = disp.sound_manager.mix_frame(128);
assert!(square_mix.iter().any(|sample| *sample != 0x80));
let synth = 0x310000;
let record = 0x311000;
let wave = 0x312000;
bus.write_word(synth, 1);
bus.write_long(synth + 2, record);
bus.write_word(record, 1); bus.write_long(record + 2, 0x0001_0000); bus.write_long(record + 6, 0); for voice in 1..4u32 {
bus.write_long(record + 2 + voice * 8, 0);
bus.write_long(record + 6 + voice * 8, 0);
}
bus.write_long(record + 34, wave);
bus.write_long(record + 38, 0);
bus.write_long(record + 42, 0);
bus.write_long(record + 46, 0);
for offset in 0..256u32 {
bus.write_byte(wave + offset, 0xE0);
}
assert_eq!(disp.write_device_driver(&mut bus, -4, synth, 6), Ok(6));
let four_tone_mix = disp.sound_manager.mix_frame(128);
assert!(four_tone_mix.iter().any(|sample| *sample != 0x80));
assert_eq!(disp.sound_manager.channels.len(), 1);
}
#[test]
fn square_wave_synth_rejects_oversized_decoded_output() {
let (mut disp, _cpu, mut bus) = setup();
let square = 0x300000;
bus.write_word(square, (-1i16) as u16);
for index in 0..2u32 {
let tone = square + 2 + index * 6;
bus.write_word(tone, 14_243);
bus.write_word(tone + 2, 255);
bus.write_word(tone + 4, u16::MAX);
}
assert_eq!(
disp.write_device_driver(&mut bus, -4, square, 14),
Err(MEM_FULL_ERR)
);
assert!(disp.sound_manager.channels.is_empty());
assert_eq!(disp.legacy_sound_driver_channel, None);
}
#[test]
fn four_tone_phase_is_a_waveform_byte_offset() {
let (_disp, _cpu, mut bus) = setup();
let synth = 0x310000;
let record = 0x311000;
let wave = 0x312000;
bus.write_word(synth, 1);
bus.write_long(synth + 2, record);
bus.write_word(record, 1);
bus.write_long(record + 2, 0);
bus.write_long(record + 6, 1);
for voice in 1..4u32 {
bus.write_long(record + 2 + voice * 8, 0);
bus.write_long(record + 6 + voice * 8, 0);
}
bus.write_long(record + 34, wave);
bus.write_long(record + 38, 0);
bus.write_long(record + 42, 0);
bus.write_long(record + 46, 0);
bus.write_byte(wave, 0x20);
bus.write_byte(wave + 1, 0xE0);
let (samples, _) =
super::super::TrapDispatcher::decode_four_tone_synth(&bus, synth, 6).unwrap();
assert_eq!(samples[0], 0xE0);
}
#[test]
fn parse_aiff_samples_decodes_canonical_files() {
let one_hz = [0x3F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let aiff = build_aiff(b"AIFF", 1, 8, one_hz, &[0x40, 0xC0], None);
let (samples, rate) = parse_aiff_samples(&aiff).expect("valid AIFF");
assert_eq!(samples, vec![0xC0, 0x40]);
assert_eq!(rate, 0x0001_0000, "1.0 Hz → unity 16.16 fixed");
let frame_16bit_stereo: [u8; 4] = [0x40, 0x00, 0x40, 0x00];
let aiff = build_aiff(b"AIFF", 2, 16, one_hz, &frame_16bit_stereo, None);
let (samples, rate) = parse_aiff_samples(&aiff).expect("valid stereo AIFF");
assert_eq!(samples, vec![0xC0]);
assert_eq!(rate, 0x0001_0000);
let aiff = build_aiff(b"AIFC", 1, 8, one_hz, &[0x40, 0xC0], Some(b"NONE"));
let (samples, _) = parse_aiff_samples(&aiff).expect("AIFC-NONE accepted");
assert_eq!(samples, vec![0xC0, 0x40]);
let aiff = build_aiff(b"AIFC", 1, 8, one_hz, &[0x40, 0xC0], Some(b"ima4"));
assert!(
parse_aiff_samples(&aiff).is_none(),
"AIFC with unsupported compression must be rejected"
);
let aiff = build_aiff(b"AIFL", 1, 8, one_hz, &[0x40], None);
assert!(
parse_aiff_samples(&aiff).is_none(),
"non-AIFF/AIFC FORM kind must be rejected"
);
assert!(parse_aiff_samples(b"FOR").is_none());
}
#[test]
fn parse_aiff_samples_ignores_bytes_after_declared_form() {
let one_hz = [0x3F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let mut aiff = build_aiff(b"AIFF", 1, 8, one_hz, &[0x40, 0xC0], None);
aiff.extend_from_slice(&[0xFF; 8]);
let (samples, rate) = parse_aiff_samples(&aiff).expect("valid FORM with trailing bytes");
assert_eq!(samples, vec![0xC0, 0x40]);
assert_eq!(rate, 0x0001_0000);
}
#[test]
fn parse_aiff_samples_accepts_truncated_final_sound_data_chunk() {
let one_hz = [0x3F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let mut aiff = build_aiff(b"AIFF", 2, 8, one_hz, &[0x40, 0x40, 0xC0, 0xC0], None);
aiff.truncate(aiff.len() - 2);
let (samples, rate) = parse_aiff_samples(&aiff).expect("truncated final SSND accepted");
assert_eq!(samples, vec![0xC0]);
assert_eq!(rate, 0x0001_0000);
}
#[test]
fn parse_aiff_samples_rejects_overflowing_chunk_bounds() {
let mut huge_chunk = Vec::new();
huge_chunk.extend_from_slice(b"FORM");
huge_chunk.extend_from_slice(&12u32.to_be_bytes());
huge_chunk.extend_from_slice(b"AIFF");
huge_chunk.extend_from_slice(b"JUNK");
huge_chunk.extend_from_slice(&u32::MAX.to_be_bytes());
assert!(parse_aiff_samples(&huge_chunk).is_none());
let one_hz = [0x3F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let mut ssnd_overflow = Vec::new();
ssnd_overflow.extend_from_slice(b"FORM");
ssnd_overflow.extend_from_slice(&42u32.to_be_bytes());
ssnd_overflow.extend_from_slice(b"AIFF");
ssnd_overflow.extend_from_slice(b"COMM");
ssnd_overflow.extend_from_slice(&18u32.to_be_bytes());
ssnd_overflow.extend_from_slice(&1u16.to_be_bytes());
ssnd_overflow.extend_from_slice(&0u32.to_be_bytes());
ssnd_overflow.extend_from_slice(&8u16.to_be_bytes());
ssnd_overflow.extend_from_slice(&one_hz);
ssnd_overflow.extend_from_slice(b"SSND");
ssnd_overflow.extend_from_slice(&8u32.to_be_bytes());
ssnd_overflow.extend_from_slice(&u32::MAX.to_be_bytes());
ssnd_overflow.extend_from_slice(&0u32.to_be_bytes());
assert!(parse_aiff_samples(&ssnd_overflow).is_none());
}
#[test]
fn sndstartfileplay_loads_non_preloaded_sound_resource() {
let (mut disp, _cpu, mut bus) = setup();
let fork = ResourceFork::from_test_resources(vec![(
*b"snd ",
30_000,
vec![0x00, 0x02, 0x00, 0x00, 0x00, 0x00],
)]);
disp.load_resources(&fork, &mut bus);
assert!(disp.find_loaded_resource_any(*b"snd ", 30_000).is_none());
let chan_ptr = 0x250000;
disp.sound_manager
.add_channel(SndChannel::new(chan_ptr, false));
assert_eq!(
disp.snd_start_file_play(&mut bus, chan_ptr, 0, 30_000, 0, 0, 1),
0
);
assert!(disp.find_loaded_resource_any(*b"snd ", 30_000).is_some());
assert_eq!(disp.sound_manager.debug_file_play_count, 1);
}
#[test]
fn extended80_to_f64_parses_canonical_values() {
let zero = [0u8; 10];
assert_eq!(extended80_to_f64(&zero), Some(0.0));
let one = [0x3F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
assert_eq!(extended80_to_f64(&one), Some(1.0));
let two = [0x40, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
assert_eq!(extended80_to_f64(&two), Some(2.0));
let neg_one = [0xBF, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
assert_eq!(extended80_to_f64(&neg_one), Some(-1.0));
let half = [0x3F, 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
assert_eq!(extended80_to_f64(&half), Some(0.5));
let short = [0u8; 9];
assert_eq!(extended80_to_f64(&short), None);
}
#[test]
fn sndplay_loaded_format2_resource_returns_noerr() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let snd_handle = 0x210000;
let snd_ptr = 0x210100;
write_minimal_format2_snd_handle(&mut bus, snd_handle, snd_ptr, 2);
let channel_count_before = disp.sound_manager.channels.len();
bus.write_word(sp, 0); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0); bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(disp.sound_manager.channels.len(), channel_count_before);
}
#[test]
fn sndplay_format2_refcount_does_not_shift_command_stream() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let chan_ptr = 0x250000;
disp.sound_manager
.add_channel(SndChannel::new(chan_ptr, false));
let (snd_handle, snd_ptr) = alloc_minimal_format2_snd_handle(&mut bus, 2, 80);
let header_offset = 16u32;
let header = snd_ptr + header_offset;
bus.write_word(snd_ptr, 2); bus.write_word(snd_ptr + 2, 1); bus.write_word(snd_ptr + 4, 1); bus.write_word(snd_ptr + 6, 0x8000 | cmd::BUFFER); bus.write_word(snd_ptr + 8, 0); bus.write_long(snd_ptr + 10, header_offset);
bus.write_long(header, 0); bus.write_long(header + 4, 4); bus.write_long(header + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_long(header + 12, 0); bus.write_long(header + 16, 0); bus.write_byte(header + 20, 0); bus.write_byte(header + 21, 60); bus.write_bytes(header + 22, &[0x40, 0x80, 0xC0, 0xFF]);
bus.write_word(sp, 0); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
assert!(disp
.sound_manager
.find_channel(chan_ptr)
.expect("channel exists")
.is_playing());
assert_eq!(
disp.sound_manager.mix_frame(4),
vec![0x40, 0x80, 0xC0, 0xFF]
);
}
#[test]
fn sndplay_resolves_legacy_format2_sampled_voice_header() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let chan_ptr = 0x250200;
disp.sound_manager
.add_channel(SndChannel::new(chan_ptr, false));
let (snd_handle, snd_ptr) = alloc_minimal_format2_snd_handle(&mut bus, 2, 80);
let compact = snd_ptr + 14;
bus.write_word(snd_ptr + 4, 1);
bus.write_word(snd_ptr + 6, 0x8000 | cmd::SOUND);
bus.write_word(snd_ptr + 8, 0);
bus.write_long(snd_ptr + 10, 20); bus.write_long(compact, 0); bus.write_long(compact + 4, 4);
bus.write_long(compact + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_long(compact + 12, 0);
bus.write_long(compact + 16, 0);
bus.write_byte(compact + 20, 0);
bus.write_byte(compact + 21, 60);
bus.write_bytes(compact + 22, &[0x40, 0x80, 0xC0, 0xFF]);
bus.write_word(sp, 1);
bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(
disp.sound_manager.mix_frame(4),
vec![0x40, 0x80, 0xC0, 0xFF]
);
}
#[test]
fn sndplay_async_busy_channel_queues_buffer_resource_until_current_playback_finishes() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let chan_ptr = 0x250400;
disp.sound_manager
.add_channel(SndChannel::new(chan_ptr, false));
bus.write_word(chan_ptr + GUEST_SND_CHANNEL_Q_LENGTH_OFFSET, 128);
bus.write_word(chan_ptr + GUEST_SND_CHANNEL_Q_HEAD_OFFSET, 0);
bus.write_word(chan_ptr + GUEST_SND_CHANNEL_Q_TAIL_OFFSET, 0);
disp.sound_manager
.with_channel_mut(chan_ptr, |channel| {
channel.play_buffer(
vec![0x10, 0x11],
crate::sound::OUTPUT_RATE << 16,
PlaybackKind::Buffer,
0,
);
})
.expect("channel exists");
let (snd_handle, snd_ptr) = alloc_minimal_format2_snd_handle(&mut bus, 2, 80);
let header_offset = 16u32;
let header = snd_ptr + header_offset;
bus.write_word(snd_ptr + 4, 1); bus.write_word(snd_ptr + 6, 0x8000 | cmd::BUFFER); bus.write_word(snd_ptr + 8, 0);
bus.write_long(snd_ptr + 10, header_offset);
bus.write_long(header, 0);
bus.write_long(header + 4, 2);
bus.write_long(header + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_long(header + 12, 0);
bus.write_long(header + 16, 0);
bus.write_byte(header + 20, 0);
bus.write_byte(header + 21, 60);
bus.write_bytes(header + 22, &[0xA0, 0xA1]);
bus.write_word(sp, 1); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(
bus.read_word(chan_ptr + GUEST_SND_CHANNEL_Q_TAIL_OFFSET),
1,
"resource command should enter the channel FIFO"
);
assert_eq!(
disp.sound_manager.mix_frame(2),
vec![0x10, 0x11],
"current buffer must finish before queued SndPlay data starts"
);
disp.service_guest_sound_queues(&mut bus);
assert_eq!(disp.sound_manager.mix_frame(2), vec![0xA0, 0xA1]);
}
#[test]
fn sndplay_nil_chan_async_true_is_accepted() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let snd_handle = 0x210200;
let snd_ptr = 0x210300;
write_minimal_format2_snd_handle(&mut bus, snd_handle, snd_ptr, 2);
bus.write_word(sp, 1); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0); bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
}
#[test]
fn sndplay_nil_chan_sync_path_reclaims_internal_channel_before_return() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let snd_handle = 0x210350;
let snd_ptr = 0x210450;
let channel_count_before = disp.sound_manager.channels.len();
write_minimal_format2_snd_handle(&mut bus, snd_handle, snd_ptr, 2);
bus.write_word(sp, 1); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0); bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(disp.sound_manager.channels.len(), channel_count_before);
}
#[test]
fn sndplay_nil_chan_sync_path_returns_internal_guest_channel_allocation_to_free_list() {
let (mut disp, mut cpu, mut bus) = setup();
let recycled = bus.alloc(GUEST_SND_CHANNEL_SIZE);
assert_ne!(recycled, 0);
bus.write_long(recycled, 0x1111_0001);
bus.write_long(recycled + 4, 0x1111_0002);
bus.write_long(recycled + 8, 0x1111_0003);
bus.write_long(recycled + 12, 0x1111_0004);
bus.write_long(recycled + 16, 0x1111_0005);
bus.write_long(recycled + 20, 0x1111_0006);
bus.write_long(recycled + 24, 0x1111_0007);
bus.write_word(recycled + 28, 0x1111);
bus.write_word(recycled + 30, 0x2222);
bus.write_word(recycled + 32, 0x3333);
bus.write_word(recycled + 34, 0x4444);
bus.free(recycled);
let sp = TEST_SP;
let snd_handle = 0x210700;
let snd_ptr = 0x210800;
write_minimal_format2_snd_handle(&mut bus, snd_handle, snd_ptr, 2);
bus.write_word(sp, 0); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0); bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
let reallocated = bus.alloc(GUEST_SND_CHANNEL_SIZE);
assert_eq!(
reallocated, recycled,
"internal NIL-chan SndPlay must return its guest channel block to the allocator"
);
assert_eq!(bus.read_long(reallocated), 0, "nextChan cleared");
assert_eq!(bus.read_long(reallocated + 4), 0, "firstMod cleared");
assert_eq!(bus.read_long(reallocated + 8), 0, "callBack cleared");
assert_eq!(bus.read_long(reallocated + 12), 0, "userInfo cleared");
assert_eq!(bus.read_long(reallocated + 16), 0, "wait cleared");
assert_eq!(
bus.read_long(reallocated + 20),
0,
"cmdInProgress cmd/param1 cleared"
);
assert_eq!(
bus.read_long(reallocated + 24),
0,
"cmdInProgress param2 cleared"
);
assert_eq!(bus.read_word(reallocated + 28), 0, "flags cleared");
assert_eq!(bus.read_word(reallocated + 30), 0, "qLength cleared");
assert_eq!(bus.read_word(reallocated + 32), 0, "qHead cleared");
assert_eq!(bus.read_word(reallocated + 34), 0, "qTail cleared");
bus.free(reallocated);
}
#[test]
fn sndplay_nil_chan_active_playback_survives_until_mixed_then_releases() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let (snd_handle, snd_ptr) = alloc_minimal_format2_snd_handle(&mut bus, 2, 80);
let header_offset = 16u32;
let header = snd_ptr + header_offset;
bus.write_word(snd_ptr + 4, 1); bus.write_word(snd_ptr + 6, 0x8000 | cmd::BUFFER); bus.write_word(snd_ptr + 8, 0);
bus.write_long(snd_ptr + 10, header_offset);
bus.write_long(header, 0);
bus.write_long(header + 4, 4);
bus.write_long(header + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_long(header + 12, 0);
bus.write_long(header + 16, 0);
bus.write_byte(header + 20, 0);
bus.write_byte(header + 21, 60);
bus.write_bytes(header + 22, &[0x40, 0x80, 0xC0, 0xFF]);
let channel_count_before = disp.sound_manager.channels.len();
bus.write_word(sp, 0);
bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(disp.sound_manager.channels.len(), channel_count_before + 1);
assert_eq!(
disp.sound_manager.mix_frame(4),
vec![0x40, 0x80, 0xC0, 0xFF]
);
disp.release_finished_internal_sound_channels(&mut bus);
assert_eq!(disp.sound_manager.channels.len(), channel_count_before);
}
#[test]
fn sndplay_unloaded_handle_returns_resproblem() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let snd_handle = 0x210400;
bus.write_long(snd_handle, 0);
bus.write_word(sp, 0); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10) as i16, -204);
}
#[test]
fn sndplay_nil_handle_returns_resproblem() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
bus.write_word(sp, 0); bus.write_long(sp + 2, 0); bus.write_long(sp + 6, 0);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10) as i16, -204);
}
#[test]
fn sndplay_bad_format_returns_badformat() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let snd_handle = 0x210500;
let snd_ptr = 0x210600;
write_minimal_format2_snd_handle(&mut bus, snd_handle, snd_ptr, 3);
bus.write_word(sp, 0); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0x220000);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10) as i16, -206);
}
#[test]
fn sndplay_truncated_format2_handle_returns_badformat() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let (snd_handle, _snd_ptr) = alloc_minimal_format2_snd_handle(&mut bus, 2, 4);
bus.write_word(sp, 0); bus.write_long(sp + 2, snd_handle);
bus.write_long(sp + 6, 0);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x005, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10) as i16, -206);
}
#[test]
fn sysbeep_consumes_duration_word_and_queues_beep_audio() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 30); bus.write_word(sp + 2, 0xBEEF); let channel_count_before = disp.sound_manager.channels.len();
let result = disp.dispatch_sound(true, 0x1C8, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 2);
assert_eq!(bus.read_word(sp + 2), 0xBEEF);
assert_eq!(disp.sound_manager.channels.len(), channel_count_before + 1);
assert_eq!(
disp.sound_manager
.mix_frame(256)
.iter()
.filter(|&&sample| sample != 0x80)
.count(),
256
);
let remaining = synth_sys_beep_samples().len();
let _ = disp.sound_manager.mix_frame(remaining);
disp.release_finished_internal_sound_channels(&mut bus);
assert_eq!(disp.sound_manager.channels.len(), channel_count_before);
}
#[test]
fn sounddispatch_reports_sound_manager_3x_final_numversion() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x000C_0008); bus.write_long(sp, 0xDEAD_BEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(
bus.read_long(sp),
0x0333_8000,
"NumVersion bytes must encode Sound Manager 3.3.3 final"
);
assert_eq!(
bus.read_byte(sp + 1),
0x33,
"minorAndBugRev must be BCD-style 3.3, not raw decimal bytes"
);
assert_eq!(
bus.read_byte(sp + 2),
0x80,
"release stage byte must mark a final release"
);
}
#[test]
fn sounddispatch_unsigned_fixed_mul_div_returns_result_and_pops_arguments() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x060C_0018); bus.write_long(sp, 0x5622_0000); bus.write_long(sp + 4, 0x5622_0000); bus.write_long(sp + 8, 0x0001_0000); bus.write_long(sp + 12, 0xDEAD_BEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 12);
assert_eq!(bus.read_long(sp + 12), 0x0001_0000);
}
#[test]
fn setup_snd_header_builds_standard_format_one_resource() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let handle = 0x230800;
let resource = 0x230900;
let header_len = 0x230880;
bus.write_long(handle, resource);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0D48_0014);
bus.write_long(sp, header_len);
bus.write_long(sp + 4, 0x2000);
bus.write_word(sp + 8, 0x43);
bus.write_long(sp + 10, u32::from_be_bytes(*b"NONE"));
bus.write_word(sp + 14, 8);
bus.write_long(sp + 16, 0x5622_0000);
bus.write_word(sp + 20, 1);
bus.write_long(sp + 22, handle);
bus.write_word(sp + 26, 0xBEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 26);
assert_eq!(bus.read_word(sp + 26), 0);
assert_eq!(bus.read_word(header_len), 42);
assert_eq!(bus.read_word(resource), 1);
assert_eq!(bus.read_word(resource + 2), 1);
assert_eq!(bus.read_word(resource + 4), 5);
assert_eq!(bus.read_long(resource + 6), 0x80);
assert_eq!(bus.read_word(resource + 10), 1);
assert_eq!(bus.read_word(resource + 12), 0x8051);
assert_eq!(bus.read_long(resource + 16), 20);
assert_eq!(bus.read_long(resource + 24), 0x2000);
assert_eq!(bus.read_long(resource + 28), 0x5622_0000);
assert_eq!(bus.read_byte(resource + 40), 0);
assert_eq!(bus.read_byte(resource + 41), 0x43);
}
#[test]
fn setup_snd_header_builds_extended_and_mace_headers() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let handle = 0x230A00;
let resource = 0x230B00;
let header_len = 0x230A80;
bus.write_long(handle, resource);
let mut invoke = |compression: u32, channels: u16, sample_size: u16, num_bytes: u32| {
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0D48_0014);
bus.write_long(sp, header_len);
bus.write_long(sp + 4, num_bytes);
bus.write_word(sp + 8, 60);
bus.write_long(sp + 10, compression);
bus.write_word(sp + 14, sample_size);
bus.write_long(sp + 16, 0x5622_0000);
bus.write_word(sp + 20, channels);
bus.write_long(sp + 22, handle);
bus.write_word(sp + 26, 0xBEEF);
disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus)
.unwrap()
.unwrap();
(
bus.read_word(header_len),
bus.read_long(resource + 6),
bus.read_byte(resource + 40),
bus.read_long(resource + 42),
bus.read_long(resource + 60),
bus.read_word(resource + 68),
bus.read_word(resource + 76),
bus.read_word(resource + 78),
)
};
let extended = invoke(u32::from_be_bytes(*b"NONE"), 2, 16, 0x2000);
assert_eq!(extended.0, 84);
assert_eq!(extended.1, 0xC0);
assert_eq!(extended.2, 0xFF);
assert_eq!(extended.3, 0x800);
assert_eq!(extended.5, 16);
let mace3 = invoke(u32::from_be_bytes(*b"MAC3"), 1, 8, 1000);
assert_eq!(mace3.0, 84);
assert_eq!(mace3.1, 0x380);
assert_eq!(mace3.2, 0xFE);
assert_eq!(mace3.3, 3000);
assert_eq!(mace3.4, u32::from_be_bytes(*b"MAC3"));
assert_eq!(mace3.6, 0xFFFF);
assert_eq!(mace3.7, 16);
let mace6 = invoke(u32::from_be_bytes(*b"MAC6"), 1, 8, 1000);
assert_eq!(mace6.1, 0x480);
assert_eq!(mace6.3, 6000);
assert_eq!(mace6.7, 8);
}
#[test]
fn setup_snd_header_rejects_unknown_compression_without_outputs() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let handle = 0x230C00;
let resource = 0x230D00;
let header_len = 0x230C80;
bus.write_long(handle, resource);
bus.write_long(resource, 0xDEAD_BEEF);
bus.write_word(header_len, 0xCAFE);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0D48_0014);
bus.write_long(sp, header_len);
bus.write_long(sp + 4, 1000);
bus.write_word(sp + 8, 60);
bus.write_long(sp + 10, u32::from_be_bytes(*b"BAD!"));
bus.write_word(sp + 14, 8);
bus.write_long(sp + 16, 0x5622_0000);
bus.write_word(sp + 20, 1);
bus.write_long(sp + 22, handle);
disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus)
.unwrap()
.unwrap();
assert_eq!(cpu.read_reg(Register::A7), sp + 26);
assert_eq!(bus.read_word(sp + 26) as i16, -223);
assert_eq!(bus.read_word(header_len), 0xCAFE);
assert_eq!(bus.read_long(resource), 0xDEAD_BEEF);
}
#[test]
fn sounddispatch_volume_selectors_persist_and_return_levels() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let level_ptr = 0x230800;
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x022C_0024); bus.write_long(sp, level_ptr);
bus.write_word(sp + 4, 0xBEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 4);
assert_eq!(bus.read_word(sp + 4), 0);
assert_eq!(bus.read_long(level_ptr), 0x0100_0100);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0230_0024); bus.write_long(sp, 0x0040_00C0);
bus.write_word(sp + 4, 0xBEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 4);
assert_eq!(bus.read_word(sp + 4), 0);
assert_eq!(disp.sound_manager.default_output_volume(), 0x0040_00C0);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x022C_0024); bus.write_long(sp, level_ptr);
bus.write_long(level_ptr, 0);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_long(level_ptr), 0x0040_00C0);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0228_0024); bus.write_long(sp, 0x0020_0060);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(disp.sound_manager.sys_beep_volume(), 0x0020_0060);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0224_0024); bus.write_long(sp, level_ptr);
bus.write_long(level_ptr, 0);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_long(level_ptr), 0x0020_0060);
}
#[test]
fn sounddispatch_sndmanagerstatus_reports_default_cpu_load_capacity() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let status_ptr = 0x230A00;
disp.sound_manager
.add_channel(SndChannel::new(0x0039_38C8, false));
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0314_0008); bus.write_long(sp, status_ptr);
bus.write_word(sp + 4, 6); bus.write_word(sp + 6, 0xBEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert_eq!(bus.read_word(status_ptr), 100);
assert_eq!(bus.read_word(status_ptr + 2), 1);
assert_eq!(bus.read_word(status_ptr + 4), 0);
}
#[test]
fn sounddispatch_documented_zero_param_selectors_pop_real_pascal_frames() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let sc_status_ptr = 0x230A80;
let sm_status_ptr = 0x230AA0;
let state_ptr = 0x230AC0;
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0010_0008); bus.write_long(sp, sc_status_ptr);
bus.write_word(sp + 4, 24);
bus.write_long(sp + 6, 0);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0014_0008); bus.write_long(sp, sm_status_ptr);
bus.write_word(sp + 4, 6);
bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0018_0008); bus.write_long(sp, state_ptr);
bus.write_word(sp + 4, 0xFFFF);
bus.write_word(state_ptr, 0);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 4);
assert_eq!(bus.read_word(sp + 4), 0);
assert_eq!(bus.read_word(state_ptr), 1);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x001C_0008); bus.write_word(sp, 0);
bus.write_word(sp + 2, 0xFFFF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 2);
assert_eq!(bus.read_word(sp + 2), 0);
}
#[test]
fn sounddispatch_midi_stop_time_consumes_refnum_argument() {
let (mut disp, mut cpu, mut bus) = setup();
disp.current_trap_word = 0xA800;
let sp = TEST_SP + 0x80;
let refnum: i16 = 0x003B;
let caller_ret_addr: u32 = 0x003B_2E3C;
let caller_stack_sentinel: u32 = 0xA5A5_5A5A;
bus.write_word(sp, refnum as u16);
bus.write_long(sp + 2, caller_ret_addr);
bus.write_long(sp + 6, caller_stack_sentinel);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0064_0004);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(
disp.current_selector_operation,
Some("selector-operation:_SoundDispatch:0x00640004:d0-long-immediate:32")
);
assert_eq!(cpu.read_reg(Register::A7), sp + 2);
assert_eq!(bus.read_long(sp + 2), caller_ret_addr);
assert_eq!(bus.read_long(sp + 6), caller_stack_sentinel);
assert_eq!(cpu.read_reg(Register::D0), 0);
}
#[test]
fn sounddispatch_get_sound_header_offset_returns_embedded_header_offset() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let (snd_handle, snd_ptr) = alloc_minimal_format2_snd_handle(&mut bus, 2, 80);
let offset_ptr = 0x230C00;
let header_offset = 16u32;
bus.write_word(snd_ptr + 4, 1); bus.write_word(snd_ptr + 6, 0x8000 | cmd::BUFFER);
bus.write_word(snd_ptr + 8, 0);
bus.write_long(snd_ptr + 10, header_offset);
bus.write_long(snd_ptr + header_offset, 0); bus.write_long(snd_ptr + header_offset + 4, 1); bus.write_long(snd_ptr + header_offset + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_byte(snd_ptr + header_offset + 20, 0);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0404_0018);
bus.write_long(sp, offset_ptr);
bus.write_long(sp + 4, snd_handle);
bus.write_word(sp + 8, 0xFFFF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 8);
assert_eq!(bus.read_word(sp + 8), 0);
assert_eq!(bus.read_long(offset_ptr), header_offset);
}
#[test]
fn sndaddmodifier_nil_channel_returns_badchannel_and_consumes_pascal_frame() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, 0); bus.write_word(sp + 4, 5); bus.write_long(sp + 6, 0); bus.write_long(sp + 10, 0); bus.write_word(sp + 14, 0xFFFF);
let result = disp.dispatch_sound(true, 0x002, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 14);
assert_eq!(bus.read_word(sp + 14), (-205i16) as u16);
assert!(disp.sound_manager.channels.is_empty());
}
#[test]
fn sndcontrol_consumes_cmdptr_and_id_and_returns_noerr() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let cmd_ptr = 0x230600;
bus.write_word(cmd_ptr, 0x7777);
bus.write_word(cmd_ptr + 2, 0x8888);
bus.write_long(cmd_ptr + 4, 0x99AA_BBCC);
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr); bus.write_word(sp + 4, 5); bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x006, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert_eq!(bus.read_word(cmd_ptr), 0x7777);
assert_eq!(bus.read_word(cmd_ptr + 2), 0x8888);
assert_eq!(bus.read_long(cmd_ptr + 4), 0x99AA_BBCC);
}
#[test]
fn sndcontrol_availablecmd_zero_init_known_synth_sets_param1_true() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let cmd_ptr = 0x230700;
bus.write_word(cmd_ptr, cmd::AVAILABLE);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr);
bus.write_word(sp + 4, 5); bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x006, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert_eq!(bus.read_word(cmd_ptr), cmd::AVAILABLE);
assert_eq!(bus.read_word(cmd_ptr + 2), 1);
assert_eq!(bus.read_long(cmd_ptr + 4), 0);
}
#[test]
fn sndcontrol_versioncmd_sampled_synth_reports_sound_manager_3_version() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let cmd_ptr = 0x230720;
bus.write_word(cmd_ptr, cmd::VERSION);
bus.write_word(cmd_ptr + 2, 0x7FFF);
bus.write_long(cmd_ptr + 4, 0);
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr);
bus.write_word(sp + 4, SAMPLED_SYNTH_ID as u16);
bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x006, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert_eq!(bus.read_word(cmd_ptr), cmd::VERSION);
assert_eq!(bus.read_word(cmd_ptr + 2), 0);
assert_eq!(bus.read_long(cmd_ptr + 4), SOUND_MANAGER_3_SYNTH_VERSION);
}
#[test]
fn sounddispatch_spb_open_device_pops_frame_and_returns_refnum() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let refnum_ptr = 0x240000;
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0518_0014);
bus.write_long(sp, refnum_ptr);
bus.write_word(sp + 4, 0); bus.write_long(sp + 6, 0); bus.write_word(sp + 10, 0xFFFF);
bus.write_long(refnum_ptr, 0xDEAD_BEEF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(bus.read_long(refnum_ptr), 1);
assert_eq!(cpu.read_reg(Register::D0), 0);
}
#[test]
fn sndnewchannel_returns_noerr_and_writes_nonnull_channel_pointer() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let chan_ptr_ptr = 0x220000;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(sp, 0); bus.write_long(sp + 4, 0); bus.write_word(sp + 8, 5); bus.write_long(sp + 10, chan_ptr_ptr);
bus.write_word(sp + 14, 0xFFFF);
let result = disp.dispatch_sound(true, 0x007, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 14);
assert_eq!(bus.read_word(sp + 14), 0);
let chan_ptr = bus.read_long(chan_ptr_ptr);
assert_ne!(chan_ptr, 0);
assert!(disp.sound_manager.find_channel(chan_ptr).is_some());
}
#[test]
fn sndnewchannel_sets_channel_callback_to_userroutine() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let chan_ptr_ptr = 0x220100;
let user_routine = 0x00C0_FFEE;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(sp, user_routine);
bus.write_long(sp + 4, 0);
bus.write_word(sp + 8, 5);
bus.write_long(sp + 10, chan_ptr_ptr);
let result = disp.dispatch_sound(true, 0x007, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
assert_ne!(chan_ptr, 0);
assert_eq!(bus.read_long(chan_ptr + 8), user_routine);
let chan = disp
.sound_manager
.find_channel(chan_ptr)
.expect("channel tracked");
assert_eq!(chan.callback_addr, user_routine);
}
#[test]
fn sndnewchannel_unsupported_synth_returns_resproblem_and_does_not_allocate() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP;
let chan_ptr_ptr = 0x220180;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(sp, 0); bus.write_long(sp + 4, 0); bus.write_word(sp + 8, 0x7FFF); bus.write_long(sp + 10, chan_ptr_ptr);
bus.write_word(sp + 14, 0xFFFF);
let result = disp.dispatch_sound(true, 0x007, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 14);
assert_eq!(bus.read_word(sp + 14), (-204i16) as u16);
assert_eq!(bus.read_long(chan_ptr_ptr), 0, "chan VAR remains NIL");
assert!(
disp.sound_manager.channels.is_empty(),
"unsupported synth must not create a tracked channel"
);
}
#[test]
fn snddisposechannel_returns_noerr_and_removes_channel_from_manager_state() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220200;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
assert!(disp.sound_manager.find_channel(chan_ptr).is_some());
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 0x0100); bus.write_long(sp + 2, chan_ptr);
bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x001, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert!(disp.sound_manager.find_channel(chan_ptr).is_none());
assert!(
bus.get_alloc_size(chan_ptr).is_none(),
"Sound Manager-owned channel storage must be released on dispose"
);
assert_eq!(
bus.alloc(GUEST_SND_CHANNEL_SIZE),
chan_ptr,
"disposed Sound Manager-owned channel block must return to allocator"
);
}
#[test]
fn snddisposechannel_preserves_caller_owned_channel_storage() {
let (mut disp, mut cpu, mut bus) = setup();
let chan_ptr = bus.alloc(GUEST_SND_CHANNEL_SIZE);
assert_ne!(chan_ptr, 0);
let chan_ptr_ptr = 0x220280;
let create_sp = TEST_SP;
bus.write_long(chan_ptr_ptr, chan_ptr);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
assert!(disp.sound_manager.find_channel(chan_ptr).is_some());
bus.write_long(chan_ptr, 0x1111_0001);
bus.write_long(chan_ptr + 4, 0x1111_0002);
bus.write_long(chan_ptr + 8, 0x1111_0003);
bus.write_long(chan_ptr + 12, 0x1111_0004);
bus.write_long(chan_ptr + 16, 0x1111_2222);
bus.write_long(chan_ptr + 20, 0x3333_4444);
bus.write_long(chan_ptr + 24, 0x5555_6666);
bus.write_word(chan_ptr + 28, 0x7777);
bus.write_word(chan_ptr + 30, 0xAAAA);
bus.write_word(chan_ptr + 32, 0x8888);
bus.write_word(chan_ptr + 34, 0x9999);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 0); bus.write_long(sp + 2, chan_ptr);
bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x001, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert!(disp.sound_manager.find_channel(chan_ptr).is_none());
assert_eq!(
bus.get_alloc_size(chan_ptr),
Some(GUEST_SND_CHANNEL_SIZE),
"caller-owned channel storage must stay allocated after dispose"
);
assert_eq!(bus.read_long(chan_ptr), 0, "nextChan cleared");
assert_eq!(bus.read_long(chan_ptr + 4), 0, "firstMod cleared");
assert_eq!(bus.read_long(chan_ptr + 8), 0, "callBack cleared");
assert_eq!(bus.read_long(chan_ptr + 12), 0, "userInfo cleared");
assert_eq!(bus.read_long(chan_ptr + 16), 0, "wait field cleared");
assert_eq!(
bus.read_long(chan_ptr + 20),
0,
"cmdInProgress cmd/param1 cleared"
);
assert_eq!(
bus.read_long(chan_ptr + 24),
0,
"cmdInProgress param2 cleared"
);
assert_eq!(bus.read_word(chan_ptr + 28), 0, "flags cleared");
assert_eq!(bus.read_word(chan_ptr + 30), 0, "qLength cleared");
assert_eq!(bus.read_word(chan_ptr + 32), 0, "qHead cleared");
assert_eq!(bus.read_word(chan_ptr + 34), 0, "qTail cleared");
}
#[test]
fn snddisposechannel_discards_pending_callbacks_for_disposed_channel() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x2202C0;
let other_chan_ptr = 0x220340;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.queue_sound_callback(PendingSoundCallback::Command {
architecture: crate::callback_manager::CallbackTaskArchitecture::M68k,
callback_addr: 0x00AB_CDEF,
chan_ptr,
cmd: crate::sound::SndCommand {
cmd: cmd::CALLBACK,
param1: 7,
param2: 0x1111_2222,
},
});
disp.sound_manager
.queue_sound_callback(PendingSoundCallback::FileCompletion {
architecture: crate::callback_manager::CallbackTaskArchitecture::M68k,
callback_addr: 0x00FE_DCBA,
chan_ptr,
});
disp.sound_manager
.queue_sound_callback(PendingSoundCallback::FileCompletion {
architecture: crate::callback_manager::CallbackTaskArchitecture::M68k,
callback_addr: 0x0000_2222,
chan_ptr: other_chan_ptr,
});
disp.sound_manager
.queue_doubleback_callback(PendingDoubleBackCallback {
callback_addr: 0x00CA_FE00,
chan_ptr,
header_ptr: 0x0022_4400,
exhausted_buffer_index: 1,
});
disp.sound_manager
.queue_doubleback_callback(PendingDoubleBackCallback {
callback_addr: 0x0000_3333,
chan_ptr: other_chan_ptr,
header_ptr: 0x0022_5500,
exhausted_buffer_index: 0,
});
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 0x0100); bus.write_long(sp + 2, chan_ptr);
bus.write_word(sp + 6, 0xFFFF);
let result = disp.dispatch_sound(true, 0x001, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 6), 0);
assert_eq!(disp.sound_manager.pending_sound_callbacks.len(), 1);
assert!(matches!(
&disp.sound_manager.pending_sound_callbacks[0],
PendingSoundCallback::FileCompletion {
callback_addr: 0x0000_2222,
chan_ptr: ptr,
..
} if *ptr == other_chan_ptr
));
assert_eq!(disp.sound_manager.pending_callbacks.len(), 1);
assert_eq!(
disp.sound_manager.pending_callbacks[0].chan_ptr, other_chan_ptr,
"double-back callbacks for disposed channel must be removed"
);
}
#[test]
fn snddocommand_nullcmd_returns_noerr() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220300;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
let cmd_ptr = 0x230000;
bus.write_word(cmd_ptr, cmd::NULL);
bus.write_word(cmd_ptr + 2, 0x1234);
bus.write_long(cmd_ptr + 4, 0x89AB_CDEF);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 1); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
}
#[test]
fn snddocommand_queues_quiet_behind_active_buffer() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220380;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.with_channel_mut(chan_ptr, |channel| {
channel.play_buffer(
vec![0x80; 128],
crate::sound::OUTPUT_RATE << 16,
PlaybackKind::Buffer,
0,
);
})
.expect("channel exists");
let cmd_ptr = 0x230040;
bus.write_word(cmd_ptr, cmd::QUIET);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 1); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), 0);
assert!(disp
.sound_manager
.find_channel(chan_ptr)
.expect("channel exists")
.is_playing());
assert_eq!(disp.sound_manager.mix_frame(64).len(), 64);
}
#[test]
fn snddoimmediate_flush_discards_guest_fifo_entries() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x2203A0;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.with_channel_mut(chan_ptr, |channel| {
channel.play_buffer(
vec![0xa0; 8],
crate::sound::OUTPUT_RATE << 16,
PlaybackKind::Buffer,
0,
);
})
.expect("channel exists");
let cmd_ptr = 0x230030;
bus.write_word(cmd_ptr, cmd::VOLUME);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 1); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xffff);
assert!(disp
.dispatch_sound(true, 0x003, &mut cpu, &mut bus)
.unwrap()
.is_ok());
assert_ne!(
bus.read_word(chan_ptr + GUEST_SND_CHANNEL_Q_HEAD_OFFSET),
bus.read_word(chan_ptr + GUEST_SND_CHANNEL_Q_TAIL_OFFSET)
);
bus.write_word(cmd_ptr, cmd::FLUSH);
bus.write_long(sp, cmd_ptr); bus.write_long(sp + 4, chan_ptr);
bus.write_word(sp + 8, 0xffff);
cpu.write_reg(Register::A7, sp);
assert!(disp
.dispatch_sound(true, 0x004, &mut cpu, &mut bus)
.unwrap()
.is_ok());
assert_eq!(bus.read_word(sp + 8), 0);
assert_eq!(
bus.read_word(chan_ptr + GUEST_SND_CHANNEL_Q_HEAD_OFFSET),
bus.read_word(chan_ptr + GUEST_SND_CHANNEL_Q_TAIL_OFFSET),
"flush must discard entries still resident in guest FIFO"
);
assert_eq!(
bus.read_long(chan_ptr + GUEST_SND_CHANNEL_CMD_IN_PROGRESS_OFFSET),
0
);
disp.service_guest_sound_queues(&mut bus);
assert_eq!(disp.sound_manager.mix_frame(4), vec![0xa0; 4]);
}
#[test]
fn snddocommand_callbackcmd_on_busy_channel_fires_after_buffer_exhaustion() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x2203C0;
let user_routine = 0x00AB_CDEF;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, user_routine);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.with_channel_mut(chan_ptr, |channel| {
channel.play_buffer(
vec![0x80, 0x80],
crate::sound::OUTPUT_RATE << 16,
PlaybackKind::Buffer,
0,
);
})
.expect("channel exists");
let cmd_ptr = 0x230060;
bus.write_word(cmd_ptr, cmd::CALLBACK);
bus.write_word(cmd_ptr + 2, 7);
bus.write_long(cmd_ptr + 4, 0x1122_3344);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 1); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert!(
disp.sound_manager.pending_sound_callbacks.is_empty(),
"callback must wait for the active buffer to finish"
);
disp.sound_manager.mix_frame(4);
assert_eq!(disp.sound_manager.pending_sound_callbacks.len(), 1);
match &disp.sound_manager.pending_sound_callbacks[0] {
PendingSoundCallback::Command {
architecture,
callback_addr,
chan_ptr: callback_chan,
cmd,
} => {
assert_eq!(
*architecture,
crate::callback_manager::CallbackTaskArchitecture::M68k
);
assert_eq!(*callback_addr, user_routine);
assert_eq!(*callback_chan, chan_ptr);
assert_eq!(cmd.cmd, cmd::CALLBACK);
assert_eq!(cmd.param1, 7);
assert_eq!(cmd.param2, 0x1122_3344);
}
other => panic!("unexpected callback variant: {other:?}"),
}
}
#[test]
fn snddocommand_nil_channel_returns_badchannel() {
let (mut disp, mut cpu, mut bus) = setup();
let cmd_ptr = 0x230080;
bus.write_word(cmd_ptr, cmd::NULL);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 1); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, 0); bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 10);
assert_eq!(bus.read_word(sp + 10), (-205i16) as u16);
}
#[test]
fn snddocommand_callbackcmd_uses_channel_callback_proc() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220400;
let user_routine = 0x00AB_CDEF;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, user_routine);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.with_mut(|manager| manager.pending_sound_callbacks.clear());
let cmd_ptr = 0x230100;
bus.write_word(cmd_ptr, cmd::CALLBACK);
bus.write_word(cmd_ptr + 2, 7);
bus.write_long(cmd_ptr + 4, 0x1122_3344);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 0); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, chan_ptr);
let result = disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(disp.sound_manager.pending_sound_callbacks.len(), 1);
match &disp.sound_manager.pending_sound_callbacks[0] {
PendingSoundCallback::Command {
architecture,
callback_addr,
chan_ptr: callback_chan,
cmd,
} => {
assert_eq!(
*architecture,
crate::callback_manager::CallbackTaskArchitecture::M68k
);
assert_eq!(*callback_addr, user_routine);
assert_eq!(*callback_chan, chan_ptr);
assert_eq!(cmd.cmd, cmd::CALLBACK);
assert_eq!(cmd.param1, 7);
assert_eq!(cmd.param2, 0x1122_3344);
}
other => panic!("unexpected callback variant: {other:?}"),
}
}
#[test]
fn snddoimmediate_nullcmd_returns_noerr() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220500;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
let cmd_ptr = 0x230200;
bus.write_word(cmd_ptr, cmd::NULL);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr);
bus.write_long(sp + 4, chan_ptr);
bus.write_word(sp + 8, 0xFFFF);
let result = disp.dispatch_sound(true, 0x004, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 8);
assert_eq!(bus.read_word(sp + 8), 0);
}
#[test]
fn snddoimmediate_nil_channel_returns_badchannel() {
let (mut disp, mut cpu, mut bus) = setup();
let cmd_ptr = 0x230180;
bus.write_word(cmd_ptr, cmd::NULL);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr);
bus.write_long(sp + 4, 0); bus.write_word(sp + 8, 0xFFFF);
let result = disp.dispatch_sound(true, 0x004, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 8);
assert_eq!(bus.read_word(sp + 8), (-205i16) as u16);
}
#[test]
fn snddoimmediate_getratecmd_writes_fixed_rate_to_param2() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220600;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.with_channel_mut(chan_ptr, |channel| channel.set_rate(0x0001_8000))
.expect("channel exists");
let rate_out_ptr = 0x230300;
bus.write_long(rate_out_ptr, 0xDEAD_BEEF);
let cmd_ptr = 0x230308;
bus.write_word(cmd_ptr, cmd::GET_RATE);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, rate_out_ptr);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr);
bus.write_long(sp + 4, chan_ptr);
bus.write_word(sp + 8, 0xFFFF);
let result = disp.dispatch_sound(true, 0x004, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 8);
assert_eq!(bus.read_word(sp + 8), 0);
assert_eq!(bus.read_long(rate_out_ptr), 0x0001_8000);
}
#[test]
fn decode_double_buffer_samples_preserves_stereo_8bit() {
let mut bus = MacMemoryBus::new(1024 * 1024);
let data_addr = 0x1000;
let bytes = [0x00, 0xFF, 0x40, 0xC0];
for (idx, byte) in bytes.into_iter().enumerate() {
bus.write_byte(data_addr + idx as u32, byte);
}
let samples =
decode_double_buffer_samples(&bus, data_addr, 2, 2, 8).expect("stereo 8-bit audio");
assert_eq!(
samples,
vec![
StereoSample {
left: 0x00,
right: 0xFF
},
StereoSample {
left: 0x40,
right: 0xC0
},
]
);
assert_eq!(
samples
.iter()
.copied()
.map(StereoSample::downmix)
.collect::<Vec<_>>(),
vec![128, 128]
);
}
#[test]
fn snddocommand_busy_channel_queues_buffer_cmd_for_later_bus_backed_decode() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x2203A0;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
disp.sound_manager
.with_channel_mut(chan_ptr, |channel| {
channel.play_buffer(
vec![0x20, 0x21],
crate::sound::OUTPUT_RATE << 16,
PlaybackKind::Buffer,
0,
);
})
.expect("channel exists");
let header = 0x230180;
bus.write_long(header, 0);
bus.write_long(header + 4, 2);
bus.write_long(header + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_long(header + 12, 0);
bus.write_long(header + 16, 0);
bus.write_byte(header + 20, 0);
bus.write_byte(header + 21, 60);
bus.write_bytes(header + 22, &[0xB0, 0xB1]);
let cmd_ptr = 0x230160;
bus.write_word(cmd_ptr, cmd::BUFFER);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, header);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 1); bus.write_long(sp + 2, cmd_ptr);
bus.write_long(sp + 6, chan_ptr);
bus.write_word(sp + 10, 0xFFFF);
let result = disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(disp.sound_manager.mix_frame(2), vec![0x20, 0x21]);
disp.service_guest_sound_queues(&mut bus);
assert_eq!(disp.sound_manager.mix_frame(2), vec![0xB0, 0xB1]);
}
#[test]
fn decode_mace3_mono_expands_packet_frame_to_unsigned_samples() {
let samples = decode_mace3_mono_to_u8(&[0x00, 0x00]);
assert_eq!(samples, vec![0x80; 6]);
}
#[test]
fn decode_mace6_mono_expands_packet_to_unsigned_samples() {
let samples = decode_mace6_mono_to_u8(&[0x00]);
assert_eq!(samples, vec![0x80; 6]);
}
#[test]
fn buffercmd_cmpsh_mace3_starts_buffer_playback() {
let (mut disp, _cpu, mut bus) = setup();
let chan_ptr = 0x250000;
disp.sound_manager
.add_channel(SndChannel::new(chan_ptr, false));
let header = 0x260000;
bus.write_long(header, 0); bus.write_long(header + 4, 1); bus.write_long(header + 8, crate::sound::OUTPUT_RATE << 16);
bus.write_long(header + 12, 0); bus.write_long(header + 16, 0); bus.write_byte(header + 20, 0xFE); bus.write_byte(header + 21, 60); bus.write_long(header + 22, 1); bus.write_long(header + 40, super::MACE3_FORMAT);
bus.write_word(header + 56, 3); bus.write_word(header + 58, 16); bus.write_word(header + 62, 8); bus.write_byte(header + 64, 0x00);
bus.write_byte(header + 65, 0x00);
disp.execute_buffer_cmd(
&mut bus,
chan_ptr,
&SndCommand {
cmd: cmd::BUFFER,
param1: 0,
param2: header,
},
);
assert!(disp
.sound_manager
.find_channel(chan_ptr)
.expect("channel exists")
.is_playing());
assert_eq!(disp.sound_manager.mix_frame(6), vec![0x80; 6]);
}
#[test]
fn sndplaydoublebuffer_zero_sample_rate_defaults_to_classic_22khz() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let chan_ptr = 0x250000;
let header_ptr = 0x260000;
let buf0_ptr = 0x270000;
let buf1_ptr = 0x280000;
bus.write_word(header_ptr, 1); bus.write_word(header_ptr + 2, 8); bus.write_word(header_ptr + 4, 0); bus.write_word(header_ptr + 6, 0); bus.write_long(header_ptr + 8, 0); bus.write_long(header_ptr + 12, buf0_ptr);
bus.write_long(header_ptr + 16, buf1_ptr);
bus.write_long(header_ptr + 20, 0x1234_5678);
bus.write_long(buf0_ptr, 1); bus.write_long(buf0_ptr + 4, 0x0000_0001); bus.write_byte(buf0_ptr + 16, 0x40);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0020_0008); bus.write_long(sp, header_ptr);
bus.write_long(sp + 4, chan_ptr);
bus.write_word(sp + 8, 0xFFFF);
let result = disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus);
assert!(result.unwrap().is_ok());
assert_eq!(cpu.read_reg(Register::A7), sp + 8);
assert_eq!(bus.read_word(sp + 8), 0);
assert_eq!(disp.sound_manager.debug_double_buffer_count, 1);
let chan = disp
.sound_manager
.find_channel(chan_ptr)
.expect("zero-rate probe should still create a channel");
assert!(chan.is_playing());
assert_eq!(
chan.double_buffer
.as_ref()
.expect("double-buffer state")
.sample_rate,
crate::sound::RATE_22KHZ_FIXED
);
assert_eq!(
disp.sound_manager.mix_frame(2),
vec![0x40, 0x80],
"output-rate fallback must not hold an already converted double-buffer frame for two output samples"
);
}
#[test]
fn decode_double_buffer_samples_edge_cases() {
let mut bus = MacMemoryBus::new(1024 * 1024);
let data_addr = 0x3000;
bus.write_bytes(data_addr, &[0xAA; 64]);
let out = decode_double_buffer_samples(&bus, data_addr, 0, 2, 16)
.expect("empty frames → Some(empty)");
assert!(out.is_empty(), "num_frames=0 produces empty Vec");
let out = decode_double_buffer_samples(&bus, data_addr, 4, 0, 8)
.expect("zero channels → Some(empty)");
assert!(out.is_empty(), "num_channels=0 produces empty Vec");
assert!(
decode_double_buffer_samples(&bus, data_addr, 4, 1, 24).is_none(),
"unsupported sample_size=24 must be rejected"
);
assert!(
decode_double_buffer_samples(&bus, data_addr, 4, 1, 32).is_none(),
"unsupported sample_size=32 must be rejected"
);
bus.write_byte(data_addr, 0x40);
bus.write_byte(data_addr + 1, 0x80);
bus.write_byte(data_addr + 2, 0xC0);
let out = decode_double_buffer_samples(&bus, data_addr, 3, 1, 8).expect("mono 8-bit frames");
assert_eq!(
out,
vec![
StereoSample::mono(0x40),
StereoSample::mono(0x80),
StereoSample::mono(0xC0)
],
"mono 8-bit pass-through"
);
bus.write_byte(data_addr, 0x40);
bus.write_byte(data_addr + 1, 0x00);
bus.write_byte(data_addr + 2, 0xC0);
bus.write_byte(data_addr + 3, 0x00);
let out = decode_double_buffer_samples(&bus, data_addr, 2, 1, 16).expect("mono 16-bit frames");
assert_eq!(
out,
vec![StereoSample::mono(0xC0), StereoSample::mono(0x40)],
"mono 16-bit upper-byte + center"
);
}
#[test]
fn decode_double_buffer_samples_preserves_stereo_16bit() {
let mut bus = MacMemoryBus::new(1024 * 1024);
let data_addr = 0x2000;
let bytes = [
0x80, 0x00, 0x7F, 0xFF, 0x20, 0x00, 0x60, 0x00, ];
for (idx, byte) in bytes.into_iter().enumerate() {
bus.write_byte(data_addr + idx as u32, byte);
}
let samples =
decode_double_buffer_samples(&bus, data_addr, 2, 2, 16).expect("stereo 16-bit audio");
assert_eq!(
samples,
vec![
StereoSample {
left: 0x00,
right: 0xFF
},
StereoSample {
left: 0xA0,
right: 0xE0
},
]
);
assert_eq!(
samples
.iter()
.copied()
.map(StereoSample::downmix)
.collect::<Vec<_>>(),
vec![128, 192]
);
}
#[test]
fn snddoimmediate_ampcmd_sets_channel_amplitude() {
let (mut disp, mut cpu, mut bus) = setup();
let create_sp = TEST_SP;
let chan_ptr_ptr = 0x220500;
bus.write_long(chan_ptr_ptr, 0);
bus.write_long(create_sp, 0);
bus.write_long(create_sp + 4, 0);
bus.write_word(create_sp + 8, 5);
bus.write_long(create_sp + 10, chan_ptr_ptr);
assert!(disp
.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.is_ok());
let chan_ptr = bus.read_long(chan_ptr_ptr);
let cmd_ptr = 0x230200;
bus.write_word(cmd_ptr, cmd::AMP);
bus.write_word(cmd_ptr + 2, 0);
bus.write_long(cmd_ptr + 4, 0);
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_long(sp, cmd_ptr);
bus.write_long(sp + 4, chan_ptr);
bus.write_word(sp + 8, 0xFFFF);
assert!(disp
.dispatch_sound(true, 0x004, &mut cpu, &mut bus)
.unwrap()
.is_ok());
assert_eq!(bus.read_word(sp + 8), 0);
disp.sound_manager.play_buffer_command_for_architecture(
chan_ptr,
vec![0xC0; 2],
crate::sound::OUTPUT_RATE << 16,
crate::callback_manager::CallbackTaskArchitecture::M68k,
);
assert_eq!(disp.sound_manager.mix_frame(2), vec![0x80, 0x80]);
}
#[test]
fn square_wave_note_disposal_waits_for_audio_and_queued_callback_with_false_padding() {
let (mut disp, mut cpu, mut bus) = setup();
let slot = 0x220200;
bus.write_long(TEST_SP, 0x123456); bus.write_long(TEST_SP + 4, 0x80);
bus.write_word(TEST_SP + 8, 1); bus.write_long(TEST_SP + 10, slot);
bus.write_long(slot, 0);
disp.dispatch_sound(true, 0x007, &mut cpu, &mut bus)
.unwrap()
.unwrap();
let channel = bus.read_long(slot);
let command = 0x220300;
let sp = TEST_SP + 0x40;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 0x006A); bus.write_long(sp + 2, command);
bus.write_long(sp + 6, channel);
bus.write_word(command, cmd::FREQ_DURATION);
bus.write_word(command + 2, 250); bus.write_long(command + 4, 68);
disp.dispatch_sound(true, 0x003, &mut cpu, &mut bus)
.unwrap()
.unwrap();
assert!(disp
.sound_manager
.find_channel(channel)
.unwrap()
.is_playing());
disp.execute_sound_command(
&mut bus,
channel,
SndCommand {
cmd: cmd::CALLBACK,
param1: 0,
param2: 99,
},
);
let sp = TEST_SP + 0x80;
cpu.write_reg(Register::A7, sp);
bus.write_word(sp, 0x006A);
bus.write_long(sp + 2, channel);
bus.write_word(sp + 6, 0xFFFF);
disp.dispatch_sound(true, 0x001, &mut cpu, &mut bus)
.unwrap()
.unwrap();
assert_eq!(cpu.read_reg(Register::A7), sp);
assert_eq!(bus.read_word(sp + 6), 0xFFFF);
assert!(disp.service_pending_sound_disposal(&mut cpu, &mut bus));
let audio = disp.sound_manager.mix_frame(2500);
assert!(audio.iter().any(|sample| *sample != 128));
assert!(disp.service_pending_sound_disposal(&mut cpu, &mut bus));
disp.sound_manager.mix_frame(1000);
assert!(
disp.service_pending_sound_disposal(&mut cpu, &mut bus),
"queued callback must survive disposal wait"
);
assert!(
matches!(disp.sound_manager.with_mut(|sound| sound.pending_sound_callbacks.remove(0)), PendingSoundCallback::Command { chan_ptr, cmd, .. } if chan_ptr == channel && cmd.param2 == 99)
);
assert!(!disp.service_pending_sound_disposal(&mut cpu, &mut bus));
assert_eq!(cpu.read_reg(Register::A7), sp + 6);
assert_eq!(bus.read_word(sp + 6), 0);
assert!(disp.sound_manager.find_channel(channel).is_none());
}
#[test]
fn sound_disposal_true_boolean_cancels_playback_without_waiting() {
let (mut disp, mut cpu, mut bus) = setup();
let channel = bus.alloc(GUEST_SND_CHANNEL_SIZE);
let mut chan = SndChannel::new(channel, true);
chan.play_buffer(
vec![255; 1000],
crate::sound::OUTPUT_RATE << 16,
PlaybackKind::Buffer,
0,
);
disp.sound_manager.add_channel(chan);
bus.write_word(TEST_SP, 0x016A); bus.write_long(TEST_SP + 2, channel);
disp.dispatch_sound(true, 0x001, &mut cpu, &mut bus)
.unwrap()
.unwrap();
assert_eq!(cpu.read_reg(Register::A7), TEST_SP + 6);
assert!(disp.pending_sound_disposal.is_none());
assert!(disp.sound_manager.find_channel(channel).is_none());
assert!(disp
.sound_manager
.mix_frame(100)
.iter()
.all(|sample| *sample == 128));
}
#[test]
fn snd_channel_status_tracks_active_sample_playback() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let channel_ptr = 0x0039_38C8;
let status_ptr = 0x230A80;
let mut channel = SndChannel::new(channel_ptr, false);
channel.play_buffer(vec![0x40, 0x80, 0xC0, 0xFF], crate::sound::OUTPUT_RATE << 16, crate::sound::PlaybackKind::Buffer, 0);
disp.sound_manager.add_channel(channel);
for expected_busy in [1u8, 0] {
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0510_0008);
bus.write_long(sp, status_ptr);
bus.write_word(sp + 4, 24);
bus.write_long(sp + 6, channel_ptr);
bus.write_word(sp + 10, 0xFFFF);
assert!(disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus).unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(bus.read_byte(status_ptr + 12), expected_busy);
if expected_busy == 1 {
disp.sound_manager.mix_frame(4);
}
}
}
#[test]
fn snd_channel_status_is_specific_to_channel_and_record_length() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let active_ptr = 0x0039_38C8;
let idle_ptr = 0x0039_4000;
let status_ptr = 0x230A80;
let mut active = SndChannel::new(active_ptr, false);
active.play_buffer(vec![0x80; 40], crate::sound::OUTPUT_RATE << 16, PlaybackKind::Buffer, 0);
disp.sound_manager.add_channel(active);
disp.sound_manager.add_channel(SndChannel::new(idle_ptr, false));
for (channel_ptr, len, expected) in [(idle_ptr, 24, 0), (active_ptr, 13, 1)] {
bus.write_byte(status_ptr + 13, 0xA5);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0510_0008);
bus.write_long(sp, status_ptr);
bus.write_word(sp + 4, len);
bus.write_long(sp + 6, channel_ptr);
bus.write_word(sp + 10, 0xFFFF);
assert!(disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus).unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(bus.read_byte(status_ptr + 12), expected);
if len == 13 {
assert_eq!(bus.read_byte(status_ptr + 13), 0xA5);
}
}
}
#[test]
fn snd_channel_status_reports_paused_file_playback() {
let (mut disp, mut cpu, mut bus) = setup();
let sp = TEST_SP + 0x80;
let channel_ptr = 0x0039_38C8;
let status_ptr = 0x230A80;
let mut channel = SndChannel::new(channel_ptr, false);
channel.play_buffer(vec![0x80; 40], crate::sound::OUTPUT_RATE << 16, PlaybackKind::File, 0);
channel.pause_file_playback_toggle();
disp.sound_manager.add_channel(channel);
cpu.write_reg(Register::A7, sp);
cpu.write_reg(Register::D0, 0x0510_0008);
bus.write_long(sp, status_ptr);
bus.write_word(sp + 4, 24);
bus.write_long(sp + 6, channel_ptr);
bus.write_word(sp + 10, 0xFFFF);
assert!(disp.dispatch_sound(true, 0x000, &mut cpu, &mut bus).unwrap().is_ok());
assert_eq!(bus.read_word(sp + 10), 0);
assert_eq!(bus.read_byte(status_ptr + 14), 1);
}