2019-07-06 23:33:54 +01:00
|
|
|
/// Struct containing everything
|
2019-12-09 21:37:46 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
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;
|
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::*;
|
2019-12-22 23:30:36 +00:00
|
|
|
use super::sound::SoundController;
|
2019-07-06 13:53:36 +01:00
|
|
|
use super::sysbus::SysBus;
|
2019-12-04 23:15:49 +00:00
|
|
|
|
2020-05-01 16:13:00 +01:00
|
|
|
use super::{AudioInterface, InputInterface, VideoInterface};
|
2019-07-06 13:53:36 +01:00
|
|
|
|
2019-12-22 23:27:25 +00:00
|
|
|
pub struct GameBoyAdvance {
|
2019-11-08 23:43:43 +00:00
|
|
|
pub sysbus: Box<SysBus>,
|
2020-02-10 21:45:53 +00:00
|
|
|
pub cpu: arm7tdmi::Core,
|
2020-01-16 17:56:05 +00:00
|
|
|
|
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>>,
|
2020-01-11 13:58:32 +00:00
|
|
|
|
2020-02-21 12:04:39 +00:00
|
|
|
pub cycles_to_next_event: usize,
|
2020-04-09 22:28:59 +01:00
|
|
|
|
|
|
|
overshoot_cycles: usize,
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct SaveState {
|
|
|
|
sysbus: Box<SysBus>,
|
2020-02-10 21:45:53 +00:00
|
|
|
cpu: arm7tdmi::Core,
|
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,
|
2019-12-22 23:27:25 +00:00
|
|
|
video_device: Rc<RefCell<dyn VideoInterface>>,
|
|
|
|
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-01-16 17:56:05 +00:00
|
|
|
let gpu = Box::new(Gpu::new());
|
|
|
|
let sound_controller = Box::new(SoundController::new(
|
|
|
|
audio_device.borrow().get_sample_rate() as f32,
|
|
|
|
));
|
2019-12-29 19:30:33 +00:00
|
|
|
let io = IoDevices::new(gpu, sound_controller);
|
2020-02-10 21:45:53 +00:00
|
|
|
let sysbus = Box::new(SysBus::new(io, bios_rom, gamepak));
|
2020-04-04 11:50:50 +01:00
|
|
|
|
2020-02-10 21:45:53 +00:00
|
|
|
let cpu = arm7tdmi::Core::new();
|
|
|
|
|
2020-04-04 11:50:50 +01:00
|
|
|
let mut gba = GameBoyAdvance {
|
2019-07-06 13:53:36 +01:00
|
|
|
cpu: cpu,
|
2020-02-10 21:45:53 +00:00
|
|
|
sysbus: sysbus,
|
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
|
|
|
|
|
|
|
cycles_to_next_event: 1,
|
2020-04-09 22:28:59 +01:00
|
|
|
overshoot_cycles: 0,
|
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],
|
|
|
|
video_device: Rc<RefCell<dyn VideoInterface>>,
|
|
|
|
audio_device: Rc<RefCell<dyn AudioInterface>>,
|
|
|
|
input_device: Rc<RefCell<dyn InputInterface>>,
|
|
|
|
) -> bincode::Result<GameBoyAdvance> {
|
|
|
|
let decoded: Box<SaveState> = bincode::deserialize_from(savestate)?;
|
|
|
|
|
|
|
|
Ok(GameBoyAdvance {
|
|
|
|
cpu: decoded.cpu,
|
|
|
|
sysbus: decoded.sysbus,
|
|
|
|
|
|
|
|
video_device: video_device,
|
|
|
|
audio_device: audio_device,
|
|
|
|
input_device: input_device,
|
|
|
|
|
|
|
|
cycles_to_next_event: 1,
|
2020-04-09 22:51:23 +01:00
|
|
|
|
|
|
|
overshoot_cycles: 0,
|
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 {
|
|
|
|
cpu: self.cpu.clone(),
|
|
|
|
sysbus: self.sysbus.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
bincode::serialize(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn restore_state(&mut self, bytes: &[u8]) -> bincode::Result<()> {
|
|
|
|
let decoded: Box<SaveState> = bincode::deserialize_from(bytes)?;
|
|
|
|
|
|
|
|
self.cpu = decoded.cpu;
|
|
|
|
self.sysbus = decoded.sysbus;
|
|
|
|
self.cycles_to_next_event = 1;
|
|
|
|
|
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-04-09 22:28:59 +01:00
|
|
|
|
|
|
|
let mut remaining_cycles = 280896 - self.overshoot_cycles;
|
|
|
|
|
|
|
|
while remaining_cycles > 0 {
|
|
|
|
let cycles = self.step();
|
|
|
|
if remaining_cycles >= cycles {
|
|
|
|
remaining_cycles -= cycles;
|
|
|
|
} else {
|
|
|
|
self.overshoot_cycles = cycles - remaining_cycles;
|
|
|
|
return;
|
|
|
|
}
|
2019-07-30 22:52:46 +01:00
|
|
|
}
|
2020-04-09 22:28:59 +01:00
|
|
|
|
|
|
|
self.overshoot_cycles = 0;
|
2019-07-20 21:02:18 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_breakpoint(&self) -> Option<u32> {
|
|
|
|
let next_pc = self.cpu.get_next_pc();
|
|
|
|
for bp in &self.cpu.breakpoints {
|
|
|
|
if *bp == next_pc {
|
|
|
|
return Some(next_pc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:47:05 +00:00
|
|
|
pub fn skip_bios(&mut self) {
|
|
|
|
self.cpu.skip_bios();
|
|
|
|
self.sysbus.io.gpu.skip_bios();
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:04:39 +00:00
|
|
|
pub fn step_cpu(&mut self, io: &mut IoDevices) -> usize {
|
|
|
|
if io.intc.irq_pending() {
|
2020-01-11 13:58:32 +00:00
|
|
|
self.cpu.irq(&mut self.sysbus);
|
|
|
|
io.haltcnt = HaltState::Running;
|
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
let previous_cycles = self.cpu.cycles;
|
2020-01-11 13:58:32 +00:00
|
|
|
self.cpu.step(&mut self.sysbus);
|
|
|
|
self.cpu.cycles - previous_cycles
|
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
|
2020-04-09 22:28:59 +01:00
|
|
|
pub fn step(&mut self) -> usize {
|
2020-02-21 12:04:39 +00:00
|
|
|
// I hate myself for doing this, but rust left me no choice.
|
2019-11-08 23:43:43 +00:00
|
|
|
let io = unsafe {
|
|
|
|
let ptr = &mut *self.sysbus as *mut SysBus;
|
|
|
|
&mut (*ptr).io as &mut IoDevices
|
|
|
|
};
|
|
|
|
|
2020-01-11 13:58:32 +00:00
|
|
|
let mut irqs = IrqBitmask(0);
|
2019-11-08 23:43:43 +00:00
|
|
|
|
2020-01-11 13:58:32 +00:00
|
|
|
let mut cycles_left = self.cycles_to_next_event;
|
|
|
|
let mut cycles_to_next_event = std::usize::MAX;
|
|
|
|
let mut cycles = 0;
|
|
|
|
|
|
|
|
while cycles_left > 0 {
|
|
|
|
let mut irqs = IrqBitmask(0);
|
|
|
|
let _cycles = if !io.dmac.is_active() {
|
|
|
|
if HaltState::Running == io.haltcnt {
|
|
|
|
self.step_cpu(io)
|
|
|
|
} else {
|
|
|
|
cycles = cycles_left;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-12 16:55:26 +00:00
|
|
|
} else {
|
2020-01-11 13:58:32 +00:00
|
|
|
io.dmac.perform_work(&mut self.sysbus, &mut irqs);
|
|
|
|
io.intc.request_irqs(irqs);
|
2020-04-09 22:28:59 +01:00
|
|
|
return cycles;
|
2020-01-11 13:58:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cycles += _cycles;
|
|
|
|
if cycles_left < _cycles {
|
|
|
|
break;
|
2019-11-12 16:55:26 +00:00
|
|
|
}
|
2020-01-11 13:58:32 +00:00
|
|
|
cycles_left -= _cycles;
|
|
|
|
}
|
2019-08-05 07:44:27 +01:00
|
|
|
|
2020-01-11 13:58:32 +00:00
|
|
|
// update gpu & sound
|
|
|
|
io.timers.update(cycles, &mut self.sysbus, &mut irqs);
|
2020-04-09 22:28:59 +01:00
|
|
|
io.gpu.update(
|
2020-01-11 13:58:32 +00:00
|
|
|
cycles,
|
|
|
|
&mut self.sysbus,
|
|
|
|
&mut irqs,
|
|
|
|
&mut cycles_to_next_event,
|
2020-01-16 17:56:05 +00:00
|
|
|
&self.video_device,
|
2020-01-11 13:58:32 +00:00
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
io.sound
|
|
|
|
.update(cycles, &mut cycles_to_next_event, &self.audio_device);
|
2020-01-11 13:58:32 +00:00
|
|
|
self.cycles_to_next_event = cycles_to_next_event;
|
2019-11-11 01:35:16 +00:00
|
|
|
io.intc.request_irqs(irqs);
|
2020-04-09 22:28:59 +01:00
|
|
|
|
|
|
|
cycles
|
2019-07-11 16:17:28 +01:00
|
|
|
}
|
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) {
|
|
|
|
self.cpu.reset(&mut self.sysbus);
|
|
|
|
}
|
2019-07-06 13:53:36 +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 {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-10 23:52:18 +00:00
|
|
|
let mut gba =
|
|
|
|
GameBoyAdvance::new(bios, cartridge, 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-04-11 14:06:34 +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-01 15:58:15 +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]);
|
|
|
|
}
|
|
|
|
}
|