-
Notifications
You must be signed in to change notification settings - Fork 42
/
duart.c
385 lines (359 loc) · 7.21 KB
/
duart.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "duart.h"
/* 68681 DUART */
struct duart_port {
uint8_t mr1;
uint8_t mr2;
uint8_t sr;
uint8_t csr;
uint8_t rx;
uint8_t mrp;
uint8_t txdis;
uint8_t rxdis;
};
struct duart {
struct duart_port port[2];
uint8_t ipcr;
uint8_t isr;
int32_t ct; /* We overflow this temporarily */
uint16_t ctr;
uint8_t ctstop;
uint8_t imr;
uint8_t ivr;
uint8_t opcr;
uint8_t acr;
uint8_t irq;
int input; /* Which port if any is console */
int trace; /* Debug trace */
};
static void duart_irq_calc(struct duart *d)
{
d->irq = d->isr & d->imr;
if (d->trace) {
if (d->irq)
fprintf(stderr, "DUART IRQ asserted.\n");
else
fprintf(stderr, "DUART IRQ released.\n");
}
recalc_interrupts();
}
static void duart_irq_raise(struct duart *d, uint8_t m)
{
if (!(d->isr & m)) {
d->isr |= m;
if (d->trace)
fprintf(stderr, "DUART IRQ raised %02X\n", m);
duart_irq_calc(d);
}
}
static void duart_irq_lower(struct duart *d, uint8_t m)
{
if (d->isr & m) {
d->isr &= ~m;
if (d->trace)
fprintf(stderr, "DUART IRQ lowered %02X\n", m);
duart_irq_calc(d);
}
}
static uint8_t duart_input(struct duart *d, int port)
{
if (d->port[port].sr & 0x01) {
d->port[port].sr ^= 0x01;
duart_irq_lower(d, 2 << (4 * port));
}
return d->port[port].rx;
}
static void duart_output(struct duart *d, int port, int value)
{
duart_irq_lower(d, 1 << (4 * port));
if (d->port[port].txdis)
return;
if (d->port[port].sr & 0x04) {
if (d->input - 1 == port) {
uint8_t v = value & 0xFF;
write(1, &v, 1);
}
d->port[port].sr &= 0xF3;
duart_irq_calc(d);
}
}
static void duart_command(struct duart *d, int port, int value)
{
switch ((value & 0xE0) >> 4) {
case 0:
break;
case 1:
d->port[port].mrp = 0;
break;
case 2:
/* Reset RX A */
d->port[port].rxdis = 1;
break;
case 3:
break;
case 4:
d->port[port].sr &= 0x0F;
break;
case 5:
duart_irq_lower(d, 4 << (4 * port));
duart_irq_calc(d);
break;
case 6:
break; /* Literally start break */
case 7:
break; /* Stop break */
}
if (value & 1)
d->port[port].rxdis = 0;
if (value & 2)
d->port[port].rxdis = 1;
if (value & 4)
d->port[port].txdis = 0;
if (value & 8)
d->port[port].txdis = 1;
}
/* Simulate 1/100th second of action */
static void duart_count(struct duart *d, int n)
{
/* We are clocked at ??? so divide as needed */
uint16_t clock = 184; /* 1843200 so not entirely accurate
FIXME: we could track partial clocks */
if (n == 16)
clock /= 16; /* Again needs accuracy sorting */
/* Counter mode can be stopped */
if (!(d->acr & 0x40))
if (d->ctstop)
return;
d->ct -= clock;
if (d->ct < 0) {
/* Our couunt overran so we compute the remainder */
if (d->ctr) {
d->ct %= d->ctr; /* Negative clocks left */
d->ct += d->ctr; /* Plus next cycle */
} else
d->ct = 0;
/* And raise the event */
duart_irq_raise(d, 0x08);
}
}
void duart_tick(struct duart *d)
{
uint8_t r = check_chario();
if (r & 1) {
if (d->input == 1 && d->port[0].rxdis == 0) {
d->port[0].rx = next_char();
d->port[0].sr |= 0x01;
duart_irq_raise(d, 0x02);
} else if (d->input == 2 && d->port[1].rxdis == 0) {
d->port[1].rx = next_char();
d->port[1].sr |= 0x01;
duart_irq_raise(d, 0x20);
}
}
if (r & 2) {
if (!d->port[0].txdis && !(d->port[0].sr & 0x04))
duart_irq_raise(d, 0x01);
d->port[0].sr |= 0x0C;
if (!d->port[1].txdis && !(d->port[1].sr & 0x04))
duart_irq_raise(d, 0x10);
d->port[1].sr |= 0x0C;
}
switch ((d->acr & 0x70) >> 4) {
/* Clock and timer modes */
case 0: /* Counting IP2 */
break;
case 1: /* Counting TxCA */
break;
case 2: /* Counting TxCB */
break;
case 3: /* Counting EXT/x1 clock / 16 */
duart_count(d, 16);
break;
case 4: /* Timer on IP2 */
break;
case 5: /* Timer on IP2/16 */
break;
case 6: /* Timer on X1/CLK */
duart_count(d, 1);
break;
case 7: /* Timer on X1/CLK / 16 */
duart_count(d, 16);
break;
}
}
void duart_reset(struct duart *d)
{
d->ctr = 0xFFFF;
d->ct = 0x0000;
d->acr = 0xFF;
d->isr = 0;
d->imr = 0;
d->ivr = 0xF;
d->port[0].mrp = 0;
d->port[0].sr = 0x00;
d->port[1].mrp = 0;
d->port[1].sr = 0x00;
}
uint8_t do_duart_read(struct duart *d, uint16_t address)
{
switch (address & 0x0F) {
case 0x00: /* MR1A/MR2A */
if (d->port[0].mrp)
return d->port[0].mr2;
d->port[0].mrp = 1;
return d->port[0].mr1;
case 0x01: /* SRA */
return d->port[0].sr;
case 0x02: /* BRG test */
case 0x03: /* RHRA */
return duart_input(d, 0);
case 0x04: /* IPCR */
return d->ipcr;
case 0x05: /* ISR */
return d->isr;
case 0x06: /* CTU */
return d->ct >> 8;
case 0x07: /* CTL */
return d->ct & 0xFF;
case 0x08: /* MR1B/MR2B */
if (d->port[1].mrp)
return d->port[1].mr2;
d->port[1].mrp = 1;
return d->port[1].mr1;
case 0x09: /* SRB */
return d->port[1].sr;
case 0x0A: /* 1x/16x Test */
case 0x0B: /* RHRB */
return duart_input(d, 1);
case 0x0C: /* IVR */
return d->ivr;
case 0x0D: /* IP */
return 0xff; /* d->ip; */
case 0x0E: /* START */
d->ct = d->ctr;
d->ctstop = 0;
return 0xFF;
case 0x0F: /* STOP */
d->ctstop = 1;
duart_irq_lower(d, 0x08);
return 0xFF;
}
return 0xFF;
}
uint8_t duart_read(struct duart *d, uint16_t address)
{
uint8_t value = do_duart_read(d, address);
if (d->trace)
fprintf(stderr, "duart: read reg %02X -> %02X\n",
address >> 1, value);
return value;
}
void duart_write(struct duart *d, uint16_t address, uint8_t value)
{
int bgrc = 0;
value &= 0xFF;
if (d->trace)
fprintf(stderr, "duart: write reg %02X <- %02X\n",
address >> 1, value);
switch (address & 0x0F) {
case 0x00:
if (d->port[0].mrp)
d->port[0].mr2 = value;
else
d->port[0].mr1 = value;
break;
case 0x01:
d->port[0].csr = value;
bgrc = 1;
break;
case 0x02:
duart_command(d, 0, value);
break;
case 0x03:
duart_output(d, 0, value);
break;
case 0x04:
d->acr = value;
duart_irq_calc(d);
bgrc = 1;
break;
case 0x05:
d->imr = value;
duart_irq_calc(d);
break;
case 0x06:
d->ctr &= 0xFF;
d->ctr |= value << 8;
break;
case 0x07:
d->ctr &= 0xFF00;
d->ctr |= value;
break;
case 0x08:
if (d->port[1].mrp)
d->port[1].mr2 = value;
else
d->port[1].mr1 = value;
break;
case 0x09:
d->port[1].csr = value;
break;
case 0x0A:
duart_command(d, 1, value);
break;
case 0x0B:
duart_output(d, 1, value);
break;
case 0x0C:
d->ivr = value;
break;
case 0x0D:
d->opcr = value;
break;
case 0x0E:
d->opcr |= value;
break;
case 0x0F:
d->opcr &= ~value;
break;
}
if (bgrc && d->trace) {
fprintf(stderr, "BGR %d\n", d->acr >> 7);
fprintf(stderr, "CSR %d\n", d->port[0].csr >> 4);
}
}
void duart_set_input(struct duart *duart, int port)
{
duart->input = port;
}
void duart_trace(struct duart *duart, int onoff)
{
duart->trace = onoff;
}
uint8_t duart_irq_pending(struct duart *d)
{
return d->irq;
}
struct duart *duart_create(void)
{
struct duart *d = malloc(sizeof(struct duart));
if (d == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
memset(d, 0, sizeof(*d));
duart_reset(d);
return d;
}
void duart_free(struct duart *d)
{
free(d);
}
uint8_t duart_vector(struct duart *d)
{
return d->ivr;
}