summaryrefslogtreecommitdiff
path: root/CPU.c
diff options
context:
space:
mode:
authorleo <leo@azuminha.com>2023-11-20 13:23:48 -0300
committerleo <leo@azuminha.com>2023-11-20 13:23:48 -0300
commit312f231d5d3b4523a5763eb01981885d0a66b8cd (patch)
tree1e7ade8b82f95fc56d12a4e41f16535634042eff /CPU.c
parent3dec06f9af2854969805cc3267a6095158a298dd (diff)
and, asl, enable/disable flags
Diffstat (limited to 'CPU.c')
-rw-r--r--CPU.c47
1 files changed, 45 insertions, 2 deletions
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;
}