swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 3 | #include "mnemonics.h" |
| 4 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 5 | #include <stdint.h> |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 6 | #include <stdbool.h> |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 7 | #include <mqueue.h> |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 8 | |
| 9 | #define REGISTERS R(A) R(X) R(Y) R(SP) |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 10 | #define CPU_FB_ADDR 0x200 |
| 11 | #define CPU_FB_W 32 |
| 12 | #define CPU_FB_H 32 |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 13 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 14 | #define MQ_BUF_LEN 512 |
| 15 | #define MQ_NAME "/6502-to-cpu" |
| 16 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 17 | enum // Registers |
| 18 | { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 19 | A, X, Y, SP |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | enum // Flags |
| 23 | { |
| 24 | CARRY = 1 << 0, |
| 25 | ZERO = 1 << 1, |
| 26 | NO_INT = 1 << 2, |
| 27 | DECIMAL = 1 << 3, |
| 28 | BREAK = 1 << 4, |
| 29 | UNUSED = 1 << 5, |
| 30 | OVERFLW = 1 << 6, |
| 31 | NEGATIV = 1 << 7, |
| 32 | }; |
| 33 | |
| 34 | enum // Address Modes |
| 35 | { |
| 36 | AM_ACC, // Accumulator implied as operand |
| 37 | AM_IMP, // Implied operand |
| 38 | AM_IMM, // Immediate operand (8 bit) |
| 39 | AM_ABS, // Absolute operand (16 bit) |
| 40 | AM_ZP, // Zero-page operand |
| 41 | AM_REL, // Relative operand (signed 8 bit) |
| 42 | AM_IND, // Absolute indirect operand (16 bit address of 16 bit address) |
| 43 | AM_AX, // Absolute indexed with X |
| 44 | AM_AY, // Absolute indexed with Y |
| 45 | AM_ZPX, // Zero-page indexed with X |
| 46 | AM_ZPY, // Zero-page indexed with Y |
| 47 | AM_ZIX, // Zero-page indexed indirect (zp,x) |
| 48 | AM_ZIY, // Zero-page indirect indexed (zp),y |
| 49 | }; |
| 50 | |
| 51 | enum // Opcodes |
| 52 | { |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 53 | #define MN(name) name, |
| 54 | MNEMONICS |
| 55 | #undef MN |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
swissChili | 710d18d | 2020-07-29 19:43:20 -0700 | [diff] [blame] | 58 | enum // Fetch flags |
| 59 | { |
| 60 | FETCH_NO_INDIRECTION = 1, // Do not follow indirection (used for disassembly) |
| 61 | }; |
| 62 | |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 63 | // Status register |
| 64 | typedef struct __attribute__((packed)) |
| 65 | { |
| 66 | bool negative : 1; |
| 67 | bool overflow : 1; |
| 68 | bool unused : 1; |
| 69 | bool break_ : 1; |
| 70 | bool decimal : 1; |
| 71 | bool no_int : 1; |
| 72 | bool zero : 1; |
| 73 | bool carry : 1; |
| 74 | } status_t; |
| 75 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 76 | // Emulator instance, create with new_cpu() |
| 77 | typedef struct |
| 78 | { |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 79 | uint8_t regs[4]; // A, X, Y, SP registers |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 80 | uint16_t pc; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 81 | status_t status; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 82 | uint8_t *mem; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 83 | bool running; |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 84 | bool screen_dirty; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 85 | } cpu_t; |
| 86 | |
| 87 | // Argument type, includes both pointer and its value |
| 88 | typedef struct |
| 89 | { |
| 90 | uint16_t val; // Value at pointer (used by most instructions) |
| 91 | uint16_t ptr; // Pointer (used by jumps, etc) |
| 92 | } arg_t; |
| 93 | |
| 94 | cpu_t new_cpu(); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 95 | void step(cpu_t *cpu); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 96 | void free_cpu(cpu_t *cpu); |
| 97 | void die(const char *message); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 98 | void reset(cpu_t *cpu); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 99 | void disas(cpu_t *cpu); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 100 | void disas_num(cpu_t *cpu, uint16_t num); |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 101 | // Buffer must be freed by user |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 102 | char *disas_step(cpu_t *cpu, uint16_t *pc); |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 103 | void run(cpu_t *cpu); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 104 | void run_mq(cpu_t *cpu, mqd_t mq); |