From 312f231d5d3b4523a5763eb01981885d0a66b8cd Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 20 Nov 2023 13:23:48 -0300 Subject: and, asl, enable/disable flags --- CPU.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'CPU.c') diff --git a/CPU.c b/CPU.c index db7948c..b44a84c 100644 --- a/CPU.c +++ b/CPU.c @@ -129,9 +129,9 @@ uint16_t get_operand_address(struct CPU *cpu, enum adressing_mode mode){ void update_zero_and_negative_flags(struct CPU *cpu, uint8_t result){ if(result == 0) - cpu->status |= 0b00000010; + enable_flag(cpu, ZERO);//cpu->status |= 0b00000010; else - cpu->status &= 0b11111101; + disable_flag(cpu, ZERO);//cpu->status &= 0b11111101; if((result & 0b10000000) != 0) cpu->status |= 0b10000000; @@ -154,6 +154,34 @@ void sta(struct CPU *cpu, enum adressing_mode mode){ mem_write(cpu, addr, cpu->register_a); } +void and(struct CPU *cpu, enum adressing_mode mode){ + uint16_t addr = get_operand_address(cpu, mode); + uint8_t value = mem_read(cpu, addr); + + cpu->register_a &= value; + update_zero_and_negative_flags(cpu, cpu->register_a); +} + +void enable_flag(struct CPU *cpu, uint8_t flag){ + cpu->status |= flag; +} + +void disable_flag(struct CPU *cpu, uint8_t flag){ + cpu->status &= !flag; +} + +void asl(struct CPU *cpu, enum adressing_mode mode){ + uint16_t addr = get_operand_address(cpu, mode); + uint8_t value = mem_read(cpu, addr); + + if(value >> 7 == 1) enable_flag(cpu, CARRY); + else disable_flag(cpu, CARRY); + + value = value << 1; + mem_write(cpu, addr, value); + update_zero_and_negative_flags(cpu, value); +} + void interpret(struct CPU *cpu){ while(1){ @@ -179,6 +207,21 @@ void interpret(struct CPU *cpu){ cpu->register_x++; update_zero_and_negative_flags(cpu, cpu->register_x); break; + case AND: + and(cpu, OPCODE[opcode].mode); + cpu->pc += OPCODE[opcode].bytes - 1; + break; + case ASL: + if(OPCODE[opcode].mode == NoneAddressing){ + uint8_t value = cpu->register_a; + if(value >> 7 == 1) enable_flag(cpu, CARRY); + else disable_flag(cpu, CARRY); + cpu->register_a = value << 1; + update_zero_and_negative_flags(cpu, cpu->register_a); + }else{ + asl(cpu, OPCODE[opcode].mode); + } + cpu->pc += OPCODE[opcode].bytes - 1; default: break; } -- cgit v1.2.3