gb-emu/gb-emu-lib/src/lib.rs

157 lines
3.8 KiB
Rust
Raw Normal View History

2023-02-25 09:43:39 +11:00
#![feature(
exclusive_range_pattern,
let_chains,
slice_flatten,
async_closure,
bigint_helper_methods
)]
use crate::{
processor::memory::Memory,
util::{pause, print_cycles},
2023-02-25 09:43:39 +11:00
};
2023-03-03 18:08:28 +11:00
use connect::{AudioOutput, EmulatorMessage, Renderer};
2023-02-25 09:43:39 +11:00
use once_cell::sync::OnceCell;
use processor::{memory::Rom, Cpu};
use std::{
2023-03-02 11:29:54 +11:00
fs::{self},
io::{stdout, Write},
2023-03-02 11:29:54 +11:00
path::PathBuf,
process::exit,
str::FromStr,
sync::mpsc::Receiver,
};
use util::pause_then_step;
2023-02-25 09:43:39 +11:00
pub mod connect;
2023-02-25 09:43:39 +11:00
mod constants;
mod processor;
pub mod util;
pub struct Options {
pub rom_path: String,
2023-03-02 19:10:50 +11:00
pub save_path: Option<String>,
2023-03-03 09:40:16 +11:00
pub no_save: bool,
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;
pub fn init(
2023-03-02 11:29:54 +11:00
receiver: Receiver<EmulatorMessage>,
options: Options,
mut window: Box<dyn Renderer>,
2023-03-03 18:08:28 +11:00
output: AudioOutput,
tile_window: Option<Box<dyn Renderer>>,
) {
VERBOSE.set(options.verbose).unwrap();
2023-02-25 09:43:39 +11:00
2023-03-03 09:40:16 +11:00
let maybe_save = if options.no_save {
None
2023-03-02 19:10:50 +11:00
} else {
2023-03-03 09:40:16 +11:00
Some(if let Some(path) = options.save_path {
PathBuf::from_str(&path).unwrap()
} else {
PathBuf::from_str(&options.rom_path)
.unwrap()
.with_extension("sav")
})
2023-03-02 19:10:50 +11:00
};
2023-03-02 11:29:54 +11:00
let rom: Rom = match fs::read(options.rom_path) {
2023-03-02 11:29:54 +11:00
Ok(data) => Rom::load(data, maybe_save),
2023-02-25 09:43:39 +11:00
Err(e) => {
println!("Error reading ROM: {e}");
return;
}
};
window.prepare(WIDTH, HEIGHT);
window.set_title(format!("{} on {}", rom.get_title(), rom.mbc_type()));
2023-02-25 09:43:39 +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-03-03 18:08:28 +11:00
Memory::init(
bootrom,
rom,
window,
output,
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");
match options.step_by {
2023-02-25 09:43:39 +11:00
Some(step_size) => loop {
2023-03-03 11:20:47 +11:00
process_messages(&receiver, &mut cpu);
2023-02-25 09:43:39 +11:00
for _ in 0..step_size {
cycle_num += 1;
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 {
2023-03-03 11:20:47 +11:00
process_messages(&receiver, &mut cpu);
2023-02-25 09:43:39 +11:00
cycle_num += 1;
if options.cycle_count {
2023-02-25 09:43:39 +11:00
print_cycles(&cycle_num);
}
run_cycle(&mut cpu);
},
}
}
2023-03-03 11:20:47 +11:00
fn process_messages(receiver: &Receiver<EmulatorMessage>, cpu: &mut Cpu) {
while let Ok(msg) = receiver.try_recv() {
match msg {
EmulatorMessage::Stop => {
cpu.memory.flush_rom();
exit(0);
}
}
}
}
2023-02-25 09:43:39 +11:00
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();
}
}