1 |
/* |
2 |
* CIA.h - 6526 emulation |
3 |
* |
4 |
* Frodo (C) 1994-1997,2002-2005 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 |
bool ta_irq_next_cycle, // Flag: Trigger TA IRQ in next cycle |
71 |
tb_irq_next_cycle, // Flag: Trigger TB IRQ in next cycle |
72 |
has_new_cra, // Flag: New value for CRA pending |
73 |
has_new_crb; // Flag: New value for CRB pending |
74 |
char ta_state, tb_state; // Timer A/B states |
75 |
uint8 new_cra, new_crb; // New values for CRA/CRB |
76 |
#endif |
77 |
}; |
78 |
|
79 |
|
80 |
class MOS6526_1 : public MOS6526 { |
81 |
public: |
82 |
MOS6526_1(MOS6510 *CPU, MOS6569 *VIC); |
83 |
|
84 |
void Reset(void); |
85 |
uint8 ReadRegister(uint16 adr); |
86 |
void WriteRegister(uint16 adr, uint8 byte); |
87 |
virtual void TriggerInterrupt(int bit); |
88 |
|
89 |
uint8 KeyMatrix[8]; // C64 keyboard matrix, 1 bit/key (0: key down, 1: key up) |
90 |
uint8 RevMatrix[8]; // Reversed keyboard matrix |
91 |
|
92 |
uint8 Joystick1; // Joystick 1 AND value |
93 |
uint8 Joystick2; // Joystick 2 AND value |
94 |
|
95 |
private: |
96 |
void check_lp(void); |
97 |
|
98 |
MOS6569 *the_vic; |
99 |
|
100 |
uint8 prev_lp; // Previous state of LP line (bit 4) |
101 |
}; |
102 |
|
103 |
|
104 |
class MOS6526_2 : public MOS6526{ |
105 |
public: |
106 |
MOS6526_2(MOS6510 *CPU, MOS6569 *VIC, MOS6502_1541 *CPU1541); |
107 |
|
108 |
void Reset(void); |
109 |
uint8 ReadRegister(uint16 adr); |
110 |
void WriteRegister(uint16 adr, uint8 byte); |
111 |
virtual void TriggerInterrupt(int bit); |
112 |
|
113 |
uint8 IECLines; // State of IEC lines (bit 7 - DATA, bit 6 - CLK, bit 4 - ATN) |
114 |
|
115 |
private: |
116 |
MOS6569 *the_vic; |
117 |
MOS6502_1541 *the_cpu_1541; |
118 |
}; |
119 |
|
120 |
|
121 |
// CIA state |
122 |
struct MOS6526State { |
123 |
uint8 pra; |
124 |
uint8 ddra; |
125 |
uint8 prb; |
126 |
uint8 ddrb; |
127 |
uint8 ta_lo; |
128 |
uint8 ta_hi; |
129 |
uint8 tb_lo; |
130 |
uint8 tb_hi; |
131 |
uint8 tod_10ths; |
132 |
uint8 tod_sec; |
133 |
uint8 tod_min; |
134 |
uint8 tod_hr; |
135 |
uint8 sdr; |
136 |
uint8 int_data; // Pending interrupts |
137 |
uint8 cra; |
138 |
uint8 crb; |
139 |
// Additional registers |
140 |
uint16 latcha; // Timer latches |
141 |
uint16 latchb; |
142 |
uint8 alm_10ths; // Alarm time |
143 |
uint8 alm_sec; |
144 |
uint8 alm_min; |
145 |
uint8 alm_hr; |
146 |
uint8 int_mask; // Enabled interrupts |
147 |
}; |
148 |
|
149 |
|
150 |
/* |
151 |
* Emulate CIA for one cycle/raster line |
152 |
*/ |
153 |
|
154 |
#ifdef FRODO_SC |
155 |
inline void MOS6526::CheckIRQs(void) |
156 |
{ |
157 |
// Trigger pending interrupts |
158 |
if (ta_irq_next_cycle) { |
159 |
ta_irq_next_cycle = false; |
160 |
TriggerInterrupt(1); |
161 |
} |
162 |
if (tb_irq_next_cycle) { |
163 |
tb_irq_next_cycle = false; |
164 |
TriggerInterrupt(2); |
165 |
} |
166 |
} |
167 |
#else |
168 |
inline void MOS6526::EmulateLine(int cycles) |
169 |
{ |
170 |
unsigned long tmp; |
171 |
|
172 |
// Timer A |
173 |
if (ta_cnt_phi2) { |
174 |
ta = tmp = ta - cycles; // Decrement timer |
175 |
|
176 |
if (tmp > 0xffff) { // Underflow? |
177 |
ta = latcha; // Reload timer |
178 |
|
179 |
if (cra & 8) { // One-shot? |
180 |
cra &= 0xfe; |
181 |
ta_cnt_phi2 = false; |
182 |
} |
183 |
TriggerInterrupt(1); |
184 |
if (tb_cnt_ta) { // Timer B counting underflows of Timer A? |
185 |
tb = tmp = tb - 1; // tmp = --tb doesn't work |
186 |
if (tmp > 0xffff) goto tb_underflow; |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
// Timer B |
192 |
if (tb_cnt_phi2) { |
193 |
tb = tmp = tb - cycles; // Decrement timer |
194 |
|
195 |
if (tmp > 0xffff) { // Underflow? |
196 |
tb_underflow: |
197 |
tb = latchb; |
198 |
|
199 |
if (crb & 8) { // One-shot? |
200 |
crb &= 0xfe; |
201 |
tb_cnt_phi2 = false; |
202 |
tb_cnt_ta = false; |
203 |
} |
204 |
TriggerInterrupt(2); |
205 |
} |
206 |
} |
207 |
} |
208 |
#endif |
209 |
|
210 |
#endif |