emu_nes

An NES emulator for learning how the 6502 and similar computers of the era worked
Log | Files | Refs | README | LICENSE

commit 4fcc8d48f4dd3f7be37d917826a148c02701d0e3
parent 2236e79556053d9d59a613634433848f1f7cf513
Author: vin <git@vineetk.net>
Date:   Wed,  3 Jul 2024 20:40:57 -0400

improve branching cycle accuracy

Diffstat:
Mcpu.c | 39+++++++++++++++++++++++----------------
1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/cpu.c b/cpu.c @@ -1,4 +1,5 @@ #include <libgen.h> +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -151,6 +152,20 @@ opcode_mem(enum addressing_mode mode) return val; } +static void +branch(uint16_t addr, bool cond) +{ + if (!cond) + return; + cycles++; + + addr += regs.pc; + if ((regs.pc + 1) & 0xFF00 != addr & 0xFF00) + cycles++; + + regs.pc = addr; +} + void ADC(uint16_t arg) { @@ -201,22 +216,19 @@ ASL(uint16_t mem) void BCC(uint16_t arg) { - if (regs.status.carry == 0) - regs.pc += arg, cycles++; + branch(arg, regs.status.carry == 0); } void BCS(uint16_t arg) { - if (regs.status.carry == 1) - regs.pc += arg, cycles++; + branch(arg, regs.status.carry == 1); } void BEQ(uint16_t arg) { - if (regs.status.zero == 1) - regs.pc += arg, cycles++; + branch(arg, regs.status.zero == 1); } void @@ -232,22 +244,19 @@ BIT(uint16_t arg) void BMI(uint16_t arg) { - if (regs.status.negative == 1) - regs.pc += arg, cycles++; + branch(arg, regs.status.negative == 1); } void BNE(uint16_t arg) { - if (regs.status.zero == 0) - regs.pc += arg, cycles++; + branch(arg, regs.status.zero == 0); } void BPL(uint16_t arg) { - if (regs.status.negative == 0) - regs.pc += arg, cycles++; + branch(arg, regs.status.negative == 0); } void @@ -263,16 +272,14 @@ BVC(uint16_t arg) { //regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) == 0; //if (regs.status.overflow == 0) - if ((STATUS_TO_INT() & (1 << 6)) == 0) - regs.pc += arg, cycles++; + branch(arg, (STATUS_TO_INT() & (1 << 6)) == 0); } void BVS(uint16_t arg) { regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) != 0; - if (regs.status.overflow == 1) - regs.pc += arg, cycles++; + branch(arg, regs.status.overflow == 1); } void