Fix more booboos, it now runs the BASIC test the same as alternate backend
authorNick Downing <nick@ndcode.org>
Mon, 25 Jul 2022 09:39:49 +0000 (19:39 +1000)
committerNick Downing <nick@ndcode.org>
Mon, 25 Jul 2022 10:11:43 +0000 (20:11 +1000)
cpu_6800.c
cpu_6800.h
decode_6800.sed
sim68xx

index ad77599..b159ea4 100644 (file)
@@ -77,10 +77,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_tpa(self);
     break;
   case 0x08:
-    cpu_6800_in(self, CPU_6800_EA_X);
+    cpu_6800_inx(self);
     break;
   case 0x09:
-    cpu_6800_de(self, CPU_6800_EA_X);
+    cpu_6800_dex(self);
     break;
   case 0x0a:
     cpu_6800_cl(self, CPU_6800_REG_P_BIT_V);
@@ -104,7 +104,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_sub(self, CPU_6800_EA_A, self->regs.byte.b);
     break;
   case 0x11:
-    cpu_6800_cmp(self, CPU_6800_EA_A, self->regs.byte.b);
+    cpu_6800_cmp(self, self->regs.byte.a, self->regs.byte.b);
     break;
   case 0x12:
     cpu_6800_ill(self);
@@ -197,10 +197,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bra(self, self->regs.bit.nf || self->regs.bit.vf || self->regs.bit.zf, cpu_6800_ea_relative(self));
     break;
   case 0x30:
-    cpu_6800_st(self, self->regs.word.s, CPU_6800_EA_X);
+    cpu_6800_tsx(self);
     break;
   case 0x31:
-    cpu_6800_in(self, CPU_6800_EA_S);
+    cpu_6800_ins(self);
     break;
   case 0x32:
     cpu_6800_pul(self, CPU_6800_EA_A);
@@ -209,10 +209,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_pul(self, CPU_6800_EA_B);
     break;
   case 0x34:
-    cpu_6800_de(self, CPU_6800_EA_S);
+    cpu_6800_des(self);
     break;
   case 0x35:
-    cpu_6800_st(self, self->regs.word.x, CPU_6800_EA_S);
+    cpu_6800_txs(self);
     break;
   case 0x36:
     cpu_6800_psh(self, self->regs.byte.a);
