From 1601d0353342230a2a93baa6f008bc16a10137a1 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 8 Sep 2026 17:32:08 +0200 Subject: Add an option for setting a cycle limit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This can be quite handy when it comes to checking that a piece of code doesn't exceed a given amount of cycles, or when you are tuning a block of code in order to make it go beyond the current amount of cycles. Signed-off-by: Miquel Sabaté Solà --- lib/vnf/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'lib/vnf') diff --git a/lib/vnf/src/lib.rs b/lib/vnf/src/lib.rs index 0b15d6a..e5e280a 100644 --- a/lib/vnf/src/lib.rs +++ b/lib/vnf/src/lib.rs @@ -31,6 +31,20 @@ use std::path::Path; use xixanta::opcodes::AddressingMode; use xixanta::opcodes::{Instruction, InstructionIdentifier, OPCODES}; +/// Number of cycles from an NMI to the start of rendering on an NTSC system +/// (round down). +pub const NMI_NTSC_CYCLES: usize = 2273; + +/// Number of cycles to render a full frame on an NTSC system (round down). +pub const FRAME_NTSC_CYCLES: usize = 29780; + +/// Number of cycles from an NMI to the start of rendering on a PAL system +/// (round down). +pub const NMI_PAL_CYCLES: usize = 7459; + +/// Number of cycles to render a full frame on a PAL system (round down). +pub const FRAME_PAL_CYCLES: usize = 33247; + /// The 'status' register from the CPU. All flags are set as booleans for easier /// use, and the 'break_mark' byte contains the break mark from the last 'brk' /// instruction. @@ -382,6 +396,10 @@ pub struct Machine { /// Whether execution should be halted _after_ a 'brk' instruction no matter /// what. The break mark will be set accordingly. pub halt_on_brk: bool, + + /// The maximum amount of cycles that the machine should reach before + /// halting. + pub cycle_limit: Option, } // On success, returns a vector of MemoryCell representing the RAM for a @@ -497,6 +515,7 @@ impl Machine { policy, joypads: [Joypad::default(), Joypad::default()], halt_on_brk: true, + cycle_limit: None, }) } @@ -699,6 +718,17 @@ impl Machine { self.report(); } + // Did we reach the cycle limits? + if let Some(cl) = self.cycle_limit + && cl < self.cycles + { + self.active = false; + if self.verbose { + return Err("reached the limit of cycles available".to_string()); + } + return Ok(()); + } + // After moving the PC, is it out of bounds? if self.pc < 0x8000 { return Err("out of bounds: program counter is pointing below ROM space".to_string()); -- cgit v1.2.3