-
Notifications
You must be signed in to change notification settings - Fork 22
/
hd6309.c
7490 lines (6840 loc) · 145 KB
/
hd6309.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
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 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) 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.
VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
Additional 6309 modifications by Walter ZAMBOTTI 2019
*/
#include <stdio.h>
#include <stdlib.h>
#include "hd6309.h"
#include "hd6309defs.h"
#include "tcc1014mmu.h"
#include "logger.h"
#if defined(_WIN64)
#define MSABI
#else
#define MSABI __attribute__((ms_abi))
#endif
//Global variables for CPU Emulation-----------------------
#define NTEST8(r) r>0x7F;
#define NTEST16(r) r>0x7FFF;
#define NTEST32(r) r>0x7FFFFFFF;
#define OVERFLOW8(c,a,b,r) c ^ (((a^b^r)>>7) &1);
#define OVERFLOW16(c,a,b,r) c ^ (((a^b^r)>>15)&1);
#define ZTEST(r) !r;
#define DPADDRESS(r) (dp.Reg |MemRead8(r))
#define IMMADDRESS(r) MemRead16(r)
#define INDADDRESS(r) CalculateEA(MemRead8(r))
#define M65 0
#define M64 1
#define M32 2
#define M21 3
#define M54 4
#define M97 5
#define M85 6
#define M51 7
#define M31 8
#define M1110 9
#define M76 10
#define M75 11
#define M43 12
#define M87 13
#define M86 14
#define M98 15
#define M2726 16
#define M3635 17
#define M3029 18
#define M2827 19
#define M3726 20
#define M3130 21
#define M42 22
#define M53 23
typedef union
{
unsigned short Reg;
struct
{
unsigned char lsb,msb;
} B;
} cpuregister;
typedef union
{
unsigned int Reg;
struct
{
unsigned short msw,lsw;
} Word;
struct
{
unsigned char mswlsb,mswmsb,lswlsb,lswmsb; //Might be backwards
} Byte;
} wideregister;
#define D_REG q.Word.lsw
#define W_REG q.Word.msw
#define PC_REG pc.Reg
#define X_REG x.Reg
#define Y_REG y.Reg
#define U_REG u.Reg
#define S_REG s.Reg
#define A_REG q.Byte.lswmsb
#define B_REG q.Byte.lswlsb
#define E_REG q.Byte.mswmsb
#define F_REG q.Byte.mswlsb
#define Q_REG q.Reg
#define V_REG v.Reg
#define O_REG z.Reg
static char RegName[16][10]={"D","X","Y","U","S","PC","W","V","A","B","CC","DP","ZERO","ZERO","E","F"};
static wideregister q;
static cpuregister pc, x, y, u, s, dp, v, z;
static unsigned char InsCycles[2][25];
static unsigned char cc[8];
static unsigned int md[8];
static unsigned char *ureg8[8];
static unsigned char ccbits,mdbits;
static unsigned short *xfreg16[8];
static int CycleCounter=0;
static unsigned int SyncWaiting=0;
unsigned short temp16;
static signed short stemp16;
static signed char stemp8;
static unsigned int temp32;
static int stemp32;
static unsigned char temp8;
static unsigned char PendingInterupts=0;
static unsigned char IRQWaiter=0;
static unsigned char Source=0,Dest=0;
static unsigned char postbyte=0;
static short unsigned postword=0;
static signed char *spostbyte=(signed char *)&postbyte;
static signed short *spostword=(signed short *)&postword;
static char InInterupt=0;
static int gCycleFor;
static unsigned char NatEmuCycles65 = 6;
static unsigned char NatEmuCycles64 = 6;
static unsigned char NatEmuCycles32 = 3;
static unsigned char NatEmuCycles21 = 2;
static unsigned char NatEmuCycles54 = 5;
static unsigned char NatEmuCycles97 = 9;
static unsigned char NatEmuCycles85 = 8;
static unsigned char NatEmuCycles51 = 5;
static unsigned char NatEmuCycles31 = 3;
static unsigned char NatEmuCycles1110 = 11;
static unsigned char NatEmuCycles76 = 7;
static unsigned char NatEmuCycles75 = 7;
static unsigned char NatEmuCycles43 = 4;
static unsigned char NatEmuCycles87 = 8;
static unsigned char NatEmuCycles86 = 8;
static unsigned char NatEmuCycles98 = 9;
static unsigned char NatEmuCycles2726 = 27;
static unsigned char NatEmuCycles3635 = 36;
static unsigned char NatEmuCycles3029 = 30;
static unsigned char NatEmuCycles2827 = 28;
static unsigned char NatEmuCycles3726 = 37;
static unsigned char NatEmuCycles3130 = 31;
static unsigned char NatEmuCycles42 = 4;
static unsigned char NatEmuCycles53 = 5;
static unsigned char *NatEmuCycles[] =
{
&NatEmuCycles65,
&NatEmuCycles64,
&NatEmuCycles32,
&NatEmuCycles21,
&NatEmuCycles54,
&NatEmuCycles97,
&NatEmuCycles85,
&NatEmuCycles51,
&NatEmuCycles31,
&NatEmuCycles1110,
&NatEmuCycles76,
&NatEmuCycles75,
&NatEmuCycles43,
&NatEmuCycles87,
&NatEmuCycles86,
&NatEmuCycles98,
&NatEmuCycles2726,
&NatEmuCycles3635,
&NatEmuCycles3029,
&NatEmuCycles2827,
&NatEmuCycles3726,
&NatEmuCycles3130,
&NatEmuCycles42,
&NatEmuCycles53
};
//END Global variables for CPU Emulation-------------------
//Fuction Prototypes---------------------------------------
static unsigned short CalculateEA(unsigned char);
void InvalidInsHandler(void);
void DivbyZero(void);
void ErrorVector(void);
void setcc (unsigned char);
unsigned char getcc(void);
void setmd (unsigned char);
unsigned char getmd(void);
static void cpu_firq(void);
static void cpu_irq(void);
static void cpu_nmi(void);
unsigned char GetSorceReg(unsigned char);
void Page_2(void);
void Page_3(void);
void MemWrite32(unsigned int, unsigned short);
unsigned int MemRead32(unsigned short);
// void MemWrite8(unsigned char, unsigned short);
// void MemWrite16(unsigned short, unsigned short);
// unsigned char MemRead8(unsigned short);
// unsigned short MemRead16(unsigned short);
// extern void SetNatEmuStat(unsigned char);
//unsigned char GetDestReg(unsigned char);
//END Fuction Prototypes-----------------------------------
void HD6309Reset(void)
{
char index;
for(index=0;index<=6;index++) //Set all register to 0 except V
*xfreg16[index] = 0;
for(index=0;index<=7;index++)
*ureg8[index]=0;
for(index=0;index<=7;index++)
cc[index]=0;
for(index=0;index<=7;index++)
md[index]=0;
mdbits=getmd();
dp.Reg=0;
cc[I]=1;
cc[F]=1;
SyncWaiting=0;
PC_REG=MemRead16(VRESET); //PC gets its reset vector
SetMapType(0); //shouldn't be here
return;
}
void HD6309Init(void)
{ //Call this first or RESET will core!
// reg pointers for TFR and EXG and LEA ops
xfreg16[0] = &D_REG;
xfreg16[1] = &X_REG;
xfreg16[2] = &Y_REG;
xfreg16[3] = &U_REG;
xfreg16[4] = &S_REG;
xfreg16[5] = &PC_REG;
xfreg16[6] = &W_REG;
xfreg16[7] = &V_REG;
ureg8[0]=(unsigned char*)&A_REG;
ureg8[1]=(unsigned char*)&B_REG;
ureg8[2]=(unsigned char*)&ccbits;
ureg8[3]=(unsigned char*)&dp.B.msb;
ureg8[4]=(unsigned char*)&z.B.msb;
ureg8[5]=(unsigned char*)&z.B.lsb;
ureg8[6]=(unsigned char*)&E_REG;
ureg8[7]=(unsigned char*)&F_REG;
//This handles the disparity between 6309 and 6809 Instruction timing
InsCycles[0][M65]=6; //6-5
InsCycles[1][M65]=5;
InsCycles[0][M64]=6; //6-4
InsCycles[1][M64]=4;
InsCycles[0][M32]=3; //3-2
InsCycles[1][M32]=2;
InsCycles[0][M21]=2; //2-1
InsCycles[1][M21]=1;
InsCycles[0][M54]=5; //5-4
InsCycles[1][M54]=4;
InsCycles[0][M97]=9; //9-7
InsCycles[1][M97]=7;
InsCycles[0][M85]=8; //8-5
InsCycles[1][M85]=5;
InsCycles[0][M51]=5; //5-1
InsCycles[1][M51]=1;
InsCycles[0][M31]=3; //3-1
InsCycles[1][M31]=1;
InsCycles[0][M1110]=11; //11-10
InsCycles[1][M1110]=10;
InsCycles[0][M76]=7; //7-6
InsCycles[1][M76]=6;
InsCycles[0][M75]=7; //7-5
InsCycles[1][M75]=5;
InsCycles[0][M43]=4; //4-3
InsCycles[1][M43]=3;
InsCycles[0][M87]=8; //8-7
InsCycles[1][M87]=7;
InsCycles[0][M86]=8; //8-6
InsCycles[1][M86]=6;
InsCycles[0][M98]=9; //9-8
InsCycles[1][M98]=8;
InsCycles[0][M2726]=27; //27-26
InsCycles[1][M2726]=26;
InsCycles[0][M3635]=36; //36-25
InsCycles[1][M3635]=35;
InsCycles[0][M3029]=30; //30-29
InsCycles[1][M3029]=29;
InsCycles[0][M2827]=28; //28-27
InsCycles[1][M2827]=27;
InsCycles[0][M3726]=37; //37-26
InsCycles[1][M3726]=26;
InsCycles[0][M3130]=31; //31-30
InsCycles[1][M3130]=30;
InsCycles[0][M42]=4; //4-2
InsCycles[1][M42]=2;
InsCycles[0][M53]=5; //5-3
InsCycles[1][M53]=3;
//SetNatEmuStat(1);
cc[I]=1;
cc[F]=1;
return;
}
void Neg_D(void)
{ //0
temp16 = DPADDRESS(PC_REG++);
postbyte = MemRead8(temp16);
temp8 = 0 - postbyte;
cc[C] = temp8 > 0;
cc[V] = (postbyte == 0x80);
cc[N] = NTEST8(temp8);
cc[Z] = ZTEST(temp8);
MemWrite8(temp8, temp16);
CycleCounter += NatEmuCycles65;
}
void Oim_D(void)
{//1 6309
postbyte=MemRead8(PC_REG++);
temp16 = DPADDRESS(PC_REG++);
postbyte|= MemRead8(temp16);
MemWrite8(postbyte,temp16);
cc[N] = NTEST8(postbyte);
cc[Z] = ZTEST(postbyte);
cc[V] = 0;
CycleCounter+=6;
}
void Aim_D(void)
{//2 Phase 2 6309
postbyte=MemRead8(PC_REG++);
temp16 = DPADDRESS(PC_REG++);
postbyte&= MemRead8(temp16);
MemWrite8(postbyte,temp16);
cc[N] = NTEST8(postbyte);
cc[Z] = ZTEST(postbyte);
cc[V] = 0;
CycleCounter+=6;
}
void Com_D(void)
{ //03
temp16 = DPADDRESS(PC_REG++);
temp8=MemRead8(temp16);
temp8=0xFF-temp8;
cc[Z] = ZTEST(temp8);
cc[N] = NTEST8(temp8);
cc[C] = 1;
cc[V] = 0;
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Lsr_D(void)
{ //04 S2
temp16 = DPADDRESS(PC_REG++);
temp8 = MemRead8(temp16);
cc[C] = temp8 & 1;
temp8 = temp8 >>1;
cc[Z] = ZTEST(temp8);
cc[N] = 0;
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Eim_D(void)
{ //05 6309 Untested
postbyte=MemRead8(PC_REG++);
temp16 = DPADDRESS(PC_REG++);
postbyte^= MemRead8(temp16);
MemWrite8(postbyte,temp16);
cc[N] = NTEST8(postbyte);
cc[Z] = ZTEST(postbyte);
cc[V] = 0;
CycleCounter+=6;
}
void Ror_D(void)
{ //06 S2
temp16 = DPADDRESS(PC_REG++);
temp8=MemRead8(temp16);
postbyte= cc[C]<<7;
cc[C] = temp8 & 1;
temp8 = (temp8 >> 1)| postbyte;
cc[Z] = ZTEST(temp8);
cc[N] = NTEST8(temp8);
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Asr_D(void)
{ //7
temp16 = DPADDRESS(PC_REG++);
temp8=MemRead8(temp16);
cc[C] = temp8 & 1;
temp8 = (temp8 & 0x80) | (temp8 >>1);
cc[Z] = ZTEST(temp8);
cc[N] = NTEST8(temp8);
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Asl_D(void)
{ //8
temp16 = DPADDRESS(PC_REG++);
temp8=MemRead8(temp16);
cc[C] = (temp8 & 0x80) >>7;
cc[V] = cc[C] ^ ((temp8 & 0x40) >> 6);
temp8 = temp8 <<1;
cc[N] = NTEST8(temp8);
cc[Z] = ZTEST(temp8);
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Rol_D(void)
{ //9
temp16 = DPADDRESS(PC_REG++);
temp8 = MemRead8(temp16);
postbyte=cc[C];
cc[C] =(temp8 & 0x80)>>7;
cc[V] = cc[C] ^ ((temp8 & 0x40) >>6);
temp8 = (temp8<<1) | postbyte;
cc[Z] = ZTEST(temp8);
cc[N] = NTEST8(temp8);
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Dec_D(void)
{ //A
temp16 = DPADDRESS(PC_REG++);
temp8 = MemRead8(temp16)-1;
cc[Z] = ZTEST(temp8);
cc[N] = NTEST8(temp8);
cc[V] = temp8==0x7F;
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Tim_D(void)
{ //B 6309 Untested wcreate
postbyte=MemRead8(PC_REG++);
temp8=MemRead8(DPADDRESS(PC_REG++));
postbyte&=temp8;
cc[N] = NTEST8(postbyte);
cc[Z] = ZTEST(postbyte);
cc[V] = 0;
CycleCounter+=6;
}
void Inc_D(void)
{ //C
temp16=(DPADDRESS(PC_REG++));
temp8 = MemRead8(temp16)+1;
cc[Z] = ZTEST(temp8);
cc[V] = temp8==0x80;
cc[N] = NTEST8(temp8);
MemWrite8(temp8,temp16);
CycleCounter+=NatEmuCycles65;
}
void Tst_D(void)
{ //D
temp8 = MemRead8(DPADDRESS(PC_REG++));
cc[Z] = ZTEST(temp8);
cc[N] = NTEST8(temp8);
cc[V] = 0;
CycleCounter+=NatEmuCycles64;
}
void Jmp_D(void)
{ //E
PC_REG= ((dp.Reg |MemRead8(PC_REG)));
CycleCounter+=NatEmuCycles32;
}
void Clr_D(void)
{ //F
MemWrite8(0,DPADDRESS(PC_REG++));
cc[Z] = 1;
cc[N] = 0;
cc[V] = 0;
cc[C] = 0;
CycleCounter+=NatEmuCycles65;
}
void LBeq_R(void)
{ //1027
if (cc[Z])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBrn_R(void)
{ //1021
PC_REG+=2;
CycleCounter+=5;
}
void LBhi_R(void)
{ //1022
if (!(cc[C] | cc[Z]))
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBls_R(void)
{ //1023
if (cc[C] | cc[Z])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBhs_R(void)
{ //1024
if (!cc[C])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=6;
}
void LBcs_R(void)
{ //1025
if (cc[C])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBne_R(void)
{ //1026
if (!cc[Z])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBvc_R(void)
{ //1028
if ( !cc[V])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBvs_R(void)
{ //1029
if ( cc[V])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBpl_R(void)
{ //102A
if (!cc[N])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBmi_R(void)
{ //102B
if ( cc[N])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBge_R(void)
{ //102C
if (! (cc[N] ^ cc[V]))
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBlt_R(void)
{ //102D
if ( cc[V] ^ cc[N])
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBgt_R(void)
{ //102E
if ( !( cc[Z] | (cc[N]^cc[V] ) ))
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void LBle_R(void)
{ //102F
if ( cc[Z] | (cc[N]^cc[V]) )
{
*spostword=IMMADDRESS(PC_REG);
PC_REG+=*spostword;
CycleCounter+=1;
}
PC_REG+=2;
CycleCounter+=5;
}
void Addr(void)
{ //1030 6309 - WallyZ 2019
unsigned char dest8, source8;
unsigned short dest16, source16;
temp8 = MemRead8(PC_REG++);
Source = temp8 >> 4;
Dest = temp8 & 15;
if (Dest > 7) // 8 bit dest
{
Dest &= 7;
if (Dest == 2) dest8 = getcc();
else dest8 = *ureg8[Dest];
if (Source > 7) // 8 bit source
{
Source &= 7;
if (Source == 2)
source8 = getcc();
else
source8 = *ureg8[Source];
}
else // 16 bit source - demote to 8 bit
{
Source &= 7;
source8 = (unsigned char)*xfreg16[Source];
}
temp16 = source8 + dest8;
switch (Dest)
{
case 2: setcc((unsigned char)temp16); break;
case 4: case 5: break; // never assign to zero reg
default: *ureg8[Dest] = (unsigned char)temp16; break;
}
cc[C] = (temp16 & 0x100) >> 8;
cc[V] = OVERFLOW8(cc[C], source8, dest8, temp16);
cc[N] = NTEST8(*ureg8[Dest]);
cc[Z] = ZTEST(*ureg8[Dest]);
}
else // 16 bit dest
{
dest16 = *xfreg16[Dest];
if (Source < 8) // 16 bit source
{
source16 = *xfreg16[Source];
}
else // 8 bit source - promote to 16 bit
{
Source &= 7;
switch (Source)
{
case 0: case 1: source16 = D_REG; break; // A & B Reg
case 2: source16 = (unsigned short)getcc(); break; // CC
case 3: source16 = (unsigned short)dp.Reg; break; // DP
case 4: case 5: source16 = 0; break; // Zero Reg
case 6: case 7: source16 = W_REG; break; // E & F Reg
}
}
temp32 = source16 + dest16;
*xfreg16[Dest] = (unsigned short)temp32;
cc[C] = (temp32 & 0x10000) >> 16;
cc[V] = OVERFLOW16(cc[C], source16, dest16, temp32);
cc[N] = NTEST16(*xfreg16[Dest]);
cc[Z] = ZTEST(*xfreg16[Dest]);
}
CycleCounter += 4;
}
void Adcr(void)
{ //1031 6309 - WallyZ 2019
unsigned char dest8, source8;
unsigned short dest16, source16;
temp8 = MemRead8(PC_REG++);
Source = temp8 >> 4;
Dest = temp8 & 15;
if (Dest > 7) // 8 bit dest
{
Dest &= 7;
if (Dest == 2) dest8 = getcc();
else dest8 = *ureg8[Dest];
if (Source > 7) // 8 bit source
{
Source &= 7;
if (Source == 2) source8 = getcc();
else source8 = *ureg8[Source];
}
else // 16 bit source - demote to 8 bit
{
Source &= 7;
source8 = (unsigned char)*xfreg16[Source];
}
temp16 = source8 + dest8 + cc[C];
switch (Dest)
{
case 2: setcc((unsigned char)temp16); break;
case 4: case 5: break; // never assign to zero reg
default: *ureg8[Dest] = (unsigned char)temp16; break;
}
cc[C] = (temp16 & 0x100) >> 8;
cc[V] = OVERFLOW8(cc[C], source8, dest8, temp16);
cc[N] = NTEST8(*ureg8[Dest]);
cc[Z] = ZTEST(*ureg8[Dest]);
}
else // 16 bit dest
{
dest16 = *xfreg16[Dest];
if (Source < 8) // 16 bit source
{
source16 = *xfreg16[Source];
}
else // 8 bit source - promote to 16 bit
{
Source &= 7;
switch (Source)
{
case 0: case 1: source16 = D_REG; break; // A & B Reg
case 2: source16 = (unsigned short)getcc(); break; // CC
case 3: source16 = (unsigned short)dp.Reg; break; // DP
case 4: case 5: source16 = 0; break; // Zero Reg
case 6: case 7: source16 = W_REG; break; // E & F Reg
}
}
temp32 = source16 + dest16 + cc[C];
*xfreg16[Dest] = (unsigned short)temp32;
cc[C] = (temp32 & 0x10000) >> 16;
cc[V] = OVERFLOW16(cc[C], source16, dest16, temp32);
cc[N] = NTEST16(*xfreg16[Dest]); cc[Z] = ZTEST(*xfreg16[Dest]);
}
CycleCounter += 4;
}
void Subr(void)
{ //1032 6309 - WallyZ 2019
unsigned char dest8, source8;
unsigned short dest16, source16;
temp8 = MemRead8(PC_REG++);
Source = temp8 >> 4;
Dest = temp8 & 15;
if (Dest > 7) // 8 bit dest
{
Dest &= 7;
if (Dest == 2) dest8 = getcc();
else dest8 = *ureg8[Dest];
if (Source > 7) // 8 bit source
{
Source &= 7;
if (Source == 2) source8 = getcc();
else source8 = *ureg8[Source];
}
else // 16 bit source - demote to 8 bit
{
Source &= 7;
source8 = (unsigned char)*xfreg16[Source];
}
temp16 = dest8 - source8;
switch (Dest)
{
case 2: setcc((unsigned char)temp16); break;
case 4: case 5: break; // never assign to zero reg
default: *ureg8[Dest] = (unsigned char)temp16; break;
}
cc[C] = (temp16 & 0x100) >> 8;
cc[V] = cc[C] ^ ((dest8 ^ *ureg8[Dest] ^ source8) >> 7);
cc[N] = *ureg8[Dest] >> 7;
cc[Z] = ZTEST(*ureg8[Dest]);
}
else // 16 bit dest
{
dest16 = *xfreg16[Dest];
if (Source < 8) // 16 bit source
{
source16 = *xfreg16[Source];
}
else // 8 bit source - promote to 16 bit
{
Source &= 7;
switch (Source)
{
case 0: case 1: source16 = D_REG; break; // A & B Reg
case 2: source16 = (unsigned short)getcc(); break; // CC
case 3: source16 = (unsigned short)dp.Reg; break; // DP
case 4: case 5: source16 = 0; break; // Zero Reg
case 6: case 7: source16 = W_REG; break; // E & F Reg
}
}
temp32 = dest16 - source16;
cc[C] = (temp32 & 0x10000) >> 16;
cc[V] = !!((dest16 ^ source16 ^ temp32 ^ (temp32 >> 1)) & 0x8000);
*xfreg16[Dest] = (unsigned short)temp32;
cc[N] = (temp32 & 0x8000) >> 15;
cc[Z] = ZTEST(temp32);
}
CycleCounter += 4;
}
void Sbcr(void)
{ //1033 6309 - WallyZ 2019
unsigned char dest8, source8;
unsigned short dest16, source16;
temp8 = MemRead8(PC_REG++);
Source = temp8 >> 4;
Dest = temp8 & 15;
if (Dest > 7) // 8 bit dest
{
Dest &= 7;
if (Dest == 2) dest8 = getcc();
else dest8 = *ureg8[Dest];
if (Source > 7) // 8 bit source
{
Source &= 7;
if (Source == 2) source8 = getcc();
else source8 = *ureg8[Source];
}
else // 16 bit source - demote to 8 bit
{
Source &= 7;
source8 = (unsigned char)*xfreg16[Source];
}
temp16 = dest8 - source8 - cc[C];
switch (Dest)
{
case 2: setcc((unsigned char)temp16); break;
case 4: case 5: break; // never assign to zero reg
default: *ureg8[Dest] = (unsigned char)temp16; break;
}
cc[C] = (temp16 & 0x100) >> 8;
cc[V] = cc[C] ^ ((dest8 ^ *ureg8[Dest] ^ source8) >> 7);
cc[N] = *ureg8[Dest] >> 7;
cc[Z] = ZTEST(*ureg8[Dest]);
}
else // 16 bit dest
{
dest16 = *xfreg16[Dest];
if (Source < 8) // 16 bit source
{
source16 = *xfreg16[Source];
}
else // 8 bit source - promote to 16 bit
{
Source &= 7;
switch (Source)
{
case 0: case 1: source16 = D_REG; break; // A & B Reg
case 2: source16 = (unsigned short)getcc(); break; // CC
case 3: source16 = (unsigned short)dp.Reg; break; // DP
case 4: case 5: source16 = 0; break; // Zero Reg
case 6: case 7: source16 = W_REG; break; // E & F Reg
}
}
temp32 = dest16 - source16 - cc[C];
cc[C] = (temp32 & 0x10000) >> 16;
cc[V] = !!((dest16 ^ source16 ^ temp32 ^ (temp32 >> 1)) & 0x8000);
*xfreg16[Dest] = (unsigned short)temp32;
cc[N] = (temp32 & 0x8000) >> 15;
cc[Z] = ZTEST(temp32);
}
CycleCounter += 4;
}
void Andr(void)
{ //1034 6309 - WallyZ 2019
unsigned char dest8, source8;
unsigned short dest16, source16;
temp8 = MemRead8(PC_REG++);
Source = temp8 >> 4;
Dest = temp8 & 15;
if (Dest > 7) // 8 bit dest
{
Dest &= 7;
if (Dest == 2) dest8 = getcc();
else dest8 = *ureg8[Dest];
if (Source > 7) // 8 bit source
{
Source &= 7;
if (Source == 2) source8 = getcc();
else source8 = *ureg8[Source];
}
else // 16 bit source - demote to 8 bit
{
Source &= 7;
source8 = (unsigned char)*xfreg16[Source];
}
temp8 = dest8 & source8;
switch (Dest)
{
case 2: setcc((unsigned char)temp8); break;
case 4: case 5: break; // never assign to zero reg
default: *ureg8[Dest] = (unsigned char)temp8; break;
}
cc[N] = temp8 >> 7;
cc[Z] = ZTEST(temp8);
}
else // 16 bit dest
{
dest16 = *xfreg16[Dest];
if (Source < 8) // 16 bit source
{
source16 = *xfreg16[Source];
}
else // 8 bit source - promote to 16 bit
{
Source &= 7;
switch (Source)
{
case 0: case 1: source16 = D_REG; break; // A & B Reg
case 2: source16 = (unsigned short)getcc(); break; // CC
case 3: source16 = (unsigned short)dp.Reg; break; // DP