2023-02-25 09:43:39 +11:00
|
|
|
#![feature(
|
|
|
|
exclusive_range_pattern,
|
|
|
|
let_chains,
|
|
|
|
slice_flatten,
|
|
|
|
async_closure,
|
|
|
|
bigint_helper_methods
|
|
|
|
)]
|
|
|
|
|
2023-02-26 09:42:15 +11:00
|
|
|
use crate::{
|
|
|
|
processor::memory::Memory,
|
|
|
|
util::{pause, print_cycles},
|
2023-02-25 09:43:39 +11:00
|
|
|
};
|
2023-02-26 10:18:58 +11:00
|
|
|
use connect::Renderer;
|
2023-02-25 09:43:39 +11:00
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use processor::{memory::Rom, Cpu};
|
2023-02-26 09:42:15 +11:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
io::{stdout, Write},
|
|
|
|
};
|
|
|
|
use util::pause_then_step;
|
2023-02-25 09:43:39 +11:00
|
|
|
|
2023-02-26 10:18:58 +11:00
|
|
|
pub mod connect;
|
2023-02-25 09:43:39 +11:00
|
|
|
mod constants;
|
|
|
|
mod processor;
|
2023-02-26 09:42:15 +11:00
|
|
|
pub mod util;
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
pub rom_path: String,
|
|
|
|
pub bootrom_path: Option<String>,
|
|
|
|
pub connect_serial: bool,
|
|
|
|
pub verbose: bool,
|
|
|
|
pub step_by: Option<usize>,
|
|
|
|
pub cycle_count: bool,
|
2023-02-25 09:43:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-02-26 10:18:58 +11:00
|
|
|
pub fn init(
|
|
|
|
options: Options,
|
|
|
|
mut window: Box<dyn Renderer>,
|
|
|
|
tile_window: Option<Box<dyn Renderer>>,
|
|
|
|
) {
|
2023-02-26 09:42:15 +11:00
|
|
|
VERBOSE.set(options.verbose).unwrap();
|
2023-02-25 09:43:39 +11:00
|
|
|
|
2023-02-26 09:42:15 +11:00
|
|
|
let rom: Rom = match fs::read(options.rom_path) {
|
2023-02-25 09:43:39 +11:00
|
|
|
Ok(data) => Rom::load(data),
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error reading ROM: {e}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2023-02-26 10:18:58 +11:00
|
|
|
|
|
|
|
window.prepare(WIDTH, HEIGHT);
|
|
|
|
window.set_title(format!("{} on {}", rom.get_title(), rom.mbc_type()));
|
2023-02-25 09:43:39 +11:00
|
|
|
|
2023-02-26 09:42:15 +11:00
|
|
|
let bootrom_enabled = options.bootrom_path.is_some();
|
|
|
|
let bootrom: Option<Vec<u8>> = if let Some(path) = options.bootrom_path {
|
2023-02-25 09:43:39 +11:00
|
|
|
match fs::read(path) {
|
|
|
|
Ok(data) => Some(data),
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error reading bootROM: {e}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cpu = Cpu::new(
|
2023-02-26 09:42:15 +11:00
|
|
|
Memory::init(bootrom, rom, window, options.connect_serial, tile_window),
|
2023-02-25 09:43:39 +11:00
|
|
|
bootrom_enabled,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut cycle_num = 0;
|
|
|
|
|
|
|
|
verbose_println!("\n\n Begin execution...\n");
|
2023-02-26 09:42:15 +11:00
|
|
|
match options.step_by {
|
2023-02-25 09:43:39 +11:00
|
|
|
Some(step_size) => loop {
|
|
|
|
for _ in 0..step_size {
|
|
|
|
cycle_num += 1;
|
2023-02-26 09:42:15 +11:00
|
|
|
if options.cycle_count {
|
2023-02-25 09:43:39 +11:00
|
|
|
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;
|
2023-02-26 09:42:15 +11:00
|
|
|
if options.cycle_count {
|
2023-02-25 09:43:39 +11:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|