chore: code cleanup

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-11-30 03:45:54 -04:00
parent a0e3c7c602
commit 70e0b5868d
6 changed files with 42 additions and 121 deletions

View File

@ -587,7 +587,7 @@ impl Channel1 {
pub(crate) struct Channel2 { pub(crate) struct Channel2 {
/// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty /// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty
duty: SoundDuty, duty: SoundDuty,
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope /// 0xFF17 | NR22 - Channel 2 Volume Envelope
envelope: VolumeEnvelope, envelope: VolumeEnvelope,
/// 0xFF18 | NR23 - Channel 2 Frequency low (lower 8 bits only) /// 0xFF18 | NR23 - Channel 2 Frequency low (lower 8 bits only)
freq_lo: u8, freq_lo: u8,
@ -619,12 +619,12 @@ impl Channel2 {
self.length_counter = 64 - self.duty.sound_length() as u16; self.length_counter = 64 - self.duty.sound_length() as u16;
} }
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope /// 0xFF17 | NR22 - Channel 2 Volume Envelope
pub(crate) fn envelope(&self) -> u8 { pub(crate) fn envelope(&self) -> u8 {
u8::from(self.envelope) u8::from(self.envelope)
} }
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope /// 0xFF17 | NR22 - Channel 2 Volume Envelope
pub(crate) fn set_envelope(&mut self, byte: u8) { pub(crate) fn set_envelope(&mut self, byte: u8) {
self.envelope = byte.into(); self.envelope = byte.into();

View File

@ -17,11 +17,11 @@ impl Default for HighRam {
} }
impl BusIo for HighRam { impl BusIo for HighRam {
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - HIGH_RAM_START_ADDRESS] = byte;
}
fn read_byte(&self, addr: u16) -> u8 { fn read_byte(&self, addr: u16) -> u8 {
self.buf[addr as usize - HIGH_RAM_START_ADDRESS] self.buf[addr as usize - HIGH_RAM_START_ADDRESS]
} }
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - HIGH_RAM_START_ADDRESS] = byte;
}
} }

View File

