Split decode_65c02.sed into decode_65c02_(pre|post).sed, simplify decoder
authorNick Downing <nick@ndcode.org>
Sat, 30 Jul 2022 02:53:48 +0000 (12:53 +1000)
committerNick Downing <nick@ndcode.org>
Sat, 30 Jul 2022 08:46:19 +0000 (18:46 +1000)
.gitignore
cpu_65c02.c
cpu_65c02.h
decode_65c02.py
decode_65c02.sed [deleted file]
decode_65c02.sh [new file with mode: 0755]
decode_65c02_post.sed [new file with mode: 0644]
decode_65c02_pre.sed [new file with mode: 0644]

index e8b5b2c..8d0d587 100644 (file)
@@ -1,5 +1,6 @@
 *.ihx
 *.o
+/decode_65c02.txt
 /emu_65c02
 /emu_65c02_alt
 /emu_6800
index ae741ee..df21d78 100644 (file)
@@ -66,10 +66,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ora(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x)));
     break;
   case 0x02:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0x03:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x04:
     cpu_65c02_tsb(self, cpu_65c02_ea_zero_page(self));
@@ -93,7 +93,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_asl(self, CPU_65C02_EA_A);
     break;
   case 0x0b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x0c:
     cpu_65c02_tsb(self, cpu_65c02_ea_absolute(self));
@@ -120,7 +120,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ora(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0x13:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x14:
     cpu_65c02_trb(self, cpu_65c02_ea_zero_page(self));
@@ -144,7 +144,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_inc(self, CPU_65C02_EA_A);
     break;
   case 0x1b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x1c:
     cpu_65c02_trb(self, cpu_65c02_ea_absolute(self));
@@ -168,10 +168,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_and(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x)));
     break;
   case 0x22:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0x23:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x24:
     cpu_65c02_bit(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page(self)));
@@ -195,7 +195,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_rol(self, CPU_65C02_EA_A);
     break;
   case 0x2b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x2c:
     cpu_65c02_bit(self, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute(self)));
@@ -222,7 +222,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_and(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0x33:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x34:
     cpu_65c02_bit(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x)));
@@ -246,7 +246,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_dec(self, CPU_65C02_EA_A);
     break;
   case 0x3b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x3c:
     cpu_65c02_bit(self, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.x)));
@@ -270,13 +270,13 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_eor(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x)));
     break;
   case 0x42:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0x43:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x44:
-    cpu_65c02_ill23(self);
+    cpu_65c02_illegal_opcode23(self);
     break;
   case 0x45:
     cpu_65c02_eor(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page(self)));
@@ -297,7 +297,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_lsr(self, CPU_65C02_EA_A);
     break;
   case 0x4b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x4c:
     cpu_65c02_jmp(self, cpu_65c02_ea_absolute(self));
@@ -324,10 +324,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_eor(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0x53:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x54:
-    cpu_65c02_ill24(self);
+    cpu_65c02_illegal_opcode24(self);
     break;
   case 0x55:
     cpu_65c02_eor(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x)));
@@ -348,10 +348,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ph(self, self->regs.byte.y);
     break;
   case 0x5b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x5c:
-    cpu_65c02_ill38(self);
+    cpu_65c02_illegal_opcode38(self);
     break;
   case 0x5d:
     cpu_65c02_eor(self, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.x)));
@@ -372,10 +372,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_adc(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x)));
     break;
   case 0x62:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0x63:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x64:
     cpu_65c02_st(self, 0, cpu_65c02_ea_zero_page(self));
@@ -399,7 +399,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ror(self, CPU_65C02_EA_A);
     break;
   case 0x6b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x6c:
     cpu_65c02_jmp(self, cpu_65c02_ea_absolute_indirect(self));
@@ -426,7 +426,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_adc(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0x73:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x74:
     cpu_65c02_st(self, 0, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x));
@@ -450,7 +450,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_pl(self, CPU_65C02_EA_Y);
     break;
   case 0x7b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x7c:
     cpu_65c02_jmp(self, cpu_65c02_ea_absolute_indexed_indirect(self, self->regs.byte.x));
@@ -474,10 +474,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_st(self, self->regs.byte.a, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x));
     break;
   case 0x82:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0x83:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x84:
     cpu_65c02_st(self, self->regs.byte.y, cpu_65c02_ea_zero_page(self));
@@ -501,7 +501,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ld(self, CPU_65C02_EA_A, self->regs.byte.x);
     break;
   case 0x8b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x8c:
     cpu_65c02_st(self, self->regs.byte.y, cpu_65c02_ea_absolute(self));
