-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCIA.cpp
executable file
·1150 lines (1012 loc) · 23.8 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
/*
Frodo, Commodore 64 emulator for the iPhone
Copyright (C) 1994-1997,2002 Christian Bauer
See gpl.txt for license information.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* CIA.cpp - 6526 emulation
*
* Frodo (C) 1994-1997,2002 Christian Bauer
*
*
* Notes:
* ------
*
* - The EmulateLine() function is called for every emulated raster
* line. It counts down the timers and triggers interrupts if
* necessary.
* - The TOD clocks are counted by CountTOD() during the VBlank, so
* the input frequency is 50Hz
* - The fields KeyMatrix and RevMatrix contain one bit for each
* key on the C64 keyboard (0: key pressed, 1: key released).
* KeyMatrix is used for normal keyboard polling (PRA->PRB),
* RevMatrix for reversed polling (PRB->PRA).
*
* Incompatibilities:
* ------------------
*
* - The TOD clock should not be stopped on a read access, but
* latched
* - The SDR interrupt is faked
*/
#include "sysdeps.h"
#include "CIA.h"
#include "CPUC64.h"
#include "CPU1541.h"
#include "VIC.h"
#include "Prefs.h"
/*
* Constructors
*/
MOS6526::MOS6526(MOS6510 *CPU) : the_cpu(CPU) {}
MOS6526_1::MOS6526_1(MOS6510 *CPU, MOS6569 *VIC) : MOS6526(CPU), the_vic(VIC) { type=1; }
MOS6526_2::MOS6526_2(MOS6510 *CPU, MOS6569 *VIC, MOS6502_1541 *CPU1541) :
MOS6526(CPU), the_vic(VIC), the_cpu_1541(CPU1541) { type=2; }
/*
* Switch from standard emulation to single cycle emulation
*/
void MOS6526::SwitchToSC(void)
{
ta_irq_next_cycle = false;
tb_irq_next_cycle = false;
has_new_cra = false;
has_new_crb = false;
ta_state = ta_cnt_phi2 ? T_COUNT : T_STOP;
tb_state = (tb_cnt_phi2 || tb_cnt_ta) ? T_COUNT : T_STOP;
CyclesTillAction = 1;
CyclesTillActionCnt = 1;
}
/*
* Switch from single cycle emulation to standard emulation
*/
void MOS6526::SwitchToStandard(void)
{
UpdateTATB(false);
if(has_new_cra)
{
cra = new_cra & 0xef;
if (new_cra & 0x10) // Force load
ta = latcha;
}
if(has_new_crb)
{
crb = new_crb & 0xef;
if (new_crb & 0x10) // Force load
tb = latchb;
}
if(ta_irq_next_cycle)
TriggerInterrupt(1);
if(tb_irq_next_cycle)
TriggerInterrupt(2);
}
/*
* Reset the CIA
*/
void MOS6526::Reset(void)
{
pra = prb = ddra = ddrb = 0;
ta = tb = 0xffff;
latcha = latchb = 1;
tod_10ths = tod_sec = tod_min = tod_hr = 0;
alm_10ths = alm_sec = alm_min = alm_hr = 0;
sdr = icr = cra = crb = int_mask = 0;
tod_halt = ta_cnt_phi2 = tb_cnt_phi2 = tb_cnt_ta = false;
tod_divider = 0;
ta_irq_next_cycle = tb_irq_next_cycle = false;
ta_state = tb_state = T_STOP;
CyclesTillAction = 1;
CyclesTillAction = 1;
}
void MOS6526_1::Reset(void)
{
MOS6526::Reset();
// Clear keyboard matrix and joystick states
for (int i=0; i<8; i++)
KeyMatrix[i] = RevMatrix[i] = 0xff;
Joystick1 = Joystick2 = 0xff;
prev_lp = 0x10;
}
void MOS6526_2::Reset(void)
{
MOS6526::Reset();
// VA14/15 = 0
the_vic->ChangedVA(0);
// IEC
IECLines = 0xd0;
}
/*
* Get CIA state
*/
void MOS6526::GetState(MOS6526State *cs)
{
cs->pra = pra;
cs->prb = prb;
cs->ddra = ddra;
cs->ddrb = ddrb;
cs->ta_lo = ta & 0xff;
cs->ta_hi = ta >> 8;
cs->tb_lo = tb & 0xff;
cs->tb_hi = tb >> 8;
cs->latcha = latcha;
cs->latchb = latchb;
cs->cra = cra;
cs->crb = crb;
cs->tod_10ths = tod_10ths;
cs->tod_sec = tod_sec;
cs->tod_min = tod_min;
cs->tod_hr = tod_hr;
cs->alm_10ths = alm_10ths;
cs->alm_sec = alm_sec;
cs->alm_min = alm_min;
cs->alm_hr = alm_hr;
cs->sdr = sdr;
cs->int_data = icr;
cs->int_mask = int_mask;
cs->CyclesTillAction = CyclesTillAction;
cs->CyclesTillActionCnt = CyclesTillActionCnt;
cs->has_new_cra = has_new_cra;
cs->has_new_crb = has_new_crb;
cs->new_cra = new_cra;
cs->new_crb = new_crb;
cs->ta_irq_next_cycle = ta_irq_next_cycle;
cs->tb_irq_next_cycle = tb_irq_next_cycle;
cs->ta_state = ta_state;
cs->tb_state = tb_state;
cs->ta_cnt_phi2 = ta_cnt_phi2;
cs->tb_cnt_phi2 = tb_cnt_phi2;
cs->tb_cnt_ta = tb_cnt_ta;
cs->more_info = GetMoreInfo();
}
/*
* Restore CIA state
*/
void MOS6526::SetState(MOS6526State *cs)
{
pra = cs->pra;
prb = cs->prb;
ddra = cs->ddra;
ddrb = cs->ddrb;
ta = (cs->ta_hi << 8) | cs->ta_lo;
tb = (cs->tb_hi << 8) | cs->tb_lo;
latcha = cs->latcha;
latchb = cs->latchb;
cra = cs->cra;
crb = cs->crb;
tod_10ths = cs->tod_10ths;
tod_sec = cs->tod_sec;
tod_min = cs->tod_min;
tod_hr = cs->tod_hr;
alm_10ths = cs->alm_10ths;
alm_sec = cs->alm_sec;
alm_min = cs->alm_min;
alm_hr = cs->alm_hr;
sdr = cs->sdr;
icr = cs->int_data;
int_mask = cs->int_mask;
tod_halt = false;
CyclesTillAction = cs->CyclesTillAction;
CyclesTillActionCnt = cs->CyclesTillActionCnt;
has_new_cra = cs->has_new_cra;
has_new_crb = cs->has_new_crb;
new_cra = cs->new_cra;
new_crb = cs->new_crb;
ta_irq_next_cycle = cs->ta_irq_next_cycle;
tb_irq_next_cycle = cs->tb_irq_next_cycle;
ta_state = cs->ta_state;
tb_state = cs->tb_state;
ta_cnt_phi2 = cs->ta_cnt_phi2;
tb_cnt_phi2 = cs->tb_cnt_phi2;
tb_cnt_ta = cs->tb_cnt_ta;
SetMoreInfo(cs->more_info);
}
/*
* Read from register (CIA 1)
*/
void MOS6526_1::UpdateDataPorts(void)
{
uint8 ret = pra | ~ddra;
uint8 tst = (prb | ~ddrb) & Joystick1;
if (!(tst & 0x01)) ret &= RevMatrix[0]; // AND all active columns
if (!(tst & 0x02)) ret &= RevMatrix[1];
if (!(tst & 0x04)) ret &= RevMatrix[2];
if (!(tst & 0x08)) ret &= RevMatrix[3];
if (!(tst & 0x10)) ret &= RevMatrix[4];
if (!(tst & 0x20)) ret &= RevMatrix[5];
if (!(tst & 0x40)) ret &= RevMatrix[6];
if (!(tst & 0x80)) ret &= RevMatrix[7];
dataport1 = ret & Joystick2;
ret = ~ddrb;
tst = (pra | ~ddra) & Joystick2;
if (!(tst & 0x01)) ret &= KeyMatrix[0]; // AND all active rows
if (!(tst & 0x02)) ret &= KeyMatrix[1];
if (!(tst & 0x04)) ret &= KeyMatrix[2];
if (!(tst & 0x08)) ret &= KeyMatrix[3];
if (!(tst & 0x10)) ret &= KeyMatrix[4];
if (!(tst & 0x20)) ret &= KeyMatrix[5];
if (!(tst & 0x40)) ret &= KeyMatrix[6];
if (!(tst & 0x80)) ret &= KeyMatrix[7];
dataport2 = (ret | (prb & ddrb)) & Joystick1;
}
uint8 MOS6526_1::ReadRegister(uint16 adr)
{
switch (adr) {
case 0x00: return dataport1;
case 0x01: return dataport2;
case 0x02: return ddra;
case 0x03: return ddrb;
case 0x04:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return ta;
case 0x05:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return ta >> 8;
case 0x06:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return tb;
case 0x07:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return tb >> 8;
case 0x08: tod_halt = false; return tod_10ths;
case 0x09: return tod_sec;
case 0x0a: return tod_min;
case 0x0b: tod_halt = true; return tod_hr;
case 0x0c: return sdr;
case 0x0d: {
uint8 ret = icr; // Read and clear ICR
icr = 0;
the_cpu->ClearCIAIRQ(); // Clear IRQ
return ret;
}
case 0x0e: return cra;
case 0x0f: return crb;
}
return 0; // Can't happen
}
/*
* Read from register (CIA 2)
*/
uint8 MOS6526_2::ReadRegister(uint16 adr)
{
switch (adr) {
case 0x00:
return (pra | ~ddra) & 0x3f
| IECLines & the_cpu_1541->IECLines;
case 0x01: return prb | ~ddrb;
case 0x02: return ddra;
case 0x03: return ddrb;
case 0x04:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return ta;
case 0x05:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return ta >> 8;
case 0x06:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return tb;
case 0x07:
if(ThePrefs.SingleCycleEmulation)
UpdateTATB(false);
return tb >> 8;
case 0x08: tod_halt = false; return tod_10ths;
case 0x09: return tod_sec;
case 0x0a: return tod_min;
case 0x0b: tod_halt = true; return tod_hr;
case 0x0c: return sdr;
case 0x0d: {
uint8 ret = icr; // Read and clear ICR
icr = 0;
the_cpu->ClearNMI(); // Clear NMI
return ret;
}
case 0x0e: return cra;
case 0x0f: return crb;
}
return 0; // Can't happen
}
/*
* Write to register (CIA 1)
*/
// Write to port B, check for lightpen interrupt
inline void MOS6526_1::check_lp(void)
{
if ((prb | ~ddrb) & 0x10 != prev_lp)
the_vic->TriggerLightpen();
prev_lp = (prb | ~ddrb) & 0x10;
}
void MOS6526_1::WriteRegister(uint16 adr, uint8 byte)
{
switch (adr) {
case 0x0:
pra = byte;
UpdateDataPorts();
break;
case 0x1:
prb = byte;
UpdateDataPorts();
check_lp();
break;
case 0x2:
ddra = byte;
UpdateDataPorts();
break;
case 0x3:
ddrb = byte;
UpdateDataPorts();
check_lp();
break;
case 0x4: latcha = (latcha & 0xff00) | byte; break;
case 0x5:
latcha = (latcha & 0xff) | (byte << 8);
if (!(cra & 1)) // Reload timer if stopped
ta = latcha;
break;
case 0x6: latchb = (latchb & 0xff00) | byte; break;
case 0x7:
latchb = (latchb & 0xff) | (byte << 8);
if (!(crb & 1)) // Reload timer if stopped
tb = latchb;
break;
case 0x8:
if (crb & 0x80)
alm_10ths = byte & 0x0f;
else
tod_10ths = byte & 0x0f;
break;
case 0x9:
if (crb & 0x80)
alm_sec = byte & 0x7f;
else
tod_sec = byte & 0x7f;
break;
case 0xa:
if (crb & 0x80)
alm_min = byte & 0x7f;
else
tod_min = byte & 0x7f;
break;
case 0xb:
if (crb & 0x80)
alm_hr = byte & 0x9f;
else
tod_hr = byte & 0x9f;
break;
case 0xc:
sdr = byte;
TriggerInterrupt(8); // Fake SDR interrupt for programs that need it
break;
case 0xd:
if(ThePrefs.SingleCycleEmulation)
{
if (byte & 0x80)
int_mask |= byte & 0x7f;
else
int_mask &= ~byte;
if (icr & int_mask & 0x1f) { // Trigger IRQ if pending
icr |= 0x80;
the_cpu->TriggerCIAIRQ();
}
}
else
{
if (ThePrefs.CIAIRQHack) // Hack for addressing modes that read from the address
icr = 0;
if (byte & 0x80) {
int_mask |= byte & 0x7f;
if (icr & int_mask & 0x1f) { // Trigger IRQ if pending
icr |= 0x80;
the_cpu->TriggerCIAIRQ();
}
} else
int_mask &= ~byte;
}
break;
case 0xe:
if(ThePrefs.SingleCycleEmulation)
{
UpdateTATB(true);
has_new_cra = true; // Delay write by 1 cycle
new_cra = byte;
ta_cnt_phi2 = ((byte & 0x20) == 0x00);
}
else
{
cra = byte & 0xef;
if (byte & 0x10) // Force load
ta = latcha;
ta_cnt_phi2 = ((byte & 0x21) == 0x01);
}
break;
case 0xf:
if(ThePrefs.SingleCycleEmulation)
{
UpdateTATB(true);
has_new_crb = true; // Delay write by 1 cycle
new_crb = byte;
tb_cnt_phi2 = ((byte & 0x60) == 0x00);
tb_cnt_ta = ((byte & 0x60) == 0x40);
}
else
{
crb = byte & 0xef;
if (byte & 0x10) // Force load
tb = latchb;
tb_cnt_phi2 = ((byte & 0x61) == 0x01);
tb_cnt_ta = ((byte & 0x61) == 0x41);
}
break;
}
}
/*
* Write to register (CIA 2)
*/
void MOS6526_2::WriteRegister(uint16 adr, uint8 byte)
{
uint8 old_lines;
switch (adr) {
case 0x0:{
pra = byte;
if(ThePrefs.SingleCycleEmulation)
{
the_vic->ChangedVA(~(pra | ~ddra) & 3);
old_lines = IECLines;
IECLines = (~byte << 2) & 0x80 // DATA
| (~byte << 2) & 0x40 // CLK
| (~byte << 1) & 0x10; // ATN
}
else
{
byte = ~pra & ddra;
the_vic->ChangedVA(byte & 3);
old_lines = IECLines;
IECLines = (byte << 2) & 0x80 // DATA
| (byte << 2) & 0x40 // CLK
| (byte << 1) & 0x10; // ATN
}
if ((IECLines ^ old_lines) & 0x10) { // ATN changed
the_cpu_1541->NewATNState();
if (old_lines & 0x10) // ATN 1->0
the_cpu_1541->IECInterrupt();
}
break;
}
case 0x1: prb = byte; break;
case 0x2:
ddra = byte;
the_vic->ChangedVA(~(pra | ~ddra) & 3);
break;
case 0x3: ddrb = byte; break;
case 0x4: latcha = (latcha & 0xff00) | byte; break;
case 0x5:
latcha = (latcha & 0xff) | (byte << 8);
if (!(cra & 1)) // Reload timer if stopped
ta = latcha;
break;
case 0x6: latchb = (latchb & 0xff00) | byte; break;
case 0x7:
latchb = (latchb & 0xff) | (byte << 8);
if (!(crb & 1)) // Reload timer if stopped
tb = latchb;
break;
case 0x8:
if (crb & 0x80)
alm_10ths = byte & 0x0f;
else
tod_10ths = byte & 0x0f;
break;
case 0x9:
if (crb & 0x80)
alm_sec = byte & 0x7f;
else
tod_sec = byte & 0x7f;
break;
case 0xa:
if (crb & 0x80)
alm_min = byte & 0x7f;
else
tod_min = byte & 0x7f;
break;
case 0xb:
if (crb & 0x80)
alm_hr = byte & 0x9f;
else
tod_hr = byte & 0x9f;
break;
case 0xc:
sdr = byte;
TriggerInterrupt(8); // Fake SDR interrupt for programs that need it
break;
case 0xd:
if(ThePrefs.SingleCycleEmulation)
{
if (byte & 0x80)
int_mask |= byte & 0x7f;
else
int_mask &= ~byte;
if (icr & int_mask & 0x1f) { // Trigger NMI if pending
icr |= 0x80;
the_cpu->TriggerNMI();
}
}
else
{
if (ThePrefs.CIAIRQHack)
icr = 0;
if (byte & 0x80) {
int_mask |= byte & 0x7f;
if (icr & int_mask & 0x1f) { // Trigger NMI if pending
icr |= 0x80;
the_cpu->TriggerNMI();
}
} else
int_mask &= ~byte;
}
break;
case 0xe:
if(ThePrefs.SingleCycleEmulation)
{
UpdateTATB(true);
has_new_cra = true; // Delay write by 1 cycle
new_cra = byte;
ta_cnt_phi2 = ((byte & 0x20) == 0x00);
}
else
{
cra = byte & 0xef;
if (byte & 0x10) // Force load
ta = latcha;
ta_cnt_phi2 = ((byte & 0x21) == 0x01);
}
break;
case 0xf:
if(ThePrefs.SingleCycleEmulation)
{
UpdateTATB(true);
has_new_crb = true; // Delay write by 1 cycle
new_crb = byte;
tb_cnt_phi2 = ((byte & 0x60) == 0x00);
tb_cnt_ta = ((byte & 0x60) == 0x40);
}
else
{
crb = byte & 0xef;
if (byte & 0x10) // Force load
tb = latchb;
tb_cnt_phi2 = ((byte & 0x61) == 0x01);
tb_cnt_ta = ((byte & 0x61) == 0x41);
}
break;
}
}
inline void MOS6526::UpdateTATB(bool ResetCyclesTillAction)
{
uint16 ElapsedCycles = CyclesTillAction - CyclesTillActionCnt;
if(ElapsedCycles > 0)
{
// Check, if we have to update ta
if(ta_cnt_phi2 && ta_state == T_COUNT && ta > 0)
ta = ta - ElapsedCycles;
// Check, if we have to update tb
if(tb_cnt_phi2 && tb_state == T_COUNT && tb > 0)
tb = tb - ElapsedCycles;
}
if(ResetCyclesTillAction)
{
// Called from WriteRegister -> next action after one cycle
CyclesTillAction = 1;
CyclesTillActionCnt = 1;
}
else
CyclesTillAction = CyclesTillActionCnt;
}
void MOS6526::EmulateCycles(void)
{
bool ta_underflow = false;
// Trigger pending interrupts
if (ta_irq_next_cycle)
{
ta_irq_next_cycle = false;
TriggerInterrupt(1);
}
if (tb_irq_next_cycle)
{
tb_irq_next_cycle = false;
TriggerInterrupt(2);
}
// Timer A state machine
switch (ta_state)
{
case T_WAIT_THEN_COUNT:
ta_state = T_COUNT; // fall through
case T_STOP:
goto ta_idle;
case T_LOAD_THEN_STOP:
ta_state = T_STOP;
ta = latcha; // Reload timer
goto ta_idle;
case T_LOAD_THEN_COUNT:
ta_state = T_COUNT;
ta = latcha; // Reload timer
goto ta_idle;
case T_LOAD_THEN_WAIT_THEN_COUNT:
ta_state = T_WAIT_THEN_COUNT;
if (ta == 1)
goto ta_interrupt; // Interrupt if timer == 1
else
{
ta = latcha; // Reload timer
goto ta_idle;
}
case T_COUNT:
goto ta_count;
case T_COUNT_THEN_STOP:
ta_state = T_STOP;
goto ta_count;
}
// Count timer A
ta_count:
if (ta_cnt_phi2)
{
if(ta > 0)
ta = ta - (CyclesTillAction - CyclesTillActionCnt);
if (!ta)
{ // underflow?
if (ta_state != T_STOP)
{
ta_interrupt:
ta = latcha; // Reload timer
ta_irq_next_cycle = true; // Trigger interrupt in next cycle
icr |= 1; // But set ICR bit now
if (cra & 8) { // One-shot?
cra &= 0xfe; // Yes, stop timer
new_cra &= 0xfe;
ta_state = T_LOAD_THEN_STOP; // Reload in next cycle
} else
ta_state = T_LOAD_THEN_COUNT; // No, delay one cycle (and reload)
}
ta_underflow = true;
}
}
// Delayed write to CRA?
ta_idle:
if (has_new_cra)
{
switch (ta_state)
{
case T_STOP:
case T_LOAD_THEN_STOP:
if (new_cra & 1) { // Timer started, wasn't running
if (new_cra & 0x10) // Force load
ta_state = T_LOAD_THEN_WAIT_THEN_COUNT;
else // No force load
ta_state = T_WAIT_THEN_COUNT;
} else { // Timer stopped, was already stopped
if (new_cra & 0x10) // Force load
ta_state = T_LOAD_THEN_STOP;
}
break;
case T_COUNT:
if (new_cra & 1) { // Timer started, was already running
if (new_cra & 0x10) // Force load
ta_state = T_LOAD_THEN_WAIT_THEN_COUNT;
} else { // Timer stopped, was running
if (new_cra & 0x10) // Force load
ta_state = T_LOAD_THEN_STOP;
else // No force load
ta_state = T_COUNT_THEN_STOP;
}
break;
case T_LOAD_THEN_COUNT:
case T_WAIT_THEN_COUNT:
if (new_cra & 1) {
if (new_cra & 8) { // One-shot?
new_cra &= 0xfe; // Yes, stop timer
ta_state = T_STOP;
} else if (new_cra & 0x10) // Force load
ta_state = T_LOAD_THEN_WAIT_THEN_COUNT;
} else {
ta_state = T_STOP;
}
break;
}
cra = new_cra & 0xef;
has_new_cra = false;
}
// Timer B state machine
switch (tb_state)
{
case T_WAIT_THEN_COUNT:
tb_state = T_COUNT; // fall through
case T_STOP:
goto tb_idle;
case T_LOAD_THEN_STOP:
tb_state = T_STOP;
tb = latchb; // Reload timer
goto tb_idle;
case T_LOAD_THEN_COUNT:
tb_state = T_COUNT;
tb = latchb; // Reload timer
goto tb_idle;
case T_LOAD_THEN_WAIT_THEN_COUNT:
tb_state = T_WAIT_THEN_COUNT;
if (tb == 1)
goto tb_interrupt; // Interrupt if timer == 1
else {
tb = latchb; // Reload timer
goto tb_idle;
}
case T_COUNT:
goto tb_count;
case T_COUNT_THEN_STOP:
tb_state = T_STOP;
goto tb_count;
}
// Count timer B
tb_count:
if (tb_cnt_phi2 || (tb_cnt_ta && ta_underflow))
{
if(tb > 0)
{
if(tb_cnt_phi2)
tb = tb - (CyclesTillAction - CyclesTillActionCnt);
else
--tb;
}
if (!tb)
{ // underflow?
if (tb_state != T_STOP)
{
tb_interrupt:
tb = latchb; // Reload timer
tb_irq_next_cycle = true; // Trigger interrupt in next cycle
icr |= 2; // But set ICR bit now
if (crb & 8) { // One-shot?
crb &= 0xfe; // Yes, stop timer
new_crb &= 0xfe;
tb_state = T_LOAD_THEN_STOP; // Reload in next cycle
} else
tb_state = T_LOAD_THEN_COUNT; // No, delay one cycle (and reload)
}
}
}
// Delayed write to CRB?
tb_idle:
if (has_new_crb)
{
switch (tb_state)
{
case T_STOP:
case T_LOAD_THEN_STOP:
if (new_crb & 1) { // Timer started, wasn't running
if (new_crb & 0x10) // Force load
tb_state = T_LOAD_THEN_WAIT_THEN_COUNT;
else // No force load
tb_state = T_WAIT_THEN_COUNT;
} else { // Timer stopped, was already stopped
if (new_crb & 0x10) // Force load
tb_state = T_LOAD_THEN_STOP;
}
break;
case T_COUNT:
if (new_crb & 1) { // Timer started, was already running
if (new_crb & 0x10) // Force load
tb_state = T_LOAD_THEN_WAIT_THEN_COUNT;
} else { // Timer stopped, was running
if (new_crb & 0x10) // Force load
tb_state = T_LOAD_THEN_STOP;
else // No force load
tb_state = T_COUNT_THEN_STOP;
}
break;
case T_LOAD_THEN_COUNT:
case T_WAIT_THEN_COUNT:
if (new_crb & 1) {
if (new_crb & 8) { // One-shot?
new_crb &= 0xfe; // Yes, stop timer
tb_state = T_STOP;
} else if (new_crb & 0x10) // Force load
tb_state = T_LOAD_THEN_WAIT_THEN_COUNT;
} else {
tb_state = T_STOP;
}
break;
}
crb = new_crb & 0xef;
has_new_crb = false;
}
// Calculate cycles till next action
CyclesTillAction = 65535;
if(ta_state == T_COUNT)
{
if(ta_cnt_phi2 && ta > 0)
CyclesTillAction = ta;
else
CyclesTillAction = 1;
}
if((ta_state != T_COUNT && ta_state != T_STOP)
|| (tb_state != T_COUNT && tb_state != T_STOP)
|| ta_irq_next_cycle || tb_irq_next_cycle)
CyclesTillAction = 1;
if(tb_state == T_COUNT)
{
if(tb_cnt_phi2 && tb > 0)
{
if(tb < CyclesTillAction)
CyclesTillAction = tb;
}
else
CyclesTillAction = 1;
}
//CyclesTillAction = 1;
CyclesTillActionCnt = CyclesTillAction;
}
/*
* Count CIA TOD clock (called during VBlank)
*/
void MOS6526::CountTOD(void)
{
uint8 lo, hi;
// Decrement frequency divider
if (tod_divider)
tod_divider--;
else {
// Reload divider according to 50/60 Hz flag
if (cra & 0x80)
tod_divider = 4;
else
tod_divider = 5;
// 1/10 seconds
tod_10ths++;
if (tod_10ths > 9) {
tod_10ths = 0;
// Seconds
lo = (tod_sec & 0x0f) + 1;
hi = tod_sec >> 4;
if (lo > 9) {
lo = 0;
hi++;