begin move to lib

This commit is contained in:
Alex Janka 2023-02-25 09:43:39 +11:00
parent 567cbf0264
commit 0c45be7c58
2 changed files with 193 additions and 163 deletions

175
src/lib.rs Normal file
View file

@ -0,0 +1,175 @@
#![feature(
exclusive_range_pattern,
let_chains,
slice_flatten,
async_closure,
bigint_helper_methods
)]
use std::{
fs,
io::{self, stdout, Write},
};
use gilrs::Gilrs;
use minifb::Window;
use once_cell::sync::OnceCell;
use processor::{memory::Rom, Cpu};
use crate::processor::memory::Memory;
mod constants;
mod processor;
mod util;
#[macro_export]
macro_rules! verbose_println {
($($tts:tt)*) => {
if $crate::is_verbose() {
println!($($tts)*);
}
};
}
#[macro_export]
macro_rules! verbose_print {
($($tts:tt)*) => {
if $crate::is_verbose() {
print!($($tts)*);
}
};
}
static mut PAUSE_ENABLED: bool = false;
static mut PAUSE_QUEUED: bool = false;
static VERBOSE: OnceCell<bool> = OnceCell::new();
pub const WIDTH: usize = 160;
pub const HEIGHT: usize = 144;
static FACTOR: OnceCell<usize> = OnceCell::new();
#[allow(clippy::too_many_arguments)]
pub fn init(
rom_path: String,
bootrom_path: Option<String>,
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();
let rom: Rom = match fs::read(rom_path) {
Ok(data) => Rom::load(data),
Err(e) => {
println!("Error reading ROM: {e}");
return;
}
};
window.set_title(format!("{} on {}", rom.get_title(), rom.mbc_type()).as_str());
let bootrom_enabled = bootrom_path.is_some();
let bootrom: Option<Vec<u8>> = if let Some(path) = bootrom_path {
match fs::read(path) {
Ok(data) => Some(data),
Err(e) => {
println!("Error reading bootROM: {e}");
return;
}
}
} else {
None
};
let mut cpu = Cpu::new(
Memory::init(bootrom, rom, window, connect_serial, tile_window),
bootrom_enabled,
Gilrs::new().unwrap(),
);
let mut cycle_num = 0;
verbose_println!("\n\n Begin execution...\n");
match step_by {
Some(step_size) => loop {
for _ in 0..step_size {
cycle_num += 1;
if cycle_count {
print_cycles(&cycle_num);
}
run_cycle(&mut cpu);
}
print!(" ...{cycle_num} cycles - press enter to continue\r");
stdout().flush().unwrap();
pause();
},
None => loop {
cycle_num += 1;
if cycle_count {
print_cycles(&cycle_num);
}
run_cycle(&mut cpu);
},
}
}
fn run_cycle(cpu: &mut Cpu) {
let will_pause = unsafe { PAUSE_QUEUED };
let pause_enabled = unsafe { PAUSE_ENABLED };
cpu.exec_next();
if !pause_enabled && cpu.reg.pc >= 0x100 {
unsafe { PAUSE_ENABLED = true };
}
if will_pause {
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

@ -1,45 +1,9 @@
#![feature(
exclusive_range_pattern,
let_chains,
slice_flatten,
async_closure,
bigint_helper_methods
)]
mod constants;
mod processor;
mod util;
use clap::{ArgGroup, Parser};
use gilrs::Gilrs;
use gb_emu::{HEIGHT, WIDTH};
use minifb::{Window, WindowOptions};
use once_cell::sync::OnceCell;
use processor::{
memory::{Memory, Rom},
Cpu,
};
use std::{
fs,
io::{self, stdout, Write},
};
#[macro_export]
macro_rules! verbose_println {
($($tts:tt)*) => {
if $crate::is_verbose() {
println!($($tts)*);
}
};
}
#[macro_export]
macro_rules! verbose_print {
($($tts:tt)*) => {
if $crate::is_verbose() {
print!($($tts)*);
}
};
}
/// Gameboy (DMG-A/B/C) emulator
#[derive(Parser, Debug)]
@ -79,54 +43,19 @@ struct Args {
scale_factor: Option<usize>,
}
static mut PAUSE_ENABLED: bool = false;
static mut PAUSE_QUEUED: bool = false;
static VERBOSE: OnceCell<bool> = OnceCell::new();
const WIDTH: usize = 160;
const HEIGHT: usize = 144;
static FACTOR: OnceCell<usize> = OnceCell::new();
fn main() {
let args = Args::parse();
VERBOSE.set(args.verbose).unwrap();
FACTOR
.set(if let Some(factor) = args.scale_factor {
factor
} else {
3
})
.unwrap();
crate::processor::memory::mmio::gpu::init_statics();
let rom: Rom = match fs::read(args.rom) {
Ok(data) => Rom::load(data),
Err(e) => {
println!("Error reading ROM: {e}");
return;
}
};
let bootrom_enabled = args.bootrom.is_some();
let bootrom: Option<Vec<u8>> = if let Some(path) = args.bootrom {
match fs::read(path) {
Ok(data) => Some(data),
Err(e) => {
println!("Error reading bootROM: {e}");
return;
}
}
let factor = if let Some(factor) = args.scale_factor {
factor
} else {
None
3
};
let mut window = Window::new(
format!("{} on {}", rom.get_title(), rom.mbc_type()).as_str(),
WIDTH * FACTOR.get().unwrap(),
HEIGHT * FACTOR.get().unwrap(),
"emu",
WIDTH * factor,
HEIGHT * factor,
WindowOptions::default(),
)
.unwrap_or_else(|e| {
@ -137,89 +66,15 @@ fn main() {
window.topmost(true);
let mut cpu = Cpu::new(
Memory::init(bootrom, rom, window, args.connect_serial, args.tile_window),
bootrom_enabled,
Gilrs::new().unwrap(),
gb_emu::init(
args.rom,
args.bootrom,
window,
args.connect_serial,
args.tile_window,
args.verbose,
args.step_by,
args.cycle_count,
factor,
);
let mut cycle_num = 0;
verbose_println!("\n\n Begin execution...\n");
match args.step_by {
Some(step_size) => loop {
for _ in 0..step_size {
cycle_num += 1;
if args.cycle_count {
print_cycles(&cycle_num);
}
run_cycle(&mut cpu);
}
print!(" ...{cycle_num} cycles - press enter to continue\r");
stdout().flush().unwrap();
pause();
},
None => loop {
cycle_num += 1;
if args.cycle_count {
print_cycles(&cycle_num);
}
run_cycle(&mut cpu);
},
}
}
fn run_cycle(cpu: &mut Cpu) {
let will_pause = unsafe { PAUSE_QUEUED };
let pause_enabled = unsafe { PAUSE_ENABLED };
cpu.exec_next();
if !pause_enabled && cpu.reg.pc >= 0x100 {
unsafe { PAUSE_ENABLED = true };
}
if will_pause {
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,
}
}