-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmos6502.h
190 lines (162 loc) · 4.6 KB
/
mos6502.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//============================================================================
// Name : mos6502
// Author : Gianluca Ghettini
// Version : 1.0
// Copyright :
// Description : A MOS 6502 CPU emulator written in C++
//============================================================================
#pragma once
#include <stdint.h>
class mos6502
{
private:
// register reset values
uint8_t reset_A;
uint8_t reset_X;
uint8_t reset_Y;
uint8_t reset_sp;
uint8_t reset_status;
// registers
uint8_t A; // accumulator
uint8_t X; // X-index
uint8_t Y; // Y-index
// stack pointer
uint8_t sp;
// program counter
uint16_t pc;
// status register
uint8_t status;
typedef void (mos6502::*CodeExec)(uint16_t);
typedef uint16_t (mos6502::*AddrExec)();
struct Instr
{
AddrExec addr;
CodeExec code;
uint8_t cycles;
};
static Instr InstrTable[256];
void Exec(Instr i);
bool illegalOpcode;
// addressing modes
uint16_t Addr_ACC(); // ACCUMULATOR
uint16_t Addr_IMM(); // IMMEDIATE
uint16_t Addr_ABS(); // ABSOLUTE
uint16_t Addr_ZER(); // ZERO PAGE
uint16_t Addr_ZEX(); // INDEXED-X ZERO PAGE
uint16_t Addr_ZEY(); // INDEXED-Y ZERO PAGE
uint16_t Addr_ABX(); // INDEXED-X ABSOLUTE
uint16_t Addr_ABY(); // INDEXED-Y ABSOLUTE
uint16_t Addr_IMP(); // IMPLIED
uint16_t Addr_REL(); // RELATIVE
uint16_t Addr_INX(); // INDEXED-X INDIRECT
uint16_t Addr_INY(); // INDEXED-Y INDIRECT
uint16_t Addr_ABI(); // ABSOLUTE INDIRECT
// opcodes (grouped as per datasheet)
void Op_ADC(uint16_t src);
void Op_AND(uint16_t src);
void Op_ASL(uint16_t src); void Op_ASL_ACC(uint16_t src);
void Op_BCC(uint16_t src);
void Op_BCS(uint16_t src);
void Op_BEQ(uint16_t src);
void Op_BIT(uint16_t src);
void Op_BMI(uint16_t src);
void Op_BNE(uint16_t src);
void Op_BPL(uint16_t src);
void Op_BRK(uint16_t src);
void Op_BVC(uint16_t src);
void Op_BVS(uint16_t src);
void Op_CLC(uint16_t src);
void Op_CLD(uint16_t src);
void Op_CLI(uint16_t src);
void Op_CLV(uint16_t src);
void Op_CMP(uint16_t src);
void Op_CPX(uint16_t src);
void Op_CPY(uint16_t src);
void Op_DEC(uint16_t src);
void Op_DEX(uint16_t src);
void Op_DEY(uint16_t src);
void Op_EOR(uint16_t src);
void Op_INC(uint16_t src);
void Op_INX(uint16_t src);
void Op_INY(uint16_t src);
void Op_JMP(uint16_t src);
void Op_JSR(uint16_t src);
void Op_LDA(uint16_t src);
void Op_LDX(uint16_t src);
void Op_LDY(uint16_t src);
void Op_LSR(uint16_t src); void Op_LSR_ACC(uint16_t src);
void Op_NOP(uint16_t src);
void Op_ORA(uint16_t src);
void Op_PHA(uint16_t src);
void Op_PHP(uint16_t src);
void Op_PLA(uint16_t src);
void Op_PLP(uint16_t src);
void Op_ROL(uint16_t src); void Op_ROL_ACC(uint16_t src);
void Op_ROR(uint16_t src); void Op_ROR_ACC(uint16_t src);
void Op_RTI(uint16_t src);
void Op_RTS(uint16_t src);
void Op_SBC(uint16_t src);
void Op_SEC(uint16_t src);
void Op_SED(uint16_t src);
void Op_SEI(uint16_t src);
void Op_STA(uint16_t src);
void Op_STX(uint16_t src);
void Op_STY(uint16_t src);
void Op_TAX(uint16_t src);
void Op_TAY(uint16_t src);
void Op_TSX(uint16_t src);
void Op_TXA(uint16_t src);
void Op_TXS(uint16_t src);
void Op_TYA(uint16_t src);
void Op_ILLEGAL(uint16_t src);
// IRQ, reset, NMI vectors
static const uint16_t irqVectorH = 0xFFFF;
static const uint16_t irqVectorL = 0xFFFE;
static const uint16_t rstVectorH = 0xFFFD;
static const uint16_t rstVectorL = 0xFFFC;
static const uint16_t nmiVectorH = 0xFFFB;
static const uint16_t nmiVectorL = 0xFFFA;
// read/write/clock-cycle callbacks
typedef void (*BusWrite)(uint16_t, uint8_t);
typedef uint8_t (*BusRead)(uint16_t);
typedef void (*ClockCycle)(mos6502*);
BusRead Read;
BusWrite Write;
ClockCycle Cycle;
// stack operations
inline void StackPush(uint8_t byte);
inline uint8_t StackPop();
public:
enum CycleMethod {
INST_COUNT,
CYCLE_COUNT,
};
mos6502(BusRead r, BusWrite w, ClockCycle c = nullptr);
void NMI();
void IRQ();
void Reset();
void Run(
int32_t cycles,
uint64_t& cycleCount,
CycleMethod cycleMethod = CYCLE_COUNT);
void RunEternally(); // until it encounters a illegal opcode
// useful when running e.g. WOZ Monitor
// no need to worry about cycle exhaus-
// tion
uint16_t GetPC();
uint8_t GetS();
uint8_t GetP();
uint8_t GetA();
uint8_t GetX();
uint8_t GetY();
void SetResetS(uint8_t value);
void SetResetP(uint8_t value);
void SetResetA(uint8_t value);
void SetResetX(uint8_t value);
void SetResetY(uint8_t value);
uint8_t GetResetS();
uint8_t GetResetP();
uint8_t GetResetA();
uint8_t GetResetX();
uint8_t GetResetY();
};