-
Notifications
You must be signed in to change notification settings - Fork 90
/
cia.cpp
3122 lines (2822 loc) · 68.1 KB
/
cia.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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* UAE - The Un*x Amiga Emulator
*
* CIA chip support
*
* Copyright 1995 Bernd Schmidt, Alessandro Bissacco
* Copyright 1996, 1997 Stefan Reinauer, Christian Schmitt
* Copyright 2022 Toni Wilen
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <assert.h>
#include "options.h"
#include "events.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "cia.h"
#ifdef SERIAL_PORT
#include "serial.h"
#endif
#include "disk.h"
#include "xwin.h"
#include "keybuf.h"
#include "gui.h"
#include "savestate.h"
#include "inputdevice.h"
#include "zfile.h"
#include "ar.h"
#include "parallel.h"
#include "akiko.h"
#include "cdtv.h"
#include "debug.h"
#include "arcadia.h"
#include "audio.h"
#include "keyboard.h"
#include "uae.h"
#include "amax.h"
#include "sampler.h"
#include "dongle.h"
#include "inputrecord.h"
#include "uae/ppc.h"
#include "rommgr.h"
#include "scsi.h"
#include "rtc.h"
#include "devices.h"
#define CIAA_DEBUG_R 0
#define CIAA_DEBUG_W 0
#define CIAA_DEBUG_IRQ 0
#define CIAB_DEBUG_R 0
#define CIAB_DEBUG_W 0
#define CIAB_DEBUG_IRQ 0
#define DONGLE_DEBUG 0
#define KB_DEBUG 0
#define CLOCK_DEBUG 0
#define CIA_EVERY_CYCLE_DEBUG 0
#define TOD_HACK
#define CIA_IRQ_PROCESS_DELAY 0
#define CR_START 1
#define CR_PBON 2
#define CR_OUTMODE 4
#define CR_RUNMODE 8
#define CR_LOAD 0x10
#define CR_INMODE 0x20
#define CR_INMODE1 0x40
#define CR_SPMODE 0x40
#define CR_ALARM 0x80
#define ICR_A 1
#define ICR_B 2
#define ICR_ALARM 4
#define ICR_SP 8
#define ICR_FLAG 0x10
#define ICR_MASK 0x1f
#define CIA_PIPE_ALL_BITS 2
#define CIA_PIPE_ALL_MASK ((1 << CIA_PIPE_ALL_BITS) - 1)
#define CIA_PIPE_INPUT 2
#define CIA_PIPE_CLR1 1
#define CIA_PIPE_CLR2 3
#define CIA_PIPE_OUTPUT 1
/* Akiko internal CIA differences:
- BFE101 and BFD100: reads 3F if data direction is in.
*/
#define E_CLOCK_SYNC_N 2
#define E_CLOCK_START_N 4
#define E_CLOCK_END_N 6
#define E_CLOCK_TOD_N -2
#define E_CLOCK_SYNC_N2 4
#define E_CLOCK_START_N2 6
#define E_CLOCK_END_N2 6
#define E_CLOCK_TOD_N2 0
#define E_CLOCK_SYNC_X 4
#define E_CLOCK_START_X 2
#define E_CLOCK_END_X 6
#define E_CLOCK_TOD_X 0
static int e_clock_sync = E_CLOCK_SYNC_N;
static int e_clock_start = E_CLOCK_START_N;
static int e_clock_end = E_CLOCK_END_N;
static int e_clock_tod = E_CLOCK_TOD_N;
#define E_CLOCK_LENGTH 10
#define E_CYCLE_UNIT (CYCLE_UNIT / 2)
#define DIV10 (E_CLOCK_LENGTH * E_CYCLE_UNIT) /* Yes, a bad identifier. */
struct CIATimer
{
uae_u32 timer;
uae_u32 latch;
uae_u32 passed;
uae_u16 inputpipe;
uae_u32 loaddelay;
uae_u8 preovfl;
uae_u8 cr;
};
struct CIA
{
uae_u8 pra, prb;
uae_u8 dra, drb;
struct CIATimer t[2];
uae_u32 tod;
uae_u32 tol;
uae_u32 alarm;
uae_u32 tlatch;
uae_u8 todon;
int tod_event_state;
int tod_offset;
uae_u8 icr1, icr2;
bool icr_change;
uae_u8 imask;
uae_u8 sdr;
uae_u8 sdr_buf;
uae_u8 sdr_load;
uae_u8 sdr_cnt;
};
static struct CIA cia[2];
static bool oldovl;
static int led;
static int led_old_brightness;
static evt_t led_cycle;
static evt_t cia_now_evt;
static int led_cycles_on, led_cycles_off;
static int kbstate, kblostsynccnt;
static evt_t kbhandshakestart;
static uae_u8 kbcode;
static uae_u8 serbits;
static int warned = 100;
static struct rtc_msm_data rtc_msm;
static struct rtc_ricoh_data rtc_ricoh;
static int internaleclockphase;
static bool cia_cycle_accurate;
static bool acc_mode(void)
{
return cia_cycle_accurate;
}
int blop, blop2;
void cia_adjust_eclock_phase(int diff)
{
internaleclockphase += diff;
if (internaleclockphase < 0) {
internaleclockphase += ((-internaleclockphase) / 20) * 20;
internaleclockphase += 20;
}
internaleclockphase %= 20;
//write_log("CIA E-clock phase %d\n", internaleclockphase);
}
void cia_set_eclockphase(void)
{
if (currprefs.cs_eclocksync == 3) {
e_clock_sync = E_CLOCK_SYNC_X;
e_clock_start = E_CLOCK_START_X;
e_clock_end = E_CLOCK_END_X;
e_clock_tod = E_CLOCK_TOD_X;
} else if (currprefs.cs_eclocksync == 2) {
e_clock_sync = E_CLOCK_SYNC_N2;
e_clock_start = E_CLOCK_START_N2;
e_clock_end = E_CLOCK_END_N2;
e_clock_tod = E_CLOCK_TOD_N2;
} else {
e_clock_sync = E_CLOCK_SYNC_N;
e_clock_start = E_CLOCK_START_N;
e_clock_end = E_CLOCK_END_N;
e_clock_tod = E_CLOCK_TOD_N;
}
}
static evt_t get_e_cycles(void)
{
// temporary e-clock phase shortcut
if (blop) {
cia_adjust_eclock_phase(1);
blop = 0;
}
if (blop2) {
if (currprefs.cs_eclocksync == 0) {
currprefs.cs_eclocksync = 1;
}
currprefs.cs_eclocksync += 1;
if (currprefs.cs_eclocksync >= 4) {
currprefs.cs_eclocksync = 1;
}
changed_prefs.cs_eclocksync = currprefs.cs_eclocksync;
cia_set_eclockphase();
write_log("CIA elock timing mode %d\n", currprefs.cs_eclocksync);
blop2 = 0;
}
evt_t c = get_cycles();
c += currprefs.cs_eclockphase * E_CYCLE_UNIT;
c += internaleclockphase * 2 * E_CYCLE_UNIT;
return c;
}
static void setclr(uae_u8 *p, uae_u8 val)
{
if (val & 0x80) {
*p |= val & 0x7F;
} else {
*p &= ~val;
}
}
#if CIA_IRQ_PROCESS_DELAY
/* delay interrupt after current CIA register access if
* interrupt would have triggered mid access
*/
static int cia_interrupt_disabled;
static int cia_interrupt_delay;
#endif
static void ICRIRQ(uae_u32 data)
{
safe_interrupt_set(IRQ_SOURCE_CIA, 0, (data & 0x2000) != 0);
}
static void ICR(uae_u32 num)
{
struct CIA *c = &cia[num];
#if CIA_IRQ_PROCESS_DELAY
if (currprefs.cpu_memory_cycle_exact && !(c->icr & 0x20) && (cia_interrupt_disabled & (1 << num))) {
c->cia_interrupt_delay |= 1 << num;
#if CIAB_DEBUG_IRQ
write_log(_T("cia%c interrupt disabled ICR=%02X PC=%x\n"), num ? 'b' : 'a', c->icr, M68K_GETPC);
#endif
return;
}
#endif
c->icr1 |= 0x20;
if (num && currprefs.cs_compatible != CP_VELVET) {
ICRIRQ(0x2000);
} else {
ICRIRQ(0x0008);
}
}
static void RethinkICR(int num)
{
struct CIA *c = &cia[num];
if (c->icr1 & c->imask & ICR_MASK) {
#if CIAA_DEBUG_IRQ
write_log(_T("CIA%c IRQ %02X %02X\n"), num ? 'B' : 'A', c->icr1, c->icr2);
#endif
if (!(c->icr1 & 0x80)) {
c->icr1 |= 0x80 | 0x40;
#ifdef DEBUGGER
if (debug_dma) {
record_dma_event(num ? DMA_EVENT_CIAB_IRQ : DMA_EVENT_CIAA_IRQ, current_hpos(), vpos);
}
#endif
ICR(num);
}
}
}
void rethink_cias(void)
{
if (cia[0].icr1 & 0x40) {
ICRIRQ(0x0008);
}
if (cia[1].icr1 & 0x40) {
if (currprefs.cs_compatible == CP_VELVET) {
ICRIRQ(0x0008);
} else {
ICRIRQ(0x2000);
}
}
}
static uae_u16 bitstodelay(uae_u16 v)
{
switch (v)
{
case 0:
return CIA_PIPE_ALL_BITS - 0;
case 1:
case 2:
#if CIA_PIPE_ALL_BITS > 2
case 4:
#endif
return CIA_PIPE_ALL_BITS - 1;
case 3:
#if CIA_PIPE_ALL_BITS > 2
case 5:
case 6:
#endif
return CIA_PIPE_ALL_BITS - 2;
#if CIA_PIPE_ALL_BITS > 2
case 7:
return CIA_PIPE_ALL_BITS - 3;
#endif
default:
abort();
break;
}
}
/* Figure out how many CIA timer cycles have passed for each timer since the
last call of CIA_calctimers. */
static void compute_passed_time_cia(int num, uae_u32 ciaclocks)
{
struct CIA *c = &cia[num];
c->t[0].passed = 0;
c->t[1].passed = 0;
if ((c->t[0].cr & (CR_INMODE | CR_START)) == CR_START) {
uae_u32 cc = ciaclocks;
int pipe = bitstodelay(c->t[0].inputpipe);
if (cc > pipe) {
cc -= pipe;
} else {
cc = 0;
}
c->t[0].passed = cc;
assert(cc < 65536);
}
if ((c->t[1].cr & (CR_INMODE | CR_INMODE1 | CR_START)) == CR_START) {
uae_u32 cc = ciaclocks;
int pipe = bitstodelay(c->t[1].inputpipe);
if (cc > pipe) {
cc -= pipe;
} else {
cc = 0;
}
c->t[1].passed = cc;
assert(cc < 65536);
}
}
static void compute_passed_time(void)
{
evt_t ccount = get_cycles() - eventtab[ev_cia].oldcycles;
if (ccount > MAXINT) {
ccount = MAXINT;
}
uae_u32 ciaclocks = (uae_u32)ccount / DIV10;
compute_passed_time_cia(0, ciaclocks);
compute_passed_time_cia(1, ciaclocks);
}
static void timer_reset(struct CIATimer *t)
{
t->timer = t->latch;
if (acc_mode()) {
if (t->cr & CR_RUNMODE) {
t->inputpipe &= ~CIA_PIPE_CLR1;
} else {
t->inputpipe &= ~CIA_PIPE_CLR2;
}
}
}
static uae_u8 cia_inmode_cnt(int num)
{
struct CIA *c = &cia[num];
uae_u8 icr = 0;
bool decb = false;
// A INMODE=1 (count CNT pulses)
if ((c->t[0].cr & (CR_INMODE | CR_START)) == (CR_INMODE | CR_START)) {
if (c->t[0].timer == 0) {
icr |= ICR_A;
timer_reset(&c->t[0]);
if (c->t[0].cr & CR_RUNMODE) {
c->t[0].cr &= ~CR_START;
}
// B INMODE = 1x (count A undeflows)
if ((c->t[1].cr & (CR_INMODE1 | CR_START)) == (CR_INMODE1 | CR_START)) {
decb = true;
}
} else {
c->t[0].timer--;
}
}
// B INMODE=01 (count CNT pulses)
if ((c->t[1].cr & (CR_INMODE1 | CR_INMODE | CR_START)) == (CR_INMODE | CR_START)) {
decb = 1;
}
if (decb) {
if (c->t[1].timer == 0) {
icr |= ICR_B;
timer_reset(&c->t[1]);
if (c->t[1].cr & CR_RUNMODE) {
c->t[1].cr &= ~CR_START;
}
} else {
c->t[1].timer--;
}
}
return icr;
}
static int process_pipe(struct CIATimer *t, int cc, uae_u8 crmask, int *ovfl, int loadednow)
{
int ccout = cc;
if (cc == 1 && acc_mode()) {
int out = t->inputpipe & CIA_PIPE_OUTPUT;
t->inputpipe >>= 1;
if ((t->cr & crmask) == CR_START) {
t->inputpipe |= CIA_PIPE_INPUT;
}
// interrupt 1 cycle early if timer is already zero
if (t->timer == 0 && t->latch == 0 && (t->inputpipe & CIA_PIPE_OUTPUT)) {
*ovfl = loadednow ? 1 : 2;
}
return out;
}
while (t->inputpipe != CIA_PIPE_ALL_MASK && cc > 0) {
if (!(t->inputpipe & CIA_PIPE_OUTPUT)) {
ccout--;
}
t->inputpipe >>= 1;
if ((t->cr & crmask) == CR_START) {
t->inputpipe |= CIA_PIPE_INPUT;
}
cc--;
}
return ccout;
}
/* Called to advance all CIA timers to the current time. This expects that
one of the timer values will be modified, and CIA_calctimers will be called
in the same cycle. */
static void CIA_update_check(void)
{
evt_t ccount = get_cycles() - eventtab[ev_cia].oldcycles;
if (ccount > MAXINT) {
ccount = MAXINT;
}
int ciaclocks = (uae_u32)(ccount / DIV10);
if (!ciaclocks) {
return;
}
uae_u8 icr = 0;
for (int num = 0; num < 2; num++) {
struct CIA *c = &cia[num];
int ovfl[2], sp;
bool loaded[2], loaded2[2], loaded3[3];
c->icr1 |= c->icr2;
c->icr2 = 0;
c->icr_change = false;
ovfl[0] = 0;
ovfl[1] = 0;
sp = 0;
for (int tn = 0; tn < 2; tn++) {
struct CIATimer *t = &c->t[tn];
loaded[tn] = false;
loaded2[tn] = false;
loaded3[tn] = false;
// CIA special cases
if (t->loaddelay) {
if (ciaclocks > 1) {
abort();
}
if (t->loaddelay & 0x00000001) {
t->timer = t->latch;
t->inputpipe &= ~CIA_PIPE_CLR1;
}
// timer=0 special cases. TODO: better way to do this..
// delayed timer stop and interrupt (timer=0 condition)
if ((t->loaddelay & 0x00010000)) {
t->cr &= ~CR_START;
ovfl[tn] = 2;
}
// Do not set START=0 until timer has started (timer==0 special case)
if ((t->loaddelay & 0x00000100) && t->timer == 0) {
loaded2[tn] = true;
}
if ((t->loaddelay & 0x01000000)) {
loaded[tn] = true;
}
if ((t->loaddelay & 0x10000000)) {
loaded3[tn] = true;
}
t->loaddelay >>= 1;
t->loaddelay &= 0x77777777;
}
}
// Timer A
int cc = 0;
if ((c->t[0].cr & (CR_INMODE | CR_START)) == CR_START || c->t[0].inputpipe) {
cc = process_pipe(&c->t[0], ciaclocks, CR_INMODE | CR_START, &ovfl[0], loaded3[0]);
}
if (cc > 0) {
c->t[0].timer -= cc;
if (c->t[0].timer == 0) {
// SP in output mode (data sent can be ignored if CIA-A)
if ((c->t[0].cr & (CR_SPMODE | CR_RUNMODE)) == CR_SPMODE && c->sdr_cnt > 0) {
c->sdr_cnt--;
if (c->sdr_cnt == 0) {
sp = 1;
if (c->sdr_load) {
c->sdr_load = 0;
c->sdr_buf = c->sdr;
c->sdr_cnt = 8 * 2;
}
}
}
ovfl[0] = 2;
}
}
assert(c->t[0].timer < 0x10000);
// Timer B
cc = 0;
if ((c->t[1].cr & (CR_INMODE | CR_INMODE1 | CR_START)) == CR_START || c->t[1].inputpipe) {
cc = process_pipe(&c->t[1], ciaclocks, CR_INMODE | CR_INMODE1 | CR_START, &ovfl[1], loaded3[1]);
}
if (cc > 0) {
if ((c->t[1].timer == 0 && (c->t[1].cr & (CR_INMODE | CR_INMODE1)))) {
ovfl[1] = 2;
} else {
c->t[1].timer -= cc;
if ((c->t[1].timer == 0 && !(c->t[1].cr & (CR_INMODE | CR_INMODE1)))) {
ovfl[1] = 2;
}
}
}
assert(c->t[1].timer < 0x10000);
// B INMODE=10 or 11 (B counting A underflows)
if (ovfl[0] && ((c->t[1].cr & (CR_INMODE | CR_INMODE1 | CR_START)) == (CR_INMODE1 | CR_START) || (c->t[1].cr & (CR_INMODE | CR_INMODE1 | CR_START)) == (CR_INMODE | CR_INMODE1 | CR_START))) {
c->t[1].inputpipe |= CIA_PIPE_INPUT;
}
for (int tn = 0; tn < 2; tn++) {
struct CIATimer *t = &c->t[tn];
if (ovfl[tn] || t->preovfl) {
if (ovfl[tn]) {
if (ovfl[tn] > 1) {
c->icr2 |= tn ? ICR_B : ICR_A;
icr |= 1 << num;
}
t->timer = t->latch;
}
if (!loaded[tn]) {
if (t->cr & CR_RUNMODE) {
if (loaded2[tn]) {
t->loaddelay |= 0x00010000;
} else {
t->cr &= ~CR_START;
}
if (!acc_mode()) {
t->inputpipe = 0;
}
if (acc_mode()) {
t->inputpipe &= ~CIA_PIPE_CLR2;
}
} else {
if (acc_mode()) {
t->inputpipe &= ~CIA_PIPE_CLR1;
}
}
}
t->preovfl = false;
}
}
if (sp) {
c->icr2 |= ICR_SP;
icr |= 1 << num;
}
if (!acc_mode()) {
c->icr1 |= c->icr2;
c->icr2 = 0;
} else {
if (icr) {
c->icr_change = true;
}
}
}
}
static void CIA_check_ICR(void)
{
if (cia[0].icr1 & ICR_MASK) {
RethinkICR(0);
}
if (cia[1].icr1 & ICR_MASK) {
RethinkICR(1);
}
}
static void CIA_update(void)
{
CIA_update_check();
CIA_check_ICR();
}
/* Call this only after CIA_update has been called in the same cycle. */
static void CIA_calctimers(void)
{
uae_s32 timevals[4];
timevals[0] = -1;
timevals[1] = -1;
timevals[2] = -1;
timevals[3] = -1;
eventtab[ev_cia].oldcycles = get_cycles();
for (int num = 0; num < 2; num++) {
struct CIA *c = &cia[num];
int idx = num * 2;
bool counting[2] = { false, false };
if ((c->t[0].cr & (CR_INMODE | CR_START)) == CR_START) {
int pipe = bitstodelay(c->t[0].inputpipe);
timevals[idx + 0] = DIV10 * (c->t[0].timer + pipe);
if (!timevals[idx + 0]) {
timevals[idx + 0] = DIV10;
}
counting[0] = true;
}
if ((c->t[1].cr & (CR_INMODE | CR_INMODE1 | CR_START)) == CR_START) {
int pipe = bitstodelay(c->t[1].inputpipe);
timevals[idx + 1] = DIV10 * (c->t[1].timer + pipe);
if (!timevals[idx + 1]) {
timevals[idx + 1] = DIV10;
}
counting[1] = true;
}
for (int tn = 0; tn < 2; tn++) {
struct CIATimer *t = &c->t[tn];
bool timerspecial = t->loaddelay != 0;
int tnidx = idx + tn;
if (t->cr & CR_START) {
if (t->inputpipe != CIA_PIPE_ALL_MASK) {
if (counting[tn] || t->inputpipe != 0) {
timerspecial = true;
}
}
} else {
if (t->inputpipe != 0) {
timerspecial = true;
}
}
if (timerspecial && (timevals[tnidx] < 0 || timevals[tnidx] > DIV10)) {
timevals[tnidx] = DIV10;
}
}
if (c->icr_change && (timevals[idx] < 0 || timevals[idx] > DIV10)) {
timevals[idx] = DIV10;
}
#if CIA_EVERY_CYCLE_DEBUG
timevals[idx] = DIV10;
#endif
}
uae_s32 ciatime = INT_MAX;
if (timevals[0] >= 0)
ciatime = timevals[0];
if (timevals[1] >= 0 && timevals[1] < ciatime)
ciatime = timevals[1];
if (timevals[2] >= 0 && timevals[2] < ciatime)
ciatime = timevals[2];
if (timevals[3] >= 0 && timevals[3] < ciatime)
ciatime = timevals[3];
if (ciatime < INT_MAX) {
eventtab[ev_cia].evtime = get_cycles() + ciatime;
eventtab[ev_cia].active = true;
} else {
eventtab[ev_cia].active = false;
}
events_schedule();
}
void CIA_handler(void)
{
CIA_update();
CIA_calctimers();
}
static int get_cia_sync_cycles(int *syncdelay)
{
evt_t c = get_e_cycles();
int div10 = c % DIV10;
int add = 0;
int synccycle = e_clock_sync * E_CYCLE_UNIT;
if (div10 < synccycle) {
add += synccycle - div10;
} else if (div10 > synccycle) {
add += DIV10 - div10;
add += synccycle;
}
*syncdelay = add;
// 4 first cycles of E-clock
add = e_clock_start * E_CYCLE_UNIT;
return add;
}
void event_CIA_synced_interrupt(uae_u32 v)
{
CIA_update();
CIA_calctimers();
}
static void CIA_sync_interrupt(int num, uae_u8 icr)
{
struct CIA *c = &cia[num];
if (acc_mode()) {
if (!(icr & c->imask)) {
c->icr1 |= icr;
return;
}
c->icr2 |= icr;
if ((c->icr1 & ICR_MASK) == (c->icr2 & ICR_MASK)) {
return;
}
int syncdelay = 0;
int delay = get_cia_sync_cycles(&syncdelay);
delay += syncdelay;
event2_newevent_xx(-1, DIV10 + delay, num, event_CIA_synced_interrupt);
} else {
c->icr1 |= icr;
CIA_check_ICR();
}
}
void cia_diskindex(void)
{
CIA_sync_interrupt(1, ICR_FLAG);
}
void cia_parallelack(void)
{
CIA_sync_interrupt(0, ICR_FLAG);
}
static bool checkalarm(uae_u32 tod, uae_u32 alarm, bool inc)
{
if (tod == alarm)
return true;
if (!currprefs.cs_ciatodbug)
return false;
if (!inc)
return false;
/* emulate buggy TODMED counter.
* it counts: .. 29 2A 2B 2C 2D 2E 2F 20 30 31 32 ..
* (2F->20->30 only takes couple of cycles but it will trigger alarm..
*/
if (tod & 0x000fff)
return false;
if (((tod - 1) & 0xfff000) == alarm)
return true;
return false;
}
static bool cia_checkalarm(bool inc, bool irq, int num)
{
struct CIA *c = &cia[num];
#if 0
// hack: do not trigger alarm interrupt if KS code and both
// tod and alarm == 0. This incorrectly triggers on non-cycle exact
// modes. Real hardware value written to ciabtod by KS is always
// at least 1 or larger due to bus cycle delays when reading
// old value.
if (num) {
if (!currprefs.cpu_compatible && (munge24(m68k_getpc()) & 0xFFF80000) != 0xF80000) {
if (c->tod == 0 && c->alarm == 0)
return false;
}
}
#endif
if (checkalarm(c->tod, c->alarm, inc)) {
#if CIAB_DEBUG_IRQ
write_log(_T("CIAB tod %08x %08x\n"), c->tod, c->alarm);
#endif
if (irq) {
CIA_sync_interrupt(num, ICR_ALARM);
}
return true;
}
return false;
}
#ifdef TOD_HACK
static uae_u64 tod_hack_tv, tod_hack_tod, tod_hack_tod_last;
static int tod_hack_enabled;
static int tod_hack_delay;
static int tod_diff_cnt;
#define TOD_HACK_DELAY 50
#define TOD_HACK_TIME 312 * 50 * 10
static void tod_hack_reset(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
tod_hack_tv = (uae_u64)tv.tv_sec * 1000000 + tv.tv_usec;
tod_hack_tod = cia[0].tod;
tod_hack_tod_last = tod_hack_tod;
tod_diff_cnt = 0;
}
#endif
static int heartbeat_cnt;
void cia_heartbeat(void)
{
heartbeat_cnt = 10;
}
static void do_tod_hack(bool dotod)
{
struct timeval tv;
static int oldrate;
uae_u64 t;
int rate;
int docount = 0;
if (tod_hack_enabled == 0)
return;
if (!heartbeat_cnt) {
if (tod_hack_enabled > 0)
tod_hack_enabled = -1;
return;
}
if (tod_hack_enabled < 0) {
tod_hack_enabled = TOD_HACK_TIME;
return;
}
if (tod_hack_enabled > 1) {
tod_hack_enabled--;
if (tod_hack_enabled == 1) {
//write_log(_T("TOD HACK enabled\n"));
tod_hack_reset();
}
return;
}
if (currprefs.cs_ciaatod == 0) {
rate = (int)(vblank_hz + 0.5);
if (rate >= 59 && rate <= 61)
rate = 60;
if (rate >= 49 && rate <= 51)
rate = 50;
} else if (currprefs.cs_ciaatod == 1) {
rate = 50;
} else {
rate = 60;
}
if (rate <= 0)
return;
if (rate != oldrate || (cia[0].tod & 0xfff) != (tod_hack_tod_last & 0xfff)) {
write_log(_T("TOD HACK reset %d,%d %ld,%lld\n"), rate, oldrate, cia[0].tod, tod_hack_tod_last);
tod_hack_reset();
oldrate = rate;
docount = 1;
}
if (!dotod && currprefs.cs_ciaatod == 0)
return;
if (tod_hack_delay > 0) {
tod_hack_delay--;
if (tod_hack_delay > 0)
return;
tod_hack_delay = TOD_HACK_DELAY;
}
gettimeofday(&tv, NULL);
t = (uae_u64)tv.tv_sec * 1000000 + tv.tv_usec;
if (t - tod_hack_tv >= 1000000 / rate) {
tod_hack_tv += 1000000 / rate;
tod_diff_cnt += 1000000 - (1000000 / rate) * rate;
tod_hack_tv += tod_diff_cnt / rate;
tod_diff_cnt %= rate;
docount = 1;
}
if (docount) {
cia[0].tod++;
cia[0].tod &= 0x00ffffff;
tod_hack_tod_last = cia[0].tod;
cia_checkalarm(false, true, 0);
}
}
static int resetwarning_phase, resetwarning_timer;
static void setcode(uae_u8 keycode)
{
kbcode = ~((keycode << 1) | (keycode >> 7));
}
static void sendrw(void)
{
setcode(AK_RESETWARNING);
cia[0].sdr = kbcode;
kblostsynccnt = 8 * maxvpos * 8; // 8 frames * 8 bits.
CIA_sync_interrupt(0, ICR_SP);
write_log(_T("KB: sent reset warning code (phase=%d)\n"), resetwarning_phase);
}
int resetwarning_do(int canreset)
{
if (!currprefs.keyboard_connected)
return 0;
if (resetwarning_phase || regs.halted > 0) {
/* just force reset if second reset happens during resetwarning */
if (canreset) {
resetwarning_phase = 0;
resetwarning_timer = 0;
}
return 0;
}
resetwarning_phase = 1;
resetwarning_timer = maxvpos_nom * 5;
write_log(_T("KB: reset warning triggered\n"));
sendrw();
return 1;
}
static void resetwarning_check(void)
{
if (resetwarning_timer > 0) {
resetwarning_timer--;
if (resetwarning_timer <= 0) {
write_log(_T("KB: reset warning forced reset. Phase=%d\n"), resetwarning_phase);
resetwarning_phase = -1;
kblostsynccnt = 0;
inputdevice_do_kb_reset();
}
}
if (resetwarning_phase == 1) {