-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mikie.h
1768 lines (1583 loc) · 50.5 KB
/
Mikie.h
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
//
// Copyright (c) 2004 K. Wilkins
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from
// the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//////////////////////////////////////////////////////////////////////////////
// Handy - An Atari Lynx Emulator //
// Copyright (c) 1996,1997 //
// K. Wilkins //
//////////////////////////////////////////////////////////////////////////////
// Mikey class header file //
//////////////////////////////////////////////////////////////////////////////
// //
// This header file provides the interface definition and some of the code //
// for the Mikey chip within the Lynx. The most crucial code is the //
// Update() function which as you can probably guess updates all of the //
// Mikey hardware counters and screen DMA from the prevous time it was //
// called. Yes I know how to spell Mikey but I cant be bothered to change //
// it everywhere. //
// //
// K. Wilkins //
// August 1997 //
// //
//////////////////////////////////////////////////////////////////////////////
// Revision History: //
// ----------------- //
// //
// 01Aug1997 KW Document header added & class documented. //
// //
//////////////////////////////////////////////////////////////////////////////
#ifndef MIKIE_H
#define MIKIE_H
//#include <crtdbg.h>
//#define TRACE_MIKIE
#ifdef TRACE_MIKIE
#define TRACE_MIKIE0(msg) _RPT1(_CRT_WARN,"CMikie::"msg" (Time=%012d)\n",gSystemCycleCount)
#define TRACE_MIKIE1(msg,arg1) _RPT2(_CRT_WARN,"CMikie::"msg" (Time=%012d)\n",arg1,gSystemCycleCount)
#define TRACE_MIKIE2(msg,arg1,arg2) _RPT3(_CRT_WARN,"CMikie::"msg" (Time=%012d)\n",arg1,arg2,gSystemCycleCount)
#define TRACE_MIKIE3(msg,arg1,arg2,arg3) _RPT4(_CRT_WARN,"CMikie::"msg" (Time=%012d)\n",arg1,arg2,arg3,gSystemCycleCount)
#else
#define TRACE_MIKIE0(msg)
#define TRACE_MIKIE1(msg,arg1)
#define TRACE_MIKIE2(msg,arg1,arg2)
#define TRACE_MIKIE3(msg,arg1,arg2,arg3)
#endif
class CSystem;
#define MIKIE_START 0xfd00
#define MIKIE_SIZE 0x100
//
// Define counter types and defines
//
#define CTRL_A_IRQEN 0x80
#define CTRL_A_RTD 0x40
#define CTRL_A_RELOAD 0x10
#define CTRL_A_COUNT 0x08
#define CTRL_A_DIVIDE 0x07
#define CTRL_B_TDONE 0x08
#define CTRL_B_LASTCK 0x04
#define CTRL_B_CIN 0x02
#define CTRL_B_COUT 0x01
#define LINE_TIMER 0x00
#define SCREEN_TIMER 0x02
#define LINE_WIDTH 160
#define LINE_SIZE 80
#define UART_TX_INACTIVE 0x80000000
#define UART_RX_INACTIVE 0x80000000
#define UART_BREAK_CODE 0x00008000
#define UART_MAX_RX_QUEUE 32
#define UART_TX_TIME_PERIOD (11)
#define UART_RX_TIME_PERIOD (11)
#define UART_RX_NEXT_DELAY (44)
typedef struct
{
UBYTE backup;
UBYTE count;
UBYTE controlA;
UBYTE controlB;
bool linkedlastcarry;
}MTIMER;
typedef struct
{
union
{
struct
{
#ifdef MSB_FIRST
UBYTE unused:4;
UBYTE Colour:1;
UBYTE FourColour:1;
UBYTE Flip:1;
UBYTE DMAEnable:1;
#else
UBYTE DMAEnable:1;
UBYTE Flip:1;
UBYTE FourColour:1;
UBYTE Colour:1;
UBYTE unused:4;
#endif
}Bits;
UBYTE Byte;
};
}TDISPCTL;
typedef struct
{
union
{
struct
{
#ifdef MSB_FIRST
UBYTE unused:8;
UBYTE unused2:8;
UBYTE unused3:4;
UBYTE Blue:4;
UBYTE Red:4;
UBYTE Green:4;
#else
UBYTE Green:4;
UBYTE Red:4;
UBYTE Blue:4;
#endif
}Colours;
ULONG Index;
};
}TPALETTE;
//
// Emumerated types for possible mikie windows independant modes
//
enum
{
MIKIE_BAD_MODE=0,
MIKIE_NO_ROTATE,
MIKIE_ROTATE_L,
MIKIE_ROTATE_R
};
enum
{
MIKIE_PIXEL_FORMAT_8BPP=0,
MIKIE_PIXEL_FORMAT_16BPP_555,
MIKIE_PIXEL_FORMAT_16BPP_5551,
MIKIE_PIXEL_FORMAT_16BPP_565,
MIKIE_PIXEL_FORMAT_24BPP,
MIKIE_PIXEL_FORMAT_32BPP,
};
class CMikie : public CLynxBase
{
public:
CMikie(CSystem& parent);
~CMikie();
bool ContextSave(FILE *fp);
bool ContextLoad(LSS_FILE *fp);
void Reset(void);
UBYTE Peek(ULONG addr);
void Poke(ULONG addr,UBYTE data);
ULONG ReadCycle(void) {return 5;};
ULONG WriteCycle(void) {return 5;};
ULONG ObjectSize(void) {return MIKIE_SIZE;};
void PresetForHomebrew(void);
ULONG GetLfsrNext(ULONG current);
void ComLynxCable(int status);
void ComLynxRxData(int data);
void ComLynxTxLoopback(int data);
void ComLynxTxCallback(void (*function)(int data,ULONG objref),ULONG objref);
void DisplaySetAttributes(ULONG Rotate, ULONG Format, ULONG Pitch, UBYTE* (*DisplayCallback)(ULONG objref),ULONG objref);
ULONG DisplayGetRotation() { return mDisplayRotate; }
void BlowOut(void);
ULONG DisplayRenderLine(void);
ULONG DisplayEndOfFrame(void);
inline void SetCPUSleep(void) {gSystemCPUSleep=TRUE;};
inline void ClearCPUSleep(void) {gSystemCPUSleep=FALSE;gSystemCPUSleep_Saved=FALSE;};
inline void Update(void)
{
SLONG divide;
SLONG decval;
ULONG tmp;
ULONG mikie_work_done=0;
//
// To stop problems with cycle count wrap we will check and then correct the
// cycle counter.
//
// TRACE_MIKIE0("Update()");
if(gSystemCycleCount>0xf0000000)
{
gSystemCycleCount-=0x80000000;
gThrottleNextCycleCheckpoint-=0x80000000;
gAudioLastUpdateCycle-=0x80000000;
mTIM_0_LAST_COUNT-=0x80000000;
mTIM_1_LAST_COUNT-=0x80000000;
mTIM_2_LAST_COUNT-=0x80000000;
mTIM_3_LAST_COUNT-=0x80000000;
mTIM_4_LAST_COUNT-=0x80000000;
mTIM_5_LAST_COUNT-=0x80000000;
mTIM_6_LAST_COUNT-=0x80000000;
mTIM_7_LAST_COUNT-=0x80000000;
mAUDIO_0_LAST_COUNT-=0x80000000;
mAUDIO_1_LAST_COUNT-=0x80000000;
mAUDIO_2_LAST_COUNT-=0x80000000;
mAUDIO_3_LAST_COUNT-=0x80000000;
// Only correct if sleep is active
if(gCPUWakeupTime)
{
gCPUWakeupTime-=0x80000000;
gIRQEntryCycle-=0x80000000;
}
}
gNextTimerEvent=0xffffffff;
//
// Check if the CPU needs to be woken up from sleep mode
//
if(gCPUWakeupTime)
{
if(gSystemCycleCount>=gCPUWakeupTime)
{
TRACE_MIKIE0("*********************************************************");
TRACE_MIKIE0("**** CPU SLEEP COMPLETED ****");
TRACE_MIKIE0("*********************************************************");
ClearCPUSleep();
gCPUWakeupTime=0;
}
else
{
if(gCPUWakeupTime>gSystemCycleCount) gNextTimerEvent=gCPUWakeupTime;
}
}
// Timer updates, rolled out flat in group order
//
// Group A:
// Timer 0 -> Timer 2 -> Timer 4.
//
// Group B:
// Timer 1 -> Timer 3 -> Timer 5 -> Timer 7 -> Audio 0 -> Audio 1-> Audio 2 -> Audio 3 -> Timer 1.
//
//
// Within each timer code block we will predict the cycle count number of
// the next timer event
//
// We don't need to count linked timers as the timer they are linked
// from will always generate earlier events.
//
// As Timer 4 (UART) will generate many events we will ignore it
//
// We set the next event to the end of time at first and let the timers
// overload it. Any writes to timer controls will force next event to
// be immediate and hence a new preidction will be done. The prediction
// causes overflow as opposed to zero i.e. current+1
// (In reality T0 line counter should always be running.)
//
//
// Timer 0 of Group A
//
//
// Optimisation, assume T0 (Line timer) is never in one-shot,
// never placed in link mode
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
// if(mTIM_0_ENABLE_COUNT && (mTIM_0_ENABLE_RELOAD || !mTIM_0_TIMER_DONE))
if(mTIM_0_ENABLE_COUNT)
{
// Timer 0 has no linking
// if(mTIM_0_LINKING!=0x07)
{
// Ordinary clocked mode as opposed to linked mode
// 16MHz clock downto 1us == cyclecount >> 4
divide=(4+mTIM_0_LINKING);
decval=(gSystemCycleCount-mTIM_0_LAST_COUNT)>>divide;
if(decval)
{
mTIM_0_LAST_COUNT+=decval<<divide;
mTIM_0_CURRENT-=decval;
if(mTIM_0_CURRENT&0x80000000)
{
// Set carry out
mTIM_0_BORROW_OUT=TRUE;
// // Reload if neccessary
// if(mTIM_0_ENABLE_RELOAD)
// {
mTIM_0_CURRENT+=mTIM_0_BKUP+1;
// }
// else
// {
// mTIM_0_CURRENT=0;
// }
mTIM_0_TIMER_DONE=TRUE;
// Interupt flag setting code moved into DisplayRenderLine()
// Line timer has expired, render a line, we cannot incrememnt
// the global counter at this point as it will screw the other timers
// so we save under work done and inc at the end.
mikie_work_done+=DisplayRenderLine();
}
else
{
mTIM_0_BORROW_OUT=FALSE;
}
// Set carry in as we did a count
mTIM_0_BORROW_IN=TRUE;
}
else
{
// Clear carry in as we didn't count
mTIM_0_BORROW_IN=FALSE;
// Clear carry out
mTIM_0_BORROW_OUT=FALSE;
}
}
// Prediction for next timer event cycle number
// if(mTIM_0_LINKING!=7)
{
// Sometimes timeupdates can be >2x rollover in which case
// then CURRENT may still be negative and we can use it to
// calc the next timer value, we just want another update ASAP
tmp=(mTIM_0_CURRENT&0x80000000)?1:((mTIM_0_CURRENT+1)<<divide);
tmp+=gSystemCycleCount;
if(tmp<gNextTimerEvent)
{
gNextTimerEvent=tmp;
// TRACE_MIKIE1("Update() - TIMER 0 Set NextTimerEvent = %012d",gNextTimerEvent);
}
}
// TRACE_MIKIE1("Update() - mTIM_0_CURRENT = %012d",mTIM_0_CURRENT);
// TRACE_MIKIE1("Update() - mTIM_0_BKUP = %012d",mTIM_0_BKUP);
// TRACE_MIKIE1("Update() - mTIM_0_LASTCNT = %012d",mTIM_0_LAST_COUNT);
// TRACE_MIKIE1("Update() - mTIM_0_LINKING = %012d",mTIM_0_LINKING);
}
//
// Timer 2 of Group A
//
//
// Optimisation, assume T2 (Frame timer) is never in one-shot
// always in linked mode i.e clocked by Line Timer
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
// if(mTIM_2_ENABLE_COUNT && (mTIM_2_ENABLE_RELOAD || !mTIM_2_TIMER_DONE))
if(mTIM_2_ENABLE_COUNT)
{
decval=0;
// if(mTIM_2_LINKING==0x07)
{
if(mTIM_0_BORROW_OUT) decval=1;
mTIM_2_LAST_LINK_CARRY=mTIM_0_BORROW_OUT;
}
// else
// {
// // Ordinary clocked mode as opposed to linked mode
// // 16MHz clock downto 1us == cyclecount >> 4
// divide=(4+mTIM_2_LINKING);
// decval=(gSystemCycleCount-mTIM_2_LAST_COUNT)>>divide;
// }
if(decval)
{
// mTIM_2_LAST_COUNT+=decval<<divide;
mTIM_2_CURRENT-=decval;
if(mTIM_2_CURRENT&0x80000000)
{
// Set carry out
mTIM_2_BORROW_OUT=TRUE;
// // Reload if neccessary
// if(mTIM_2_ENABLE_RELOAD)
// {
mTIM_2_CURRENT+=mTIM_2_BKUP+1;
// }
// else
// {
// mTIM_2_CURRENT=0;
// }
mTIM_2_TIMER_DONE=TRUE;
// Interupt flag setting code moved into DisplayEndOfFrame(), also
// park any CPU cycles lost for later inclusion
mikie_work_done+=DisplayEndOfFrame();
}
else
{
mTIM_2_BORROW_OUT=FALSE;
}
// Set carry in as we did a count
mTIM_2_BORROW_IN=TRUE;
}
else
{
// Clear carry in as we didn't count
mTIM_2_BORROW_IN=FALSE;
// Clear carry out
mTIM_2_BORROW_OUT=FALSE;
}
// Prediction for next timer event cycle number
// We dont need to predict this as its the frame timer and will always
// be beaten by the line timer on Timer 0
// if(mTIM_2_LINKING!=7)
// {
// tmp=gSystemCycleCount+((mTIM_2_CURRENT+1)<<divide);
// if(tmp<gNextTimerEvent) gNextTimerEvent=tmp;
// }
// TRACE_MIKIE1("Update() - mTIM_2_CURRENT = %012d",mTIM_2_CURRENT);
// TRACE_MIKIE1("Update() - mTIM_2_BKUP = %012d",mTIM_2_BKUP);
// TRACE_MIKIE1("Update() - mTIM_2_LASTCNT = %012d",mTIM_2_LAST_COUNT);
// TRACE_MIKIE1("Update() - mTIM_2_LINKING = %012d",mTIM_2_LINKING);
}
//
// Timer 4 of Group A
//
// For the sake of speed it is assumed that Timer 4 (UART timer)
// never uses one-shot mode, never uses linking, hence the code
// is commented out. Timer 4 is at the end of a chain and seems
// no reason to update its carry in-out variables
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
// if(mTIM_4_ENABLE_COUNT && (mTIM_4_ENABLE_RELOAD || !mTIM_4_TIMER_DONE))
if(mTIM_4_ENABLE_COUNT)
{
decval=0;
// if(mTIM_4_LINKING==0x07)
// {
//// if(mTIM_2_BORROW_OUT && !mTIM_4_LAST_LINK_CARRY) decval=1;
// if(mTIM_2_BORROW_OUT) decval=1;
// mTIM_4_LAST_LINK_CARRY=mTIM_2_BORROW_OUT;
// }
// else
{
// Ordinary clocked mode as opposed to linked mode
// 16MHz clock downto 1us == cyclecount >> 4
// Additional /8 (+3) for 8 clocks per bit transmit
divide=4+3+mTIM_4_LINKING;
decval=(gSystemCycleCount-mTIM_4_LAST_COUNT)>>divide;
}
if(decval)
{
mTIM_4_LAST_COUNT+=decval<<divide;
mTIM_4_CURRENT-=decval;
if(mTIM_4_CURRENT&0x80000000)
{
// Set carry out
mTIM_4_BORROW_OUT=TRUE;
//
// Update the UART counter models for Rx & Tx
//
//
// According to the docs IRQ's are level triggered and hence will always assert
// what a pain in the arse
//
// Rx & Tx are loopedback due to comlynx structure
//
// Receive
//
if(!mUART_RX_COUNTDOWN)
{
// Fetch a byte from the input queue
if(mUART_Rx_waiting>0)
{
mUART_RX_DATA=mUART_Rx_input_queue[mUART_Rx_output_ptr];
mUART_Rx_output_ptr=(++mUART_Rx_output_ptr)%UART_MAX_RX_QUEUE;
mUART_Rx_waiting--;
TRACE_MIKIE2("Update() - RX Byte output ptr=%02d waiting=%02d",mUART_Rx_output_ptr,mUART_Rx_waiting);
}
else
{
TRACE_MIKIE0("Update() - RX Byte but no data waiting ????");
}
// Retrigger input if more bytes waiting
if(mUART_Rx_waiting>0)
{
mUART_RX_COUNTDOWN=UART_RX_TIME_PERIOD+UART_RX_NEXT_DELAY;
TRACE_MIKIE1("Update() - RX Byte retriggered, %d waiting",mUART_Rx_waiting);
}
else
{
mUART_RX_COUNTDOWN=UART_RX_INACTIVE;
TRACE_MIKIE0("Update() - RX Byte nothing waiting, deactivated");
}
// If RX_READY already set then we have an overrun
// as previous byte hasnt been read
if(mUART_RX_READY) mUART_Rx_overun_error=1;
// Flag byte as being recvd
mUART_RX_READY=1;
}
else if(!(mUART_RX_COUNTDOWN&UART_RX_INACTIVE))
{
mUART_RX_COUNTDOWN--;
}
if(!mUART_TX_COUNTDOWN)
{
if(mUART_SENDBREAK)
{
mUART_TX_DATA=UART_BREAK_CODE;
// Auto-Respawn new transmit
mUART_TX_COUNTDOWN=UART_TX_TIME_PERIOD;
// Loop back what we transmitted
ComLynxTxLoopback(mUART_TX_DATA);
}
else
{
// Serial activity finished
mUART_TX_COUNTDOWN=UART_TX_INACTIVE;
}
// If a networking object is attached then use its callback to send the data byte.
if(mpUART_TX_CALLBACK)
{
TRACE_MIKIE0("Update() - UART_TX_CALLBACK");
(*mpUART_TX_CALLBACK)(mUART_TX_DATA,mUART_TX_CALLBACK_OBJECT);
}
}
else if(!(mUART_TX_COUNTDOWN&UART_TX_INACTIVE))
{
mUART_TX_COUNTDOWN--;
}
// Set the timer status flag
// Timer 4 is the uart timer and doesn't generate IRQ's using this method
// 16 Clocks = 1 bit transmission. Hold separate Rx & Tx counters
// Reload if neccessary
// if(mTIM_4_ENABLE_RELOAD)
// {
mTIM_4_CURRENT+=mTIM_4_BKUP+1;
// The low reload values on TIM4 coupled with a longer
// timer service delay can sometimes cause
// an underun, check and fix
if(mTIM_4_CURRENT&0x80000000)
{
mTIM_4_CURRENT=mTIM_4_BKUP;
mTIM_4_LAST_COUNT=gSystemCycleCount;
}
// }
// else
// {
// mTIM_4_CURRENT=0;
// }
// mTIM_4_TIMER_DONE=TRUE;
}
// else
// {
// mTIM_4_BORROW_OUT=FALSE;
// }
// // Set carry in as we did a count
// mTIM_4_BORROW_IN=TRUE;
}
// else
// {
// // Clear carry in as we didn't count
// mTIM_4_BORROW_IN=FALSE;
// // Clear carry out
// mTIM_4_BORROW_OUT=FALSE;
// }
//
// // Prediction for next timer event cycle number
//
// if(mTIM_4_LINKING!=7)
// {
// Sometimes timeupdates can be >2x rollover in which case
// then CURRENT may still be negative and we can use it to
// calc the next timer value, we just want another update ASAP
tmp=(mTIM_4_CURRENT&0x80000000)?1:((mTIM_4_CURRENT+1)<<divide);
tmp+=gSystemCycleCount;
if(tmp<gNextTimerEvent)
{
gNextTimerEvent=tmp;
TRACE_MIKIE1("Update() - TIMER 4 Set NextTimerEvent = %012d",gNextTimerEvent);
}
// }
// TRACE_MIKIE1("Update() - mTIM_4_CURRENT = %012d",mTIM_4_CURRENT);
// TRACE_MIKIE1("Update() - mTIM_4_BKUP = %012d",mTIM_4_BKUP);
// TRACE_MIKIE1("Update() - mTIM_4_LASTCNT = %012d",mTIM_4_LAST_COUNT);
// TRACE_MIKIE1("Update() - mTIM_4_LINKING = %012d",mTIM_4_LINKING);
}
// Emulate the UART bug where UART IRQ is level sensitive
// in that it will continue to generate interrupts as long
// as they are enabled and the interrupt condition is true
// If Tx is inactive i.e ready for a byte to eat and the
// IRQ is enabled then generate it always
if((mUART_TX_COUNTDOWN&UART_TX_INACTIVE) && mUART_TX_IRQ_ENABLE)
{
TRACE_MIKIE0("Update() - UART TX IRQ Triggered");
mTimerStatusFlags|=0x10;
gSystemIRQ=TRUE; // Added 19/09/06 fix for IRQ issue
}
// Is data waiting and the interrupt enabled, if so then
// what are we waiting for....
if(mUART_RX_READY && mUART_RX_IRQ_ENABLE)
{
TRACE_MIKIE0("Update() - UART RX IRQ Triggered");
mTimerStatusFlags|=0x10;
gSystemIRQ=TRUE; // Added 19/09/06 fix for IRQ issue
}
//
// Timer 1 of Group B
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
if(mTIM_1_ENABLE_COUNT && (mTIM_1_ENABLE_RELOAD || !mTIM_1_TIMER_DONE))
{
if(mTIM_1_LINKING!=0x07)
{
// Ordinary clocked mode as opposed to linked mode
// 16MHz clock downto 1us == cyclecount >> 4
divide=(4+mTIM_1_LINKING);
decval=(gSystemCycleCount-mTIM_1_LAST_COUNT)>>divide;
if(decval)
{
mTIM_1_LAST_COUNT+=decval<<divide;
mTIM_1_CURRENT-=decval;
if(mTIM_1_CURRENT&0x80000000)
{
// Set carry out
mTIM_1_BORROW_OUT=TRUE;
// Set the timer status flag
if(mTimerInterruptMask&0x02)
{
TRACE_MIKIE0("Update() - TIMER1 IRQ Triggered");
mTimerStatusFlags|=0x02;
gSystemIRQ=TRUE; // Added 19/09/06 fix for IRQ issue
}
// Reload if neccessary
if(mTIM_1_ENABLE_RELOAD)
{
mTIM_1_CURRENT+=mTIM_1_BKUP+1;
}
else
{
mTIM_1_CURRENT=0;
}
mTIM_1_TIMER_DONE=TRUE;
}
else
{
mTIM_1_BORROW_OUT=FALSE;
}
// Set carry in as we did a count
mTIM_1_BORROW_IN=TRUE;
}
else
{
// Clear carry in as we didn't count
mTIM_1_BORROW_IN=FALSE;
// Clear carry out
mTIM_1_BORROW_OUT=FALSE;
}
}
// Prediction for next timer event cycle number
if(mTIM_1_LINKING!=7)
{
// Sometimes timeupdates can be >2x rollover in which case
// then CURRENT may still be negative and we can use it to
// calc the next timer value, we just want another update ASAP
tmp=(mTIM_1_CURRENT&0x80000000)?1:((mTIM_1_CURRENT+1)<<divide);
tmp+=gSystemCycleCount;
if(tmp<gNextTimerEvent)
{
gNextTimerEvent=tmp;
TRACE_MIKIE1("Update() - TIMER 1 Set NextTimerEvent = %012d",gNextTimerEvent);
}
}
// TRACE_MIKIE1("Update() - mTIM_1_CURRENT = %012d",mTIM_1_CURRENT);
// TRACE_MIKIE1("Update() - mTIM_1_BKUP = %012d",mTIM_1_BKUP);
// TRACE_MIKIE1("Update() - mTIM_1_LASTCNT = %012d",mTIM_1_LAST_COUNT);
// TRACE_MIKIE1("Update() - mTIM_1_LINKING = %012d",mTIM_1_LINKING);
}
//
// Timer 3 of Group A
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
if(mTIM_3_ENABLE_COUNT && (mTIM_3_ENABLE_RELOAD || !mTIM_3_TIMER_DONE))
{
decval=0;
if(mTIM_3_LINKING==0x07)
{
if(mTIM_1_BORROW_OUT) decval=1;
mTIM_3_LAST_LINK_CARRY=mTIM_1_BORROW_OUT;
}
else
{
// Ordinary clocked mode as opposed to linked mode
// 16MHz clock downto 1us == cyclecount >> 4
divide=(4+mTIM_3_LINKING);
decval=(gSystemCycleCount-mTIM_3_LAST_COUNT)>>divide;
}
if(decval)
{
mTIM_3_LAST_COUNT+=decval<<divide;
mTIM_3_CURRENT-=decval;
if(mTIM_3_CURRENT&0x80000000)
{
// Set carry out
mTIM_3_BORROW_OUT=TRUE;
// Set the timer status flag
if(mTimerInterruptMask&0x08)
{
TRACE_MIKIE0("Update() - TIMER3 IRQ Triggered");
mTimerStatusFlags|=0x08;
gSystemIRQ=TRUE; // Added 19/09/06 fix for IRQ issue
}
// Reload if neccessary
if(mTIM_3_ENABLE_RELOAD)
{
mTIM_3_CURRENT+=mTIM_3_BKUP+1;
}
else
{
mTIM_3_CURRENT=0;
}
mTIM_3_TIMER_DONE=TRUE;
}
else
{
mTIM_3_BORROW_OUT=FALSE;
}
// Set carry in as we did a count
mTIM_3_BORROW_IN=TRUE;
}
else
{
// Clear carry in as we didn't count
mTIM_3_BORROW_IN=FALSE;
// Clear carry out
mTIM_3_BORROW_OUT=FALSE;
}
// Prediction for next timer event cycle number
if(mTIM_3_LINKING!=7)
{
// Sometimes timeupdates can be >2x rollover in which case
// then CURRENT may still be negative and we can use it to
// calc the next timer value, we just want another update ASAP
tmp=(mTIM_3_CURRENT&0x80000000)?1:((mTIM_3_CURRENT+1)<<divide);
tmp+=gSystemCycleCount;
if(tmp<gNextTimerEvent)
{
gNextTimerEvent=tmp;
TRACE_MIKIE1("Update() - TIMER 3 Set NextTimerEvent = %012d",gNextTimerEvent);
}
}
// TRACE_MIKIE1("Update() - mTIM_3_CURRENT = %012d",mTIM_3_CURRENT);
// TRACE_MIKIE1("Update() - mTIM_3_BKUP = %012d",mTIM_3_BKUP);
// TRACE_MIKIE1("Update() - mTIM_3_LASTCNT = %012d",mTIM_3_LAST_COUNT);
// TRACE_MIKIE1("Update() - mTIM_3_LINKING = %012d",mTIM_3_LINKING);
}
//
// Timer 5 of Group A
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
if(mTIM_5_ENABLE_COUNT && (mTIM_5_ENABLE_RELOAD || !mTIM_5_TIMER_DONE))
{
decval=0;
if(mTIM_5_LINKING==0x07)
{
if(mTIM_3_BORROW_OUT) decval=1;
mTIM_5_LAST_LINK_CARRY=mTIM_3_BORROW_OUT;
}
else
{
// Ordinary clocked mode as opposed to linked mode
// 16MHz clock downto 1us == cyclecount >> 4
divide=(4+mTIM_5_LINKING);
decval=(gSystemCycleCount-mTIM_5_LAST_COUNT)>>divide;
}
if(decval)
{
mTIM_5_LAST_COUNT+=decval<<divide;
mTIM_5_CURRENT-=decval;
if(mTIM_5_CURRENT&0x80000000)
{
// Set carry out
mTIM_5_BORROW_OUT=TRUE;
// Set the timer status flag
if(mTimerInterruptMask&0x20)
{
TRACE_MIKIE0("Update() - TIMER5 IRQ Triggered");
mTimerStatusFlags|=0x20;
gSystemIRQ=TRUE; // Added 19/09/06 fix for IRQ issue
}
// Reload if neccessary
if(mTIM_5_ENABLE_RELOAD)
{
mTIM_5_CURRENT+=mTIM_5_BKUP+1;
}
else
{
mTIM_5_CURRENT=0;
}
mTIM_5_TIMER_DONE=TRUE;
}
else
{
mTIM_5_BORROW_OUT=FALSE;
}
// Set carry in as we did a count
mTIM_5_BORROW_IN=TRUE;
}
else
{
// Clear carry in as we didn't count
mTIM_5_BORROW_IN=FALSE;
// Clear carry out
mTIM_5_BORROW_OUT=FALSE;
}
// Prediction for next timer event cycle number
if(mTIM_5_LINKING!=7)
{
// Sometimes timeupdates can be >2x rollover in which case
// then CURRENT may still be negative and we can use it to
// calc the next timer value, we just want another update ASAP
tmp=(mTIM_5_CURRENT&0x80000000)?1:((mTIM_5_CURRENT+1)<<divide);
tmp+=gSystemCycleCount;
if(tmp<gNextTimerEvent)
{
gNextTimerEvent=tmp;
TRACE_MIKIE1("Update() - TIMER 5 Set NextTimerEvent = %012d",gNextTimerEvent);
}
}
// TRACE_MIKIE1("Update() - mTIM_5_CURRENT = %012d",mTIM_5_CURRENT);
// TRACE_MIKIE1("Update() - mTIM_5_BKUP = %012d",mTIM_5_BKUP);
// TRACE_MIKIE1("Update() - mTIM_5_LASTCNT = %012d",mTIM_5_LAST_COUNT);
// TRACE_MIKIE1("Update() - mTIM_5_LINKING = %012d",mTIM_5_LINKING);
}
//
// Timer 7 of Group A
//
// KW bugfix 13/4/99 added (mTIM_x_ENABLE_RELOAD || ..)
if(mTIM_7_ENABLE_COUNT && (mTIM_7_ENABLE_RELOAD || !mTIM_7_TIMER_DONE))
{
decval=0;
if(mTIM_7_LINKING==0x07)
{
if(mTIM_5_BORROW_OUT) decval=1;
mTIM_7_LAST_LINK_CARRY=mTIM_5_BORROW_OUT;
}
else
{
// Ordinary clocked mode as opposed to linked mode
// 16MHz clock downto 1us == cyclecount >> 4
divide=(4+mTIM_7_LINKING);
decval=(gSystemCycleCount-mTIM_7_LAST_COUNT)>>divide;
}
if(decval)
{
mTIM_7_LAST_COUNT+=decval<<divide;
mTIM_7_CURRENT-=decval;
if(mTIM_7_CURRENT&0x80000000)
{
// Set carry out
mTIM_7_BORROW_OUT=TRUE;
// Set the timer status flag
if(mTimerInterruptMask&0x80)
{
TRACE_MIKIE0("Update() - TIMER7 IRQ Triggered");
mTimerStatusFlags|=0x80;
gSystemIRQ=TRUE; // Added 19/09/06 fix for IRQ issue
}
// Reload if neccessary
if(mTIM_7_ENABLE_RELOAD)
{
mTIM_7_CURRENT+=mTIM_7_BKUP+1;
}
else
{
mTIM_7_CURRENT=0;
}
mTIM_7_TIMER_DONE=TRUE;
}
else
{
mTIM_7_BORROW_OUT=FALSE;
}
// Set carry in as we did a count
mTIM_7_BORROW_IN=TRUE;
}
else
{
// Clear carry in as we didn't count
mTIM_7_BORROW_IN=FALSE;
// Clear carry out
mTIM_7_BORROW_OUT=FALSE;
}
// Prediction for next timer event cycle number
if(mTIM_7_LINKING!=7)
{
// Sometimes timeupdates can be >2x rollover in which case
// then CURRENT may still be negative and we can use it to
// calc the next timer value, we just want another update ASAP
tmp=(mTIM_7_CURRENT&0x80000000)?1:((mTIM_7_CURRENT+1)<<divide);
tmp+=gSystemCycleCount;
if(tmp<gNextTimerEvent)
{
gNextTimerEvent=tmp;
TRACE_MIKIE1("Update() - TIMER 7 Set NextTimerEvent = %012d",gNextTimerEvent);
}