@@ -528,7 +528,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_st(self, self->regs.byte.a, cpu_65c02_ea_zero_page_indirect(self));
     break;
   case 0x93:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x94:
     cpu_65c02_st(self, self->regs.byte.y, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x));
@@ -552,7 +552,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_st(self, self->regs.byte.x, CPU_65C02_EA_S);
     break;
   case 0x9b:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0x9c:
     cpu_65c02_st(self, 0, cpu_65c02_ea_absolute(self));
@@ -579,7 +579,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ld(self, CPU_65C02_EA_X, cpu_65c02_fetch_byte(self));
     break;
   case 0xa3:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xa4:
     cpu_65c02_ld(self, CPU_65C02_EA_Y, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page(self)));
@@ -603,7 +603,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ld(self, CPU_65C02_EA_X, self->regs.byte.a);
     break;
   case 0xab:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xac:
     cpu_65c02_ld(self, CPU_65C02_EA_Y, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute(self)));
@@ -630,7 +630,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ld(self, CPU_65C02_EA_A, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0xb3:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xb4:
     cpu_65c02_ld(self, CPU_65C02_EA_Y, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x)));
@@ -654,7 +654,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ld(self, CPU_65C02_EA_X, self->regs.byte.s);
     break;
   case 0xbb:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xbc:
     cpu_65c02_ld(self, CPU_65C02_EA_Y, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.x)));
@@ -678,10 +678,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_cmp(self, self->regs.byte.a, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x)));
     break;
   case 0xc2:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0xc3:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xc4:
     cpu_65c02_cmp(self, self->regs.byte.y, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page(self)));
@@ -732,10 +732,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_cmp(self, self->regs.byte.a, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0xd3:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xd4:
-    cpu_65c02_ill24(self);
+    cpu_65c02_illegal_opcode24(self);
     break;
   case 0xd5:
     cpu_65c02_cmp(self, self->regs.byte.a, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x)));
@@ -756,10 +756,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_ph(self, self->regs.byte.x);
     break;
   case 0xdb:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xdc:
-    cpu_65c02_ill34(self);
+    cpu_65c02_illegal_opcode34(self);
     break;
   case 0xdd:
     cpu_65c02_cmp(self, self->regs.byte.a, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.x)));
@@ -780,10 +780,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_sbc(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x)));
     break;
   case 0xe2:
-    cpu_65c02_ill22(self);
+    cpu_65c02_illegal_opcode22(self);
     break;
   case 0xe3:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xe4:
     cpu_65c02_cmp(self, self->regs.byte.x, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page(self)));
@@ -807,7 +807,7 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_nop(self);
     break;
   case 0xeb:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xec:
     cpu_65c02_cmp(self, self->regs.byte.x, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute(self)));
@@ -834,10 +834,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_sbc(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self)));
     break;
   case 0xf3:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xf4:
-    cpu_65c02_ill24(self);
+    cpu_65c02_illegal_opcode24(self);
     break;
   case 0xf5:
     cpu_65c02_sbc(self, cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x)));
@@ -858,10 +858,10 @@ void cpu_65c02_execute(struct cpu_65c02 *self) {
     cpu_65c02_pl(self, CPU_65C02_EA_X);
     break;
   case 0xfb:
-    cpu_65c02_ill11(self);
+    cpu_65c02_illegal_opcode11(self);
     break;
   case 0xfc:
-    cpu_65c02_ill34(self);
+    cpu_65c02_illegal_opcode34(self);
     break;
   case 0xfd:
     cpu_65c02_sbc(self, cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.x)));
index 559ed85..2b27a43 100644 (file)
@@ -327,30 +327,30 @@ static ALWAYS_INLINE void cpu_65c02_eor(struct cpu_65c02 *self, int rvalue) {
   self->regs.bit.nf = result >> 7;
 }
 
