swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdint.h> |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 4 | #include <stdbool.h> |
| 5 | |
| 6 | #define REGISTERS R(A) R(X) R(Y) R(SP) |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 7 | #define CPU_FB_ADDR 0x200 |
| 8 | #define CPU_FB_W 32 |
| 9 | #define CPU_FB_H 32 |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 10 | |
| 11 | enum // Registers |
| 12 | { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 13 | A, X, Y, SP |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 14 | }; |
| 15 | |
| 16 | enum // Flags |
| 17 | { |
| 18 | CARRY = 1 << 0, |
| 19 | ZERO = 1 << 1, |
| 20 | NO_INT = 1 << 2, |
| 21 | DECIMAL = 1 << 3, |
| 22 | BREAK = 1 << 4, |
| 23 | UNUSED = 1 << 5, |
| 24 | OVERFLW = 1 << 6, |
| 25 | NEGATIV = 1 << 7, |
| 26 | }; |
| 27 | |
| 28 | enum // Address Modes |
| 29 | { |
| 30 | AM_ACC, // Accumulator implied as operand |
| 31 | AM_IMP, // Implied operand |
| 32 | AM_IMM, // Immediate operand (8 bit) |
| 33 | AM_ABS, // Absolute operand (16 bit) |
| 34 | AM_ZP, // Zero-page operand |
| 35 | AM_REL, // Relative operand (signed 8 bit) |
| 36 | AM_IND, // Absolute indirect operand (16 bit address of 16 bit address) |
| 37 | AM_AX, // Absolute indexed with X |
| 38 | AM_AY, // Absolute indexed with Y |
| 39 | AM_ZPX, // Zero-page indexed with X |
| 40 | AM_ZPY, // Zero-page indexed with Y |
| 41 | AM_ZIX, // Zero-page indexed indirect (zp,x) |
| 42 | AM_ZIY, // Zero-page indirect indexed (zp),y |
| 43 | }; |
| 44 | |
| 45 | enum // Opcodes |
| 46 | { |
| 47 | LDA, |
| 48 | LDX, |
| 49 | LDY, |
| 50 | STA, |
| 51 | STX, |
| 52 | STY, |
| 53 | ADC, |
| 54 | SBC, |
| 55 | INC, |
| 56 | INX, |
| 57 | INY, |
| 58 | DEC, |
| 59 | DEX, |
| 60 | DEY, |
| 61 | ASL, |
| 62 | LSR, |
| 63 | ROL, |
| 64 | ROR, |
| 65 | AND, |
| 66 | ORA, |
| 67 | EOR, |
| 68 | CMP, |
| 69 | CPX, |
| 70 | CPY, |
| 71 | BIT, |
| 72 | BCC, |
| 73 | BCS, |
| 74 | BNE, |
| 75 | BEQ, |
| 76 | BPL, |
| 77 | BMI, |
| 78 | BVC, |
| 79 | BVS, |
| 80 | TAX, |
| 81 | TXA, |
| 82 | TAY, |
| 83 | TYA, |
| 84 | TSX, |
| 85 | TXS, |
| 86 | PHA, |
| 87 | PLA, |
| 88 | PHP, |
| 89 | PLP, |
| 90 | JMP, |
| 91 | JSR, |
| 92 | RTS, |
| 93 | RTI, |
| 94 | CLC, |
| 95 | SEC, |
| 96 | CLD, |
| 97 | SED, |
| 98 | CLI, |
| 99 | SEI, |
| 100 | CLV, |
| 101 | BRK, |
| 102 | NOP, |
| 103 | }; |
| 104 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 105 | enum // Fetch flags |
| 106 | { |
| 107 | FETCH_NO_INDIRECTION = 1, // Do not follow indirection (used for disassembly) |
| 108 | }; |
| 109 | |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 110 | // Status register |
| 111 | typedef struct __attribute__((packed)) |
| 112 | { |
| 113 | bool negative : 1; |
| 114 | bool overflow : 1; |
| 115 | bool unused : 1; |
| 116 | bool break_ : 1; |
| 117 | bool decimal : 1; |
| 118 | bool no_int : 1; |
| 119 | bool zero : 1; |
| 120 | bool carry : 1; |
| 121 | } status_t; |
| 122 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 123 | // Emulator instance, create with new_cpu() |
| 124 | typedef struct |
| 125 | { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 126 | uint8_t regs[4]; // A, X, Y, SP registers |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 127 | uint16_t pc; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 128 | status_t status; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 129 | uint8_t *mem; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 130 | bool running; |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 131 | bool screen_dirty; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 132 | } cpu_t; |
| 133 | |
| 134 | // Argument type, includes both pointer and its value |
| 135 | typedef struct |
| 136 | { |
| 137 | uint16_t val; // Value at pointer (used by most instructions) |
| 138 | uint16_t ptr; // Pointer (used by jumps, etc) |
| 139 | } arg_t; |
| 140 | |
| 141 | cpu_t new_cpu(); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 142 | void step(cpu_t *cpu); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 143 | void free_cpu(cpu_t *cpu); |
| 144 | void die(const char *message); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 145 | void reset(cpu_t *cpu); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 146 | void disas(cpu_t *cpu); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 147 | void disas_num(cpu_t *cpu, uint16_t num); |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 148 | // Buffer must be freed by user |
| 149 | char *disas_step(cpu_t *cpu); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 150 | void run(cpu_t *cpu); |