forked from gianlucag/SidBerry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mos6502.h
executable file
·191 lines (153 loc) · 4.96 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
191
//============================================================================
// Name : mos6502
// Author : Gianluca Ghettini
// Version : 1.0
// Copyright :
// Description : A MOS 6502 CPU emulator written in C++
//============================================================================
#include <iostream>
#include <stdint.h>
using namespace std;
#define NEGATIVE 0x80
#define OVERFLOW 0x40
#define CONSTANT 0x20
#define BREAK 0x10
#define DECIMAL 0x08
#define INTERRUPT 0x04
#define ZERO 0x02
#define CARRY 0x01
#define SET_NEGATIVE(x) (x ? (status |= NEGATIVE) : (status &= (~NEGATIVE)) )
#define SET_OVERFLOW(x) (x ? (status |= OVERFLOW) : (status &= (~OVERFLOW)) )
#define SET_CONSTANT(x) (x ? (status |= CONSTANT) : (status &= (~CONSTANT)) )
#define SET_BREAK(x) (x ? (status |= BREAK) : (status &= (~BREAK)) )
#define SET_DECIMAL(x) (x ? (status |= DECIMAL) : (status &= (~DECIMAL)) )
#define SET_INTERRUPT(x) (x ? (status |= INTERRUPT) : (status &= (~INTERRUPT)) )
#define SET_ZERO(x) (x ? (status |= ZERO) : (status &= (~ZERO)) )
#define SET_CARRY(x) (x ? (status |= CARRY) : (status &= (~CARRY)) )
#define IF_NEGATIVE() ((status & NEGATIVE) ? true : false)
#define IF_OVERFLOW() ((status & OVERFLOW) ? true : false)
#define IF_CONSTANT() ((status & CONSTANT) ? true : false)
#define IF_BREAK() ((status & BREAK) ? true : false)
#define IF_DECIMAL() ((status & DECIMAL) ? true : false)
#define IF_INTERRUPT() ((status & INTERRUPT) ? true : false)
#define IF_ZERO() ((status & ZERO) ? true : false)
#define IF_CARRY() ((status & CARRY) ? true : false)
class mos6502
{
private:
// 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;
// consumed clock cycles
uint32_t cycles;
typedef void (mos6502::*CodeExec)(uint16_t);
typedef uint16_t (mos6502::*AddrExec)();
typedef struct Instr
{
AddrExec addr;
CodeExec code;
};
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 callbacks
typedef void (*BusWrite)(uint16_t, uint8_t);
typedef uint8_t (*BusRead)(uint16_t);
BusRead Read;
BusWrite Write;
// stack operations
inline void StackPush(uint8_t byte);
inline uint8_t StackPop();
public:
mos6502(BusRead r, BusWrite w);
void NMI();
void IRQ();
void Reset();
void Run(uint32_t n);
};