From 115cec9830153516ee15e390c59d6365fba9598f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Fri, 8 Jul 2022 16:25:08 +0100 Subject: [PATCH] feat: better display of information --- examples/sdl/src/main.rs | 3 ++- src/gb.rs | 3 +++ src/ppu.rs | 2 +- src/rom.rs | 58 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index 4e8d5dba..dc95629e 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -87,9 +87,10 @@ fn main() { //game_boy.load_rom_file("../../res/roms.prop/tetris.gb"); //game_boy.load_rom_file("../../res/roms.prop/dr_mario.gb"); //game_boy.load_rom_file("../../res/roms.prop/alleyway.gb"); + game_boy.load_rom_file("../../res/roms.prop/super_mario.gb"); //game_boy.load_rom_file("../../res/roms/firstwhite.gb"); - game_boy.load_rom_file("../../res/roms/opus5.gb"); + //game_boy.load_rom_file("../../res/roms/opus5.gb"); //game_boy.load_rom_file("../../res/roms/paradius/cpu/01-special.gb"); // PASSED //game_boy.load_rom_file("../../res/roms/paradius/cpu/02-interrupts.gb"); // PASSED diff --git a/src/gb.rs b/src/gb.rs index acfd5d98..4d2a545d 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -4,6 +4,7 @@ use crate::{ mmu::Mmu, pad::{Pad, PadKey}, ppu::{Ppu, Tile, FRAME_BUFFER_SIZE}, + rom::Rom, timer::Timer, util::read_file, }; @@ -69,6 +70,8 @@ impl GameBoy { } pub fn load_rom(&mut self, data: &[u8]) { + let rom = Rom::from_data(data); + println!("{}", rom); self.cpu.mmu().write_rom(0x0000, data); } diff --git a/src/ppu.rs b/src/ppu.rs index 55fc2dfd..6f546fdc 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -113,7 +113,7 @@ impl Display for ObjectData { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!( f, - "Index => {} X => {} Y => {} Tile => {}", + "Index => {}\nX => {}\nY => {}\nTile => {}", self.index, self.x, self.y, self.tile ) } diff --git a/src/rom.rs b/src/rom.rs index 8c6fbeda..551e940e 100644 --- a/src/rom.rs +++ b/src/rom.rs @@ -1,7 +1,9 @@ +use core::fmt; +use std::fmt::{Display, Formatter}; + pub struct Rom { data: Vec<u8>, } - pub enum RomType { RomOnly = 0x00, Mbc1 = 0x01, @@ -12,12 +14,58 @@ pub enum RomType { Unknown = 0xff, } +pub enum RomSize { + Size32K = 32, + Size64K = 64, + Size128K = 128, + SizeUnknown = 0, +} + impl Rom { - pub fn title() -> &'static str { - "asdas" + pub fn from_data(data: &[u8]) -> Self { + Self { + data: data.to_vec(), + } } - pub fn rom_type() -> RomType { - RomType::RomOnly + pub fn data(&self) -> &Vec<u8> { + &self.data + } + + pub fn title(&self) -> &str { + std::str::from_utf8(&self.data[0x0134..0x0143]).unwrap() + } + + pub fn rom_type(&self) -> RomType { + match self.data[0x0147] { + 0x00 => RomType::RomOnly, + 0x01 => RomType::Mbc1, + 0x02 => RomType::Mbc1Ram, + 0x03 => RomType::Mbc1RamBattery, + 0x05 => RomType::Mbc2, + 0x06 => RomType::Mbc2Battery, + _ => RomType::Unknown, + } + } + + pub fn size(&self) -> RomSize { + match self.data[0x0148] { + 0x00 => RomSize::Size32K, + 0x01 => RomSize::Size64K, + 0x02 => RomSize::Size128K, + _ => RomSize::SizeUnknown, + } + } +} + +impl Display for Rom { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!( + f, + "Name => {}\nType => {}\nSize => {}", + self.title(), + self.rom_type() as u8, + self.size() as u32 + ) } } -- GitLab