-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmc6821.cpp
346 lines (291 loc) · 6.71 KB
/
mc6821.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
@file
@see mc6821.h for details
@version 1.0
@author Arco van Geest <[email protected]>
@copyright 2013 Arco van Geest <[email protected]> All right reserved.
This file is part of PinDA.
PinDA 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.
PinDA 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 PinDA. If not, see <http://www.gnu.org/licenses/>.
@date 20130520 Initial documented version
@brief Interface to the 6821/6820 PIA
*/
/*****************************************************************************
* Includes
******************************************************************************/
# include "pinda.h"
# include "CPUBUS.h"
# include "mc6821.h"
/*****************************************************************************
* Defines
******************************************************************************/
#define CA1_CTL0 0
#define CA1_CTL1 1
#define DDRA_XS 2
#define CA2_CTL0 3
#define CA2_CTL1 4
#define CA2_CTL2 5
#define IRQA2 6
#define IRQA1 7
#define CB1_CTL0 0
#define CB1_CTL1 1
#define DDRB_XS 2
#define CB2_CTL0 3
#define CB2_CTL1 4
#define CB2_CTL2 5
#define IRQB2 6
#define IRQB1 7
/******************************************************************************
* Base class
******************************************************************************/
MC6821::MC6821( CPUBUSClass *busptr ,int addr , String _name) {
objName=_name;
pia_addr = addr;
cra=addr+1;
crb=addr+3;
ddra=addr;
ddrb=addr+2;
pdra=ddra;
pdrb=ddrb;
bus = busptr;
write_cra(0); //clear cra
write_crb(0); //clear crb
write_ddra(0); //inputs
write_ddrb(0); //inputs
} //MC6821::MC6821
void MC6821::outputA( void) {
ddra_sv=255;
write_ddra(ddra_sv);
} //MC6821::outputA
void MC6821::outputB( void) {
ddrb_sv=255;
write_ddrb(255);
} //MC6821::outputB
void MC6821::inputA( void) {
write_ddra(0);
} //MC6821::inputA
void MC6821::inputB( void) {
write_ddrb(0);
}
void MC6821::allOnA( void) {
write_pdra(255);
} //MC6821::allOnA
void MC6821::allOnB( void) {
write_pdrb(255);
} // MC6821::allOnB
void MC6821::allOffA( void) {
write_pdra(0);
} // MC6821::allOffA
void MC6821::allOffB( void) {
write_pdrb(0);
} // MC6821::allOffB
void MC6821::on ( uint8_t pin ) {
if ( pin < 8) {
pdra_sv |= (1<<pin);
write_pdra( pdra_sv);
} else {
if ( pin < 16 ) {
pdrb_sv |= 1<<(pin-8);
write_pdrb( pdrb_sv);
}
}
if ( pin == 18 ) {
cra_sv |= (1<<3);
write_cra( cra_sv);
}
if ( pin == 20 ) {
crb_sv |= (1<<3);
write_crb( crb_sv);
}
} //MC6821::on
void MC6821::off ( uint8_t pin ) {
if ( pin < 8) {
pdra_sv &= ~(1<<pin);
write_pdra( pdra_sv);
} else {
if ( pin < 16 ) {
pdrb_sv &= ~(1<<(pin-8));
write_pdrb( pdrb_sv);
}
}
if ( pin == 18 ) {
cra_sv &= ~(1<<3);
write_cra( cra_sv);
}
if ( pin == 20 ) {
crb_sv &= ~(1<<3);
write_crb( crb_sv);
}
} //MC6821::off
// turn pin into output
void MC6821::output ( uint8_t pin ) {
if ( pin < 8) {
ddra_sv |= (1<<pin);
write_ddra( pdra_sv);
} else {
if ( pin < 16 ) {
ddrb_sv |= (1<<(pin-8));
write_ddrb( pdrb_sv);
}
}
if ( pin == 18 ) {
cra_sv |= (1<<4)|(1<<5);
write_cra( cra_sv);
}
if ( pin == 20 ) {
crb_sv |= (1<<4)|(1<<5);
write_crb( crb_sv);
}
} //MC6821::output
// turn pin into input
void MC6821::input ( uint8_t pin ) {
if ( pin < 8) {
ddra_sv &= ~(1<<pin);
write_ddra( pdra_sv);
} else {
if ( pin < 16 ) {
ddrb_sv &= ~(1<<(pin-8));
write_ddrb( pdrb_sv);
}
}
if ( pin == 18 ) {
cra_sv &= ~((1<<4)|(1<<5));
write_cra( cra_sv);
}
if ( pin == 20 ) {
crb_sv &= ~((1<<4)|(1<<5));
write_crb( crb_sv);
}
} // MC6821::input
void MC6821::toggle ( uint8_t pin ) {
if ( pin < 8) {
pdra_sv ^= (1<<pin);
write_pdra( pdra_sv);
} else {
if ( pin < 16 ) {
pdrb_sv ^= (1<<(pin-8));
write_pdrb( pdrb_sv);
}
}
if ( pin == 18 ) {
cra_sv ^= ~(1<<3);
write_cra( cra_sv );
}
if ( pin == 20 ) {
crb_sv ^= ~(1<<3);
write_crb( crb_sv );
}
} //MC6821::toggle
bool MC6821::isOn( uint8_t pin) {
if (pin<8) {
// when defined as output, see cache
if (ddra_sv & (1<<pin) ) {
return ( pdra_sv & (1<pin) )?true:false;
} else {
return ( read_pdra() & (1<pin) )?true:false;
}
} else {
if (pin<16) {
// when defined as output, see cache
if (ddrb_sv & (1<<pin-8) ) {
return ( pdrb_sv & (1<<(pin-8) ))?true:false;
} else {
return ( read_pdrb() & (1<<(pin-8)) )?true:false;
}
}
}
if (pin==17) {
return ( read_cra() & (1<<6) )?true:false;
}
if (pin==18) {
return ( read_cra() & (1<<7) )?true:false;
}
if (pin==19) {
return ( read_crb() & (1<<6) )?true:false;
}
if (pin==20) {
return ( read_crb() & (1<<7) )?true:false;
}
return false;
} //MC6821::isOn
void MC6821::write_cra( const uint8_t & data ) {
cra_sv=data;
bus->write( cra ,data);
} //write_cra
void MC6821::write_crb( const uint8_t & data ) {
crb_sv=data;
bus->write( crb ,data);
}
void MC6821::write_ddra( const uint8_t & data){
cra_sv &= ~(1<<DDRA_XS);
bus->write( cra ,cra_sv);
ddra_sv=data;
bus->write( ddra ,ddra_sv);
}
void MC6821::write_ddrb( const uint8_t & data){
crb_sv &= ~(1<<DDRB_XS);
bus->write( crb ,crb_sv);
ddrb_sv=data;
bus->write( ddrb ,ddrb_sv);
}
void MC6821::write_pdra( const uint8_t & data){
cra_sv |= (1<<DDRA_XS);
bus->write( cra ,cra_sv);
pdra_sv=data;
bus->write( pdra ,pdra_sv);
}
void MC6821::write_pdrb( const uint8_t & data){
crb_sv |= (1<<DDRB_XS);
bus->write( crb ,crb_sv);
pdrb_sv=data;
bus->write( pdrb ,pdrb_sv);
}
uint8_t MC6821::read_pdra(){
cra_sv |= (1<<DDRA_XS);
bus->write( cra ,cra_sv);
pdra_sv=bus->read( pdra );
return pdra_sv;
}
uint8_t MC6821::read_pdrb(){
crb_sv |= (1<<DDRB_XS);
bus->write( crb ,crb_sv);
pdrb_sv=bus->read( pdrb);
return pdrb_sv;
}
uint8_t MC6821::read_cra(){
return bus->read( cra );
}
uint8_t MC6821::read_crb(){
return bus->read( crb );
}
void MC6821::select_ddra(){
cra_sv &= ~(1<<DDRA_XS);
bus->write( cra ,cra_sv);
}
void MC6821::select_ddrb(){
crb_sv &= ~(1<<DDRB_XS);
bus->write( crb ,crb_sv);
}
void MC6821::select_pdra(){
cra_sv |= (1<<DDRA_XS);
bus->write( cra ,cra_sv);
}
void MC6821::select_pdrb(){
crb_sv |= (1<<DDRB_XS);
bus->write( crb ,crb_sv);
}
/*
uint8_t MC6821::read( const int & address ){
return 0;
}
void MC6821::write ( const int & address, const uint8_t & data){
}
*/