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