Skip to content

Commit

Permalink
Handle CLI options with clap and allow enabling doctor mode via an at…
Browse files Browse the repository at this point in the history
…omic bool
  • Loading branch information
jonathanballs committed Sep 12, 2024
1 parent 08b8f42 commit 444e9a4
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 3 deletions.
120 changes: 120 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.17", features = ["derive"] }
colored = "2.1.0"
ctrlc = "3.4"
minifb = "0.27"
Expand Down
9 changes: 9 additions & 0 deletions src/debugger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering};

use crate::gameboy::GameBoy;
pub static DEBUG_MODE: AtomicBool = AtomicBool::new(false);
pub static GAMEBOY_DOCTOR: AtomicBool = AtomicBool::new(false);

pub fn enable_debug() {
DEBUG_MODE.store(true, Ordering::SeqCst);
Expand All @@ -15,6 +16,14 @@ pub fn is_debug_enabled() -> bool {
DEBUG_MODE.load(Ordering::SeqCst)
}

pub fn enable_gameboy_doctor() {
GAMEBOY_DOCTOR.store(true, Ordering::SeqCst);
}

pub fn is_gameboy_doctor() -> bool {
GAMEBOY_DOCTOR.load(Ordering::SeqCst)
}

pub fn debugger_cli(gameboy: &mut GameBoy) {
gameboy.debugger_cli();
}
29 changes: 26 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ pub mod mmu;
mod renderer;

use cartridge::header::CartridgeHeader;
use clap::Parser;
use colored::*;
use debugger::{enable_debug, is_debug_enabled};
use debugger::{enable_debug, enable_gameboy_doctor, is_debug_enabled};
use std::fs::File;
use std::io::Read;
use std::process::exit;
Expand All @@ -24,10 +25,32 @@ use renderer::window_loop;

pub static DEBUG_MODE: AtomicBool = AtomicBool::new(false);

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Whether to enable the doctor or not.
#[arg(short, long, default_value_t = false)]
doctor: bool,

rom_path: Option<String>,
}

fn main() {
let args = Args::parse();

let rom_path = match args.rom_path {
Some(path) => path,
_ => "roms/super-mario-land.gb".to_string(),
};

if args.doctor {
enable_gameboy_doctor();
}

let (tx, rx) = mpsc::channel::<PPU>();
let (tx_key, rx_key) = mpsc::channel::<(bool, Key)>();
let rom = read_file_to_bytes("roms/super-mario-land.gb").unwrap();
let rom = read_file_to_bytes(rom_path.as_str()).unwrap();
let game_title = CartridgeHeader::new(&rom).unwrap().title();

let _ = thread::spawn(move || emulator_loop(rom, tx, rx_key));
Expand All @@ -45,7 +68,7 @@ fn emulator_loop(rom: Vec<u8>, tx: Sender<PPU>, rx: Receiver<(bool, Key)>) {
} else {
// If running, pause the emulator
enable_debug();
println!("Received Ctrl+C! Pausing at the end of this step...");
println!("{}", "Received Ctrl+C! Entering debugger.".red());
}
})
.expect("Error setting Ctrl-C handler");
Expand Down

0 comments on commit 444e9a4

Please sign in to comment.