From bf2b0122f20bb6ddc628b2b8f45de7e1943d139e Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 25 Aug 2026 17:48:54 +0200 Subject: vnf: fix the accounting of cycles on branches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was broken since the very beginning. Signed-off-by: Miquel Sabaté Solà --- lib/vnf/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'lib/vnf') diff --git a/lib/vnf/src/lib.rs b/lib/vnf/src/lib.rs index 002c4da..4ba04c1 100644 --- a/lib/vnf/src/lib.rs +++ b/lib/vnf/src/lib.rs @@ -1012,11 +1012,19 @@ impl Machine { self.pc - val.wrapping_neg() as usize }; + // If the next PC results in crossing the page boundary compared to the + // old PC, then there is a page penalty to be accounted. That is, + // branch instructions are defined with a base 'cycles' count, and then + // needs to be increased either by 1 or 2 depending on whether there was + // a page penalty or not. We could have easily defined the base 'cycles' + // with an increased value of 1 and then only increase by one on page + // penalty, but the definition tries to be close to the 6502 + // specification, which is defined like so. Hence, we keep this quirk. if (next & 0xFF00) == (self.pc & 0xFF00) { + self.extra_cycles += 1; + } else { self.extra_cycles += 2; self.page_penalty += 1; - } else { - self.extra_cycles += 1; } self.pc = next; // TODO -- cgit v1.2.3