-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCPU1541.h
executable file
·316 lines (255 loc) · 6.83 KB
/
CPU1541.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Frodo, Commodore 64 emulator for the iPhone
Copyright (C) 1994-1997,2002 Christian Bauer
See gpl.txt for license information.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* CPU1541.h - 6502 (1541) emulation (line based)
*
* Frodo (C) 1994-1997,2002 Christian Bauer
*/
#ifndef _CPU_1541_H
#define _CPU_1541_H
#include "CIA.h"
#include "C64.h"
// Interrupt types
enum {
INT_VIA1IRQ,
INT_VIA2IRQ,
INT_IECIRQ
// INT_RESET (private)
};
class C64;
class Job1541;
class C64Display;
struct MOS6502State;
// 6502 emulation (1541)
class MOS6502_1541 {
public:
MOS6502_1541(C64 *c64, Job1541 *job, C64Display *disp, uint8 *Ram, uint8 *Rom);
void EmulateCycle(void); // Emulate one clock cycle
int EmulateLine(int cycles_left, int cycles_c64); // Emulate until cycles_left underflows
void Reset(void);
void AsyncReset(void); // Reset the CPU asynchronously
void GetState(MOS6502State *s);
void SetState(MOS6502State *s);
void CountVIATimers(int cycles);
void NewATNState(void);
void IECInterrupt(void);
void TriggerJobIRQ(void);
bool InterruptEnabled(void);
void SwitchToSC(void);
void SwitchToStandard(void);
MOS6526_2 *TheCIA2; // Pointer to C64 CIA 2
uint8 IECLines; // State of IEC lines (bit 7 - DATA, bit 6 - CLK)
bool Idle; // true: 1541 is idle
private:
//uint8 *ModeTab;
//uint8 *OpTab;
uint8 read_byte(uint16 adr);
uint16 read_word(uint16 adr);
void write_byte(uint16 adr, uint8 byte);
uint16 read_zp_word(uint16 adr);
void jump(uint16 adr);
void illegal_op(uint8 op, uint16 at);
void illegal_jump(uint16 at, uint16 to);
void do_adc(uint8 byte);
void do_sbc(uint8 byte);
void do_adc_bcd(uint8 byte);
void do_sbc_bcd(uint8 byte);
uint8 *ram; // Pointer to main RAM
uint8 *rom; // Pointer to ROM
C64 *the_c64; // Pointer to C64 object
C64Display *the_display; // Pointer to C64 display object
Job1541 *the_job; // Pointer to 1541 job object
union { // Pending interrupts
uint8 intr[4]; // Index: See definitions above
unsigned long intr_any;
} interrupt;
uint8 n_flag, z_flag;
bool v_flag, d_flag, i_flag, c_flag;
uint8 a, x, y, sp;
uint8 *pc, *pc_base;
uint16 pcSC;
uint32 first_irq_cycle;
uint8 state, op; // Current state and opcode
uint16 ar, ar2; // Address registers
uint8 rdbuf; // Data buffer for RMW instructions
uint8 ddr, pr; // Processor port
int borrowed_cycles; // Borrowed cycles from next line
uint8 via1_pra; // PRA of VIA 1
uint8 via1_ddra; // DDRA of VIA 1
uint8 via1_prb; // PRB of VIA 1
uint8 via1_ddrb; // DDRB of VIA 1
uint16 via1_t1c; // T1 Counter of VIA 1
uint16 via1_t1l; // T1 Latch of VIA 1
uint16 via1_t2c; // T2 Counter of VIA 1
uint16 via1_t2l; // T2 Latch of VIA 1
uint8 via1_sr; // SR of VIA 1
uint8 via1_acr; // ACR of VIA 1
uint8 via1_pcr; // PCR of VIA 1
uint8 via1_ifr; // IFR of VIA 1
uint8 via1_ier; // IER of VIA 1
uint8 via2_pra; // PRA of VIA 2
uint8 via2_ddra; // DDRA of VIA 2
uint8 via2_prb; // PRB of VIA 2
uint8 via2_ddrb; // DDRB of VIA 2
uint16 via2_t1c; // T1 Counter of VIA 2
uint16 via2_t1l; // T1 Latch of VIA 2
uint16 via2_t2c; // T2 Counter of VIA 2
uint16 via2_t2l; // T2 Latch of VIA 2
uint8 via2_sr; // SR of VIA 2
uint8 via2_acr; // ACR of VIA 2
uint8 via2_pcr; // PCR of VIA 2
uint8 via2_ifr; // IFR of VIA 2
uint8 via2_ier; // IER of VIA 2
};
// 6502 state
struct MOS6502State {
uint8 a, x, y;
uint8 p; // Processor flags
uint16 pc, sp;
uint8 intr[4]; // Interrupt state
bool instruction_complete; // no longer required
bool idle;
uint8 via1_pra; // VIA 1
uint8 via1_ddra;
uint8 via1_prb;
uint8 via1_ddrb;
uint16 via1_t1c;
uint16 via1_t1l;
uint16 via1_t2c;
uint16 via1_t2l;
uint8 via1_sr;
uint8 via1_acr;
uint8 via1_pcr;
uint8 via1_ifr;
uint8 via1_ier;
uint8 via2_pra; // VIA 2
uint8 via2_ddra;
uint8 via2_prb;
uint8 via2_ddrb;
uint16 via2_t1c;
uint16 via2_t1l;
uint16 via2_t2c;
uint16 via2_t2l;
uint8 via2_sr;
uint8 via2_acr;
uint8 via2_pcr;
uint8 via2_ifr;
uint8 via2_ier;
uint16 ar, ar2; // Address registers
uint8 state, op; // Current state and opcode
uint8 rdbuf; // Data buffer for RMW instructions
};
struct MOS6502StateOld {
uint8 a, x, y;
uint8 p; // Processor flags
uint16 pc, sp;
uint8 intr[4]; // Interrupt state
bool instruction_complete;
bool idle;
uint8 via1_pra; // VIA 1
uint8 via1_ddra;
uint8 via1_prb;
uint8 via1_ddrb;
uint16 via1_t1c;
uint16 via1_t1l;
uint16 via1_t2c;
uint16 via1_t2l;
uint8 via1_sr;
uint8 via1_acr;
uint8 via1_pcr;
uint8 via1_ifr;
uint8 via1_ier;
uint8 via2_pra; // VIA 2
uint8 via2_ddra;
uint8 via2_prb;
uint8 via2_ddrb;
uint16 via2_t1c;
uint16 via2_t1l;
uint16 via2_t2c;
uint16 via2_t2l;
uint8 via2_sr;
uint8 via2_acr;
uint8 via2_pcr;
uint8 via2_ifr;
uint8 via2_ier;
};
/*
* Trigger job loop IRQ
*/
inline void MOS6502_1541::TriggerJobIRQ(void)
{
if (!(interrupt.intr[INT_VIA2IRQ]))
first_irq_cycle = the_c64->CycleCounter;
interrupt.intr[INT_VIA2IRQ] = true;
Idle = false;
}
/*
* Count VIA timers
*/
inline void MOS6502_1541::CountVIATimers(int cycles)
{
unsigned long tmp;
via1_t1c = tmp = via1_t1c - cycles;
if (tmp > 0xffff) {
if (via1_acr & 0x40) // Reload from latch in free-run mode
via1_t1c = via1_t1l;
via1_ifr |= 0x40;
}
if (!(via1_acr & 0x20)) { // Only count in one-shot mode
via1_t2c = tmp = via1_t2c - cycles;
if (tmp > 0xffff)
via1_ifr |= 0x20;
}
via2_t1c = tmp = via2_t1c - cycles;
if (tmp > 0xffff) {
if (via2_acr & 0x40) // Reload from latch in free-run mode
via2_t1c = via2_t1l;
via2_ifr |= 0x40;
if (via2_ier & 0x40)
TriggerJobIRQ();
}
if (!(via2_acr & 0x20)) { // Only count in one-shot mode
via2_t2c = tmp = via2_t2c - cycles;
if (tmp > 0xffff)
via2_ifr |= 0x20;
}
}
/*
* ATN line probably changed state, recalc IECLines
*/
inline void MOS6502_1541::NewATNState(void)
{
uint8 byte = ~via1_prb & via1_ddrb;
IECLines = (byte << 6) & ((~byte ^ TheCIA2->IECLines) << 3) & 0x80 // DATA (incl. ATN acknowledge)
| (byte << 3) & 0x40; // CLK
}
/*
* Interrupt by negative edge of ATN on IEC bus
*/
inline void MOS6502_1541::IECInterrupt(void)
{
ram[0x7c] = 1;
// Wake up 1541
Idle = false;
}
/*
* Test if interrupts are enabled (for job loop)
*/
inline bool MOS6502_1541::InterruptEnabled(void)
{
return !i_flag;
}
#endif