-
Notifications
You must be signed in to change notification settings - Fork 8
/
emu6502.cpp
1181 lines (1037 loc) · 34.4 KB
/
emu6502.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// emu6502.cpp - Emu6502 - MOS6502 Emulator
//
////////////////////////////////////////////////////////////////////////////////
//
// c-simple-emu-cbm (C Portable Version)
// C64/6502 Emulator for Microsoft Windows Console
//
// MIT License
//
// Copyright (c) 2024 by David R. Van Wagner
// davevw.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef WINDOWS
#include <windows.h> // OutputDebugStringA
#define snprintf sprintf_s
#endif
#include "emu6502.h"
Emu6502::Emu6502(Memory* mem)
{
memory = mem;
A = 0;
X = 0;
Y = 0;
S = 0xFF;
N = false;
V = false;
B = false;
D = false;
I = false;
Z = false;
C = false;
PC = 0;
trace = false;
step = false;
quit = false;
}
Emu6502::~Emu6502()
{
delete memory;
}
byte Emu6502::GetMemory(ushort addr)
{
return memory->read(addr);
}
void Emu6502::SetMemory(ushort addr, byte value)
{
memory->write(addr, value);
}
void Emu6502::ResetRun()
{
ushort addr = (ushort)((GetMemory(0xFFFC) | (GetMemory(0xFFFD) << 8))); // RESET vector
Execute(addr);
}
#ifndef WINDOWS
void strcpy_s(char* dest, size_t size, const char* src)
{
strncpy(dest, src, size);
}
void strcat_s(char* dest, size_t size, const char* src)
{
strncat(dest, src, size);
}
#endif
void Emu6502::PHP()
{
int flags = (N ? 0x80 : 0)
| (V ? 0x40 : 0)
| 0x20 // reserved, always set
| 0x10 // break always set when push
| (D ? 0x08 : 0)
| (I ? 0x04 : 0)
| (Z ? 0x02 : 0)
| (C ? 0x01 : 0);
Push(flags);
}
byte Emu6502::LO(ushort value)
{
return (byte)value;
}
byte Emu6502::HI(ushort value)
{
return (byte)(value >> 8);
}
byte Emu6502::Subtract(byte reg, byte value, bool *p_overflow)
{
bool old_reg_neg = (reg & 0x80) != 0;
bool value_neg = (value & 0x80) != 0;
int result = reg - value - (C ? 0 : 1);
N = (result & 0x80) != 0;
C = (result >= 0);
Z = (result == 0);
bool result_neg = (result & 0x80) != 0;
*p_overflow = (old_reg_neg && !value_neg && !result_neg) // neg - pos = pos
|| (!old_reg_neg && value_neg && result_neg); // pos - neg = neg
return (byte)result;
}
byte Emu6502::SubtractWithoutOverflow(byte reg, byte value)
{
C = true; // init for CMP, etc.
bool unused;
return Subtract(reg, value, &unused);
}
void Emu6502::CMP(byte value)
{
SubtractWithoutOverflow(A, value);
}
void Emu6502::CPX(byte value)
{
SubtractWithoutOverflow(X, value);
}
void Emu6502::CPY(byte value)
{
SubtractWithoutOverflow(Y, value);
}
void Emu6502::SetReg(byte *p_reg, int value)
{
*p_reg = (byte)value;
Z = (*p_reg == 0);
N = ((*p_reg & 0x80) != 0);
}
void Emu6502::SetA(int value)
{
SetReg(&A, value);
}
void Emu6502::SetX(int value)
{
SetReg(&X, value);
}
void Emu6502::SetY(int value)
{
SetReg(&Y, value);
}
void Emu6502::SBC(byte value)
{
if (D)
{
int A_dec = (A & 0xF) + ((A >> 4) * 10);
int value_dec = (value & 0xF) + ((value >> 4) * 10);
int result_dec = A_dec - value_dec - (C ? 0 : 1);
C = (result_dec >= 0);
if (!C)
result_dec += 100; // wrap negative value
int result = (result_dec % 10) | (((result_dec / 10) % 10) << 4);
SetA(result);
N = false; // undefined?
V = false; // undefined?
}
else
{
byte result = Subtract(A, value, &V);
SetA(result);
}
}
void Emu6502::ADC(byte value)
{
int result;
if (D)
{
int A_dec = (A & 0xF) + ((A >> 4) * 10);
int value_dec = (value & 0xF) + ((value >> 4) * 10);
int result_dec = A_dec + value_dec + (C ? 1 : 0);
C = (result_dec > 99);
result = (result_dec % 10) | (((result_dec / 10) % 10) << 4);
SetA(result);
Z = (result_dec == 0); // BCD quirk -- 100 doesn't set Z
V = false;
}
else
{
bool A_old_neg = (A & 0x80) != 0;
bool value_neg = (value & 0x80) != 0;
result = A + value + (C ? 1 : 0);
C = (result & 0x100) != 0;
SetA(result);
bool result_neg = (result & 0x80) != 0;
V = (!A_old_neg && !value_neg && result_neg) // pos + pos = neg: overflow
|| (A_old_neg && value_neg && !result_neg); // neg + neg = pos: overflow
}
}
void Emu6502::ORA(int value)
{
SetA(A | value);
}
void Emu6502::EOR(int value)
{
SetA(A ^ value);
}
void Emu6502::AND(int value)
{
SetA(A & value);
}
void Emu6502::BIT(byte value)
{
Z = (A & value) == 0;
N = (value & 0x80) != 0;
V = (value & 0x40) != 0;
}
byte Emu6502::ASL(int value)
{
C = (value & 0x80) != 0;
value = (byte)(value << 1);
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
byte Emu6502::LSR(int value)
{
C = (value & 0x01) != 0;
value = (byte)(value >> 1);
Z = (value == 0);
N = false;
return (byte)value;
}
byte Emu6502::ROL(int value)
{
bool newC = (value & 0x80) != 0;
value = (byte)((value << 1) | (C ? 1 : 0));
C = newC;
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
byte Emu6502::ROR(int value)
{
bool newC = (value & 0x01) != 0;
N = C;
value = (byte)((value >> 1) | (C ? 0x80 : 0));
C = newC;
Z = (value == 0);
return (byte)value;
}
void Emu6502::Push(int value)
{
SetMemory((ushort)(0x100 + (S--)), (byte)value);
}
byte Emu6502::Pop(void)
{
return GetMemory((ushort)(0x100 + (++S)));
}
void Emu6502::PLP()
{
int flags = Pop();
N = (flags & 0x80) != 0;
V = (flags & 0x40) != 0;
B = (flags & 0x10) != 0;
D = (flags & 0x08) != 0;
I = (flags & 0x04) != 0;
Z = (flags & 0x02) != 0;
C = (flags & 0x01) != 0;
}
void Emu6502::PHA()
{
Push(A);
}
void Emu6502::PLA()
{
SetA(Pop());
}
void Emu6502::CLC()
{
C = false;
}
void Emu6502::CLD()
{
D = false;
}
void Emu6502::CLI()
{
I = false;
}
void Emu6502::CLV()
{
V = false;
}
void Emu6502::SEC()
{
C = true;
}
void Emu6502::SED()
{
D = true;
}
void Emu6502::SEI()
{
I = true;
}
byte Emu6502::INC(byte value)
{
++value;
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
void Emu6502::INX()
{
X = INC(X);
}
void Emu6502::INY()
{
Y = INC(Y);
}
byte Emu6502::DEC(byte value)
{
--value;
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
void Emu6502::DEX()
{
X = DEC(X);
}
void Emu6502::DEY()
{
Y = DEC(Y);
}
void Emu6502::NOP()
{
}
void Emu6502::TXA()
{
SetReg(&A, X);
}
void Emu6502::TAX()
{
SetReg(&X, A);
}
void Emu6502::TYA()
{
SetReg(&A, Y);
}
void Emu6502::TAY()
{
SetReg(&Y, A);
}
void Emu6502::TXS()
{
S = X;
}
void Emu6502::TSX()
{
SetReg(&X, S);
}
ushort Emu6502::GetBR(ushort addr, bool *p_conditional, byte *p_bytes)
{
*p_conditional = true;
*p_bytes = 2;
sbyte offset = (sbyte)GetMemory((ushort)(addr + 1));
ushort addr2 = (ushort)(addr + 2 + offset);
return addr2;
}
void Emu6502::BR(bool branch, ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
ushort addr2 = GetBR(*p_addr, p_conditional, p_bytes);
if (branch)
{
*p_addr = addr2;
*p_bytes = 0; // don't advance addr
}
}
void Emu6502::BPL(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(!N, p_addr, p_conditional, p_bytes);
}
void Emu6502::BMI(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(N, p_addr, p_conditional, p_bytes);
}
void Emu6502::BCC(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(!C, p_addr, p_conditional, p_bytes);
}
void Emu6502::BCS(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(C, p_addr, p_conditional, p_bytes);
}
void Emu6502::BVC(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(!V, p_addr, p_conditional, p_bytes);
}
void Emu6502::BVS(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(V, p_addr, p_conditional, p_bytes);
}
void Emu6502::BNE(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(!Z, p_addr, p_conditional, p_bytes);
}
void Emu6502::BEQ(ushort *p_addr, bool *p_conditional, byte *p_bytes)
{
BR(Z, p_addr, p_conditional, p_bytes);
}
void Emu6502::JSR(ushort *p_addr, byte *p_bytes)
{
*p_bytes = 3; // for next calculation
ushort addr2 = (ushort)(*p_addr + *p_bytes - 1);
ushort addr3 = (ushort)(GetMemory((ushort)(*p_addr + 1)) | (GetMemory((ushort)(*p_addr + 2)) << 8));
Push(HI(addr2));
Push(LO(addr2));
*p_addr = addr3;
*p_bytes = 0; // addr already changed
}
void Emu6502::RTS(ushort *p_addr, byte *p_bytes)
{
byte lo = Pop();
byte hi = Pop();
*p_addr = (ushort)(((hi << 8) | lo) + 1);
*p_bytes = 0; // addr already changed
}
void Emu6502::RTI(ushort *p_addr, byte *p_bytes)
{
PLP();
byte lo = Pop();
byte hi = Pop();
*p_bytes = 0; // make sure caller does not increase addr by one
*p_addr = (ushort)((hi << 8) | lo);
}
void Emu6502::BRK(byte *p_bytes)
{
++PC;
++PC;
Push(HI(PC));
Push(LO(PC));
B = true;
PHP();
I = true;
PC = (ushort)(GetMemory(0xFFFE) + (GetMemory(0xFFFF) << 8)); // JMP(IRQ)
*p_bytes = 0;
}
void Emu6502::JMP(ushort *p_addr, byte *p_bytes)
{
*p_bytes = 0; // caller should not advance address
ushort addr2 = (ushort)(GetMemory((ushort)(*p_addr + 1)) | (GetMemory((ushort)(*p_addr + 2)) << 8));
*p_addr = addr2;
}
void Emu6502::JMPIND(ushort *p_addr, byte *p_bytes)
{
*p_bytes = 0; // caller should not advance address
ushort addr2 = (ushort)(GetMemory((ushort)(*p_addr + 1)) | (GetMemory((ushort)(*p_addr + 2)) << 8));
ushort addr3;
if ((addr2 & 0xFF) == 0xFF) // JMP($XXFF) won't go over page boundary
addr3 = (ushort)(GetMemory(addr2) | (GetMemory((ushort)(addr2 - 0xFF)) << 8)); // 6502 "bug" - will use XXFF and XX00 as source of address
else
addr3 = (ushort)(GetMemory(addr2) | (GetMemory((ushort)(addr2 + 1)) << 8));
*p_addr = addr3;
}
// "A:FF X:FF Y:FF S:FF P:XX-XXXXX"
void Emu6502::GetDisplayState(char *state, int state_size)
{
snprintf(state, state_size, "A:%02X X:%02X Y:%02X S:%02X P:%c%c-%c%c%c%c%c",
A,
X,
Y,
S,
N ? 'N' : ' ',
V ? 'V' : ' ',
B ? 'B' : ' ',
D ? 'D' : ' ',
I ? 'I' : ' ',
Z ? 'Z' : ' ',
C ? 'C' : ' '
);
}
byte Emu6502::GetIndX(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
byte zpaddr = (byte)(GetMemory((ushort)(addr + 1)) + X); // address must be within zero page
return GetMemory((ushort)(GetMemory(zpaddr) | (GetMemory((byte)(zpaddr+1)) << 8))); // must keep zpaddr+1 within zero page (byte address)
}
void Emu6502::SetIndX(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
byte zpaddr = (byte)(GetMemory((ushort)(addr + 1)) + X); // address must be within zero page
ushort addr3 = (ushort)(GetMemory(zpaddr) | (GetMemory((byte)(zpaddr+1)) << 8)); // must keep zpaddr+1 within zero page (byte address)
SetMemory(addr3, value);
}
byte Emu6502::GetIndY(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = (ushort)(GetMemory((ushort)(addr + 1)));
ushort addr3 = (ushort)((GetMemory(addr2) | (GetMemory((ushort)(addr2 + 1)) << 8)) + Y);
return GetMemory(addr3);
}
void Emu6502::SetIndY(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = (ushort)(GetMemory((ushort)(addr + 1)));
ushort addr3 = (ushort)((GetMemory(addr2) | (GetMemory((ushort)(addr2 + 1)) << 8)) + Y);
SetMemory(addr3, value);
}
byte Emu6502::GetZP(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = GetMemory((ushort)(addr + 1));
return GetMemory(addr2);
}
void Emu6502::SetZP(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = GetMemory((ushort)(addr + 1));
SetMemory(addr2, value);
}
byte Emu6502::GetZPX(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = GetMemory((ushort)(addr + 1));
return GetMemory((byte)(addr2 + X));
}
void Emu6502::SetZPX(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = GetMemory((ushort)(addr + 1));
SetMemory((byte)(addr2 + X), value);
}
byte Emu6502::GetZPY(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = GetMemory((ushort)(addr + 1));
return GetMemory((byte)(addr2 + Y));
}
void Emu6502::SetZPY(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
ushort addr2 = GetMemory((ushort)(addr + 1));
SetMemory((byte)(addr2 + Y), value);
}
byte Emu6502::GetABS(ushort addr, byte *p_bytes)
{
*p_bytes = 3;
ushort addr2 = (ushort)(GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
return GetMemory(addr2);
}
void Emu6502::SetABS(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 3;
ushort addr2 = (ushort)(GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
SetMemory(addr2, value);
}
byte Emu6502::GetABSX(ushort addr, byte *p_bytes)
{
*p_bytes = 3;
ushort addr2 = (ushort)(GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
return GetMemory((ushort)(addr2 + X));
}
void Emu6502::SetABSX(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 3;
ushort addr2 = (ushort)((GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8)) + X);
SetMemory(addr2, value);
}
byte Emu6502::GetABSY(ushort addr, byte *p_bytes)
{
*p_bytes = 3;
ushort addr2 = (ushort)(GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
return GetMemory((ushort)(addr2 + Y));
}
void Emu6502::SetABSY(byte value, ushort addr, byte *p_bytes)
{
*p_bytes = 3;
ushort addr2 = (ushort)((GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8)) + Y);
SetMemory(addr2, value);
}
byte Emu6502::GetIM(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
return GetMemory((ushort)(addr + 1));
}
void Emu6502::Execute(ushort addr)
{
bool conditional;
byte bytes;
PC = addr;
bool breakpoint = false;
while (true)
{
while (true)
{
if (quit)
return;
bytes = 1;
//if (Breakpoints.Contains(PC))
// breakpoint = true;
if (trace || breakpoint || step)
{
ushort addr2;
char line[27];
char dis[13];
DisassembleLong(PC, &conditional, &bytes, &addr2, dis, sizeof(dis), line, sizeof(line));
char state[33];
GetDisplayState(state, sizeof(state));
char full_line[80];
snprintf(full_line, sizeof(full_line), "%-30s%s\n", line, state);
#ifdef WINDOWS
OutputDebugStringA(full_line);
#else
fprintf(stderr, "%s", full_line);
#endif
if (step)
step = step; // user can put debug breakpoint here to allow stepping
if (breakpoint)
breakpoint = breakpoint; // user can put debug breakpoint here to allow break;
}
if (!ExecutePatch()) // allow execute to be overriden at a specific address
break;
}
switch (GetMemory(PC))
{
case 0x00: BRK(&bytes); break;
case 0x01: ORA(GetIndX(PC, &bytes)); break;
case 0x05: ORA(GetZP(PC, &bytes)); break;
case 0x06: SetZP(ASL(GetZP(PC, &bytes)), PC, &bytes); break;
case 0x08: PHP(); break;
case 0x09: ORA(GetIM(PC, &bytes)); break;
case 0x0A: SetA(ASL(A)); break;
case 0x0D: ORA(GetABS(PC, &bytes)); break;
case 0x0E: SetABS(ASL(GetABS(PC, &bytes)), PC, &bytes); break;
case 0x10: BPL(&PC, &conditional, &bytes); break;
case 0x11: ORA(GetIndY(PC, &bytes)); break;
case 0x15: ORA(GetZPX(PC, &bytes)); break;
case 0x16: SetZPX(ASL(GetZPX(PC, &bytes)), PC, &bytes); break;
case 0x18: CLC(); break;
case 0x19: ORA(GetABSY(PC, &bytes)); break;
case 0x1D: ORA(GetABSX(PC, &bytes)); break;
case 0x1E: SetABSX(ASL(GetABSX(PC, &bytes)), PC, &bytes); break;
case 0x20: JSR(&PC, &bytes); break;
case 0x21: AND(GetIndX(PC, &bytes)); break;
case 0x24: BIT(GetZP(PC, &bytes)); break;
case 0x25: AND(GetZP(PC, &bytes)); break;
case 0x26: SetZP(ROL(GetZP(PC, &bytes)), PC, &bytes); break;
case 0x28: PLP(); break;
case 0x29: AND(GetIM(PC, &bytes)); break;
case 0x2A: SetA(ROL(A)); break;
case 0x2C: BIT(GetABS(PC, &bytes)); break;
case 0x2D: AND(GetABS(PC, &bytes)); break;
case 0x2E: SetABS(ROL(GetABS(PC, &bytes)), PC, &bytes); break;
case 0x30: BMI(&PC, &conditional, &bytes); break;
case 0x31: AND(GetIndY(PC, &bytes)); break;
case 0x35: AND(GetZPX(PC, &bytes)); break;
case 0x36: SetZPX(ROL(GetZPX(PC, &bytes)), PC, &bytes); break;
case 0x38: SEC(); break;
case 0x39: AND(GetABSY(PC, &bytes)); break;
case 0x3D: AND(GetABSX(PC, &bytes)); break;
case 0x3E: SetABSX(ROL(GetABSX(PC, &bytes)), PC, &bytes); break;
case 0x40: RTI(&PC, &bytes); break;
case 0x41: EOR(GetIndX(PC, &bytes)); break;
case 0x45: EOR(GetZP(PC, &bytes)); break;
case 0x46: SetZP(LSR(GetZP(PC, &bytes)), PC, &bytes); break;
case 0x48: PHA(); break;
case 0x49: EOR(GetIM(PC, &bytes)); break;
case 0x4A: SetA(LSR(A)); break;
case 0x4C: JMP(&PC, &bytes); break;
case 0x4D: EOR(GetABS(PC, &bytes)); break;
case 0x4E: SetABS(LSR(GetABS(PC, &bytes)), PC, &bytes); break;
case 0x50: BVC(&PC, &conditional, &bytes); break;
case 0x51: EOR(GetIndY(PC, &bytes)); break;
case 0x55: EOR(GetZPX(PC, &bytes)); break;
case 0x56: SetZPX(LSR(GetZPX(PC, &bytes)), PC, &bytes); break;
case 0x58: CLI(); break;
case 0x59: EOR(GetABSY(PC, &bytes)); break;
case 0x5D: EOR(GetABSX(PC, &bytes)); break;
case 0x5E: SetABSX(LSR(GetABSX(PC, &bytes)), PC, &bytes); break;
case 0x60: RTS(&PC, &bytes); break;
case 0x61: ADC(GetIndX(PC, &bytes)); break;
case 0x65: ADC(GetZP(PC, &bytes)); break;
case 0x66: SetZP(ROR(GetZP(PC, &bytes)), PC, &bytes); break;
case 0x68: PLA(); break;
case 0x69: ADC(GetIM(PC, &bytes)); break;
case 0x6A: SetA(ROR(A)); break;
case 0x6C: JMPIND(&PC, &bytes); break;
case 0x6D: ADC(GetABS(PC, &bytes)); break;
case 0x6E: SetABS(ROR(GetABS(PC, &bytes)), PC, &bytes); break;
case 0x70: BVS(&PC, &conditional, &bytes); break;
case 0x71: ADC(GetIndY(PC, &bytes)); break;
case 0x75: ADC(GetZPX(PC, &bytes)); break;
case 0x76: SetZPX(ROR(GetZPX(PC, &bytes)), PC, &bytes); break;
case 0x78: SEI(); break;
case 0x79: ADC(GetABSY(PC, &bytes)); break;
case 0x7D: ADC(GetABSX(PC, &bytes)); break;
case 0x7E: SetABSX(ROR(GetABSX(PC, &bytes)), PC, &bytes); break;
case 0x81: SetIndX(A, PC, &bytes); break;
case 0x84: SetZP(Y, PC, &bytes); break;
case 0x85: SetZP(A, PC, &bytes); break;
case 0x86: SetZP(X, PC, &bytes); break;
case 0x88: DEY(); break;
case 0x8A: TXA(); break;
case 0x8C: SetABS(Y, PC, &bytes); break;
case 0x8D: SetABS(A, PC, &bytes); break;
case 0x8E: SetABS(X, PC, &bytes); break;
case 0x90: BCC(&PC, &conditional, &bytes); break;
case 0x91: SetIndY(A, PC, &bytes); break;
case 0x94: SetZPX(Y, PC, &bytes); break;
case 0x95: SetZPX(A, PC, &bytes); break;
case 0x96: SetZPY(X, PC, &bytes); break;
case 0x98: TYA(); break;
case 0x99: SetABSY(A, PC, &bytes); break;
case 0x9A: TXS(); break;
case 0x9D: SetABSX(A, PC, &bytes); break;
case 0xA0: SetY(GetIM(PC, &bytes)); break;
case 0xA1: SetA(GetIndX(PC, &bytes)); break;
case 0xA2: SetX(GetIM(PC, &bytes)); break;
case 0xA4: SetY(GetZP(PC, &bytes)); break;
case 0xA5: SetA(GetZP(PC, &bytes)); break;
case 0xA6: SetX(GetZP(PC, &bytes)); break;
case 0xA8: TAY(); break;
case 0xA9: SetA(GetIM(PC, &bytes)); break;
case 0xAA: TAX(); break;
case 0xAC: SetY(GetABS(PC, &bytes)); break;
case 0xAD: SetA(GetABS(PC, &bytes)); break;
case 0xAE: SetX(GetABS(PC, &bytes)); break;
case 0xB0: BCS(&PC, &conditional, &bytes); break;
case 0xB1: SetA(GetIndY(PC, &bytes)); break;
case 0xB4: SetY(GetZPX(PC, &bytes)); break;
case 0xB5: SetA(GetZPX(PC, &bytes)); break;
case 0xB6: SetX(GetZPY(PC, &bytes)); break;
case 0xB8: CLV(); break;
case 0xB9: SetA(GetABSY(PC, &bytes)); break;
case 0xBA: TSX(); break;
case 0xBC: SetY(GetABSX(PC, &bytes)); break;
case 0xBD: SetA(GetABSX(PC, &bytes)); break;
case 0xBE: SetX(GetABSY(PC, &bytes)); break;
case 0xC0: CPY(GetIM(PC, &bytes)); break;
case 0xC1: CMP(GetIndX(PC, &bytes)); break;
case 0xC4: CPY(GetZP(PC, &bytes)); break;
case 0xC5: CMP(GetZP(PC, &bytes)); break;
case 0xC6: SetZP(DEC(GetZP(PC, &bytes)), PC, &bytes); break;
case 0xC8: INY(); break;
case 0xC9: CMP(GetIM(PC, &bytes)); break;
case 0xCA: DEX(); break;
case 0xCC: CPY(GetABS(PC, &bytes)); break;
case 0xCD: CMP(GetABS(PC, &bytes)); break;
case 0xCE: SetABS(DEC(GetABS(PC, &bytes)), PC, &bytes); break;
case 0xD0: BNE(&PC, &conditional, &bytes); break;
case 0xD1: CMP(GetIndY(PC, &bytes)); break;
case 0xD5: CMP(GetZPX(PC, &bytes)); break;
case 0xD6: SetZPX(DEC(GetZPX(PC, &bytes)), PC, &bytes); break;
case 0xD8: CLD(); break;
case 0xD9: CMP(GetABSY(PC, &bytes)); break;
case 0xDD: CMP(GetABSX(PC, &bytes)); break;
case 0xDE: SetABSX(DEC(GetABSX(PC, &bytes)), PC, &bytes); break;
case 0xE0: CPX(GetIM(PC, &bytes)); break;
case 0xE1: SBC(GetIndX(PC, &bytes)); break;
case 0xE4: CPX(GetZP(PC, &bytes)); break;
case 0xE5: SBC(GetZP(PC, &bytes)); break;
case 0xE6: SetZP(INC(GetZP(PC, &bytes)), PC, &bytes); break;
case 0xE8: INX(); break;
case 0xE9: SBC(GetIM(PC, &bytes)); break;
case 0xEA: NOP(); break;
case 0xEC: CPX(GetABS(PC, &bytes)); break;
case 0xED: SBC(GetABS(PC, &bytes)); break;
case 0xEE: SetABS(INC(GetABS(PC, &bytes)), PC, &bytes); break;
case 0xF0: BEQ(&PC, &conditional, &bytes); break;
case 0xF1: SBC(GetIndY(PC, &bytes)); break;
case 0xF5: SBC(GetZPX(PC, &bytes)); break;
case 0xF6: SetZPX(INC(GetZPX(PC, &bytes)), PC, &bytes); break;
case 0xF8: SED(); break;
case 0xF9: SBC(GetABSY(PC, &bytes)); break;
case 0xFD: SBC(GetABSX(PC, &bytes)); break;
case 0xFE: SetABSX(INC(GetABSX(PC, &bytes)), PC, &bytes); break;
default:
{
printf("Invalid opcode %02X at %04X", GetMemory(PC), PC);
exit(1);
}
}
PC += bytes;
}
}
// Examples:
// FFFF FF FF FF JMP ($FFFF)
// FFFF FF FF FF LDA $FFFF,X
void Emu6502::DisassembleLong(ushort addr, bool *p_conditional, byte *p_bytes, ushort *p_addr2, char *dis, int dis_size, char *line, int line_size)
{
DisassembleShort(addr, p_conditional, p_bytes, p_addr2, dis, dis_size);
snprintf(line, line_size, "%04X ", addr);
for (int i = 0; i < 3; ++i)
{
if (i < *p_bytes)
snprintf(line+strlen(line), line_size-strlen(line), "%02X ", GetMemory((ushort)(addr + i)));
else
strcat_s(line, line_size, " ");
}
strcat_s(line, line_size, dis);
}
void Emu6502::Ind(char *dis, int dis_size, const char* opcode, ushort addr, ushort *p_addr2, byte *p_bytes)
{
*p_bytes = 3;
ushort addr1 = (ushort)(GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
*p_addr2 = (ushort)(GetMemory(addr1) | (GetMemory((ushort)(addr1 + 1)) << 8));
snprintf(dis, dis_size, "%s ($%04X)", opcode, addr1);
}
void Emu6502::IndX(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
snprintf(dis, dis_size, "%s ($%02X,X)", opcode, GetMemory((ushort)(addr + 1)));
}
void Emu6502::IndY(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
snprintf(dis, dis_size, "%s ($%02X),Y", opcode, GetMemory((ushort)(addr + 1)));
}
void Emu6502::ZP(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
snprintf(dis, dis_size, "%s $%02X", opcode, GetMemory((ushort)(addr + 1)));
}
void Emu6502::ZPX(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
snprintf(dis, dis_size, "%s $%02X,X", opcode, GetMemory((ushort)(addr + 1)));
}
void Emu6502::ZPY(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
snprintf(dis, dis_size, "%s $%02X,Y", opcode, GetMemory((ushort)(addr + 1)));
}
void Emu6502::ABS(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 3;
snprintf(dis, dis_size, "%s $%04X", opcode, GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
}
void Emu6502::ABSAddr(char *dis, int dis_size, const char* opcode, ushort addr, ushort *p_addr2, byte *p_bytes)
{
*p_bytes = 3;
*p_addr2 = (ushort)(GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
snprintf(dis, dis_size, "%s $%04X", opcode, *p_addr2);
}
void Emu6502::ABSX(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 3;
snprintf(dis, dis_size, "%s $%04X,X", opcode, GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
}
void Emu6502::ABSY(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 3;
snprintf(dis, dis_size, "%s $%04X,Y", opcode, GetMemory((ushort)(addr + 1)) | (GetMemory((ushort)(addr + 2)) << 8));
}
void Emu6502::IM(char *dis, int dis_size, const char* opcode, ushort addr, byte *p_bytes)
{
*p_bytes = 2;
snprintf(dis, dis_size, "%s #$%02X", opcode, GetMemory((ushort)(addr + 1)));
}
void Emu6502::BRX(char *dis, int dis_size, const char* opcode, ushort addr, bool *p_conditional, ushort *p_addr2, byte *p_bytes)
{
*p_bytes = 2;
*p_conditional = true;
sbyte offset = (sbyte)GetMemory((ushort)(addr + 1));
*p_addr2 = (ushort)(addr + 2 + offset);
snprintf(dis, dis_size, "%s $%04X", opcode, *p_addr2);
}
// JMP ($FFFF)
// LDA $FFFF,X