2023-02-07 09:19:50 +11:00
|
|
|
use self::rom::ROM;
|
2023-02-07 09:12:39 +11:00
|
|
|
use crate::{
|
|
|
|
processor::{clear_bit, get_bit},
|
|
|
|
verbose_println,
|
|
|
|
};
|
2023-02-07 09:09:52 +11:00
|
|
|
use minifb::Key;
|
2023-02-07 09:12:39 +11:00
|
|
|
use std::io::{stdout, Write};
|
2023-02-07 09:09:52 +11:00
|
|
|
|
2023-02-09 12:14:55 +11:00
|
|
|
use super::SplitRegister;
|
|
|
|
|
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-02-09 10:05:13 +11:00
|
|
|
#[derive(Debug)]
|
2023-02-07 09:09:52 +11:00
|
|
|
enum JoypadBank {
|
|
|
|
Action,
|
|
|
|
Direction,
|
|
|
|
}
|
|
|
|
|
2023-02-09 10:05:13 +11:00
|
|
|
#[derive(Debug)]
|
2023-02-07 09:09:52 +11:00
|
|
|
struct Joypad {
|
|
|
|
bank_sel: JoypadBank,
|
|
|
|
down: bool,
|
|
|
|
up: bool,
|
|
|
|
left: bool,
|
|
|
|
right: bool,
|
|
|
|
start: bool,
|
|
|
|
select: bool,
|
|
|
|
b: bool,
|
|
|
|
a: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Joypad {
|
|
|
|
fn as_register(&self) -> u8 {
|
|
|
|
let mut reg = 0xFF;
|
|
|
|
match self.bank_sel {
|
|
|
|
JoypadBank::Action => {
|
2023-02-09 10:05:13 +11:00
|
|
|
reg = clear_bit(reg, 5);
|
2023-02-07 09:09:52 +11:00
|
|
|
if self.start {
|
|
|
|
reg = clear_bit(reg, 3);
|
|
|
|
}
|
|
|
|
if self.select {
|
|
|
|
reg = clear_bit(reg, 2);
|
|
|
|
}
|
|
|
|
if self.b {
|
|
|
|
reg = clear_bit(reg, 1);
|
|
|
|
}
|
|
|
|
if self.a {
|
|
|
|
reg = clear_bit(reg, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JoypadBank::Direction => {
|
2023-02-09 10:05:13 +11:00
|
|
|
reg = clear_bit(reg, 4);
|
2023-02-07 09:09:52 +11:00
|
|
|
if self.down {
|
|
|
|
reg = clear_bit(reg, 3);
|
|
|
|
}
|
|
|
|
if self.up {
|
|
|
|
reg = clear_bit(reg, 2);
|
|
|
|
}
|
|
|
|
if self.left {
|
|
|
|
reg = clear_bit(reg, 1);
|
|
|
|
}
|
|
|
|
if self.right {
|
|
|
|
reg = clear_bit(reg, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Joypad {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
bank_sel: JoypadBank::Action,
|
|
|
|
down: false,
|
|
|
|
up: false,
|
|
|
|
left: false,
|
|
|
|
right: false,
|
|
|
|
start: false,
|
|
|
|
select: false,
|
|
|
|
b: false,
|
|
|
|
a: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 20:54:26 +11:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Memory {
|
2023-02-07 09:19:50 +11:00
|
|
|
bootrom: Vec<u8>,
|
2023-02-07 09:13:45 +11:00
|
|
|
bootrom_enabled: bool,
|
|
|
|
rom: ROM,
|
|
|
|
vram: [u8; 8192],
|
|
|
|
ram: [u8; 8192],
|
|
|
|
switchable_ram: [u8; 8192],
|
|
|
|
cpu_ram: [u8; 128],
|
|
|
|
oam: [u8; 160],
|
|
|
|
interrupts: u8,
|
2023-02-06 20:54:26 +11:00
|
|
|
pub(super) ime: bool,
|
|
|
|
pub(super) ime_scheduled: u8,
|
2023-02-07 09:13:45 +11:00
|
|
|
io: [u8; 76],
|
2023-02-06 20:54:26 +11:00
|
|
|
pub(super) user_mode: bool,
|
2023-02-07 09:09:52 +11:00
|
|
|
joypad: Joypad,
|
2023-02-06 20:54:26 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Memory {
|
2023-02-07 09:19:50 +11:00
|
|
|
pub fn init(bootrom: Vec<u8>, bootrom_enabled: bool, rom: ROM) -> Self {
|
2023-02-06 20:54:26 +11:00
|
|
|
Self {
|
|
|
|
bootrom,
|
|
|
|
bootrom_enabled,
|
|
|
|
rom,
|
|
|
|
vram: [0x0; 8192],
|
|
|
|
ram: [0x0; 8192],
|
|
|
|
switchable_ram: [0x0; 8192],
|
|
|
|
cpu_ram: [0x0; 128],
|
|
|
|
oam: [0x0; 160],
|
|
|
|
interrupts: 0x0,
|
|
|
|
ime: false,
|
|
|
|
ime_scheduled: 0x0,
|
|
|
|
io: [0xFF; 76],
|
|
|
|
user_mode: false,
|
2023-02-07 09:09:52 +11:00
|
|
|
joypad: Joypad::default(),
|
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
|
|
|
|
if self.bootrom_enabled && ((address as usize) < self.bootrom.len()) {
|
|
|
|
return self.bootrom[address as usize];
|
|
|
|
} else {
|
2023-02-07 09:19:50 +11:00
|
|
|
return self.rom.get(address);
|
2023-02-06 20:54:26 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
0x8000..0xA000 => {
|
|
|
|
return self.vram[(address - 0x8000) as usize];
|
|
|
|
}
|
2023-02-08 09:06:21 +11:00
|
|
|
0xA000..0xC000 => {
|
|
|
|
// cart ram
|
|
|
|
0xFF
|
|
|
|
}
|
2023-02-06 20:54:26 +11:00
|
|
|
0xC000..0xE000 => {
|
|
|
|
return self.ram[(address - 0xC000) as usize];
|
|
|
|
}
|
|
|
|
0xE000..0xFE00 => {
|
|
|
|
return self.ram[(address - 0xE000) as usize];
|
|
|
|
}
|
|
|
|
0xFE00..0xFEA0 => {
|
|
|
|
return self.oam[(address - 0xFE00) as usize];
|
|
|
|
}
|
|
|
|
0xFEA0..0xFF00 => {
|
2023-02-07 09:09:52 +11:00
|
|
|
return 0xFF;
|
2023-02-06 20:54:26 +11:00
|
|
|
}
|
2023-02-06 21:00:56 +11:00
|
|
|
0xFF00..0xFF4C => self.get_io(address),
|
2023-02-06 20:54:26 +11:00
|
|
|
0xFF4C..0xFF80 => {
|
|
|
|
// println!("empty space 2 read");
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
0xFF80..0xFFFF => {
|
|
|
|
return self.cpu_ram[(address - 0xFF80) as usize];
|
|
|
|
}
|
|
|
|
0xFFFF => {
|
|
|
|
return self.interrupts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, address: Address, data: u8) {
|
|
|
|
// verbose_println!("write addr: {:#X}, data: {:#X}", address, data);
|
|
|
|
|
|
|
|
match address {
|
|
|
|
0x0..0x8000 => {
|
|
|
|
// change this with MBC code...
|
|
|
|
// println!("tried to write {:#5X} at {:#X}", data, address);
|
|
|
|
}
|
|
|
|
0x8000..0xA000 => {
|
|
|
|
self.vram[(address - 0x8000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xA000..0xC000 => {
|
|
|
|
// panic!("switchable write");
|
|
|
|
// self.switchable_ram[(address - 0xA000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xC000..0xE000 => {
|
|
|
|
self.ram[(address - 0xC000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xE000..0xFE00 => {
|
|
|
|
self.ram[(address - 0xE000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xFE00..0xFEA0 => {
|
|
|
|
self.oam[(address - 0xFE00) as usize] = data;
|
|
|
|
}
|
|
|
|
0xFEA0..0xFF00 => {
|
|
|
|
// println!("empty space write: {:#X} to addr {:#X}", data, address);
|
|
|
|
}
|
2023-02-06 21:00:56 +11:00
|
|
|
0xFF00..0xFF4C => {
|
|
|
|
self.set_io(address, data);
|
2023-02-06 20:54:26 +11:00
|
|
|
// verbose_print!("writing to addr {:#X}\r", address);
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
}
|
|
|
|
0xFF50 => {
|
|
|
|
self.bootrom_enabled = false;
|
|
|
|
}
|
|
|
|
0xFF4C..0xFF50 | 0xFF51..0xFF80 => {
|
|
|
|
// println!("empty space 2 write: {:#X} to addr {:#X}", data, address);
|
|
|
|
}
|
|
|
|
0xFF80..0xFFFF => {
|
|
|
|
self.cpu_ram[(address - 0xFF80) as usize] = data;
|
|
|
|
}
|
|
|
|
0xFFFF => {
|
|
|
|
verbose_println!("interrupts set to {:#b}", data);
|
|
|
|
verbose_println!(" / {:#X}", data);
|
|
|
|
self.interrupts = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 21:00:56 +11:00
|
|
|
|
|
|
|
fn get_io(&self, address: Address) -> u8 {
|
|
|
|
if address == 0xFF00 {
|
2023-02-07 09:09:52 +11:00
|
|
|
return self.joypad.as_register();
|
2023-02-06 21:00:56 +11:00
|
|
|
}
|
|
|
|
return self.io[(address - 0xFF00) as usize];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_io(&mut self, address: Address, data: u8) {
|
|
|
|
let addr_l = (address - 0xFF00) as usize;
|
|
|
|
if !self.user_mode {
|
|
|
|
self.io[addr_l] = data;
|
|
|
|
} else {
|
|
|
|
match address {
|
|
|
|
0xFF02 => {
|
|
|
|
if data == 0x81 {
|
|
|
|
print!("{}", self.get(0xFF01) as char);
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
0xFF04 => self.io[addr_l] = 0,
|
2023-02-07 08:38:21 +11:00
|
|
|
0xFF00 => {
|
|
|
|
// joypad
|
2023-02-07 09:09:52 +11:00
|
|
|
if !get_bit(data, 5) {
|
|
|
|
self.joypad.bank_sel = JoypadBank::Action
|
|
|
|
}
|
|
|
|
if !get_bit(data, 4) {
|
|
|
|
self.joypad.bank_sel = JoypadBank::Direction
|
|
|
|
}
|
2023-02-07 08:38:21 +11:00
|
|
|
}
|
|
|
|
0xFF11 | 0xFF14 | 0xFF16 | 0xFF19 | 0xFF1E | 0xFF23 | 0xFF26 => {
|
|
|
|
// sound
|
2023-02-09 10:05:13 +11:00
|
|
|
self.io[addr_l] = data;
|
2023-02-07 08:38:21 +11:00
|
|
|
}
|
|
|
|
0xFF41 => {
|
2023-02-09 10:05:13 +11:00
|
|
|
// mixed read/write
|
2023-02-09 11:10:33 +11:00
|
|
|
self.io[addr_l] = (self.io[addr_l] & 0b00000111) | (data & 0b11111000)
|
2023-02-06 21:10:13 +11:00
|
|
|
}
|
2023-02-07 08:38:21 +11:00
|
|
|
0xFF4D | 0xFF56 => {
|
|
|
|
// cgb only
|
2023-02-09 10:05:13 +11:00
|
|
|
self.io[addr_l] = data;
|
2023-02-07 08:38:21 +11:00
|
|
|
}
|
2023-02-06 21:10:13 +11:00
|
|
|
0xFF44 | 0xFF76 | 0xFF77 => {
|
|
|
|
// read-only addresses
|
|
|
|
println!("BANNED write: {:#X} to {:#X}", data, address);
|
|
|
|
}
|
2023-02-09 12:14:55 +11:00
|
|
|
0xFF46 => {
|
|
|
|
if data > 0xDF {
|
|
|
|
panic!("dma transfer out of bounds: {:#X}", data);
|
|
|
|
}
|
|
|
|
let mut addr: u16 = 0x0;
|
|
|
|
addr.set_high(data);
|
|
|
|
for l in 0x0..0xA0 {
|
|
|
|
addr.set_low(l);
|
|
|
|
self.oam[l as usize] = self.get(addr);
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 21:00:56 +11:00
|
|
|
_ => {
|
|
|
|
self.io[addr_l] = data;
|
|
|
|
// panic!("passed non-io address to io handler!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 09:09:52 +11:00
|
|
|
|
|
|
|
pub fn update_pressed_keys(&mut self, keys: Vec<Key>) {
|
|
|
|
self.joypad.down = keys.contains(&Key::Down) || keys.contains(&Key::S);
|
|
|
|
self.joypad.up = keys.contains(&Key::Up) || keys.contains(&Key::W);
|
|
|
|
self.joypad.left = keys.contains(&Key::Left) || keys.contains(&Key::A);
|
|
|
|
self.joypad.right = keys.contains(&Key::Right) || keys.contains(&Key::D);
|
|
|
|
self.joypad.start = keys.contains(&Key::Equal);
|
|
|
|
self.joypad.select = keys.contains(&Key::Minus);
|
|
|
|
self.joypad.a = keys.contains(&Key::Apostrophe);
|
|
|
|
self.joypad.b = keys.contains(&Key::Semicolon);
|
|
|
|
}
|
2023-02-06 20:54:26 +11:00
|
|
|
}
|