Skip to content
Snippets Groups Projects
Verified Commit d07e7f09 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: better structure for boot loading of CGB

parent a914056c
No related branches found
No related tags found
1 merge request!16Support for Game Boy Color (CGB) 😎🖍️
Pipeline #2554 failed
...@@ -7,7 +7,7 @@ pub mod graphics; ...@@ -7,7 +7,7 @@ pub mod graphics;
use audio::Audio; use audio::Audio;
use boytacean::{ use boytacean::{
devices::printer::PrinterDevice, devices::printer::PrinterDevice,
gb::{AudioProvider, GameBoy}, gb::{AudioProvider, GBMode, GameBoy},
pad::PadKey, pad::PadKey,
ppu::{PaletteInfo, PpuMode, DISPLAY_HEIGHT, DISPLAY_WIDTH}, ppu::{PaletteInfo, PpuMode, DISPLAY_HEIGHT, DISPLAY_WIDTH},
}; };
...@@ -186,7 +186,7 @@ impl Emulator { ...@@ -186,7 +186,7 @@ impl Emulator {
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.system.reset(); self.system.reset();
self.system.load_boot_default(); self.system.load(true);
self.load_rom(None); self.load_rom(None);
} }
...@@ -317,7 +317,7 @@ impl Emulator { ...@@ -317,7 +317,7 @@ impl Emulator {
} }
Event::DropFile { filename, .. } => { Event::DropFile { filename, .. } => {
self.system.reset(); self.system.reset();
self.system.load_boot_cgb(); self.system.load(true);
self.load_rom(Some(&filename)); self.load_rom(Some(&filename));
} }
_ => (), _ => (),
...@@ -446,7 +446,7 @@ impl Emulator { ...@@ -446,7 +446,7 @@ impl Emulator {
fn main() { fn main() {
// creates a new Game Boy instance and loads both the boot ROM // creates a new Game Boy instance and loads both the boot ROM
// and the initial game ROM to "start the engine" // and the initial game ROM to "start the engine"
let mut game_boy = GameBoy::new(); let mut game_boy = GameBoy::new(GBMode::Cgb);
let mut printer = Box::<PrinterDevice>::default(); let mut printer = Box::<PrinterDevice>::default();
printer.set_callback(|image_buffer| { printer.set_callback(|image_buffer| {
let file_name = format!("printer-{}.png", Utc::now().format("%Y%m%d-%H%M%S")); let file_name = format!("printer-{}.png", Utc::now().format("%Y%m%d-%H%M%S"));
...@@ -460,7 +460,7 @@ fn main() { ...@@ -460,7 +460,7 @@ fn main() {
.unwrap(); .unwrap();
}); });
game_boy.attach_serial(printer); game_boy.attach_serial(printer);
game_boy.load_boot_cgb(); game_boy.load(true);
// creates a new generic emulator structure then starts // creates a new generic emulator structure then starts
// both the video and audio sub-systems, loads default // both the video and audio sub-systems, loads default
......
...@@ -30,11 +30,24 @@ use std::{ ...@@ -30,11 +30,24 @@ use std::{
panic::{set_hook, take_hook, PanicInfo}, panic::{set_hook, take_hook, PanicInfo},
}; };
/// Enumeration that describes the multiple running
// modes of the Game Boy emulator.
pub enum GBMode {
Dmg = 1,
Cgb = 2,
Sgb = 3,
}
/// Top level structure that abstracts the usage of the /// Top level structure that abstracts the usage of the
/// Game Boy system under the Boytacean emulator. /// Game Boy system under the Boytacean emulator.
/// Should serve as the main entry-point API. /// Should serve as the main entry-point API.
#[cfg_attr(feature = "wasm", wasm_bindgen)] #[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct GameBoy { pub struct GameBoy {
/// The current running mode of the emulator, this
/// may affect many aspects of the emulation, like
/// CPU frequency, PPU frequency, Boot rome size, etc.
mode: GBMode,
/// Reference to the Game Boy CPU component to be /// Reference to the Game Boy CPU component to be
/// used as the main element of the system, when /// used as the main element of the system, when
/// clocked, the amount of ticks from it will be /// clocked, the amount of ticks from it will be
...@@ -90,7 +103,7 @@ pub trait AudioProvider { ...@@ -90,7 +103,7 @@ pub trait AudioProvider {
#[cfg_attr(feature = "wasm", wasm_bindgen)] #[cfg_attr(feature = "wasm", wasm_bindgen)]
impl GameBoy { impl GameBoy {
#[cfg_attr(feature = "wasm", wasm_bindgen(constructor))] #[cfg_attr(feature = "wasm", wasm_bindgen(constructor))]
pub fn new() -> Self { pub fn new(mode: GBMode) -> Self {
let ppu = Ppu::default(); let ppu = Ppu::default();
let apu = Apu::default(); let apu = Apu::default();
let pad = Pad::default(); let pad = Pad::default();
...@@ -99,6 +112,7 @@ impl GameBoy { ...@@ -99,6 +112,7 @@ impl GameBoy {
let mmu = Mmu::new(ppu, apu, pad, timer, serial); let mmu = Mmu::new(ppu, apu, pad, timer, serial);
let cpu = Cpu::new(mmu); let cpu = Cpu::new(mmu);
Self { Self {
mode,
cpu, cpu,
ppu_enabled: true, ppu_enabled: true,
apu_enabled: true, apu_enabled: true,
...@@ -178,10 +192,11 @@ impl GameBoy { ...@@ -178,10 +192,11 @@ impl GameBoy {
self.cpu.boot(); self.cpu.boot();
} }
pub fn load_default(&mut self, boot: bool) { pub fn load(&mut self, boot: bool) {
self.mmu().allocate_default(); match self.mode {
if boot { GBMode::Dmg => self.load_dmg(boot),
self.load_boot_default(); GBMode::Cgb => self.load_cgb(boot),
GBMode::Sgb => todo!(),
} }
} }
...@@ -601,6 +616,6 @@ impl AudioProvider for GameBoy { ...@@ -601,6 +616,6 @@ impl AudioProvider for GameBoy {
impl Default for GameBoy { impl Default for GameBoy {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new(GBMode::Dmg)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment