forked from TASEmulators/BizHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cpu.cs
3485 lines (2949 loc) · 78.9 KB
/
Cpu.cs
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
using System;
using System.Globalization;
namespace Jellyfish.Virtu
{
public interface ICpu
{
// ReSharper disable once UnusedMember.Global
void Reset();
// ReSharper disable once UnusedMember.Global
int Execute();
long Cycles { get; }
int Multiplier { get; }
// ReSharper disable once UnusedMember.Global
void Sync(IComponentSerializer ser);
}
// ReSharper disable once UnusedMember.Global
public sealed partial class Cpu : ICpu
{
private readonly IMemoryBus _memory;
public Cpu(IMemoryBus memory)
{
_memory = memory;
InitializeOpCodeDelegates();
Is65C02 = true;
Multiplier = 1;
RS = 0xFF;
}
private void InitializeOpCodeDelegates()
{
_executeOpCode65N02 = new Action[]
{
Execute65X02Brk00, Execute65X02Ora01, Execute65N02Nop02, Execute65N02Nop03,
Execute65N02Nop04, Execute65X02Ora05, Execute65X02Asl06, Execute65N02Nop07,
Execute65X02Php08, Execute65X02Ora09, Execute65X02Asl0A, Execute65N02Nop0B,
Execute65N02Nop0C, Execute65X02Ora0D, Execute65X02Asl0E, Execute65N02Nop0F,
Execute65X02Bpl10, Execute65X02Ora11, Execute65N02Nop12, Execute65N02Nop13,
Execute65N02Nop14, Execute65X02Ora15, Execute65X02Asl16, Execute65N02Nop17,
Execute65X02Clc18, Execute65X02Ora19, Execute65N02Nop1A, Execute65N02Nop1B,
Execute65N02Nop1C, Execute65X02Ora1D, Execute65N02Asl1E, Execute65N02Nop1F,
Execute65X02Jsr20, Execute65X02And21, Execute65N02Nop22, Execute65N02Nop23,
Execute65X02Bit24, Execute65X02And25, Execute65X02Rol26, Execute65N02Nop27,
Execute65X02Plp28, Execute65X02And29, Execute65X02Rol2A, Execute65N02Nop2B,
Execute65X02Bit2C, Execute65X02And2D, Execute65X02Rol2E, Execute65N02Nop2F,
Execute65X02Bmi30, Execute65X02And31, Execute65N02Nop32, Execute65N02Nop33,
Execute65N02Nop34, Execute65X02And35, Execute65X02Rol36, Execute65N02Nop37,
Execute65X02Sec38, Execute65X02And39, Execute65N02Nop3A, Execute65N02Nop3B,
Execute65N02Nop3C, Execute65X02And3D, Execute65N02Rol3E, Execute65N02Nop3F,
Execute65X02Rti40, Execute65X02Eor41, Execute65N02Nop42, Execute65N02Nop43,
Execute65N02Nop44, Execute65X02Eor45, Execute65X02Lsr46, Execute65N02Nop47,
Execute65X02Pha48, Execute65X02Eor49, Execute65X02Lsr4A, Execute65N02Nop4B,
Execute65X02Jmp4C, Execute65X02Eor4D, Execute65X02Lsr4E, Execute65N02Nop4F,
Execute65X02Bvc50, Execute65X02Eor51, Execute65N02Nop52, Execute65N02Nop53,
Execute65N02Nop54, Execute65X02Eor55, Execute65X02Lsr56, Execute65N02Nop57,
Execute65X02Cli58, Execute65X02Eor59, Execute65N02Nop5A, Execute65N02Nop5B,
Execute65N02Nop5C, Execute65X02Eor5D, Execute65N02Lsr5E, Execute65N02Nop5F,
Execute65X02Rts60, Execute65N02Adc61, Execute65N02Nop62, Execute65N02Nop63,
Execute65N02Nop64, Execute65N02Adc65, Execute65X02Ror66, Execute65N02Nop67,
Execute65X02Pla68, Execute65N02Adc69, Execute65X02Ror6A, Execute65N02Nop6B,
Execute65N02Jmp6C, Execute65N02Adc6D, Execute65X02Ror6E, Execute65N02Nop6F,
Execute65X02Bvs70, Execute65N02Adc71, Execute65N02Nop72, Execute65N02Nop73,
Execute65N02Nop74, Execute65N02Adc75, Execute65X02Ror76, Execute65N02Nop77,
Execute65X02Sei78, Execute65N02Adc79, Execute65N02Nop7A, Execute65N02Nop7B,
Execute65N02Nop7C, Execute65N02Adc7D, Execute65N02Ror7E, Execute65N02Nop7F,
Execute65N02Nop80, Execute65X02Sta81, Execute65N02Nop82, Execute65N02Nop83,
Execute65X02Sty84, Execute65X02Sta85, Execute65X02Stx86, Execute65N02Nop87,
Execute65X02Dey88, Execute65N02Nop89, Execute65X02Txa8A, Execute65N02Nop8B,
Execute65X02Sty8C, Execute65X02Sta8D, Execute65X02Stx8E, Execute65N02Nop8F,
Execute65X02Bcc90, Execute65X02Sta91, Execute65N02Nop92, Execute65N02Nop93,
Execute65X02Sty94, Execute65X02Sta95, Execute65X02Stx96, Execute65N02Nop97,
Execute65X02Tya98, Execute65X02Sta99, Execute65X02Txs9A, Execute65N02Nop9B,
Execute65N02Nop9C, Execute65X02Sta9D, Execute65N02Nop9E, Execute65N02Nop9F,
Execute65X02LdyA0, Execute65X02LdaA1, Execute65X02LdxA2, Execute65N02NopA3,
Execute65X02LdyA4, Execute65X02LdaA5, Execute65X02LdxA6, Execute65N02NopA7,
Execute65X02TayA8, Execute65X02LdaA9, Execute65X02TaxAA, Execute65N02NopAB,
Execute65X02LdyAC, Execute65X02LdaAD, Execute65X02LdxAE, Execute65N02NopAF,
Execute65X02BcsB0, Execute65X02LdaB1, Execute65N02NopB2, Execute65N02NopB3,
Execute65X02LdyB4, Execute65X02LdaB5, Execute65X02LdxB6, Execute65N02NopB7,
Execute65X02ClvB8, Execute65X02LdaB9, Execute65X02TsxBA, Execute65N02NopBB,
Execute65X02LdyBC, Execute65X02LdaBD, Execute65X02LdxBE, Execute65N02NopBF,
Execute65X02CpyC0, Execute65X02CmpC1, Execute65N02NopC2, Execute65N02NopC3,
Execute65X02CpyC4, Execute65X02CmpC5, Execute65X02DecC6, Execute65N02NopC7,
Execute65X02InyC8, Execute65X02CmpC9, Execute65X02DexCA, Execute65N02NopCB,
Execute65X02CpyCC, Execute65X02CmpCD, Execute65X02DecCE, Execute65N02NopCF,
Execute65X02BneD0, Execute65X02CmpD1, Execute65N02NopD2, Execute65N02NopD3,
Execute65N02NopD4, Execute65X02CmpD5, Execute65X02DecD6, Execute65N02NopD7,
Execute65X02CldD8, Execute65X02CmpD9, Execute65N02NopDA, Execute65N02NopDB,
Execute65N02NopDC, Execute65X02CmpDD, Execute65N02DecDE, Execute65N02NopDF,
Execute65X02CpxE0, Execute65N02SbcE1, Execute65N02NopE2, Execute65N02NopE3,
Execute65X02CpxE4, Execute65N02SbcE5, Execute65X02IncE6, Execute65N02NopE7,
Execute65X02InxE8, Execute65N02SbcE9, Execute65X02NopEA, Execute65N02NopEB,
Execute65X02CpxEC, Execute65N02SbcED, Execute65X02IncEE, Execute65N02NopEF,
Execute65X02BeqF0, Execute65N02SbcF1, Execute65N02NopF2, Execute65N02NopF3,
Execute65N02NopF4, Execute65N02SbcF5, Execute65X02IncF6, Execute65N02NopF7,
Execute65X02SedF8, Execute65N02SbcF9, Execute65N02NopFA, Execute65N02NopFB,
Execute65N02NopFC, Execute65N02SbcFD, Execute65N02IncFE, Execute65N02NopFF
};
_executeOpCode65C02 = new Action[]
{
Execute65X02Brk00, Execute65X02Ora01, Execute65C02Nop02, Execute65C02Nop03,
Execute65C02Tsb04, Execute65X02Ora05, Execute65X02Asl06, Execute65C02Nop07,
Execute65X02Php08, Execute65X02Ora09, Execute65X02Asl0A, Execute65C02Nop0B,
Execute65C02Tsb0C, Execute65X02Ora0D, Execute65X02Asl0E, Execute65C02Nop0F,
Execute65X02Bpl10, Execute65X02Ora11, Execute65C02Ora12, Execute65C02Nop13,
Execute65C02Trb14, Execute65X02Ora15, Execute65X02Asl16, Execute65C02Nop17,
Execute65X02Clc18, Execute65X02Ora19, Execute65C02Ina1A, Execute65C02Nop1B,
Execute65C02Trb1C, Execute65X02Ora1D, Execute65C02Asl1E, Execute65C02Nop1F,
Execute65X02Jsr20, Execute65X02And21, Execute65C02Nop22, Execute65C02Nop23,
Execute65X02Bit24, Execute65X02And25, Execute65X02Rol26, Execute65C02Nop27,
Execute65X02Plp28, Execute65X02And29, Execute65X02Rol2A, Execute65C02Nop2B,
Execute65X02Bit2C, Execute65X02And2D, Execute65X02Rol2E, Execute65C02Nop2F,
Execute65X02Bmi30, Execute65X02And31, Execute65C02And32, Execute65C02Nop33,
Execute65C02Bit34, Execute65X02And35, Execute65X02Rol36, Execute65C02Nop37,
Execute65X02Sec38, Execute65X02And39, Execute65C02Dea3A, Execute65C02Nop3B,
Execute65C02Bit3C, Execute65X02And3D, Execute65C02Rol3E, Execute65C02Nop3F,
Execute65X02Rti40, Execute65X02Eor41, Execute65C02Nop42, Execute65C02Nop43,
Execute65C02Nop44, Execute65X02Eor45, Execute65X02Lsr46, Execute65C02Nop47,
Execute65X02Pha48, Execute65X02Eor49, Execute65X02Lsr4A, Execute65C02Nop4B,
Execute65X02Jmp4C, Execute65X02Eor4D, Execute65X02Lsr4E, Execute65C02Nop4F,
Execute65X02Bvc50, Execute65X02Eor51, Execute65C02Eor52, Execute65C02Nop53,
Execute65C02Nop54, Execute65X02Eor55, Execute65X02Lsr56, Execute65C02Nop57,
Execute65X02Cli58, Execute65X02Eor59, Execute65C02Phy5A, Execute65C02Nop5B,
Execute65C02Nop5C, Execute65X02Eor5D, Execute65C02Lsr5E, Execute65C02Nop5F,
Execute65X02Rts60, Execute65C02Adc61, Execute65C02Nop62, Execute65C02Nop63,
Execute65C02Stz64, Execute65C02Adc65, Execute65X02Ror66, Execute65C02Nop67,
Execute65X02Pla68, Execute65C02Adc69, Execute65X02Ror6A, Execute65C02Nop6B,
Execute65C02Jmp6C, Execute65C02Adc6D, Execute65X02Ror6E, Execute65C02Nop6F,
Execute65X02Bvs70, Execute65C02Adc71, Execute65C02Adc72, Execute65C02Nop73,
Execute65C02Stz74, Execute65C02Adc75, Execute65X02Ror76, Execute65C02Nop77,
Execute65X02Sei78, Execute65C02Adc79, Execute65C02Ply7A, Execute65C02Nop7B,
Execute65C02Jmp7C, Execute65C02Adc7D, Execute65C02Ror7E, Execute65C02Nop7F,
Execute65C02Bra80, Execute65X02Sta81, Execute65C02Nop82, Execute65C02Nop83,
Execute65X02Sty84, Execute65X02Sta85, Execute65X02Stx86, Execute65C02Nop87,
Execute65X02Dey88, Execute65C02Bit89, Execute65X02Txa8A, Execute65C02Nop8B,
Execute65X02Sty8C, Execute65X02Sta8D, Execute65X02Stx8E, Execute65C02Nop8F,
Execute65X02Bcc90, Execute65X02Sta91, Execute65C02Sta92, Execute65C02Nop93,
Execute65X02Sty94, Execute65X02Sta95, Execute65X02Stx96, Execute65C02Nop97,
Execute65X02Tya98, Execute65X02Sta99, Execute65X02Txs9A, Execute65C02Nop9B,
Execute65C02Stz9C, Execute65X02Sta9D, Execute65C02Stz9E, Execute65C02Nop9F,
Execute65X02LdyA0, Execute65X02LdaA1, Execute65X02LdxA2, Execute65C02NopA3,
Execute65X02LdyA4, Execute65X02LdaA5, Execute65X02LdxA6, Execute65C02NopA7,
Execute65X02TayA8, Execute65X02LdaA9, Execute65X02TaxAA, Execute65C02NopAB,
Execute65X02LdyAC, Execute65X02LdaAD, Execute65X02LdxAE, Execute65C02NopAF,
Execute65X02BcsB0, Execute65X02LdaB1, Execute65C02LdaB2, Execute65C02NopB3,
Execute65X02LdyB4, Execute65X02LdaB5, Execute65X02LdxB6, Execute65C02NopB7,
Execute65X02ClvB8, Execute65X02LdaB9, Execute65X02TsxBA, Execute65C02NopBB,
Execute65X02LdyBC, Execute65X02LdaBD, Execute65X02LdxBE, Execute65C02NopBF,
Execute65X02CpyC0, Execute65X02CmpC1, Execute65C02NopC2, Execute65C02NopC3,
Execute65X02CpyC4, Execute65X02CmpC5, Execute65X02DecC6, Execute65C02NopC7,
Execute65X02InyC8, Execute65X02CmpC9, Execute65X02DexCA, Execute65C02NopCB,
Execute65X02CpyCC, Execute65X02CmpCD, Execute65X02DecCE, Execute65C02NopCF,
Execute65X02BneD0, Execute65X02CmpD1, Execute65C02CmpD2, Execute65C02NopD3,
Execute65C02NopD4, Execute65X02CmpD5, Execute65X02DecD6, Execute65C02NopD7,
Execute65X02CldD8, Execute65X02CmpD9, Execute65C02PhxDA, Execute65C02NopDB,
Execute65C02NopDC, Execute65X02CmpDD, Execute65C02DecDE, Execute65C02NopDF,
Execute65X02CpxE0, Execute65C02SbcE1, Execute65C02NopE2, Execute65C02NopE3,
Execute65X02CpxE4, Execute65C02SbcE5, Execute65X02IncE6, Execute65C02NopE7,
Execute65X02InxE8, Execute65C02SbcE9, Execute65X02NopEA, Execute65C02NopEB,
Execute65X02CpxEC, Execute65C02SbcED, Execute65X02IncEE, Execute65C02NopEF,
Execute65X02BeqF0, Execute65C02SbcF1, Execute65C02SbcF2, Execute65C02NopF3,
Execute65C02NopF4, Execute65C02SbcF5, Execute65X02IncF6, Execute65C02NopF7,
Execute65X02SedF8, Execute65C02SbcF9, Execute65C02PlxFA, Execute65C02NopFB,
Execute65C02NopFC, Execute65C02SbcFD, Execute65C02IncFE, Execute65C02NopFF
};
}
public void Reset()
{
RS = (RS - 3) & 0xFF; // [4-14]
RPC = _memory.ReadRomRegionE0FF(0xFFFC) | (_memory.ReadRomRegionE0FF(0xFFFD) << 8);
RP |= (Pb | Pi);
if (Is65C02) // [C-10]
{
RP &= ~Pd;
}
}
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "A = 0x{0:X2} X = 0x{1:X2} Y = 0x{2:X2} P = 0x{3:X2} S = 0x01{4:X2} PC = 0x{5:X4} EA = 0x{6:X4} CC = {7}",
RA, RX, RY, RP, RS, RPC, EA, CC);
}
private string[] TraceState()
{
string[] parts = new string[2];
parts[0] = $"{RPC:X4} {_memory.Read(RPC):X2} {ReadOpcode(RPC)} ";
parts[1] = string.Format(
"A:{0:X2} X:{1:X2} Y:{2:X2} P:{3:X2} SP:{4:X2} {6}{7}{8}{9}{10}{11}{12}{13} Cy:{5}",
RA,
RX,
RY,
RP,
RS,
Cycles,
FlagN ? "N" : "",
FlagV ? "V" : "",
FlagT ? "T" : "",
FlagB ? "B" : "",
FlagD ? "D" : "",
FlagI ? "I" : "",
FlagZ ? "Z" : "",
FlagC ? "C" : "");
return parts;
}
private string ReadOpcode(int pc)
{
//It would be so much better if I could just use the MOS6502X's Disassemble Method here.
if (pc <= 0xFFFD) //sanity check to make sure we don't read from outside address space.
{
switch (_memory.Peek(pc))
{
case 0x0C: return $"NOP (${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4})";
case 0x0D: return $"ORA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x0E: return $"ASL ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x19: return $"ORA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0x1D: return $"ORA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0x1E: return $"ASL ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
case 0x20: return $"JSR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x2C: return $"BIT ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x2D: return $"AND ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x2E: return $"ROL ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x39: return $"AND ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0x3D: return $"AND ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0x3E: return $"ROL ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
case 0x4C: return $"JMP ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x4D: return $"EOR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x4E: return $"LSR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x59: return $"EOR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0x5D: return $"EOR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0x5E: return $"LSR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
case 0x6C: return $"JMP (${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4})";
case 0x6D: return $"ADC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x6E: return $"ROR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x79: return $"ADC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0x7D: return $"ADC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0x7E: return $"ROR ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
case 0x8C: return $"STY ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x8D: return $"STA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x8E: return $"STX ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0x99: return $"STA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y";
case 0x9D: return $"STA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
case 0xAC: return $"LDY ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xAD: return $"LDA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xAE: return $"LDX ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xB9: return $"LDA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0xBC: return $"LDY ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0xBD: return $"LDA ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0xBE: return $"LDX ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0xCC: return $"CPY ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xCD: return $"CMP ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xCE: return $"DEC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xD9: return $"CMP ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0xDD: return $"CMP ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0xDE: return $"DEC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
case 0xEC: return $"CPX ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xED: return $"SBC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xEE: return $"INC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4}";
case 0xF9: return $"SBC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},Y *";
case 0xFD: return $"SBC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X *";
case 0xFE: return $"INC ${_memory.Peek(pc + 1) | _memory.Peek(pc + 2) << 8:X4},X";
}
}
if (pc <= 0xFFFE) //read two-byte opcodes here
{
switch (_memory.Peek(pc))
{
case 0x01: return $"ORA (${_memory.Peek(++pc):X2},X)";
case 0x04: return $"NOP ${_memory.Peek(++pc):X2}";
case 0x05: return $"ORA ${_memory.Peek(++pc):X2}";
case 0x06: return $"ASL ${_memory.Peek(++pc):X2}";
case 0x09: return $"ORA #${_memory.Peek(++pc):X2}";
case 0x10: return $"BPL ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0x11: return $"ORA (${_memory.Peek(++pc):X2}),Y *";
case 0x14: return $"NOP ${_memory.Peek(++pc):X2},X";
case 0x15: return $"ORA ${_memory.Peek(++pc):X2},X";
case 0x16: return $"ASL ${_memory.Peek(++pc):X2},X";
case 0x1C: return $"NOP (${_memory.Peek(++pc):X2},X)";
case 0x21: return $"AND (${_memory.Peek(++pc):X2},X)";
case 0x24: return $"BIT ${_memory.Peek(++pc):X2}";
case 0x25: return $"AND ${_memory.Peek(++pc):X2}";
case 0x26: return $"ROL ${_memory.Peek(++pc):X2}";
case 0x29: return $"AND #${_memory.Peek(++pc):X2}";
case 0x30: return $"BMI ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0x31: return $"AND (${_memory.Peek(++pc):X2}),Y *";
case 0x34: return $"NOP ${_memory.Peek(++pc):X2},X";
case 0x35: return $"AND ${_memory.Peek(++pc):X2},X";
case 0x36: return $"ROL ${_memory.Peek(++pc):X2},X";
case 0x3C: return $"NOP (${_memory.Peek(++pc):X2},X)";
case 0x41: return $"EOR (${_memory.Peek(++pc):X2},X)";
case 0x44: return $"NOP ${_memory.Peek(++pc):X2}";
case 0x45: return $"EOR ${_memory.Peek(++pc):X2}";
case 0x46: return $"LSR ${_memory.Peek(++pc):X2}";
case 0x49: return $"EOR #${_memory.Peek(++pc):X2}";
case 0x50: return $"BVC ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0x51: return $"EOR (${_memory.Peek(++pc):X2}),Y *";
case 0x54: return $"NOP ${_memory.Peek(++pc):X2},X";
case 0x55: return $"EOR ${_memory.Peek(++pc):X2},X";
case 0x56: return $"LSR ${_memory.Peek(++pc):X2},X";
case 0x5C: return $"NOP (${_memory.Peek(++pc):X2},X)";
case 0x61: return $"ADC (${_memory.Peek(++pc):X2},X)";
case 0x64: return $"NOP ${_memory.Peek(++pc):X2}";
case 0x65: return $"ADC ${_memory.Peek(++pc):X2}";
case 0x66: return $"ROR ${_memory.Peek(++pc):X2}";
case 0x69: return $"ADC #${_memory.Peek(++pc):X2}";
case 0x70: return $"BVS ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0x71: return $"ADC (${_memory.Peek(++pc):X2}),Y *";
case 0x74: return $"NOP ${_memory.Peek(++pc):X2},X";
case 0x75: return $"ADC ${_memory.Peek(++pc):X2},X";
case 0x76: return $"ROR ${_memory.Peek(++pc):X2},X";
case 0x7C: return $"NOP (${_memory.Peek(++pc):X2},X)";
case 0x80: return $"NOP #${_memory.Peek(++pc):X2}";
case 0x81: return $"STA (${_memory.Peek(++pc):X2},X)";
case 0x82: return $"NOP #${_memory.Peek(++pc):X2}";
case 0x84: return $"STY ${_memory.Peek(++pc):X2}";
case 0x85: return $"STA ${_memory.Peek(++pc):X2}";
case 0x86: return $"STX ${_memory.Peek(++pc):X2}";
case 0x89: return $"NOP #${_memory.Peek(++pc):X2}";
case 0x90: return $"BCC ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0x91: return $"STA (${_memory.Peek(++pc):X2}),Y";
case 0x94: return $"STY ${_memory.Peek(++pc):X2},X";
case 0x95: return $"STA ${_memory.Peek(++pc):X2},X";
case 0x96: return $"STX ${_memory.Peek(++pc):X2},Y";
case 0xA0: return $"LDY #${_memory.Peek(++pc):X2}";
case 0xA1: return $"LDA (${_memory.Peek(++pc):X2},X)";
case 0xA2: return $"LDX #${_memory.Peek(++pc):X2}";
case 0xA4: return $"LDY ${_memory.Peek(++pc):X2}";
case 0xA5: return $"LDA ${_memory.Peek(++pc):X2}";
case 0xA6: return $"LDX ${_memory.Peek(++pc):X2}";
case 0xA9: return $"LDA #${_memory.Peek(++pc):X2}";
case 0xB0: return $"BCS ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0xB1: return $"LDA (${_memory.Peek(++pc):X2}),Y *";
case 0xB4: return $"LDY ${_memory.Peek(++pc):X2},X";
case 0xB5: return $"LDA ${_memory.Peek(++pc):X2},X";
case 0xB6: return $"LDX ${_memory.Peek(++pc):X2},Y";
case 0xC0: return $"CPY #${_memory.Peek(++pc):X2}";
case 0xC1: return $"CMP (${_memory.Peek(++pc):X2},X)";
case 0xC2: return $"NOP #${_memory.Peek(++pc):X2}";
case 0xC4: return $"CPY ${_memory.Peek(++pc):X2}";
case 0xC5: return $"CMP ${_memory.Peek(++pc):X2}";
case 0xC6: return $"DEC ${_memory.Peek(++pc):X2}";
case 0xC9: return $"CMP #${_memory.Peek(++pc):X2}";
case 0xD0: return $"BNE ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0xD1: return $"CMP (${_memory.Peek(++pc):X2}),Y *";
case 0xD4: return $"NOP ${_memory.Peek(++pc):X2},X";
case 0xD5: return $"CMP ${_memory.Peek(++pc):X2},X";
case 0xD6: return $"DEC ${_memory.Peek(++pc):X2},X";
case 0xDC: return $"NOP (${_memory.Peek(++pc):X2},X)";
case 0xE0: return $"CPX #${_memory.Peek(++pc):X2}";
case 0xE1: return $"SBC (${_memory.Peek(++pc):X2},X)";
case 0xE2: return $"NOP #${_memory.Peek(++pc):X2}";
case 0xE4: return $"CPX ${_memory.Peek(++pc):X2}";
case 0xE5: return $"SBC ${_memory.Peek(++pc):X2}";
case 0xE6: return $"INC ${_memory.Peek(++pc):X2}";
case 0xE9: return $"SBC #${_memory.Peek(++pc):X2}";
case 0xF0: return $"BEQ ${pc + 2 + (sbyte) _memory.Peek(pc + 1):X4}";
case 0xF1: return $"SBC (${_memory.Peek(++pc):X2}),Y *";
case 0xF4: return $"NOP ${_memory.Peek(++pc):X2},X";
case 0xF5: return $"SBC ${_memory.Peek(++pc):X2},X";
case 0xF6: return $"INC ${_memory.Peek(++pc):X2},X";
case 0xFC: return $"NOP (${_memory.Peek(++pc):X2},X)";
}
}
if (pc <= 0xFFFF) // read one-byte opcodes here
{
switch (_memory.Peek(pc))
{
case 0x00: return "BRK";
case 0x08: return "PHP";
case 0x0A: return "ASL A";
case 0x18: return "CLC";
case 0x1A: return "NOP";
case 0x28: return "PLP";
case 0x2A: return "ROL A";
case 0x38: return "SEC";
case 0x3A: return "NOP";
case 0x40: return "RTI";
case 0x48: return "PHA";
case 0x4A: return "LSR A";
case 0x58: return "CLI";
case 0x5A: return "NOP";
case 0x60: return "RTS";
case 0x68: return "PLA";
case 0x6A: return "ROR A";
case 0x78: return "SEI";
case 0x7A: return "NOP";
case 0x88: return "DEY";
case 0x8A: return "TXA";
case 0x98: return "TYA";
case 0x9A: return "TXS";
case 0xA8: return "TAY";
case 0xAA: return "TAX";
case 0xB8: return "CLV";
case 0xBA: return "TSX";
case 0xC8: return "INY";
case 0xCA: return "DEX";
case 0xD8: return "CLD";
case 0xDA: return "NOP";
case 0xE8: return "INX";
case 0xEA: return "NOP";
case 0xF8: return "SED";
case 0xFA: return "NOP";
}
}
return "---";
}
public int Execute()
{
TraceCallback?.Invoke(TraceState());
CC = 0;
OpCode = _memory.ReadOpcode(RPC);
RPC = (RPC + 1) & 0xFFFF;
_executeOpCode[OpCode]();
Cycles += CC;
return CC;
}
private void GetAddressAbs() // abs
{
EA = _memory.Read(RPC) | (_memory.Read(RPC + 1) << 8);
RPC = (RPC + 2) & 0xFFFF;
}
private void GetAddressAbsX() // abs, x
{
EA = (_memory.Read(RPC) + RX + (_memory.Read(RPC + 1) << 8)) & 0xFFFF;
RPC = (RPC + 2) & 0xFFFF;
}
private void GetAddressAbsXCC() // abs, x
{
int ea = _memory.Read(RPC) + RX;
EA = (ea + (_memory.Read(RPC + 1) << 8)) & 0xFFFF;
RPC = (RPC + 2) & 0xFFFF;
CC += (ea >> 8);
}
private void GetAddressAbsY() // abs, y
{
EA = (_memory.Read(RPC) + RY + (_memory.Read(RPC + 1) << 8)) & 0xFFFF;
RPC = (RPC + 2) & 0xFFFF;
}
private void GetAddressAbsYCC() // abs, y
{
int ea = _memory.Read(RPC) + RY;
EA = (ea + (_memory.Read(RPC + 1) << 8)) & 0xFFFF;
RPC = (RPC + 2) & 0xFFFF;
CC += (ea >> 8);
}
private void GetAddressZpg() // zpg
{
EA = _memory.Read(RPC);
RPC = (RPC + 1) & 0xFFFF;
}
private void GetAddressZpgInd() // (zpg)
{
int zp = _memory.Read(RPC);
EA = _memory.ReadZeroPage(zp) | (_memory.ReadZeroPage((zp + 1) & 0xFF) << 8);
RPC = (RPC + 1) & 0xFFFF;
}
private void GetAddressZpgIndX() // (zpg, x)
{
int zp = (_memory.Read(RPC) + RX) & 0xFF;
EA = _memory.ReadZeroPage(zp) | (_memory.ReadZeroPage((zp + 1) & 0xFF) << 8);
RPC = (RPC + 1) & 0xFFFF;
}
private void GetAddressZpgIndY() // (zpg), y
{
int zp = _memory.Read(RPC);
EA = (_memory.ReadZeroPage(zp) + RY + (_memory.ReadZeroPage((zp + 1) & 0xFF) << 8)) & 0xFFFF;
RPC = (RPC + 1) & 0xFFFF;
}
private void GetAddressZpgIndYCC() // (zpg), y
{
int zp = _memory.Read(RPC);
int ea = _memory.ReadZeroPage(zp) + RY;
EA = (ea + (_memory.ReadZeroPage((zp + 1) & 0xFF) << 8)) & 0xFFFF;
RPC = (RPC + 1) & 0xFFFF;
CC += (ea >> 8);
}
private void GetAddressZpgX() // zpg, x
{
EA = (_memory.Read(RPC) + RX) & 0xFF;
RPC = (RPC + 1) & 0xFFFF;
}
private void GetAddressZpgY() // zpg, y
{
EA = (_memory.Read(RPC) + RY) & 0xFF;
RPC = (RPC + 1) & 0xFFFF;
}
private int Pull()
{
RS = (RS + 1) & 0xFF;
return _memory.ReadZeroPage(0x0100 + RS);
}
private void Push(int data)
{
_memory.WriteZeroPage(0x0100 + RS, data);
RS = (RS - 1) & 0xFF;
}
private int ReadAbs() // abs
{
return _memory.Read(EA);
}
private int ReadAbsX() // abs, x
{
return _memory.Read(EA);
}
private int ReadAbsY() // abs, y
{
return _memory.Read(EA);
}
private int ReadImm() // imm
{
int data = _memory.Read(RPC);
RPC = (RPC + 1) & 0xFFFF;
return data;
}
private int ReadZpg() // zpg
{
return _memory.ReadZeroPage(EA);
}
private int ReadZpgInd() // (zpg)
{
return _memory.Read(EA);
}
private int ReadZpgIndX() // (zpg, x)
{
return _memory.Read(EA);
}
private int ReadZpgIndY() // (zpg), y
{
return _memory.Read(EA);
}
private int ReadZpgX() // zpg, x
{
return _memory.ReadZeroPage(EA);
}
private int ReadZpgY() // zpg, y
{
return _memory.ReadZeroPage(EA);
}
private void WriteAbs(int data) // abs
{
_memory.Write(EA, data);
}
private void WriteAbsX(int data) // abs, x
{
_memory.Write(EA, data);
}
private void WriteAbsY(int data) // abs, y
{
_memory.Write(EA, data);
}
private void WriteZpg(int data) // zpg
{
_memory.WriteZeroPage(EA, data);
}
private void WriteZpgInd(int data) // (zpg)
{
_memory.Write(EA, data);
}
private void WriteZpgIndX(int data) // (zpg, x)
{
_memory.Write(EA, data);
}
private void WriteZpgIndY(int data) // (zpg), y
{
_memory.Write(EA, data);
}
private void WriteZpgX(int data) // zpg, x
{
_memory.WriteZeroPage(EA, data);
}
private void WriteZpgY(int data) // zpg, y
{
_memory.WriteZeroPage(EA, data);
}
private void ExecuteAdc65N02(int data, int cc)
{
if ((RP & Pd) == 0x0)
{
int ra = RA + data + (RP & Pc);
RP = RP & ~(Pc | Pn | Pv | Pz) | ((ra >> 8) & Pc) | DataPnz[ra & 0xFF] | (((~(RA ^ data) & (RA ^ (ra & 0xFF))) >> 1) & Pv);
RA = ra & 0xFF;
CC += cc;
}
else // decimal
{
int ral = (RA & 0x0F) + (data & 0x0F) + (RP & Pc);
int rah = (RA >> 4) + (data >> 4);
if (ral >= 10)
{
ral -= 10;
rah++;
}
int ra = (ral | (rah << 4)) & 0xFF;
RP = RP & ~(Pc | Pn | Pv | Pz) | DataPn[ra] | (((~(RA ^ data) & (RA ^ ra)) >> 1) & Pv) | DataPz[(RA + data + (RP & Pc)) & 0xFF];
if (rah >= 10)
{
rah -= 10;
RP |= Pc;
}
RA = (ral | (rah << 4)) & 0xFF;
CC += cc;
}
}
private void ExecuteAdc65C02(int data, int cc)
{
if ((RP & Pd) == 0x0)
{
int ra = RA + data + (RP & Pc);
RP = RP & ~(Pc | Pn | Pv | Pz) | ((ra >> 8) & Pc) | DataPnz[ra & 0xFF] | (((~(RA ^ data) & (RA ^ (ra & 0xFF))) >> 1) & Pv);
RA = ra & 0xFF;
CC += cc;
}
else // decimal
{
int ral = (RA & 0x0F) + (data & 0x0F) + (RP & Pc);
int rah = (RA >> 4) + (data >> 4);
if (ral >= 10)
{
ral -= 10;
rah++;
}
RP &= ~Pc;
if (rah >= 10)
{
rah -= 10;
RP |= Pc;
}
int ra = (ral | (rah << 4)) & 0xFF;
RP = RP & ~(Pn | Pv | Pz) | DataPnz[ra] | (((~(RA ^ data) & (RA ^ ra)) >> 1) & Pv);
RA = ra;
CC += cc + 1;
}
}
private void ExecuteAnd(int data, int cc)
{
RA &= data;
RP = RP & ~(Pn | Pz) | DataPnz[RA];
CC += cc;
}
private int ExecuteAsl(int data, int cc)
{
RP = RP & ~Pc | ((data >> 7) & Pc);
data = (data << 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[data];
CC += cc;
return data;
}
private void ExecuteAslImp(int cc)
{
RP = RP & ~Pc | ((RA >> 7) & Pc);
RA = (RA << 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RA];
CC += cc;
}
private void ExecuteBcc(int cc)
{
if ((RP & Pc) == 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBcs(int cc)
{
if ((RP & Pc) != 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBeq(int cc)
{
if ((RP & Pz) != 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBit(int data, int cc)
{
RP = RP & ~(Pn | Pv | Pz) | (data & (Pn | Pv)) | DataPz[RA & data];
CC += cc;
}
private void ExecuteBitImm(int data, int cc)
{
RP = RP & ~Pz | DataPz[RA & data];
CC += cc;
}
private void ExecuteBmi(int cc)
{
if ((RP & Pn) != 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBne(int cc)
{
if ((RP & Pz) == 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBpl(int cc)
{
if ((RP & Pn) == 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBra(int cc)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
private void ExecuteBrk(int cc)
{
int rpc = (RPC + 1) & 0xFFFF; // [4-18]
Push(rpc >> 8);
Push(rpc & 0xFF);
Push(RP | Pb);
RP |= Pi;
RPC = _memory.Read(0xFFFE) | (_memory.Read(0xFFFF) << 8);
CC += cc;
}
private void ExecuteBvc(int cc)
{
if ((RP & Pv) == 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteBvs(int cc)
{
if ((RP & Pv) != 0x0)
{
int rpc = (RPC + 1) & 0xFFFF;
RPC = (RPC + 1 + (sbyte)_memory.Read(RPC)) & 0xFFFF;
CC += cc + 1 + (((RPC ^ rpc) >> 8) & 0x01);
}
else
{
RPC = (RPC + 1) & 0xFFFF;
CC += cc;
}
}
private void ExecuteClc(int cc)
{
RP &= ~Pc;
CC += cc;
}
private void ExecuteCld(int cc)
{
RP &= ~Pd;
CC += cc;
}
private void ExecuteCli(int cc)
{
RP &= ~Pi;
CC += cc;
}
private void ExecuteClv(int cc)
{
RP &= ~Pv;
CC += cc;
}
private void ExecuteCmp(int data, int cc)
{
int diff = RA - data;
RP = RP & ~(Pc | Pn | Pz) | ((~diff >> 8) & Pc) | DataPnz[diff & 0xFF];
CC += cc;
}
private void ExecuteCpx(int data, int cc)
{
int diff = RX - data;
RP = RP & ~(Pc | Pn | Pz) | ((~diff >> 8) & Pc) | DataPnz[diff & 0xFF];
CC += cc;
}
private void ExecuteCpy(int data, int cc)
{
int diff = RY - data;
RP = RP & ~(Pc | Pn | Pz) | ((~diff >> 8) & Pc) | DataPnz[diff & 0xFF];
CC += cc;
}
private void ExecuteDea(int cc)
{
RA = (RA - 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RA];
CC += cc;
}
private int ExecuteDec(int data, int cc)
{
data = (data - 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[data];
CC += cc;
return data;
}
private void ExecuteDex(int cc)
{
RX = (RX - 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RX];
CC += cc;
}
private void ExecuteDey(int cc)
{
RY = (RY - 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RY];
CC += cc;
}
private void ExecuteEor(int data, int cc)
{
RA ^= data;
RP = RP & ~(Pn | Pz) | DataPnz[RA];
CC += cc;
}
private void ExecuteIna(int cc)
{
RA = (RA + 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RA];
CC += cc;
}
private int ExecuteInc(int data, int cc)
{
data = (data + 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[data];
CC += cc;
return data;
}
private void ExecuteInx(int cc)
{
RX = (RX + 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RX];
CC += cc;
}
private void ExecuteIny(int cc)
{
RY = (RY + 1) & 0xFF;
RP = RP & ~(Pn | Pz) | DataPnz[RY];
CC += cc;
}
private void ExecuteJmpAbs(int cc) // jmp abs
{
RPC = _memory.Read(RPC) | (_memory.Read(RPC + 1) << 8);
CC += cc;
}
private void ExecuteJmpAbsInd65N02(int cc) // jmp (abs)
{
int ea = _memory.Read(RPC) | (_memory.Read(RPC + 1) << 8);
RPC = _memory.Read(ea) | (_memory.Read((ea & 0xFF00) | ((ea + 1) & 0x00FF)) << 8);
CC += cc;
}
private void ExecuteJmpAbsInd65C02(int cc) // jmp (abs)
{
int ea = _memory.Read(RPC) | (_memory.Read(RPC + 1) << 8);
RPC = _memory.Read(ea) | (_memory.Read(ea + 1) << 8);
CC += cc;
}
private void ExecuteJmpAbsIndX(int cc) // jmp (abs, x)
{
int ea = (_memory.Read(RPC) + RX + (_memory.Read(RPC + 1) << 8)) & 0xFFFF;
RPC = _memory.Read(ea) | (_memory.Read(ea + 1) << 8);
CC += cc;
}