index a7a9400..0360052 100644 (file)
@@ -216,27 +216,31 @@ static ALWAYS_INLINE int cpu_6800_ea_direct_indexed(struct cpu_6800 *self, int r
 static ALWAYS_INLINE void cpu_6800_adc(struct cpu_6800 *self, int lvalue, int rvalue) {
   int data = cpu_6800_read_byte(self, lvalue);
 
-  int result0 = self->regs.bit.cf + (data & 0x7f) + (rvalue & 0x7f);
-  int result1 = result0 + (data & 0x80) + (rvalue & 0x80);
+  int result0 = (data & 0xf) + (rvalue & 0xf) + self->regs.bit.cf;
+  int result1 = result0 + (data & 0x70) + (rvalue & 0x70);
+  int result2 = result1 + (data & 0x80) + (rvalue & 0x80);
 
-  cpu_6800_write_byte(self, lvalue, result1 & 0xff);
-  self->regs.bit.cf = result1 >> 8;
-  self->regs.bit.zf = (result1 & 0xff) == 0;
-  self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
-  self->regs.bit.nf = (result1 >> 7) & 1;
+  cpu_6800_write_byte(self, lvalue, result2 & 0xff);
+  self->regs.bit.hf = result0 >> 4;
+  self->regs.bit.nf = (result2 >> 7) & 1;
+  self->regs.bit.zf = (result2 & 0xff) == 0;
+  self->regs.bit.vf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
+  self->regs.bit.cf = result2 >> 8;
 }
 
 static ALWAYS_INLINE void cpu_6800_add(struct cpu_6800 *self, int lvalue, int rvalue) {
   int data = cpu_6800_read_byte(self, lvalue);
 
-  int result0 = (data & 0x7f) + (rvalue & 0x7f);
-  int result1 = result0 + (data & 0x80) + (rvalue & 0x80);
+  int result0 = (data & 0xf) + (rvalue & 0xf);
+  int result1 = result0 + (data & 0x70) + (rvalue & 0x70);
+  int result2 = result1 + (data & 0x80) + (rvalue & 0x80);
 
-  cpu_6800_write_byte(self, lvalue, result1 & 0xff);
-  self->regs.bit.cf = result1 >> 8;
-  self->regs.bit.zf = (result1 & 0xff) == 0;
-  self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
-  self->regs.bit.nf = (result1 >> 7) & 1;
+  cpu_6800_write_byte(self, lvalue, result2 & 0xff);
+  self->regs.bit.hf = result0 >> 4;
+  self->regs.bit.nf = (result2 >> 7) & 1;
+  self->regs.bit.zf = (result2 & 0xff) == 0;
+  self->regs.bit.vf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
+  self->regs.bit.cf = result2 >> 8;
 }
 
 static ALWAYS_INLINE void cpu_6800_and(struct cpu_6800 *self, int lvalue, int rvalue) {
@@ -255,7 +259,7 @@ static ALWAYS_INLINE void cpu_6800_asr(struct cpu_6800 *self, int lvalue) {
   ++self->cycles;
 
   cpu_6800_write_byte(self, lvalue, result >> 1);
-  self->regs.bit.nf = false;
+  self->regs.bit.nf = result >> 8;
   self->regs.bit.zf = (result & 0xfe) == 0;
   self->regs.bit.vf = (result ^ (result >> 8)) & 1;
   self->regs.bit.cf = result & 1;
@@ -310,11 +314,13 @@ static ALWAYS_INLINE void cpu_6800_cmp(struct cpu_6800 *self, int rvalue0, int r
 }
 
 static ALWAYS_INLINE void cpu_6800_com(struct cpu_6800 *self, int lvalue) {
-  int result = ~cpu_6800_read_byte(self, lvalue);
+  int result = cpu_6800_read_byte(self, lvalue) ^ 0xff;
 
   cpu_6800_write_byte(self, lvalue, result);
-  self->regs.bit.nf = true;
-  self->regs.bit.hf = true;
+  self->regs.bit.nf = result >> 7;
+  self->regs.bit.zf = result == 0;
+  self->regs.bit.vf = false;
+  self->regs.bit.cf = true;
 }
 
 static ALWAYS_INLINE void cpu_6800_cp(struct cpu_6800 *self, int rvalue0, int rvalue1) {
@@ -330,20 +336,30 @@ static ALWAYS_INLINE void cpu_6800_daa(struct cpu_6800 *self) {
   abort();
 }
 
-static ALWAYS_INLINE void cpu_6800_de(struct cpu_6800 *self, int lvalue) {
-  int result = (cpu_6800_read_word(self, lvalue) - 1) & 0xffff;
+static ALWAYS_INLINE void cpu_6800_des(struct cpu_6800 *self) {
+  --self->regs.word.s;
+  ++self->cycles;
+}
+
+static ALWAYS_INLINE void cpu_6800_dex(struct cpu_6800 *self) {
+  int result = (self->regs.word.x - 1) & 0xffff;
   ++self->cycles;
-  cpu_6800_write_word(self, lvalue, result);
+
+  self->regs.word.x = result;
   self->regs.bit.zf = result == 0;
-  self->regs.bit.nf = result >> 15;
 }
 
 static ALWAYS_INLINE void cpu_6800_dec(struct cpu_6800 *self, int lvalue) {
-  int result = (cpu_6800_read_byte(self, lvalue) - 1) & 0xff;
+  int data = cpu_6800_read_byte(self, lvalue);
+
+  int result0 = (data & 0x7f) - 1;
+  int result1 = result0 + (data & 0x80);
   ++self->cycles;
-  cpu_6800_write_byte(self, lvalue, result);
-  self->regs.bit.zf = result == 0;
-  self->regs.bit.nf = result >> 7;
+
+  cpu_6800_write_byte(self, lvalue, result1 & 0xff);
+  self->regs.bit.nf = (result1 >> 7) & 1;
+  self->regs.bit.zf = (result1 & 0xff) == 0;
+  self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
 }
 
 static ALWAYS_INLINE void cpu_6800_eor(struct cpu_6800 *self, int lvalue, int rvalue) {
@@ -359,20 +375,30 @@ static ALWAYS_INLINE void cpu_6800_ill(struct cpu_6800 *self) {
   abort();
 }
 
-static ALWAYS_INLINE void cpu_6800_in(struct cpu_6800 *self, int lvalue) {
-  int result = (cpu_6800_read_word(self, lvalue) + 1) & 0xffff;
+static ALWAYS_INLINE void cpu_6800_ins(struct cpu_6800 *self) {
+  ++self->regs.word.s;
   ++self->cycles;
-  cpu_6800_write_word(self, lvalue, result);
+}
+
+static ALWAYS_INLINE void cpu_6800_inx(struct cpu_6800 *self) {
+  int result = (self->regs.word.x + 1) & 0xffff;
+  ++self->cycles;
+
+  self->regs.word.x = result;
   self->regs.bit.zf = result == 0;
-  self->regs.bit.nf = result >> 15;
 }
 
 static ALWAYS_INLINE void cpu_6800_inc(struct cpu_6800 *self, int lvalue) {
-  int result = (cpu_6800_read_byte(self, lvalue) + 1) & 0xff;
+  int data = cpu_6800_read_byte(self, lvalue);
+
+  int result0 = (data & 0x7f) + 1;
+  int result1 = result0 + (data & 0x80);
   ++self->cycles;
-  cpu_6800_write_byte(self, lvalue, result);
-  self->regs.bit.zf = result == 0;
-  self->regs.bit.nf = result >> 7;
+
+  cpu_6800_write_byte(self, lvalue, result1 & 0xff);
+  self->regs.bit.nf = (result1 >> 7) & 1;
+  self->regs.bit.zf = (result1 & 0xff) == 0;
+  self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
 }
 
 static ALWAYS_INLINE void cpu_6800_swi(struct cpu_6800 *self, int lvalue) {
@@ -411,38 +437,40 @@ static ALWAYS_INLINE void cpu_6800_lda(struct cpu_6800 *self, int lvalue, int rv
 static ALWAYS_INLINE void cpu_6800_lsl(struct cpu_6800 *self, int lvalue) {
   int result = cpu_6800_read_byte(self, lvalue) << 1;
   ++self->cycles;
+
   cpu_6800_write_byte(self, lvalue, result & 0xff);
-  self->regs.bit.cf = result >> 8;
-  self->regs.bit.zf = (result & 0xff) == 0;
   self->regs.bit.nf = (result >> 7) & 1;
+  self->regs.bit.zf = (result & 0xff) == 0;
+  self->regs.bit.vf = ((result >> 7) ^ (result >> 8)) & 1;
+  self->regs.bit.cf = result >> 8;
 }
 
 static ALWAYS_INLINE void cpu_6800_lsr(struct cpu_6800 *self, int lvalue) {
-  int result = cpu_6800_read_byte(self, lvalue);
+  int data = cpu_6800_read_byte(self, lvalue);
+
+  int result = data;
   ++self->cycles;
+
   cpu_6800_write_byte(self, lvalue, result >> 1);
-  self->regs.bit.cf = result & 1;
+  self->regs.bit.nf = result >> 8;
   self->regs.bit.zf = (result & 0xfe) == 0;
-  self->regs.bit.nf = false;
+  self->regs.bit.vf = (result ^ (result >> 8)) & 1;
+  self->regs.bit.cf = result & 1;
 }
 
 static ALWAYS_INLINE void cpu_6800_neg(struct cpu_6800 *self, int lvalue) {
   int data = cpu_6800_read_byte(self, lvalue);
 
-  int result0 = -(data & 0xf);
-  int result1 = result0 - (data & 0x70);
-  int result2 = result1 - (data & 0x80);
+  int result0 = -(data & 0x7f);
+  int result1 = result0 - (data & 0x80);
 
-  cpu_6800_write_byte(self, lvalue, result2 & 0xff);
-  self->regs.bit.cf = (result2 >> 8) & 1;
-  self->regs.bit.nf = true;
-  self->regs.bit.vf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
-  self->regs.bit.hf = (result0 >> 4) & 1;
-  self->regs.bit.zf = (result2 & 0xff) == 0;
-  self->regs.bit.nf = (result2 >> 7) & 1;
+  cpu_6800_write_byte(self, lvalue, result1 & 0xff);
+  self->regs.bit.nf = (result1 >> 7) & 1;
+  self->regs.bit.zf = (result1 & 0xff) == 0;
+  self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
+  self->regs.bit.cf = (result1 >> 8) & 1;
 }
 
-
 static ALWAYS_INLINE void cpu_6800_nop(struct cpu_6800 *self) {
 }
 
@@ -455,10 +483,6 @@ static ALWAYS_INLINE void cpu_6800_ora(struct cpu_6800 *self, int lvalue, int rv
   self->regs.bit.vf = false;
 }
 
-//static ALWAYS_INLINE void cpu_6800_plp(struct cpu_6800 *self) {
-//  self->regs.byte.p = cpu_6800_pop_byte(self) | 0xc0;
-//}
-
 static ALWAYS_INLINE void cpu_6800_psh(struct cpu_6800 *self, int rvalue) {
   cpu_6800_push_byte(self, rvalue);
 }
@@ -467,28 +491,26 @@ static ALWAYS_INLINE void cpu_6800_pul(struct cpu_6800 *self, int lvalue) {
   cpu_6800_write_byte(self, lvalue, cpu_6800_pop_byte(self));
 }
 
-//static ALWAYS_INLINE void cpu_6800_rmb(struct cpu_6800 *self, int n, int lvalue) {
-//  int result = cpu_6800_read_byte(self, lvalue) & ~(1 << n);
-//  ++self->cycles;
-//  cpu_6800_write_byte(self, lvalue, result);
-//}
-
 static ALWAYS_INLINE void cpu_6800_rol(struct cpu_6800 *self, int lvalue) {
   int result = self->regs.bit.cf | (cpu_6800_read_byte(self, lvalue) << 1);
   ++self->cycles;
+
   cpu_6800_write_byte(self, lvalue, result & 0xff);
-  self->regs.bit.cf = result >> 8;
-  self->regs.bit.zf = (result & 0xff) == 0;
   self->regs.bit.nf = (result >> 7) & 1;
+  self->regs.bit.zf = (result & 0xff) == 0;
+  self->regs.bit.vf = ((result >> 7) ^ (result >> 8)) & 1;
+  self->regs.bit.cf = result >> 8;
 }
 
 static ALWAYS_INLINE void cpu_6800_ror(struct cpu_6800 *self, int lvalue) {
   int result = cpu_6800_read_byte(self, lvalue) | (self->regs.bit.cf << 8);
   ++self->cycles;
   cpu_6800_write_byte(self, lvalue, result >> 1);
-  self->regs.bit.cf = result & 1;
+
+  self->regs.bit.nf = result >> 8;
   self->regs.bit.zf = (result & 0x1fe) == 0;
-  self->regs.bit.nf = (result >> 8) & 1;
+  self->regs.bit.vf = (result ^ (result >> 8)) & 1;
+  self->regs.bit.cf = result & 1;
 }
 
 static ALWAYS_INLINE void cpu_6800_rti(struct cpu_6800 *self) {
@@ -502,28 +524,21 @@ static ALWAYS_INLINE void cpu_6800_rts(struct cpu_6800 *self) {
 
 static ALWAYS_INLINE void cpu_6800_sbc(struct cpu_6800 *self, int lvalue, int rvalue) {
   int data = cpu_6800_read_byte(self, lvalue);
-  rvalue ^= 0xff;
 
-  int result0 = self->regs.bit.cf + (data & 0x7f) + (rvalue & 0x7f);
-  int result1 = result0 + (data & 0x80) + (rvalue & 0x80);
+  int result0 = (data & 0x7f) - (rvalue & 0x7f) - self->regs.bit.cf;
+  int result1 = result0 + (data & 0x80) - (rvalue & 0x80);
 
   cpu_6800_write_byte(self, lvalue, result1 & 0xff);
-  self->regs.bit.cf = result1 >> 8;
+  self->regs.bit.nf = (result1 >> 7) & 1;
   self->regs.bit.zf = (result1 & 0xff) == 0;
   self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
-  self->regs.bit.nf = (result1 >> 7) & 1;
+  self->regs.bit.cf = (result1 >> 8) & 1;
 }
 
 static ALWAYS_INLINE void cpu_6800_se(struct cpu_6800 *self, int n) {
   self->regs.byte.p |= 1 << n;
 }
 
-//static ALWAYS_INLINE void cpu_6800_smb(struct cpu_6800 *self, int n, int lvalue) {
-//  int result = cpu_6800_read_byte(self, lvalue) | (1 << n);
-//  ++self->cycles;
-//  cpu_6800_write_byte(self, lvalue, result);
-//}
-
 static ALWAYS_INLINE void cpu_6800_st(struct cpu_6800 *self, int rvalue, int lvalue) {
   cpu_6800_write_word(self, lvalue, rvalue);
 
@@ -561,27 +576,19 @@ static ALWAYS_INLINE void cpu_6800_tpa(struct cpu_6800 *self) {
   self->regs.byte.a = self->regs.byte.p;
 }
 
-//static ALWAYS_INLINE void cpu_6800_trb(struct cpu_6800 *self, int lvalue) {
-//  int result = cpu_6800_read_byte(self, lvalue);
-//  ++self->cycles;
-//  cpu_6800_write_byte(self, lvalue, result & ~self->regs.byte.a);
-//  result &= self->regs.byte.a;
-//  self->regs.bit.zf = result == 0;
-//}
-//
-//static ALWAYS_INLINE void cpu_6800_tsb(struct cpu_6800 *self, int lvalue) {
-//  int result = cpu_6800_read_byte(self, lvalue);
-//  ++self->cycles;
-//  cpu_6800_write_byte(self, lvalue, result | self->regs.byte.a);
-//  result &= self->regs.byte.a;
-//  self->regs.bit.zf = result == 0;
-//}
-
 static ALWAYS_INLINE void cpu_6800_tst(struct cpu_6800 *self, int rvalue) {
   self->regs.bit.zf = rvalue == 0;
   self->regs.bit.nf = rvalue >> 7;
 }
 
+static ALWAYS_INLINE void cpu_6800_tsx(struct cpu_6800 *self) {
+  self->regs.word.x = (self->regs.word.s + 1) & 0xffff;
+}
+
+static ALWAYS_INLINE void cpu_6800_txs(struct cpu_6800 *self) {
+  self->regs.word.s = (self->regs.word.x - 1) & 0xffff;
+}
+
 static ALWAYS_INLINE void cpu_6800_wai(struct cpu_6800 *self) {
   self->regs.bit.wai_flag = true;
 }
index 8903cf2..666b40c 100644 (file)
@@ -15,16 +15,12 @@ s/cpu_6800_blt(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, self->regs.bit.
 s/cpu_6800_st\([xs]\)(self/cpu_6800_st(self, self->regs.word.\1/
 s/cpu_6800_ld\([xs]\)(self\(.*\)byte/cpu_6800_ld(self, CPU_6800_EA_\1\2word/
 s/cpu_6800_ld\([xs]\)(self/cpu_6800_ld(self, CPU_6800_EA_\1/
-s/cpu_6800_txs(self/cpu_6800_st(self, self->regs.word.x, CPU_6800_EA_S/
-s/cpu_6800_tsx(self/cpu_6800_st(self, self->regs.word.s, CPU_6800_EA_X/
 s/cpu_6800_a\([ab]\)\([ab]\)(self/cpu_6800_add(self, CPU_6800_EA_\2, self->regs.byte.\1/
-s/cpu_6800_c\([ab]\)\([ab]\)(self/cpu_6800_cmp(self, CPU_6800_EA_\2, self->regs.byte.\1/
+s/cpu_6800_c\([ab]\)\([ab]\)(self/cpu_6800_cmp(self, self->regs.byte.\2, self->regs.byte.\1/
 s/cpu_6800_s\([ab]\)\([ab]\)(self/cpu_6800_sub(self, CPU_6800_EA_\2, self->regs.byte.\1/
 s/cpu_6800_t\([ab]\)\([ab]\)(self/cpu_6800_lda(self, CPU_6800_EA_\2, self->regs.byte.\1/
 s/cpu_6800_cl\([cidv]\)(self/cpu_6800_cl(self, CPU_6800_REG_P_BIT_\1/
 s/cpu_6800_se\([cidv]\)(self/cpu_6800_se(self, CPU_6800_REG_P_BIT_\1/
-s/cpu_6800_in\([xs]\)(self/cpu_6800_in(self, CPU_6800_EA_\1/
-s/cpu_6800_de\([xs]\)(self/cpu_6800_de(self, CPU_6800_EA_\1/
 s/cpu_6800_cp\([xs]\)(self\(.*\)byte/cpu_6800_cp(self, self->regs.word.\1\2word/
 s/cpu_6800_cp\([xs]\)(self/cpu_6800_cp(self, self->regs.word.\1/
 s/cpu_6800_swi(self/cpu_6800_swi(self, CPU_6800_SWI_VECTOR/
diff --git a/sim68xx b/sim68xx
index 706f5f8..8b65ae6 160000 (submodule)
--- a/sim68xx
+++ b/sim68xx
@@ -1 +1 @@
-Subproject commit 706f5f8076336bc722a0ea3e51ed489587f18d68
+Subproject commit 8b65ae6a4c8169cc40844026d472d97bdcde3e2f