disassembler.cpp 12.2 KB
Newer Older
Screwtape's avatar
Screwtape committed
1
auto SPC700::disassemble(uint16 addr, bool p) -> string {
Screwtape's avatar
Screwtape committed
2
  auto read = [&](uint16 addr) -> uint8 {
Screwtape's avatar
Screwtape committed
3
    return readDisassembler(addr);
Screwtape's avatar
Screwtape committed
4 5
  };

Screwtape's avatar
Screwtape committed
6
  auto relative = [&](uint length, int8 offset) -> uint16 {
Screwtape's avatar
Screwtape committed
7 8 9 10
    uint16 pc = addr + length;
    return pc + offset;
  };

Screwtape's avatar
Screwtape committed
11
  auto a = [&] { return hex((read(addr + 1) << 0) + (read(addr + 2) << 8), 4L); };
Screwtape's avatar
Screwtape committed
12
  auto b = [&](uint n) { return hex(read(addr + 1 + n), 2L); };
Screwtape's avatar
Screwtape committed
13
  auto rel = [&](uint r, uint n = 0) { return hex(addr + r + (int8)read(addr + 1 + n), 4L); };
Screwtape's avatar
Screwtape committed
14
  auto dp = [&](uint n) { return hex((p << 8) + read(addr + 1 + n), 3L); };
Screwtape's avatar
Screwtape committed
15
  auto ab = [&] {
Screwtape's avatar
Screwtape committed
16 17
    uint n = (read(addr + 1) << 0) + (read(addr + 2) << 8);
    return string{hex(n & 0x1fff, 4L), ":", hex(n >> 13, 1L)};
Screwtape's avatar
Screwtape committed
18 19 20 21 22 23 24
  };

  auto mnemonic = [&]() -> string {
    switch(read(addr)) {
    case 0x00: return { "nop" };
    case 0x01: return { "jst $ffde" };
    case 0x02: return { "set $", dp(0), ":0" };
Screwtape's avatar
Screwtape committed
25
    case 0x03: return { "bbs $", dp(0), ":0=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
26 27 28 29 30 31 32 33 34 35 36 37
    case 0x04: return { "ora $", dp(0) };
    case 0x05: return { "ora $", a() };
    case 0x06: return { "ora (x)" };
    case 0x07: return { "ora ($", dp(0), ",x)" };
    case 0x08: return { "ora #$", b(0) };
    case 0x09: return { "orr $", dp(1), "=$", dp(0) };
    case 0x0a: return { "orc $", ab() };
    case 0x0b: return { "asl $", dp(0) };
    case 0x0c: return { "asl $", a() };
    case 0x0d: return { "php" };
    case 0x0e: return { "tsb $", a() };
    case 0x0f: return { "brk" };
Screwtape's avatar
Screwtape committed
38
    case 0x10: return { "bpl $", rel(+2) };
Screwtape's avatar
Screwtape committed
39 40
    case 0x11: return { "jst $ffdc" };
    case 0x12: return { "clr $", dp(0), ":0" };
Screwtape's avatar
Screwtape committed
41
    case 0x13: return { "bbc $", dp(0), ":0=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    case 0x14: return { "ora $", dp(0), ",x" };
    case 0x15: return { "ora $", a(), ",x" };
    case 0x16: return { "ora $", a(), ",y" };
    case 0x17: return { "ora ($", dp(0), "),y" };
    case 0x18: return { "orr $", dp(1), "=#$", b(0) };
    case 0x19: return { "orr (x)=(y)" };
    case 0x1a: return { "dew $", dp(0) };
    case 0x1b: return { "asl $", dp(0), ",x" };
    case 0x1c: return { "asl" };
    case 0x1d: return { "dex" };
    case 0x1e: return { "cpx $", a() };
    case 0x1f: return { "jmp ($", a(), ",x)" };
    case 0x20: return { "clp" };
    case 0x21: return { "jst $ffda" };
    case 0x22: return { "set $", dp(0), ":1" };
Screwtape's avatar
Screwtape committed
57
    case 0x23: return { "bbs $", dp(0), ":1=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
58 59 60 61
    case 0x24: return { "and $", dp(0) };
    case 0x25: return { "and $", a() };
    case 0x26: return { "and (x)" };
    case 0x27: return { "and ($", dp(0), ",x)" };
Screwtape's avatar
Screwtape committed
62
    case 0x28: return { "and #$", b(0) };
Screwtape's avatar
Screwtape committed
63 64 65 66 67
    case 0x29: return { "and $", dp(1), "=$", dp(0) };
    case 0x2a: return { "orc !$", ab() };
    case 0x2b: return { "rol $", dp(0) };
    case 0x2c: return { "rol $", a() };
    case 0x2d: return { "pha" };
Screwtape's avatar
Screwtape committed
68 69 70
    case 0x2e: return { "bne $", dp(0), "=$", rel(+3, 1) };
    case 0x2f: return { "bra $", rel(+2) };
    case 0x30: return { "bmi $", rel(+2) };
Screwtape's avatar
Screwtape committed
71 72
    case 0x31: return { "jst $ffd8" };
    case 0x32: return { "clr $", dp(0), ":1" };
Screwtape's avatar
Screwtape committed
73
    case 0x33: return { "bbc $", dp(0), ":1=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    case 0x34: return { "and $", dp(0), ",x" };
    case 0x35: return { "and $", a(), ",x" };
    case 0x36: return { "and $", a(), ",y" };
    case 0x37: return { "and ($", dp(0), "),y" };
    case 0x38: return { "and $", dp(1), "=#$", b(0) };
    case 0x39: return { "and (x)=(y)" };
    case 0x3a: return { "inw $", dp(0) };
    case 0x3b: return { "rol $", dp(0), ",x" };
    case 0x3c: return { "rol" };
    case 0x3d: return { "inx" };
    case 0x3e: return { "cpx $", dp(0) };
    case 0x3f: return { "jsr $", a() };
    case 0x40: return { "sep" };
    case 0x41: return { "jst $ffd6" };
    case 0x42: return { "set $", dp(0), ":2" };
Screwtape's avatar
Screwtape committed
89
    case 0x43: return { "bbs $", dp(0), ":2=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
90 91 92 93 94 95 96 97 98 99 100 101
    case 0x44: return { "eor $", dp(0) };
    case 0x45: return { "eor $", a() };
    case 0x46: return { "eor (x)" };
    case 0x47: return { "eor ($", dp(0), ",x)" };
    case 0x48: return { "eor #$", b(0) };
    case 0x49: return { "eor $", dp(1), "=$", dp(0) };
    case 0x4a: return { "and $", ab() };
    case 0x4b: return { "lsr $", dp(0) };
    case 0x4c: return { "lsr $", a() };
    case 0x4d: return { "phx" };
    case 0x4e: return { "trb $", a() };
    case 0x4f: return { "jsp $ff", b(0) };
Screwtape's avatar
Screwtape committed
102
    case 0x50: return { "bvc $", rel(+2) };
Screwtape's avatar
Screwtape committed
103 104
    case 0x51: return { "jst $ffd4" };
    case 0x52: return { "clr $", dp(0), ":2" };
Screwtape's avatar
Screwtape committed
105
    case 0x53: return { "bbc $", dp(0), ":2=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    case 0x54: return { "eor $", dp(0), ",x" };
    case 0x55: return { "eor $", a(), ",x" };
    case 0x56: return { "eor $", a(), ",y" };
    case 0x57: return { "eor ($", dp(0), "),y" };
    case 0x58: return { "eor $", dp(1), "=#$", b(0) };
    case 0x59: return { "eor (x)=(y)" };
    case 0x5a: return { "cpw $", a() };
    case 0x5b: return { "lsr $", dp(0), ",x" };
    case 0x5c: return { "lsr" };
    case 0x5d: return { "tax" };
    case 0x5e: return { "cpy $", a() };
    case 0x5f: return { "jmp $", a() };
    case 0x60: return { "clc" };
    case 0x61: return { "jst $ffd2" };
    case 0x62: return { "set $", dp(0), ":3" };
Screwtape's avatar
Screwtape committed
121
    case 0x63: return { "bbs $", dp(0), ":3=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
122 123 124 125 126 127 128 129 130 131
    case 0x64: return { "cmp $", dp(0) };
    case 0x65: return { "cmp $", a() };
    case 0x66: return { "cmp (x)" };
    case 0x67: return { "cmp ($", dp(0), ",x)" };
    case 0x68: return { "cmp #$", b(0) };
    case 0x69: return { "cmp $", dp(1), "=$", dp(0) };
    case 0x6a: return { "and !$", ab() };
    case 0x6b: return { "ror $", dp(0) };
    case 0x6c: return { "ror $", a() };
    case 0x6d: return { "phy" };
Screwtape's avatar
Screwtape committed
132
    case 0x6e: return { "bne --$", dp(0), "=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
133
    case 0x6f: return { "rts" };
Screwtape's avatar
Screwtape committed
134
    case 0x70: return { "bvs $", rel(+2) };
Screwtape's avatar
Screwtape committed
135 136
    case 0x71: return { "jst $ffd0" };
    case 0x72: return { "clr $", dp(0), ":3" };
Screwtape's avatar
Screwtape committed
137
    case 0x73: return { "bbc $", dp(0), ":3=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    case 0x74: return { "cmp $", dp(0), ",x" };
    case 0x75: return { "cmp $", a(), ",x" };
    case 0x76: return { "cmp $", a(), ",y" };
    case 0x77: return { "cmp ($", dp(0), "),y" };
    case 0x78: return { "cmp $", dp(1), "=#$", b(0) };
    case 0x79: return { "cmp (x)=(y)" };
    case 0x7a: return { "adw $", a() };
    case 0x7b: return { "ror $", dp(0), ",x" };
    case 0x7c: return { "ror" };
    case 0x7d: return { "txa" };
    case 0x7e: return { "cpy $", dp(0) };
    case 0x7f: return { "rti" };
    case 0x80: return { "sec" };
    case 0x81: return { "jst $ffce" };
    case 0x82: return { "set $", dp(0), ":4" };
Screwtape's avatar
Screwtape committed
153
    case 0x83: return { "bbs $", dp(0), ":4=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
154 155 156 157 158 159 160 161 162 163 164 165
    case 0x84: return { "adc $", dp(0) };
    case 0x85: return { "adc $", a() };
    case 0x86: return { "adc (x)" };
    case 0x87: return { "adc ($", dp(0), ",x)" };
    case 0x88: return { "adc #$", b(0) };
    case 0x89: return { "adc $", dp(1), "=$", dp(0) };
    case 0x8a: return { "eor $", ab() };
    case 0x8b: return { "dec $", dp(0) };
    case 0x8c: return { "dec $", a() };
    case 0x8d: return { "ldy #$", b(0) };
    case 0x8e: return { "plp" };
    case 0x8f: return { "str $", dp(1), "=#$", b(0) };
Screwtape's avatar
Screwtape committed
166
    case 0x90: return { "bcc $", rel(+2) };
Screwtape's avatar
Screwtape committed
167 168
    case 0x91: return { "jst $ffcc" };
    case 0x92: return { "clr $", dp(0), ":4" };
Screwtape's avatar
Screwtape committed
169
    case 0x93: return { "bbc $", dp(0), ":4=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    case 0x94: return { "adc $", dp(0), ",x" };
    case 0x95: return { "adc $", a(), ",x" };
    case 0x96: return { "adc $", a(), ",y" };
    case 0x97: return { "adc ($", dp(0), "),y" };
    case 0x98: return { "adc $", dp(1), "=#$", b(0) };
    case 0x99: return { "adc (x)=(y)" };
    case 0x9a: return { "sbw $", a() };
    case 0x9b: return { "dec $", dp(0), ",x" };
    case 0x9c: return { "dec" };
    case 0x9d: return { "tsx" };
    case 0x9e: return { "div" };
    case 0x9f: return { "xcn" };
    case 0xa0: return { "sei" };
    case 0xa1: return { "jst $ffca" };
    case 0xa2: return { "set $", dp(0), ":5" };
Screwtape's avatar
Screwtape committed
185
    case 0xa3: return { "bbs $", dp(0), ":5=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
186 187 188 189 190 191 192 193 194 195 196 197
    case 0xa4: return { "sbc $", dp(0) };
    case 0xa5: return { "sbc $", a() };
    case 0xa6: return { "sbc (x)" };
    case 0xa7: return { "sbc ($", dp(0), ",x)" };
    case 0xa8: return { "sbc #$", b(0) };
    case 0xa9: return { "sbc $", dp(1), "=$", dp(0) };
    case 0xaa: return { "ldc $", ab() };
    case 0xab: return { "inc $", dp(0) };
    case 0xac: return { "inc $", a() };
    case 0xad: return { "cpy #$", b(0) };
    case 0xae: return { "pla" };
    case 0xaf: return { "sta (x++)" };
Screwtape's avatar
Screwtape committed
198
    case 0xb0: return { "bcs $", rel(+2) };
Screwtape's avatar
Screwtape committed
199 200
    case 0xb1: return { "jst $ffc8" };
    case 0xb2: return { "clr $", dp(0), ":5" };
Screwtape's avatar
Screwtape committed
201
    case 0xb3: return { "bbc $", dp(0), ":5=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    case 0xb4: return { "sbc $", dp(0), ",x" };
    case 0xb5: return { "sbc $", a(), ",x" };
    case 0xb6: return { "sbc $", a(), ",y" };
    case 0xb7: return { "sbc ($", dp(0), "),y" };
    case 0xb8: return { "sbc $", dp(1), "=#$", b(0) };
    case 0xb9: return { "sbc (x)=(y)" };
    case 0xba: return { "ldw $", dp(0) };
    case 0xbb: return { "inc $", dp(0), ",x" };
    case 0xbc: return { "inc" };
    case 0xbd: return { "txs" };
    case 0xbe: return { "das" };
    case 0xbf: return { "lda (x++)" };
    case 0xc0: return { "cli" };
    case 0xc1: return { "jst $ffc6" };
    case 0xc2: return { "set $", dp(0), ":6" };
Screwtape's avatar
Screwtape committed
217
    case 0xc3: return { "bbs $", dp(0), ":6=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
218 219 220 221 222 223 224 225 226 227 228 229
    case 0xc4: return { "sta $", dp(0) };
    case 0xc5: return { "sta $", a() };
    case 0xc6: return { "sta (x)" };
    case 0xc7: return { "sta ($", dp(0), ",x)" };
    case 0xc8: return { "cpx #$", b(0) };
    case 0xc9: return { "stx $", a() };
    case 0xca: return { "stc $", ab() };
    case 0xcb: return { "sty $", dp(0) };
    case 0xcc: return { "sty $", a() };
    case 0xcd: return { "ldx #$", b(0) };
    case 0xce: return { "plx" };
    case 0xcf: return { "mul" };
Screwtape's avatar
Screwtape committed
230
    case 0xd0: return { "bne $", rel(+2) };
Screwtape's avatar
Screwtape committed
231 232
    case 0xd1: return { "jst $ffc4" };
    case 0xd2: return { "clr $", dp(0), ":6" };
Screwtape's avatar
Screwtape committed
233
    case 0xd3: return { "bbc $", dp(0), ":6=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
234 235 236 237 238 239 240 241 242 243
    case 0xd4: return { "sta $", dp(0), ",x" };
    case 0xd5: return { "sta $", a(), ",x" };
    case 0xd6: return { "sta $", a(), ",y" };
    case 0xd7: return { "sta ($", dp(0), "),y" };
    case 0xd8: return { "stx $", dp(0) };
    case 0xd9: return { "stx $", dp(0), ",y" };
    case 0xda: return { "stw $", dp(0) };
    case 0xdb: return { "sty $", dp(0), ",x" };
    case 0xdc: return { "dey" };
    case 0xdd: return { "tya" };
Screwtape's avatar
Screwtape committed
244
    case 0xde: return { "bne $", dp(0), ",x=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
245 246 247 248
    case 0xdf: return { "daa" };
    case 0xe0: return { "clv" };
    case 0xe1: return { "jst $ffc2" };
    case 0xe2: return { "set $", dp(0), ":7" };
Screwtape's avatar
Screwtape committed
249
    case 0xe3: return { "bbs $", dp(0), ":7=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
250 251 252 253 254 255 256 257 258 259 260 261
    case 0xe4: return { "lda $", dp(0) };
    case 0xe5: return { "lda $", a() };
    case 0xe6: return { "lda (x)" };
    case 0xe7: return { "lda ($", dp(0), ",x)" };
    case 0xe8: return { "lda #$", b(0) };
    case 0xe9: return { "ldx $", a() };
    case 0xea: return { "not $", ab() };
    case 0xeb: return { "ldy $", dp(0) };
    case 0xec: return { "ldy $", a() };
    case 0xed: return { "cmc" };
    case 0xee: return { "ply" };
    case 0xef: return { "wai" };
Screwtape's avatar
Screwtape committed
262
    case 0xf0: return { "beq $", rel(+2) };
Screwtape's avatar
Screwtape committed
263 264
    case 0xf1: return { "jst $ffc0" };
    case 0xf2: return { "clr $", dp(0), ":7" };
Screwtape's avatar
Screwtape committed
265
    case 0xf3: return { "bbc $", dp(0), ":7=$", rel(+3, 1) };
Screwtape's avatar
Screwtape committed
266 267 268 269 270 271 272 273 274 275
    case 0xf4: return { "lda $", dp(0), ",x" };
    case 0xf5: return { "lda $", a(), ",x" };
    case 0xf6: return { "lda $", a(), ",y" };
    case 0xf7: return { "lda ($", dp(0), "),y" };
    case 0xf8: return { "ldx $", dp(0) };
    case 0xf9: return { "ldx $", dp(0), ",y" };
    case 0xfa: return { "str $", dp(1), "=$", dp(0) };
    case 0xfb: return { "ldy $", dp(0), ",x" };
    case 0xfc: return { "iny" };
    case 0xfd: return { "tay" };
Screwtape's avatar
Screwtape committed
276
    case 0xfe: return { "bne --y=$", rel(+2) };
Screwtape's avatar
Screwtape committed
277 278 279 280 281
    case 0xff: return { "stp" };
    }
    throw;
  };

Screwtape's avatar
Screwtape committed
282
  string output = {"..", hex(addr, 4L), " ", mnemonic()};
Screwtape's avatar
Screwtape committed
283

Screwtape's avatar
Screwtape committed
284
  uint length = output.length();
Screwtape's avatar
Screwtape committed
285 286 287
  while(length++ < 30) output.append(" ");

  output.append(
Screwtape's avatar
Screwtape committed
288 289 290 291 292
    "YA:", hex(YA, 4L),
    " A:", hex(A,  2L),
    " X:", hex(X,  2L),
    " Y:", hex(Y,  2L),
    " S:", hex(S,  2L),
Screwtape's avatar
Screwtape committed
293
    " ",
Screwtape's avatar
Screwtape committed
294 295 296 297 298 299 300 301
    NF ? "N" : "n",
    VF ? "V" : "v",
    PF ? "P" : "p",
    BF ? "B" : "b",
    HF ? "H" : "h",
    IF ? "I" : "i",
    ZF ? "Z" : "z",
    CF ? "C" : "c"
Screwtape's avatar
Screwtape committed
302 303 304 305
  );

  return output;
}