Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/lynx/c65c02.h
2 views
1
//
2
// Copyright (c) 2004 K. Wilkins
3
//
4
// This software is provided 'as-is', without any express or implied warranty.
5
// In no event will the authors be held liable for any damages arising from
6
// the use of this software.
7
//
8
// Permission is granted to anyone to use this software for any purpose,
9
// including commercial applications, and to alter it and redistribute it
10
// freely, subject to the following restrictions:
11
//
12
// 1. The origin of this software must not be misrepresented; you must not
13
// claim that you wrote the original software. If you use this software
14
// in a product, an acknowledgment in the product documentation would be
15
// appreciated but is not required.
16
//
17
// 2. Altered source versions must be plainly marked as such, and must not
18
// be misrepresented as being the original software.
19
//
20
// 3. This notice may not be removed or altered from any source distribution.
21
//
22
23
//////////////////////////////////////////////////////////////////////////////
24
// Handy - An Atari Lynx Emulator //
25
// Copyright (c) 1996,1997 //
26
// K. Wilkins //
27
//////////////////////////////////////////////////////////////////////////////
28
// 65C02 Emulation class //
29
//////////////////////////////////////////////////////////////////////////////
30
// //
31
// This class emulates a 65C02 processor. It is interfaced to the rest of //
32
// the system via the PEEK/POKE macros and a number of global variables //
33
// //
34
// K. Wilkins //
35
// August 1997 //
36
// //
37
//////////////////////////////////////////////////////////////////////////////
38
// Revision History: //
39
// ----------------- //
40
// //
41
// 01Aug1997 KW Document header added & class documented. //
42
// //
43
//////////////////////////////////////////////////////////////////////////////
44
45
#ifndef C65C02_H
46
#define C65C02_H
47
48
//#include <crtdbg.h>
49
//#define TRACE_CPU
50
51
#ifdef TRACE_CPU
52
53
#define TRACE_CPU0(msg) _RPT1(_CRT_WARN,"C65C02::"msg" (Time=%012d)\n",gSystemCycleCount)
54
#define TRACE_CPU1(msg,arg1) _RPT2(_CRT_WARN,"C65C02::"msg" (Time=%012d)\n",arg1,gSystemCycleCount)
55
#define TRACE_CPU2(msg,arg1,arg2) _RPT3(_CRT_WARN,"C65C02::"msg" (Time=%012d)\n",arg1,arg2,gSystemCycleCount)
56
#define TRACE_CPU3(msg,arg1,arg2,arg3) _RPT4(_CRT_WARN,"C65C02::"msg" (Time=%012d)\n",arg1,arg2,arg3,gSystemCycleCount)
57
58
#else
59
60
#define TRACE_CPU0(msg)
61
#define TRACE_CPU1(msg,arg1)
62
#define TRACE_CPU2(msg,arg1,arg2)
63
#define TRACE_CPU3(msg,arg1,arg2,arg3)
64
65
#endif
66
67
//
68
// Handy definitions
69
//
70
71
#define NMI_VECTOR 0xfffa
72
#define BOOT_VECTOR 0xfffc
73
#define IRQ_VECTOR 0xfffe
74
75
#define MAX_CPU_BREAKPOINTS 8
76
77
//
78
// ACCESS MACROS
79
//
80
81
//#define CPU_PEEK(m) (mSystem.Peek_CPU(m))
82
//#define CPU_PEEKW(m) (mSystem.PeekW_CPU(m))
83
//#define CPU_POKE(m1,m2) (mSystem.Poke_CPU(m1,m2))
84
85
#define CPU_PEEK(m) (((m<0xfc00)?mRamPointer[m]:mSystem.Peek_CPU(m)))
86
#define CPU_PEEKW(m) (((m<0xfc00)?(mRamPointer[m]+(mRamPointer[m+1]<<8)):mSystem.PeekW_CPU(m)))
87
#define CPU_POKE(m1,m2) {if(m1<0xfc00) mRamPointer[m1]=m2; else mSystem.Poke_CPU(m1,m2);}
88
89
90
enum { illegal=0,
91
accu,
92
imm,
93
absl,
94
zp,
95
zpx,
96
zpy,
97
absx,
98
absy,
99
iabsx,
100
impl,
101
rel,
102
zrel,
103
indx,
104
indy,
105
iabs,
106
ind
107
};
108
109
typedef struct
110
{
111
int PS; // Processor status register 8 bits
112
int A; // Accumulator 8 bits
113
int X; // X index register 8 bits
114
int Y; // Y index register 8 bits
115
int SP; // Stack Pointer 8 bits
116
int Opcode; // Instruction opcode 8 bits
117
int Operand;// Intructions operand 16 bits
118
int PC; // Program Counter 16 bits
119
bool NMI;
120
bool IRQ;
121
bool WAIT;
122
}C6502_REGS;
123
124
//
125
// The CPU emulation macros
126
//
127
#include "c6502mak.h"
128
//
129
// The CPU emulation macros
130
//
131
132
class C65C02
133
{
134
public:
135
C65C02(CSystem& parent)
136
:mSystem(parent)
137
{
138
TRACE_CPU0("C65C02()");
139
// Compute the BCD lookup table
140
for(uint16 t=0;t<256;++t)
141
{
142
mBCDTable[0][t]=((t >> 4) * 10) + (t & 0x0f);
143
mBCDTable[1][t]=(((t % 100) / 10) << 4) | (t % 10);
144
}
145
Reset();
146
147
}
148
149
~C65C02()
150
{
151
TRACE_CPU0("~C65C02()");
152
}
153
154
public:
155
void Reset();
156
void Update();
157
158
void SetRegs(C6502_REGS &regs);
159
void GetRegs(C6502_REGS &regs);
160
161
162
inline int GetPC() { return mPC; }
163
164
template<bool isReader>void SyncState(NewState *ns);
165
166
private:
167
CSystem &mSystem;
168
169
// CPU Flags & status
170
171
int mA; // Accumulator 8 bits
172
int mX; // X index register 8 bits
173
int mY; // Y index register 8 bits
174
int mSP; // Stack Pointer 8 bits
175
int mOpcode; // Instruction opcode 8 bits
176
int mOperand; // Intructions operand 16 bits
177
int mPC; // Program Counter 16 bits
178
179
int mN; // N flag for processor status register
180
int mV; // V flag for processor status register
181
int mB; // B flag for processor status register
182
int mD; // D flag for processor status register
183
int mI; // I flag for processor status register
184
int mZ; // Z flag for processor status register
185
int mC; // C flag for processor status register
186
187
int mIRQActive;
188
189
uint8 *mRamPointer;
190
191
// Associated lookup tables
192
193
int mBCDTable[2][256];
194
195
//
196
// Opcode prototypes
197
//
198
199
private:
200
201
// Answers value of the Processor Status register
202
INLINE int PS() const
203
{
204
uint8 ps = 0x20;
205
if(mN) ps|=0x80;
206
if(mV) ps|=0x40;
207
if(mB) ps|=0x10;
208
if(mD) ps|=0x08;
209
if(mI) ps|=0x04;
210
if(mZ) ps|=0x02;
211
if(mC) ps|=0x01;
212
return ps;
213
}
214
215
216
// Change the processor flags to correspond to the given value
217
INLINE void PS(int ps)
218
{
219
mN=ps&0x80;
220
mV=ps&0x40;
221
mB=ps&0x10;
222
mD=ps&0x08;
223
mI=ps&0x04;
224
mZ=ps&0x02;
225
mC=ps&0x01;
226
}
227
228
};
229
230
231
#endif
232
233