-static ALWAYS_INLINE void cpu_65c02_ill11(struct cpu_65c02 *self) {
+static ALWAYS_INLINE void cpu_65c02_illegal_opcode11(struct cpu_65c02 *self) {
 }
 
-static ALWAYS_INLINE void cpu_65c02_ill22(struct cpu_65c02 *self) {
+static ALWAYS_INLINE void cpu_65c02_illegal_opcode22(struct cpu_65c02 *self) {
   ++self->cycles;
   self->regs.word.pc += 1;
 }
 
-static ALWAYS_INLINE void cpu_65c02_ill23(struct cpu_65c02 *self) {
+static ALWAYS_INLINE void cpu_65c02_illegal_opcode23(struct cpu_65c02 *self) {
   self->cycles += 2;
   self->regs.word.pc += 1;
 }
 
-static ALWAYS_INLINE void cpu_65c02_ill24(struct cpu_65c02 *self) {
+static ALWAYS_INLINE void cpu_65c02_illegal_opcode24(struct cpu_65c02 *self) {
   self->cycles += 3;
   self->regs.word.pc += 1;
 }
 
-static ALWAYS_INLINE void cpu_65c02_ill34(struct cpu_65c02 *self) {
+static ALWAYS_INLINE void cpu_65c02_illegal_opcode34(struct cpu_65c02 *self) {
   self->cycles += 3;
   self->regs.word.pc += 2;
 }
 
-static ALWAYS_INLINE void cpu_65c02_ill38(struct cpu_65c02 *self) {
+static ALWAYS_INLINE void cpu_65c02_illegal_opcode38(struct cpu_65c02 *self) {
   self->cycles += 7;
   self->regs.word.pc += 2;
 }
index a17babc..a542ac8 100755 (executable)
@@ -2,6 +2,13 @@
 
 import sys
 
+# for these opcodes, operand 1 is an rvalue
+rvalue1_opcodes = {
+  'cmp',
+  'st'
+}
+
+# for these opcodes, last operand is an rvalue
 rvalue_opcodes = {
   'adc',
   'and',
@@ -10,27 +17,29 @@ rvalue_opcodes = {
   'cpx',
   'cpy',
   'eor',
-  'lda',
-  'ldx',
-  'ldy',
+  'ld',
   'ora',
+  'ph',
   'sbc',
 }
+
 rvalue_modes = {
   '#$12': 'cpu_65c02_fetch_byte(self)',
-  '$12': 'cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page(self))',
-  '$12,x': 'cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x))',
-  '$12,y': 'cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.y))',
-  '$3412': 'cpu_65c02_read_byte(self, cpu_65c02_ea_absolute(self))',
-  '$3412,x': 'cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.x))',
-  '$3412,y': 'cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed(self, self->regs.byte.y))',
-  '($12)': 'cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect(self))',
-  '($12),y': 'cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indirect_indexed(self, self->regs.byte.y))',
-  '($12,x)': 'cpu_65c02_read_byte(self, cpu_65c02_ea_zero_page_indexed_indirect(self, self->regs.byte.x))',
-  '($3412)': 'cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indirect(self))',
-  '($3412,x)': 'cpu_65c02_read_byte(self, cpu_65c02_ea_absolute_indexed_indirect(self, self->regs.byte.x))',
+  'a': 'self->regs.byte.a',
+  'x': 'self->regs.byte.x',
+  'y': 'self->regs.byte.y',
+  's': 'self->regs.byte.s',
+  'p': 'self->regs.byte.p',
 }
 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',
   '$0014': 'cpu_65c02_ea_relative(self)',
   '$12': 'cpu_65c02_ea_zero_page(self)',
   '$12,x': 'cpu_65c02_ea_zero_page_indexed(self, self->regs.byte.x)',
@@ -44,6 +53,14 @@ lvalue_modes = {
   '($3412)': 'cpu_65c02_ea_absolute_indirect(self)',
   '($3412,x)': 'cpu_65c02_ea_absolute_indexed_indirect(self, self->regs.byte.x)',
   'a': 'CPU_65C02_EA_A',
+  'x': 'CPU_65C02_EA_X',
+  'y': 'CPU_65C02_EA_Y',
+  'p': 'CPU_65C02_EA_P',
+  's': 'CPU_65C02_EA_S',
+  'c': 'CPU_65C02_REG_P_BIT_C',
+  'd': 'CPU_65C02_REG_P_BIT_D',
+  'i': 'CPU_65C02_REG_P_BIT_I',
+  'v': 'CPU_65C02_REG_P_BIT_V',
 }
 
 line = sys.stdin.readline().strip()
@@ -55,14 +72,34 @@ for i in range(0x100):
   line = sys.stdin.readline().strip()
 
   print(f'  case 0x{i:02x}:')
-  instr = line.lower().split()
-  if instr[0] == '???':
-    instr[0] = 'ill' # illegal opcode
-  if len(instr) >= 2:
-    if instr[0] in rvalue_opcodes:
-      instr[1] = rvalue_modes[instr[1]]
-    else:
-      instr[1] = lvalue_modes[instr[1]]
+  instr = line.split()
+
+  j0 = 1 + int(instr[0] in rvalue1_opcodes)
+  j1 = len(instr) - int(instr[0] in rvalue_opcodes)
+
+  # operands [1, j0) are rvalue
+  for k in range(1, j0):
+    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]]
+      )
+
+  # 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],
diff --git a/decode_65c02.sed b/decode_65c02.sed
deleted file mode 100644 (file)
index d813378..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-s/cpu_65c02_ill(self/cpu_65c02_ill11(self/
-s/cpu_65c02_rmb\([0-7]\)(self/cpu_65c02_rmb(self, \1/
-s/cpu_65c02_smb\([0-7]\)(self/cpu_65c02_smb(self, \1/
-s/cpu_65c02_ph\([axyp]\)(self/cpu_65c02_ph(self, self->regs.byte.\1/
-s/cpu_65c02_pl\([axy]\)(self/cpu_65c02_pl(self, CPU_65C02_EA_\1/
-s/cpu_65c02_bpl(self/cpu_65c02_bnc(self/
-s/cpu_65c02_bmi(self/cpu_65c02_bns(self/
-s/cpu_65c02_bne(self/cpu_65c02_bzc(self/
-s/cpu_65c02_beq(self/cpu_65c02_bzs(self/
-s/cpu_65c02_bra(self/cpu_65c02_bra(self, true/
-s/cpu_65c02_b\([czvn]\)c(self/cpu_65c02_bra(self, !self->regs.bit.\1f/
-s/cpu_65c02_b\([czvn]\)s(self/cpu_65c02_bra(self, self->regs.bit.\1f/
-s/cpu_65c02_stz(self/cpu_65c02_st(self, 0/
-s/cpu_65c02_st\([axy]\)(self/cpu_65c02_st(self, self->regs.byte.\1/
-s/cpu_65c02_ld\([axy]\)(self/cpu_65c02_ld(self, CPU_65C02_EA_\1/
-s/cpu_65c02_txs(self/cpu_65c02_st(self, self->regs.byte.x, CPU_65C02_EA_S)/
-s/cpu_65c02_t\([axys]\)\([axys]\)(self/cpu_65c02_ld(self, CPU_65C02_EA_\2, self->regs.byte.\1/
-s/cpu_65c02_cl\([cidv]\)(self/cpu_65c02_cl(self, CPU_65C02_REG_P_BIT_\1/
-s/cpu_65c02_se\([cidv]\)(self/cpu_65c02_se(self, CPU_65C02_REG_P_BIT_\1/
-s/cpu_65c02_in\([xy]\)(self/cpu_65c02_inc(self, CPU_65C02_EA_\1/
-s/cpu_65c02_de\([xy]\)(self/cpu_65c02_dec(self, CPU_65C02_EA_\1/
-s/cpu_65c02_cmp(self/cpu_65c02_cmp(self, self->regs.byte.a/
-s/cpu_65c02_cp\([xy]\)(self/cpu_65c02_cmp(self, self->regs.byte.\1/
-s/^    cpu_65c02_brk(self);$/    ++self->regs.word.pc;\n    cpu_65c02_irq(self, true, CPU_65C02_IRQ_VECTOR);/
-s/CPU_65C02_EA_a/CPU_65C02_EA_A/g
-s/CPU_65C02_EA_x/CPU_65C02_EA_X/g
-s/CPU_65C02_EA_y/CPU_65C02_EA_Y/g
-s/CPU_65C02_EA_p/CPU_65C02_EA_P/g
-s/CPU_65C02_EA_s/CPU_65C02_EA_S/g
-s/CPU_65C02_REG_P_BIT_c/CPU_65C02_REG_P_BIT_C/g
-s/CPU_65C02_REG_P_BIT_i/CPU_65C02_REG_P_BIT_I/g
-s/CPU_65C02_REG_P_BIT_d/CPU_65C02_REG_P_BIT_D/g
-s/CPU_65C02_REG_P_BIT_v/CPU_65C02_REG_P_BIT_V/g
diff --git a/decode_65c02.sh b/decode_65c02.sh
new file mode 100755 (executable)
index 0000000..07b5ea1
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+sed -f decode_65c02_pre.sed <instr_65c02.txt |\
+./decode_65c02.py |\
+sed -f decode_65c02_post.sed >decode_65c02.txt
diff --git a/decode_65c02_post.sed b/decode_65c02_post.sed
new file mode 100644 (file)
index 0000000..50a676a
--- /dev/null
@@ -0,0 +1 @@
+s/^    cpu_65c02_brk(self);$/    ++self->regs.word.pc;\n    cpu_65c02_irq(self, true, CPU_65C02_IRQ_VECTOR);/
diff --git a/decode_65c02_pre.sed b/decode_65c02_pre.sed
new file mode 100644 (file)
index 0000000..3d54ead
--- /dev/null
@@ -0,0 +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/