swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 1 | #include "cpu.h" |
| 2 | #include "instructions.h" |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 3 | #define SCREEN_ONLY_SDL |
| 4 | #include "screen.h" |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 5 | |
| 6 | #include <endian.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #define die(m, ...) \ |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 12 | printf("\033[31m" m "\033[0m\n", ##__VA_ARGS__); \ |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 13 | exit(1); |
| 14 | |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 15 | #define warn(m, ...) \ |
| 16 | printf("\033[33m" m "\033[0m\n", ##__VA_ARGS__); |
| 17 | |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 18 | |
| 19 | sdl_screen_t *g_scr = NULL; |
| 20 | |
| 21 | |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 22 | void reset(cpu_t *cpu) |
| 23 | { |
| 24 | cpu->regs[SP] = 0xFD; // stack at is 0x100 + SP |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 25 | cpu->pc = 0x600; // arbitrary program counter start |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 26 | cpu->running = true; |
| 27 | } |
| 28 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 29 | cpu_t new_cpu() |
| 30 | { |
| 31 | cpu_t cpu = { 0 }; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 32 | cpu.regs[SP] = 0xFD; // stack at is 0x100 + SP |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 33 | cpu.pc = 0x600; // arbitrary program counter start |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 34 | cpu.running = true; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 35 | cpu.mem = malloc(0xFFFF); |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 36 | cpu.screen_dirty = true; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 37 | memset(cpu.mem, 0, 0xFFFF); |
| 38 | |
| 39 | if (!cpu.mem) |
| 40 | { |
| 41 | die("Could not allocate memory for CPU"); |
| 42 | } |
| 43 | |
| 44 | return cpu; |
| 45 | } |
| 46 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 47 | uint16_t le_to_native(uint8_t a, uint8_t b) |
| 48 | { |
| 49 | #ifdef LITTLE_ENDIAN |
| 50 | return b << 8 | a; |
| 51 | #else |
| 52 | return a << 8 | b; |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | void native_to_le(uint16_t n, uint8_t *a, uint8_t *b) |
| 57 | { |
| 58 | #ifdef LITTLE_ENDIAN |
| 59 | *a = n >> 8; |
| 60 | *b = n & 0xFF; |
| 61 | #else |
| 62 | *a = n & 0xFF; |
| 63 | *b = n >> 8; |
| 64 | #endif |
| 65 | } |
| 66 | |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 67 | void stack_push(cpu_t *cpu, uint8_t v) |
| 68 | { |
| 69 | cpu->mem[cpu->regs[SP]-- + 0x100] = v; |
| 70 | } |
| 71 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 72 | void stack_pushle(cpu_t *cpu, uint16_t v) |
| 73 | { |
| 74 | uint8_t a, b; |
| 75 | native_to_le(v, &a, &b); |
| 76 | // push in "reverse" order so that the address is stored as LE |
| 77 | stack_push(cpu, b); |
| 78 | stack_push(cpu, a); |
| 79 | } |
| 80 | |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 81 | uint8_t stack_pop(cpu_t *cpu) |
| 82 | { |
| 83 | return cpu->mem[cpu->regs[SP]++ + 0x100]; |
| 84 | } |
| 85 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 86 | uint16_t stack_pople(cpu_t *cpu) |
| 87 | { |
| 88 | uint8_t a = stack_pop(cpu); |
| 89 | uint8_t b = stack_pop(cpu); |
| 90 | return le_to_native(a, b); |
| 91 | } |
| 92 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 93 | void free_cpu(cpu_t *cpu) |
| 94 | { |
| 95 | free(cpu->mem); |
| 96 | } |
| 97 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 98 | // rotate right |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 99 | uint8_t ror(uint8_t a, uint8_t n) |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 100 | { |
| 101 | return (a >> n) | (a << (8 - n)); |
| 102 | } |
| 103 | |
| 104 | // rotate left |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 105 | uint8_t rol(uint8_t a, uint8_t n) |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 106 | { |
| 107 | return (a << n) | (a >> (8 - n)); |
| 108 | } |
| 109 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 110 | void stat_nz(cpu_t *cpu, int8_t v) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 111 | { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 112 | cpu->status.negative = v < 0; |
| 113 | cpu->status.zero = v == 0; |
| 114 | } |
| 115 | |
| 116 | // Used to check for overflow, is c unique? |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 117 | bool last_unique(bool a, bool b, bool c) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 118 | { |
| 119 | return a == b && a != c; |
| 120 | } |
| 121 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 122 | void stat_cv(cpu_t *cpu, uint8_t a, uint8_t b, uint8_t c) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 123 | { |
| 124 | cpu->status.overflow = last_unique(a >> 7, b >> 7, c >> 7); |
| 125 | cpu->status.carry = c < a || c < b; |
| 126 | } |
| 127 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 128 | void cmp(cpu_t *cpu, uint8_t reg, uint8_t mem) |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 129 | { |
| 130 | cpu->status.negative = 0; |
| 131 | cpu->status.zero = 0; |
| 132 | cpu->status.carry = 0; |
| 133 | if (cpu->regs[reg] < mem) |
| 134 | { |
| 135 | cpu->status.negative = 1; |
| 136 | } |
| 137 | else if (cpu->regs[reg] == mem) |
| 138 | { |
| 139 | cpu->status.zero = 1; |
| 140 | cpu->status.carry = 1; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | cpu->status.carry = 1; |
| 145 | } |
| 146 | } |
| 147 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 148 | uint16_t scr_dirty(cpu_t *cpu, uint16_t mem) |
| 149 | { |
| 150 | if (mem >= 0x200 && mem <= 0x200 + 32 * 32) |
| 151 | cpu->screen_dirty = true; |
| 152 | |
| 153 | return mem; |
| 154 | } |
| 155 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 156 | void execute(cpu_t *cpu, const char *mnemonic, uint8_t op, arg_t a, uint8_t am) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 157 | { |
| 158 | // used to save space |
| 159 | #define REGS \ |
| 160 | R(X) R(A) R(Y) |
| 161 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 162 | switch (op) { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 163 | // Load and store instructions: |
| 164 | #define R(reg) \ |
| 165 | case LD##reg: \ |
| 166 | cpu->regs[reg] = a.val; \ |
| 167 | stat_nz(cpu, a.val); \ |
| 168 | break; |
| 169 | |
| 170 | REGS |
| 171 | |
| 172 | #undef R |
| 173 | |
| 174 | #define R(reg) \ |
| 175 | case ST##reg: \ |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 176 | cpu->mem[scr_dirty(cpu, a.ptr)] = cpu->regs[reg]; \ |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 177 | break; \ |
| 178 | |
| 179 | REGS |
| 180 | |
| 181 | #undef R |
| 182 | |
| 183 | // Arithmetic instructions: |
| 184 | // NOTE: binary coded decimals are NOT SUPPORTED because I don't want |
| 185 | // to implement them. |
| 186 | case ADC: |
| 187 | { |
| 188 | uint8_t sum = cpu->regs[A] + a.val + cpu->status.carry; |
| 189 | // signed overflow |
| 190 | stat_cv(cpu, cpu->regs[A], a.val + cpu->status.carry, sum); |
| 191 | stat_nz(cpu, sum); |
| 192 | cpu->regs[A] = sum; |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | case SBC: |
| 197 | { |
| 198 | uint8_t diff = cpu->regs[A] - a.val - !cpu->status.carry; |
| 199 | stat_cv(cpu, cpu->regs[A], a.val - !cpu->status.carry, diff); |
| 200 | stat_nz(cpu, diff); |
| 201 | cpu->regs[A] = diff; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | case INC: |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 206 | cpu->mem[scr_dirty(cpu, scr_dirty(cpu, a.ptr))]++; |
| 207 | stat_nz(cpu, cpu->mem[scr_dirty(cpu, a.ptr)]); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 208 | break; |
| 209 | |
| 210 | case INX: |
| 211 | cpu->regs[X]++; |
| 212 | stat_nz(cpu, cpu->regs[X]); |
| 213 | break; |
| 214 | |
| 215 | case INY: |
| 216 | cpu->regs[Y]++; |
| 217 | stat_nz(cpu, cpu->regs[Y]); |
| 218 | break; |
| 219 | |
| 220 | case DEC: |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 221 | cpu->mem[scr_dirty(cpu, a.ptr)]--; |
| 222 | stat_nz(cpu, cpu->mem[scr_dirty(cpu, a.ptr)]); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 223 | break; |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 224 | |
| 225 | case DEX: |
| 226 | cpu->regs[X]--; |
| 227 | stat_nz(cpu, cpu->regs[X]); |
| 228 | break; |
| 229 | |
| 230 | case DEY: |
| 231 | cpu->regs[Y]--; |
| 232 | stat_nz(cpu, cpu->regs[Y]); |
| 233 | break; |
| 234 | |
| 235 | case ASL: |
| 236 | // This check must be done here unfortunately, it would be nice |
| 237 | // to do this while decoding operands but it would require |
| 238 | // a substantial change to the architecture of the emulator |
| 239 | if (am == AM_ACC) |
| 240 | { |
| 241 | cpu->status.carry = cpu->regs[A] >> 7; |
| 242 | cpu->regs[A] <<= 1; |
| 243 | stat_nz(cpu, cpu->regs[A]); |
| 244 | } |
| 245 | else |
| 246 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 247 | cpu->status.carry = cpu->mem[scr_dirty(cpu, a.val)] >> 7; |
| 248 | cpu->mem[scr_dirty(cpu, a.ptr)] <<= 1; |
| 249 | stat_nz(cpu, cpu->mem[scr_dirty(cpu, a.ptr)]); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 250 | } |
| 251 | break; |
| 252 | |
| 253 | case LSR: |
| 254 | if (am == AM_ACC) |
| 255 | { |
| 256 | cpu->status.carry = cpu->regs[A] & 1; |
| 257 | cpu->regs[A] >>= 1; |
| 258 | stat_nz(cpu, cpu->regs[A]); |
| 259 | } |
| 260 | else |
| 261 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 262 | cpu->status.carry = cpu->mem[scr_dirty(cpu, a.val)] & 7; |
| 263 | cpu->mem[scr_dirty(cpu, a.ptr)] >>= 1; |
| 264 | stat_nz(cpu, cpu->mem[scr_dirty(cpu, a.ptr)]); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 265 | } |
| 266 | break; |
| 267 | |
| 268 | case ROL: |
| 269 | if (am == AM_ACC) |
| 270 | { |
| 271 | cpu->status.carry = cpu->regs[A] >> 7; |
| 272 | cpu->regs[A] = rol(cpu->regs[A], 1); |
| 273 | stat_nz(cpu, cpu->regs[A]); |
| 274 | } |
| 275 | else |
| 276 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 277 | cpu->status.carry = cpu->mem[scr_dirty(cpu, a.val)] >> 7; |
| 278 | cpu->mem[scr_dirty(cpu, a.ptr)] = rol(a.val, 1); |
| 279 | stat_nz(cpu, cpu->mem[scr_dirty(cpu, a.ptr)]); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 280 | } |
| 281 | break; |
| 282 | |
| 283 | case ROR: |
| 284 | if (am == AM_ACC) |
| 285 | { |
| 286 | cpu->status.carry = cpu->regs[A] & 1; |
| 287 | cpu->regs[A] = ror(cpu->regs[A], 1); |
| 288 | stat_nz(cpu, cpu->regs[A]); |
| 289 | } |
| 290 | else |
| 291 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 292 | cpu->status.carry = cpu->mem[scr_dirty(cpu, a.val)] & 1; |
| 293 | cpu->mem[scr_dirty(cpu, a.ptr)] = ror(a.val, 1); |
| 294 | stat_nz(cpu, cpu->mem[scr_dirty(cpu, a.ptr)]); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 295 | } |
| 296 | break; |
| 297 | |
| 298 | case AND: |
| 299 | cpu->regs[A] &= a.val; |
| 300 | stat_nz(cpu, cpu->regs[A]); |
| 301 | break; |
| 302 | |
| 303 | case ORA: |
| 304 | cpu->regs[A] |= a.val; |
| 305 | stat_nz(cpu, cpu->regs[A]); |
| 306 | break; |
| 307 | |
| 308 | case EOR: |
| 309 | cpu->regs[A] ^= a.val; |
| 310 | stat_nz(cpu, cpu->regs[A]); |
| 311 | break; |
| 312 | |
| 313 | case CMP: |
| 314 | cmp(cpu, A, a.val); |
| 315 | break; |
| 316 | |
| 317 | case CPX: |
| 318 | cmp(cpu, X, a.val); |
| 319 | break; |
| 320 | |
| 321 | case CPY: |
| 322 | cmp(cpu, Y, a.val); |
| 323 | break; |
| 324 | |
| 325 | // TODO: implement BIT here |
| 326 | |
| 327 | #define BRANCHES \ |
| 328 | B(BCC, carry == 0) \ |
| 329 | B(BCS, carry == 1) \ |
| 330 | B(BNE, zero == 0) \ |
| 331 | B(BEQ, zero == 1) \ |
| 332 | B(BPL, negative == 0) \ |
| 333 | B(BMI, negative == 1) \ |
| 334 | B(BVC, overflow == 0) \ |
| 335 | B(BVS, overflow == 1) |
| 336 | |
| 337 | #define B(i, c) \ |
| 338 | case i: \ |
| 339 | if (cpu->status . c) \ |
| 340 | cpu->pc = a.ptr;\ |
| 341 | break; |
| 342 | |
| 343 | BRANCHES |
| 344 | |
| 345 | #undef B |
| 346 | #undef BRANCHES |
| 347 | |
| 348 | #define TRANSFERS \ |
| 349 | T(A, X) \ |
| 350 | T(X, A) \ |
| 351 | T(A, Y) \ |
| 352 | T(Y, A) |
| 353 | |
| 354 | #define T(a, b) \ |
| 355 | case T ## a ## b: \ |
| 356 | cpu->regs[b] = cpu->regs[a]; \ |
| 357 | stat_nz(cpu, cpu->regs[b]); \ |
| 358 | break; |
| 359 | |
| 360 | TRANSFERS |
| 361 | |
| 362 | #undef T |
| 363 | #undef TRANSFERS |
| 364 | |
| 365 | case TSX: |
| 366 | cpu->regs[X] = cpu->regs[SP]; |
| 367 | stat_nz(cpu, cpu->regs[X]); |
| 368 | break; |
| 369 | |
| 370 | case TXS: |
| 371 | cpu->regs[SP] = cpu->regs[X]; |
| 372 | stat_nz(cpu, cpu->regs[X]); |
| 373 | break; |
| 374 | |
| 375 | case PHA: |
| 376 | stack_push(cpu, cpu->regs[A]); |
| 377 | break; |
| 378 | |
| 379 | case PLA: |
| 380 | cpu->regs[A] = stack_pop(cpu); |
| 381 | stat_nz(cpu, cpu->regs[A]); |
| 382 | break; |
| 383 | |
| 384 | case PHP: |
| 385 | stack_push(cpu, *(uint8_t *)(&cpu->status)); |
| 386 | break; |
| 387 | |
| 388 | case PLP: |
| 389 | { |
| 390 | uint8_t s = stack_pop(cpu); |
| 391 | *(uint8_t *)(&cpu->status) = s; |
| 392 | } |
| 393 | |
| 394 | case JMP: |
| 395 | cpu->pc = a.ptr; |
| 396 | break; |
| 397 | |
| 398 | case JSR: |
| 399 | stack_pushle(cpu, cpu->pc); |
| 400 | break; |
| 401 | |
| 402 | case RTS: |
| 403 | cpu->pc = stack_pople(cpu); |
| 404 | break; |
| 405 | |
| 406 | // TODO: implement RTI |
| 407 | // TODO: implement flag instructions |
| 408 | |
| 409 | case BRK: |
| 410 | // TODO: trigger an interrupt |
| 411 | cpu->running = false; |
| 412 | break; |
| 413 | |
| 414 | case NOP: |
| 415 | break; |
| 416 | |
| 417 | default: |
| 418 | die("Unsupported opcode: %x\n", op); |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 419 | } |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 420 | #undef REGS |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 421 | } |
| 422 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 423 | uint16_t fetch_le(cpu_t *cpu) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 424 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 425 | uint8_t a = cpu->mem[scr_dirty(cpu, cpu->pc++)]; |
| 426 | uint8_t b = cpu->mem[scr_dirty(cpu, cpu->pc++)]; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 427 | return le_to_native(a, b); |
| 428 | } |
| 429 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 430 | arg_t arg_imm(uint16_t a) |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 431 | { |
| 432 | return (arg_t){ a, a }; |
| 433 | } |
| 434 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 435 | arg_t arg_ptr(cpu_t *c, uint flags, uint16_t p) |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 436 | { |
| 437 | if (flags & FETCH_NO_INDIRECTION) |
| 438 | return arg_imm(p); |
| 439 | |
| 440 | return (arg_t){ c->mem[p], p }; |
| 441 | } |
| 442 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 443 | arg_t arg(uint16_t v, uint16_t a) |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 444 | { |
| 445 | return (arg_t){ v, a }; |
| 446 | } |
| 447 | |
| 448 | arg_t fetch_addr(cpu_t *cpu, uint8_t am, uint f) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 449 | { |
| 450 | switch (am) |
| 451 | { |
| 452 | case AM_ACC: |
| 453 | case AM_IMP: |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 454 | return arg_imm(0); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 455 | |
| 456 | // In both cases return immediate 8 bit value |
| 457 | case AM_IMM: |
| 458 | case AM_ZP: |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 459 | return arg_imm(cpu->mem[scr_dirty(cpu, cpu->pc++)]); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 460 | |
| 461 | case AM_ABS: |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 462 | return arg_ptr(cpu, f, fetch_le(cpu)); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 463 | |
| 464 | case AM_REL: |
| 465 | { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 466 | // Aparently, PC should will point to the NEXT opcode |
| 467 | // I can't find any documentation on this unfortunately, but |
| 468 | // I have discovered this through testing the output of other |
| 469 | // assemblers. |
| 470 | uint16_t pc = cpu->pc + 1; |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 471 | return arg_ptr(cpu, f, (int8_t)cpu->mem[scr_dirty(cpu, cpu->pc++)] + pc); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | case AM_IND: |
| 475 | { |
| 476 | uint16_t addr = fetch_le(cpu); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 477 | |
| 478 | if (f & FETCH_NO_INDIRECTION) |
| 479 | return arg_imm(addr); |
| 480 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 481 | uint8_t low = cpu->mem[scr_dirty(cpu, addr)], |
| 482 | high = cpu->mem[scr_dirty(cpu, addr + 1)]; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 483 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 484 | return arg_ptr(cpu, f, le_to_native(low, high)); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | case AM_AX: |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 488 | if (f & FETCH_NO_INDIRECTION) |
| 489 | return arg_ptr(cpu, f, fetch_le(cpu)); |
| 490 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 491 | return arg_ptr(cpu, f, fetch_le(cpu) + cpu->regs[X]); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 492 | |
| 493 | case AM_AY: |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 494 | if (f & FETCH_NO_INDIRECTION) |
| 495 | return arg_ptr(cpu, f, fetch_le(cpu)); |
| 496 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 497 | return arg_ptr(cpu, f, fetch_le(cpu) + cpu->regs[Y]); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 498 | |
| 499 | case AM_ZPX: |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 500 | if (f & FETCH_NO_INDIRECTION) |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 501 | return arg_ptr(cpu, f, cpu->mem[scr_dirty(cpu, cpu->pc++)]); |
| 502 | return arg_ptr(cpu, f, cpu->mem[scr_dirty(cpu, cpu->pc++)] + cpu->regs[X]); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 503 | |
| 504 | case AM_ZPY: |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 505 | if (f & FETCH_NO_INDIRECTION) |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 506 | return arg_ptr(cpu, f, cpu->mem[scr_dirty(cpu, cpu->pc++)]); |
| 507 | return arg_ptr(cpu, f, cpu->mem[scr_dirty(cpu, cpu->pc++)] + cpu->regs[Y]); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 508 | |
| 509 | case AM_ZIX: |
| 510 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 511 | uint8_t zp = cpu->mem[scr_dirty(cpu, cpu->pc++)]; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 512 | |
| 513 | if (f & FETCH_NO_INDIRECTION) |
| 514 | return arg_imm(zp); |
| 515 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 516 | uint16_t addr = zp + cpu->regs[X]; |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 517 | uint16_t indirect = le_to_native(cpu->mem[scr_dirty(cpu, addr)], cpu->mem[scr_dirty(cpu, addr + 1)]); |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 518 | return arg_ptr(cpu, f, indirect); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | case AM_ZIY: |
| 522 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 523 | uint8_t zp = cpu->mem[scr_dirty(cpu, cpu->pc++)]; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 524 | |
| 525 | if (f & FETCH_NO_INDIRECTION) |
| 526 | return arg_imm(zp); |
| 527 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 528 | uint16_t base = le_to_native(cpu->mem[scr_dirty(cpu, zp)], cpu->mem[scr_dirty(cpu, zp + 1)]); |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 529 | return arg_ptr(cpu, f, base + cpu->regs[Y]); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | default: |
| 533 | die("Unknown address mode %x", am); |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 534 | __builtin_unreachable(); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 538 | void step(cpu_t *cpu) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 539 | { |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 540 | static int steps; |
| 541 | steps++; |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 542 | cpu->screen_dirty = false; |
| 543 | switch (cpu->mem[scr_dirty(cpu, cpu->pc++)]) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 544 | { |
| 545 | #define INST(mn, am, op) \ |
| 546 | case op: \ |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 547 | execute(cpu, #mn, mn, fetch_addr(cpu, am, 0), am); \ |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 548 | break; |
| 549 | |
| 550 | INSTRUCTIONS |
| 551 | |
| 552 | #undef INST |
| 553 | |
| 554 | default: |
| 555 | die("Undefined opcode"); |
| 556 | } |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 557 | |
| 558 | if (steps % 100 == 0) |
| 559 | printf("%d\n", steps); |
| 560 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 561 | // If can't run screen in seperate thread, just run it here (bad) |
| 562 | #ifdef NO_PTHREAD |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 563 | if (g_scr) |
| 564 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 565 | sdl_screen(g_scr, cpu->mem + CPU_FB_ADDR, cpu->screen_dirty); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 566 | } |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 567 | #endif |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 568 | } |
| 569 | |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 570 | int dump_inst(cpu_t *cpu, char *buf, const char *mn, uint16_t addr, uint8_t am) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 571 | { |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 572 | char *end = buf; |
| 573 | end += sprintf(end, "%s ", mn); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 574 | |
| 575 | switch (am) |
| 576 | { |
| 577 | case AM_IMM: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 578 | end += sprintf(end, "#"); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 579 | case AM_REL: |
| 580 | case AM_ABS: |
| 581 | case AM_ZP: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 582 | end += sprintf(end, "$%x", addr); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 583 | break; |
| 584 | |
| 585 | case AM_IND: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 586 | end += sprintf(end, "($%x)", addr); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 587 | break; |
| 588 | |
| 589 | case AM_AX: |
| 590 | case AM_ZPX: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 591 | end += sprintf(end, "$%x, X", addr); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 592 | break; |
| 593 | |
| 594 | case AM_AY: |
| 595 | case AM_ZPY: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 596 | end += sprintf(end, "$%x, Y", addr); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 597 | break; |
| 598 | |
| 599 | case AM_ZIX: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 600 | end += sprintf(end, "($%x, X)", addr); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 601 | break; |
| 602 | |
| 603 | case AM_ZIY: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 604 | end += sprintf(end, "($%x), Y", addr); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 605 | break; |
| 606 | } |
| 607 | |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 608 | return end - buf; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 609 | } |
| 610 | |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 611 | char *disas_step(cpu_t *cpu) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 612 | { |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 613 | char *buffer = malloc(80); |
| 614 | char *end = buffer; |
| 615 | |
| 616 | // end += sprintf(buffer, "$%x", cpu->pc); |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 617 | uint8_t op = cpu->mem[scr_dirty(cpu, cpu->pc++)]; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 618 | switch (op) |
| 619 | { |
| 620 | #define INST(mn, am, op) \ |
| 621 | case op: \ |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 622 | end += dump_inst(cpu, end, #mn, \ |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 623 | fetch_addr(cpu, am, FETCH_NO_INDIRECTION).ptr, am); \ |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 624 | break; |
| 625 | |
| 626 | INSTRUCTIONS |
| 627 | |
| 628 | #undef INST |
| 629 | |
| 630 | default: |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 631 | end += sprintf(end, "Undefined opcode %x", op); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 632 | } |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 633 | |
| 634 | *end = 0; |
| 635 | |
| 636 | return buffer; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 637 | } |
| 638 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 639 | void disas_num(cpu_t *cpu, uint16_t num) |
| 640 | { |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 641 | uint16_t pc = cpu->pc; |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 642 | for (int i = 0; i < num; i++) |
| 643 | { |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 644 | uint16_t last_pc = cpu->pc; |
| 645 | char *line = disas_step(cpu); |
| 646 | printf("$%x\t%s\n", last_pc, line); |
| 647 | free(line); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 648 | } |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 649 | cpu->pc = pc; |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 650 | } |
| 651 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 652 | void disas(cpu_t *cpu) |
| 653 | { |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 654 | uint16_t pc = cpu->pc; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 655 | // Raw binary, no way to know what's code what isn't |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 656 | while (cpu->pc < 0xFFFF) |
| 657 | { |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 658 | uint16_t last_pc = cpu->pc; |
| 659 | char *line = disas_step(cpu); |
| 660 | printf("$%x\t%s\n", last_pc, line); |
| 661 | free(line); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 662 | } |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 663 | cpu->pc = pc; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 664 | } |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 665 | |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 666 | void run(cpu_t *cpu) |
| 667 | { |
| 668 | while (cpu->running) |
| 669 | { |
| 670 | step(cpu); |
| 671 | } |
| 672 | |
| 673 | printf("CPU Halted\n"); |
| 674 | } |