OSDN Git Service

adc bug fixed!!!!
[motonesemu/motonesemu.git] / emulator / 6502core.c
index 521a703..4e9ecf3 100644 (file)
@@ -921,6 +921,7 @@ int func_ADC(void) {
     int done = FALSE;
     int ret;
     unsigned char data;
+    unsigned char old_carry;
 
     ret = load_addr_mode(&done);
     if (!ret)
@@ -930,11 +931,12 @@ int func_ADC(void) {
         return TRUE;
 
     data = get_cpu_data_buf();
+    old_carry = cpu_reg.status.carry;
     //signed, unsigned overflow check.
     set_ADD_carry(cpu_reg.acc, cpu_reg.status.carry, data);
     set_ADD_overflow(cpu_reg.acc, cpu_reg.status.carry, data);
     //add data with carry to accumurator.
-    cpu_reg.acc += data + cpu_reg.status.carry;
+    cpu_reg.acc += data + old_carry;
 
     // N/Z flags set.
     set_negative(cpu_reg.acc);
@@ -985,7 +987,7 @@ int func_ASL(void) {
     if (current_inst->addr_mode == ADDR_MODE_ACC) {
         unsigned char op_data = cpu_reg.acc;
         //set carry flag from the pre-opration value.
-        cpu_reg.status.carry = ((op_data & 0x80) > 0);
+        cpu_reg.status.carry = ((op_data & 0x80) != 0);
         cpu_reg.acc = (op_data << 1);
         set_negative(cpu_reg.acc);
         set_zero(cpu_reg.acc);
@@ -999,7 +1001,7 @@ int func_ASL(void) {
         if (operation) {
             unsigned char op_data = get_cpu_data_buf();
             //set carry flag from the pre-opration value.
-            cpu_reg.status.carry = ((op_data & 0x80) > 0);
+            cpu_reg.status.carry = ((op_data & 0x80) != 0);
             op_data = (op_data << 1);
             set_cpu_data_buf(op_data);
             return TRUE;
@@ -1828,10 +1830,12 @@ int func_ROL(void) {
 
     if (current_inst->addr_mode == ADDR_MODE_ACC) {
         unsigned char op_data = cpu_reg.acc;
+        unsigned char old_carry = cpu_reg.status.carry;
+
         //set carry flag from the pre-opration value.
-        cpu_reg.status.carry = ((op_data & 0x80) > 0);
+        cpu_reg.status.carry = ((op_data & 0x80) != 0);
         cpu_reg.acc = (op_data << 1);
-        if (cpu_reg.status.carry)
+        if (old_carry)
             cpu_reg.acc |= 0x01;
         set_negative(cpu_reg.acc);
         set_zero(cpu_reg.acc);
@@ -1844,10 +1848,12 @@ int func_ROL(void) {
 
         if (operation) {
             unsigned char op_data = get_cpu_data_buf();
+            unsigned char old_carry = cpu_reg.status.carry;
+
             //set carry flag from the pre-opration value.
-            cpu_reg.status.carry = ((op_data & 0x80) > 0);
+            cpu_reg.status.carry = ((op_data & 0x80) != 0);
             op_data = (op_data << 1);
-            if (cpu_reg.status.carry)
+            if (old_carry)
                 op_data |= 0x01;
             set_cpu_data_buf(op_data);
             return TRUE;
@@ -1880,10 +1886,12 @@ int func_ROR(void) {
 
     if (current_inst->addr_mode == ADDR_MODE_ACC) {
         unsigned char op_data = cpu_reg.acc;
+        unsigned char old_carry = cpu_reg.status.carry;
+
         //set carry flag from the pre-opration value.
         cpu_reg.status.carry = (op_data & 0x01);
         cpu_reg.acc = (op_data >> 1);
-        if (cpu_reg.status.carry)
+        if (old_carry)
             cpu_reg.acc |= 0x80;
         set_negative(cpu_reg.acc);
         set_zero(cpu_reg.acc);
@@ -1896,10 +1904,12 @@ int func_ROR(void) {
 
         if (operation) {
             unsigned char op_data = get_cpu_data_buf();
+            unsigned char old_carry = cpu_reg.status.carry;
+
             //set carry flag from the pre-opration value.
             cpu_reg.status.carry = (op_data & 0x01);
             op_data = (op_data >> 1);
-            if (cpu_reg.status.carry)
+            if (old_carry)
                 op_data |= 0x80;
             set_cpu_data_buf(op_data);
             return TRUE;
@@ -2342,7 +2352,7 @@ int nmi6502(void) {
             {
                 //step 3, push status_reg
                 unsigned char stat;
-                memcpy(&stat, &cpu_reg, sizeof(stat));
+                memcpy(&stat, &cpu_reg.status, sizeof(stat));
                 push(stat);
                 return TRUE;
             }