diff --git a/src/bin/main.rs b/src/bin/main.rs index 4569e89..02cb054 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -9,6 +9,7 @@ extern crate rustboyadvance_ng; use rustboyadvance_ng::arm7tdmi; use rustboyadvance_ng::debugger::{Debugger, DebuggerError}; +use rustboyadvance_ng::cartridge::Cartridge; use rustboyadvance_ng::sysbus::SysBus; use rustboyadvance_ng::util::read_bin_file; @@ -50,7 +51,10 @@ fn run_debug(matches: &ArgMatches) -> GBAResult<()> { let bios_bin = read_bin_file(matches.value_of("bios").unwrap_or_default())?; let rom_bin = read_bin_file(matches.value_of("game_rom").unwrap())?; - let sysbus = SysBus::new(bios_bin, rom_bin); + let gamepak = Cartridge::new(rom_bin); + println!("loaded rom: {:#?}", gamepak.header); + + let sysbus = SysBus::new(bios_bin, gamepak); let mut core = arm7tdmi::cpu::Core::new(); core.reset(); core.set_verbose(true); diff --git a/src/cartridge.rs b/src/cartridge.rs new file mode 100644 index 0000000..65c5e27 --- /dev/null +++ b/src/cartridge.rs @@ -0,0 +1,104 @@ +use std::str::from_utf8; + +use nom::number::streaming::le_u32; + +use crate::arm7tdmi::{ + bus::{Bus, MemoryAccess, MemoryAccessType, MemoryAccessWidth}, + Addr, +}; +use crate::sysbus::WaitState; + +/// From GBATEK +/// +/// The first 192 bytes at 8000000h-80000BFh in ROM are used as cartridge header. The same header is also used for Multiboot images at 2000000h-20000BFh (plus some additional multiboot entries at 20000C0h and up). +/// +/// Header Overview +/// Address Bytes Expl. +/// 000h 4 ROM Entry Point (32bit ARM branch opcode, eg. "B rom_start") +/// 004h 156 Nintendo Logo (compressed bitmap, required!) +/// 0A0h 12 Game Title (uppercase ascii, max 12 characters) +/// 0ACh 4 Game Code (uppercase ascii, 4 characters) +/// 0B0h 2 Maker Code (uppercase ascii, 2 characters) +/// 0B2h 1 Fixed value (must be 96h, required!) +/// 0B3h 1 Main unit code (00h for current GBA models) +/// 0B4h 1 Device type (usually 00h) (bit7=DACS/debug related) +/// 0B5h 7 Reserved Area (should be zero filled) +/// 0BCh 1 Software version (usually 00h) +/// 0BDh 1 Complement check (header checksum, required!) +/// 0BEh 2 Reserved Area (should be zero filled) +/// --- Additional Multiboot Header Entries --- +/// 0C0h 4 RAM Entry Point (32bit ARM branch opcode, eg. "B ram_start") +/// 0C4h 1 Boot mode (init as 00h - BIOS overwrites this value!) +/// 0C5h 1 Slave ID Number (init as 00h - BIOS overwrites this value!) +/// 0C6h 26 Not used (seems to be unused) +/// 0E0h 4 JOYBUS Entry Pt. (32bit ARM branch opcode, eg. "B joy_start") +/// +#[derive(Debug)] +pub struct CartridgeHeader { + // rom_entry_point: Addr, + game_title: String, + game_code: String, + maker_code: String, + software_version: u8, + checksum: u8, + // ram_entry_point: Addr, + // joybus_entry_point: Addr, +} + +impl CartridgeHeader { + fn parse(bytes: &[u8]) -> CartridgeHeader { + // let (_, rom_entry_point) = le_u32(bytes).unwrap(); + let game_title = from_utf8(&bytes[0xa0..0xac]).unwrap(); + let game_code = from_utf8(&bytes[0xac..0xb0]).unwrap(); + let maker_code = from_utf8(&bytes[0xb0..0xb2]).unwrap(); + // let (_, ram_entry_point) = le_u32(&bytes[0xc0..]).unwrap(); + // let (_, joybus_entry_point) = le_u32(&bytes[0xc0..]).unwrap(); + + CartridgeHeader { + // rom_entry_point: rom_entry_point, + game_title: String::from(game_title), + game_code: String::from(game_code), + maker_code: String::from(maker_code), + software_version: bytes[0xbc], + checksum: bytes[0xbd], + // ram_entry_point: ram_entry_point, + // joybus_entry_point: joybus_entry_point, + } + } +} + +#[derive(Debug)] +pub struct Cartridge { + pub header: CartridgeHeader, + bytes: Box<[u8]>, + ws: WaitState, +} + +impl Cartridge { + pub fn new(gamepak: Vec) -> Cartridge { + let header = CartridgeHeader::parse(&gamepak); + Cartridge { + header: header, + bytes: gamepak.into_boxed_slice(), + ws: WaitState::new(5, 5, 8), + } + } +} + +impl Bus for Cartridge { + fn get_bytes(&self, addr: Addr) -> &[u8] { + &self.bytes[addr as usize..] + } + + fn get_bytes_mut(&mut self, addr: Addr) -> &mut [u8] { + &mut self.bytes[addr as usize..] + } + + fn get_cycles(&self, _addr: Addr, access: MemoryAccess) -> usize { + match access.1 { + MemoryAccessWidth::MemoryAccess8 => self.ws.access8, + MemoryAccessWidth::MemoryAccess16 => self.ws.access16, + MemoryAccessWidth::MemoryAccess32 => self.ws.access32, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 5c893e3..ab3dce9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ extern crate ansi_term; extern crate colored; // not needed in Rust 2018 pub mod arm7tdmi; +pub mod cartridge; pub mod debugger; pub mod disass; pub mod sysbus; diff --git a/src/sysbus.rs b/src/sysbus.rs index 7e958cb..ba31c04 100644 --- a/src/sysbus.rs +++ b/src/sysbus.rs @@ -1,5 +1,7 @@ use std::io; +use crate::cartridge::Cartridge; + use super::arm7tdmi::bus::{Bus, MemoryAccess, MemoryAccessWidth}; use super::arm7tdmi::Addr; @@ -30,7 +32,7 @@ pub struct WaitState { } impl WaitState { - fn new(access8: usize, access16: usize, access32: usize) -> WaitState { + pub fn new(access8: usize, access16: usize, access32: usize) -> WaitState { WaitState { access8, access16, @@ -73,36 +75,30 @@ pub struct SysBus { palette_ram: BoxedMemory, vram: BoxedMemory, oam: BoxedMemory, - gamepak_flashrom: BoxedMemory, + gamepak: Cartridge, } impl SysBus { - pub fn new(bios_rom: Vec, game_rom: Vec) -> SysBus { + pub fn new(bios_rom: Vec, gamepak: Cartridge) -> SysBus { SysBus { - bios: BoxedMemory(bios_rom.into_boxed_slice(), Default::default()), - onboard_work_ram: BoxedMemory( - vec![0; WORK_RAM_SIZE].into_boxed_slice(), - Default::default(), - ), - internal_work_ram: BoxedMemory( - vec![0; INTERNAL_RAM].into_boxed_slice(), - Default::default(), - ), - ioregs: BoxedMemory(vec![0; 1024].into_boxed_slice(), Default::default()), - palette_ram: BoxedMemory( + 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].into_boxed_slice()), + ioregs: BoxedMemory::new(vec![0; 1024].into_boxed_slice()), + palette_ram: BoxedMemory::new_with_waitstate( vec![0; PALETTE_RAM_SIZE].into_boxed_slice(), WaitState::new(1, 1, 2), ), - vram: BoxedMemory( + vram: BoxedMemory::new_with_waitstate( vec![0; VIDEO_RAM_SIZE].into_boxed_slice(), WaitState::new(1, 1, 2), ), - oam: BoxedMemory(vec![0; OAM_SIZE].into_boxed_slice(), Default::default()), - gamepak_flashrom: BoxedMemory(game_rom.into_boxed_slice(), WaitState::new(5, 5, 8)), + oam: BoxedMemory::new(vec![0; OAM_SIZE].into_boxed_slice()), + gamepak: gamepak, } } - fn map(&self, addr: Addr) -> & Bus { + fn map(&self, addr: Addr) -> &Bus { match addr as usize { 0x0000_0000...0x0000_3fff => &self.bios, 0x0200_0000...0x0203_ffff => &self.onboard_work_ram, @@ -111,7 +107,7 @@ impl SysBus { 0x0500_0000...0x0500_03ff => &self.palette_ram, 0x0600_0000...0x0601_7fff => &self.vram, 0x0700_0000...0x0700_03ff => &self.oam, - 0x0800_0000...0x09ff_ffff => &self.gamepak_flashrom, + 0x0800_0000...0x09ff_ffff => &self.gamepak, _ => panic!("unmapped address @0x{:08x}", addr), } } @@ -126,7 +122,7 @@ impl SysBus { 0x0500_0000...0x0500_03ff => &mut self.palette_ram, 0x0600_0000...0x0601_7fff => &mut self.vram, 0x0700_0000...0x0700_03ff => &mut self.oam, - 0x0800_0000...0x09ff_ffff => &mut self.gamepak_flashrom, + 0x0800_0000...0x09ff_ffff => &mut self.gamepak, _ => panic!("unmapped address @0x{:08x}", addr), } }