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