diff options
author | leo <leo@azuminha.com> | 2024-03-18 21:21:20 -0300 |
---|---|---|
committer | leo <leo@azuminha.com> | 2024-03-18 21:21:20 -0300 |
commit | 1cffb789b5d258c3d6fced0fb949f068b1b23bae (patch) | |
tree | 069aa9dc20433c38893cc4354cab63d626415708 | |
parent | 21bb49029a4a30c80714a4b05193e1ba58117d5a (diff) |
CLC
CLD
CLI
CLV
CMP
CPX
CPY
-rw-r--r-- | CPU.c | 37 | ||||
-rw-r--r-- | CPU.h | 3 | ||||
-rw-r--r-- | opcode_pc.h | 35 |
3 files changed, 73 insertions, 2 deletions
@@ -194,6 +194,20 @@ void asl(struct CPU *cpu, enum adressing_mode mode){ update_zero_and_negative_flags(cpu, value); } +void compare(struct CPU *cpu, enum adressing_mode mode, uint8_t comp_val){ + uint16_t addr = get_operand_address(cpu, mode); + uint8_t value = mem_read(cpu, addr); + + + if(comp_val >= value){ + enable_flag(cpu, CARRY); + }else{ + disable_flag(cpu, CARRY); + } + + update_zero_and_negative_flags(cpu, (comp_val - value)); +} + void interpret(struct CPU *cpu){ while(1){ @@ -259,6 +273,27 @@ void interpret(struct CPU *cpu){ case BVS: branch(cpu, test_flag(cpu, OVERFLOW)); break; + case CLC: + disable_flag(cpu, CARRY); + break; + case CLD: + disable_flag(cpu, DECIMAL_MODE); + break; + case CLI: + disable_flag(cpu, INTERRUPT_DISABLE); + break; + case CLV: + disable_flag(cpu, OVERFLOW); + break; + case CMP: + compare(cpu, OPCODE[opcode].mode, cpu->register_a); + break; + case CPX: + compare(cpu, OPCODE[opcode].mode, cpu->register_x); + break; + case CPY: + compare(cpu, OPCODE[opcode].mode, cpu->register_y); + break; default: break; } @@ -314,4 +349,4 @@ int main(){ load_and_run(&cpu_6, program_6); assert(cpu_6.register_a == 0x55); printf("PASSED\n"); -}
\ No newline at end of file +} @@ -1,5 +1,6 @@ #ifndef _CPU #define _CPU +#include <stdint.h> #define CARRY 0b00000001 #define ZERO 0b00000010 @@ -53,5 +54,7 @@ int test_flag(struct CPU *cpu, uint8_t flag); void asl(struct CPU *cpu, enum adressing_mode mode); void branch(struct CPU *cpu, bool cond); +void compare(struct CPU *cpu, enum adressing_mode mode, uint8_t comp_val)); + #endif
\ No newline at end of file diff --git a/opcode_pc.h b/opcode_pc.h index 0d79aad..30a0d7b 100644 --- a/opcode_pc.h +++ b/opcode_pc.h @@ -1,6 +1,7 @@ #ifndef _OPCODEPC #define _OPCODEPC #include "CPU.h" +#include <stdint.h> enum _INSTRUCTION{ ADC = 1, @@ -19,6 +20,13 @@ enum _INSTRUCTION{ BPL, BVC, BVS, + CLC, + CLD, + CLI, + CLV, + CMP, + CPX, + CPY, }; struct _OPCODE{ @@ -82,6 +90,31 @@ void init_opcode_pc(){ OPCODE[0x50] = (struct _OPCODE){BVC, NoneAddressing, 2}; /*BVS*/ OPCODE[0x70] = (struct _OPCODE){BVS, NoneAddressing, 2}; + /*CLC*/ + OPCODE[0x18] = (struct _OPCODE){CLC, NoneAddressing, 2}; + /*CLD*/ + OPCODE[0xD8] = (struct _OPCODE){CLD, NoneAddressing, 2}; + /*CLI*/ + OPCODE[0x58] = (struct _OPCODE){CLI, NoneAddressing, 2}; + /*CLV*/ + OPCODE[0xB8] = (struct _OPCODE){CLV, NoneAddressing, 2}; + /*CMP*/ + OPCODE[0xC9] = (struct _OPCODE){CMP, Immediate, 2}; + OPCODE[0xC5] = (struct _OPCODE){CMP, ZeroPage, 2}; + OPCODE[0xD5] = (struct _OPCODE){CMP, ZeroPage_X, 2}; + OPCODE[0xCD] = (struct _OPCODE){CMP, Absolute, 3}; + OPCODE[0xDD] = (struct _OPCODE){CMP, Absolute_X, 3}; + OPCODE[0xD9] = (struct _OPCODE){CMP, Absolute_Y, 3}; + OPCODE[0xC1] = (struct _OPCODE){CMP, Indirect_X, 2}; + OPCODE[0xD1] = (struct _OPCODE){CMP, Indirect_Y, 2}; + /*CPX*/ + OPCODE[0xE0] = (struct _OPCODE){CPX, Immediate, 2}; + OPCODE[0xE4] = (struct _OPCODE){CPX, ZeroPage, 2}; + OPCODE[0xEC] = (struct _OPCODE){CPX, Absolute, 3}; + /*CPY*/ + OPCODE[0xC0] = (struct _OPCODE){CPY, Immediate, 2}; + OPCODE[0xC4] = (struct _OPCODE){CPY, ZeroPage, 2}; + OPCODE[0xCC] = (struct _OPCODE){CPY, Absolute, 3}; } -#endif
\ No newline at end of file +#endif |