-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc6809.c
3733 lines (3304 loc) · 71.7 KB
/
mc6809.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/>.
*/
#include <windows.h>
#include <stdio.h>
#include "defines.h"
#include "mc6809.h"
#include "mc6809defs.h"
#include "tcc1014mmu.h"
#include "OpDecoder.h"
//Global variables for CPU Emulation-----------------------
#define NTEST8(r) r>0x7F;
#define NTEST16(r) r>0x7FFF;
#define OTEST8(c,a,b,r) c ^ (((a^b^r)>>7) &1);
#define OTEST16(c,a,b,r) c ^ (((a^b^r)>>15)&1);
#define ZTEST(r) !r;
typedef union
{
unsigned short Reg;
struct
{
unsigned char lsb,msb;
} B;
} cpuregister;
#define D_REG d.Reg
#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 d.B.msb
#define B_REG d.B.lsb
#define DP_REG dp.B.msb
static cpuregister pc,x,y,u,s,dp,d;
static unsigned int cc[8];
static unsigned char *ureg8[8];
static unsigned char ccbits;
static unsigned short *xfreg16[8];
static int CycleCounter=0;
static unsigned int SyncWaiting=0;
static unsigned int temp32;
static unsigned short temp16;
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 std::vector<unsigned short> CPUBreakpoints;
static std::vector<unsigned short> CPUTraceTriggers;
//END Global variables for CPU Emulation-------------------
//Fuction Prototypes---------------------------------------
_inline unsigned short CalculateEA(unsigned char);
static void setcc (unsigned char);
static unsigned char getcc(void);
static void cpu_firq(void);
static void cpu_irq(void);
static void cpu_nmi(void);
//END Fuction Prototypes-----------------------------------
void MC6809Reset(void)
{
char index;
for(index=0;index<=5;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;
dp.Reg=0;
cc[I]=1;
cc[F]=1;
SyncWaiting=0;
pc.Reg=MemRead16(VRESET); //PC gets its reset vector
SetMapType(0);
return;
}
void MC6809Init(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;
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*)&dp.B.msb;
ureg8[5]=(unsigned char*)&dp.B.msb;
ureg8[6]=(unsigned char*)&dp.B.msb;
ureg8[7]=(unsigned char*)&dp.B.msb;
cc[I]=1;
cc[F]=1;
return;
}
VCC::CPUState MC6809GetState()
{
VCC::CPUState regs = { 0 };
regs.CC = getcc();
regs.DP = DP_REG;
regs.A = A_REG;
regs.B = B_REG;
regs.X = X_REG;
regs.Y = Y_REG;
regs.U = U_REG;
regs.S = S_REG;
regs.PC = PC_REG;
regs.IsNative6309 = false;
return regs;
}
void MC6809SetBreakpoints(const std::vector<unsigned short>& breakpoints)
{
CPUBreakpoints = breakpoints;
}
void MC6809SetTraceTriggers(const std::vector<unsigned short>& triggers)
{
CPUTraceTriggers = triggers;
}
int MC6809Exec( int CycleFor)
{
static unsigned char opcode=0;
static unsigned char msn,lsn;
extern int JS_Ramp_Clock;
int PrevCycleCount = 0;
CycleCounter=0;
while (CycleCounter<CycleFor) {
if (PendingInterupts)
{
if (PendingInterupts & 4)
cpu_nmi();
if (PendingInterupts & 2)
cpu_firq();
if (PendingInterupts & 1)
{
if (IRQWaiter==0) // This is needed to fix a subtle timming problem
cpu_irq(); // It allows the CPU to see $FF03 bit 7 high before
else // The IRQ is asserted.
IRQWaiter-=1;
}
}
if (SyncWaiting==1)
return(0);
// CPU is halted.
if (EmuState.Debugger.IsHalted())
{
return(CycleFor - CycleCounter);
}
// Any CPU Breakpoints set?
if (!EmuState.Debugger.IsStepping() && !CPUBreakpoints.empty())
{
if (find(CPUBreakpoints.begin(), CPUBreakpoints.end(), pc.Reg) != CPUBreakpoints.end())
{
EmuState.Debugger.Halt();
return(CycleFor - CycleCounter);
}
}
// Is the execution trace enabled - but currently not running?
if (EmuState.Debugger.IsTracingEnabled() && !EmuState.Debugger.IsTracing())
{
// Only Start Tracing when we hit a start trigger.
if (!CPUTraceTriggers.empty())
{
if (find(CPUTraceTriggers.begin(), CPUTraceTriggers.end(), pc.Reg) != CPUTraceTriggers.end())
{
EmuState.Debugger.TraceStart();
}
}
else
{
// Otherwise start right away.
EmuState.Debugger.TraceStart();
}
}
// Trace is running.
if (EmuState.Debugger.IsTracing())
{
EmuState.Debugger.TraceCaptureBefore(CycleCounter, MC6809GetState());
}
switch (MemRead8(pc.Reg++)){
case NEG_D: //0
temp16=(dp.Reg |MemRead8(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+=6;
break;
case COM_D:
temp16=(dp.Reg |MemRead8(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+=6;
break;
case LSR_D: //S2
temp16=(dp.Reg |MemRead8(pc.Reg++));
temp8=MemRead8(temp16);
cc[C]= temp8 & 1;
temp8= temp8 >>1;
cc[Z]= ZTEST(temp8);
cc[N]=0;
MemWrite8(temp8,temp16);
CycleCounter+=6;
break;
case ROR_D: //S2
temp16=(dp.Reg |MemRead8(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+=6;
break;
case ASR_D: //7
temp16=(dp.Reg |MemRead8(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+=6;
break;
case ASL_D: //8
temp16=(dp.Reg |MemRead8(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+=6;
break;
case ROL_D: //9
temp16=(dp.Reg |MemRead8(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+=6;
break;
case DEC_D: //A
temp16=(dp.Reg |MemRead8(pc.Reg++));
temp8=MemRead8(temp16)-1;
cc[Z]= ZTEST(temp8);
cc[N]= NTEST8(temp8);
cc[V]= temp8==0x7F;
MemWrite8(temp8,temp16);
CycleCounter+=6;
break;
case INC_D: //C
temp16=(dp.Reg |MemRead8(pc.Reg++));
temp8=MemRead8(temp16)+1;
cc[Z]= ZTEST(temp8);
cc[V]= temp8==0x80;
cc[N]= NTEST8(temp8);
MemWrite8(temp8,temp16);
CycleCounter+=6;
break;
case TST_D: //D
temp8=MemRead8((dp.Reg |MemRead8(pc.Reg++)));
cc[Z]= ZTEST(temp8);
cc[N]= NTEST8(temp8);
cc[V] = 0;
CycleCounter+=6;
break;
case JMP_D: //E
pc.Reg= ((dp.Reg |MemRead8(pc.Reg)));
CycleCounter+=3;
break;
case CLR_D: //F
MemWrite8(0,(dp.Reg |MemRead8(pc.Reg++)));
cc[Z]=1;
cc[N]=0;
cc[V] = 0;
cc[C]=0;
CycleCounter+=6;
break;
case Page2:
switch (MemRead8(pc.Reg++))
{
case LBEQ_R: //1027
if (cc[Z])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBRN_R: //1021
pc.Reg+=2;
CycleCounter+=5;
break;
case LBHI_R: //1022
if (!(cc[C] | cc[Z]))
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBLS_R: //1023
if (cc[C] | cc[Z])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBHS_R: //1024
if (!cc[C])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=6;
break;
case LBCS_R: //1025
if (cc[C])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBNE_R: //1026
if (!cc[Z])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBVC_R: //1028
if ( !cc[V])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBVS_R: //1029
if ( cc[V])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBPL_R: //102A
if (!cc[N])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBMI_R: //102B
if ( cc[N])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBGE_R: //102C
if (! (cc[N] ^ cc[V]))
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBLT_R: //102D
if ( cc[V] ^ cc[N])
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBGT_R: //102E
if ( !( cc[Z] | (cc[N]^cc[V] ) ))
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case LBLE_R: //102F
if ( cc[Z] | (cc[N]^cc[V]) )
{
*spostword=MemRead16(pc.Reg);
pc.Reg+=*spostword;
CycleCounter+=1;
}
pc.Reg+=2;
CycleCounter+=5;
break;
case SWI2_I: //103F
cc[E]=1;
MemWrite8( pc.B.lsb,--s.Reg);
MemWrite8( pc.B.msb,--s.Reg);
MemWrite8( u.B.lsb,--s.Reg);
MemWrite8( u.B.msb,--s.Reg);
MemWrite8( y.B.lsb,--s.Reg);
MemWrite8( y.B.msb,--s.Reg);
MemWrite8( x.B.lsb,--s.Reg);
MemWrite8( x.B.msb,--s.Reg);
MemWrite8( dp.B.msb,--s.Reg);
MemWrite8(B_REG,--s.Reg);
MemWrite8(A_REG,--s.Reg);
MemWrite8(getcc(),--s.Reg);
pc.Reg=MemRead16(VSWI2);
CycleCounter+=20;
break;
case CMPD_M: //1083
postword=MemRead16(pc.Reg);
temp16 = D_REG-postword;
cc[C]= temp16 > D_REG;
cc[V]= OTEST16(cc[C],postword,temp16,D_REG);//cc[C]^((postword^temp16^D_REG)>>15);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=5;
break;
case CMPY_M: //108C
postword=MemRead16(pc.Reg);
temp16 = y.Reg-postword;
cc[C]= temp16 > y.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,y.Reg);//cc[C]^((postword^temp16^y.Reg)>>15);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=5;
break;
case LDY_M: //108E
y.Reg= MemRead16(pc.Reg);
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
pc.Reg+=2;
CycleCounter+=5;
break;
case CMPD_D: //1093
postword=MemRead16((dp.Reg |MemRead8(pc.Reg++)));
temp16= D_REG - postword ;
cc[C]= temp16 > D_REG;
cc[V]= OTEST16(cc[C],postword,temp16,D_REG); //cc[C]^((postword^temp16^D_REG)>>15);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case CMPY_D: //109C
postword=MemRead16((dp.Reg |MemRead8(pc.Reg++)));
temp16= y.Reg - postword ;
cc[C]= temp16 > y.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,y.Reg);//cc[C]^((postword^temp16^y.Reg)>>15);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case LDY_D: //109E
y.Reg=MemRead16((dp.Reg |MemRead8(pc.Reg++)));
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case STY_D: //109F
MemWrite16(y.Reg,(dp.Reg |MemRead8(pc.Reg++)));
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case CMPD_X: //10A3
postword=MemRead16(CalculateEA(MemRead8(pc.Reg++)));
temp16= D_REG - postword ;
cc[C]= temp16 > D_REG;
cc[V]= OTEST16(cc[C],postword,temp16,D_REG);//cc[C]^((postword^temp16^D_REG)>>15);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case CMPY_X: //10AC
postword=MemRead16(CalculateEA(MemRead8(pc.Reg++)));
temp16= y.Reg - postword ;
cc[C]= temp16 > y.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,Y_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case LDY_X: //10AE
y.Reg=MemRead16(CalculateEA(MemRead8(pc.Reg++)));
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case STY_X: //10AF
MemWrite16(y.Reg,CalculateEA(MemRead8(pc.Reg++)));
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case CMPD_E: //10B3
postword=MemRead16(MemRead16(pc.Reg));
temp16 = D_REG-postword;
cc[C]= temp16 > D_REG;
cc[V]= OTEST16(cc[C],postword,temp16,D_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=8;
break;
case CMPY_E: //10BC
postword=MemRead16(MemRead16(pc.Reg));
temp16 = y.Reg-postword;
cc[C]= temp16 > y.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,Y_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=8;
break;
case LDY_E: //10BE
y.Reg=MemRead16(MemRead16(pc.Reg));
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
pc.Reg+=2;
CycleCounter+=7;
break;
case STY_E: //10BF
MemWrite16(y.Reg,MemRead16(pc.Reg));
cc[Z]= ZTEST(y.Reg);
cc[N]= NTEST16(y.Reg);
cc[V]= 0;
pc.Reg+=2;
CycleCounter+=7;
break;
case LDS_I: //10CE
s.Reg=MemRead16(pc.Reg);
cc[Z]= ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V] = 0;
pc.Reg+=2;
CycleCounter+=4;
break;
case LDS_D: //10DE
s.Reg=MemRead16((dp.Reg |MemRead8(pc.Reg++)));
cc[Z]= ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V] = 0;
CycleCounter+=6;
break;
case STS_D: //10DF
MemWrite16(s.Reg,(dp.Reg |MemRead8(pc.Reg++)));
cc[Z]= ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case LDS_X: //10EE
s.Reg=MemRead16(CalculateEA(MemRead8(pc.Reg++)));
cc[Z]= ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case STS_X: //10EF
MemWrite16(s.Reg,CalculateEA(MemRead8(pc.Reg++)));
cc[Z]= ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V]= 0;
CycleCounter+=6;
break;
case LDS_E: //10FE
s.Reg=MemRead16(MemRead16(pc.Reg));
cc[Z]= ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V]= 0;
pc.Reg+=2;
CycleCounter+=7;
break;
case STS_E: //10FF
MemWrite16(s.Reg,MemRead16(pc.Reg));
cc[Z] = ZTEST(s.Reg);
cc[N]= NTEST16(s.Reg);
cc[V] = 0;
pc.Reg+=2;
CycleCounter+=7;
break;
default:
// MessageBox(0,"Unhandled Op","Ok",0);
break;
} //Page 2 switch END
break;
case Page3:
switch (MemRead8(pc.Reg++))
{
case SWI3_I: //113F
cc[E]=1;
MemWrite8( pc.B.lsb,--s.Reg);
MemWrite8( pc.B.msb,--s.Reg);
MemWrite8( u.B.lsb,--s.Reg);
MemWrite8( u.B.msb,--s.Reg);
MemWrite8( y.B.lsb,--s.Reg);
MemWrite8( y.B.msb,--s.Reg);
MemWrite8( x.B.lsb,--s.Reg);
MemWrite8( x.B.msb,--s.Reg);
MemWrite8( dp.B.msb,--s.Reg);
MemWrite8(B_REG,--s.Reg);
MemWrite8(A_REG,--s.Reg);
MemWrite8(getcc(),--s.Reg);
pc.Reg=MemRead16(VSWI3);
CycleCounter+=20;
break;
case CMPU_M: //1183
postword=MemRead16(pc.Reg);
temp16 = u.Reg-postword;
cc[C]= temp16 > u.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,U_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=5;
break;
case CMPS_M: //118C
postword=MemRead16(pc.Reg);
temp16 = s.Reg-postword;
cc[C]= temp16 > s.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,S_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=5;
break;
case CMPU_D: //1193
postword=MemRead16((dp.Reg |MemRead8(pc.Reg++)));
temp16= u.Reg - postword ;
cc[C]= temp16 > u.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,U_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case CMPS_D: //119C
postword=MemRead16((dp.Reg |MemRead8(pc.Reg++)));
temp16= s.Reg - postword ;
cc[C]= temp16 > s.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,S_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case CMPU_X: //11A3
postword=MemRead16(CalculateEA(MemRead8(pc.Reg++)));
temp16= u.Reg - postword ;
cc[C]= temp16 > u.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,U_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case CMPS_X: //11AC
postword=MemRead16(CalculateEA(MemRead8(pc.Reg++)));
temp16= s.Reg - postword ;
cc[C]= temp16 > s.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,S_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
CycleCounter+=7;
break;
case CMPU_E: //11B3
postword=MemRead16(MemRead16(pc.Reg));
temp16 = u.Reg-postword;
cc[C]= temp16 > u.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,U_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=8;
break;
case CMPS_E: //11BC
postword=MemRead16(MemRead16(pc.Reg));
temp16 = s.Reg-postword;
cc[C]= temp16 > s.Reg;
cc[V]= OTEST16(cc[C],postword,temp16,S_REG);
cc[N]= NTEST16(temp16);
cc[Z]= ZTEST(temp16);
pc.Reg+=2;
CycleCounter+=8;
break;
default:
// MessageBox(0,"Unhandled Op","Ok",0);
break;
} //Page 3 switch END
break;
case NOP_I: //12
CycleCounter+=2;
break;
case SYNC_I: //13
CycleCounter=CycleFor;
SyncWaiting=1;
break;
case LBRA_R: //16
*spostword=MemRead16(pc.Reg);
pc.Reg+=2;
pc.Reg+=*spostword;
CycleCounter+=5;
break;
case LBSR_R: //17
*spostword=MemRead16(pc.Reg);
pc.Reg+=2;
s.Reg--;
MemWrite8(pc.B.lsb,s.Reg--);
MemWrite8(pc.B.msb,s.Reg);
pc.Reg+=*spostword;
CycleCounter+=9;
break;
case DAA_I: //19
// MessageBox(0,"DAA","Ok",0);
msn=(A_REG & 0xF0) ;
lsn=(A_REG & 0xF);
temp8=0;
if ( cc[H] || (lsn >9) )
temp8 |= 0x06;
if ( (msn>0x80) && (lsn>9))
temp8|=0x60;
if ( (msn>0x90) || cc[C] )
temp8|=0x60;
temp16= A_REG+temp8;
cc[C]|=((temp16 & 0x100)>>8);
A_REG= temp16 & 0xFF;
cc[N]= NTEST8(A_REG);
cc[Z]= ZTEST(A_REG);
CycleCounter+=2;
break;
case ORCC_M: //1A
postbyte=MemRead8(pc.Reg++);
temp8=getcc();
temp8 = (temp8 | postbyte);
setcc(temp8);
CycleCounter+=3;
break;
case ANDCC_M: //1C
postbyte=MemRead8(pc.Reg++);
temp8=getcc();
temp8 = (temp8 & postbyte);
setcc(temp8);
CycleCounter+=3;
break;
case SEX_I: //1D
// A_REG = ((signed char)B_REG < 0) ? 0xff : 0x00;
A_REG= 0-(B_REG>>7);
cc[Z]= ZTEST(D_REG);
cc[N]= NTEST16(D_REG);
CycleCounter+=2;
break;
case EXG_M: //1E
postbyte=MemRead8(pc.Reg++);
ccbits=getcc();
if ( ((postbyte & 0x80)>>4)==(postbyte & 0x08)) //Verify like size registers
{
if (postbyte & 0x08) //8 bit EXG
{
temp8= (*ureg8[((postbyte & 0x70) >> 4)]);
(*ureg8[((postbyte & 0x70) >> 4)]) = (*ureg8[postbyte & 0x07]);
(*ureg8[postbyte & 0x07])=temp8;
}
else // 16 bit EXG
{
temp16=(*xfreg16[((postbyte & 0x70) >> 4)]);
(*xfreg16[((postbyte & 0x70) >> 4)])=(*xfreg16[postbyte & 0x07]);
(*xfreg16[postbyte & 0x07])=temp16;
}
}
setcc(ccbits);
CycleCounter+=8;
break;
case TFR_M: //1F
postbyte=MemRead8(pc.Reg++);
Source= postbyte>>4;
Dest=postbyte & 15;
switch (Dest)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
*xfreg16[Dest]=0xFFFF;
if ((Source == 12) | (Source == 13))
{
*xfreg16[Dest] = 0;
}
else if (Source <= 7)
{
//make sure the source is valud
if (xfreg16[Source])
{
*xfreg16[Dest] = *xfreg16[Source];
}
}
break;
case 8:
case 9:
case 10:
case 11:
case 14:
case 15:
ccbits=getcc();
*ureg8[Dest&7]=0xFF;
if ( (Source==12) | (Source==13) )
*ureg8[Dest&7]=0;
else
if (Source>7)
*ureg8[Dest&7]=*ureg8[Source&7];
setcc(ccbits);
break;
}
CycleCounter+=6;
break;