-
Notifications
You must be signed in to change notification settings - Fork 0
/
mos6502.h
258 lines (197 loc) · 6.79 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
MIT License
Copyright (c) 2017 Gianluca Ghettini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//minor mods for breakpoint mechanism and other things...
// !!!! MAKE SURE you call 'init_opcodes()', straight after calling constructor
#ifndef chss_mos6502_h
#define chss_mos6502_h
//============================================================================
// Name : mos6502
// Author : Gianluca Ghettini
// Version : 1.0
// Copyright :
// Description : A MOS 6502 CPU emulator written in C++
//============================================================================
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <locale.h>
#include <string>
#include <vector>
#include <wchar.h>
#include <algorithm>
#define __STDC_FORMAT_MACROS //needed this with windows/msys/mingw for: PRIu64
#include <inttypes.h>
#include "globals.h"
//#include "cpu.h"
#include "mos6502.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
{
public:
int bp_addr;
bool bp_hit;
// 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
uint64_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;
uint8_t read_bp( uint16_t addr ); //breakpoint monitored mem read
void write_bp( uint16_t addr, uint8_t data ); //breakpoint monitored mem write
// stack operations
inline void StackPush(uint8_t byte);
inline uint8_t StackPop();
public:
mos6502(BusRead r, BusWrite w);
void init_opcodes();
void NMI();
void IRQ();
void Reset();
void Run(uint32_t n);
bool Run_breakpoint( uint32_t n, int bp_addr );
};
#endif