cleanup: move stuff to util & use struct for options

This commit is contained in:
Alex Janka 2023-02-26 09:42:15 +11:00
parent 0c45be7c58
commit c54027e5f6
3 changed files with 103 additions and 100 deletions

View file

@ -6,38 +6,32 @@
bigint_helper_methods bigint_helper_methods
)] )]
use std::{ use crate::{
fs, processor::memory::Memory,
io::{self, stdout, Write}, util::{pause, print_cycles},
}; };
use gilrs::Gilrs; use gilrs::Gilrs;
use minifb::Window; use minifb::Window;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use processor::{memory::Rom, Cpu}; use processor::{memory::Rom, Cpu};
use std::{
use crate::processor::memory::Memory; fs,
io::{stdout, Write},
};
use util::pause_then_step;
mod constants; mod constants;
mod processor; mod processor;
mod util; pub mod util;
#[macro_export] pub struct Options {
macro_rules! verbose_println { pub rom_path: String,
($($tts:tt)*) => { pub bootrom_path: Option<String>,
if $crate::is_verbose() { pub connect_serial: bool,
println!($($tts)*); pub verbose: bool,
} pub step_by: Option<usize>,
}; pub cycle_count: bool,
} pub factor: usize,
#[macro_export]
macro_rules! verbose_print {
($($tts:tt)*) => {
if $crate::is_verbose() {
print!($($tts)*);
}
};
} }
static mut PAUSE_ENABLED: bool = false; static mut PAUSE_ENABLED: bool = false;
@ -50,22 +44,12 @@ pub const HEIGHT: usize = 144;
static FACTOR: OnceCell<usize> = OnceCell::new(); static FACTOR: OnceCell<usize> = OnceCell::new();
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn init( pub fn init(options: Options, mut window: Window, tile_window: bool) {
rom_path: String, VERBOSE.set(options.verbose).unwrap();
bootrom_path: Option<String>, FACTOR.set(options.factor).unwrap();
mut window: Window,
connect_serial: bool,
tile_window: bool,
verbose: bool,
step_by: Option<usize>,
cycle_count: bool,
factor: usize,
) {
VERBOSE.set(verbose).unwrap();
FACTOR.set(factor).unwrap();
crate::processor::memory::mmio::gpu::init_statics(); crate::processor::memory::mmio::gpu::init_statics();
let rom: Rom = match fs::read(rom_path) { let rom: Rom = match fs::read(options.rom_path) {
Ok(data) => Rom::load(data), Ok(data) => Rom::load(data),
Err(e) => { Err(e) => {
println!("Error reading ROM: {e}"); println!("Error reading ROM: {e}");
@ -74,8 +58,8 @@ pub fn init(
}; };
window.set_title(format!("{} on {}", rom.get_title(), rom.mbc_type()).as_str()); window.set_title(format!("{} on {}", rom.get_title(), rom.mbc_type()).as_str());
let bootrom_enabled = bootrom_path.is_some(); let bootrom_enabled = options.bootrom_path.is_some();
let bootrom: Option<Vec<u8>> = if let Some(path) = bootrom_path { let bootrom: Option<Vec<u8>> = if let Some(path) = options.bootrom_path {
match fs::read(path) { match fs::read(path) {
Ok(data) => Some(data), Ok(data) => Some(data),
Err(e) => { Err(e) => {
@ -88,7 +72,7 @@ pub fn init(
}; };
let mut cpu = Cpu::new( let mut cpu = Cpu::new(
Memory::init(bootrom, rom, window, connect_serial, tile_window), Memory::init(bootrom, rom, window, options.connect_serial, tile_window),
bootrom_enabled, bootrom_enabled,
Gilrs::new().unwrap(), Gilrs::new().unwrap(),
); );
@ -96,11 +80,11 @@ pub fn init(
let mut cycle_num = 0; let mut cycle_num = 0;
verbose_println!("\n\n Begin execution...\n"); verbose_println!("\n\n Begin execution...\n");
match step_by { match options.step_by {
Some(step_size) => loop { Some(step_size) => loop {
for _ in 0..step_size { for _ in 0..step_size {
cycle_num += 1; cycle_num += 1;
if cycle_count { if options.cycle_count {
print_cycles(&cycle_num); print_cycles(&cycle_num);
} }
run_cycle(&mut cpu); run_cycle(&mut cpu);
@ -111,7 +95,7 @@ pub fn init(
}, },
None => loop { None => loop {
cycle_num += 1; cycle_num += 1;
if cycle_count { if options.cycle_count {
print_cycles(&cycle_num); print_cycles(&cycle_num);
} }
run_cycle(&mut cpu); run_cycle(&mut cpu);
@ -130,46 +114,3 @@ fn run_cycle(cpu: &mut Cpu) {
pause_then_step(); pause_then_step();
} }
} }
fn pause_then_step() {
unsafe {
if PAUSE_ENABLED {
let line = pause();
PAUSE_QUEUED = !line.contains("continue");
}
}
}
#[allow(dead_code)]
fn pause_once() {
println!("paused...");
pause();
}
fn pause() -> String {
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) => line,
Err(_) => String::from(""),
}
}
fn print_cycles(cycles: &i32) {
if *cycles % 45678 != 0 {
return;
}
let instructions_per_second = 400000;
print!(
"cycle {} - approx {} seconds on real hardware\r",
cycles,
cycles / instructions_per_second
);
stdout().flush().unwrap();
}
fn is_verbose() -> bool {
match VERBOSE.get() {
Some(v) => *v,
None => false,
}
}

View file

@ -3,8 +3,6 @@ use gb_emu::{HEIGHT, WIDTH};
use minifb::{Window, WindowOptions}; use minifb::{Window, WindowOptions};
/// Gameboy (DMG-A/B/C) emulator /// Gameboy (DMG-A/B/C) emulator
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@ -66,15 +64,15 @@ fn main() {
window.topmost(true); window.topmost(true);
gb_emu::init( let options = gb_emu::Options {
args.rom, rom_path: args.rom,
args.bootrom, bootrom_path: args.bootrom,
window, connect_serial: args.connect_serial,
args.connect_serial, verbose: args.verbose,
args.tile_window, step_by: args.step_by,
args.verbose, cycle_count: args.cycle_count,
args.step_by,
args.cycle_count,
factor, factor,
); };
gb_emu::init(options, window, args.tile_window);
} }

View file

@ -1,5 +1,69 @@
use crate::processor::Direction; use crate::{processor::Direction, PAUSE_ENABLED, PAUSE_QUEUED, VERBOSE};
use std::mem::transmute; use std::{
io::{self, stdout, Write},
mem::transmute,
};
#[macro_export]
macro_rules! verbose_println {
($($tts:tt)*) => {
if $crate::util::is_verbose() {
println!($($tts)*);
}
};
}
#[macro_export]
macro_rules! verbose_print {
($($tts:tt)*) => {
if $crate::util::is_verbose() {
print!($($tts)*);
}
};
}
pub(crate) fn pause_then_step() {
unsafe {
if PAUSE_ENABLED {
let line = pause();
PAUSE_QUEUED = !line.contains("continue");
}
}
}
#[allow(dead_code)]
pub(crate) fn pause_once() {
println!("paused...");
pause();
}
pub(crate) fn pause() -> String {
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) => line,
Err(_) => String::from(""),
}
}
pub(crate) fn print_cycles(cycles: &i32) {
if *cycles % 45678 != 0 {
return;
}
let instructions_per_second = 400000;
print!(
"cycle {} - approx {} seconds on real hardware\r",
cycles,
cycles / instructions_per_second
);
stdout().flush().unwrap();
}
pub(crate) fn is_verbose() -> bool {
match VERBOSE.get() {
Some(v) => *v,
None => false,
}
}
pub(crate) fn as_signed(unsigned: u8) -> i8 { pub(crate) fn as_signed(unsigned: u8) -> i8 {
unsafe { transmute(unsigned) } unsafe { transmute(unsigned) }