-
Notifications
You must be signed in to change notification settings - Fork 0
/
cia.c
651 lines (602 loc) · 13.7 KB
/
cia.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
* UAE - The Un*x Amiga Emulator
*
* CIA chip support
*
* Copyright 1995 Bernd Schmidt, Alessandro Bissacco
*/
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include "config.h"
#include "amiga.h"
#include "options.h"
#include "events.h"
#include "memory.h"
#include "custom.h"
#include "cia.h"
#include "disk.h"
#include "xwin.h"
#include "keybuf.h"
#define DIV10 5 /* Yes, a bad identifier. */
static UBYTE ciaaicr,ciaaimask,ciabicr,ciabimask;
static UBYTE ciaacra,ciaacrb,ciabcra,ciabcrb;
static ULONG ciaata,ciaatb,ciabta,ciabtb;
static UWORD ciaala,ciaalb,ciabla,ciablb;
static ULONG ciaatod,ciabtod,ciaatol,ciabtol,ciaaalarm,ciabalarm;
static bool ciaatlatch,ciabtlatch;
static UBYTE ciaapra,ciaaprb,ciaadra,ciaadrb,ciaasdr;
static UBYTE ciabpra,ciabprb,ciabdra,ciabdrb,ciabsdr;
static int div10;
static int kbstate;
static bool kback;
static void setclr(UBYTE *p, UBYTE val)
{
if (val & 0x80) {
*p |= val & 0x7F;
} else {
*p &= ~val;
}
}
static void RethinkICRA(void)
{
if (ciaaimask & ciaaicr) {
ciaaicr |= 0x80;
custom_bank.wput(0xDFF09C,0x8008);
} else {
ciaaicr &= 0x7F;
custom_bank.wput(0xDFF09C,0x0008);
}
}
static void RethinkICRB(void)
{
if (ciabimask & ciabicr) {
ciabicr |= 0x80;
custom_bank.wput(0xDFF09C,0xA000);
} else {
ciabicr &= 0x7F;
custom_bank.wput(0xDFF09C,0x2000);
}
}
static int lastdiv10;
static void CIA_update(void)
{
unsigned long int ccount = cycles - eventtab[ev_cia].oldcycles + lastdiv10;
unsigned long int ciaclocks = ccount / DIV10;
int aovfla = 0, aovflb = 0, bovfla = 0, bovflb = 0;
lastdiv10 = div10;
div10 = ccount % DIV10;
/* CIA A timers */
if ((ciaacra & 0x21) == 0x01) {
assert((ciaata+1) >= ciaclocks);
if ((ciaata+1) == ciaclocks) {
aovfla = 1;
if ((ciaacrb & 0x61) == 0x41) {
if (ciaatb-- == 0) aovflb = 1;
}
}
ciaata -= ciaclocks;
}
if ((ciaacrb & 0x61) == 0x01) {
assert((ciaatb+1) >= ciaclocks);
if ((ciaatb+1) == ciaclocks) aovflb = 1;
ciaatb -= ciaclocks;
}
/* CIA B timers */
if ((ciabcra & 0x21) == 0x01) {
assert((ciabta+1) >= ciaclocks);
if ((ciabta+1) == ciaclocks) {
bovfla = 1;
if ((ciabcrb & 0x61) == 0x41) {
if (ciabtb-- == 0) bovflb = 1;
}
}
ciabta -= ciaclocks;
}
if ((ciabcrb & 0x61) == 0x01) {
assert ((ciabtb+1) >= ciaclocks);
if ((ciabtb+1) == ciaclocks) bovflb = 1;
ciabtb -= ciaclocks;
}
if (aovfla) {
ciaaicr |= 1; RethinkICRA();
ciaata = ciaala;
if (ciaacra & 0x8) ciaacra &= ~1;
}
if (aovflb) {
ciaaicr |= 2; RethinkICRA();
ciaatb = ciaalb;
if (ciaacrb & 0x8) ciaacrb &= ~1;
}
if (bovfla) {
ciabicr |= 1; RethinkICRB();
ciabta = ciabla;
if (ciabcra & 0x8) ciabcra &= ~1;
}
if (bovflb) {
ciabicr |= 2; RethinkICRA();
ciabtb = ciablb;
if (ciabcrb & 0x8) ciabcrb &= ~1;
}
}
static void CIA_calctimers(void)
{
int ciaatimea = -1, ciaatimeb = -1, ciabtimea = -1, ciabtimeb = -1;
eventtab[ev_cia].oldcycles = cycles;
if ((ciaacra & 0x21) == 0x01) {
ciaatimea = (DIV10-div10) + DIV10*ciaata;
}
if ((ciaacrb & 0x61) == 0x41) {
/* Timer B will not get any pulses if Timer A is off. */
if (ciaatimea >= 0) {
/* If Timer A is in one-shot mode, and Timer B needs more than
* one pulse, it will not underflow. */
if (ciaatb == 0 || (ciaacra & 0x8) == 0) {
/* Otherwise, we can determine the time of the underflow. */
ciaatimeb = ciaatimea + ciaala * DIV10 * ciaatb;
}
}
}
if ((ciaacrb & 0x61) == 0x01) {
ciaatimeb = (DIV10-div10) + DIV10*ciaatb;
}
if ((ciabcra & 0x21) == 0x01) {
ciabtimea = (DIV10-div10) + DIV10*ciabta;
}
if ((ciabcrb & 0x61) == 0x41) {
/* Timer B will not get any pulses if Timer A is off. */
if (ciabtimea >= 0) {
/* If Timer A is in one-shot mode, and Timer B needs more than
* one pulse, it will not underflow. */
if (ciabtb == 0 || (ciabcra & 0x8) == 0) {
/* Otherwise, we can determine the time of the underflow. */
ciabtimeb = ciabtimea + ciabla * DIV10 * ciabtb;
}
}
}
if ((ciabcrb & 0x61) == 0x01) {
ciabtimeb = (DIV10-div10) + DIV10*ciabtb;
}
eventtab[ev_cia].active = (ciaatimea != -1 || ciaatimeb != -1
|| ciabtimea != -1 || ciabtimeb != -1);
if (eventtab[ev_cia].active) {
unsigned long int ciatime = ~0L;
if (ciaatimea != -1) ciatime = ciaatimea;
if (ciaatimeb != -1 && ciaatimeb < ciatime) ciatime = ciaatimeb;
if (ciabtimea != -1 && ciabtimea < ciatime) ciatime = ciabtimea;
if (ciabtimeb != -1 && ciabtimeb < ciatime) ciatime = ciabtimeb;
eventtab[ev_cia].evtime = ciatime;
}
events_schedule();
}
void CIA_handler(void)
{
CIA_update();
CIA_calctimers();
}
void CIA_hsync_handler(void)
{
static int keytime = 0;
ciabtod++;
ciabtod &= 0xFFFFFF;
if (ciabtod == ciabalarm) {
ciabicr |= 4; RethinkICRB();
}
if (keys_available() && kback && (++keytime & 15) == 0) {
switch(kbstate) {
case 0:
ciaasdr = (BYTE)~0xFB; /* aaarghh... stupid compiler */
kbstate++;
break;
case 1:
kbstate++;
ciaasdr = (BYTE)~0xFD;
break;
case 2:
ciaasdr = ~get_next_key();
break;
}
ciaaicr |= 8; RethinkICRA();
}
}
void CIA_vsync_handler()
{
ciaatod++;
ciaatod &= 0xFFFFFF;
if (ciaatod == ciaaalarm) {
ciaaicr |= 4; RethinkICRA();
}
}
static UBYTE ReadCIAA(UWORD addr)
{
UBYTE tmp;
switch(addr & 0xf){
case 0:
tmp = (DISK_status() & 0x3C);
if (!buttonstate[0]) tmp |= 0x40;
if (!buttonstate[1]) tmp |= 0x80;
return tmp;
case 1:
return ciaaprb;
case 2:
return ciaadra;
case 3:
return ciaadrb;
case 4:
return ciaata & 0xff;
case 5:
return ciaata >> 8;
case 6:
return ciaatb & 0xff;
case 7:
return ciaatb >> 8;
case 8:
if (ciaatlatch) {
ciaatlatch = 0;
return ciaatol & 0xff;
} else return ciaatod & 0xff;
case 9:
if (ciaatlatch) return (ciaatol >> 8) & 0xff;
else return (ciaatod >> 8) & 0xff;
case 10:
ciaatlatch = 1; ciaatol = ciaatod; /* ??? only if not already latched? */
return (ciaatol >> 16) & 0xff;
case 12:
return ciaasdr;
case 13:
tmp = ciaaicr; ciaaicr = 0; RethinkICRA(); return tmp;
case 14:
return ciaacra;
case 15:
return ciaacrb;
}
return 0;
}
static UBYTE ReadCIAB(UWORD addr)
{
UBYTE tmp;
switch(addr & 0xf){
case 0:
return ciabpra;
case 1:
return ciabprb;
case 2:
return ciabdra;
case 3:
return ciabdrb;
case 4:
return ciabta & 0xff;
case 5:
return ciabta >> 8;
case 6:
return ciabtb & 0xff;
case 7:
return ciabtb >> 8;
case 8:
if (ciabtlatch) {
ciabtlatch = 0;
return ciabtol & 0xff;
} else return ciabtod & 0xff;
case 9:
if (ciabtlatch) return (ciabtol >> 8) & 0xff;
else return (ciabtod >> 8) & 0xff;
case 10:
ciabtlatch = 1; ciabtol = ciabtod;
return (ciabtol >> 16) & 0xff;
case 12:
return ciabsdr;
case 13:
tmp = ciabicr; ciabicr = 0; RethinkICRB();
if (!dskdmaen)
if (indexpulse == 0){
tmp |= 0x10;
DISK_Index();
indexpulse = 100; /* whatever */
} else {
indexpulse--;
}
return tmp;
case 14:
return ciabcra;
case 15:
return ciabcrb;
}
return 0;
}
static void WriteCIAA(UWORD addr,UBYTE val)
{
switch(addr & 0xf){
case 0:
ciaapra = (ciaapra & ~0x3) | (val & 0x3); LED(ciaapra & 0x2); break;
case 1:
ciaaprb = val; break;
case 2:
ciaadra = val; break;
case 3:
ciaadrb = val; break;
case 4:
CIA_update();
ciaala = (ciaala & 0xff00) | val;
CIA_calctimers();
break;
case 5:
CIA_update();
ciaala = (ciaala & 0xff) | (val << 8);
if ((ciaacra & 1) == 0) {
ciaata = ciaala;
}
if (ciaacra & 8) {
ciaata = ciaala;
ciaacra |= 1;
}/*??? load latch always? */
CIA_calctimers();
break;
case 6:
CIA_update();
ciaalb = (ciaalb & 0xff00) | val;
CIA_calctimers();
break;
case 7:
CIA_update();
ciaalb = (ciaalb & 0xff) | (val << 8);
if ((ciaacrb & 1) == 0) ciaatb = ciaalb;
if (ciaacrb & 8) { ciaatb = ciaalb; ciaacrb |= 1; }
CIA_calctimers();
break;
case 8:
if (ciaacrb & 0x80){
ciaaalarm = (ciaaalarm & ~0xff) | val;
} else {
ciaatod = (ciaatod & ~0xff) | val;
}
break;
case 9:
if (ciaacrb & 0x80){
ciaaalarm = (ciaaalarm & ~0xff00) | (val << 8);
} else {
ciaatod = (ciaatod & ~0xff00) | (val << 8);
}
break;
case 10:
if (ciaacrb & 0x80){
ciaaalarm = (ciaaalarm & ~0xff0000) | (val << 16);
} else {
ciaatod = (ciaatod & ~0xff0000) | (val << 16);
}
break;
case 12:
ciaasdr = val; break;
case 13:
setclr(&ciaaimask,val); break; /* ??? call RethinkICR() ? */
case 14:
CIA_update();
ciaacra = val;
if (ciaacra & 0x10){
ciaacra &= ~0x10;
ciaata = ciaala;
}
if (ciaacra & 0x40) {
kback = true;
}
CIA_calctimers();
break;
case 15:
CIA_update();
ciaacrb = val;
if (ciaacrb & 0x10){
ciaacrb &= ~0x10;
ciaatb = ciaalb;
}
CIA_calctimers();
break;
}
}
static void WriteCIAB(UWORD addr,UBYTE val)
{
switch(addr & 0xf){
case 0:
ciabpra = (ciabpra & ~0x3) | (val & 0x3); break;
case 1:
ciabprb = val; DISK_select(val); break;
case 2:
ciabdra = val; break;
case 3:
ciabdrb = val; break;
case 4:
CIA_update();
ciabla = (ciabla & 0xff00) | val;
CIA_calctimers();
break;
case 5:
CIA_update();
ciabla = (ciabla & 0xff) | (val << 8);
if ((ciabcra & 1) == 0) ciabta = ciabla;
if (ciabcra & 8) { ciabta = ciabla; ciabcra |= 1; }
CIA_calctimers();
break;
case 6:
CIA_update();
ciablb = (ciablb & 0xff00) | val;
CIA_calctimers();
break;
case 7:
CIA_update();
ciablb = (ciablb & 0xff) | (val << 8);
if ((ciabcrb & 1) == 0) ciabtb = ciablb;
if (ciabcrb & 8) { ciabtb = ciablb; ciabcrb |= 1; }
CIA_calctimers();
break;
case 8:
if (ciabcrb & 0x80){
ciabalarm = (ciabalarm & ~0xff) | val;
} else {
ciabtod = (ciabtod & ~0xff) | val;
}
break;
case 9:
if (ciabcrb & 0x80){
ciabalarm = (ciabalarm & ~0xff00) | (val << 8);
} else {
ciabtod = (ciabtod & ~0xff00) | (val << 8);
}
break;
case 10:
if (ciabcrb & 0x80){
ciabalarm = (ciabalarm & ~0xff0000) | (val << 16);
} else {
ciabtod = (ciabtod & ~0xff0000) | (val << 16);
}
break;
case 12:
ciabsdr = val;
break;
case 13:
setclr(&ciabimask,val);
break;
case 14:
CIA_update();
ciabcra = val;
if (ciabcra & 0x10){
ciabcra &= ~0x10;
ciabta = ciabla;
}
CIA_calctimers();
break;
case 15:
CIA_update();
ciabcrb = val;
if (ciabcrb & 0x10){
ciabcrb &= ~0x10;
ciabtb = ciablb;
}
CIA_calctimers();
break;
}
}
void CIA_reset(void)
{
kback = true;
kbstate = 0;
ciaatlatch = ciabtlatch = 0;
ciaatod = ciabtod = 0;
ciaaicr = ciabicr = ciaaimask = ciabimask = 0;
ciaacra = ciaacrb = ciabcra = ciabcrb = 0x4; /* outmode = toggle; */
div10 = 0;
lastdiv10 = 0;
CIA_calctimers();
}
void dumpcia(void)
{
printf("A: CRA: %02x, CRB: %02x, IMASK: %02x, TOD: %08lx %7s TA: %04lx, TB: %04lx\n",
(int)ciaacra, (int)ciaacrb, (int)ciaaimask, ciaatod,
ciaatlatch ? " latched" : "", ciaata, ciaatb);
printf("B: CRA: %02x, CRB: %02x, IMASK: %02x, TOD: %08lx %7s TA: %04lx, TB: %04lx\n",
(int)ciabcra, (int)ciabcrb, (int)ciabimask, ciabtod,
ciabtlatch ? " latched" : "", ciabta, ciabtb);
}
/* CIA memory access */
static ULONG cia_lget(CPTR);
static UWORD cia_wget(CPTR);
static UBYTE cia_bget(CPTR);
static void cia_lput(CPTR, ULONG);
static void cia_wput(CPTR, UWORD);
static void cia_bput(CPTR, UBYTE);
addrbank cia_bank = {
cia_lget, cia_wget, cia_bget,
cia_lput, cia_wput, cia_bput,
default_xlate, default_check
};
ULONG cia_lget(CPTR addr)
{
return cia_bget(addr+3);
}
UWORD cia_wget(CPTR addr)
{
return cia_bget(addr+1);
}
UBYTE cia_bget(CPTR addr)
{
#ifdef DUALCPU
customacc = true;
#endif
if ((addr & 0xF0FF) == 0xE001)
return ReadCIAA((addr & 0xF00) >> 8);
if ((addr & 0xF0FF) == 0xD000)
return ReadCIAB((addr & 0xF00) >> 8);
return 0;
}
void cia_lput(CPTR addr, ULONG value)
{
cia_bput(addr+3,value); /* FIXME ? */
}
void cia_wput(CPTR addr, UWORD value)
{
cia_bput(addr+1,value);
}
void cia_bput(CPTR addr, UBYTE value)
{
#ifdef DUALCPU
customacc = true;
#endif
if ((addr & 0xF0FF) == 0xE001)
WriteCIAA((addr & 0xF00) >> 8,value);
if ((addr & 0xF0FF) == 0xD000)
WriteCIAB((addr & 0xF00) >> 8,value);
}
/* battclock memory access */
static ULONG clock_lget(CPTR);
static UWORD clock_wget(CPTR);
static UBYTE clock_bget(CPTR);
static void clock_lput(CPTR, ULONG);
static void clock_wput(CPTR, UWORD);
static void clock_bput(CPTR, UBYTE);
addrbank clock_bank = {
clock_lget, clock_wget, clock_bget,
clock_lput, clock_wput, clock_bput,
default_xlate, default_check
};
ULONG clock_lget(CPTR addr)
{
return clock_bget(addr+3);
}
UWORD clock_wget(CPTR addr)
{
return clock_bget(addr+1);
}
UBYTE clock_bget(CPTR addr)
{
#ifdef DUALCPU
customacc = true;
#endif
time_t t=time(0);
struct tm *ct;
ct=localtime(&t);
switch (addr & 0x3f)
{
case 0x03: return ct->tm_sec % 10;
case 0x07: return ct->tm_sec / 10;
case 0x0b: return ct->tm_min % 10;
case 0x0f: return ct->tm_min / 10;
case 0x13: return ct->tm_hour % 10;
case 0x17: return ct->tm_hour / 10;
case 0x1b: return ct->tm_mday % 10;
case 0x1f: return ct->tm_mday / 10;
case 0x23: return (ct->tm_mon+1) % 10;
case 0x27: return (ct->tm_mon+1) / 10;
case 0x2b: return ct->tm_year % 10;
case 0x2f: return ct->tm_year / 10;
}
return 0;
}
void clock_lput(CPTR addr, ULONG value)
{
/* No way */
}
void clock_wput(CPTR addr, UWORD value)
{
/* No way */
}
void clock_bput(CPTR addr, UBYTE value)
{
/* No way */
}