forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cia.cpp
2769 lines (2564 loc) · 63.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
*/
#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"
#include "serial.h"
#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 "autoconf.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 TOD_HACK
/* Akiko internal CIA differences:
- BFE101 and BFD100: reads 3F if data direction is in.
*/
/* e-clock is 10 CPU cycles, 4 cycles high, 6 low
* data transfer happens during 4 high cycles
*/
#define ECLOCK_DATA_CYCLE 4
#define ECLOCK_WAIT_CYCLE 6
#define DIV10 ((ECLOCK_DATA_CYCLE + ECLOCK_WAIT_CYCLE) * CYCLE_UNIT / 2) /* Yes, a bad identifier. */
#define CIASTARTCYCLESHI 3
#define CIASTARTCYCLESCRA 2
static uae_u32 ciaaicr, ciaaimask, ciabicr, ciabimask;
static uae_u32 ciaacra, ciaacrb, ciabcra, ciabcrb;
static uae_u32 ciaastarta, ciaastartb, ciabstarta, ciabstartb;
/* Values of the CIA timers. */
static uae_u32 ciaata, ciaatb, ciabta, ciabtb;
/* Computed by compute_passed_time. */
static uae_u32 ciaata_passed, ciaatb_passed, ciabta_passed, ciabtb_passed;
static uae_u32 ciaatod, ciabtod, ciaatol, ciabtol, ciaaalarm, ciabalarm;
static int ciaatlatch, ciabtlatch;
static bool oldovl;
static bool led;
static int led_old_brightness;
static uae_u32 led_cycles_on, led_cycles_off, led_cycle;
uae_u32 ciabpra;
static uae_u32 ciaala, ciaalb, ciabla, ciablb;
static int ciaatodon, ciabtodon;
static uae_u32 ciaapra, ciaaprb, ciaadra, ciaadrb, ciaasdr, ciaasdr_buf, ciaasdr_load, ciaasdr_cnt;
static uae_u32 ciabprb, ciabdra, ciabdrb, ciabsdr, ciabsdr_buf, ciabsdr_load, ciabsdr_cnt;
static int div10;
static int kbstate, kblostsynccnt;
static uae_u32 kbhandshakestart;
static uae_u8 kbcode;
static uae_u8 serbits;
static int warned = 10;
static struct rtc_msm_data rtc_msm;
static struct rtc_ricoh_data rtc_ricoh;
static void setclr (uae_u32 *p, uae_u32 val)
{
if (val & 0x80) {
*p |= val & 0x7F;
} else {
*p &= ~val;
}
}
/* delay interrupt after current CIA register access if
* interrupt would have triggered mid access
*/
static int cia_interrupt_disabled;
static int cia_interrupt_delay;
static void ICR (uae_u32 data)
{
safe_interrupt_set(IRQ_SOURCE_CIA, 0, (data & 0x2000) != 0);
}
static void ICRA (uae_u32 dummy)
{
if (ciaaicr & 0x80)
ciaaicr |= 0x40;
#if 1
if (currprefs.cpu_memory_cycle_exact && !(ciaaicr & 0x20) && (cia_interrupt_disabled & 1)) {
cia_interrupt_delay |= 1;
#if CIAB_DEBUG_IRQ
write_log(_T("ciab interrupt disabled ICR=%02X PC=%x\n"), ciabicr, M68K_GETPC);
#endif
return;
}
#endif
ciaaicr |= 0x20;
ICR (0x0008);
}
static void ICRB (uae_u32 dummy)
{
if (ciabicr & 0x80)
ciabicr |= 0x40;
#if 1
if (currprefs.cpu_memory_cycle_exact && !(ciabicr & 0x20) && (cia_interrupt_disabled & 2)) {
cia_interrupt_delay |= 2;
#if CIAB_DEBUG_IRQ
write_log(_T("ciab interrupt disabled ICR=%02X PC=%x\n"), ciabicr, M68K_GETPC);
#endif
return;
}
#endif
ciabicr |= 0x20;
if (currprefs.cs_compatible == CP_VELVET) {
// Both CIAs in Velvet are connected to level 2.
ICR (0x0008);
} else {
ICR (0x2000);
}
}
static void RethinkICRA (void)
{
if (ciaaicr & ciaaimask) {
#if CIAA_DEBUG_IRQ
write_log (_T("CIAA IRQ %02X\n"), ciaaicr);
#endif
if (!(ciaaicr & 0x80)) {
ciaaicr |= 0x80;
if (currprefs.cpu_memory_cycle_exact) {
event2_newevent_xx (-1, DIV10 + 2 * CYCLE_UNIT + CYCLE_UNIT / 2 + 4 * CYCLE_UNIT, 0, ICRA);
} else {
ICRA (0);
}
}
}
}
static void RethinkICRB (void)
{
if (ciabicr & ciabimask) {
#if CIAB_DEBUG_IRQ
write_log (_T("CIAB IRQ %02X\n"), ciabicr);
#endif
if (!(ciabicr & 0x80)) {
ciabicr |= 0x80;
if (currprefs.cpu_memory_cycle_exact) {
event2_newevent_xx (-1, DIV10 + 2 * CYCLE_UNIT + CYCLE_UNIT / 2 + 4 * CYCLE_UNIT, 0, ICRB);
} else {
ICRB (0);
}
}
}
}
void rethink_cias (void)
{
if (ciaaicr & 0x40)
ICRA (0);
if (ciabicr & 0x40)
ICRB (0);
}
/* Figure out how many CIA timer cycles have passed for each timer since the
last call of CIA_calctimers. */
static void compute_passed_time (void)
{
uae_u32 ccount = (get_cycles () - eventtab[ev_cia].oldcycles + div10);
uae_u32 ciaclocks = ccount / DIV10;
ciaata_passed = ciaatb_passed = ciabta_passed = ciabtb_passed = 0;
/* CIA A timers */
if ((ciaacra & 0x21) == 0x01) {
uae_u32 cc = ciaclocks;
if (cc > ciaastarta)
cc -= ciaastarta;
else
cc = 0;
assert ((ciaata + 1) >= cc);
ciaata_passed = cc;
}
if ((ciaacrb & 0x61) == 0x01) {
uae_u32 cc = ciaclocks;
if (cc > ciaastartb)
cc -= ciaastartb;
else
cc = 0;
assert ((ciaatb + 1) >= cc);
ciaatb_passed = cc;
}
/* CIA B timers */
if ((ciabcra & 0x21) == 0x01) {
uae_u32 cc = ciaclocks;
if (cc > ciabstarta)
cc -= ciabstarta;
else
cc = 0;
assert ((ciabta + 1) >= cc);
ciabta_passed = cc;
}
if ((ciabcrb & 0x61) == 0x01) {
uae_u32 cc = ciaclocks;
if (cc > ciabstartb)
cc -= ciabstartb;
else
cc = 0;
assert ((ciabtb + 1) >= cc);
ciabtb_passed = cc;
}
}
/* 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 int CIA_update_check (void)
{
uae_u32 ccount = (get_cycles () - eventtab[ev_cia].oldcycles + div10);
uae_u32 ciaclocks = ccount / DIV10;
int aovfla = 0, aovflb = 0, asp = 0, bovfla = 0, bovflb = 0, bsp = 0;
int icr = 0;
div10 = ccount % DIV10;
/* CIA A timers */
// A INMODE=0
if ((ciaacra & 0x21) == 0x01) {
bool check = true;
uae_u32 cc = ciaclocks;
if (ciaastarta > 0) {
if (cc > ciaastarta) {
cc -= ciaastarta;
ciaastarta = 0;
} else {
ciaastarta -= cc;
check = false;
}
}
if (check) {
assert ((ciaata + 1) >= cc);
if ((ciaata + 1) == cc) {
// SP in output mode (data sent can be ignored)
if ((ciaacra & 0x48) == 0x40 && ciaasdr_cnt > 0) {
ciaasdr_cnt--;
if (ciaasdr_cnt == 0) {
asp = 1;
if (ciaasdr_load) {
ciaasdr_load = 0;
ciaasdr_buf = ciaasdr;
ciaasdr_cnt = 8 * 2;
}
}
}
aovfla = 1;
// B INMODE=10 or 11
if ((ciaacrb & 0x61) == 0x41 || (ciaacrb & 0x61) == 0x61) {
if (ciaatb-- == 0)
aovflb = 1;
}
}
ciaata -= cc;
}
}
// A INMODE=00
if ((ciaacrb & 0x61) == 0x01) {
bool check = true;
uae_u32 cc = ciaclocks;
if (ciaastartb > 0) {
if (cc > ciaastartb) {
cc -= ciaastartb;
ciaastartb = 0;
} else {
ciaastartb -= cc;
check = false;
}
}
if (check) {
assert ((ciaatb + 1) >= cc);
if ((ciaatb + 1) == cc)
aovflb = 1;
ciaatb -= cc;
}
}
/* CIA B timers */
// A INMODE=0
if ((ciabcra & 0x21) == 0x01) {
bool check = true;
uae_u32 cc = ciaclocks;
if (ciabstarta > 0) {
if (cc > ciabstarta) {
cc -= ciabstarta;
ciabstarta = 0;
} else {
ciabstarta -= cc;
check = false;
}
}
if (check) {
assert ((ciabta + 1) >= cc);
if ((ciabta + 1) == cc) {
// SP in output mode
if ((ciabcra & 0x48) == 0x40 && ciabsdr_cnt > 0) {
ciaasdr_cnt--;
if (!(ciaasdr_cnt & 1)) {
ciabsdr_buf <<= 1;
}
if (ciabsdr_cnt == 0) {
bsp = 1;
if (ciabsdr_load) {
ciabsdr_load = 0;
ciabsdr_buf = ciabsdr;
ciabsdr_cnt = 8 * 2;
}
}
}
bovfla = 1;
// B INMODE=10 or 11
if ((ciabcrb & 0x61) == 0x41 || (ciabcrb & 0x61) == 0x61) {
if (ciabtb-- == 0)
bovflb = 1;
}
}
ciabta -= cc;
}
}
// B INMODE=00
if ((ciabcrb & 0x61) == 0x01) {
bool check = true;
uae_u32 cc = ciaclocks;
if (ciabstartb > 0) {
if (cc > ciabstartb) {
cc -= ciabstartb;
ciabstartb = 0;
} else {
ciabstartb -= cc;
check = false;
}
}
if (check) {
assert ((ciabtb + 1) >= cc);
if ((ciabtb + 1) == cc)
bovflb = 1;
ciabtb -= cc;
}
}
if (aovfla) {
ciaaicr |= 1; icr = 1;
ciaata = ciaala;
if (ciaacra & 0x8) {
ciaacra &= ~1;
}
}
if (aovflb) {
ciaaicr |= 2; icr = 1;
ciaatb = ciaalb;
if (ciaacrb & 0x8) {
ciaacrb &= ~1;
}
}
if (asp) {
ciaaicr |= 8; icr = 1;
}
if (bovfla) {
ciabicr |= 1; icr |= 2;
ciabta = ciabla;
if (ciabcra & 0x8) {
ciabcra &= ~1;
}
}
if (bovflb) {
ciabicr |= 2; icr |= 2;
ciabtb = ciablb;
if (ciabcrb & 0x8) {
ciabcrb &= ~1;
}
}
if (bsp) {
ciabicr |= 8; icr |= 2;
}
return icr;
}
static void CIA_update (void)
{
int icr = CIA_update_check ();
if (icr & 1)
RethinkICRA ();
if (icr & 2)
RethinkICRB ();
}
/* Call this only after CIA_update has been called in the same cycle. */
static void CIA_calctimers (void)
{
uae_s32 ciaatimea = -1, ciaatimeb = -1, ciabtimea = -1, ciabtimeb = -1;
int div10diff = DIV10 - div10;
eventtab[ev_cia].oldcycles = get_cycles ();
if ((ciaacra & 0x21) == 0x01) {
ciaatimea = div10diff + DIV10 * (ciaata + ciaastarta);
}
#if 0
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. */
/* This may overflow, however. So just ignore this timer and
use the fact that we'll call CIA_handler for the A timer. */
/* ciaatimeb = ciaatimea + ciaala * DIV10 * ciaatb; */
}
}
}
#endif
if ((ciaacrb & 0x61) == 0x01) {
ciaatimeb = div10diff + DIV10 * (ciaatb + ciaastartb);
}
if ((ciabcra & 0x21) == 0x01) {
ciabtimea = div10diff + DIV10 * (ciabta + ciabstarta);
}
#if 0
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; */
}
}
}
#endif
if ((ciabcrb & 0x61) == 0x01) {
ciabtimeb = div10diff + DIV10 * (ciabtb + ciabstartb);
}
eventtab[ev_cia].active = (ciaatimea != -1 || ciaatimeb != -1
|| ciabtimea != -1 || ciabtimeb != -1);
if (eventtab[ev_cia].active) {
uae_u32 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 + get_cycles ();
}
events_schedule();
}
void CIA_handler (void)
{
CIA_update ();
CIA_calctimers ();
}
void cia_diskindex (void)
{
ciabicr |= 0x10;
RethinkICRB ();
}
void cia_parallelack (void)
{
ciaaicr |= 0x10;
RethinkICRA ();
}
static bool checkalarm (uae_u32 tod, uae_u32 alarm, bool inc, int ab)
{
if (tod == alarm)
return true;
// if (!ab)
// return false;
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_INLINE bool ciab_checkalarm (bool inc, bool irq)
{
// 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 1
if ((munge24 (m68k_getpc ()) & 0xFFF80000) != 0xF80000) {
if (ciabtod == 0 && ciabalarm == 0)
return false;
}
#endif
if (checkalarm (ciabtod, ciabalarm, inc, 1)) {
#if CIAB_DEBUG_IRQ
write_log (_T("CIAB tod %08x %08x\n"), ciabtod, ciabalarm);
#endif
if (irq) {
ciabicr |= 4;
RethinkICRB ();
}
return true;
}
return false;
}
STATIC_INLINE void ciaa_checkalarm (bool inc)
{
if (checkalarm (ciaatod, ciaaalarm, inc, 0)) {
#if CIAA_DEBUG_IRQ
write_log (_T("CIAA tod %08x %08x\n"), ciaatod, ciaaalarm);
#endif
ciaaicr |= 4;
RethinkICRA ();
}
}
#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 = ciaatod;
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 (int 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 || (ciaatod & 0xfff) != (tod_hack_tod_last & 0xfff)) {
write_log (_T("TOD HACK reset %d,%d %ld,%lld\n"), rate, oldrate, ciaatod, 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) {
ciaatod++;
ciaatod &= 0x00ffffff;
tod_hack_tod_last = ciaatod;
ciaa_checkalarm (false);
}
}
static int resetwarning_phase, resetwarning_timer;
static void setcode (uae_u8 keycode)
{
kbcode = ~((keycode << 1) | (keycode >> 7));
}
static void sendrw (void)
{
setcode (AK_RESETWARNING);
ciaasdr = kbcode;
kblostsynccnt = 8 * maxvpos * 8; // 8 frames * 8 bits.
ciaaicr |= 8;
RethinkICRA ();
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;
send_internalevent (INTERNALEVENT_KBRESET);
uae_reset (0, 1);
}
}
if (resetwarning_phase == 1) {
if (!kblostsynccnt) { /* first AK_RESETWARNING handshake received */
write_log (_T("KB: reset warning second phase..\n"));
resetwarning_phase = 2;
resetwarning_timer = maxvpos_nom * 5;
sendrw ();
}
} else if (resetwarning_phase == 2) {
if (ciaacra & 0x40) { /* second AK_RESETWARNING handshake active */
resetwarning_phase = 3;
write_log (_T("KB: reset warning SP = output\n"));
/* System won't reset until handshake signal becomes inactive or 10s has passed */
resetwarning_timer = 10 * maxvpos_nom * vblank_hz;
}
} else if (resetwarning_phase == 3) {
if (!(ciaacra & 0x40)) { /* second AK_RESETWARNING handshake disabled */
write_log (_T("KB: reset warning end by software. reset.\n"));
resetwarning_phase = -1;
kblostsynccnt = 0;
send_internalevent (INTERNALEVENT_KBRESET);
uae_reset (0, 1);
}
}
}
void CIA_hsync_prehandler (void)
{
}
static void keyreq (void)
{
#if KB_DEBUG
write_log (_T("code=%02x (%02x)\n"), kbcode, (uae_u8)(~((kbcode >> 1) | (kbcode << 7))));
#endif
ciaasdr = kbcode;
kblostsynccnt = 8 * maxvpos * 8; // 8 frames * 8 bits.
ciaaicr |= 8;
RethinkICRA ();
}
/* All this complexity to lazy evaluate TOD increase.
* Only increase it cycle-exactly if it is visible to running program:
* causes interrupt or program is reading or writing TOD registers
*/
static int ciab_tod_hoffset;
static int ciab_tod_event_state;
// TOD increase has extra 14-16 E-clock delay
// Possibly TICK input pin has built-in debounce circuit
#define TOD_INC_DELAY (14 * (ECLOCK_DATA_CYCLE + ECLOCK_WAIT_CYCLE) / 2)
static void CIAB_tod_inc (bool irq)
{
ciab_tod_event_state = 3; // done
if (!ciabtodon)
return;
ciabtod++;
ciabtod &= 0xFFFFFF;
ciab_checkalarm (true, irq);
}
static void CIAB_tod_inc_event (uae_u32 v)
{
if (ciab_tod_event_state != 2)
return;
CIAB_tod_inc (true);
}
// Someone reads or writes TOD registers, sync TOD increase
static void CIAB_tod_check (void)
{
if (ciab_tod_event_state != 1 || !ciabtodon)
return;
int hpos = current_hpos ();
hpos -= ciab_tod_hoffset;
if (hpos >= 0 || currprefs.m68k_speed < 0) {
// Program should see the changed TOD
CIAB_tod_inc (true);
return;
}
// Not yet, add event to guarantee exact TOD inc position
ciab_tod_event_state = 2; // event active
event2_newevent_xx (-1, -hpos, 0, CIAB_tod_inc_event);
}
void CIAB_tod_handler (int hoffset)
{
if (!ciabtodon)
return;
ciab_tod_hoffset = hoffset + TOD_INC_DELAY;
ciab_tod_event_state = 1; // TOD inc needed
if (checkalarm ((ciabtod + 1) & 0xffffff, ciabalarm, true, 1)) {
// causes interrupt on this line, add event
ciab_tod_event_state = 2; // event active
event2_newevent_xx (-1, ciab_tod_hoffset, 0, CIAB_tod_inc_event);
}
}
void keyboard_connected(bool connect)
{
if (connect) {
write_log(_T("Keyboard connected\n"));
} else {
write_log(_T("Keyboard disconnected\n"));
}
kbstate = 0;
kblostsynccnt = 0;
resetwarning_phase = 0;
}
static void check_keyboard(void)
{
if (currprefs.keyboard_connected) {
if ((keys_available () || kbstate < 3) && !kblostsynccnt ) {
switch (kbstate)
{
case 0:
kbcode = 0; /* powerup resync */
kbstate++;
break;
case 1:
setcode (AK_INIT_POWERUP);
kbstate++;
break;
case 2:
setcode (AK_TERM_POWERUP);
kbstate++;
break;
case 3:
kbcode = ~get_next_key ();
break;
}
keyreq ();
}
} else {
while (keys_available()) {
get_next_key();
}
}
}
void CIA_hsync_posthandler (bool ciahsync, bool dotod)
{
if (ciahsync) {
// cia hysnc
// Previous line was supposed to increase TOD but
// no one cared. Do it now.
if (ciab_tod_event_state == 1)
CIAB_tod_inc (false);
ciab_tod_event_state = 0;
if (currprefs.tod_hack && ciaatodon)
do_tod_hack (dotod);
} else if (currprefs.keyboard_connected) {
// custom hsync
if (resetwarning_phase) {
resetwarning_check ();
while (keys_available ())
get_next_key ();
} else {
if ((hsync_counter & 15) == 0)
check_keyboard();
}
} else {
while (keys_available()) {
get_next_key();
}
}
}
static void calc_led (int old_led)
{
uae_u32 c = get_cycles ();
uae_u32 t = (c - led_cycle) / CYCLE_UNIT;
if (old_led)
led_cycles_on += t;
else
led_cycles_off += t;
led_cycle = c;
}
static void led_vsync (void)
{
int v;
calc_led (led);
if (led_cycles_on && !led_cycles_off)
v = 255;
else if (led_cycles_off && !led_cycles_on)
v = 0;
else if (led_cycles_off)
v = led_cycles_on * 255 / (led_cycles_on + led_cycles_off);
else
v = 255;
if (v < 0)
v = 0;
if (currprefs.power_led_dim && v < currprefs.power_led_dim)
v = currprefs.power_led_dim;
if (v > 255)
v = 255;
gui_data.powerled_brightness = v;
led_cycles_on = 0;
led_cycles_off = 0;
if (led_old_brightness != gui_data.powerled_brightness) {
gui_data.powerled = gui_data.powerled_brightness > 96;
gui_led (LED_POWER, gui_data.powerled, gui_data.powerled_brightness);
led_filter_audio ();
}
led_old_brightness = gui_data.powerled_brightness;
led_cycle = get_cycles ();
}
static void write_battclock (void);
void CIA_vsync_prehandler (void)
{
if (heartbeat_cnt > 0)
heartbeat_cnt--;
if (rtc_msm.delayed_write < 0) {
rtc_msm.delayed_write = 50;
} else if (rtc_msm.delayed_write > 0) {
rtc_msm.delayed_write--;
if (rtc_msm.delayed_write == 0)
write_battclock ();
}
if (rtc_ricoh.delayed_write < 0) {
rtc_ricoh.delayed_write = 50;
} else if (rtc_ricoh.delayed_write > 0) {
rtc_ricoh.delayed_write--;
if (rtc_ricoh.delayed_write == 0)
write_battclock ();
}
led_vsync ();
CIA_handler ();
if (kblostsynccnt > 0) {
kblostsynccnt -= maxvpos;
if (kblostsynccnt <= 0) {
kblostsynccnt = 0;
kbcode = 0;
keyreq ();
#if KB_DEBUG
write_log (_T("lostsync\n"));
#endif
}
}
}
static void CIAA_tod_handler (uae_u32 v)
{
ciaatod++;
ciaatod &= 0xFFFFFF;
ciaa_checkalarm (true);
}
void CIAA_tod_inc (int cycles)
{
#ifdef TOD_HACK
if (currprefs.tod_hack && tod_hack_enabled == 1)
return;
#endif
if (!ciaatodon)
return;
event2_newevent_xx (-1, cycles + TOD_INC_DELAY, 0, CIAA_tod_handler);