Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
- Removed some unnecessary clones
- Cleaned up Config
- Boot ROM is now mandatory
  • Loading branch information
IsaacMarovitz committed May 3, 2024
1 parent e160993 commit 378c890
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 97 deletions.
41 changes: 17 additions & 24 deletions src/components/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,27 @@ impl CPU {
config: Config,
framebuffer: Framebuffer) -> Self {
let mut boot_rom: [u8; 0x900] = [0; 0x900];
let booting: bool = match config.boot_rom {
Some(ref path) => {
let mut boot_rom_vec = Vec::new();
let mut boot = match File::open(path.clone()) {
Ok(file) => file,
Err(err) => {
eprintln!("Failed to open Boot ROM at \"{}\": {}", path.clone(), err);
process::exit(1);
}
};
boot.read_to_end(&mut boot_rom_vec)
.expect("Failed to read Boot ROM!");

// Copy Boot ROM
if config.mode == GBMode::DMG {
boot_rom[0..=0x00FF].copy_from_slice(boot_rom_vec.as_slice());
} else if config.mode == GBMode::CGB {
boot_rom[0..=0x08FF].copy_from_slice(boot_rom_vec.as_slice());
}

true
let mut boot_rom_vec = Vec::new();
let mut boot = match File::open(config.boot_rom.clone()) {
Ok(file) => file,
Err(err) => {
eprintln!("Failed to open Boot ROM at \"{}\": {}", config.boot_rom, err);
process::exit(1);
}
None => false,
};
boot.read_to_end(&mut boot_rom_vec)
.expect("Failed to read Boot ROM!");

// Copy Boot ROM
if config.mode == GBMode::DMG {
boot_rom[0..=0x00FF].copy_from_slice(boot_rom_vec.as_slice());
} else if config.mode == GBMode::CGB {
boot_rom[0..=0x08FF].copy_from_slice(boot_rom_vec.as_slice());
}

Self {
reg: Registers::new(config.clone().mode, booting),
mem: MMU::new(rom, config, booting, boot_rom, framebuffer),
reg: Registers::new(config.mode),
mem: MMU::new(rom, config, boot_rom, framebuffer),
halted: false,
ime: false,
ime_ask: false
Expand Down
5 changes: 2 additions & 3 deletions src/components/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ bitflags! {
impl MMU {
pub fn new(rom: Vec<u8>,
config: Config,
booting: bool,
boot_rom: [u8; 0x900],
framebuffer: Framebuffer) -> Self {
let cart_type: CartTypes = FromPrimitive::from_u8(rom[0x0147]).expect("Failed to get Cart Type!");
Expand All @@ -61,7 +60,7 @@ impl MMU {

Self {
mbc: mbc,
apu: APU::new(config.audio.clone()),
apu: APU::new(config.apu_config),
ppu: PPU::new(config.clone(), framebuffer),
serial: Serial::new(config.print_serial),
joypad: Joypad::new(),
Expand All @@ -72,7 +71,7 @@ impl MMU {
inte: Interrupts::empty(),
wram_bank: 0x01,
boot_rom,
boot_rom_enabled: booting,
boot_rom_enabled: true,
mode: config.mode
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/components/ppu/ppu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::prelude::*;
use crate::config::{Color, Config, Palette};
use crate::config::{Color, Config, Palette, PPUConfig};
use crate::Framebuffer;
use bitflags::bitflags;
use crate::components::ppu::cc::ColorCorrection;
Expand All @@ -11,8 +11,7 @@ pub const SCREEN_H: usize = 144;

pub struct PPU {
mode: GBMode,
cc_mode: CCMode,
palette: Palette,
ppu_config: PPUConfig,
cc: ColorCorrection,
ppu_mode: PPUMode,
cycle_count: u32,
Expand Down Expand Up @@ -139,9 +138,8 @@ impl PPU {
pub fn new(config: Config, framebuffer: Framebuffer) -> Self {
Self {
mode: config.mode,
cc_mode: config.cc_mode,
ppu_config: config.ppu_config,
cc: ColorCorrection::new(),
palette: config.palette,
ppu_mode: PPUMode::OAMScan,
cycle_count: 0,
vblanked_lines: 0,
Expand Down Expand Up @@ -291,7 +289,7 @@ impl PPU {
}

fn set_rgb_mapped(&mut self, x: usize, color: u16) {
let color = match self.cc_mode {
let color = match self.ppu_config.cc_mode {
CCMode::True => self.cc.true_color_lut[color as usize],
CCMode::CGB => self.cc.cgb_color_lut[color as usize],
CCMode::GBA => self.cc.gba_color_lut[color as usize],
Expand Down Expand Up @@ -414,9 +412,9 @@ impl PPU {
self.set_rgb_mapped(x, color);
} else {
let color = if !self.lcdc.contains(LCDC::WINDOW_PRIORITY) {
Self::grey_to_l(self.palette.clone(), self.bgp, 0)
Self::grey_to_l(self.ppu_config.palette, self.bgp, 0)
} else {
Self::grey_to_l(self.palette.clone(), self.bgp, color)
Self::grey_to_l(self.ppu_config.palette, self.bgp, color)
};

self.set_rgb(x, color.r, color.g, color.b);
Expand Down Expand Up @@ -523,9 +521,9 @@ impl PPU {
self.set_rgb_mapped(px.wrapping_add(x) as usize, color);
} else {
let color = if tile_attributes.contains(Attributes::PALETTE_NO_0) {
Self::grey_to_l(self.palette.clone(), self.obp1, color)
Self::grey_to_l(self.ppu_config.palette, self.obp1, color)
} else {
Self::grey_to_l(self.palette.clone(), self.obp0, color)
Self::grey_to_l(self.ppu_config.palette, self.obp0, color)
};

self.set_rgb(px.wrapping_add(x) as usize, color.r, color.g, color.b);
Expand Down
6 changes: 3 additions & 3 deletions src/components/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Registers {
}
}

pub fn new(mode: GBMode, booting: bool) -> Registers {
pub fn new(mode: GBMode) -> Registers {
match mode {
GBMode::DMG => Registers {
a: 0x01,
Expand All @@ -92,7 +92,7 @@ impl Registers {
e: 0xD8,
h: 0x01,
l: 0x4D,
pc: if booting { 0x0000 } else { 0x0100 },
pc: 0x0000,
sp: 0xFFFE,
},
GBMode::CGB => Registers {
Expand All @@ -104,7 +104,7 @@ impl Registers {
e: 0x56,
h: 0x00,
l: 0x0D,
pc: if booting { 0x0000 } else { 0x0100 },
pc: 0x0000,
sp: 0xFFFE,
},
}
Expand Down
125 changes: 76 additions & 49 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,104 @@
use serde::{Deserialize, Serialize};
use winit::keyboard::Key;
use winit::keyboard::{Key, SmolStr};
use crate::components::mode::{CCMode, GBMode};

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Config {
pub window_w: u32,
pub window_h: u32,
pub mode: GBMode,
pub cc_mode: CCMode,
pub print_serial: bool,
pub boot_rom: Option<String>,
pub audio: Audio,
pub boot_rom: String,
pub shader_path: String,
pub mode: GBMode,
pub ppu_config: PPUConfig,
pub apu_config: APUConfig,
pub input: Input,
pub palette: Palette,
pub shader_path: String
}

impl Default for Config {
fn default() -> Self {
Config {
window_w: 160 * 2,
window_h: 144 * 2,
mode: GBMode::DMG,
cc_mode: CCMode::CGB,
print_serial: false,
boot_rom: None,
audio: Audio {
ch1_enabled: true,
ch2_enabled: true,
ch3_enabled: true,
ch4_enabled: true,
},
input: Input {
up: Key::Character("w".parse().unwrap()),
left: Key::Character("a".parse().unwrap()),
down: Key::Character("s".parse().unwrap()),
right: Key::Character("d".parse().unwrap()),
a: Key::Character("z".parse().unwrap()),
b: Key::Character("x".parse().unwrap()),
select: Key::Character("c".parse().unwrap()),
start: Key::Character("v".parse().unwrap()),
},
palette: Palette {
dark: Color {
r: 175,
g: 203,
b: 70,
},
dark_gray: Color {
r: 121,
g: 170,
b: 109,
},
light_gray: Color {
r: 34,
g: 111,
b: 95,
},
light: Color { r: 8, g: 41, b: 85 },
},
shader_path: String::default()
boot_rom: String::default(),
shader_path: String::default(),
mode: GBMode::DMG,
ppu_config: PPUConfig::new(),
apu_config: APUConfig::new(),
input: Input::new()
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub struct PPUConfig {
pub palette: Palette,
pub cc_mode: CCMode,
}

impl PPUConfig {
pub fn new() -> Self {
Self {
palette: Palette::new(),
cc_mode: CCMode::CGB
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub struct Palette {
pub dark: Color,
pub dark_gray: Color,
pub light_gray: Color,
pub light: Color,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
impl Palette {
pub fn new() -> Self {
Self {
dark: Color::new(175, 203, 70),
dark_gray: Color::new(121, 170, 109),
light_gray: Color::new(34, 111, 95),
light: Color::new(8, 41, 95)
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Audio {
impl Color {
pub fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}

#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub struct APUConfig {
pub master_enabled: bool,
pub ch1_enabled: bool,
pub ch2_enabled: bool,
pub ch3_enabled: bool,
pub ch4_enabled: bool,
}

impl APUConfig {
pub fn new() -> Self {
Self {
master_enabled: true,
ch1_enabled: true,
ch2_enabled: true,
ch3_enabled: true,
ch4_enabled: true
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Input {
pub up: Key,
Expand All @@ -98,3 +110,18 @@ pub struct Input {
pub select: Key,
pub start: Key,
}

impl Input {
pub fn new() -> Self {
Self {
up: Key::Character(SmolStr::new("w")),
left: Key::Character(SmolStr::new("a")),
down: Key::Character(SmolStr::new("s")),
right: Key::Character(SmolStr::new("d")),
a: Key::Character(SmolStr::new("z")),
b: Key::Character(SmolStr::new("x")),
select: Key::Character(SmolStr::new("c")),
start: Key::Character(SmolStr::new("v")),
}
}
}
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl ApplicationHandler for App {
}

fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
let context_arc = Arc::clone(&self.context.as_ref().unwrap());
let context_arc = &self.context.as_ref().unwrap();
let mut context = context_arc.lock().unwrap();
let size = context.size;

Expand All @@ -86,14 +86,14 @@ impl ApplicationHandler for App {
send_input(
event.key_without_modifiers(),
true,
self.config.clone().input,
self.config.input.clone(),
self.input_tx.clone(),
);
} else if event.state == ElementState::Released {
send_input(
event.key_without_modifiers(),
false,
self.config.clone().input,
self.config.input.clone(),
self.input_tx.clone(),
);
}
Expand All @@ -104,10 +104,10 @@ impl ApplicationHandler for App {
}

fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
let context_arc = Arc::clone(&self.context.as_ref().unwrap());
let context_arc = &self.context.as_ref().unwrap();
let mut context = context_arc.lock().unwrap();

let framebuffer = Arc::clone(&self.framebuffer);
let framebuffer = &self.framebuffer;
context.update(&*framebuffer.read().unwrap());

let _ = context.render();
Expand Down
Loading

0 comments on commit 378c890

Please sign in to comment.