gb-emu/lib/src/processor/memory.rs

320 lines
11 KiB
Rust
Raw Normal View History

2023-02-13 12:42:38 +11:00
pub use self::rom::Rom;
2023-03-11 08:44:23 +11:00
use self::{
mmio::{
apu::ApuSaveState,
gpu::{Colour, GpuSaveState},
Apu, Gpu, Joypad, Serial, Timer,
},
rom::RomSaveState,
};
2023-02-26 11:56:45 +11:00
use crate::{
2023-03-03 18:08:28 +11:00
connect::{AudioOutput, JoypadState, Renderer},
2023-02-26 11:56:45 +11:00
processor::SplitRegister,
verbose_println, Cpu,
};
2023-02-07 09:09:52 +11:00
2023-02-24 10:41:09 +11:00
mod interrupts;
pub use interrupts::{Interrupt, Interrupts};
2023-03-11 08:44:23 +11:00
use serde::{Deserialize, Serialize};
2023-02-22 09:37:31 +11:00
pub mod mmio;
2023-02-07 09:19:50 +11:00
pub(crate) mod rom;
2023-02-06 20:54:26 +11:00
pub(crate) type Address = u16;
2023-03-08 11:01:18 +11:00
pub struct Memory<ColourFormat: From<Colour> + Clone> {
2023-02-22 10:26:20 +11:00
bootrom: Option<Vec<u8>>,
2023-02-12 09:46:47 +11:00
rom: Rom,
2023-02-07 09:13:45 +11:00
ram: [u8; 8192],
cpu_ram: [u8; 128],
pub(super) interrupts: Interrupts,
2023-02-06 20:54:26 +11:00
pub(super) ime: bool,
pub(super) ime_scheduled: u8,
2023-02-24 10:41:09 +11:00
dma_addr: u8,
2023-02-07 09:09:52 +11:00
joypad: Joypad,
2023-03-08 11:01:18 +11:00
gpu: Gpu<ColourFormat>,
2023-02-13 13:22:50 +11:00
apu: Apu,
2023-02-21 10:10:24 +11:00
serial: Serial,
2023-02-24 10:11:05 +11:00
timers: Timer,
2023-02-06 20:54:26 +11:00
}
2023-03-11 08:44:23 +11:00
#[serde_with::serde_as]
#[derive(Serialize, Deserialize)]
pub struct MemorySaveState<ColourFormat: From<Colour> + Clone> {
rom: RomSaveState,
#[serde_as(as = "[_; 8192]")]
ram: [u8; 8192],
#[serde_as(as = "[_; 128]")]
cpu_ram: [u8; 128],
pub(super) interrupts: Interrupts,
pub(super) ime: bool,
pub(super) ime_scheduled: u8,
dma_addr: u8,
joypad: Joypad,
gpu: GpuSaveState<ColourFormat>,
apu: ApuSaveState,
serial: Serial,
timers: Timer,
}
impl<ColourFormat: From<Colour> + Clone> MemorySaveState<ColourFormat> {
pub fn create(memory: &Memory<ColourFormat>) -> Self {
Self {
rom: RomSaveState::create(&memory.rom),
ram: memory.ram,
cpu_ram: memory.cpu_ram,
interrupts: memory.interrupts,
ime: memory.ime,
ime_scheduled: memory.ime_scheduled,
dma_addr: memory.dma_addr,
joypad: memory.joypad,
gpu: GpuSaveState::create(&memory.gpu),
apu: ApuSaveState::create(&memory.apu),
serial: memory.serial,
timers: memory.timers,
}
}
}
2023-03-08 11:01:18 +11:00
impl<ColourFormat: From<Colour> + Clone> Memory<ColourFormat> {
2023-02-22 15:24:31 +11:00
pub fn init(
bootrom: Option<Vec<u8>>,
rom: Rom,
2023-03-08 11:01:18 +11:00
window: Box<dyn Renderer<ColourFormat>>,
2023-03-03 18:08:28 +11:00
output: AudioOutput,
2023-02-22 15:24:31 +11:00
connect_serial: bool,
2023-03-08 11:01:18 +11:00
tile_window: Option<Box<dyn Renderer<ColourFormat>>>,
2023-02-22 15:24:31 +11:00
) -> Self {
let serial = if connect_serial {
Serial::default().connected()
} else {
Serial::default()
};
2023-02-06 20:54:26 +11:00
Self {
bootrom,
rom,
ram: [0x0; 8192],
cpu_ram: [0x0; 128],
2023-02-24 10:41:09 +11:00
interrupts: Interrupts::default(),
2023-02-06 20:54:26 +11:00
ime: false,
ime_scheduled: 0x0,
2023-02-24 10:41:09 +11:00
dma_addr: 0xFF,
2023-02-07 09:09:52 +11:00
joypad: Joypad::default(),
gpu: Gpu::new(window, tile_window),
2023-03-03 18:08:28 +11:00
apu: Apu::new(output),
serial,
2023-02-24 10:11:05 +11:00
timers: Timer::init(),
2023-02-06 20:54:26 +11:00
}
}
pub fn get(&self, address: Address) -> u8 {
match address {
0x0..0x8000 => {
// rom access
// todo - switchable rom banks
2023-02-22 10:26:20 +11:00
if let Some(bootrom) = &self.bootrom && (address as usize) < bootrom.len() {
bootrom[address as usize]
2023-02-06 20:54:26 +11:00
} else {
2023-02-12 09:41:34 +11:00
self.rom.get(address)
2023-02-06 20:54:26 +11:00
}
}
2023-02-22 15:24:31 +11:00
0x8000..0xA000 => self.gpu.vram.get(address),
2023-02-08 09:06:21 +11:00
0xA000..0xC000 => {
// cart ram
2023-02-11 21:43:36 +11:00
self.rom.get_ram(address)
2023-02-08 09:06:21 +11:00
}
2023-02-12 09:46:47 +11:00
0xC000..0xE000 => self.ram[(address - 0xC000) as usize],
0xE000..0xFE00 => self.ram[(address - 0xE000) as usize],
2023-02-22 15:24:31 +11:00
0xFE00..0xFEA0 => self.gpu.oam.get(address),
2023-02-12 09:46:47 +11:00
0xFEA0..0xFF00 => 0xFF,
2023-02-06 21:00:56 +11:00
0xFF00..0xFF4C => self.get_io(address),
2023-02-13 10:36:04 +11:00
0xFF4C..0xFF80 => 0xFF,
2023-02-12 09:46:47 +11:00
0xFF80..0xFFFF => self.cpu_ram[(address - 0xFF80) as usize],
2023-02-24 10:41:09 +11:00
0xFFFF => self.interrupts.get_enable_register(),
2023-02-06 20:54:26 +11:00
}
}
pub fn set(&mut self, address: Address, data: u8) {
match address {
0x0..0x8000 => {
// change this with MBC code...
2023-02-09 12:18:13 +11:00
self.rom.set(address, data);
2023-02-27 10:00:12 +11:00
if self.rom.can_rumble() {
// rumble
self.gpu.window.set_rumble(self.rom.is_rumbling())
}
2023-02-06 20:54:26 +11:00
}
2023-02-22 15:24:31 +11:00
0x8000..0xA000 => self.gpu.vram.set(address, data),
2023-02-21 10:10:24 +11:00
0xA000..0xC000 => self.rom.set_ram(address, data),
0xC000..0xE000 => self.ram[(address - 0xC000) as usize] = data,
0xE000..0xFE00 => self.ram[(address - 0xE000) as usize] = data,
2023-02-22 15:24:31 +11:00
0xFE00..0xFEA0 => self.gpu.oam.set(address, data),
2023-02-13 10:36:04 +11:00
0xFEA0..0xFF00 => {}
2023-02-21 10:10:24 +11:00
0xFF00..0xFF4C => self.set_io(address, data),
2023-02-22 10:26:20 +11:00
0xFF50 => self.bootrom = None,
2023-02-09 12:18:13 +11:00
0xFF4C..0xFF50 | 0xFF51..0xFF80 => {}
2023-02-21 10:10:24 +11:00
0xFF80..0xFFFF => self.cpu_ram[(address - 0xFF80) as usize] = data,
2023-02-06 20:54:26 +11:00
0xFFFF => {
verbose_println!("interrupts set to {:#b}", data);
verbose_println!(" / {:#X}", data);
2023-02-24 10:41:09 +11:00
self.interrupts.set_enable_register(data);
2023-02-06 20:54:26 +11:00
}
}
}
2023-02-06 21:00:56 +11:00
fn get_io(&self, address: Address) -> u8 {
2023-02-15 08:25:04 +11:00
// range: 0xFF00 - 0xFF4B inclusive
2023-02-13 13:22:50 +11:00
match address {
0xFF00 => self.joypad.as_register(),
2023-02-21 10:10:24 +11:00
0xFF01 => self.serial.get_queued(),
0xFF02 => self.serial.get_control(),
2023-02-24 10:11:05 +11:00
0xFF04 => self.timers.get_div(),
0xFF05 => self.timers.get_tima(),
0xFF06 => self.timers.get_tma(),
0xFF07 => self.timers.get_timer_control(),
2023-02-24 10:41:09 +11:00
0xFF0F => self.interrupts.get_flag_register(),
2023-02-13 13:22:50 +11:00
0xFF10..0xFF40 => self.apu.get_register(address),
2023-02-22 15:24:31 +11:00
0xFF40 => self.gpu.get_lcdc(),
0xFF41 => self.gpu.get_lcd_status(),
0xFF42 => self.gpu.get_scy(),
0xFF43 => self.gpu.get_scx(),
0xFF44 => self.gpu.get_ly(),
0xFF45 => self.gpu.get_lyc(),
2023-02-24 10:41:09 +11:00
0xFF46 => self.dma_addr,
2023-02-22 15:24:31 +11:00
0xFF47 => self.gpu.get_bg_palette(),
0xFF48 => self.gpu.get_obj_palette_0(),
0xFF49 => self.gpu.get_obj_palette_1(),
0xFF4A => self.gpu.get_wy(),
0xFF4B => self.gpu.get_wx(),
2023-02-24 10:41:09 +11:00
0xFF03 | 0xFF08..0xFF0F => 0xFF,
0x0..0xFF00 | 0xFF4C..=0xFFFF => panic!("passed wrong address to get_io"),
2023-02-06 21:00:56 +11:00
}
}
fn set_io(&mut self, address: Address, data: u8) {
2023-02-15 08:25:04 +11:00
// range: 0xFF00 - 0xFF4B inclusive
2023-02-24 10:41:09 +11:00
match address {
0xFF00 => {
// joypad
self.joypad.mmio_write(data);
}
0xFF01 => self.serial.update_queued(data),
0xFF02 => self.serial.update_control(data),
0xFF04 => self.timers.update_div(),
0xFF05 => self.timers.update_tima(data),
0xFF06 => self.timers.update_tma(data),
0xFF07 => self.timers.update_timer_control(data),
0xFF0F => self.interrupts.set_flag_register(data),
0xFF10..0xFF40 => self.apu.mmio_write(address, data),
0xFF40 => self.gpu.update_lcdc(data),
0xFF41 => self.gpu.update_lcd_status(data),
0xFF42 => self.gpu.update_scy(data),
0xFF43 => self.gpu.update_scx(data),
0xFF45 => self.gpu.update_lyc(data),
0xFF46 => {
if data > 0xDF {
panic!("dma transfer out of bounds: {data:#X}");
2023-02-09 12:14:55 +11:00
}
2023-02-24 10:41:09 +11:00
self.dma_addr = data;
let mut addr: u16 = 0x0;
addr.set_high(data);
for l in 0x0..0xA0 {
addr.set_low(l);
self.gpu.oam.data[l as usize] = self.get(addr);
2023-02-22 15:24:31 +11:00
}
2023-02-06 21:00:56 +11:00
}
2023-02-24 10:41:09 +11:00
0xFF47 => self.gpu.update_bg_palette(data),
0xFF48 => self.gpu.update_obj_palette_0(data),
0xFF49 => self.gpu.update_obj_palette_1(data),
0xFF4A => self.gpu.update_wy(data),
0xFF4B => self.gpu.update_wx(data),
0xFF03 | 0xFF08..0xFF0F | 0xFF44 => {
// read-only addresses
2023-03-03 09:35:56 +11:00
verbose_println!("BANNED write: {data:#X} to {address:#X}");
2023-02-24 10:41:09 +11:00
}
0x0..0xFF00 | 0xFF4C..=u16::MAX => panic!("passed wrong address to set_io"),
2023-02-06 21:00:56 +11:00
}
}
2023-02-07 09:09:52 +11:00
2023-02-26 11:56:45 +11:00
pub fn update_pressed_keys(&mut self, latest_state: JoypadState) -> bool {
self.joypad.update_pressed_keys(latest_state)
2023-02-09 17:30:38 +11:00
}
2023-02-12 08:51:23 +11:00
pub(super) fn cpu_ram_init(&mut self) {
self.set(0xFF04, 0xAD);
// self.set(0xFF10, 0x80);
// self.set(0xFF11, 0xBF);
// self.set(0xFF12, 0xF3);
// self.set(0xFF14, 0xBF);
// self.set(0xFF16, 0x3F);
// self.set(0xFF19, 0xBF);
// self.set(0xFF1A, 0x7F);
// self.set(0xFF1B, 0xFF);
// self.set(0xFF1C, 0x9F);
// self.set(0xFF1E, 0xBF);
// self.set(0xFF20, 0xFF);
// self.set(0xFF23, 0xBF);
// self.set(0xFF24, 0x77);
// self.set(0xFF25, 0xF3);
// self.set(0xFF26, 0xF1);
2023-02-12 08:51:23 +11:00
self.set(0xFF40, 0x91);
self.set(0xFF47, 0xFC);
self.set(0xFF48, 0xFF);
self.set(0xFF49, 0xFF);
for i in 0xC000..0xE000 {
self.set(i, if rand::random() { 0xFF } else { 0x00 });
}
}
2023-03-02 11:29:54 +11:00
pub fn flush_rom(&mut self) {
self.rom.flush();
}
2023-03-06 19:20:47 +11:00
pub fn is_audio_buffer_full(&self) -> bool {
self.apu.is_buffer_full()
}
pub fn replace_output(&mut self, new: AudioOutput) {
self.apu.replace_output(new);
}
2023-02-06 20:54:26 +11:00
}
2023-02-13 13:22:50 +11:00
2023-03-08 11:01:18 +11:00
impl<ColourFormat: From<Colour> + Clone> Cpu<ColourFormat> {
2023-02-24 10:11:05 +11:00
pub fn increment_timers(&mut self, machine_cycles: u8) {
let steps = (machine_cycles as usize) * 4;
let timer_return = self.memory.timers.tick(steps);
2023-02-24 10:41:09 +11:00
2023-02-24 10:11:05 +11:00
for _ in 0..timer_return.num_apu_ticks {
self.memory.apu.div_apu_tick();
}
if timer_return.timer_interrupt {
self.memory.interrupts.set_interrupt(Interrupt::Timer, true);
}
2023-02-24 10:11:05 +11:00
2023-02-21 10:10:24 +11:00
self.memory.apu.tick(steps);
2023-02-24 10:41:09 +11:00
let serial_interrupt = self.memory.serial.tick(steps);
self.memory
.interrupts
.set_interrupt(Interrupt::Serial, serial_interrupt);
2023-02-22 15:24:31 +11:00
let gpu_interrupts = self.memory.gpu.tick(steps);
2023-02-24 10:41:09 +11:00
self.memory
.interrupts
.set_interrupt(Interrupt::Vblank, gpu_interrupts.vblank);
self.memory
.interrupts
.set_interrupt(Interrupt::LcdStat, gpu_interrupts.lcd_stat);
2023-02-22 15:24:31 +11:00
2023-02-24 10:41:09 +11:00
if gpu_interrupts.vblank {
2023-02-26 11:56:45 +11:00
let latest_state = self.memory.gpu.window.latest_joypad_state();
let joypad_interrupt = self.memory.update_pressed_keys(latest_state);
self.memory
2023-02-24 10:41:09 +11:00
.interrupts
.set_interrupt(Interrupt::Joypad, joypad_interrupt);
2023-02-22 15:24:31 +11:00
}
2023-02-21 10:10:24 +11:00
}
}