2019-08-07 07:50:33 +01:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops::Add;
|
2019-07-15 05:30:52 +01:00
|
|
|
|
2019-08-05 07:44:27 +01:00
|
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
2019-07-02 23:40:08 +01:00
|
|
|
|
2019-08-07 07:50:33 +01:00
|
|
|
use super::arm7tdmi::bus::Bus;
|
2019-06-30 14:59:19 +01:00
|
|
|
use super::arm7tdmi::Addr;
|
2019-11-08 23:43:43 +00:00
|
|
|
use super::cartridge::Cartridge;
|
2019-08-07 07:50:33 +01:00
|
|
|
use super::gpu::GpuState;
|
2019-11-08 23:43:43 +00:00
|
|
|
use super::iodev::IoDevices;
|
2019-06-25 00:10:09 +01:00
|
|
|
|
|
|
|
const VIDEO_RAM_SIZE: usize = 128 * 1024;
|
|
|
|
const WORK_RAM_SIZE: usize = 256 * 1024;
|
2019-07-28 23:28:22 +01:00
|
|
|
const INTERNAL_RAM_SIZE: usize = 32 * 1024;
|
2019-07-01 15:45:29 +01:00
|
|
|
const PALETTE_RAM_SIZE: usize = 1 * 1024;
|
2019-06-25 00:10:09 +01:00
|
|
|
const OAM_SIZE: usize = 1 * 1024;
|
|
|
|
|
2019-08-07 07:50:33 +01:00
|
|
|
pub const BIOS_ADDR: u32 = 0x0000_0000;
|
|
|
|
pub const EWRAM_ADDR: u32 = 0x0200_0000;
|
|
|
|
pub const IWRAM_ADDR: u32 = 0x0300_0000;
|
|
|
|
pub const IOMEM_ADDR: u32 = 0x0400_0000;
|
|
|
|
pub const PALRAM_ADDR: u32 = 0x0500_0000;
|
|
|
|
pub const VRAM_ADDR: u32 = 0x0600_0000;
|
|
|
|
pub const OAM_ADDR: u32 = 0x0700_0000;
|
|
|
|
pub const GAMEPAK_WS0_ADDR: u32 = 0x0800_0000;
|
2019-11-16 16:09:37 +00:00
|
|
|
pub const GAMEPAK_MIRROR_WS0_ADDR: u32 = 0x0900_0000;
|
2019-08-07 07:50:33 +01:00
|
|
|
pub const GAMEPAK_WS1_ADDR: u32 = 0x0A00_0000;
|
|
|
|
pub const GAMEPAK_WS2_ADDR: u32 = 0x0C00_0000;
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum MemoryAccessType {
|
|
|
|
NonSeq,
|
|
|
|
Seq,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for MemoryAccessType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
match self {
|
|
|
|
MemoryAccessType::NonSeq => "N",
|
|
|
|
MemoryAccessType::Seq => "S",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
pub enum MemoryAccessWidth {
|
|
|
|
MemoryAccess8,
|
|
|
|
MemoryAccess16,
|
|
|
|
MemoryAccess32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<MemoryAccessWidth> for MemoryAccessType {
|
|
|
|
type Output = MemoryAccess;
|
|
|
|
|
|
|
|
fn add(self, other: MemoryAccessWidth) -> Self::Output {
|
|
|
|
MemoryAccess(self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct MemoryAccess(pub MemoryAccessType, pub MemoryAccessWidth);
|
|
|
|
|
|
|
|
impl fmt::Display for MemoryAccess {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}-Cycle ({:?})", self.0, self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 00:10:09 +01:00
|
|
|
#[derive(Debug)]
|
2019-07-28 23:28:22 +01:00
|
|
|
pub struct BoxedMemory {
|
2019-08-02 15:58:56 +01:00
|
|
|
pub mem: Box<[u8]>,
|
2019-07-28 23:28:22 +01:00
|
|
|
}
|
2019-07-02 23:26:48 +01:00
|
|
|
|
|
|
|
impl BoxedMemory {
|
2019-11-08 22:55:09 +00:00
|
|
|
pub fn new(boxed_slice: Box<[u8]>) -> BoxedMemory {
|
|
|
|
BoxedMemory { mem: boxed_slice }
|
2019-07-02 23:26:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-25 00:10:09 +01:00
|
|
|
|
2019-07-01 15:45:29 +01:00
|
|
|
impl Bus for BoxedMemory {
|
2019-07-15 05:30:52 +01:00
|
|
|
fn read_32(&self, addr: Addr) -> u32 {
|
2019-11-08 22:55:09 +00:00
|
|
|
(&self.mem[addr as usize..])
|
2019-07-15 05:30:52 +01:00
|
|
|
.read_u32::<LittleEndian>()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_16(&self, addr: Addr) -> u16 {
|
2019-11-08 22:55:09 +00:00
|
|
|
(&self.mem[addr as usize..])
|
2019-07-15 05:30:52 +01:00
|
|
|
.read_u16::<LittleEndian>()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_8(&self, addr: Addr) -> u8 {
|
2019-11-08 22:55:09 +00:00
|
|
|
(&self.mem[addr as usize..])[0]
|
2019-07-15 05:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_32(&mut self, addr: Addr, value: u32) {
|
2019-11-08 22:55:09 +00:00
|
|
|
(&mut self.mem[addr as usize..])
|
2019-07-15 05:30:52 +01:00
|
|
|
.write_u32::<LittleEndian>(value)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_16(&mut self, addr: Addr, value: u16) {
|
2019-11-08 22:55:09 +00:00
|
|
|
(&mut self.mem[addr as usize..])
|
2019-07-15 05:30:52 +01:00
|
|
|
.write_u16::<LittleEndian>(value)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_8(&mut self, addr: Addr, value: u8) {
|
2019-11-08 22:55:09 +00:00
|
|
|
(&mut self.mem[addr as usize..]).write_u8(value).unwrap()
|
2019-07-15 05:30:52 +01:00
|
|
|
}
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 23:56:50 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct DummyBus([u8; 4]);
|
|
|
|
|
|
|
|
impl Bus for DummyBus {
|
2019-07-05 13:34:52 +01:00
|
|
|
fn read_32(&self, _addr: Addr) -> u32 {
|
2019-07-03 23:56:50 +01:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:34:52 +01:00
|
|
|
fn read_16(&self, _addr: Addr) -> u16 {
|
2019-07-03 23:56:50 +01:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:34:52 +01:00
|
|
|
fn read_8(&self, _addr: Addr) -> u8 {
|
2019-07-03 23:56:50 +01:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-07-15 05:30:52 +01:00
|
|
|
fn write_32(&mut self, _addr: Addr, _value: u32) {}
|
2019-07-03 23:56:50 +01:00
|
|
|
|
2019-07-15 05:30:52 +01:00
|
|
|
fn write_16(&mut self, _addr: Addr, _value: u16) {}
|
|
|
|
|
|
|
|
fn write_8(&mut self, _addr: Addr, _value: u8) {}
|
2019-07-03 23:56:50 +01:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SysBus {
|
2019-11-08 23:43:43 +00:00
|
|
|
pub io: IoDevices,
|
2019-08-05 07:44:27 +01:00
|
|
|
|
2019-07-01 15:45:29 +01:00
|
|
|
bios: BoxedMemory,
|
|
|
|
onboard_work_ram: BoxedMemory,
|
|
|
|
internal_work_ram: BoxedMemory,
|
2019-08-02 15:58:56 +01:00
|
|
|
pub palette_ram: BoxedMemory,
|
|
|
|
pub vram: BoxedMemory,
|
|
|
|
pub oam: BoxedMemory,
|
2019-07-02 23:40:08 +01:00
|
|
|
gamepak: Cartridge,
|
2019-07-03 23:56:50 +01:00
|
|
|
dummy: DummyBus,
|
2019-11-16 16:09:37 +00:00
|
|
|
|
|
|
|
pub trace_access: bool,
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SysBus {
|
2019-11-08 23:43:43 +00:00
|
|
|
pub fn new(io: IoDevices, bios_rom: Vec<u8>, gamepak: Cartridge) -> SysBus {
|
2019-07-01 15:45:29 +01:00
|
|
|
SysBus {
|
2019-08-05 07:44:27 +01:00
|
|
|
io: io,
|
|
|
|
|
2019-11-08 22:55:09 +00:00
|
|
|
bios: BoxedMemory::new(bios_rom.into_boxed_slice()),
|
|
|
|
onboard_work_ram: BoxedMemory::new(vec![0; WORK_RAM_SIZE].into_boxed_slice()),
|
|
|
|
internal_work_ram: BoxedMemory::new(vec![0; INTERNAL_RAM_SIZE].into_boxed_slice()),
|
|
|
|
palette_ram: BoxedMemory::new(vec![0; PALETTE_RAM_SIZE].into_boxed_slice()),
|
|
|
|
vram: BoxedMemory::new(vec![0; VIDEO_RAM_SIZE].into_boxed_slice()),
|
|
|
|
oam: BoxedMemory::new(vec![0; OAM_SIZE].into_boxed_slice()),
|
2019-07-02 23:40:08 +01:00
|
|
|
gamepak: gamepak,
|
2019-07-03 23:56:50 +01:00
|
|
|
dummy: DummyBus([0; 4]),
|
2019-11-16 16:09:37 +00:00
|
|
|
|
|
|
|
trace_access: false,
|
2019-07-01 15:45:29 +01:00
|
|
|
}
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
fn map(&self, addr: Addr) -> (&dyn Bus, Addr) {
|
2019-11-08 22:55:09 +00:00
|
|
|
let ofs = addr & 0x00ff_ffff;
|
2019-08-07 07:50:33 +01:00
|
|
|
match addr & 0xff000000 {
|
2019-11-16 16:09:37 +00:00
|
|
|
BIOS_ADDR => {
|
|
|
|
if ofs >= 0x4000 {
|
|
|
|
(&self.dummy, ofs) // TODO return last fetched opcode
|
|
|
|
} else {
|
|
|
|
(&self.bios, ofs)
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 22:55:09 +00:00
|
|
|
EWRAM_ADDR => (&self.onboard_work_ram, ofs & 0x3_ffff),
|
|
|
|
IWRAM_ADDR => (&self.internal_work_ram, ofs & 0x7fff),
|
2019-11-08 23:43:43 +00:00
|
|
|
IOMEM_ADDR => (&self.io, {
|
2019-11-08 22:55:09 +00:00
|
|
|
if ofs & 0xffff == 0x8000 {
|
|
|
|
0x800
|
|
|
|
} else {
|
|
|
|
ofs & 0x7ff
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
PALRAM_ADDR => (&self.palette_ram, ofs & 0x3ff),
|
|
|
|
VRAM_ADDR => (&self.vram, {
|
|
|
|
let mut ofs = ofs & ((VIDEO_RAM_SIZE as u32) - 1);
|
|
|
|
if ofs > 0x18000 {
|
|
|
|
ofs -= 0x8000;
|
|
|
|
}
|
|
|
|
ofs
|
|
|
|
}),
|
|
|
|
OAM_ADDR => (&self.oam, ofs & 0x3ff),
|
2019-11-16 16:09:37 +00:00
|
|
|
GAMEPAK_WS0_ADDR | GAMEPAK_MIRROR_WS0_ADDR | GAMEPAK_WS1_ADDR | GAMEPAK_WS2_ADDR => {
|
|
|
|
(&self.gamepak, addr & 0x01ff_ffff)
|
|
|
|
}
|
2019-11-08 22:55:09 +00:00
|
|
|
_ => (&self.dummy, ofs),
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:45:29 +01:00
|
|
|
/// TODO proc-macro for generating this function
|
2019-11-08 23:43:43 +00:00
|
|
|
fn map_mut(&mut self, addr: Addr) -> (&mut dyn Bus, Addr) {
|
2019-11-08 22:55:09 +00:00
|
|
|
let ofs = addr & 0x00ff_ffff;
|
2019-08-07 07:50:33 +01:00
|
|
|
match addr & 0xff000000 {
|
2019-11-16 16:09:37 +00:00
|
|
|
BIOS_ADDR => (&mut self.dummy, ofs),
|
2019-11-08 22:55:09 +00:00
|
|
|
EWRAM_ADDR => (&mut self.onboard_work_ram, ofs & 0x3_ffff),
|
|
|
|
IWRAM_ADDR => (&mut self.internal_work_ram, ofs & 0x7fff),
|
2019-11-08 23:43:43 +00:00
|
|
|
IOMEM_ADDR => (&mut self.io, {
|
2019-11-08 22:55:09 +00:00
|
|
|
if ofs & 0xffff == 0x8000 {
|
|
|
|
0x800
|
|
|
|
} else {
|
|
|
|
ofs & 0x7ff
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
PALRAM_ADDR => (&mut self.palette_ram, ofs & 0x3ff),
|
|
|
|
VRAM_ADDR => (&mut self.vram, {
|
|
|
|
let mut ofs = ofs & ((VIDEO_RAM_SIZE as u32) - 1);
|
|
|
|
if ofs > 0x18000 {
|
|
|
|
ofs -= 0x8000;
|
|
|
|
}
|
|
|
|
ofs
|
|
|
|
}),
|
|
|
|
OAM_ADDR => (&mut self.oam, ofs & 0x3ff),
|
2019-11-16 16:09:37 +00:00
|
|
|
GAMEPAK_WS0_ADDR | GAMEPAK_MIRROR_WS0_ADDR | GAMEPAK_WS1_ADDR | GAMEPAK_WS2_ADDR => {
|
|
|
|
(&mut self.gamepak, addr & 0x01ff_ffff)
|
|
|
|
}
|
2019-11-08 22:55:09 +00:00
|
|
|
_ => (&mut self.dummy, ofs),
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 07:50:33 +01:00
|
|
|
|
|
|
|
pub fn get_cycles(&self, addr: Addr, access: MemoryAccess) -> usize {
|
|
|
|
let nonseq_cycles = [4, 3, 2, 8];
|
|
|
|
let seq_cycles = [2, 1];
|
|
|
|
|
|
|
|
let mut cycles = 0;
|
|
|
|
|
|
|
|
// TODO handle EWRAM accesses
|
|
|
|
match addr & 0xff000000 {
|
2019-08-08 17:46:56 +01:00
|
|
|
EWRAM_ADDR => match access.1 {
|
|
|
|
MemoryAccessWidth::MemoryAccess32 => cycles += 6,
|
|
|
|
_ => cycles += 3,
|
|
|
|
},
|
2019-08-07 07:50:33 +01:00
|
|
|
OAM_ADDR | VRAM_ADDR | PALRAM_ADDR => {
|
|
|
|
match access.1 {
|
|
|
|
MemoryAccessWidth::MemoryAccess32 => cycles += 2,
|
2019-08-08 17:46:56 +01:00
|
|
|
_ => cycles += 1,
|
2019-08-07 07:50:33 +01:00
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
if self.io.gpu.state == GpuState::HDraw {
|
2019-08-07 07:50:33 +01:00
|
|
|
cycles += 1;
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 16:09:37 +00:00
|
|
|
GAMEPAK_WS0_ADDR | GAMEPAK_MIRROR_WS0_ADDR => match access.0 {
|
2019-08-08 17:46:56 +01:00
|
|
|
MemoryAccessType::NonSeq => match access.1 {
|
|
|
|
MemoryAccessWidth::MemoryAccess32 => {
|
2019-11-08 23:43:43 +00:00
|
|
|
cycles += nonseq_cycles[self.io.waitcnt.ws0_first_access() as usize];
|
|
|
|
cycles += seq_cycles[self.io.waitcnt.ws0_second_access() as usize];
|
2019-08-08 17:46:56 +01:00
|
|
|
}
|
|
|
|
_ => {
|
2019-11-08 23:43:43 +00:00
|
|
|
cycles += nonseq_cycles[self.io.waitcnt.ws0_first_access() as usize];
|
2019-08-07 07:50:33 +01:00
|
|
|
}
|
2019-08-08 17:46:56 +01:00
|
|
|
},
|
|
|
|
MemoryAccessType::Seq => {
|
2019-11-08 23:43:43 +00:00
|
|
|
cycles += seq_cycles[self.io.waitcnt.ws0_second_access() as usize];
|
2019-08-08 17:46:56 +01:00
|
|
|
if access.1 == MemoryAccessWidth::MemoryAccess32 {
|
2019-11-08 23:43:43 +00:00
|
|
|
cycles += seq_cycles[self.io.waitcnt.ws0_second_access() as usize];
|
2019-08-07 07:50:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
GAMEPAK_WS1_ADDR | GAMEPAK_WS2_ADDR => {
|
|
|
|
panic!("unimplemented - need to refactor code with a nice macro :(")
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
cycles
|
|
|
|
}
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bus for SysBus {
|
|
|
|
fn read_32(&self, addr: Addr) -> u32 {
|
2019-11-08 22:55:09 +00:00
|
|
|
let (dev, addr) = self.map(addr);
|
2019-11-16 16:09:37 +00:00
|
|
|
dev.read_32(addr & 0x1ff_fffc)
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_16(&self, addr: Addr) -> u16 {
|
2019-11-16 16:09:37 +00:00
|
|
|
if self.trace_access {
|
|
|
|
println!("[TRACE] read_32 addr={:x}", addr);
|
|
|
|
}
|
2019-11-08 22:55:09 +00:00
|
|
|
let (dev, addr) = self.map(addr);
|
2019-11-16 16:09:37 +00:00
|
|
|
dev.read_16(addr & 0x1ff_fffe)
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_8(&self, addr: Addr) -> u8 {
|
2019-11-16 16:09:37 +00:00
|
|
|
if self.trace_access {
|
|
|
|
println!("[TRACE] read_32 addr={:x}", addr);
|
|
|
|
}
|
2019-11-08 22:55:09 +00:00
|
|
|
let (dev, addr) = self.map(addr);
|
2019-11-16 16:09:37 +00:00
|
|
|
dev.read_8(addr & 0x1ff_ffff)
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
2019-07-15 05:30:52 +01:00
|
|
|
fn write_32(&mut self, addr: Addr, value: u32) {
|
2019-11-08 22:55:09 +00:00
|
|
|
let (dev, addr) = self.map_mut(addr);
|
2019-11-16 16:09:37 +00:00
|
|
|
dev.write_32(addr & 0x1ff_fffc, value);
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
2019-07-15 05:30:52 +01:00
|
|
|
fn write_16(&mut self, addr: Addr, value: u16) {
|
2019-11-08 22:55:09 +00:00
|
|
|
let (dev, addr) = self.map_mut(addr);
|
2019-11-16 16:09:37 +00:00
|
|
|
dev.write_16(addr & 0x1ff_fffe, value);
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
2019-07-15 05:30:52 +01:00
|
|
|
fn write_8(&mut self, addr: Addr, value: u8) {
|
2019-11-08 22:55:09 +00:00
|
|
|
let (dev, addr) = self.map_mut(addr);
|
2019-11-16 16:09:37 +00:00
|
|
|
dev.write_8(addr & 0x1ff_ffff, value);
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
2019-06-25 00:10:09 +01:00
|
|
|
}
|