2019-07-06 23:33:54 +01:00
|
|
|
/// Struct containing everything
|
2020-05-25 23:46:05 +01:00
|
|
|
use std::cell::{Cell, RefCell};
|
2019-12-09 21:37:46 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
use bincode;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-02-10 21:45:53 +00:00
|
|
|
use super::arm7tdmi;
|
2019-07-06 13:53:36 +01:00
|
|
|
use super::cartridge::Cartridge;
|
2020-05-25 23:46:05 +01:00
|
|
|
use super::dma::DmaController;
|
2019-07-20 12:44:49 +01:00
|
|
|
use super::gpu::*;
|
2019-07-15 05:35:09 +01:00
|
|
|
use super::interrupt::*;
|
2019-11-08 23:43:43 +00:00
|
|
|
use super::iodev::*;
|
2020-10-17 14:22:31 +01:00
|
|
|
use super::sched::{EventType, Scheduler, SharedScheduler};
|
2019-12-22 23:30:36 +00:00
|
|
|
use super::sound::SoundController;
|
2019-07-06 13:53:36 +01:00
|
|
|
use super::sysbus::SysBus;
|
2020-05-25 23:46:05 +01:00
|
|
|
use super::timer::Timers;
|
2020-10-10 19:06:11 +01:00
|
|
|
use super::util::Shared;
|
2019-12-04 23:15:49 +00:00
|
|
|
|
2020-09-29 22:10:47 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
|
|
|
use super::VideoInterface;
|
2020-10-03 20:09:38 +01:00
|
|
|
use super::{AudioInterface, InputInterface};
|
2019-07-06 13:53:36 +01:00
|
|
|
|
2019-12-22 23:27:25 +00:00
|
|
|
pub struct GameBoyAdvance {
|
2020-10-17 14:22:31 +01:00
|
|
|
pub cpu: Box<arm7tdmi::Core<SysBus>>,
|
|
|
|
pub sysbus: Shared<SysBus>,
|
|
|
|
pub io_devs: Shared<IoDevices>,
|
|
|
|
pub scheduler: SharedScheduler,
|
|
|
|
interrupt_flags: SharedInterruptFlags,
|
2020-09-29 22:10:47 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
2020-02-21 12:04:39 +00:00
|
|
|
pub video_device: Rc<RefCell<dyn VideoInterface>>,
|
|
|
|
pub audio_device: Rc<RefCell<dyn AudioInterface>>,
|
|
|
|
pub input_device: Rc<RefCell<dyn InputInterface>>,
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct SaveState {
|
2020-10-03 20:09:38 +01:00
|
|
|
scheduler: Scheduler,
|
2020-10-10 19:06:11 +01:00
|
|
|
io_devs: IoDevices,
|
|
|
|
cartridge: Cartridge,
|
|
|
|
ewram: Box<[u8]>,
|
|
|
|
iwram: Box<[u8]>,
|
2020-09-27 13:44:17 +01:00
|
|
|
interrupt_flags: u16,
|
2020-10-17 14:22:31 +01:00
|
|
|
cpu_state: arm7tdmi::SavedCpuState,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum BusMaster {
|
|
|
|
Dma,
|
|
|
|
Cpu,
|
2020-01-16 18:06:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 11:22:58 +01:00
|
|
|
/// Checks if the bios provided is the real one
|
|
|
|
fn check_real_bios(bios: &[u8]) -> bool {
|
2020-05-01 15:58:15 +01:00
|
|
|
use sha2::{Digest, Sha256};
|
2020-05-15 11:22:58 +01:00
|
|
|
|
2020-05-01 15:58:15 +01:00
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
hasher.input(bios);
|
|
|
|
let digest = hasher.result();
|
|
|
|
|
|
|
|
let expected_hash = hex!("fd2547724b505f487e6dcb29ec2ecff3af35a841a77ab2e85fd87350abd36570");
|
2020-05-15 11:22:58 +01:00
|
|
|
|
|
|
|
digest.as_slice() == &expected_hash[..]
|
2020-05-01 15:58:15 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:27:25 +00:00
|
|
|
impl GameBoyAdvance {
|
2019-12-09 21:37:46 +00:00
|
|
|
pub fn new(
|
2020-02-24 22:11:10 +00:00
|
|
|
bios_rom: Box<[u8]>,
|
2019-12-09 21:37:46 +00:00
|
|
|
gamepak: Cartridge,
|
2020-10-03 20:09:38 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))] video_device: Rc<RefCell<dyn VideoInterface>>,
|
2019-12-22 23:27:25 +00:00
|
|
|
audio_device: Rc<RefCell<dyn AudioInterface>>,
|
|
|
|
input_device: Rc<RefCell<dyn InputInterface>>,
|
|
|
|
) -> GameBoyAdvance {
|
2020-05-01 15:58:15 +01:00
|
|
|
// Warn the user if the bios is not the real one
|
2020-05-15 11:22:58 +01:00
|
|
|
match check_real_bios(&bios_rom) {
|
|
|
|
true => info!("Verified bios rom"),
|
|
|
|
false => warn!("This is not the real bios rom, some games may not be compatible"),
|
|
|
|
};
|
2020-05-25 23:46:05 +01:00
|
|
|
|
|
|
|
let interrupt_flags = Rc::new(Cell::new(IrqBitmask(0)));
|
2020-10-03 20:09:38 +01:00
|
|
|
let scheduler = Scheduler::new_shared();
|
2020-05-25 23:46:05 +01:00
|
|
|
|
|
|
|
let intc = InterruptController::new(interrupt_flags.clone());
|
2020-10-03 20:09:38 +01:00
|
|
|
let gpu = Box::new(Gpu::new(scheduler.clone(), interrupt_flags.clone()));
|
2020-10-10 18:57:00 +01:00
|
|
|
let dmac = DmaController::new(interrupt_flags.clone(), scheduler.clone());
|
2020-10-17 14:22:31 +01:00
|
|
|
let timers = Timers::new(interrupt_flags.clone(), scheduler.clone());
|
2020-01-16 17:56:05 +00:00
|
|
|
let sound_controller = Box::new(SoundController::new(
|
2020-10-03 20:09:38 +01:00
|
|
|
scheduler.clone(),
|
2020-01-16 17:56:05 +00:00
|
|
|
audio_device.borrow().get_sample_rate() as f32,
|
|
|
|
));
|
2020-10-10 19:06:11 +01:00
|
|
|
let io_devs = Shared::new(IoDevices::new(intc, gpu, dmac, timers, sound_controller));
|
2020-10-17 14:22:31 +01:00
|
|
|
let sysbus = Shared::new(SysBus::new(
|
2020-10-10 19:06:11 +01:00
|
|
|
scheduler.clone(),
|
|
|
|
io_devs.clone(),
|
|
|
|
bios_rom,
|
|
|
|
gamepak,
|
|
|
|
));
|
2020-04-04 11:50:50 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
let cpu = Box::new(arm7tdmi::Core::new(sysbus.clone()));
|
2020-02-10 21:45:53 +00:00
|
|
|
|
2020-04-04 11:50:50 +01:00
|
|
|
let mut gba = GameBoyAdvance {
|
2020-10-10 19:06:11 +01:00
|
|
|
cpu,
|
|
|
|
sysbus,
|
|
|
|
io_devs,
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2020-09-29 22:10:47 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
2020-01-16 17:56:05 +00:00
|
|
|
video_device: video_device,
|
|
|
|
audio_device: audio_device,
|
2019-12-09 21:37:46 +00:00
|
|
|
input_device: input_device,
|
2020-01-11 13:58:32 +00:00
|
|
|
|
2020-10-03 20:09:38 +01:00
|
|
|
scheduler: scheduler,
|
|
|
|
|
2020-09-27 13:44:17 +01:00
|
|
|
interrupt_flags: interrupt_flags,
|
2020-04-04 11:50:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
gba.sysbus.created();
|
|
|
|
|
|
|
|
gba
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 21:40:05 +00:00
|
|
|
pub fn from_saved_state(
|
|
|
|
savestate: &[u8],
|
2020-10-10 19:06:11 +01:00
|
|
|
bios: Box<[u8]>,
|
|
|
|
rom: Box<[u8]>,
|
2020-10-03 20:09:38 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))] video_device: Rc<RefCell<dyn VideoInterface>>,
|
2020-02-29 21:40:05 +00:00
|
|
|
audio_device: Rc<RefCell<dyn AudioInterface>>,
|
|
|
|
input_device: Rc<RefCell<dyn InputInterface>>,
|
|
|
|
) -> bincode::Result<GameBoyAdvance> {
|
|
|
|
let decoded: Box<SaveState> = bincode::deserialize_from(savestate)?;
|
|
|
|
|
2020-09-27 13:44:17 +01:00
|
|
|
let interrupts = Rc::new(Cell::new(IrqBitmask(decoded.interrupt_flags)));
|
2020-10-03 20:09:38 +01:00
|
|
|
let scheduler = decoded.scheduler.make_shared();
|
2020-10-10 19:06:11 +01:00
|
|
|
let mut io_devs = Shared::new(decoded.io_devs);
|
|
|
|
let mut cartridge = decoded.cartridge;
|
|
|
|
cartridge.set_rom_bytes(rom);
|
|
|
|
io_devs.connect_irq(interrupts.clone());
|
|
|
|
io_devs.gpu.set_scheduler(scheduler.clone());
|
|
|
|
io_devs.sound.set_scheduler(scheduler.clone());
|
2020-10-17 14:22:31 +01:00
|
|
|
let sysbus = Shared::new(SysBus::new_with_memories(
|
2020-10-10 19:06:11 +01:00
|
|
|
scheduler.clone(),
|
|
|
|
io_devs.clone(),
|
|
|
|
cartridge,
|
|
|
|
bios,
|
|
|
|
decoded.ewram,
|
|
|
|
decoded.iwram,
|
|
|
|
));
|
2020-10-17 14:22:31 +01:00
|
|
|
let arm7tdmi = Box::new(arm7tdmi::Core::from_saved_state(
|
|
|
|
sysbus.clone(),
|
|
|
|
decoded.cpu_state,
|
|
|
|
));
|
2020-09-27 13:44:17 +01:00
|
|
|
|
2020-02-29 21:40:05 +00:00
|
|
|
Ok(GameBoyAdvance {
|
2020-09-27 13:44:17 +01:00
|
|
|
cpu: arm7tdmi,
|
|
|
|
sysbus: sysbus,
|
2020-10-10 19:06:11 +01:00
|
|
|
io_devs,
|
2020-09-27 13:44:17 +01:00
|
|
|
|
|
|
|
interrupt_flags: interrupts,
|
2020-02-29 21:40:05 +00:00
|
|
|
|
2020-09-29 22:10:47 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
2020-02-29 21:40:05 +00:00
|
|
|
video_device: video_device,
|
|
|
|
audio_device: audio_device,
|
|
|
|
input_device: input_device,
|
|
|
|
|
2020-10-10 19:06:11 +01:00
|
|
|
scheduler,
|
2020-02-29 21:40:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
pub fn save_state(&self) -> bincode::Result<Vec<u8>> {
|
|
|
|
let s = SaveState {
|
2020-10-17 14:22:31 +01:00
|
|
|
cpu_state: self.cpu.save_state(),
|
2020-10-10 19:06:11 +01:00
|
|
|
io_devs: self.io_devs.clone_inner(),
|
|
|
|
cartridge: self.sysbus.cartridge.thin_copy(),
|
|
|
|
iwram: Box::from(self.sysbus.get_iwram()),
|
|
|
|
ewram: Box::from(self.sysbus.get_ewram()),
|
2020-09-27 13:44:17 +01:00
|
|
|
interrupt_flags: self.interrupt_flags.get().value(),
|
2020-10-10 19:06:11 +01:00
|
|
|
scheduler: self.scheduler.clone_inner(),
|
2020-01-16 18:06:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bincode::serialize(&s)
|
|
|
|
}
|
|
|
|
|
2020-10-10 19:06:11 +01:00
|
|
|
pub fn restore_state(&mut self, bytes: &[u8], bios: Box<[u8]>) -> bincode::Result<()> {
|
2020-01-16 18:06:22 +00:00
|
|
|
let decoded: Box<SaveState> = bincode::deserialize_from(bytes)?;
|
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
self.cpu.restore_state(decoded.cpu_state);
|
2020-10-03 20:09:38 +01:00
|
|
|
self.scheduler = Scheduler::make_shared(decoded.scheduler);
|
2020-09-27 13:44:17 +01:00
|
|
|
self.interrupt_flags = Rc::new(Cell::new(IrqBitmask(decoded.interrupt_flags)));
|
2020-10-10 19:06:11 +01:00
|
|
|
self.io_devs = Shared::new(decoded.io_devs);
|
|
|
|
// Restore memory state
|
2020-10-17 14:22:31 +01:00
|
|
|
self.cpu.set_memory_interface(self.sysbus.clone());
|
2020-10-10 19:06:11 +01:00
|
|
|
self.sysbus.set_bios(bios);
|
|
|
|
self.sysbus.set_iwram(decoded.iwram);
|
|
|
|
self.sysbus.set_ewram(decoded.ewram);
|
2020-10-03 20:09:38 +01:00
|
|
|
// Redistribute shared pointers
|
2020-10-10 19:06:11 +01:00
|
|
|
self.io_devs.connect_irq(self.interrupt_flags.clone());
|
|
|
|
self.io_devs.gpu.set_scheduler(self.scheduler.clone());
|
|
|
|
self.io_devs.sound.set_scheduler(self.scheduler.clone());
|
|
|
|
self.sysbus.set_scheduler(self.scheduler.clone());
|
|
|
|
self.sysbus.set_io_devices(self.io_devs.clone());
|
|
|
|
self.sysbus.cartridge.update_from(decoded.cartridge);
|
2020-04-04 11:50:50 +01:00
|
|
|
self.sysbus.created();
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-02-27 09:03:34 +00:00
|
|
|
pub fn get_game_title(&self) -> String {
|
|
|
|
self.sysbus.cartridge.header.game_title.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_game_code(&self) -> String {
|
|
|
|
self.sysbus.cartridge.header.game_code.clone()
|
|
|
|
}
|
|
|
|
|
2019-12-21 18:19:43 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn key_poll(&mut self) {
|
2019-12-09 21:37:46 +00:00
|
|
|
self.sysbus.io.keyinput = self.input_device.borrow_mut().poll();
|
2019-12-21 18:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn frame(&mut self) {
|
|
|
|
self.key_poll();
|
2020-10-17 14:22:31 +01:00
|
|
|
static mut OVERSHOOT: usize = 0;
|
|
|
|
unsafe {
|
|
|
|
OVERSHOOT = self.run(CYCLES_FULL_REFRESH - OVERSHOOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn dma_step(&mut self) {
|
|
|
|
self.io_devs.dmac.perform_work(&mut self.sysbus);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn cpu_step(&mut self) {
|
|
|
|
if self.io_devs.intc.irq_pending() {
|
|
|
|
self.cpu.irq();
|
|
|
|
self.io_devs.haltcnt = HaltState::Running;
|
|
|
|
}
|
|
|
|
self.cpu.step();
|
|
|
|
}
|
2020-04-09 22:28:59 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
#[inline]
|
|
|
|
fn get_bus_master(&mut self) -> Option<BusMaster> {
|
|
|
|
match (self.io_devs.dmac.is_active(), self.io_devs.haltcnt) {
|
|
|
|
(true, _) => Some(BusMaster::Dma),
|
|
|
|
(false, HaltState::Running) => Some(BusMaster::Cpu),
|
|
|
|
(false, _) => None,
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 20:09:38 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
/// Runs the emulation for a given amount of cycles
|
|
|
|
/// @return number of extra cycle ran in this iteration
|
|
|
|
#[inline]
|
|
|
|
fn run(&mut self, cycles_to_run: usize) -> usize {
|
|
|
|
let run_start_time = self.scheduler.timestamp();
|
|
|
|
|
|
|
|
// Register an event to mark the end of this run
|
|
|
|
self.scheduler
|
|
|
|
.push(EventType::RunLimitReached, cycles_to_run);
|
|
|
|
|
|
|
|
let mut running = true;
|
|
|
|
while running {
|
|
|
|
// The tricky part is to avoid unnecessary calls for Scheduler::process_pending,
|
|
|
|
// performance-wise it would be best to run as many cycles as fast as possible while we know there are no pending events.
|
|
|
|
// Fast forward emulation until an event occurs
|
|
|
|
while self.scheduler.timestamp() <= self.scheduler.timestamp_of_next_event() {
|
|
|
|
// 3 Options:
|
|
|
|
// 1. DMA is active - thus CPU is blocked
|
|
|
|
// 2. DMA inactive and halt state is RUN - CPU can run
|
|
|
|
// 3. DMA inactive and halt state is HALT - CPU is blocked
|
|
|
|
match self.get_bus_master() {
|
|
|
|
Some(BusMaster::Dma) => self.dma_step(),
|
|
|
|
Some(BusMaster::Cpu) => self.cpu_step(),
|
|
|
|
None => {
|
|
|
|
if self.io_devs.intc.irq_pending() {
|
|
|
|
self.io_devs.haltcnt = HaltState::Running;
|
|
|
|
} else {
|
|
|
|
self.scheduler.fast_forward_to_next();
|
|
|
|
let (event, cycles_late) = self
|
|
|
|
.scheduler
|
|
|
|
.pop_pending_event()
|
|
|
|
.unwrap_or_else(|| unreachable!());
|
|
|
|
self.handle_event(event, cycles_late, &mut running);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 22:28:59 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
while let Some((event, cycles_late)) = self.scheduler.pop_pending_event() {
|
|
|
|
self.handle_event(event, cycles_late, &mut running);
|
2020-04-09 22:28:59 +01:00
|
|
|
}
|
2019-07-30 22:52:46 +01:00
|
|
|
}
|
2020-04-09 22:28:59 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
let total_cycles_ran = self.scheduler.timestamp() - run_start_time;
|
|
|
|
total_cycles_ran - cycles_to_run
|
2019-07-20 21:02:18 +01:00
|
|
|
}
|
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
#[inline]
|
|
|
|
fn handle_event(&mut self, event: EventType, cycles_late: usize, running: &mut bool) {
|
|
|
|
let io = &mut (*self.io_devs);
|
|
|
|
match event {
|
|
|
|
EventType::RunLimitReached => {
|
|
|
|
*running = false;
|
|
|
|
}
|
|
|
|
EventType::DmaActivateChannel(channel_id) => io.dmac.activate_channel(channel_id),
|
|
|
|
EventType::TimerOverflow(channel_id) => {
|
|
|
|
let timers = &mut io.timers;
|
|
|
|
let dmac = &mut io.dmac;
|
|
|
|
let apu = &mut io.sound;
|
|
|
|
timers.handle_overflow_event(channel_id, cycles_late, apu, dmac);
|
|
|
|
}
|
|
|
|
EventType::Gpu(event) => io.gpu.on_event(
|
|
|
|
event,
|
|
|
|
cycles_late,
|
|
|
|
&mut *self.sysbus,
|
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
|
|
|
&self.video_device,
|
|
|
|
),
|
|
|
|
EventType::Apu(event) => io.sound.on_event(event, cycles_late, &self.audio_device),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn skip_bios(&mut self) {
|
|
|
|
self.cpu.skip_bios();
|
|
|
|
self.sysbus.io.gpu.skip_bios();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "debugger")]
|
2019-11-16 15:58:44 +00:00
|
|
|
pub fn add_breakpoint(&mut self, addr: u32) -> Option<usize> {
|
|
|
|
if !self.cpu.breakpoints.contains(&addr) {
|
|
|
|
let new_index = self.cpu.breakpoints.len();
|
|
|
|
self.cpu.breakpoints.push(addr);
|
|
|
|
Some(new_index)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
#[cfg(feature = "debugger")]
|
2019-11-16 15:58:44 +00:00
|
|
|
pub fn check_breakpoint(&self) -> Option<u32> {
|
|
|
|
let next_pc = self.cpu.get_next_pc();
|
|
|
|
for bp in &self.cpu.breakpoints {
|
2020-05-21 20:08:49 +01:00
|
|
|
if (*bp & !1) == next_pc {
|
|
|
|
return Some(*bp);
|
2019-11-16 15:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-05-21 20:08:49 +01:00
|
|
|
#[cfg(feature = "debugger")]
|
|
|
|
/// 'step' function that checks for breakpoints
|
|
|
|
/// TODO avoid code duplication
|
|
|
|
pub fn step_debugger(&mut self) -> Option<u32> {
|
|
|
|
// clear any pending DMAs
|
2020-10-17 14:22:31 +01:00
|
|
|
self.dma_step();
|
2020-05-21 20:08:49 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
// Run the CPU
|
|
|
|
let _cycles = self.scheduler.measure_cycles(|| {
|
|
|
|
self.cpu_step();
|
|
|
|
});
|
2020-05-21 20:08:49 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
let breakpoint = self.check_breakpoint();
|
2020-10-03 20:09:38 +01:00
|
|
|
|
2020-10-17 14:22:31 +01:00
|
|
|
while let Some((event, cycles_late)) = self.scheduler.pop_pending_event() {
|
|
|
|
self.handle_event(event, cycles_late, &mut running);
|
|
|
|
}
|
2020-05-21 20:08:49 +01:00
|
|
|
|
|
|
|
breakpoint
|
|
|
|
}
|
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
/// Query the emulator for the recently drawn framebuffer.
|
|
|
|
/// for use with implementations where the VideoInterface is not a viable option.
|
|
|
|
pub fn get_frame_buffer(&self) -> &[u32] {
|
|
|
|
self.sysbus.io.gpu.get_frame_buffer()
|
|
|
|
}
|
2020-05-15 11:22:58 +01:00
|
|
|
|
|
|
|
/// Reset the emulator
|
|
|
|
pub fn soft_reset(&mut self) {
|
2020-10-17 14:22:31 +01:00
|
|
|
self.cpu.reset();
|
2020-10-03 20:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:28:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2020-01-26 00:06:44 +00:00
|
|
|
use super::super::bus::Bus;
|
2020-01-31 12:24:41 +00:00
|
|
|
use super::super::cartridge::GamepakBuilder;
|
2020-01-16 23:28:11 +00:00
|
|
|
|
|
|
|
struct DummyInterface {}
|
|
|
|
|
|
|
|
impl DummyInterface {
|
|
|
|
fn new() -> DummyInterface {
|
|
|
|
DummyInterface {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:09:38 +01:00
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
2020-01-16 23:28:11 +00:00
|
|
|
impl VideoInterface for DummyInterface {}
|
|
|
|
impl AudioInterface for DummyInterface {}
|
|
|
|
impl InputInterface for DummyInterface {}
|
|
|
|
|
|
|
|
fn make_mock_gba(rom: &[u8]) -> GameBoyAdvance {
|
2020-02-24 22:11:10 +00:00
|
|
|
let bios = vec![0; 0x4000].into_boxed_slice();
|
2020-01-31 12:24:41 +00:00
|
|
|
let cartridge = GamepakBuilder::new()
|
|
|
|
.buffer(rom)
|
|
|
|
.with_sram()
|
|
|
|
.without_backup_to_file()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-01-16 23:28:11 +00:00
|
|
|
let dummy = Rc::new(RefCell::new(DummyInterface::new()));
|
2020-10-03 20:09:38 +01:00
|
|
|
let mut gba = GameBoyAdvance::new(
|
|
|
|
bios,
|
|
|
|
cartridge,
|
|
|
|
#[cfg(not(feature = "no_video_interface"))]
|
|
|
|
dummy.clone(),
|
|
|
|
dummy.clone(),
|
|
|
|
dummy.clone(),
|
|
|
|
);
|
2020-01-16 23:28:11 +00:00
|
|
|
gba.skip_bios();
|
|
|
|
|
|
|
|
gba
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arm7tdmi_arm_eggvance() {
|
2020-05-14 09:57:24 +01:00
|
|
|
let mut gba = make_mock_gba(include_bytes!("../../external/gba-suite/arm/arm.gba"));
|
2020-01-16 23:28:11 +00:00
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
gba.frame();
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:02:56 +00:00
|
|
|
let insn = gba.sysbus.read_32(gba.cpu.pc - 8);
|
2020-01-18 22:30:50 +00:00
|
|
|
assert_eq!(insn, 0xeafffffe); // loop
|
2020-01-16 23:28:11 +00:00
|
|
|
assert_eq!(0, gba.cpu.gpr[12]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arm7tdmi_thumb_eggvance() {
|
2020-05-14 09:57:24 +01:00
|
|
|
let mut gba = make_mock_gba(include_bytes!("../../external/gba-suite/thumb/thumb.gba"));
|
2020-01-16 23:28:11 +00:00
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
gba.frame();
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:02:56 +00:00
|
|
|
let insn = gba.sysbus.read_16(gba.cpu.pc - 4);
|
2020-01-18 22:30:50 +00:00
|
|
|
assert_eq!(insn, 0xe7fe); // loop
|
2020-01-16 23:28:11 +00:00
|
|
|
assert_eq!(0, gba.cpu.gpr[7]);
|
|
|
|
}
|
|
|
|
}
|