Refactor 6800 decoder to be more like Z80 decoder
authorNick Downing <nick@ndcode.org>
Sat, 30 Jul 2022 08:44:43 +0000 (18:44 +1000)
committerNick Downing <nick@ndcode.org>
Sat, 30 Jul 2022 08:46:19 +0000 (18:46 +1000)
15 files changed:
.gitignore
cpu_6800.c
cpu_6800.h
decode_65c02.py
decode_65c02_post.sed
decode_65c02_pre.sed
decode_6800.py
decode_6800.sed [deleted file]
decode_6800.sh [new file with mode: 0755]
decode_6800_post.sed [new file with mode: 0644]
decode_6800_pre.sed [new file with mode: 0644]
decode_z80.py
decode_z80_pre.sed
instr_6800.txt
sim68xx

index e224f36..650b28f 100644 (file)
@@ -1,6 +1,7 @@
 *.ihx
 *.o
 /decode_65c02.txt
+/decode_6800.txt
 /decode_z80.txt
 /emu_65c02
 /emu_65c02_alt
index a7c69b6..228a045 100644 (file)
@@ -54,22 +54,22 @@ void cpu_6800_execute(struct cpu_6800 *self) {
 
   switch (cpu_6800_fetch_byte(self)) {
   case 0x00:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x01:
     cpu_6800_nop(self);
     break;
   case 0x02:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x03:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x04:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x05:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x06:
     cpu_6800_tap(self);
@@ -78,10 +78,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_tpa(self);
     break;
   case 0x08:
-    cpu_6800_inx(self);
+    cpu_6800_inc_word_zf(self, CPU_6800_EA_X);
     break;
   case 0x09:
-    cpu_6800_dex(self);
+    cpu_6800_dec_word_zf(self, CPU_6800_EA_X);
     break;
   case 0x0a:
     cpu_6800_cl(self, CPU_6800_REG_P_BIT_V);
@@ -105,55 +105,55 @@ 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, self->regs.byte.a, self->regs.byte.b);
+    cpu_6800_cmp_byte(self, self->regs.byte.a, self->regs.byte.b);
     break;
   case 0x12:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x13:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x14:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x15:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x16:
-    cpu_6800_lda(self, CPU_6800_EA_B, self->regs.byte.a);
+    cpu_6800_ld_byte(self, CPU_6800_EA_B, self->regs.byte.a);
     break;
   case 0x17:
-    cpu_6800_lda(self, CPU_6800_EA_A, self->regs.byte.b);
+    cpu_6800_ld_byte(self, CPU_6800_EA_A, self->regs.byte.b);
     break;
   case 0x18:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x19:
     cpu_6800_daa(self);
     break;
   case 0x1a:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x1b:
     cpu_6800_add(self, CPU_6800_EA_A, self->regs.byte.b);
     break;
   case 0x1c:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x1d:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x1e:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x1f:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x20:
     cpu_6800_bra(self, true, cpu_6800_ea_relative(self));
     break;
   case 0x21:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x22:
     cpu_6800_bra(self, !self->regs.bit.cf && !self->regs.bit.zf, cpu_6800_ea_relative(self));
@@ -201,7 +201,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_tsx(self);
     break;
   case 0x31:
-    cpu_6800_ins(self);
+    cpu_6800_inc_word(self, CPU_6800_EA_S);
     break;
   case 0x32:
     cpu_6800_pul(self, CPU_6800_EA_A);
@@ -210,7 +210,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_pul(self, CPU_6800_EA_B);
     break;
   case 0x34:
-    cpu_6800_des(self);
+    cpu_6800_dec_word(self, CPU_6800_EA_S);
     break;
   case 0x35:
     cpu_6800_txs(self);
@@ -222,22 +222,22 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_psh(self, self->regs.byte.b);
     break;
   case 0x38:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x39:
     cpu_6800_rts(self);
     break;
   case 0x3a:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x3b:
     cpu_6800_rti(self);
     break;
   case 0x3c:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x3d:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x3e:
     cpu_6800_wai(self);
@@ -249,10 +249,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_neg(self, CPU_6800_EA_A);
     break;
   case 0x41:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x42:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x43:
     cpu_6800_com(self, CPU_6800_EA_A);
@@ -261,7 +261,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_lsr(self, CPU_6800_EA_A);
     break;
   case 0x45:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x46:
     cpu_6800_ror(self, CPU_6800_EA_A);
@@ -276,19 +276,19 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_rol(self, CPU_6800_EA_A);
     break;
   case 0x4a:
-    cpu_6800_dec(self, CPU_6800_EA_A);
+    cpu_6800_dec_byte(self, CPU_6800_EA_A);
     break;
   case 0x4b:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x4c:
-    cpu_6800_inc(self, CPU_6800_EA_A);
+    cpu_6800_inc_byte(self, CPU_6800_EA_A);
     break;
   case 0x4d:
-    cpu_6800_cmp(self, self->regs.byte.a, 0);
+    cpu_6800_cmp_byte(self, self->regs.byte.a, 0);
     break;
   case 0x4e:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x4f:
     cpu_6800_clr(self, CPU_6800_EA_A);
@@ -297,10 +297,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_neg(self, CPU_6800_EA_B);
     break;
   case 0x51:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x52:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x53:
     cpu_6800_com(self, CPU_6800_EA_B);
@@ -309,7 +309,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_lsr(self, CPU_6800_EA_B);
     break;
   case 0x55:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x56:
     cpu_6800_ror(self, CPU_6800_EA_B);
@@ -324,19 +324,19 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_rol(self, CPU_6800_EA_B);
     break;
   case 0x5a:
-    cpu_6800_dec(self, CPU_6800_EA_B);
+    cpu_6800_dec_byte(self, CPU_6800_EA_B);
     break;
   case 0x5b:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x5c:
-    cpu_6800_inc(self, CPU_6800_EA_B);
+    cpu_6800_inc_byte(self, CPU_6800_EA_B);
     break;
   case 0x5d:
-    cpu_6800_cmp(self, self->regs.byte.b, 0);
+    cpu_6800_cmp_byte(self, self->regs.byte.b, 0);
     break;
   case 0x5e:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x5f:
     cpu_6800_clr(self, CPU_6800_EA_B);
@@ -345,10 +345,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_neg(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0x61:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x62:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x63:
     cpu_6800_com(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
@@ -357,7 +357,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_lsr(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0x65:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x66:
     cpu_6800_ror(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
@@ -372,16 +372,16 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_rol(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0x6a:
-    cpu_6800_dec(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
+    cpu_6800_dec_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0x6b:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x6c:
-    cpu_6800_inc(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
+    cpu_6800_inc_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0x6d:
-    cpu_6800_cmp(self, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)), 0);
+    cpu_6800_cmp_byte(self, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)), 0);
     break;
   case 0x6e:
     cpu_6800_jmp(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
@@ -393,10 +393,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_neg(self, cpu_6800_ea_extended(self));
     break;
   case 0x71:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x72:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x73:
     cpu_6800_com(self, cpu_6800_ea_extended(self));
@@ -405,7 +405,7 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_lsr(self, cpu_6800_ea_extended(self));
     break;
   case 0x75:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x76:
     cpu_6800_ror(self, cpu_6800_ea_extended(self));
@@ -420,16 +420,16 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_rol(self, cpu_6800_ea_extended(self));
     break;
   case 0x7a:
-    cpu_6800_dec(self, cpu_6800_ea_extended(self));
+    cpu_6800_dec_byte(self, cpu_6800_ea_extended(self));
     break;
   case 0x7b:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x7c:
-    cpu_6800_inc(self, cpu_6800_ea_extended(self));
+    cpu_6800_inc_byte(self, cpu_6800_ea_extended(self));
     break;
   case 0x7d:
-    cpu_6800_cmp(self, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)), 0);
+    cpu_6800_cmp_byte(self, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)), 0);
     break;
   case 0x7e:
     cpu_6800_jmp(self, cpu_6800_ea_extended(self));
@@ -441,13 +441,13 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_sub(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
     break;
   case 0x81:
-    cpu_6800_cmp(self, self->regs.byte.a, cpu_6800_fetch_byte(self));
+    cpu_6800_cmp_byte(self, self->regs.byte.a, cpu_6800_fetch_byte(self));
     break;
   case 0x82:
     cpu_6800_sbc(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
     break;
   case 0x83:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x84:
     cpu_6800_and(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
@@ -456,10 +456,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
     break;
   case 0x86:
-    cpu_6800_lda(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
+    cpu_6800_ld_byte(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
     break;
   case 0x87:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x88:
     cpu_6800_eor(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
@@ -474,28 +474,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_A, cpu_6800_fetch_byte(self));
     break;
   case 0x8c:
-    cpu_6800_cp(self, self->regs.word.x, cpu_6800_fetch_word(self));
+    cpu_6800_cmp_word(self, self->regs.word.x, cpu_6800_fetch_word(self));
     break;
   case 0x8d:
     cpu_6800_bsr(self, cpu_6800_ea_relative(self));
     break;
   case 0x8e:
-    cpu_6800_ld(self, CPU_6800_EA_S, cpu_6800_fetch_word(self));
+    cpu_6800_ld_word(self, CPU_6800_EA_S, cpu_6800_fetch_word(self));
     break;
   case 0x8f:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x90:
     cpu_6800_sub(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0x91:
-    cpu_6800_cmp(self, self->regs.byte.a, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
+    cpu_6800_cmp_byte(self, self->regs.byte.a, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0x92:
     cpu_6800_sbc(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0x93:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x94:
     cpu_6800_and(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
@@ -504,10 +504,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0x96:
-    cpu_6800_lda(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
+    cpu_6800_ld_byte(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0x97:
-    cpu_6800_sta(self, self->regs.byte.a, cpu_6800_ea_direct(self));
+    cpu_6800_st_byte(self, self->regs.byte.a, cpu_6800_ea_direct(self));
     break;
   case 0x98:
     cpu_6800_eor(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
@@ -522,28 +522,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0x9c:
-    cpu_6800_cp(self, self->regs.word.x, cpu_6800_read_word(self, cpu_6800_ea_direct(self)));
+    cpu_6800_cmp_word(self, self->regs.word.x, cpu_6800_read_word(self, cpu_6800_ea_direct(self)));
     break;
   case 0x9d:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0x9e:
-    cpu_6800_ld(self, CPU_6800_EA_S, cpu_6800_read_word(self, cpu_6800_ea_direct(self)));
+    cpu_6800_ld_word(self, CPU_6800_EA_S, cpu_6800_read_word(self, cpu_6800_ea_direct(self)));
     break;
   case 0x9f:
-    cpu_6800_st(self, self->regs.word.s, cpu_6800_ea_direct(self));
+    cpu_6800_st_word(self, self->regs.word.s, cpu_6800_ea_direct(self));
     break;
   case 0xa0:
     cpu_6800_sub(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xa1:
-    cpu_6800_cmp(self, self->regs.byte.a, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_cmp_byte(self, self->regs.byte.a, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xa2:
     cpu_6800_sbc(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xa3:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xa4:
     cpu_6800_and(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
@@ -552,10 +552,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xa6:
-    cpu_6800_lda(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_ld_byte(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xa7:
-    cpu_6800_sta(self, self->regs.byte.a, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
+    cpu_6800_st_byte(self, self->regs.byte.a, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0xa8:
     cpu_6800_eor(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
@@ -570,28 +570,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xac:
-    cpu_6800_cp(self, self->regs.word.x, cpu_6800_read_word(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_cmp_word(self, self->regs.word.x, cpu_6800_read_word(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xad:
     cpu_6800_jsr(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0xae:
-    cpu_6800_ld(self, CPU_6800_EA_S, cpu_6800_read_word(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_ld_word(self, CPU_6800_EA_S, cpu_6800_read_word(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xaf:
-    cpu_6800_st(self, self->regs.word.s, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
+    cpu_6800_st_word(self, self->regs.word.s, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0xb0:
     cpu_6800_sub(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xb1:
-    cpu_6800_cmp(self, self->regs.byte.a, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
+    cpu_6800_cmp_byte(self, self->regs.byte.a, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xb2:
     cpu_6800_sbc(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xb3:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xb4:
     cpu_6800_and(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
@@ -600,10 +600,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xb6:
-    cpu_6800_lda(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
+    cpu_6800_ld_byte(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xb7:
-    cpu_6800_sta(self, self->regs.byte.a, cpu_6800_ea_extended(self));
+    cpu_6800_st_byte(self, self->regs.byte.a, cpu_6800_ea_extended(self));
     break;
   case 0xb8:
     cpu_6800_eor(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
@@ -618,28 +618,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_A, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xbc:
-    cpu_6800_cp(self, self->regs.word.x, cpu_6800_read_word(self, cpu_6800_ea_extended(self)));
+    cpu_6800_cmp_word(self, self->regs.word.x, cpu_6800_read_word(self, cpu_6800_ea_extended(self)));
     break;
   case 0xbd:
     cpu_6800_jsr(self, cpu_6800_ea_extended(self));
     break;
   case 0xbe:
-    cpu_6800_ld(self, CPU_6800_EA_S, cpu_6800_read_word(self, cpu_6800_ea_extended(self)));
+    cpu_6800_ld_word(self, CPU_6800_EA_S, cpu_6800_read_word(self, cpu_6800_ea_extended(self)));
     break;
   case 0xbf:
-    cpu_6800_st(self, self->regs.word.s, cpu_6800_ea_extended(self));
+    cpu_6800_st_word(self, self->regs.word.s, cpu_6800_ea_extended(self));
     break;
   case 0xc0:
     cpu_6800_sub(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
     break;
   case 0xc1:
-    cpu_6800_cmp(self, self->regs.byte.b, cpu_6800_fetch_byte(self));
+    cpu_6800_cmp_byte(self, self->regs.byte.b, cpu_6800_fetch_byte(self));
     break;
   case 0xc2:
     cpu_6800_sbc(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
     break;
   case 0xc3:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xc4:
     cpu_6800_and(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
@@ -648,10 +648,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
     break;
   case 0xc6:
-    cpu_6800_lda(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
+    cpu_6800_ld_byte(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
     break;
   case 0xc7:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xc8:
     cpu_6800_eor(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
@@ -666,28 +666,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_B, cpu_6800_fetch_byte(self));
     break;
   case 0xcc:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xcd:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xce:
-    cpu_6800_ld(self, CPU_6800_EA_X, cpu_6800_fetch_word(self));
+    cpu_6800_ld_word(self, CPU_6800_EA_X, cpu_6800_fetch_word(self));
     break;
   case 0xcf:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xd0:
     cpu_6800_sub(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0xd1:
-    cpu_6800_cmp(self, self->regs.byte.b, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
+    cpu_6800_cmp_byte(self, self->regs.byte.b, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0xd2:
     cpu_6800_sbc(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0xd3:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xd4:
     cpu_6800_and(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
@@ -696,10 +696,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0xd6:
-    cpu_6800_lda(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
+    cpu_6800_ld_byte(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0xd7:
-    cpu_6800_sta(self, self->regs.byte.b, cpu_6800_ea_direct(self));
+    cpu_6800_st_byte(self, self->regs.byte.b, cpu_6800_ea_direct(self));
     break;
   case 0xd8:
     cpu_6800_eor(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
@@ -714,28 +714,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct(self)));
     break;
   case 0xdc:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xdd:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xde:
-    cpu_6800_ld(self, CPU_6800_EA_X, cpu_6800_read_word(self, cpu_6800_ea_direct(self)));
+    cpu_6800_ld_word(self, CPU_6800_EA_X, cpu_6800_read_word(self, cpu_6800_ea_direct(self)));
     break;
   case 0xdf:
-    cpu_6800_st(self, self->regs.word.x, cpu_6800_ea_direct(self));
+    cpu_6800_st_word(self, self->regs.word.x, cpu_6800_ea_direct(self));
     break;
   case 0xe0:
     cpu_6800_sub(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xe1:
-    cpu_6800_cmp(self, self->regs.byte.b, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_cmp_byte(self, self->regs.byte.b, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xe2:
     cpu_6800_sbc(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xe3:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xe4:
     cpu_6800_and(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
@@ -744,10 +744,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xe6:
-    cpu_6800_lda(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_ld_byte(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xe7:
-    cpu_6800_sta(self, self->regs.byte.b, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
+    cpu_6800_st_byte(self, self->regs.byte.b, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0xe8:
     cpu_6800_eor(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
@@ -762,28 +762,28 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xec:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xed:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xee:
-    cpu_6800_ld(self, CPU_6800_EA_X, cpu_6800_read_word(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
+    cpu_6800_ld_word(self, CPU_6800_EA_X, cpu_6800_read_word(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x)));
     break;
   case 0xef:
-    cpu_6800_st(self, self->regs.word.x, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
+    cpu_6800_st_word(self, self->regs.word.x, cpu_6800_ea_direct_indexed(self, self->regs.word.x));
     break;
   case 0xf0:
     cpu_6800_sub(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xf1:
-    cpu_6800_cmp(self, self->regs.byte.b, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
+    cpu_6800_cmp_byte(self, self->regs.byte.b, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xf2:
     cpu_6800_sbc(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xf3:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xf4:
     cpu_6800_and(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
@@ -792,10 +792,10 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_bit(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xf6:
-    cpu_6800_lda(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
+    cpu_6800_ld_byte(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xf7:
-    cpu_6800_sta(self, self->regs.byte.b, cpu_6800_ea_extended(self));
+    cpu_6800_st_byte(self, self->regs.byte.b, cpu_6800_ea_extended(self));
     break;
   case 0xf8:
     cpu_6800_eor(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
@@ -810,16 +810,16 @@ void cpu_6800_execute(struct cpu_6800 *self) {
     cpu_6800_add(self, CPU_6800_EA_B, cpu_6800_read_byte(self, cpu_6800_ea_extended(self)));
     break;
   case 0xfc:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xfd:
-    cpu_6800_ill(self);
+    cpu_6800_illegal_opcode(self);
     break;
   case 0xfe:
-    cpu_6800_ld(self, CPU_6800_EA_X, cpu_6800_read_word(self, cpu_6800_ea_extended(self)));
+    cpu_6800_ld_word(self, CPU_6800_EA_X, cpu_6800_read_word(self, cpu_6800_ea_extended(self)));
     break;
   case 0xff:
-    cpu_6800_st(self, self->regs.word.x, cpu_6800_ea_extended(self));
+    cpu_6800_st_word(self, self->regs.word.x, cpu_6800_ea_extended(self));
     break;
   }
 }
index cc68fdb..fc3296a 100644 (file)
@@ -311,7 +311,7 @@ static ALWAYS_INLINE void cpu_6800_clr(struct cpu_6800 *self, int lvalue) {
   self->regs.bit.cf = false;
 }
 
-static ALWAYS_INLINE void cpu_6800_cmp(struct cpu_6800 *self, int rvalue0, int rvalue1) {
+static ALWAYS_INLINE void cpu_6800_cmp_byte(struct cpu_6800 *self, int rvalue0, int rvalue1) {
   int result0 = (rvalue0 & 0x7f) - (rvalue1 & 0x7f);
   int result1 = result0 + (rvalue0 & 0x80) - (rvalue1 & 0x80);
 
@@ -331,7 +331,7 @@ static ALWAYS_INLINE void cpu_6800_com(struct cpu_6800 *self, int lvalue) {
   self->regs.bit.cf = true;
 }
 
-static ALWAYS_INLINE void cpu_6800_cp(struct cpu_6800 *self, int rvalue0, int rvalue1) {
+static ALWAYS_INLINE void cpu_6800_cmp_word(struct cpu_6800 *self, int rvalue0, int rvalue1) {
   int result0 = (rvalue0 & 0x7fff) - (rvalue1 & 0x7fff);
   int result1 = result0 + (rvalue0 & 0x8000) - (rvalue1 & 0x8000);
 
@@ -359,7 +359,7 @@ static ALWAYS_INLINE void cpu_6800_daa(struct cpu_6800 *self) {
   //self->regs.bit.cf |= result1 >> 8;
 }
 
-static ALWAYS_INLINE void cpu_6800_dec(struct cpu_6800 *self, int lvalue) {
+static ALWAYS_INLINE void cpu_6800_dec_byte(struct cpu_6800 *self, int lvalue) {
   int data = cpu_6800_read_byte(self, lvalue);
 
   int result0 = (data & 0x7f) - 1;
@@ -372,13 +372,17 @@ static ALWAYS_INLINE void cpu_6800_dec(struct cpu_6800 *self, int lvalue) {
   self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
 }
 
-static ALWAYS_INLINE void cpu_6800_des(struct cpu_6800 *self) {
-  --self->regs.word.s;
+static ALWAYS_INLINE void cpu_6800_dec_word(struct cpu_6800 *self, int lvalue) {
+  cpu_6800_write_word(
+    self,
+    lvalue,
+    (cpu_6800_read_word(self, lvalue) - 1) & 0xffff
+  );
   ++self->cycles;
 }
 
-static ALWAYS_INLINE void cpu_6800_dex(struct cpu_6800 *self) {
-  int result = (self->regs.word.x - 1) & 0xffff;
+static ALWAYS_INLINE void cpu_6800_dec_word_zf(struct cpu_6800 *self, int lvalue) {
+  int result = (cpu_6800_read_word(self, lvalue) - 1) & 0xffff;
   ++self->cycles;
 
   self->regs.word.x = result;
@@ -394,11 +398,11 @@ static ALWAYS_INLINE void cpu_6800_eor(struct cpu_6800 *self, int lvalue, int rv
   self->regs.bit.vf = false;
 }
 
-static ALWAYS_INLINE void cpu_6800_ill(struct cpu_6800 *self) {
+static ALWAYS_INLINE void cpu_6800_illegal_opcode(struct cpu_6800 *self) {
   abort();
 }
 
-static ALWAYS_INLINE void cpu_6800_inc(struct cpu_6800 *self, int lvalue) {
+static ALWAYS_INLINE void cpu_6800_inc_byte(struct cpu_6800 *self, int lvalue) {
   int data = cpu_6800_read_byte(self, lvalue);
 
   int result0 = (data & 0x7f) + 1;
@@ -411,13 +415,17 @@ static ALWAYS_INLINE void cpu_6800_inc(struct cpu_6800 *self, int lvalue) {
   self->regs.bit.vf = ((result0 >> 7) ^ (result1 >> 8)) & 1;
 }
 
-static ALWAYS_INLINE void cpu_6800_ins(struct cpu_6800 *self) {
-  ++self->regs.word.s;
+static ALWAYS_INLINE void cpu_6800_inc_word(struct cpu_6800 *self, int lvalue) {
+  cpu_6800_write_word(
+    self,
+    lvalue,
+    (cpu_6800_read_word(self, lvalue) + 1) & 0xffff
+  );
   ++self->cycles;
 }
 
-static ALWAYS_INLINE void cpu_6800_inx(struct cpu_6800 *self) {
-  int result = (self->regs.word.x + 1) & 0xffff;
+static ALWAYS_INLINE void cpu_6800_inc_word_zf(struct cpu_6800 *self, int lvalue) {
+  int result = (cpu_6800_read_word(self, lvalue) + 1) & 0xffff;
   ++self->cycles;
 
   self->regs.word.x = result;
@@ -443,7 +451,7 @@ static ALWAYS_INLINE void cpu_6800_jsr(struct cpu_6800 *self, int lvalue) {
   self->regs.word.pc = lvalue;
 }
 
-static ALWAYS_INLINE void cpu_6800_ld(struct cpu_6800 *self, int lvalue, int rvalue) {
+static ALWAYS_INLINE void cpu_6800_ld_word(struct cpu_6800 *self, int lvalue, int rvalue) {
   cpu_6800_write_word(self, lvalue, rvalue);
 
   self->regs.bit.nf = (rvalue >> 15) & 1;
@@ -451,7 +459,7 @@ static ALWAYS_INLINE void cpu_6800_ld(struct cpu_6800 *self, int lvalue, int rva
   self->regs.bit.vf = false;
 }
 
-static ALWAYS_INLINE void cpu_6800_lda(struct cpu_6800 *self, int lvalue, int rvalue) {
+static ALWAYS_INLINE void cpu_6800_ld_byte(struct cpu_6800 *self, int lvalue, int rvalue) {
   cpu_6800_write_byte(self, lvalue, rvalue);
 
   self->regs.bit.nf = (rvalue >> 7) & 1;
@@ -556,7 +564,7 @@ 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_st(struct cpu_6800 *self, int rvalue, int lvalue) {
+static ALWAYS_INLINE void cpu_6800_st_word(struct cpu_6800 *self, int rvalue, int lvalue) {
   cpu_6800_write_word(self, lvalue, rvalue);
 
   self->regs.bit.nf = (rvalue >> 15) & 1;
@@ -564,7 +572,7 @@ static ALWAYS_INLINE void cpu_6800_st(struct cpu_6800 *self, int rvalue, int lva
   self->regs.bit.vf = false;
 }
 
-static ALWAYS_INLINE void cpu_6800_sta(struct cpu_6800 *self, int rvalue, int lvalue) {
+static ALWAYS_INLINE void cpu_6800_st_byte(struct cpu_6800 *self, int rvalue, int lvalue) {
   cpu_6800_write_byte(self, lvalue, rvalue);
 
   self->regs.bit.nf = (rvalue >> 7) & 1;
index a542ac8..104c33c 100755 (executable)
@@ -2,25 +2,18 @@
 
 import sys
 
-# for these opcodes, operand 1 is an rvalue
-rvalue1_opcodes = {
-  'cmp',
-  'st'
-}
-
-# for these opcodes, last operand is an rvalue
+# end-relative range of operands that are rvalues
 rvalue_opcodes = {
-  'adc',
-  'and',
-  'bit',
-  'cmp',
-  'cpx',
-  'cpy',
-  'eor',
-  'ld',
-  'ora',
-  'ph',
-  'sbc',
+  'adc': (-1, 0),
+  'and': (-1, 0),
+  'bit': (-1, 0),
+  'cmp': (-2, 0),
+  'eor': (-1, 0),
+  'ld': (-1, 0),
+  'ora': (-1, 0),
+  'ph': (-1, 0),
+  'sbc': (-1, 0),
+  'st': (-2, -1),
 }
 
 rvalue_modes = {
@@ -74,32 +67,23 @@ for i in range(0x100):
   print(f'  case 0x{i:02x}:')
   instr = line.split()
 
-  j0 = 1 + int(instr[0] in rvalue1_opcodes)
-  j1 = len(instr) - int(instr[0] in rvalue_opcodes)
+  # work out which operands are rvalue
+  j0, j1 = rvalue_opcodes.get(instr[0], (0, 0))
+  j0 += len(instr)
+  j1 += len(instr)
 
-  # operands [1, j0) are rvalue
-  for k in range(1, j0):
-    if instr[k] in rvalue_modes:
-      instr[k] = rvalue_modes[instr[k]]
+  # translate operands
+  for k in range(1, len(instr)):
+    if k >= j0 and k < j1:
+      if instr[k] in rvalue_modes:
+        instr[k] = rvalue_modes[instr[k]]
+      elif instr[k] in lvalue_modes:
+        instr[k] = 'cpu_65c02_read_byte(self, {0:s})'.format(
+          lvalue_modes[instr[k]]
+        )
     elif instr[k] in lvalue_modes:
-      instr[k] = 'cpu_65c02_read_byte(self, {0:s})'.format(
-        lvalue_modes[instr[k]]
-      )
-
-  # operands [j0, j1) are lvalue
-  for k in range(j0, j1):
-    if instr[k] in lvalue_modes:
       instr[k] = lvalue_modes[instr[k]]
 
-  # operands [j1, n) are rvalue
-  for k in range(j1, len(instr)):
-    if instr[k] in rvalue_modes:
-      instr[k] = rvalue_modes[instr[k]]
-    elif instr[k] in lvalue_modes:
-      instr[k] = 'cpu_65c02_read_byte(self, {0:s})'.format(
-        lvalue_modes[instr[k]]
-      )
-
   print(
     '    cpu_65c02_{0:s}(self{1:s});'.format(
       instr[0],
index 50a676a..a10c60b 100644 (file)
@@ -1 +1,2 @@
+s/cpu_65c02_bit(self, cpu_65c02_fetch_byte(self))/cpu_65c02_bit_imm(self, cpu_65c02_fetch_byte(self))/
 s/^    cpu_65c02_brk(self);$/    ++self->regs.word.pc;\n    cpu_65c02_irq(self, true, CPU_65C02_IRQ_VECTOR);/
index 3d54ead..a5bf771 100644 (file)
@@ -1,18 +1,18 @@
 y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
-s/???/illegal_opcode/
-s/\(rmb\)\([0-7]\)/\1 \2/
-s/\([rs]mb\)\([0-7]\)/\1 \2/
-s/\(ph\)\([axyp]\)/\1 \2/
-s/\(pl\)\([axy]\)/\1 \2/
-s/\(cl\|se\)\([cdiv]\)/\1 \2/
-s/bra/bra true/
-s/b\(ne\|eq\|[cv][cs]\|pl\|mi\)/bra \1/
-s/de\([xy]\)/dec \1/
-s/in\([xy]\)/inc \1/
-s/cmp/cmp a/
-s/cp\([xy]\)/cmp \1/
-s/\(ld\)\([axy]\)/\1 \2/
-s/\(st\)\([axy]\)/\1 \2/
-s/stz/st 0/
-s/txs/st x s/
-s/t\([axys]\)\([axys]\)/ld \2 \1/
+s/^???/illegal_opcode/
+s/^\(rmb\)\([0-7]\)/\1 \2/
+s/^\([rs]mb\)\([0-7]\)/\1 \2/
+s/^\(ph\)\([axyp]\)/\1 \2/
+s/^\(pl\)\([axy]\)/\1 \2/
+s/^\(cl\|se\)\([cdiv]\)/\1 \2/
+s/^bra/bra true/
+s/^b\(ne\|eq\|[cv][cs]\|pl\|mi\)/bra \1/
+s/^de\([xy]\)/dec \1/
+s/^in\([xy]\)/inc \1/
+s/^cmp/cmp a/
+s/^cp\([xy]\)/cmp \1/
+s/^\(ld\)\([axy]\)/\1 \2/
+s/^\(st\)\([axy]\)/\1 \2/
+s/^stz/st 0/
+s/^txs/st x s/
+s/^t\([axys]\)\([axys]\)/ld \2 \1/
index 20143ab..0de9923 100755 (executable)
 
 import sys
 
-# if first argument is embedded in opcode, it is rvalue for these
-acc_rvalue_opcodes = {
-  'cmp',
-  'psh',
-  'sta',
-  'tst',
+# end-relative range of operands that are rvalues
+rvalue_opcodes = {
+  'add': (-1, 0),
+  'adc': (-1, 0),
+  'and': (-1, 0),
+  'bit': (-1, 0),
+  'cmp': (-2, 0),
+  'cmp_byte': (-2, 0),
+  'eor': (-1, 0),
+  'ld': (-1, 0),
+  'ora': (-1, 0),
+  'psh': (-1, 0),
+  'sub': (-1, 0),
+  'sbc': (-1, 0),
+  'st': (-2, -1),
 }
 
-rvalue_opcodes = {
+# if it is in byte_opcodes it is treated as byte and has no suffix
+byte_opcodes = {
   'add',
   'adc',
   'and',
+  'asl',
+  'asr',
   'bit',
-  'cmp',
-  'cpx',
+  'cl',
+  'clr',
+  'cmp_byte',
+  'com',
+  'dec_byte',
   'eor',
-  'lda',
-  'ldx',
-  'lds',
+  'inc_byte',
+  'lsr',
+  'neg',
   'ora',
+  'pul',
+  'psh',
+  'rol',
+  'ror',
   'sub',
   'sbc',
-  'tst',
+  'se',
 }
-rvalue_modes = {
+# if any operand is in byte_operands it is treated as byte and has suffix
+# in this case it is mandatory that no operand also be in word_operands
+byte_operands = {
+  '#12',
+  'a',
+  'b',
+}
+byte_rvalue_modes = {
   '#12': 'cpu_6800_fetch_byte(self)',
-  '#1234': 'cpu_6800_fetch_word(self)',
-  '12': 'cpu_6800_read_byte(self, cpu_6800_ea_direct(self))',
-  '12,x': 'cpu_6800_read_byte(self, cpu_6800_ea_direct_indexed(self, self->regs.word.x))',
-  '1234': 'cpu_6800_read_byte(self, cpu_6800_ea_extended(self))',
-  '1234,x': 'cpu_6800_read_byte(self, cpu_6800_ea_extended_indexed(self, self->regs.word.x))',
   'a': 'self->regs.byte.a',
   'b': 'self->regs.byte.b',
 }
-lvalue_modes = {
-  '0014': 'cpu_6800_ea_relative(self)',
+byte_lvalue_modes = {
   '12': 'cpu_6800_ea_direct(self)',
   '12,x': 'cpu_6800_ea_direct_indexed(self, self->regs.word.x)',
   '1234': 'cpu_6800_ea_extended(self)',
   '1234,x': 'cpu_6800_ea_extended_indexed(self, self->regs.word.x)',
   'a': 'CPU_6800_EA_A',
   'b': 'CPU_6800_EA_B',
+  'c': 'CPU_6800_REG_P_BIT_C',
+  'i': 'CPU_6800_REG_P_BIT_I',
+  'v': 'CPU_6800_REG_P_BIT_V',
 }
 
+# if it is in word_opcodes it is treated as word and has no suffix
+# in this case the operands "c" and "(hl)" do not imply a byte opcode
+word_opcodes = {
+  'bra',
+  'bsr',
+  'dec_word_zf',
+  'inc_word_zf',
+  'jmp',
+  'jsr',
+}
+# if any operand is in word_operands it is treated as word and has suffix
+# in this case it is mandatory that no operand also be in byte_operands
+word_operands = {
+  '#1234',
+  's',
+  'x',
+}
+word_rvalue_modes = {
+  '#1234': 'cpu_6800_fetch_word(self)',
+  's': 'self->regs.word.s',
+  'x': 'self->regs.word.x',
+}
+word_lvalue_modes = {
+  'ne': '!self->regs.bit.zf',
+  'eq': 'self->regs.bit.zf',
+  'cc': '!self->regs.bit.cf',
+  'cs': 'self->regs.bit.cf',
+  'vc': '!self->regs.bit.vf',
+  'vs': 'self->regs.bit.vf',
+  'pl': '!self->regs.bit.nf',
+  'mi': 'self->regs.bit.nf',
+  'ge': '!self->regs.bit.nf && !self->regs.bit.vf',
+  'gt': '!self->regs.bit.nf && !self->regs.bit.vf && !self->regs.bit.zf',
+  'hi': '!self->regs.bit.cf && !self->regs.bit.zf',
+  'le': 'self->regs.bit.nf || self->regs.bit.vf || self->regs.bit.zf',
+  'ls': 'self->regs.bit.cf || self->regs.bit.zf',
+  'lt': 'self->regs.bit.nf || self->regs.bit.vf',
+  '0014': 'cpu_6800_ea_relative(self)',
+  '12': 'cpu_6800_ea_direct(self)',
+  '12,x': 'cpu_6800_ea_direct_indexed(self, self->regs.word.x)',
+  '1234': 'cpu_6800_ea_extended(self)',
+  '1234,x': 'cpu_6800_ea_extended_indexed(self, self->regs.word.x)',
+  's': 'CPU_6800_EA_S',
+  'x': 'CPU_6800_EA_X',
+}
+
+line = sys.stdin.readline().strip()
+assert line == 'opcodes'
+
 print('void cpu_6800_execute(struct cpu_6800 *self) {')
 print('  switch (cpu_6800_fetch_byte(self)) {')
+
 for i in range(0x100):
+  line = sys.stdin.readline().strip()
+
   print(f'  case 0x{i:02x}:')
-  instr = sys.stdin.readline().strip().split('\t')[-1].split()
-  if instr[0] == '---':
-    instr[0] = 'ill' # illegal opcode
-  #print('xxx', instr)
-  if len(instr) >= 2:
-    if instr[0][:3] in rvalue_opcodes:
-      instr[1] = rvalue_modes[instr[1]]
-    else:
-      instr[1] = lvalue_modes[instr[1]]
-  if len(instr[0]) >= 4:
-    # move accumulator a/b from opcode to first argument
-    if instr[0][:3] in acc_rvalue_opcodes:
-      instr[:1] = [instr[0][:3], rvalue_modes[instr[0][3:]]]
-    else:
-      instr[:1] = [instr[0][:3], lvalue_modes[instr[0][3:]]]
+  instr = line.split()
+
+  # detect operation size (byte or word)
+  suffix = ''
+  if instr[0] not in byte_opcodes and instr[0] not in word_opcodes:
+    for j in instr[1:]:
+      if j in byte_operands:
+        assert suffix != '_word'
+        suffix = '_byte'
+      elif j in word_operands:
+        assert suffix != '_byte'
+        suffix = '_word'
+
+  # work out which operands are rvalue
+  j0, j1 = rvalue_opcodes.get(instr[0], (0, 0))
+  j0 += len(instr)
+  j1 += len(instr)
+
+  # translate operands
+  if suffix == '_byte' or instr[0] in byte_opcodes:
+    for k in range(1, len(instr)):
+      if k >= j0 and k < j1:
+        if instr[k] in byte_rvalue_modes:
+          instr[k] = byte_rvalue_modes[instr[k]]
+        elif instr[k] in byte_lvalue_modes:
+          instr[k] = 'cpu_6800_read_byte(self, {0:s})'.format(
+            byte_lvalue_modes[instr[k]]
+          )
+      elif instr[k] in byte_lvalue_modes:
+        instr[k] = byte_lvalue_modes[instr[k]]
+  elif suffix == '_word' or instr[0] in word_opcodes:
+    for k in range(1, len(instr)):
+      if k >= j0 and k < j1:
+        if instr[k] in word_rvalue_modes:
+          instr[k] = word_rvalue_modes[instr[k]]
+        elif instr[k] in word_lvalue_modes:
+          instr[k] = 'cpu_6800_read_word(self, {0:s})'.format(
+            word_lvalue_modes[instr[k]]
+          )
+      elif instr[k] in word_lvalue_modes:
+        instr[k] = word_lvalue_modes[instr[k]]
+
   print(
-    '    cpu_6800_{0:s}(self{1:s});'.format(
+    '    cpu_6800_{0:s}{1:s}(self{2:s});'.format(
       instr[0],
-      ''.join([', ' + j for j in instr[1:]])
+      suffix,
+      ''.join([', ' + k for k in instr[1:]])
     )
   )
   print('    break;')
+
+line = sys.stdin.readline().strip()
+assert len(line) == 0
+
 print('  }')
 print('}')
+print()
diff --git a/decode_6800.sed b/decode_6800.sed
deleted file mode 100644 (file)
index 7c0ab02..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-s/cpu_6800_bpl(self/cpu_6800_bnc(self/
-s/cpu_6800_bmi(self/cpu_6800_bns(self/
-s/cpu_6800_bne(self/cpu_6800_bzc(self/
-s/cpu_6800_beq(self/cpu_6800_bzs(self/
-s/cpu_6800_bra(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, true, cpu_6800_ea_relative(self)/
-s/cpu_6800_bsr(self, cpu_6800_ea_direct(self)/cpu_6800_bsr(self, cpu_6800_ea_relative(self)/
-s/cpu_6800_b\([czvn]\)c(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, !self->regs.bit.\1f, cpu_6800_ea_relative(self)/
-s/cpu_6800_b\([czvn]\)s(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, self->regs.bit.\1f, cpu_6800_ea_relative(self)/
-s/cpu_6800_bge(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, !self->regs.bit.nf \&\& !self->regs.bit.vf, cpu_6800_ea_relative(self)/
-s/cpu_6800_bgt(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, !self->regs.bit.nf \&\& !self->regs.bit.vf \&\& !self->regs.bit.zf, cpu_6800_ea_relative(self)/
-s/cpu_6800_bhi(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, !self->regs.bit.cf \&\& !self->regs.bit.zf, cpu_6800_ea_relative(self)/
-s/cpu_6800_ble(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, self->regs.bit.nf || self->regs.bit.vf || self->regs.bit.zf, cpu_6800_ea_relative(self)/
-s/cpu_6800_bls(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, self->regs.bit.cf || self->regs.bit.zf, cpu_6800_ea_relative(self)/
-s/cpu_6800_blt(self, cpu_6800_ea_direct(self)/cpu_6800_bra(self, self->regs.bit.nf || self->regs.bit.vf, cpu_6800_ea_relative(self)/
-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_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, 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_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_lsl(self/cpu_6800_asl(self/
-s/cpu_6800_tst(self\(.*\));/cpu_6800_cmp(self\1, 0);/
-s/CPU_6800_EA_a/CPU_6800_EA_A/g
-s/CPU_6800_EA_b/CPU_6800_EA_B/g
-s/CPU_6800_EA_x/CPU_6800_EA_X/g
-s/CPU_6800_EA_s/CPU_6800_EA_S/g
-s/CPU_6800_REG_P_BIT_c/CPU_6800_REG_P_BIT_C/g
-s/CPU_6800_REG_P_BIT_i/CPU_6800_REG_P_BIT_I/g
-s/CPU_6800_REG_P_BIT_d/CPU_6800_REG_P_BIT_D/g
-s/CPU_6800_REG_P_BIT_v/CPU_6800_REG_P_BIT_V/g
diff --git a/decode_6800.sh b/decode_6800.sh
new file mode 100755 (executable)
index 0000000..b253b1b
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+sed -f decode_6800_pre.sed <instr_6800.txt |\
+./decode_6800.py |\
+sed -f decode_6800_post.sed >decode_6800.txt
diff --git a/decode_6800_post.sed b/decode_6800_post.sed
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/decode_6800_pre.sed b/decode_6800_pre.sed
new file mode 100644 (file)
index 0000000..fe5ec5b
--- /dev/null
@@ -0,0 +1,24 @@
+s/.*\t//
+s/ \+/ /
+s/^---/illegal_opcode/
+s/^\(bra\|bhi\|bls\|bcc\|bcs\|bne\|beq\|bvc\|bvs\|bpl\|bmi\|bge\|blt\|bgt\|ble\|bsr\) 12/\1 0014/
+s/^bra/bra true/
+s/^b\(hi\|ls\|cc\|cs\|ne\|eq\|vc\|vs\|pl\|mi\|ge\|lt\|gt\|le\)/bra \1/
+s/^\(...\)\([ab]\)/\1 \2/
+s/^lda/ld/
+s/^sta/st/
+s/^\(cl\|se\)\([civ]\)/\1 \2/
+s/^\(inc\|dec\)/\1_byte/
+s/^des/dec s/
+s/^dex/dec_word_zf x/
+s/^ins/inc s/
+s/^inx/inc_word_zf x/
+s/^cp\([sx]\)/cmp \1/
+s/^\(ld\)\([sx]\)/\1 \2/
+s/^\(st\)\([sx]\)/\1 \2/
+s/^aba/add a b/
+s/^sba/sub a b/
+s/^cba/cmp a b/
+s/^t\([ab]\)\([ab]\)/ld \2 \1/
+s/^tst \(.*\)/cmp_byte \1 0/
+s/^lsl/asl/
index fd7965a..3c8c650 100755 (executable)
@@ -2,26 +2,26 @@
 
 import sys
 
-# for these opcodes, the last operand is rvalue (any other is lvalue)
+# end-relative range of operands that are rvalues
 rvalue_opcodes = {
-  'ld',
-  'add',
-  'adc',
-  'sub',
-  'sbc',
-  'or',
-  'and',
-  'xor',
-  'cp',
-  'jr',
-  'jp',
-  'call',
-  'djnz',
-  'in',
-  'in_z80',
-  'out',
-  'push',
-  'bit',
+  'ld': -1,
+  'add': -1,
+  'adc': -1,
+  'sub': -1,
+  'sbc': -1,
+  'or': -1,
+  'and': -1,
+  'xor': -1,
+  'cp': -1,
+  'jr': -1,
+  'jp': -1,
+  'call': -1,
+  'djnz': -1,
+  'in': -1,
+  'in_z80': -1,
+  'out': -1,
+  'push': -1,
+  'bit': -1,
 }
 
 # if it is in byte_opcodes it is treated as byte and has no suffix
@@ -82,7 +82,7 @@ byte_rvalue_modes = {
   'iyh': 'self->regs.byte.iyh',
   'i': 'self->regs.byte.i',
   'r': 'self->regs.byte.r',
-  '(0x0012)': 'cpu_z80_in_byte(self, cpu_z80_fetch_byte(self))',
+  '(0x0012)': 'cpu_z80_in_byte(self, cpu_z80_port_word(self))',
   '(c)': 'cpu_z80_in_byte(self, self->regs.word.bc)',
 }
 byte_lvalue_modes = {
@@ -215,9 +215,10 @@ for i in prefixes:
             assert suffix != '_byte'
             suffix = '_word'
 
+      # work out which operands are rvalue
+      k = len(instr) + rvalue_opcodes.get(instr[0], 0)
+
       # translate operands
-      # use lvalue by default, but last operand can be rvalue
-      k = len(instr) - int(instr[0] in rvalue_opcodes)
       if suffix == '_byte' or instr[0] in byte_opcodes:
         # operands [1, k) are lvalue
         for l in range(1, k):
index 98c96be..fb05fdd 100644 (file)
@@ -1,8 +1,8 @@
 y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
-s/\(\(res\|rl\|rlc\|rr\|rrc\|set\|sla\|sll\|sra\|srl\) .*\)/\1,sink/
-s/ld \([abcdehl]\),\(\(res\|rl\|rlc\|rr\|rrc\|set\|sla\|sll\|sra\|srl\) .*\),sink/\2,\1/
-s/rst/call/
-s/\(ret\)$/\1 true/
-s/\(jr\|jp\|call\) \([^,]*\)$/\1 true,\2/
-s/\(in\) \([abcdehl],(c)\)/\1_z80 \2/
-s/\(in\) f,\((c)\)/\1_z80 sink,\2/
+s/^\(\(res\|rl\|rlc\|rr\|rrc\|set\|sla\|sll\|sra\|srl\) .*\)/\1,sink/
+s/^ld \([abcdehl]\),\(\(res\|rl\|rlc\|rr\|rrc\|set\|sla\|sll\|sra\|srl\) .*\)/\2,\1/
+s/^rst/call/
+s/^\(ret\)$/\1 true/
+s/^\(jr\|jp\|call\) \([^,]*\)$/\1 true,\2/
+s/^\(in\) \([abcdehl],(c)\)/\1_z80 \2/
+s/^\(in\) f,\((c)\)/\1_z80 sink,\2/
index d440a9d..6d434ca 100644 (file)
@@ -1,3 +1,4 @@
+opcodes
 0100   00              ---
 0100   01              nop
 0100   02              ---
 0100   fd              ---
 0100   fe 12 34        ldx  1234
 0100   ff 12 34        stx  1234
+
diff --git a/sim68xx b/sim68xx
index c93ed6d..bb5be7a 160000 (submodule)
--- a/sim68xx
+++ b/sim68xx
@@ -1 +1 @@
-Subproject commit c93ed6d6ddea2af58fd9ffb09081162b02378eb5
+Subproject commit bb5be7a37a63e49daa0260dc056fcf7adc143092