1 |
/* |
2 |
* CIA.h - 6526 emulation |
3 |
* |
4 |
* Frodo Copyright (C) Christian Bauer |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#ifndef _CIA_H |
22 |
#define _CIA_H |
23 |
|
24 |
#include "Prefs.h" |
25 |
|
26 |
|
27 |
class MOS6510; |
28 |
class MOS6502_1541; |
29 |
class MOS6569; |
30 |
struct MOS6526State; |
31 |
|
32 |
|
33 |
class MOS6526 { |
34 |
public: |
35 |
MOS6526(MOS6510 *CPU); |
36 |
|
37 |
void Reset(void); |
38 |
void GetState(MOS6526State *cs); |
39 |
void SetState(MOS6526State *cs); |
40 |
#ifdef FRODO_SC |
41 |
void CheckIRQs(void); |
42 |
void EmulateCycle(void); |
43 |
#else |
44 |
void EmulateLine(int cycles); |
45 |
#endif |
46 |
void CountTOD(void); |
47 |
virtual void TriggerInterrupt(int bit)=0; |
48 |
|
49 |
protected: |
50 |
MOS6510 *the_cpu; // Pointer to 6510 |
51 |
|
52 |
uint8 pra, prb, ddra, ddrb; |
53 |
|
54 |
uint16 ta, tb, latcha, latchb; |
55 |
|
56 |
uint8 tod_10ths, tod_sec, tod_min, tod_hr; |
57 |
uint8 alm_10ths, alm_sec, alm_min, alm_hr; |
58 |
|
59 |
uint8 sdr, icr, cra, crb; |
60 |
uint8 int_mask; |
61 |
|
62 |
int tod_divider; // TOD frequency divider |
63 |
|
64 |
bool tod_halt, // Flag: TOD halted |
65 |
ta_cnt_phi2, // Flag: Timer A is counting Phi 2 |
66 |
tb_cnt_phi2, // Flag: Timer B is counting Phi 2 |
67 |
tb_cnt_ta; // Flag: Timer B is counting underflows of Timer A |
68 |
|
69 |
#ifdef FRODO_SC |
70 |
uint8 timer_on_pb(uint8 prb); |
71 |
|
72 |
bool ta_irq_next_cycle, // Flag: Trigger TA IRQ in next cycle |
73 |
tb_irq_next_cycle, // Flag: Trigger TB IRQ in next cycle |
74 |
has_new_cra, // Flag: New value for CRA pending |
75 |
has_new_crb, // Flag: New value for CRB pending |
76 |
ta_toggle, // TA output to PB6 toggle state |
77 |
tb_toggle; // TB output to PB7 toggle state |
78 |
char ta_state, tb_state; // Timer A/B states |
79 |
uint8 new_cra, new_crb; // New values for CRA/CRB |
80 |
#endif |
81 |
}; |
82 |
|
83 |
|
84 |
class MOS6526_1 : public MOS6526 { |
85 |
public: |
86 |
MOS6526_1(MOS6510 *CPU, MOS6569 *VIC); |
87 |
|
88 |
void Reset(void); |
89 |
uint8 ReadRegister(uint16 adr); |
90 |
void WriteRegister(uint16 adr, uint8 byte); |
91 |
virtual void TriggerInterrupt(int bit); |
92 |
|
93 |
uint8 KeyMatrix[8]; // C64 keyboard matrix, 1 bit/key (0: key down, 1: key up) |
94 |
uint8 RevMatrix[8]; // Reversed keyboard matrix |
95 |
|
96 |
uint8 Joystick1; // Joystick 1 AND value |
97 |
uint8 Joystick2; // Joystick 2 AND value |
98 |
|
99 |
private: |
100 |
void check_lp(void); |
101 |
|
102 |
MOS6569 *the_vic; |
103 |
|
104 |
uint8 prev_lp; // Previous state of LP line (bit 4) |
105 |
}; |
106 |
|
107 |
|
108 |
class MOS6526_2 : public MOS6526{ |
109 |
public: |
110 |
MOS6526_2(MOS6510 *CPU, MOS6569 *VIC, MOS6502_1541 *CPU1541); |
111 |
|
112 |
void Reset(void); |
113 |
uint8 ReadRegister(uint16 adr); |
114 |
void WriteRegister(uint16 adr, uint8 byte); |
115 |
virtual void TriggerInterrupt(int bit); |
116 |
|
117 |
uint8 IECLines; // State of IEC lines (bit 7 - DATA, bit 6 - CLK, bit 4 - ATN) |
118 |
|
119 |
private: |
120 |
MOS6569 *the_vic; |
121 |
MOS6502_1541 *the_cpu_1541; |
122 |
}; |
123 |
|
124 |
|
125 |
// CIA state |
126 |
struct MOS6526State { |
127 |
uint8 pra; |
128 |
uint8 ddra; |
129 |
uint8 prb; |
130 |
uint8 ddrb; |
131 |
uint8 ta_lo; |
132 |
uint8 ta_hi; |
133 |
uint8 tb_lo; |
134 |
uint8 tb_hi; |
135 |
uint8 tod_10ths; |
136 |
uint8 tod_sec; |
137 |
uint8 tod_min; |
138 |
uint8 tod_hr; |
139 |
uint8 sdr; |
140 |
uint8 int_data; // Pending interrupts |
141 |
uint8 cra; |
142 |
uint8 crb; |
143 |
// Additional registers |
144 |
uint16 latcha; // Timer latches |
145 |
uint16 latchb; |
146 |
uint8 alm_10ths; // Alarm time |
147 |
uint8 alm_sec; |
148 |
uint8 alm_min; |
149 |
uint8 alm_hr; |
150 |
uint8 int_mask; // Enabled interrupts |
151 |
}; |
152 |
|
153 |
|
154 |
/* |
155 |
* Emulate CIA for one cycle/raster line |
156 |
*/ |
157 |
|
158 |
#ifdef FRODO_SC |
159 |
inline void MOS6526::CheckIRQs(void) |
160 |
{ |
161 |
// Trigger pending interrupts |
162 |
if (ta_irq_next_cycle) { |
163 |
ta_irq_next_cycle = false; |
164 |
TriggerInterrupt(1); |
165 |
} |
166 |
if (tb_irq_next_cycle) { |
167 |
tb_irq_next_cycle = false; |
168 |
TriggerInterrupt(2); |
169 |
} |
170 |
} |
171 |
#else |
172 |
inline void MOS6526::EmulateLine(int cycles) |
173 |
{ |
174 |
unsigned long tmp; |
175 |
|
176 |
// Timer A |
177 |
if (ta_cnt_phi2) { |
178 |
ta = tmp = ta - cycles; // Decrement timer |
179 |
|
180 |
if (tmp > 0xffff) { // Underflow? |
181 |
ta = latcha; // Reload timer |
182 |
|
183 |
if (cra & 8) { // One-shot? |
184 |
cra &= 0xfe; |
185 |
ta_cnt_phi2 = false; |
186 |
} |
187 |
TriggerInterrupt(1); |
188 |
if (tb_cnt_ta) { // Timer B counting underflows of Timer A? |
189 |
tb = tmp = tb - 1; // tmp = --tb doesn't work |
190 |
if (tmp > 0xffff) goto tb_underflow; |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
// Timer B |
196 |
if (tb_cnt_phi2) { |
197 |
tb = tmp = tb - cycles; // Decrement timer |
198 |
|
199 |
if (tmp > 0xffff) { // Underflow? |
200 |
tb_underflow: |
201 |
tb = latchb; |
202 |
|
203 |
if (crb & 8) { // One-shot? |
204 |
crb &= 0xfe; |
205 |
tb_cnt_phi2 = false; |
206 |
tb_cnt_ta = false; |
207 |
} |
208 |
TriggerInterrupt(2); |
209 |
} |
210 |
} |
211 |
} |
212 |
#endif |
213 |
|
214 |
#endif |