gb-emu/lib/tests/common/mod.rs

54 lines
1.7 KiB
Rust
Raw Normal View History

2023-12-21 13:01:38 +11:00
use std::sync::mpsc::{channel, Receiver, Sender};
use async_ringbuf::AsyncHeapConsumer;
use gb_emu_lib::{
connect::{AudioOutput, EmulatorMessage, EmulatorOptions, NoCamera, RomFile},
EmulatorCore,
};
pub struct TestEmulator {
pub core: EmulatorCore<[u8; 4], NoCamera>,
pub sender: Sender<EmulatorMessage<[u8; 4]>>,
pub audio_rx: AsyncHeapConsumer<[f32; 2]>,
pub serial_rx: Receiver<u8>,
}
pub fn emulator_setup(rom_file: RomFile) -> Result<TestEmulator, String> {
let (sender, receiver) = channel::<EmulatorMessage<[u8; 4]>>();
let (rom, camera) = rom_file
.load(gb_emu_lib::connect::SramType::None, NoCamera::default())
.map_err(|_e| String::from("Error reading ROM: {_e:?}"))?;
let (audio_output, audio_rx) = AudioOutput::new(
48000.,
1,
gb_emu_lib::connect::DownsampleType::ZeroOrderHold,
);
// let (tx_to_gameboy, rx_from_test) = channel::<u8>();
let (tx_to_test, serial_rx) = channel::<u8>();
let options = EmulatorOptions::new(None, rom, audio_output)
.with_no_output(true)
.with_serial_target(gb_emu_lib::connect::SerialTarget::Custom {
rx: None,
tx: Some(tx_to_test),
})
.with_cgb_bootrom(Some(RomFile::Raw(
include_bytes!("../../../sameboy-bootroms/cgb_boot.bin").to_vec(),
)))
.with_dmg_bootrom(Some(RomFile::Raw(
include_bytes!("../../../sameboy-bootroms/dmg_boot.bin").to_vec(),
)));
let core = EmulatorCore::init(true, receiver, options, camera);
sender
.send(EmulatorMessage::Start)
.map_err(|_e| String::from("Error sending message: {_e:?}"))?;
Ok(TestEmulator {
core,
sender,
audio_rx,
serial_rx,
})
}