2019-07-06 23:33:54 +01:00
|
|
|
/// Struct containing everything
|
2019-08-05 07:44:27 +01:00
|
|
|
use super::arm7tdmi::{exception::Exception, Core, DecodedInstruction};
|
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-07-06 13:53:36 +01:00
|
|
|
use super::sysbus::SysBus;
|
2019-11-08 23:43:43 +00:00
|
|
|
|
2019-08-02 22:18:59 +01:00
|
|
|
use super::GBAResult;
|
2019-08-05 07:44:27 +01:00
|
|
|
use super::SyncedIoDevice;
|
2019-07-20 21:02:18 +01:00
|
|
|
use crate::backend::*;
|
2019-07-06 13:53:36 +01:00
|
|
|
|
|
|
|
pub struct GameBoyAdvance {
|
2019-11-08 23:43:43 +00:00
|
|
|
backend: Box<dyn EmulatorBackend>,
|
2019-07-06 13:53:36 +01:00
|
|
|
pub cpu: Core,
|
2019-11-08 23:43:43 +00:00
|
|
|
pub sysbus: Box<SysBus>,
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GameBoyAdvance {
|
2019-07-20 21:02:18 +01:00
|
|
|
pub fn new(
|
|
|
|
cpu: Core,
|
|
|
|
bios_rom: Vec<u8>,
|
|
|
|
gamepak: Cartridge,
|
2019-11-08 23:43:43 +00:00
|
|
|
backend: Box<dyn EmulatorBackend>,
|
2019-07-20 21:02:18 +01:00
|
|
|
) -> GameBoyAdvance {
|
2019-11-08 23:43:43 +00:00
|
|
|
let io = IoDevices::new();
|
2019-07-06 13:53:36 +01:00
|
|
|
GameBoyAdvance {
|
2019-07-20 21:02:18 +01:00
|
|
|
backend: backend,
|
2019-07-06 13:53:36 +01:00
|
|
|
cpu: cpu,
|
2019-11-08 23:43:43 +00:00
|
|
|
sysbus: Box::new(SysBus::new(io, bios_rom, gamepak)),
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 16:17:28 +01:00
|
|
|
pub fn frame(&mut self) {
|
2019-07-20 21:02:18 +01:00
|
|
|
self.update_key_state();
|
2019-11-08 23:43:43 +00:00
|
|
|
while self.sysbus.io.gpu.state != GpuState::VBlank {
|
|
|
|
self.step_new();
|
2019-07-15 05:35:09 +01:00
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
self.backend.render(self.sysbus.io.gpu.get_framebuffer());
|
|
|
|
while self.sysbus.io.gpu.state == GpuState::VBlank {
|
|
|
|
self.step_new();
|
2019-07-30 22:52:46 +01:00
|
|
|
}
|
2019-07-20 21:02:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_key_state(&mut self) {
|
2019-11-08 23:43:43 +00:00
|
|
|
self.sysbus.io.keyinput = self.backend.get_key_state();
|
2019-07-15 05:35:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
// TODO deprecate
|
|
|
|
pub fn step(&mut self) -> GBAResult<DecodedInstruction> {
|
2019-07-15 05:35:09 +01:00
|
|
|
let previous_cycles = self.cpu.cycles;
|
2019-11-08 23:43:43 +00:00
|
|
|
let executed_insn = self.cpu.step_one(&mut self.sysbus)?;
|
|
|
|
let cycles = self.cpu.cycles - previous_cycles;
|
|
|
|
Ok(executed_insn)
|
2019-07-15 05:35:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
pub fn step_new(&mut self) {
|
2019-08-05 07:44:27 +01:00
|
|
|
let mut irqs = IrqBitmask(0);
|
2019-11-08 23:43:43 +00:00
|
|
|
let previous_cycles = self.cpu.cycles;
|
|
|
|
|
|
|
|
// // I hate myself for doing this, but rust left me no choice.
|
|
|
|
let io = unsafe {
|
|
|
|
let ptr = &mut *self.sysbus as *mut SysBus;
|
|
|
|
&mut (*ptr).io as &mut IoDevices
|
|
|
|
};
|
|
|
|
|
|
|
|
if !io.dmac.perform_work(&mut self.sysbus, &mut irqs) {
|
|
|
|
self.cpu.step(&mut self.sysbus).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let cycles = self.cpu.cycles - previous_cycles;
|
2019-08-05 07:44:27 +01:00
|
|
|
|
|
|
|
io.timers.step(cycles, &mut self.sysbus, &mut irqs);
|
2019-11-08 23:43:43 +00:00
|
|
|
if let Some(new_gpu_state) = io.gpu.step(cycles, &mut self.sysbus, &mut irqs) {
|
|
|
|
match new_gpu_state {
|
|
|
|
GpuState::VBlank => io.dmac.notify_vblank(),
|
|
|
|
GpuState::HBlank => io.dmac.notify_hblank(),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 07:44:27 +01:00
|
|
|
|
|
|
|
if !self.cpu.cpsr.irq_disabled() {
|
|
|
|
io.intc.request_irqs(irqs);
|
|
|
|
if io.intc.irq_pending() {
|
2019-08-08 17:46:56 +01:00
|
|
|
self.cpu.exception(&mut self.sysbus, Exception::Irq);
|
2019-08-05 07:44:27 +01:00
|
|
|
}
|
2019-07-28 23:28:22 +01:00
|
|
|
}
|
2019-07-11 16:17:28 +01:00
|
|
|
}
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|