-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6502proc.cpp
More file actions
1251 lines (1018 loc) · 31.5 KB
/
6502proc.cpp
File metadata and controls
1251 lines (1018 loc) · 31.5 KB
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
#include<bus.h>
#include<6502proc.h>
6502proc::6502proc()
{
using a=6502proc;
lookup=
{
{ "BRK", &a::BRK, &a::IMM, 7 },{ "ORA", &a::ORA, &a::IZX, 6 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 3 },{ "ORA", &a::ORA, &a::ZP0, 3 },{ "ASL", &a::ASL, &a::ZP0, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "PHP", &a::PHP, &a::IMP, 3 },{ "ORA", &a::ORA, &a::IMM, 2 },{ "ASL", &a::ASL, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::NOP, &a::IMP, 4 },{ "ORA", &a::ORA, &a::ABS, 4 },{ "ASL", &a::ASL, &a::ABS, 6 },{ "???", &a::XXX, &a::IMP, 6 },
{ "BPL", &a::BPL, &a::REL, 2 },{ "ORA", &a::ORA, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 4 },{ "ORA", &a::ORA, &a::ZPX, 4 },{ "ASL", &a::ASL, &a::ZPX, 6 },{ "???", &a::XXX, &a::IMP, 6 },{ "CLC", &a::CLC, &a::IMP, 2 },{ "ORA", &a::ORA, &a::ABY, 4 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 7 },{ "???", &a::NOP, &a::IMP, 4 },{ "ORA", &a::ORA, &a::ABX, 4 },{ "ASL", &a::ASL, &a::ABX, 7 },{ "???", &a::XXX, &a::IMP, 7 },
{ "JSR", &a::JSR, &a::ABS, 6 },{ "AND", &a::AND, &a::IZX, 6 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "BIT", &a::BIT, &a::ZP0, 3 },{ "AND", &a::AND, &a::ZP0, 3 },{ "ROL", &a::ROL, &a::ZP0, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "PLP", &a::PLP, &a::IMP, 4 },{ "AND", &a::AND, &a::IMM, 2 },{ "ROL", &a::ROL, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "BIT", &a::BIT, &a::ABS, 4 },{ "AND", &a::AND, &a::ABS, 4 },{ "ROL", &a::ROL, &a::ABS, 6 },{ "???", &a::XXX, &a::IMP, 6 },
{ "BMI", &a::BMI, &a::REL, 2 },{ "AND", &a::AND, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 4 },{ "AND", &a::AND, &a::ZPX, 4 },{ "ROL", &a::ROL, &a::ZPX, 6 },{ "???", &a::XXX, &a::IMP, 6 },{ "SEC", &a::SEC, &a::IMP, 2 },{ "AND", &a::AND, &a::ABY, 4 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 7 },{ "???", &a::NOP, &a::IMP, 4 },{ "AND", &a::AND, &a::ABX, 4 },{ "ROL", &a::ROL, &a::ABX, 7 },{ "???", &a::XXX, &a::IMP, 7 },
{ "RTI", &a::RTI, &a::IMP, 6 },{ "EOR", &a::EOR, &a::IZX, 6 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 3 },{ "EOR", &a::EOR, &a::ZP0, 3 },{ "LSR", &a::LSR, &a::ZP0, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "PHA", &a::PHA, &a::IMP, 3 },{ "EOR", &a::EOR, &a::IMM, 2 },{ "LSR", &a::LSR, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "JMP", &a::JMP, &a::ABS, 3 },{ "EOR", &a::EOR, &a::ABS, 4 },{ "LSR", &a::LSR, &a::ABS, 6 },{ "???", &a::XXX, &a::IMP, 6 },
{ "BVC", &a::BVC, &a::REL, 2 },{ "EOR", &a::EOR, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 4 },{ "EOR", &a::EOR, &a::ZPX, 4 },{ "LSR", &a::LSR, &a::ZPX, 6 },{ "???", &a::XXX, &a::IMP, 6 },{ "CLI", &a::CLI, &a::IMP, 2 },{ "EOR", &a::EOR, &a::ABY, 4 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 7 },{ "???", &a::NOP, &a::IMP, 4 },{ "EOR", &a::EOR, &a::ABX, 4 },{ "LSR", &a::LSR, &a::ABX, 7 },{ "???", &a::XXX, &a::IMP, 7 },
{ "RTS", &a::RTS, &a::IMP, 6 },{ "ADC", &a::ADC, &a::IZX, 6 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 3 },{ "ADC", &a::ADC, &a::ZP0, 3 },{ "ROR", &a::ROR, &a::ZP0, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "PLA", &a::PLA, &a::IMP, 4 },{ "ADC", &a::ADC, &a::IMM, 2 },{ "ROR", &a::ROR, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "JMP", &a::JMP, &a::IND, 5 },{ "ADC", &a::ADC, &a::ABS, 4 },{ "ROR", &a::ROR, &a::ABS, 6 },{ "???", &a::XXX, &a::IMP, 6 },
{ "BVS", &a::BVS, &a::REL, 2 },{ "ADC", &a::ADC, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 4 },{ "ADC", &a::ADC, &a::ZPX, 4 },{ "ROR", &a::ROR, &a::ZPX, 6 },{ "???", &a::XXX, &a::IMP, 6 },{ "SEI", &a::SEI, &a::IMP, 2 },{ "ADC", &a::ADC, &a::ABY, 4 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 7 },{ "???", &a::NOP, &a::IMP, 4 },{ "ADC", &a::ADC, &a::ABX, 4 },{ "ROR", &a::ROR, &a::ABX, 7 },{ "???", &a::XXX, &a::IMP, 7 },
{ "???", &a::NOP, &a::IMP, 2 },{ "STA", &a::STA, &a::IZX, 6 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 6 },{ "STY", &a::STY, &a::ZP0, 3 },{ "STA", &a::STA, &a::ZP0, 3 },{ "STX", &a::STX, &a::ZP0, 3 },{ "???", &a::XXX, &a::IMP, 3 },{ "DEY", &a::DEY, &a::IMP, 2 },{ "???", &a::NOP, &a::IMP, 2 },{ "TXA", &a::TXA, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "STY", &a::STY, &a::ABS, 4 },{ "STA", &a::STA, &a::ABS, 4 },{ "STX", &a::STX, &a::ABS, 4 },{ "???", &a::XXX, &a::IMP, 4 },
{ "BCC", &a::BCC, &a::REL, 2 },{ "STA", &a::STA, &a::IZY, 6 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 6 },{ "STY", &a::STY, &a::ZPX, 4 },{ "STA", &a::STA, &a::ZPX, 4 },{ "STX", &a::STX, &a::ZPY, 4 },{ "???", &a::XXX, &a::IMP, 4 },{ "TYA", &a::TYA, &a::IMP, 2 },{ "STA", &a::STA, &a::ABY, 5 },{ "TXS", &a::TXS, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 5 },{ "???", &a::NOP, &a::IMP, 5 },{ "STA", &a::STA, &a::ABX, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "???", &a::XXX, &a::IMP, 5 },
{ "LDY", &a::LDY, &a::IMM, 2 },{ "LDA", &a::LDA, &a::IZX, 6 },{ "LDX", &a::LDX, &a::IMM, 2 },{ "???", &a::XXX, &a::IMP, 6 },{ "LDY", &a::LDY, &a::ZP0, 3 },{ "LDA", &a::LDA, &a::ZP0, 3 },{ "LDX", &a::LDX, &a::ZP0, 3 },{ "???", &a::XXX, &a::IMP, 3 },{ "TAY", &a::TAY, &a::IMP, 2 },{ "LDA", &a::LDA, &a::IMM, 2 },{ "TAX", &a::TAX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "LDY", &a::LDY, &a::ABS, 4 },{ "LDA", &a::LDA, &a::ABS, 4 },{ "LDX", &a::LDX, &a::ABS, 4 },{ "???", &a::XXX, &a::IMP, 4 },
{ "BCS", &a::BCS, &a::REL, 2 },{ "LDA", &a::LDA, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 5 },{ "LDY", &a::LDY, &a::ZPX, 4 },{ "LDA", &a::LDA, &a::ZPX, 4 },{ "LDX", &a::LDX, &a::ZPY, 4 },{ "???", &a::XXX, &a::IMP, 4 },{ "CLV", &a::CLV, &a::IMP, 2 },{ "LDA", &a::LDA, &a::ABY, 4 },{ "TSX", &a::TSX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 4 },{ "LDY", &a::LDY, &a::ABX, 4 },{ "LDA", &a::LDA, &a::ABX, 4 },{ "LDX", &a::LDX, &a::ABY, 4 },{ "???", &a::XXX, &a::IMP, 4 },
{ "CPY", &a::CPY, &a::IMM, 2 },{ "CMP", &a::CMP, &a::IZX, 6 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "CPY", &a::CPY, &a::ZP0, 3 },{ "CMP", &a::CMP, &a::ZP0, 3 },{ "DEC", &a::DEC, &a::ZP0, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "INY", &a::INY, &a::IMP, 2 },{ "CMP", &a::CMP, &a::IMM, 2 },{ "DEX", &a::DEX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 2 },{ "CPY", &a::CPY, &a::ABS, 4 },{ "CMP", &a::CMP, &a::ABS, 4 },{ "DEC", &a::DEC, &a::ABS, 6 },{ "???", &a::XXX, &a::IMP, 6 },
{ "BNE", &a::BNE, &a::REL, 2 },{ "CMP", &a::CMP, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 4 },{ "CMP", &a::CMP, &a::ZPX, 4 },{ "DEC", &a::DEC, &a::ZPX, 6 },{ "???", &a::XXX, &a::IMP, 6 },{ "CLD", &a::CLD, &a::IMP, 2 },{ "CMP", &a::CMP, &a::ABY, 4 },{ "NOP", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 7 },{ "???", &a::NOP, &a::IMP, 4 },{ "CMP", &a::CMP, &a::ABX, 4 },{ "DEC", &a::DEC, &a::ABX, 7 },{ "???", &a::XXX, &a::IMP, 7 },
{ "CPX", &a::CPX, &a::IMM, 2 },{ "SBC", &a::SBC, &a::IZX, 6 },{ "???", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "CPX", &a::CPX, &a::ZP0, 3 },{ "SBC", &a::SBC, &a::ZP0, 3 },{ "INC", &a::INC, &a::ZP0, 5 },{ "???", &a::XXX, &a::IMP, 5 },{ "INX", &a::INX, &a::IMP, 2 },{ "SBC", &a::SBC, &a::IMM, 2 },{ "NOP", &a::NOP, &a::IMP, 2 },{ "???", &a::SBC, &a::IMP, 2 },{ "CPX", &a::CPX, &a::ABS, 4 },{ "SBC", &a::SBC, &a::ABS, 4 },{ "INC", &a::INC, &a::ABS, 6 },{ "???", &a::XXX, &a::IMP, 6 },
{ "BEQ", &a::BEQ, &a::REL, 2 },{ "SBC", &a::SBC, &a::IZY, 5 },{ "???", &a::XXX, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 8 },{ "???", &a::NOP, &a::IMP, 4 },{ "SBC", &a::SBC, &a::ZPX, 4 },{ "INC", &a::INC, &a::ZPX, 6 },{ "???", &a::XXX, &a::IMP, 6 },{ "SED", &a::SED, &a::IMP, 2 },{ "SBC", &a::SBC, &a::ABY, 4 },{ "NOP", &a::NOP, &a::IMP, 2 },{ "???", &a::XXX, &a::IMP, 7 },{ "???", &a::NOP, &a::IMP, 4 },{ "SBC", &a::SBC, &a::ABX, 4 },{ "INC", &a::INC, &a::ABX, 7 },{ "???", &a::XXX, &a::IMP, 7 },
}
6502proc::~6502proc()
{
}
uint8_t 6502proc::read(uint16_t addr)
{
return bus->(addr,false);
}
void 6502proc::write(uint16_t addr, uint8_t data)
{
return bus->write(addr,data);
}
void 6502proc::reset()
{
addr_abs=0x00;
uint16_t lo=read(addr_abs+0);
uint16_t hi=read(addr_abs+1);
pc=(hi<<8)|lo;
a=0x00;
x=0x00;
y=0x00;
sp=0xFD;
status=0x00|U;
addr_rel=0x00;
addr_abs=0x00;
fetched=0x00;
cycles=8;
}
void 6502proc:: irq()
{
if(GetFlag(I)==0)
{
write(0x0100+stkp,(pc>>8)&0x00FF)
stkp--;
write(0x0100+stkp,(pc)&0x00FF);
stkp--;
setFlag(B,0);
setFlag(U,1);
setFlag(I,1)l
write(0x0100+stkp,status);
stkp--;
addr_abs=0x00;
uint16_t lo=read(addr_abs+0);
uint16_t hi=read(addr_abs+1);
pc=(hi<<8)|lo;
cycles=7;
}
}
void 6502proc::nmi()
{
if(GetFlag(I)==0)
{
write(0x0100+stkp,(pc>>8)&0x00FF)
stkp--;
write(0x0100+stkp,(pc)&0x00FF);
stkp--;
setFlag(B,0);
setFlag(U,1);
setFlag(I,1)l
write(0x0100+stkp,status);
stkp--;
addr_abs=0xFFFA;
uint16_t lo=read(addr_abs+0);
uint16_t hi=read(addr_abs+1);
pc=(hi<<8)|lo;
cycles=8;
}
}
void 6502::clock()
{
if(cycles==0)
{
opcode=read(pc);
setFlag(U,true);
pc++;
cycles=lookout[opcode].cycles;
uint8_t additional_cycle1=(this->*lookup[opcode].addrmode)();
uint8_t additional_cycle2=(this->*lookup[opcode].operate)();
cycles+=(additional_cycle1+additional_cycle2);
setFlag(U,true);
}
clock_count++;
cycles--;
}
uint8_t olc6502::GetFlag(FLAGS6502 f)
{
return ((status & f) > 0) ? 1 : 0;
}
void olc6502::SetFlag(FLAGS6502 f, bool v)
{
if (v)
status |= f;
else
status &= ~f;
}
}
//addressing nodes
uint8_t olc6502::IMP()
{
fetched = a;
return 0;
}
uint8_t olc6502::IMM()
{
addr_abs = pc++;
return 0;
}
uint8_t olc6502::ZP0()
{
addr_abs = read(pc);
pc++;
addr_abs &= 0x00FF;
return 0;
}
uint8_t olc6502::ZPX()
{
addr_abs = (read(pc) + x);
pc++;
addr_abs &= 0x00FF;
return 0;
}
uint8_t olc6502::ZPY()
{
addr_abs = (read(pc) + y);
pc++;
addr_abs &= 0x00FF;
return 0;
}
uint8_t olc6502::REL()
{
addr_rel = read(pc);
pc++;
if (addr_rel & 0x80)
addr_rel |= 0xFF00;
return 0;
}
uint8_t olc6502::ABS()
{
uint16_t lo = read(pc);
pc++;
uint16_t hi = read(pc);
pc++;
addr_abs = (hi << 8) | lo;
return 0;
}
uint8_t olc6502::ABX()
{
uint16_t lo = read(pc);
pc++;
uint16_t hi = read(pc);
pc++;
addr_abs = (hi << 8) | lo;
addr_abs += x;
if ((addr_abs & 0xFF00) != (hi << 8))
return 1;
else
return 0;
}
uint8_t olc6502::ABY()
{
uint16_t lo = read(pc);
pc++;
uint16_t hi = read(pc);
pc++;
addr_abs = (hi << 8) | lo;
addr_abs += y;
if ((addr_abs & 0xFF00) != (hi << 8))
return 1;
else
return 0;
}
uint8_t olc6502::IND()
{
uint16_t ptr_lo = read(pc);
pc++;
uint16_t ptr_hi = read(pc);
pc++;
uint16_t ptr = (ptr_hi << 8) | ptr_lo;
if (ptr_lo == 0x00FF) // Simulate page boundary hardware bug
{
addr_abs = (read(ptr & 0xFF00) << 8) | read(ptr + 0);
}
else // Behave normally
{
addr_abs = (read(ptr + 1) << 8) | read(ptr + 0);
}
return 0;
}
uint8_t olc6502::IZX()
{
uint16_t t = read(pc);
pc++;
uint16_t lo = read((uint16_t)(t + (uint16_t)x) & 0x00FF);
uint16_t hi = read((uint16_t)(t + (uint16_t)x + 1) & 0x00FF);
addr_abs = (hi << 8) | lo;
return 0;
}
uint8_t olc6502::IZY()
{
uint16_t t = read(pc);
pc++;
uint16_t lo = read(t & 0x00FF);
uint16_t hi = read((t + 1) & 0x00FF);
addr_abs = (hi << 8) | lo;
addr_abs += y;
if ((addr_abs & 0xFF00) != (hi << 8))
return 1;
else
return 0;
}
uint8_t olc6502::fetch()
{
if (!(lookup[opcode].addrmode == &olc6502::IMP))
fetched = read(addr_abs);
return fetched;
}
//instruction sets
uint8_t olc6502::ADC()
{
// Grab the data that we are adding to the accumulator
fetch();
// Add is performed in 16-bit domain for emulation to capture any
// carry bit, which will exist in bit 8 of the 16-bit word
temp = (uint16_t)a + (uint16_t)fetched + (uint16_t)GetFlag(C);
// The carry flag out exists in the high byte bit 0
SetFlag(C, temp > 255);
// The Zero flag is set if the result is 0
SetFlag(Z, (temp & 0x00FF) == 0);
// The signed Overflow flag is set based on all that up there! :D
SetFlag(V, (~((uint16_t)a ^ (uint16_t)fetched) & ((uint16_t)a ^ (uint16_t)temp)) & 0x0080);
// The negative flag is set to the most significant bit of the result
SetFlag(N, temp & 0x80);
// Load the result into the accumulator (it's 8-bit dont forget!)
a = temp & 0x00FF;
// This instruction has the potential to require an additional clock cycle
return 1;
}
// Instruction: Subtraction with Borrow In
// Function: A = A - M - (1 - C)
// Flags Out: C, V, N, Z
//
// Explanation:
// Given the explanation for ADC above, we can reorganise our data
// to use the same computation for addition, for subtraction by multiplying
// the data by -1, i.e. make it negative
//
// A = A - M - (1 - C) -> A = A + -1 * (M - (1 - C)) -> A = A + (-M + 1 + C)
//
// To make a signed positive number negative, we can invert the bits and add 1
// (OK, I lied, a little bit of 1 and 2s complement :P)
//
// 5 = 00000101
// -5 = 11111010 + 00000001 = 11111011 (or 251 in our 0 to 255 range)
//
// The range is actually unimportant, because if I take the value 15, and add 251
// to it, given we wrap around at 256, the result is 10, so it has effectively
// subtracted 5, which was the original intention. (15 + 251) % 256 = 10
//
// Note that the equation above used (1-C), but this got converted to + 1 + C.
// This means we already have the +1, so all we need to do is invert the bits
// of M, the data(!) therfore we can simply add, exactly the same way we did
// before.
uint8_t olc6502::SBC()
{
fetch();
// Operating in 16-bit domain to capture carry out
// We can invert the bottom 8 bits with bitwise xor
uint16_t value = ((uint16_t)fetched) ^ 0x00FF;
// Notice this is exactly the same as addition from here!
temp = (uint16_t)a + value + (uint16_t)GetFlag(C);
SetFlag(C, temp & 0xFF00);
SetFlag(Z, ((temp & 0x00FF) == 0));
SetFlag(V, (temp ^ (uint16_t)a) & (temp ^ value) & 0x0080);
SetFlag(N, temp & 0x0080);
a = temp & 0x00FF;
return 1;
}
// OK! Complicated operations are done! the following are much simpler
// and conventional. The typical order of events is:
// 1) Fetch the data you are working with
// 2) Perform calculation
// 3) Store the result in desired place
// 4) Set Flags of the status register
// 5) Return if instruction has potential to require additional
// clock cycle
// Instruction: Bitwise Logic AND
// Function: A = A & M
// Flags Out: N, Z
uint8_t olc6502::AND()
{
fetch();
a = a & fetched;
SetFlag(Z, a == 0x00);
SetFlag(N, a & 0x80);
return 1;
}
// Instruction: Arithmetic Shift Left
// Function: A = C <- (A << 1) <- 0
// Flags Out: N, Z, C
uint8_t olc6502::ASL()
{
fetch();
temp = (uint16_t)fetched << 1;
SetFlag(C, (temp & 0xFF00) > 0);
SetFlag(Z, (temp & 0x00FF) == 0x00);
SetFlag(N, temp & 0x80);
if (lookup[opcode].addrmode == &olc6502::IMP)
a = temp & 0x00FF;
else
write(addr_abs, temp & 0x00FF);
return 0;
}
// Instruction: Branch if Carry Clear
// Function: if(C == 0) pc = address
uint8_t olc6502::BCC()
{
if (GetFlag(C) == 0)
{
cycles++;
addr_abs = pc + addr_rel;
if((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Branch if Carry Set
// Function: if(C == 1) pc = address
uint8_t olc6502::BCS()
{
if (GetFlag(C) == 1)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Branch if Equal
// Function: if(Z == 1) pc = address
uint8_t olc6502::BEQ()
{
if (GetFlag(Z) == 1)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
uint8_t olc6502::BIT()
{
fetch();
temp = a & fetched;
SetFlag(Z, (temp & 0x00FF) == 0x00);
SetFlag(N, fetched & (1 << 7));
SetFlag(V, fetched & (1 << 6));
return 0;
}
// Instruction: Branch if Negative
// Function: if(N == 1) pc = address
uint8_t olc6502::BMI()
{
if (GetFlag(N) == 1)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Branch if Not Equal
// Function: if(Z == 0) pc = address
uint8_t olc6502::BNE()
{
if (GetFlag(Z) == 0)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Branch if Positive
// Function: if(N == 0) pc = address
uint8_t olc6502::BPL()
{
if (GetFlag(N) == 0)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Break
// Function: Program Sourced Interrupt
uint8_t olc6502::BRK()
{
pc++;
SetFlag(I, 1);
write(0x0100 + stkp, (pc >> 8) & 0x00FF);
stkp--;
write(0x0100 + stkp, pc & 0x00FF);
stkp--;
SetFlag(B, 1);
write(0x0100 + stkp, status);
stkp--;
SetFlag(B, 0);
pc = (uint16_t)read(0xFFFE) | ((uint16_t)read(0xFFFF) << 8);
return 0;
}
// Instruction: Branch if Overflow Clear
// Function: if(V == 0) pc = address
uint8_t olc6502::BVC()
{
if (GetFlag(V) == 0)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Branch if Overflow Set
// Function: if(V == 1) pc = address
uint8_t olc6502::BVS()
{
if (GetFlag(V) == 1)
{
cycles++;
addr_abs = pc + addr_rel;
if ((addr_abs & 0xFF00) != (pc & 0xFF00))
cycles++;
pc = addr_abs;
}
return 0;
}
// Instruction: Clear Carry Flag
// Function: C = 0
uint8_t olc6502::CLC()
{
SetFlag(C, false);
return 0;
}
// Instruction: Clear Decimal Flag
// Function: D = 0
uint8_t olc6502::CLD()
{
SetFlag(D, false);
return 0;
}
// Instruction: Disable Interrupts / Clear Interrupt Flag
// Function: I = 0
uint8_t olc6502::CLI()
{
SetFlag(I, false);
return 0;
}
// Instruction: Clear Overflow Flag
// Function: V = 0
uint8_t olc6502::CLV()
{
SetFlag(V, false);
return 0;
}
// Instruction: Compare Accumulator
// Function: C <- A >= M Z <- (A - M) == 0
// Flags Out: N, C, Z
uint8_t olc6502::CMP()
{
fetch();
temp = (uint16_t)a - (uint16_t)fetched;
SetFlag(C, a >= fetched);
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
return 1;
}
// Instruction: Compare X Register
// Function: C <- X >= M Z <- (X - M) == 0
// Flags Out: N, C, Z
uint8_t olc6502::CPX()
{
fetch();
temp = (uint16_t)x - (uint16_t)fetched;
SetFlag(C, x >= fetched);
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
return 0;
}
// Instruction: Compare Y Register
// Function: C <- Y >= M Z <- (Y - M) == 0
// Flags Out: N, C, Z
uint8_t olc6502::CPY()
{
fetch();
temp = (uint16_t)y - (uint16_t)fetched;
SetFlag(C, y >= fetched);
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
return 0;
}
// Instruction: Decrement Value at Memory Location
// Function: M = M - 1
// Flags Out: N, Z
uint8_t olc6502::DEC()
{
fetch();
temp = fetched - 1;
write(addr_abs, temp & 0x00FF);
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
return 0;
}
// Instruction: Decrement X Register
// Function: X = X - 1
// Flags Out: N, Z
uint8_t olc6502::DEX()
{
x--;
SetFlag(Z, x == 0x00);
SetFlag(N, x & 0x80);
return 0;
}
// Instruction: Decrement Y Register
// Function: Y = Y - 1
// Flags Out: N, Z
uint8_t olc6502::DEY()
{
y--;
SetFlag(Z, y == 0x00);
SetFlag(N, y & 0x80);
return 0;
}
// Instruction: Bitwise Logic XOR
// Function: A = A xor M
// Flags Out: N, Z
uint8_t olc6502::EOR()
{
fetch();
a = a ^ fetched;
SetFlag(Z, a == 0x00);
SetFlag(N, a & 0x80);
return 1;
}
// Instruction: Increment Value at Memory Location
// Function: M = M + 1
// Flags Out: N, Z
uint8_t olc6502::INC()
{
fetch();
temp = fetched + 1;
write(addr_abs, temp & 0x00FF);
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
return 0;
}
// Instruction: Increment X Register
// Function: X = X + 1
// Flags Out: N, Z
uint8_t olc6502::INX()
{
x++;
SetFlag(Z, x == 0x00);
SetFlag(N, x & 0x80);
return 0;
}
// Instruction: Increment Y Register
// Function: Y = Y + 1
// Flags Out: N, Z
uint8_t olc6502::INY()
{
y++;
SetFlag(Z, y == 0x00);
SetFlag(N, y & 0x80);
return 0;
}
// Instruction: Jump To Location
// Function: pc = address
uint8_t olc6502::JMP()
{
pc = addr_abs;
return 0;
}
// Instruction: Jump To Sub-Routine
// Function: Push current pc to stack, pc = address
uint8_t olc6502::JSR()
{
pc--;
write(0x0100 + stkp, (pc >> 8) & 0x00FF);
stkp--;
write(0x0100 + stkp, pc & 0x00FF);
stkp--;
pc = addr_abs;
return 0;
}
// Instruction: Load The Accumulator
// Function: A = M
// Flags Out: N, Z
uint8_t olc6502::LDA()
{
fetch();
a = fetched;
SetFlag(Z, a == 0x00);
SetFlag(N, a & 0x80);
return 1;
}
// Instruction: Load The X Register
// Function: X = M
// Flags Out: N, Z
uint8_t olc6502::LDX()
{
fetch();
x = fetched;
SetFlag(Z, x == 0x00);
SetFlag(N, x & 0x80);
return 1;
}
// Instruction: Load The Y Register
// Function: Y = M
// Flags Out: N, Z
uint8_t olc6502::LDY()
{
fetch();
y = fetched;
SetFlag(Z, y == 0x00);
SetFlag(N, y & 0x80);
return 1;
}
uint8_t olc6502::LSR()
{
fetch();
SetFlag(C, fetched & 0x0001);
temp = fetched >> 1;
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
if (lookup[opcode].addrmode == &olc6502::IMP)
a = temp & 0x00FF;
else
write(addr_abs, temp & 0x00FF);
return 0;
}
uint8_t olc6502::NOP()
{
// Sadly not all NOPs are equal, Ive added a few here
// based on https://wiki.nesdev.com/w/index.php/CPU_unofficial_opcodes
// and will add more based on game compatibility, and ultimately
// I'd like to cover all illegal opcodes too
switch (opcode) {
case 0x1C:
case 0x3C:
case 0x5C:
case 0x7C:
case 0xDC:
case 0xFC:
return 1;
break;
}
return 0;
}
// Instruction: Bitwise Logic OR
// Function: A = A | M
// Flags Out: N, Z
uint8_t olc6502::ORA()
{
fetch();
a = a | fetched;
SetFlag(Z, a == 0x00);
SetFlag(N, a & 0x80);
return 1;
}
// Instruction: Push Accumulator to Stack
// Function: A -> stack
uint8_t olc6502::PHA()
{
write(0x0100 + stkp, a);
stkp--;
return 0;
}
// Instruction: Push Status Register to Stack
// Function: status -> stack
// Note: Break flag is set to 1 before push
uint8_t olc6502::PHP()
{
write(0x0100 + stkp, status | B | U);
SetFlag(B, 0);
SetFlag(U, 0);
stkp--;
return 0;
}
// Instruction: Pop Accumulator off Stack
// Function: A <- stack
// Flags Out: N, Z
uint8_t olc6502::PLA()
{
stkp++;
a = read(0x0100 + stkp);
SetFlag(Z, a == 0x00);
SetFlag(N, a & 0x80);
return 0;
}
// Instruction: Pop Status Register off Stack
// Function: Status <- stack
uint8_t olc6502::PLP()
{
stkp++;
status = read(0x0100 + stkp);
SetFlag(U, 1);
return 0;
}
uint8_t olc6502::ROL()
{
fetch();
temp = (uint16_t)(fetched << 1) | GetFlag(C);
SetFlag(C, temp & 0xFF00);
SetFlag(Z, (temp & 0x00FF) == 0x0000);
SetFlag(N, temp & 0x0080);
if (lookup[opcode].addrmode == &olc6502::IMP)
a = temp & 0x00FF;
else
write(addr_abs, temp & 0x00FF);
return 0;
}
uint8_t olc6502::ROR()
{
fetch();
temp = (uint16_t)(GetFlag(C) << 7) | (fetched >> 1);
SetFlag(C, fetched & 0x01);
SetFlag(Z, (temp & 0x00FF) == 0x00);
SetFlag(N, temp & 0x0080);
if (lookup[opcode].addrmode == &olc6502::IMP)
a = temp & 0x00FF;
else
write(addr_abs, temp & 0x00FF);
return 0;
}
uint8_t olc6502::RTI()
{
stkp++;
status = read(0x0100 + stkp);
status &= ~B;
status &= ~U;
stkp++;
pc = (uint16_t)read(0x0100 + stkp);
stkp++;
pc |= (uint16_t)read(0x0100 + stkp) << 8;
return 0;
}
uint8_t olc6502::RTS()
{
stkp++;
pc = (uint16_t)read(0x0100 + stkp);
stkp++;
pc |= (uint16_t)read(0x0100 + stkp) << 8;
pc++;
return 0;
}
// Instruction: Set Carry Flag
// Function: C = 1
uint8_t olc6502::SEC()
{
SetFlag(C, true);
return 0;
}
// Instruction: Set Decimal Flag
// Function: D = 1
uint8_t olc6502::SED()
{
SetFlag(D, true);
return 0;
}
// Instruction: Set Interrupt Flag / Enable Interrupts
// Function: I = 1