@ -2,9 +2,9 @@ use std::time::Instant;
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg}; use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
use egui_wgpu_backend::RenderPass; use egui_wgpu_backend::RenderPass;
use gb::emu; use gb::{emu, gui};
use gb::gui::GuiState;
use gilrs::Gilrs; use gilrs::Gilrs;
use gui::GuiState;
use rodio::{OutputStream, Sink}; use rodio::{OutputStream, Sink};
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use winit::event::{Event, WindowEvent}; use winit::event::{Event, WindowEvent};
@ -45,20 +45,20 @@ fn main() {
.with_env_filter(EnvFilter::from_default_env()) .with_env_filter(EnvFilter::from_default_env())
.init(); .init();
// --Here lies a lot of Winit + WGPU Boilerplate-- // --Here lies a lot of winit + wgpu Boilerplate--
let event_loop: EventLoop<Event<()>> = EventLoop::with_user_event(); let event_loop: EventLoop<Event<()>> = EventLoop::with_user_event();
let window = gb::gui::build_window(&event_loop).expect("build window"); let window = gui::build_window(&event_loop).expect("build window");
let (instance, surface) = gb::gui::create_surface(&window); let (instance, surface) = gui::create_surface(&window);
let adapter = gb::gui::request_adapter(&instance, &surface).expect("request adaptor"); let adapter = gui::request_adapter(&instance, &surface).expect("request adaptor");
let (device, queue) = gb::gui::request_device(&adapter).expect("request device"); let (device, queue) = gui::request_device(&adapter).expect("request device");
let format = surface let format = surface
.get_preferred_format(&adapter) .get_preferred_format(&adapter)
.expect("get surface format"); .expect("get surface format");
let mut config = gb::gui::surface_config(&window, format); let mut config = gui::surface_config(&window, format);
surface.configure(&device, &config); surface.configure(&device, &config);
let mut platform = gb::gui::platform_desc(&window); let mut platform = gui::platform_desc(&window);
let mut render_pass = RenderPass::new(&device, format, 1); let mut render_pass = RenderPass::new(&device, format, 1);
// We interrupt your boiler plate to initialize the emulator so that // We interrupt your boiler plate to initialize the emulator so that
@ -74,11 +74,11 @@ fn main() {
} }
}; };
// Set up the WGPU (and then EGUI) texture we'll be working with. // Set up the wgpu (and then EGUI) texture we'll be working with.
let texture_size = gb::gui::texture_size(); let texture_size = gui::texture_size();
let texture = gb::gui::create_texture(&device, texture_size); let texture = gui::create_texture(&device, texture_size);
gb::gui::write_to_texture(&queue, &texture, gb::emu::pixel_buf(&cpu), texture_size); gui::write_to_texture(&queue, &texture, emu::pixel_buf(&cpu), texture_size);
let texture_id = gb::gui::expose_texture_to_egui(&mut render_pass, &device, &texture); let texture_id = gui::expose_texture_to_egui(&mut render_pass, &device, &texture);
// Load ROM if filepath was provided // Load ROM if filepath was provided
if let Some(path) = m.value_of("rom") { if let Some(path) = m.value_of("rom") {
@ -115,7 +115,7 @@ fn main() {
// Set up state for the Immediate-mode GUI // Set up state for the Immediate-mode GUI
let mut app = GuiState::new(rom_title); let mut app = GuiState::new(rom_title);
let mut last_key = gb::gui::unused_key(); let mut last_key = gui::unused_key();
// used for egui animations // used for egui animations
let start_time = Instant::now(); let start_time = Instant::now();
@ -129,15 +129,15 @@ fn main() {
emu::save_and_exit(&cpu, control_flow); emu::save_and_exit(&cpu, control_flow);
} }
gb::emu::run_frame(&mut cpu, &mut gamepad, last_key); emu::run_frame(&mut cpu, &mut gamepad, last_key);
window.request_redraw(); window.request_redraw();
} }
Event::RedrawRequested(..) => { Event::RedrawRequested(..) => {
platform.update_time(start_time.elapsed().as_secs_f64()); platform.update_time(start_time.elapsed().as_secs_f64());
let data = gb::emu::pixel_buf(&cpu); let data = emu::pixel_buf(&cpu);
gb::gui::write_to_texture(&queue, &texture, data, texture_size); gui::write_to_texture(&queue, &texture, data, texture_size);
let output_frame = match surface.get_current_texture() { let output_frame = match surface.get_current_texture() {
Ok(frame) => frame, Ok(frame) => frame,
@ -146,17 +146,17 @@ fn main() {
return; return;
} }
}; };
let output_view = gb::gui::create_view(&output_frame); let output_view = gui::create_view(&output_frame);
// Begin to draw Egui components // Begin to draw Egui components
platform.begin_frame(); platform.begin_frame();
gb::gui::draw_egui(&mut app, &platform.context(), texture_id); gui::draw_egui(&mut app, &platform.context(), texture_id);
// End the UI frame. We could now handle the output and draw the UI with the backend. // End the UI frame. We could now handle the output and draw the UI with the backend.
let (_, paint_commands) = platform.end_frame(Some(&window)); let (_, paint_commands) = platform.end_frame(Some(&window));
let paint_jobs = platform.context().tessellate(paint_commands); let paint_jobs = platform.context().tessellate(paint_commands);
let mut encoder = gb::gui::create_command_encoder(&device); let mut encoder = gui::create_command_encoder(&device);
let screen_descriptor = gb::gui::create_screen_descriptor(&window, &config); let screen_descriptor = gui::create_screen_descriptor(&window, &config);
// Upload all resources for the GPU. // Upload all resources for the GPU.
render_pass.update_texture(&device, &queue, &platform.context().texture()); render_pass.update_texture(&device, &queue, &platform.context().texture());
@ -164,7 +164,7 @@ fn main() {
render_pass.update_buffers(&device, &queue, &paint_jobs, &screen_descriptor); render_pass.update_buffers(&device, &queue, &paint_jobs, &screen_descriptor);
// Record all render passes. // Record all render passes.
gb::gui::execute_render_pass( gui::execute_render_pass(
&mut render_pass, &mut render_pass,
&mut encoder, &mut encoder,
&output_view, &output_view,

View File

@ -265,7 +265,7 @@ impl Ppu {
TileLowB => { TileLowB => {
let obj_size = self.ctrl.obj_size(); let obj_size = self.ctrl.obj_size();
let addr = PixelFetcher::get_obj_addr(attr, &self.pos, obj_size); let addr = PixelFetcher::obj_addr(attr, &self.pos, obj_size);
let byte = self.read_byte(addr); let byte = self.read_byte(addr);
self.fetch.obj.tile.with_low(byte); self.fetch.obj.tile.with_low(byte);
@ -276,7 +276,7 @@ impl Ppu {
TileHighB => { TileHighB => {
let obj_size = self.ctrl.obj_size(); let obj_size = self.ctrl.obj_size();
let addr = PixelFetcher::get_obj_addr(attr, &self.pos, obj_size); let addr = PixelFetcher::obj_addr(attr, &self.pos, obj_size);
let byte = self.read_byte(addr + 1); let byte = self.read_byte(addr + 1);
self.fetch.obj.tile.with_high(byte); self.fetch.obj.tile.with_high(byte);
@ -441,7 +441,7 @@ impl Ppu {
fn obj_pixel(&self, obj: ObjPixelProperty) -> GrayShade { fn obj_pixel(&self, obj: ObjPixelProperty) -> GrayShade {
use ObjectPaletteKind::*; use ObjectPaletteKind::*;
assert!(obj.shade_id != 0); assert_ne!(obj.shade_id, 0);
let p0 = &self.monochrome.obj_palette_0; let p0 = &self.monochrome.obj_palette_0;
let p1 = &self.monochrome.obj_palette_1; let p1 = &self.monochrome.obj_palette_1;
@ -683,7 +683,7 @@ impl PixelFetcher {
Ok(()) Ok(())
} }
fn get_obj_addr(attr: &ObjectAttr, pos: &ScreenPosition, size: ObjectSize) -> u16 { fn obj_addr(attr: &ObjectAttr, pos: &ScreenPosition, size: ObjectSize) -> u16 {
let line_y = pos.line_y; let line_y = pos.line_y;
// TODO: Why is the offset 14 and 30 respectively? // TODO: Why is the offset 14 and 30 respectively?

View File

@ -1,79 +0,0 @@
use crate::Cycle;
use std::collections::BinaryHeap;
#[derive(Debug)]
pub(crate) struct Scheduler {
timestamp: Cycle,
queue: BinaryHeap<Event>,
}
impl Scheduler {
pub(crate) fn init() -> Self {
let mut scheduler = Self {
timestamp: Default::default(),
queue: Default::default(),
};
scheduler.push(Event {
kind: EventKind::TimestampOverflow,
timestamp: Cycle::MAX,
cb: |_delay| panic!("Reached Cycle::MAX"),
});
scheduler
}
pub(crate) fn push(&mut self, event: Event) {
self.queue.push(event);
}
pub(crate) fn step(&mut self, cycles: Cycle) {
self.timestamp += cycles;
loop {
let should_pop = match self.queue.peek() {
Some(event) => self.timestamp >= event.timestamp,
None => false,
};
if !should_pop {
break;
}
let event = self.queue.pop().expect("Pop Event from Scheduler Queue");
(event.cb)(self.timestamp - event.timestamp);
}
}
}
#[derive(Debug)]
pub(crate) struct Event {
kind: EventKind,
cb: fn(Cycle),
pub(crate) timestamp: Cycle,
}
impl Eq for Event {}
impl PartialEq for Event {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind && self.timestamp == other.timestamp
}
}
impl PartialOrd for Event {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Event {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.timestamp.cmp(&other.timestamp)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum EventKind {
TimestampOverflow,
}

View File

@ -11,13 +11,13 @@ pub(crate) struct WorkRam {
} }
impl BusIo for WorkRam { impl BusIo for WorkRam {
fn write_byte(&mut self, addr: u16, byte: u8) {
self.bank[addr as usize - WORK_RAM_START_ADDRESS] = byte;
}
fn read_byte(&self, addr: u16) -> u8 { fn read_byte(&self, addr: u16) -> u8 {
self.bank[addr as usize - WORK_RAM_START_ADDRESS] self.bank[addr as usize - WORK_RAM_START_ADDRESS]
} }
fn write_byte(&mut self, addr: u16, byte: u8) {
self.bank[addr as usize - WORK_RAM_START_ADDRESS] = byte;
}
} }
impl Default for WorkRam { impl Default for WorkRam {
@ -42,11 +42,11 @@ impl Default for VariableWorkRam {
} }
impl BusIo for VariableWorkRam { impl BusIo for VariableWorkRam {
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS] = byte;
}
fn read_byte(&self, addr: u16) -> u8 { fn read_byte(&self, addr: u16) -> u8 {
self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS] self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS]
} }
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS] = byte;
}
} }