-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6502c.h
More file actions
258 lines (257 loc) · 9.84 KB
/
Copy path6502c.h
File metadata and controls
258 lines (257 loc) · 9.84 KB
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
#ifndef C6502c_H
#define C6502c_H
//==================================================================
#define DEBUG_CPU //ON,OFF
//==================================================================
#define logindex 0
//==================================================================
#ifdef DEBUG_CPU
#define cpu_print(fmt, ...) { \
printf(fmt, __VA_ARGS__); \
log( logindex, fmt, __VA_ARGS__); \
}
#endif
//==================================================================
#define _STOP_ 0
#define _RUN_ 1
#define ENABLE 1
#define DISABLE 0
//==================================================================
//6502 MEMORY MAP
//==================================================================
//(MAP) For RAM and ROM ,...A0-A15
//Example
#define _KB 1024
#define RAM_ADDRESS 0
#define RAM_SIZE 32 * _KB
#define ROM_ADDRESS RAM_SIZE
#define RAM_SIZE 32 * _KB
//==================================================================
#define EMPTY_FF 0xFF
#define EMPTY_00 0x00
//==================================================================
#define STACK_BASE 0x0100 // STACK_BASE
//==================================================================
#define IMM 0
#define ZP0 1
#define ZPX 2
#define ZPY 3
#define ABS 4
#define ABX 5
#define ABY 6
#define IND 7
#define IZX 8
#define IZY 9
#define REL 10
#define IMP 11
#define ACC 12
#define XxX 13
//==================================================================
#define Addressing_Mode_String(mode) \
((mode) == 0 ? "IMM" : \
(mode) == 1 ? "ZP0" : \
(mode) == 2 ? "ZPX" : \
(mode) == 3 ? "ZPY" : \
(mode) == 4 ? "ABS" : \
(mode) == 5 ? "ABX" : \
(mode) == 6 ? "ABY" : \
(mode) == 7 ? "IND" : \
(mode) == 8 ? "IZX" : \
(mode) == 9 ? "IZY" : \
(mode) == 10 ? "REL": \
(mode) == 11 ? "IMP": \
(mode) == 12 ? "ACC": \
(mode) == 13 ? "XxX": "INVALID")
//==================================================================
#define get_Status(cpu) ( \
((cpu)->Carry << 0) | \
((cpu)->Zero << 1) | \
((cpu)->Interrupt << 2) | \
((cpu)->DecimalMode << 3) | \
/*(cpu)->BreakCommand*/(0<< 4) | \
/*(cpu)->Reserved*/ (1<< 5) | \
((cpu)->Overflow << 6) | \
((cpu)->Negative << 7))
//==================================================================
#define set_Status(cpu, value) \
(cpu)->Carry = ((value) >> 0) & 1; \
(cpu)->Zero = ((value) >> 1) & 1; \
(cpu)->Interrupt = ((value) >> 2) & 1; \
(cpu)->DecimalMode = ((value) >> 3) & 1; \
(cpu)->BreakCommand = 0; \
(cpu)->Reserved = 1; \
(cpu)->Overflow = ((value) >> 6) & 1; \
(cpu)->Negative = ((value) >> 7) & 1;
//==================================================================
typedef union
{
struct {
// -----------------------
// CPU Emulation & Commands...
// -----------------------
unsigned short Address;
unsigned char Data;
unsigned char Mode;
unsigned char Opcode;
const char *OpcodeString;
unsigned char OpcodeStep;
unsigned short OldPC;
// Cycle
unsigned char Opcode_Cycle;
unsigned long long Cycle;
//IRQ & NMI
unsigned char NMI_Enable;
unsigned char IRQ_Enable;
// Registers 8bit
unsigned char Accumulator;
unsigned char X;
unsigned char Y;
// Program Counter 16bit
unsigned short PC;
// Stack
unsigned char SP;
// Processor Status Flags (bit field)
unsigned char Carry : 1;
unsigned char Zero : 1;
unsigned char Interrupt : 1;
unsigned char DecimalMode : 1;
unsigned char BreakCommand : 1;
unsigned char Reserved : 1;
unsigned char Overflow : 1;
unsigned char Negative : 1;
unsigned char Memory[0x10000]; //Best Thing: Make a Full Memory For 16-bit Address (A0–A15)
};
} Cpu6502;
//==================================================================
// Logical
//ORA,AND,EOR,BIT,ADC,SBC,CMP,CPY
//==================================================================
static inline void ORA(Cpu6502 *cpu);
static inline void AND(Cpu6502 *cpu);
static inline void EOR(Cpu6502 *cpu);
static inline void BIT(Cpu6502 *cpu);
//==================================================================
// Arithmetic
//ADC,SBC,CMP,CPY
//==================================================================
static inline void ADC(Cpu6502 *cpu);
static inline void SBC(Cpu6502 *cpu);
static inline void CMP(Cpu6502 *cpu);
static inline void CPY(Cpu6502 *cpu);
static inline void CPX(Cpu6502* cpu);
//==================================================================
// Increments & Decrements
//INC,INX,INY,DEC,DEX,DEY
//==================================================================
static inline void INC(Cpu6502 *cpu);
static inline void INX(Cpu6502 *cpu);
static inline void INY(Cpu6502 *cpu);
static inline void DEC(Cpu6502 *cpu);
static inline void DEX(Cpu6502 *cpu);
static inline void DEY(Cpu6502 *cpu);
//==================================================================
// Shifts
//ASL,LSR,ROL,ROR
//==================================================================
static inline void ASL(Cpu6502 *cpu);
static inline void LSR(Cpu6502 *cpu);
static inline void ROL(Cpu6502 *cpu);
static inline void ROR(Cpu6502 *cpu);
//==================================================================
// Jumps & Calls
//JMP,JSR,RTS
//==================================================================
static inline void JMP(Cpu6502 *cpu);
static inline void JSR(Cpu6502 *cpu);
static inline void RTS(Cpu6502 *cpu);
//==================================================================
// Branches
//BCC,BCS,BEQ,BMI,BNE,BPL,BVC,BVS,
//==================================================================
static inline void BCC(Cpu6502 *cpu);
static inline void BCS(Cpu6502 *cpu);
static inline void BEQ(Cpu6502 *cpu);
static inline void BMI(Cpu6502 *cpu);
static inline void BNE(Cpu6502 *cpu);
static inline void BPL(Cpu6502 *cpu);
static inline void BVC(Cpu6502 *cpu);
static inline void BVS(Cpu6502 *cpu);
//==================================================================
// Status Flag Changes
//CLC,SEC,CLI,SEI,CLV,CLD,SED
//==================================================================
static inline void CLC(Cpu6502 *cpu);
static inline void SEC(Cpu6502 *cpu);
static inline void CLI(Cpu6502 *cpu);
static inline void SEI(Cpu6502 *cpu);
static inline void CLV(Cpu6502 *cpu);
static inline void CLD(Cpu6502 *cpu);
static inline void SED(Cpu6502 *cpu);
//==================================================================
// System Functions
//BRK,NOP,RTI
//==================================================================
static inline void BRK(Cpu6502 *cpu);
static inline void NOP(Cpu6502 *cpu);
static inline void RTI(Cpu6502 *cpu);
//==================================================================
// Register Transfers
//TXA,TYA,TAY,TAX
//==================================================================
static inline void TXA(Cpu6502 *cpu);
static inline void TYA(Cpu6502 *cpu);
static inline void TAY(Cpu6502 *cpu);
static inline void TAX(Cpu6502 *cpu);
//==================================================================
// Stack Operations
//PHP,PLP,PHA,PLA,TXS,TSX
//==================================================================
static inline void PHP(Cpu6502 *cpu);
static inline void PLP(Cpu6502 *cpu);
static inline void PHA(Cpu6502 *cpu);
static inline void PLA(Cpu6502 *cpu);
static inline void TXS(Cpu6502 *cpu);
static inline void TSX(Cpu6502 *cpu);
//==================================================================
// Load/Store Operations
//LDA,LDX,LDY,STA,STX,STY,
//==================================================================
static inline void LDA(Cpu6502 *cpu);
static inline void LDX(Cpu6502 *cpu);
static inline void LDY(Cpu6502 *cpu);
static inline void STA(Cpu6502 *cpu);
static inline void STX(Cpu6502 *cpu);
static inline void STY(Cpu6502 *cpu);
//==================================================================
// Unofficial OpCodes
//==================================================================
static inline void LAX(Cpu6502* cpu);
static inline void SAX(Cpu6502* cpu);
static inline void DCP(Cpu6502* cpu);
static inline void ISB(Cpu6502* cpu);
static inline void SLO(Cpu6502* cpu);
static inline void RLA(Cpu6502* cpu);
static inline void SRE(Cpu6502* cpu);
static inline void RRA(Cpu6502* cpu);
//==================================================================
static inline void XXX(Cpu6502* cpu);
//==================================================================
static inline void Addressing_Mode(Cpu6502 *cpu);
static inline int Execute_Opcode(Cpu6502 *cpu);
//==================================================================
unsigned char Cpu_Map_Bus_Read(Cpu6502 *cpu, unsigned short address);
void Cpu_Map_Bus_Write(Cpu6502 *cpu, unsigned short address, unsigned char data);
void Cpu_Data_Read(Cpu6502* cpu);
void Cpu_Data_Write(Cpu6502* cpu);
//==================================================================
void CPU_Reset(Cpu6502 *cpu);
void CPU_Init(Cpu6502 *cpu);
//==================================================================
void CPU_IRQ_DISABLE(Cpu6502 *cpu);
void CPU_IRQ_ENABLE(Cpu6502 *cpu);
void CPU_NMI_DISABLE(Cpu6502 *cpu);
void CPU_NMI_ENABLE(Cpu6502 *cpu);
//==================================================================
int CPU_Clock(Cpu6502 *cpu);
//==================================================================
#endif