forked from EtchedPixels/EmulatorKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6522.c
289 lines (269 loc) · 4.91 KB
/
6522.c
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "6522.h"
/*
* Minimal beginnings of 6522 VIA emulation
*/
struct via6522 {
uint8_t irq;
uint8_t acr;
uint8_t ifr;
uint8_t ier;
uint8_t pcr;
uint8_t sr;
uint8_t ora;
uint8_t orb;
uint8_t ira;
uint8_t irb;
uint8_t ddra;
uint8_t ddrb;
uint16_t t1;
uint16_t t1l;
uint16_t t2;
uint8_t t2l;
/* Pin states rather than registers */
uint8_t ca;
uint8_t cb;
unsigned int trace;
};
static void via_recalc_irq(struct via6522 *via)
{
int irq = (via->ier & via->ifr) & 0x7F;
if (irq)
via->ifr |= 0x80;
else
via->ifr &= 0x7F;
/* We interrupt if ier and ifr are set */
/* Note: the pin is inverted but we model irq state not the pin! */
if ((via->trace) && irq != via->irq)
fprintf(stderr, "[VIA IRQ now %02X.]\n", irq);
via->irq = irq;
}
static void via_recalc_all(struct via6522 *via)
{
via_recalc_outputs(via);
via_recalc_irq(via);
}
/* Perform time related processing for the VIA */
void via_tick(struct via6522 *via, unsigned int clocks)
{
/* This isn't quite right but it's near enough for the moment */
if (via->t1) {
if (clocks >= via->t1) {
if (via->trace)
fprintf(stderr,"[VIA T1 expire.].\n");
via->ifr |= 0x40;
via_recalc_irq(via);
/* +1 or + 2 ?? */
if (via->acr & 0x40)
via->t1 = via->t1l + 1;
else
via->t1 = 0;
}
else
via->t1 -= clocks;
}
if (via->t2 && !(via->acr & 0x20)) {
if (clocks >= via->t2) {
via->ifr |= 0x20;
via_recalc_irq(via);
via->t2 = 0;
if (via->trace)
fprintf(stderr,"[VIA T2 expire.].\n");
}
via->t2 -= clocks;
}
}
uint8_t via_read(struct via6522 *via, uint8_t addr)
{
uint8_t r;
if (via->trace)
fprintf(stderr, "[VIA read %d: ", addr);
switch(addr) {
case 0:
r = via->irb & ~via->ddrb;
r |= via->orb & via->ddrb;
via_handshake_b(via);
break;
case 1:
r = via->ira & ~via->ddra;
r |= via->ora & via->ddra;
via_handshake_a(via);
break;
case 2:
r = via->ddrb;
break;
case 3:
r = via->ddra;
break;
case 4:
via->ifr &= ~0x40; /* T1 timeout */
via_recalc_irq(via);
r = via->t1;
break;
case 5:
r = via->t1 >> 8;
break;
case 6:
r = via->t1l;
break;
case 7:
r = via->t1l >> 8;
break;
case 8:
via->ifr &= ~0x20; /* T2 timeout */
via_recalc_irq(via);
r = via->t2;
break;
case 9:
r = via->t2 >> 8;
break;
case 10:
r = via->sr;
break;
case 11:
r = via->acr;
break;
case 12:
r = via->pcr;
break;
case 13:
r = via->ifr;
break;
case 14:
r = via->ier;
break;
default:
case 15:
r = via->ira;
break;
}
if (via->trace)
fprintf(stderr, "%02X.]\n", r);
return r;
}
void via_write(struct via6522 *via, uint8_t addr, uint8_t val)
{
if (via->trace)
fprintf(stderr, "[VIA write %d: %02X.]\n", addr, val);
switch(addr) {
case 0:
via->orb = val;
via_recalc_outputs(via);
via_handshake_b(via);
break;
case 1:
via->ora = val;
via_recalc_outputs(via);
break;
case 2:
via->ddrb = val;
via_recalc_all(via);
break;
case 3:
via->ddra = val;
via_recalc_all(via);
break;
case 4:
case 6:
via->t1l &= 0xFF00;
via->t1l |= val;
break;
case 5:
via->t1l &= 0xFF;
via->t1l |= val << 8;
via->t1 = via->t1l;
via->ifr &= ~0x40; /* T1 timeout */
via_recalc_irq(via);
if (via->trace)
fprintf(stderr, "[VIA T1 begin %04X.]\n", via->t1);
break;
case 7:
via->t1l &= 0xFF;
via->t1l |= val << 8;
break;
case 8:
via->t2l = val;
break;
case 9:
via->t2 = val << 8;
via->t2 |= via->t2l;
via->ifr &= ~0x20; /* T2 timeout */
via_recalc_irq(via);
break;
case 10:
via->sr = val;
break;
case 11:
via->acr = val;
break;
case 12:
via->pcr = val;
break;
case 13:
via->ifr &= ~val;
if (via->ifr & 0x7F)
via->ifr |= 0x80;
via_recalc_irq(via);
break;
case 14:
if (val & 0x80)
via->ier |= val;
else
via->ier &= ~val;
via->ier &= 0x7F;
via_recalc_irq(via);
break;
case 15:
via->ora = val;
break;
}
}
int via_irq_pending(struct via6522 *via)
{
return via->irq;
}
void via_trace(struct via6522 *via, int onoff)
{
via->trace = onoff;
}
void via_free(struct via6522 *via)
{
free(via);
}
struct via6522 *via_create(void)
{
struct via6522 *v = malloc(sizeof(struct via6522));
if (v == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
memset(v, 0, sizeof(struct via6522));
return v;
}
uint8_t via_get_direction_a(struct via6522 *via)
{
return via->ddra;
}
uint8_t via_get_port_a(struct via6522 *via)
{
return via->ora & via->ddra;
}
void via_set_port_a(struct via6522 *via, uint8_t val)
{
via->ira = val;
}
uint8_t via_get_direction_b(struct via6522 *via)
{
return via->ddrb;
}
uint8_t via_get_port_b(struct via6522 *via)
{
return via->orb & via->ddrb;
}
void via_set_port_b(struct via6522 *via, uint8_t val)
{
via->irb = val;
}