From 1096c7e5d89e023bd18494729b480a632ddcff78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Thu, 27 Apr 2023 11:48:01 +0100 Subject: [PATCH] chore: better printing support for specs Will allow much better debug. --- frontends/sdl/src/main.rs | 13 +++---------- src/gb.rs | 40 +++++++++++++++++++++++++++++++++++++-- src/rom.rs | 30 ++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs index 3d5862ec..9cdc4888 100644 --- a/frontends/sdl/src/main.rs +++ b/frontends/sdl/src/main.rs @@ -8,7 +8,6 @@ use audio::Audio; use boytacean::{ devices::printer::PrinterDevice, gb::{AudioProvider, GameBoy, GameBoyMode}, - gen::VERSION, pad::PadKey, ppu::{PaletteInfo, PpuMode, DISPLAY_HEIGHT, DISPLAY_WIDTH}, }; @@ -472,15 +471,6 @@ fn main() { let args = Args::parse(); let mode = GameBoyMode::from_str(&args.mode); - // prints the current version of the emulator (informational message) - println!("========= Boytacean ========="); - println!("Version {}", VERSION); - println!("Mode {}", mode); - println!("CPU Freq. 4.34 Mhz"); - println!("RAM Size 32 KB"); - println!("ROM Size 32 KB"); - println!("VRAM Size 32 KB"); - // creates a new Game Boy instance and loads both the boot ROM // and the initial game ROM to "start the engine" let mut game_boy = GameBoy::new(mode); @@ -499,6 +489,9 @@ fn main() { game_boy.attach_serial(printer); game_boy.load(true); + // prints the current version of the emulator (informational message) + println!("========= Boytacean =========\n{}", game_boy); + // creates a new generic emulator structure then starts // both the video and audio sub-systems, loads default // ROM file and starts running it diff --git a/src/gb.rs b/src/gb.rs index b4ddc0ce..74b8dc27 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -3,11 +3,11 @@ use crate::{ cpu::Cpu, data::{BootRom, CGB_BOOT, DMG_BOOT, DMG_BOOTIX, MGB_BOOTIX, SGB_BOOT}, devices::{printer::PrinterDevice, stdout::StdoutDevice}, - gen::{COMPILATION_DATE, COMPILATION_TIME, COMPILER, COMPILER_VERSION}, + gen::{COMPILATION_DATE, COMPILATION_TIME, COMPILER, COMPILER_VERSION, VERSION}, mmu::Mmu, pad::{Pad, PadKey}, ppu::{Ppu, PpuMode, Tile, FRAME_BUFFER_SIZE}, - rom::Cartridge, + rom::{Cartridge, RamSize}, serial::{NullDevice, Serial, SerialDevice}, timer::Timer, util::read_file, @@ -591,6 +591,36 @@ impl GameBoy { pub fn attach_printer_serial(&mut self) { self.attach_serial(Box::<PrinterDevice>::default()); } + + pub fn ram_size(&self) -> RamSize { + match self.mode { + GameBoyMode::Dmg => RamSize::Size8K, + GameBoyMode::Cgb => RamSize::Size32K, + GameBoyMode::Sgb => RamSize::Size8K, + } + } + + pub fn vram_size(&self) -> RamSize { + match self.mode { + GameBoyMode::Dmg => RamSize::Size8K, + GameBoyMode::Cgb => RamSize::Size16K, + GameBoyMode::Sgb => RamSize::Size8K, + } + } + + pub fn description(&self, column_length: usize) -> String { + format!( + "{} {}\n{} {}\n{} {}\n{} {}", + format!("{:width$}", "Version", width = column_length), + VERSION, + format!("{:width$}", "Mode", width = column_length), + self.mode(), + format!("{:width$}", "RAM Size", width = column_length), + self.ram_size(), + format!("{:width$}", "VRAM Size", width = column_length), + self.vram_size(), + ) + } } /// Gameboy implementations that are meant with performance @@ -801,3 +831,9 @@ impl Default for GameBoy { Self::new(GameBoyMode::Dmg) } } + +impl Display for GameBoy { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.description(9)) + } +} diff --git a/src/rom.rs b/src/rom.rs index 5b568592..8d18fab5 100644 --- a/src/rom.rs +++ b/src/rom.rs @@ -1,5 +1,6 @@ use core::fmt; use std::{ + any::Any, cmp::max, fmt::{Display, Formatter}, }; @@ -144,6 +145,7 @@ pub enum RamSize { NoRam, Unused, Size8K, + Size16K, Size32K, Size64K, Size128K, @@ -156,6 +158,7 @@ impl RamSize { RamSize::NoRam => "No RAM", RamSize::Unused => "Unused", RamSize::Size8K => "8 KB", + RamSize::Size16K => "16 KB", RamSize::Size32K => "32 KB", RamSize::Size128K => "128 KB", RamSize::Size64K => "64 KB", @@ -168,6 +171,7 @@ impl RamSize { RamSize::NoRam => 0, RamSize::Unused => 0, RamSize::Size8K => 1, + RamSize::Size16K => 2, RamSize::Size32K => 4, RamSize::Size64K => 8, RamSize::Size128K => 16, @@ -498,6 +502,22 @@ impl Cartridge { pub fn set_ram_data(&mut self, ram_data: Vec<u8>) { self.ram_data = ram_data; } + + pub fn description(&self, column_length: usize) -> String { + format!( + "{} {}\n{} {}\n{} {}\n{} {}\n{} {}", + format!("{:width$}", "Name", width = column_length), + self.title(), + format!("{:width$}", "Type", width = column_length), + self.rom_type(), + format!("{:width$}", "ROM Size", width = column_length), + self.rom_size(), + format!("{:width$}", "RAM Size", width = column_length), + self.ram_size(), + format!("{:width$}", "CGB Mode", width = column_length), + self.cgb_flag() + ) + } } impl Default for Cartridge { @@ -508,15 +528,7 @@ impl Default for Cartridge { impl Display for Cartridge { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!( - f, - "Name {}\nType {}\nROM Size {}\nRAM Size {}\nCGB Mode {}", - self.title(), - self.rom_type(), - self.rom_size(), - self.ram_size(), - self.cgb_flag() - ) + write!(f, "{}", self.description(9)) } } -- GitLab