From a17bd972a99007099057d7901b88732b22d08270 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Mon, 7 Sep 2026 22:31:33 +0200 Subject: vnf: add the 'Machine.halt_on_brk' field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows developers to use 'brk' as asserts which can then be debugged. This is true by default but it can be turned off via the --no-halting-brk option from runrom. Signed-off-by: Miquel Sabaté Solà --- crates/runrom/src/main.rs | 10 ++++++++++ lib/vnf/src/lib.rs | 9 +++++++++ scripts/test-e2e.sh | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/runrom/src/main.rs b/crates/runrom/src/main.rs index 8f52f4a..9040261 100644 --- a/crates/runrom/src/main.rs +++ b/crates/runrom/src/main.rs @@ -16,6 +16,7 @@ struct Args { nasm: Option, dump_memory: bool, until_address: u16, + halt_on_brk: bool, } fn print_help() { @@ -26,6 +27,7 @@ fn print_help() { println!(" -f, --function\t\tRun the code by assuming it's a function."); println!(" -h, --help\t\t\tPrint this message and quit."); println!(" -n, --nasm-directory \tPath to the .nasm/ directory."); + println!(" --no-halting-brk\t\tDo not halt execution after a brk instruction."); println!(" -s, --start\t\t\tAddress from where to start (default: reset vector)."); println!(" -u, --until-address\t\tRun until the given address is met."); println!(" -v, --version\t\t\tPrint version information."); @@ -130,6 +132,8 @@ fn parse_arguments() -> Args { let mut start = None; let mut until_address = None; + res.halt_on_brk = true; + // Skip command name. args.next(); @@ -164,6 +168,9 @@ fn parse_arguments() -> Args { } None => die("you need to specify a file for the '-n/--nasm' flag".to_string()), }, + "--no-halting-brk" => { + res.halt_on_brk = false; + } "-u" | "--until-address" => { until_address = args.next(); if until_address.is_none() { @@ -260,12 +267,14 @@ fn run( start: u16, end: u16, assume_function: bool, + halt_on_brk: bool, dump_memory: bool, ) -> Result<(), String> { let mut machine = Machine::from(file, start, MemoryPolicy::default())?; machine.verbose = true; machine.run_function_mode = assume_function; + machine.halt_on_brk = halt_on_brk; machine.until_address(end)?; @@ -303,6 +312,7 @@ fn main() { start, args.until_address, args.assume_function, + args.halt_on_brk, args.dump_memory, ) { Ok(m) => m, diff --git a/lib/vnf/src/lib.rs b/lib/vnf/src/lib.rs index 61f117d..c9ae1a0 100644 --- a/lib/vnf/src/lib.rs +++ b/lib/vnf/src/lib.rs @@ -378,6 +378,10 @@ pub struct Machine { /// The status of both Joypads. joypads: [Joypad; 2], + + /// Whether execution should be halted _after_ a 'brk' instruction no matter + /// what. The break mark will be set accordingly. + pub halt_on_brk: bool, } // Returns a vector of MemoryCell representing the RAM for a Machine, which @@ -487,6 +491,7 @@ impl Machine { should_report_ppu: false, policy, joypads: [Joypad::default(), Joypad::default()], + halt_on_brk: true, }) } @@ -1220,6 +1225,10 @@ impl Machine { let low = &(self.prg_rom[len - 2] as usize); self.pc = high + low; self.skip_pc = true; + + if self.halt_on_brk { + self.active = false; + } } InstructionIdentifier::Rti => { self.pop_status_register()?; diff --git a/scripts/test-e2e.sh b/scripts/test-e2e.sh index 573a729..2dce6fb 100755 --- a/scripts/test-e2e.sh +++ b/scripts/test-e2e.sh @@ -284,7 +284,7 @@ exit_code=$((exit_code + $?)) echo "test: runrom => stack.nes" ./target/debug/nasm -Werror -o tests/out/stack.nes tests/runrom/stack.s -./target/debug/runrom --function --dump-memory tests/out/stack.nes > tests/out/stack.txt +./target/debug/runrom --no-halting-brk --function --dump-memory tests/out/stack.nes > tests/out/stack.txt diff tests/out/stack.txt tests/expected/runrom/stack.txt exit_code=$((exit_code + $?)) -- cgit v1.2.3