gb-emu/src/main.rs

204 lines
4.3 KiB
Rust
Raw Normal View History

2023-02-11 21:43:36 +11:00
#![feature(exclusive_range_pattern, let_chains)]
2023-01-16 12:13:53 +11:00
mod processor;
2023-02-12 09:27:41 +11:00
mod util;
2023-01-16 12:13:53 +11:00
2023-01-22 12:13:02 +11:00
use clap::{ArgGroup, Parser};
2023-02-13 09:56:41 +11:00
use gilrs::Gilrs;
2023-02-03 09:15:30 +11:00
use minifb::{Window, WindowOptions};
2023-02-06 20:54:26 +11:00
use processor::{
2023-02-12 09:46:47 +11:00
memory::{rom::Rom, Memory},
Cpu,
2023-02-06 20:54:26 +11:00
};
2023-01-18 12:46:15 +11:00
use std::{
fs,
io::{self, stdout, Write},
2023-01-22 12:13:02 +11:00
sync::RwLock,
2023-01-18 12:46:15 +11:00
};
2023-01-22 12:13:02 +11:00
#[macro_export]
macro_rules! verbose_println {
($($tts:tt)*) => {
2023-02-12 09:41:34 +11:00
if $crate::is_verbose() {
2023-01-22 12:13:02 +11:00
println!($($tts)*);
}
};
}
#[macro_export]
macro_rules! verbose_print {
($($tts:tt)*) => {
2023-02-12 09:41:34 +11:00
if $crate::is_verbose() {
2023-01-22 12:13:02 +11:00
print!($($tts)*);
}
};
}
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
2023-01-22 12:13:02 +11:00
#[command(group(ArgGroup::new("prints").args(["verbose","cycle_count"])))]
struct Args {
/// ROM path
#[arg(short, long)]
rom: String,
2023-01-16 11:34:36 +11:00
/// BootROM path
#[arg(short, long)]
bootrom: String,
2023-01-16 14:43:11 +11:00
/// Just run BootROM
#[arg(long)]
run_bootrom: bool,
2023-01-17 08:58:37 +11:00
2023-01-22 12:13:02 +11:00
/// Verbose print
#[arg(short, long)]
verbose: bool,
/// Show cycle count
#[arg(short, long)]
cycle_count: bool,
/// Show tile window
#[arg(short, long)]
tile_window: bool,
2023-01-17 08:58:37 +11:00
/// Step emulation by...
#[arg(long)]
step_by: Option<usize>,
}
static mut PAUSE_ENABLED: bool = false;
static mut PAUSE_QUEUED: bool = false;
2023-01-22 12:13:02 +11:00
// static mut VERBOSE: bool = false;
static VERBOSE: RwLock<bool> = RwLock::new(false);
2023-02-03 09:15:30 +11:00
const WIDTH: usize = 160;
const HEIGHT: usize = 144;
2023-02-05 22:56:18 +11:00
const FACTOR: usize = 3;
2023-02-03 09:15:30 +11:00
fn main() {
let args = Args::parse();
2023-01-22 12:13:02 +11:00
{
let mut v = VERBOSE.write().unwrap();
*v = args.verbose;
}
2023-02-12 09:46:47 +11:00
let rom: Rom = match fs::read(args.rom) {
Ok(data) => Rom::load(data),
2023-02-09 11:57:51 +11:00
Err(e) => {
2023-02-12 08:51:23 +11:00
println!("Error reading ROM: {e}");
2023-02-09 11:57:51 +11:00
return;
}
};
let bootrom: Vec<u8> = match fs::read(args.bootrom) {
Ok(data) => data,
Err(e) => {
2023-02-12 08:51:23 +11:00
println!("Error reading bootROM: {e}");
2023-02-09 11:57:51 +11:00
return;
}
};
2023-02-07 09:51:11 +11:00
let mut window = Window::new(
2023-02-12 17:21:24 +11:00
format!("{} on {}", rom.get_title(), rom.mbc_type()).as_str(),
2023-02-05 22:56:18 +11:00
WIDTH * FACTOR,
HEIGHT * FACTOR,
WindowOptions::default(),
)
.unwrap_or_else(|e| {
2023-02-12 08:51:23 +11:00
panic!("{e}");
2023-02-05 22:56:18 +11:00
});
2023-02-03 09:15:30 +11:00
2023-02-06 12:44:58 +11:00
window.set_position(500, 50);
2023-02-05 18:46:55 +11:00
window.topmost(true);
2023-02-03 09:15:30 +11:00
2023-02-12 09:46:47 +11:00
let mut cpu = Cpu::new(
Memory::init(bootrom, args.run_bootrom, rom),
window,
args.tile_window,
2023-02-12 08:51:23 +11:00
args.run_bootrom,
2023-02-13 09:56:41 +11:00
Gilrs::new().unwrap(),
);
2023-01-18 12:46:15 +11:00
let mut cycle_num = 0;
2023-01-27 11:12:38 +11:00
verbose_println!("\n\n Begin execution...\n");
2023-01-17 08:58:37 +11:00
match args.step_by {
Some(step_size) => loop {
for _ in 0..step_size {
2023-01-18 12:46:15 +11:00
cycle_num += 1;
2023-01-22 12:13:02 +11:00
if args.cycle_count {
print_cycles(&cycle_num);
}
2023-02-12 08:51:23 +11:00
run_cycle(&mut cpu);
2023-01-18 12:46:15 +11:00
}
2023-02-12 08:51:23 +11:00
print!(" ...{cycle_num} cycles - press enter to continue\r");
2023-01-18 12:46:15 +11:00
stdout().flush().unwrap();
2023-02-12 08:51:23 +11:00
pause();
2023-01-17 08:58:37 +11:00
},
None => loop {
2023-01-18 12:46:15 +11:00
cycle_num += 1;
2023-01-22 12:13:02 +11:00
if args.cycle_count {
print_cycles(&cycle_num);
}
2023-02-12 08:51:23 +11:00
run_cycle(&mut cpu);
2023-01-17 08:58:37 +11:00
},
}
}
2023-01-15 21:05:28 +11:00
2023-02-12 09:46:47 +11:00
fn run_cycle(cpu: &mut Cpu) {
2023-02-12 08:51:23 +11:00
let will_pause = unsafe { PAUSE_QUEUED };
let pause_enabled = unsafe { PAUSE_ENABLED };
2023-01-22 12:13:02 +11:00
cpu.exec_next();
2023-02-12 09:41:34 +11:00
if !pause_enabled && cpu.reg.pc >= 0x100 {
unsafe { PAUSE_ENABLED = true };
2023-01-22 12:13:02 +11:00
}
2023-02-12 08:51:23 +11:00
if will_pause {
pause_then_step();
2023-01-22 12:13:02 +11:00
}
}
2023-02-12 08:51:23 +11:00
fn pause_then_step() {
unsafe {
if PAUSE_ENABLED {
2023-02-12 08:51:23 +11:00
let line = pause();
PAUSE_QUEUED = !line.contains("continue");
}
}
2023-01-15 21:05:28 +11:00
}
2023-01-27 11:26:33 +11:00
2023-02-12 08:51:23 +11:00
#[allow(dead_code)]
2023-01-22 12:13:02 +11:00
fn pause_once() {
2023-02-06 12:44:58 +11:00
println!("paused...");
2023-02-12 08:51:23 +11:00
pause();
}
fn pause() -> String {
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) => line,
Err(_) => String::from(""),
}
2023-01-22 12:13:02 +11:00
}
2023-01-18 12:46:15 +11:00
fn print_cycles(cycles: &i32) {
2023-02-02 19:01:04 +11:00
if *cycles % 45678 != 0 {
2023-01-18 12:46:15 +11:00
return;
}
let instructions_per_second = 400000;
print!(
"cycle {} - approx {} seconds on real hardware\r",
cycles,
cycles / instructions_per_second
);
stdout().flush().unwrap();
}
2023-01-22 12:13:02 +11:00
fn is_verbose() -> bool {
match VERBOSE.read() {
Ok(v) => *v,
Err(_) => false,
}
}