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;
|
|
|
|
|
2019-11-12 16:55:26 +00:00
|
|
|
use super::arm7tdmi::Core;
|
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
|
|
|
|
|
|
|
use super::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>,
|
2019-12-09 21:37:46 +00:00
|
|
|
pub cpu: Core,
|
2019-12-22 23:27:25 +00:00
|
|
|
input_device: Rc<RefCell<dyn InputInterface>>,
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:27:25 +00:00
|
|
|
impl GameBoyAdvance {
|
2019-12-09 21:37:46 +00:00
|
|
|
pub fn new(
|
|
|
|
cpu: Core,
|
|
|
|
bios_rom: Vec<u8>,
|
|
|
|
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 {
|
2019-12-29 21:40:11 +00:00
|
|
|
let gpu = Box::new(Gpu::new(video_device));
|
|
|
|
let sound_controller = Box::new(SoundController::new(audio_device));
|
2019-12-29 19:30:33 +00:00
|
|
|
let io = IoDevices::new(gpu, sound_controller);
|
2019-07-06 13:53:36 +01:00
|
|
|
GameBoyAdvance {
|
|
|
|
cpu: cpu,
|
2019-11-08 23:43:43 +00:00
|
|
|
sysbus: Box::new(SysBus::new(io, bios_rom, gamepak)),
|
2019-12-09 21:37:46 +00:00
|
|
|
input_device: input_device,
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2019-12-29 21:03:57 +00:00
|
|
|
self.sysbus.io.gpu.clear();
|
2019-12-29 19:30:33 +00:00
|
|
|
while self.sysbus.io.gpu.vcount != DISPLAY_HEIGHT {
|
2019-12-09 21:37:46 +00:00
|
|
|
self.step();
|
2019-07-15 05:35:09 +01:00
|
|
|
}
|
2019-12-29 19:30:33 +00:00
|
|
|
while self.sysbus.io.gpu.vcount == DISPLAY_HEIGHT {
|
2019-12-09 21:37:46 +00:00
|
|
|
self.step();
|
2019-07-30 22:52:46 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-09 21:37:46 +00:00
|
|
|
pub fn step(&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
|
|
|
|
};
|
|
|
|
|
2019-12-27 10:37:32 +00:00
|
|
|
let cycles = if !io.dmac.has_work() {
|
2019-11-16 16:01:41 +00:00
|
|
|
if io.intc.irq_pending()
|
|
|
|
&& self.cpu.last_executed.is_some()
|
|
|
|
&& !self.cpu.did_pipeline_flush()
|
|
|
|
{
|
2019-11-11 01:35:16 +00:00
|
|
|
self.cpu.irq(&mut self.sysbus);
|
2019-11-12 16:55:26 +00:00
|
|
|
io.haltcnt = HaltState::Running;
|
2019-11-11 01:35:16 +00:00
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
|
2019-11-12 16:55:26 +00:00
|
|
|
if HaltState::Running == io.haltcnt {
|
|
|
|
self.cpu.step(&mut self.sysbus).unwrap();
|
|
|
|
self.cpu.cycles - previous_cycles
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-27 10:37:32 +00:00
|
|
|
io.dmac.perform_work(&mut self.sysbus, &mut irqs);
|
2019-11-12 16:55:26 +00:00
|
|
|
0
|
|
|
|
};
|
2019-08-05 07:44:27 +01:00
|
|
|
|
2019-12-21 18:19:43 +00:00
|
|
|
io.timers.step(cycles, &mut self.sysbus, &mut irqs);
|
2019-12-20 17:18:16 +00:00
|
|
|
|
2019-12-27 14:31:18 +00:00
|
|
|
io.gpu.step(cycles, &mut self.sysbus, &mut irqs);
|
2019-08-05 07:44:27 +01:00
|
|
|
|
2019-11-11 01:35:16 +00:00
|
|
|
io.intc.request_irqs(irqs);
|
2019-12-22 23:30:36 +00:00
|
|
|
io.sound.update(self.cpu.cycles);
|
2019-07-11 16:17:28 +01:00
|
|
|
}
|
2019-07-06 13:53:36 +01:00
|
|
|
}
|