From 0209e32e98b135a5a689a28e8db06880d29bbedf Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 18 Dec 2023 14:34:08 -0300 Subject: branch opcodes --- CPU.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'CPU.c') diff --git a/CPU.c b/CPU.c index b44a84c..4e7dfa1 100644 --- a/CPU.c +++ b/CPU.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "CPU.h" #include "opcode_pc.h" @@ -170,6 +171,17 @@ void disable_flag(struct CPU *cpu, uint8_t flag){ cpu->status &= !flag; } +int test_flag(struct CPU *cpu, uint8_t flag){ + return (flag & cpu->status) > 0; +} + +void branch(struct CPU *cpu, bool cond){ + if(cond){ + int8_t jump = mem_read(cpu, cpu->pc); + cpu->pc = (cpu->pc + jump + 1) % 0x10000; + } +} + void asl(struct CPU *cpu, enum adressing_mode mode){ uint16_t addr = get_operand_address(cpu, mode); uint8_t value = mem_read(cpu, addr); @@ -222,6 +234,31 @@ void interpret(struct CPU *cpu){ asl(cpu, OPCODE[opcode].mode); } cpu->pc += OPCODE[opcode].bytes - 1; + break; + case BCC: + branch(cpu, !test_flag(cpu, CARRY)); + break; + case BCS: + branch(cpu, test_flag(cpu, CARRY)); + break; + case BEQ: + branch(cpu, test_flag(cpu, ZERO)); + break; + case BMI: + branch(cpu, test_flag(cpu, NEGATIV)); + break; + case BNE: + branch(cpu, !test_flag(cpu, ZERO)); + break; + case BPL: + branch(cpu, !test_flag(cpu, NEGATIV)); + break; + case BVC: + branch(cpu, !test_flag(cpu, OVERFLOW)); + break; + case BVS: + branch(cpu, test_flag(cpu, OVERFLOW)); + break; default: break; } -- cgit v1.2.3