forked from thefossilrecord/beebem-rs97
-
Notifications
You must be signed in to change notification settings - Fork 5
/
6502core.cc
1456 lines (1294 loc) · 43.5 KB
/
6502core.cc
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
/****************************************************************************/
/* Beebem - (c) David Alan Gilbert 1994 */
/* ------------------------------------ */
/* This program may be distributed freely within the following restrictions:*/
/* */
/* 1) You may not charge for this program or for any part of it. */
/* 2) This copyright message must be distributed with all copies. */
/* 3) This program must be distributed complete with source code. Binary */
/* only distribution is not permitted. */
/* 4) The author offers no warrenties, or guarentees etc. - you use it at */
/* your own risk. If it messes something up or destroys your computer */
/* thats YOUR problem. */
/* 5) You may use small sections of code from this program in your own */
/* applications - but you must acknowledge its use. If you plan to use */
/* large sections then please ask the author. */
/* */
/* If you do not agree with any of the above then please do not use this */
/* program. */
/* Please report any problems to the author at [email protected] */
/****************************************************************************/
/* 6502 core - 6502 emulator core - David Alan Gilbert 16/10/94 */
/* Mike Wyatt 7/6/97 - Added undocumented instructions */
// Remove this lot after timing test.
#include <SDL.h>
static unsigned int previous_framecount=0;
static unsigned int previous_cycles=0;
static unsigned int previous_time=0;
/////////////////////////
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "6502core.h"
#include "beebmem.h"
#include "beebsound.h"
#include "disc8271.h"
#include "sysvia.h"
#include "uservia.h"
#include "video.h"
#include "atodconv.h"
#include "beebwin.h"
#include "beebconfig.h"
#include "messagebox.h"
#include "uefstate.h"
// To read how many bytes are in sound buffer.
#include "sdl.h"
#include "messages.h"
//#ifdef WIN32
//#include <windows.h>
//#define INLINE inline
//#else
#define INLINE inline
//#endif
int IgnoreIllegalInstructions = 1;
extern int DumpAfterEach;
//==CycleCountT TotalCycles=0;
int TotalCycles=0;
//int tmp_TotalCycles=0;
static int ProgramCounter;
static int Accumulator,XReg,YReg;
static unsigned char StackReg,PSR;
unsigned char intStatus=0; /* bit set (nums in IRQ_Nums) if interrupt being caused */
unsigned char NMIStatus=0; /* bit set (nums in NMI_Nums) if NMI being caused */
unsigned int NMILock=0; /* Well I think NMI's are maskable - to stop repeated NMI's - the lock is released when an RTI is done */
typedef int int16;
/* Stats */
int Stats[256];
enum PSRFlags {
FlagC=1,
FlagZ=2,
FlagI=4,
FlagD=8,
FlagB=16,
FlagV=64,
FlagN=128
};
/* Note how GETCFLAG is special since being bit 0 we don't need to test it to get a clean 0/1 */
#define GETCFLAG ((PSR & FlagC))
#define GETZFLAG ((PSR & FlagZ)>0)
#define GETIFLAG ((PSR & FlagI)>0)
#define GETDFLAG ((PSR & FlagD)>0)
#define GETBFLAG ((PSR & FlagB)>0)
#define GETVFLAG ((PSR & FlagV)>0)
#define GETNFLAG ((PSR & FlagN)>0)
/* Types for internal function arrays */
typedef void (*InstrHandlerFuncType)(int16 Operand);
typedef int16 (*AddrModeHandlerFuncType)(int WantsAddr);
static int CyclesTable[]={
7,6,0,0,0,3,5,5,3,2,2,0,0,4,6,0, /* 0 */
2,5,0,0,0,4,6,0,2,4,0,0,0,4,7,0, /* 1 */
6,6,0,0,3,3,5,0,4,2,2,0,4,4,6,0, /* 2 */
2,5,0,0,0,4,6,0,2,4,0,0,0,4,7,0, /* 3 */
6,6,0,0,0,3,5,0,3,2,2,2,3,4,6,0, /* 4 */
2,5,0,0,0,4,6,0,2,4,0,0,0,4,7,0, /* 5 */
6,6,0,0,0,3,5,0,4,2,2,0,5,4,6,0, /* 6 */
2,5,0,0,0,4,6,0,2,4,0,0,0,4,7,0, /* 7 */
2,6,0,0,3,3,3,3,2,0,2,0,4,4,4,0, /* 8 */
2,6,0,0,4,4,4,0,2,5,2,0,0,5,0,0, /* 9 */
2,6,2,0,3,3,3,0,2,2,2,0,4,4,4,0, /* a */
2,5,0,0,4,4,4,0,2,4,2,0,4,4,4,0, /* b */
2,6,0,0,3,3,5,0,2,2,2,0,4,4,6,0, /* c */
2,5,0,0,0,4,6,0,2,4,0,0,4,4,7,0, /* d */
2,6,0,0,3,3,5,0,2,2,2,0,4,4,6,0, /* e */
2,5,0,0,0,4,6,0,2,4,0,0,0,4,7,0 /* f */
}; /* CyclesTable */
/* The number of cycles to be used by the current instruction - exported to
allow fernangling by memory subsystem */
unsigned int Cycles;
/* A macro to speed up writes - uses a local variable called 'tmpaddr' */
#define FASTWRITE(addr,val) tmpaddr=addr; if (tmpaddr<0x8000) BEEBWRITEMEM_DIRECT(tmpaddr,val) else BeebWriteMem(tmpaddr,val);
/* Get a two byte address from the program counter, and then post inc the program counter */
#define GETTWOBYTEFROMPC(var) \
var=WholeRam[ProgramCounter]; \
var|=(WholeRam[ProgramCounter+1]<<8); \
ProgramCounter+=2;
/*----------------------------------------------------------------------------*/
INLINE int SignExtendByte(signed char in) {
/*if (in & 0x80) return(in | 0xffffff00); else return(in); */
/* I think this should sign extend by virtue of the casts - gcc does anyway - the code
above will definitly do the trick */
return((int)in);
} /* SignExtendByte */
/*----------------------------------------------------------------------------*/
/* Set the Z flag if 'in' is 0, and N if bit 7 is set - leave all other bits */
/* untouched. */
INLINE static void SetPSRZN(const unsigned char in) {
PSR&=~(FlagZ | FlagN);
PSR|=((in==0)<<1) | (in & 128);
}; /* SetPSRZN */
/*----------------------------------------------------------------------------*/
/* Note: n is 128 for true - not 1 */
INLINE static void SetPSR(int mask,int c,int z,int i,int d,int b, int v, int n) {
PSR&=~mask;
PSR|=c | (z<<1) | (i<<2) | (d<<3) | (b<<4) | (v<<6) | n;
} /* SetPSR */
/*----------------------------------------------------------------------------*/
/* NOTE!!!!! n is 128 or 0 - not 1 or 0 */
INLINE static void SetPSRCZN(int c,int z, int n) {
PSR&=~(FlagC | FlagZ | FlagN);
PSR|=c | (z<<1) | n;
} /* SetPSRCZN */
/*----------------------------------------------------------------------------*/
void DumpRegs(void) {
static char FlagNames[]="CZIDB-VNczidb-vn";
int FlagNum;
fprintf(stderr," PC=0x%x A=0x%x X=0x%x Y=0x%x S=0x%x PSR=0x%x=",
ProgramCounter,Accumulator,XReg,YReg,StackReg,PSR);
for(FlagNum=0;FlagNum<8;FlagNum++)
fputc(FlagNames[FlagNum+8*((PSR & (1<<FlagNum))==0)],stderr);
fputc('\n',stderr);
} /* DumpRegs */
/*----------------------------------------------------------------------------*/
INLINE static void Push(unsigned char ToPush) {
BEEBWRITEMEM_DIRECT(0x100+StackReg,ToPush);
StackReg--;
} /* Push */
/*----------------------------------------------------------------------------*/
INLINE static unsigned char Pop(void) {
StackReg++;
return(WholeRam[0x100+StackReg]);
} /* Pop */
/*----------------------------------------------------------------------------*/
INLINE static void PushWord(int16 topush) {
Push((topush>>8) & 255);
Push(topush & 255);
} /* PushWord */
/*----------------------------------------------------------------------------*/
INLINE static int16 PopWord() {
int16 RetValue;
RetValue=Pop();
RetValue|=(Pop()<<8);
return(RetValue);
} /* PopWord */
/*-------------------------------------------------------------------------*/
/* Relative addressing mode handler */
INLINE static int16 RelAddrModeHandler_Data(void) {
int EffectiveAddress;
/* For branches - is this correct - i.e. is the program counter incremented
at the correct time? */
EffectiveAddress=SignExtendByte((signed char)WholeRam[ProgramCounter++]);
EffectiveAddress+=ProgramCounter;
return(EffectiveAddress);
} /* RelAddrModeHandler */
/*----------------------------------------------------------------------------*/
INLINE static void ADCInstrHandler(int16 operand) {
/* NOTE! Not sure about C and V flags */
int TmpResultV,TmpResultC;
if (!GETDFLAG) {
TmpResultC=Accumulator+operand+GETCFLAG;
TmpResultV=(signed char)Accumulator+(signed char)operand+GETCFLAG;
Accumulator=TmpResultC & 255;
SetPSR(FlagC | FlagZ | FlagV | FlagN, (TmpResultC & 256)>0,Accumulator==0,0,0,0,((Accumulator & 128)>0) ^ (TmpResultV<0),(Accumulator & 128));
} else {
int ZFlag=0,NFlag=0,CFlag=0,VFlag=0;
int TmpResult,TmpCarry=0;
int ln,hn;
/* Z flag determined from 2's compl result, not BCD result! */
TmpResult=Accumulator+operand+GETCFLAG;
ZFlag=((TmpResult & 0xff)==0);
ln=(Accumulator & 0xf)+(operand & 0xf)+GETCFLAG;
if (ln>9) {
ln += 6;
ln &= 0xf;
TmpCarry=0x10;
}
hn=(Accumulator & 0xf0)+(operand & 0xf0)+TmpCarry;
/* N and V flags are determined before high nibble is adjusted.
NOTE: V is not always correct */
NFlag=hn & 128;
VFlag=((hn & 128)==0) ^ ((Accumulator & 128)==0);
if (hn>0x90) {
hn += 0x60;
hn &= 0xf0;
CFlag=1;
}
Accumulator=hn|ln;
SetPSR(FlagC | FlagZ | FlagV | FlagN,CFlag,ZFlag,0,0,0,VFlag,NFlag);
}
} /* ADCInstrHandler */
/*----------------------------------------------------------------------------*/
INLINE static void ANDInstrHandler(int16 operand) {
Accumulator=Accumulator & operand;
PSR&=~(FlagZ | FlagN);
PSR|=((Accumulator==0)<<1) | (Accumulator & 128);
} /* ANDInstrHandler */
INLINE static void ASLInstrHandler(int16 address) {
unsigned char oldVal,newVal;
oldVal=BEEBREADMEM_FAST(address);
newVal=(((unsigned int)oldVal)<<1);
BEEBWRITEMEM_FAST(address,newVal);
SetPSRCZN((oldVal & 128)>0, newVal==0,newVal & 128);
} /* ASLInstrHandler */
INLINE static void ASLInstrHandler_Acc(void) {
unsigned char oldVal,newVal;
/* Accumulator */
oldVal=Accumulator;
Accumulator=newVal=(((unsigned int)Accumulator)<<1);
SetPSRCZN((oldVal & 128)>0, newVal==0,newVal & 128);
} /* ASLInstrHandler_Acc */
INLINE static void BCCInstrHandler(void) {
if (!GETCFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BCCInstrHandler */
INLINE static void BCSInstrHandler(void) {
if (GETCFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BCSInstrHandler */
INLINE static void BEQInstrHandler(void) {
if (GETZFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BEQInstrHandler */
INLINE static void BITInstrHandler(int16 operand) {
PSR&=~(FlagZ | FlagN | FlagV);
/* z if result 0, and NV to top bits of operand */
PSR|=(((Accumulator & operand)==0)<<1) | (operand & 192);
} /* BITInstrHandler */
INLINE static void BMIInstrHandler(void) {
if (GETNFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BMIInstrHandler */
INLINE static void BNEInstrHandler(void) {
if (!GETZFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BNEInstrHandler */
INLINE static void BPLInstrHandler(void) {
if (!GETNFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
}; /* BPLInstrHandler */
INLINE static void BRKInstrHandler(void) {
PushWord(ProgramCounter+1);
SetPSR(FlagB,0,0,0,0,1,0,0); /* Set B before pushing */
Push(PSR);
SetPSR(FlagI,0,0,1,0,0,0,0); /* Set I after pushing - see Birnbaum */
ProgramCounter=BeebReadMem(0xfffe) | (BeebReadMem(0xffff)<<8);
} /* BRKInstrHandler */
INLINE static void BVCInstrHandler(void) {
if (!GETVFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BVCInstrHandler */
INLINE static void BVSInstrHandler(void) {
if (GETVFLAG) {
ProgramCounter=RelAddrModeHandler_Data();
Cycles++;
} else ProgramCounter++;
} /* BVSInstrHandler */
INLINE static void CMPInstrHandler(int16 operand) {
/* NOTE! Should we consult D flag ? */
unsigned char result=Accumulator-operand;
SetPSRCZN(Accumulator>=operand,Accumulator==operand,result & 128);
} /* CMPInstrHandler */
INLINE static void CPXInstrHandler(int16 operand) {
unsigned char result=(XReg-operand);
SetPSRCZN(XReg>=operand,XReg==operand,result & 128);
} /* CPXInstrHandler */
INLINE static void CPYInstrHandler(int16 operand) {
unsigned char result=(YReg-operand);
SetPSRCZN(YReg>=operand,YReg==operand,result & 128);
} /* CPYInstrHandler */
INLINE static void DECInstrHandler(int16 address) {
unsigned char val;
val=BEEBREADMEM_FAST(address);
val=(val-1);
BEEBWRITEMEM_FAST(address,val);
SetPSRZN(val);
} /* DECInstrHandler */
INLINE static void DEXInstrHandler(void) {
XReg=(XReg-1) & 255;
SetPSRZN(XReg);
} /* DEXInstrHandler */
INLINE static void EORInstrHandler(int16 operand) {
Accumulator^=operand;
SetPSRZN(Accumulator);
} /* EORInstrHandler */
INLINE static void INCInstrHandler(int16 address) {
unsigned char val;
val=BEEBREADMEM_FAST(address);
val=(val+1) & 255;
BEEBWRITEMEM_FAST(address,val);
SetPSRZN(val);
} /* INCInstrHandler */
INLINE static void INXInstrHandler(void) {
XReg+=1;
XReg&=255;
SetPSRZN(XReg);
} /* INXInstrHandler */
INLINE static void JSRInstrHandler(int16 address) {
PushWord(ProgramCounter-1);
ProgramCounter=address;
} /* JSRInstrHandler */
INLINE static void LDAInstrHandler(int16 operand) {
Accumulator=operand;
SetPSRZN(Accumulator);
} /* LDAInstrHandler */
INLINE static void LDXInstrHandler(int16 operand) {
XReg=operand;
SetPSRZN(XReg);
} /* LDXInstrHandler */
INLINE static void LDYInstrHandler(int16 operand) {
YReg=operand;
SetPSRZN(YReg);
} /* LDYInstrHandler */
INLINE static void LSRInstrHandler(int16 address) {
unsigned char oldVal,newVal;
oldVal=BEEBREADMEM_FAST(address);
newVal=(((unsigned int)oldVal)>>1);
BEEBWRITEMEM_FAST(address,newVal);
SetPSRCZN((oldVal & 1)>0, newVal==0,0);
} /* LSRInstrHandler */
INLINE static void LSRInstrHandler_Acc(void) {
unsigned char oldVal,newVal;
/* Accumulator */
oldVal=Accumulator;
Accumulator=newVal=(((unsigned int)Accumulator)>>1) & 255;
SetPSRCZN((oldVal & 1)>0, newVal==0,0);
} /* LSRInstrHandler_Acc */
INLINE static void ORAInstrHandler(int16 operand) {
Accumulator=Accumulator | operand;
SetPSRZN(Accumulator);
} /* ORAInstrHandler */
INLINE static void ROLInstrHandler(int16 address) {
unsigned char oldVal,newVal;
oldVal=BEEBREADMEM_FAST(address);
newVal=((unsigned int)oldVal<<1) & 254;
newVal+=GETCFLAG;
BEEBWRITEMEM_FAST(address,newVal);
SetPSRCZN((oldVal & 128)>0,newVal==0,newVal & 128);
} /* ROLInstrHandler */
INLINE static void ROLInstrHandler_Acc(void) {
unsigned char oldVal,newVal;
oldVal=Accumulator;
newVal=((unsigned int)oldVal<<1) & 254;
newVal+=GETCFLAG;
Accumulator=newVal;
SetPSRCZN((oldVal & 128)>0,newVal==0,newVal & 128);
} /* ROLInstrHandler_Acc */
INLINE static void RORInstrHandler(int16 address) {
unsigned char oldVal,newVal;
oldVal=BEEBREADMEM_FAST(address);
newVal=((unsigned int)oldVal>>1) & 127;
newVal+=GETCFLAG*128;
BEEBWRITEMEM_FAST(address,newVal);
SetPSRCZN(oldVal & 1,newVal==0,newVal & 128);
} /* RORInstrHandler */
INLINE static void RORInstrHandler_Acc(void) {
unsigned char oldVal,newVal;
oldVal=Accumulator;
newVal=((unsigned int)oldVal>>1) & 127;
newVal+=GETCFLAG*128;
Accumulator=newVal;
SetPSRCZN(oldVal & 1,newVal==0,newVal & 128);
} /* RORInstrHandler_Acc */
INLINE static void SBCInstrHandler(int16 operand) {
/* NOTE! Not sure about C and V flags */
int TmpResultV,TmpResultC;
if (!GETDFLAG) {
TmpResultV=(signed char)Accumulator-(signed char)operand-(1-GETCFLAG);
TmpResultC=Accumulator-operand-(1-GETCFLAG);
Accumulator=TmpResultC & 255;
SetPSR(FlagC | FlagZ | FlagV | FlagN, TmpResultC>=0,Accumulator==0,0,0,0,
((Accumulator & 128)>0) ^ ((TmpResultV & 256)!=0),(Accumulator & 128));
} else {
int ZFlag=0,NFlag=0,CFlag=1,VFlag=0;
int TmpResult,TmpCarry=0;
int ln,hn;
/* Z flag determined from 2's compl result, not BCD result! */
TmpResult=Accumulator-operand-(1-GETCFLAG);
ZFlag=((TmpResult & 0xff)==0);
ln=(Accumulator & 0xf)-(operand & 0xf)-(1-GETCFLAG);
if (ln<0) {
ln-=6;
ln&=0xf;
TmpCarry=0x10;
}
hn=(Accumulator & 0xf0)-(operand & 0xf0)-TmpCarry;
/* N and V flags are determined before high nibble is adjusted.
NOTE: V is not always correct */
NFlag=hn & 128;
VFlag=((hn & 128)==0) ^ ((Accumulator & 128)==0);
if (hn<0) {
hn-=0x60;
hn&=0xf0;
CFlag=0;
}
Accumulator=hn|ln;
SetPSR(FlagC | FlagZ | FlagV | FlagN,CFlag,ZFlag,0,0,0,VFlag,NFlag);
}
} /* SBCInstrHandler */
INLINE static void STXInstrHandler(int16 address) {
BEEBWRITEMEM_FAST(address,XReg);
} /* STXInstrHandler */
INLINE static void STYInstrHandler(int16 address) {
BEEBWRITEMEM_FAST(address,YReg);
} /* STYInstrHandler */
INLINE static void BadInstrHandler(int opcode) {
if (!IgnoreIllegalInstructions)
{
//#ifdef WIN32
char errstr[250];
sprintf(errstr, CORE6502_ILLEGALINSTRUCTION_M, opcode,ProgramCounter-1);
// if (MessageBox(NULL,errstr,"BBC Emulator",MB_OKCANCEL|MB_ICONERROR) == IDCANCEL)
// {
// beebmem_dumpstate();
// exit(0);
// }
//#else
fprintf(stderr,"Bad instruction handler called:\n");
DumpRegs();
fprintf(stderr,"Dumping main memory\n");
beebmem_dumpstate();
//#endif
if (EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_QUESTION, CORE6502_ILLEGALINSTRUCTION_T, errstr, "SKIP", "QUIT", NULL, NULL, 1) == 2 )
abort();
}
/* Do not know what the instruction does but can guess if it is 1,2 or 3 bytes */
switch (opcode & 0xf)
{
/* One byte instructions */
case 0xa:
break;
/* Two byte instructions */
case 0x0:
case 0x2: /* Inst 0xf2 causes the 6502 to hang! Try it on your BBC Micro */
case 0x3:
case 0x4:
case 0x7:
case 0x9:
case 0xb:
ProgramCounter++;
break;
/* Three byte instructions */
case 0xc:
case 0xe:
case 0xf:
ProgramCounter+=2;
break;
}
} /* BadInstrHandler */
/*-------------------------------------------------------------------------*/
/* Absolute addressing mode handler */
INLINE static int16 AbsAddrModeHandler_Data(void) {
int FullAddress;
/* Get the address from after the instruction */
GETTWOBYTEFROMPC(FullAddress)
/* And then read it */
return(BEEBREADMEM_FAST(FullAddress));
} /* AbsAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Absolute addressing mode handler */
INLINE static int16 AbsAddrModeHandler_Address(void) {
int FullAddress;
/* Get the address from after the instruction */
GETTWOBYTEFROMPC(FullAddress)
/* And then read it */
return(FullAddress);
} /* AbsAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Zero page addressing mode handler */
INLINE static int16 ZeroPgAddrModeHandler_Address(void) {
return(WholeRam[ProgramCounter++]);
} /* ZeroPgAddrModeHandler_Address */
/*-------------------------------------------------------------------------*/
/* Indexed with X preinc addressing mode handler */
INLINE static int16 IndXAddrModeHandler_Data(void) {
unsigned char ZeroPageAddress;
int EffectiveAddress;
ZeroPageAddress=(WholeRam[ProgramCounter++]+XReg) & 255;
EffectiveAddress=WholeRam[ZeroPageAddress] | (WholeRam[ZeroPageAddress+1]<<8);
return(BEEBREADMEM_FAST(EffectiveAddress));
} /* IndXAddrModeHandler_Data */
/*-------------------------------------------------------------------------*/
/* Indexed with X preinc addressing mode handler */
INLINE static int16 IndXAddrModeHandler_Address(void) {
unsigned char ZeroPageAddress;
int EffectiveAddress;
ZeroPageAddress=(WholeRam[ProgramCounter++]+XReg) & 255;
EffectiveAddress=WholeRam[ZeroPageAddress] | (WholeRam[ZeroPageAddress+1]<<8);
return(EffectiveAddress);
} /* IndXAddrModeHandler_Address */
/*-------------------------------------------------------------------------*/
/* Indexed with Y postinc addressing mode handler */
INLINE static int16 IndYAddrModeHandler_Data(void) {
int EffectiveAddress;
unsigned char ZPAddr=WholeRam[ProgramCounter++];
EffectiveAddress=WholeRam[ZPAddr]+YReg;
EffectiveAddress+=(WholeRam[ZPAddr+1]<<8);
return(BEEBREADMEM_FAST(EffectiveAddress));
} /* IndYAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Indexed with Y postinc addressing mode handler */
INLINE static int16 IndYAddrModeHandler_Address(void) {
int EffectiveAddress;
unsigned char ZPAddr=WholeRam[ProgramCounter++];
EffectiveAddress=WholeRam[ZPAddr]+YReg;
EffectiveAddress+=(WholeRam[ZPAddr+1]<<8);
return(EffectiveAddress);
} /* IndYAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Zero page wih X offset addressing mode handler */
INLINE static int16 ZeroPgXAddrModeHandler_Data(void) {
int EffectiveAddress;
EffectiveAddress=(WholeRam[ProgramCounter++]+XReg) & 255;
return(WholeRam[EffectiveAddress]);
} /* ZeroPgXAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Zero page wih X offset addressing mode handler */
INLINE static int16 ZeroPgXAddrModeHandler_Address(void) {
int EffectiveAddress;
EffectiveAddress=(WholeRam[ProgramCounter++]+XReg) & 255;
return(EffectiveAddress);
} /* ZeroPgXAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Absolute with X offset addressing mode handler */
INLINE static int16 AbsXAddrModeHandler_Data(void) {
int EffectiveAddress;
GETTWOBYTEFROMPC(EffectiveAddress);
EffectiveAddress+=XReg;
EffectiveAddress&=0xffff;
return(BEEBREADMEM_FAST(EffectiveAddress));
} /* AbsXAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Absolute with X offset addressing mode handler */
INLINE static int16 AbsXAddrModeHandler_Address(void) {
int EffectiveAddress;
GETTWOBYTEFROMPC(EffectiveAddress)
EffectiveAddress+=XReg;
EffectiveAddress&=0xffff;
return(EffectiveAddress);
} /* AbsXAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Absolute with Y offset addressing mode handler */
INLINE static int16 AbsYAddrModeHandler_Data(void) {
int EffectiveAddress;
GETTWOBYTEFROMPC(EffectiveAddress)
EffectiveAddress+=YReg;
return(BEEBREADMEM_FAST(EffectiveAddress));
} /* AbsYAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Absolute with Y offset addressing mode handler */
INLINE static int16 AbsYAddrModeHandler_Address(void) {
int EffectiveAddress;
GETTWOBYTEFROMPC(EffectiveAddress)
EffectiveAddress+=YReg;
return(EffectiveAddress);
} /* AbsYAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Indirect addressing mode handler */
INLINE static int16 IndAddrModeHandler_Address(void) {
/* For jump indirect only */
int VectorLocation;
int EffectiveAddress;
GETTWOBYTEFROMPC(VectorLocation)
EffectiveAddress=BEEBREADMEM_FAST(VectorLocation);
EffectiveAddress|=BEEBREADMEM_FAST(VectorLocation+1) << 8;
return(EffectiveAddress);
} /* IndAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Zero page with Y offset addressing mode handler */
INLINE static int16 ZeroPgYAddrModeHandler_Data(void) {
int EffectiveAddress;
EffectiveAddress=(WholeRam[ProgramCounter++]+YReg) & 255;
return(WholeRam[EffectiveAddress]);
} /* ZeroPgYAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Zero page with Y offset addressing mode handler */
INLINE static int16 ZeroPgYAddrModeHandler_Address(void) {
int EffectiveAddress;
EffectiveAddress=(WholeRam[ProgramCounter++]+YReg) & 255;
return(EffectiveAddress);
} /* ZeroPgYAddrModeHandler */
/*-------------------------------------------------------------------------*/
/* Initialise 6502core */
void Init6502core(void) {
ProgramCounter=BeebReadMem(0xfffc) | (BeebReadMem(0xfffd)<<8);
Accumulator=XReg=YReg=0; /* For consistancy of execution */
StackReg=0xff; /* Initial value ? */
PSR=FlagI; /* Interrupts off for starters */
intStatus=0;
NMIStatus=0;
NMILock=0;
} /* Init6502core */
#include "via.h"
/*-------------------------------------------------------------------------*/
INLINE void DoInterrupt(void) {
PushWord(ProgramCounter);
Push(PSR & ~FlagB);
ProgramCounter=BeebReadMem(0xfffe) | (BeebReadMem(0xffff)<<8);
SetPSR(FlagI,0,0,1,0,0,0,0);
} /* DoInterrupt */
/*-------------------------------------------------------------------------*/
INLINE void DoNMI(void) {
/*std::cerr << "Doing NMI\n"; */
NMILock=1;
PushWord(ProgramCounter);
Push(PSR);
ProgramCounter=BeebReadMem(0xfffa) | (BeebReadMem(0xfffb)<<8);
SetPSR(FlagI,0,0,1,0,0,0,0); /* Normal interrupts should be disabled during NMI ? */
} /* DoNMI */
// Will sync with screen refresh rate.
//XXvoid ExecVideoFrameOfInstructions(void){
//XX unsigned int i=0,f;
//XX
//XX f = mainWin->currentframe();
//XX while(f==mainWin->currentframe()){
//XX Exec6502Instruction();
//XX }
//XX
//XX}
/*-------------------------------------------------------------------------*/
/* Execute one 6502 instruction, move program counter on */
void Exec6502Instruction(void) {
static int CurrentInstruction;
static int tmpaddr;
static int OldNMIStatus;
int loop;
//== for(loop=0;loop<512;loop++) {
//## I'm thinking that we could estimate the number of cycles per frame and just loop it here?
// for (loop=0; loop<20000; loop++) {
// for (loop=0; loop<1000; loop++) {
// for (loop=0; loop<5000; loop++) {
// for (loop=0; loop<4096; loop++) {
for (loop=0; loop<2048; loop++) {
// for (loop=0; loop<1024; loop++) {
/* Read an instruction and post inc program couter */
CurrentInstruction=WholeRam[ProgramCounter++];
/*cout << "Fetch at " << hex << (ProgramCounter-1) << " giving 0x" << CurrentInstruction << dec << "\n"; */
Cycles=CyclesTable[CurrentInstruction];
/*Stats[CurrentInstruction]++; */
switch (CurrentInstruction) {
case 0x00:
BRKInstrHandler();
break;
case 0x01:
ORAInstrHandler(IndXAddrModeHandler_Data());
break;
case 0x05:
ORAInstrHandler(WholeRam[WholeRam[ProgramCounter++]]/*zp */);
break;
case 0x06:
ASLInstrHandler(ZeroPgAddrModeHandler_Address());
break;
case 0x08:
Push(PSR); /* PHP */
break;
case 0x09:
ORAInstrHandler(WholeRam[ProgramCounter++]); /* immediate */
break;
case 0x0a:
ASLInstrHandler_Acc();
break;
case 0x0d:
ORAInstrHandler(AbsAddrModeHandler_Data());
break;
case 0x0e:
ASLInstrHandler(AbsAddrModeHandler_Address());
break;
case 0x10:
BPLInstrHandler();
break;
case 0x30:
BMIInstrHandler();
break;
case 0x50:
BVCInstrHandler();
break;
case 0x70:
BVSInstrHandler();
break;
case 0x90:
BCCInstrHandler();
break;
case 0xb0:
BCSInstrHandler();
break;
case 0xd0:
BNEInstrHandler();
break;
case 0xf0:
BEQInstrHandler();
break;
case 0x11:
ORAInstrHandler(IndYAddrModeHandler_Data());
break;
case 0x15:
ORAInstrHandler(ZeroPgXAddrModeHandler_Data());
break;
case 0x16:
ASLInstrHandler(ZeroPgXAddrModeHandler_Address());
break;
case 0x18:
PSR&=255-FlagC; /* CLC */
break;
case 0x19:
ORAInstrHandler(AbsYAddrModeHandler_Data());
break;
case 0x1d:
ORAInstrHandler(AbsXAddrModeHandler_Data());
break;
case 0x1e:
ASLInstrHandler(AbsXAddrModeHandler_Address());
break;
case 0x20:
JSRInstrHandler(AbsAddrModeHandler_Address());
break;
case 0x21:
ANDInstrHandler(IndXAddrModeHandler_Data());
break;
case 0x24:
BITInstrHandler(WholeRam[WholeRam[ProgramCounter++]]/*zp */);
break;
case 0x25:
ANDInstrHandler(WholeRam[WholeRam[ProgramCounter++]]/*zp */);
break;
case 0x26:
ROLInstrHandler(ZeroPgAddrModeHandler_Address());
break;
case 0x28:
PSR=Pop(); /* PLP */
break;
case 0x29:
ANDInstrHandler(WholeRam[ProgramCounter++]); /* immediate */
break;
case 0x2a:
ROLInstrHandler_Acc();
break;
case 0x2c:
BITInstrHandler(AbsAddrModeHandler_Data());
break;
case 0x2d:
ANDInstrHandler(AbsAddrModeHandler_Data());
break;
case 0x2e:
ROLInstrHandler(AbsAddrModeHandler_Address());
break;
case 0x31:
ANDInstrHandler(IndYAddrModeHandler_Data());
break;
case 0x35:
ANDInstrHandler(ZeroPgXAddrModeHandler_Data());
break;
case 0x36:
ROLInstrHandler(ZeroPgXAddrModeHandler_Address());
break;
case 0x38:
PSR|=FlagC; /* SEC */
break;
case 0x39:
ANDInstrHandler(AbsYAddrModeHandler_Data());
break;
case 0x3d:
ANDInstrHandler(AbsXAddrModeHandler_Data());
break;
case 0x3e:
ROLInstrHandler(AbsXAddrModeHandler_Address());
break;
case 0x40:
PSR=Pop(); /* RTI */
ProgramCounter=PopWord();
NMILock=0;
break;
case 0x41:
EORInstrHandler(IndXAddrModeHandler_Data());
break;
case 0x45:
EORInstrHandler(WholeRam[WholeRam[ProgramCounter++]]/*zp */);
break;
case 0x46:
LSRInstrHandler(ZeroPgAddrModeHandler_Address());
break;
case 0x48:
Push(Accumulator); /* PHA */
break;
case 0x49:
EORInstrHandler(WholeRam[ProgramCounter++]); /* immediate */
break;
case 0x4a:
LSRInstrHandler_Acc();
break;
case 0x4c:
ProgramCounter=AbsAddrModeHandler_Address(); /* JMP */
break;
case 0x4d:
EORInstrHandler(AbsAddrModeHandler_Data());
break;
case 0x4e:
LSRInstrHandler(AbsAddrModeHandler_Address());
break;
case 0x51:
EORInstrHandler(IndYAddrModeHandler_Data());
break;
case 0x55:
EORInstrHandler(ZeroPgXAddrModeHandler_Data());
break;
case 0x56:
LSRInstrHandler(ZeroPgXAddrModeHandler_Address());
break;
case 0x58:
PSR&=255-FlagI; /* CLI */
break;
case 0x59:
EORInstrHandler(AbsYAddrModeHandler_Data());
break;
case 0x5d:
EORInstrHandler(AbsXAddrModeHandler_Data());
break;
case 0x5e:
LSRInstrHandler(AbsXAddrModeHandler_Address());
break;