-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU_Emulator.cpp
More file actions
1581 lines (1291 loc) · 38.7 KB
/
Copy pathCPU_Emulator.cpp
File metadata and controls
1581 lines (1291 loc) · 38.7 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 <cstdio>
#include <cstdint>
#include <cstddef>
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
using namespace std;
const uint32_t MEM_SIZE = 65536;
uint8_t memory[MEM_SIZE];
// Status Register (P)
union StatusRegister {
uint8_t byte; // Access the entire register as a byte
struct {
uint8_t C : 1; // Bit 0: Carry Flag
uint8_t Z : 1; // Bit 1: Zero Flag
uint8_t I : 1; // Bit 2: Interrupt Disable
uint8_t D : 1; // Bit 3: Decimal Mode
uint8_t B : 1; // Bit 4: Break Command
uint8_t unused : 1; // Bit 5: Always 1 on the stack
uint8_t V : 1; // Bit 6: Overflow Flag
uint8_t N : 1; // Bit 7: Negative Flag
} bits;
};
// CPU State (Registers)
struct CPU {
uint16_t PC; // Program Counter
uint8_t SP; // Stack Pointer
uint8_t A, X, Y; // Accumulator, Index Registers
StatusRegister P; // Status Register
};
CPU cpu;
//Store the bytes into memory from binary file
void load_program() {
const char* fileName = "6502_functional_test.bin";
ifstream file(fileName, ios::binary); //open the file in binary mode
if (!file) {
cout << "file couldn't be opened. " << endl;
return;
}
file.seekg(0, ios::end); // Move to the end of the file
streampos fileSize = file.tellg(); //get the file size
cout << "File size: " << fileSize << endl;
file.seekg(0, ios::beg); // Move back to the beginning of the file
//read data to memory
file.read(reinterpret_cast<char*>(memory), MEM_SIZE);
cout << "Total " << file.gcount() << " valid data was read from the file.\n" << endl;
file.close(); //closing the file
}
//Display Status Register for velidation checking
void check_result()
{
//printing register
cout << "PC: " << hex << cpu.PC << ", A: " << hex << (int)cpu.A << ", X: " << hex << (int)cpu.X;
cout << ", Y: " << hex << (int)cpu.Y << ", SP: " << hex << (int)cpu.SP << endl;
//printing processor status
printf("P: N:%d V:%d B:%d D:%d I:%d Z:%d C:%d (Byte:$%.2X)\n",
cpu.P.bits.N, cpu.P.bits.V, cpu.P.bits.B, cpu.P.bits.D,
cpu.P.bits.I, cpu.P.bits.Z, cpu.P.bits.C, cpu.P.byte);
}
//Load register and update Z, N flags
void LoadRegister(uint8_t& updateReg, uint16_t address) {
//Access & Store the value
updateReg = memory[address];
// Update N and Z flags
cpu.P.bits.N = (updateReg & 0x80) != 0;
cpu.P.bits.Z = (updateReg == 0);
}
//Load Value from immediate address
void LoadImmediate(uint8_t& updateReg) {
updateReg = memory[cpu.PC++];
// Update N and Z flags
cpu.P.bits.N = (updateReg & 0x80) != 0;
cpu.P.bits.Z = (updateReg == 0);
}
//Return effictive memory address using immediate addressing mode
uint16_t ZP_Address(uint8_t index) {
uint8_t baseAddress = memory[cpu.PC++];
uint16_t effAddress = (baseAddress + index) & 0xFF;
return effAddress;
}
//Return the effective address using (indexed) addressing mode
uint16_t absAddress(uint8_t index) {
//Read the address byte
uint8_t lowByte = memory[cpu.PC++];
uint8_t highByte = memory[cpu.PC++];
//Shift byte to calculate effictive address
uint16_t baseAddress = (highByte << 8) | lowByte;
uint16_t address = (baseAddress + index) & 0xFFFF;
return address;
}
//Return the effective address using (indirect, X) addressing mode
uint16_t X_indirectAddress() {
//Read operand
uint8_t operand = memory[cpu.PC++];
//Add X register to get to zero-page address
uint8_t lowByteAddress = (operand + cpu.X) & 0xFF;
uint8_t highByteAddress = (operand + cpu.X + 1) & 0xFF;
//Read the two address byte from zero-page
uint8_t lowByte = memory[lowByteAddress];
uint8_t highByte = memory[highByteAddress];
//Combine the two bytes to get effective 16-bit address
uint16_t effAddress = (highByte << 8) | lowByte;
return effAddress;
}
//Return the effective memory address using (Y, indirect) addressing mode
uint16_t Y_indirectAddress() {
//Read zero-page pointer location for low byte
uint8_t lowByteAddress = memory[cpu.PC++];
//Next byte in zero-page is pointer to high byte
uint8_t highByteAddress = (lowByteAddress + 1) & 0xFF;
//Read the two address byte from zero-page
uint8_t lowByte = memory[lowByteAddress];
uint8_t highByte = memory[highByteAddress];
//Combine the two bytes to get effective 16-bit address
uint16_t effAddress = ((highByte << 8) | lowByte) + cpu.Y;
return effAddress;
}
//Transferring first variable to second variable
void Transfer(uint8_t R, uint8_t& Q) {
Q = R;
// Update N and Z flags
cpu.P.bits.N = (Q & 0x80) != 0;
cpu.P.bits.Z = (Q == 0);
}
//Implement Add with carry
void ADC(uint8_t operand) {
//If Decimal flag is clear, perform normal binary addition
if (cpu.P.bits.D == 0) {
// Add accumulator, operand, and carry
int16_t temp_result = cpu.A + operand + cpu.P.bits.C;
// Carry Flag is set (result > 255)
cpu.P.bits.C = (temp_result > 0xFF);
// Overflow flag is set if signed overflow occurs.
// This happens when two numbers with the same sign produce
// a result with a different sign.
uint8_t result8 = temp_result & 0xFF;
cpu.P.bits.V = ((~(cpu.A ^ operand) & (cpu.A ^ result8)) & 0x80) != 0;
cpu.A = result8; // Store only the low 8 bits
}
// If Decimal flag is set, perform BCD addition
if (cpu.P.bits.D == 1) {
//Add the low nibbles
uint8_t lowNibble = (cpu.A & 0x0F) + (operand & 0x0F) + cpu.P.bits.C;
//Adjust for valid BCD if result exceeds 9
if (lowNibble > 9) {
cpu.P.bits.C = 1;
lowNibble -= 10;
}
else {
cpu.P.bits.C = 0;
}
//Add the high nibbles
uint8_t highNibble = (cpu.A >> 4) + (operand >> 4) + cpu.P.bits.C;
//Adjust for valid BCD if result exceeds 9
if (highNibble > 9) {
cpu.P.bits.C = 1;
highNibble -= 10;
}
else {
cpu.P.bits.C = 0;
}
//Combine the two results into single byte and Store
cpu.A = (highNibble << 4) | lowNibble;
}
// Update N and Z flags
cpu.P.bits.N = (cpu.A & 0x80) != 0;
cpu.P.bits.Z = (cpu.A == 0);
}
//Implement Subtract with carry
void SBC(uint8_t operand) {
//If decimal flag is clear perform binary subtraction
if (cpu.P.bits.D == 0){
//result = A + (~operand) + C
uint16_t tempResult = cpu.A + (~operand) + cpu.P.bits.C;
//set carry flag if no borrow happened
cpu.P.bits.C = (tempResult < 0x100);
//signed overflow flag
// This happens when the sign of the result differs from A
// while A and operand originally had different signs.
uint8_t result8 = tempResult & 0xFF;
cpu.P.bits.V = (((cpu.A ^ operand) & (cpu.A ^ result8)) & 0x80) != 0;
cpu.A = result8; //store only the low 8-bits
}
// If Decimal flag is set, perform BCD subtraction
if (cpu.P.bits.D == 1) {
//Save the carry flag for low nibble subtraction
uint8_t carryLow = cpu.P.bits.C;
uint8_t carryHigh;
//Subtract the lower nibble
int8_t lowNibble = (cpu.A & 0x0F) - (operand & 0x0F) - (1 - carryLow);
//If result is negative, borrow from high nibble
if (lowNibble < 0) {
carryHigh = 0;
lowNibble += 10;
}
else {
carryHigh = 1;
}
//Subtract the high nibble
int8_t highNibble = (cpu.A >> 4) - (operand >> 4) - (1 - carryHigh);
//If result is negative, borrow occured
if (highNibble < 0) {
cpu.P.bits.C = 0;
highNibble += 10;
}
else {
cpu.P.bits.C = 1;
}
//COmbine the two results and Store
cpu.A = (highNibble << 4) | lowNibble;
}
//set N and Z bit flag
cpu.P.bits.N = (cpu.A & 0x80) != 0;
cpu.P.bits.Z = (cpu.A == 0);
}
//And the accumulator
void AND(uint8_t M) {
cpu.A = cpu.A & M;
//set N and Z bit flag
cpu.P.bits.N = (cpu.A & 0x80) != 0;
cpu.P.bits.Z = (cpu.A == 0);
}
//XOR the accumulator
void EOR(uint8_t M) {
cpu.A = cpu.A ^ M;
//set N and Z bit flag
cpu.P.bits.N = (cpu.A & 0x80) != 0;
cpu.P.bits.Z = (cpu.A == 0);
}
//OR the accumulator
void ORA(uint8_t M) {
cpu.A = cpu.A | M;
//set N and Z bit flag
cpu.P.bits.N = (cpu.A & 0x80) != 0;
cpu.P.bits.Z = (cpu.A == 0);
}
//Bit Testing without changing the accumulator
void BIT(uint8_t M) {
//set flags
cpu.P.bits.Z = (cpu.A & M) == 0;
cpu.P.bits.N = (M & 0x80) != 0;
cpu.P.bits.V = (M & 0x40) != 0;
}
//Increment the operand
void Increment(uint8_t& operand) {
operand = (operand + 1) & 0xFF;
//set N and Z bit flag
cpu.P.bits.N = (operand & 0x80) != 0;
cpu.P.bits.Z = (operand == 0);
}
//Decrement the operand
void Decrement(uint8_t& operand) {
operand = (operand - 1) & 0xFF;
//set N and Z bit flag
cpu.P.bits.N = (operand & 0x80) != 0;
cpu.P.bits.Z = (operand == 0);
}
//Shift all bits to left by one bit
void ASL(uint8_t& M) {
//MSB is stored as carry
cpu.P.bits.C = (M & 0x80) != 0;
M = M << 1; //Shift left
//set N and Z bit flag
cpu.P.bits.N = (M & 0x80) != 0;
cpu.P.bits.Z = (M == 0);
}
//Shift all bits to right by one bit
void LSR(uint8_t& M) {
//LSB is stored as carry
cpu.P.bits.C = (M & 0x01) != 0;
M = M >> 1; //Shift right
//set N and Z bit flag
cpu.P.bits.N = (M & 0x80) != 0;
cpu.P.bits.Z = (M == 0);
}
//Rotate left by one bit
void ROL(uint8_t& M) {
uint8_t oldCarry = cpu.P.bits.C; //store the original carry
cpu.P.bits.C = (M & 0x80) != 0; //MSB becomes new carry
M = (M << 1) | oldCarry; //LSB is replaced with previous carry
//set N and Z bit flag
cpu.P.bits.N = (M & 0x80) != 0;
cpu.P.bits.Z = (M == 0);
}
//Rotate right by one bit
void ROR(uint8_t& M) {
uint8_t oldCarry = cpu.P.bits.C << 7; //store the original carry
cpu.P.bits.C = (M & 0x01) != 0; //LSB becomes new carry
M = (M >> 1) | oldCarry; //MSB is replaced with previous carry
//set N and Z bit flag
cpu.P.bits.N = (M & 0x80) != 0;
cpu.P.bits.Z = (M == 0);
}
//Push register status to Stack memory
void pushStack(uint8_t value) {
memory[0x100 + cpu.SP] = value; // store value at stack
cpu.SP = (cpu.SP - 1) & 0xFF; // decrement SP
}
//Pull register status from Stack memory
uint8_t pullStack() {
cpu.SP = (cpu.SP + 1) & 0xFF; //increment SP
return memory[0x100 + cpu.SP]; //return the value from stack
}
//CMP- compare register value with given value
void Compare(uint8_t RegValue, uint8_t M) {
uint8_t tempResult = (RegValue - M) & 0xFF;
//set the flags
cpu.P.bits.C = (RegValue >= M);
cpu.P.bits.N = (tempResult & 0x80) != 0;
cpu.P.bits.Z = (tempResult == 0);
}
//Main program function
int main() {
//Load memory
load_program();
//Start from PC at 400 (for this specific bin instruction set)
cpu.PC = 0x0400;
cpu.SP = 0xFF; //No stack memory is being used at this point
cpu.P.bits.unused = 1;
while (1) {
// Fetch
uint8_t opcode = memory[cpu.PC++];
// Decode and Execute
switch (opcode) {
/*Load and Store Accumulator, Index*/
// LDA Immediate
case 0xA9: {
LoadImmediate(cpu.A);
break;
}
//LDA (Zero page)
case 0xA5: {
uint16_t effAddress = ZP_Address(0); //0 - for not having index
LoadRegister(cpu.A, effAddress);
break;
}
//LDA - zero page (X)
case 0xB5: {
uint16_t effAddress = ZP_Address(cpu.X);
LoadRegister(cpu.A, effAddress);
break;
}
//LDA Absolute
case 0xAD: {
uint16_t effAddress = absAddress(0); //0- for not having index
LoadRegister(cpu.A, effAddress);
break;
}
//LDA Absolute, X
case 0xBD: {
uint16_t effAddress = absAddress(cpu.X);
LoadRegister(cpu.A, effAddress);
break;
}
//LDA Absolute, Y
case 0xB9: {
uint16_t effAddress = absAddress(cpu.Y);
LoadRegister(cpu.A, effAddress);
break;
}
//LDA - indirec X
case 0xA1: {
uint16_t effAddress = X_indirectAddress();
LoadRegister(cpu.A, effAddress);
break;
}
//LDA - indirect, Y
case 0xB1: {
uint16_t effAddress = Y_indirectAddress();
LoadRegister(cpu.A, effAddress);
break;
}
/*Load Index X*/
//LDX immediate
case 0xA2: {
LoadImmediate(cpu.X);
break;
}
//LDX - zero page
case 0xA6: {
uint16_t address = ZP_Address(0); //0- for not having index
LoadRegister(cpu.X, address);
break;
}
//LDX - zero page, Y
case 0xB6: {
uint16_t address = ZP_Address(cpu.Y);
LoadRegister(cpu.X, address);
break;
}
//LDX - absolute
case 0xAE: {
uint16_t effAddress = absAddress(0);
LoadRegister(cpu.X, effAddress);
break;
}
//LDX - absolute, Y
case 0xBE: {
uint16_t effAddress = absAddress(cpu.Y);
LoadRegister(cpu.X, effAddress);
break;
}
//LDY immediate
case 0xA0: {
LoadImmediate(cpu.Y);
break;
}
//LDY - zero page
case 0xA4: {
uint16_t address = ZP_Address(0); //0- for not having index
LoadRegister(cpu.Y, address);
break;
}
//LDY - zero page, X
case 0xB4: {
uint16_t address = ZP_Address(cpu.X);
LoadRegister(cpu.Y, address);
break;
}
//LDY - absolute
case 0xAC: {
uint16_t effAddress = absAddress(0);
LoadRegister(cpu.Y, effAddress);
break;
}
//LDY - absolute, X
case 0xBC: {
uint16_t effAddress = absAddress(cpu.X);
LoadRegister(cpu.Y, effAddress);
break;
}
//STA Zero page
case 0x85: {
uint16_t address = memory[cpu.PC++];
memory[address] = cpu.A;
break;
}
//STA - zero page, X
case 0x95: {
uint16_t effAddress = ZP_Address(cpu.X);
memory[effAddress] = cpu.A;
break;
}
// STA Absolute
case 0x8D: {
uint16_t effAddress = absAddress(0);
memory[effAddress] = cpu.A;
break;
}
// STA Absolute, X
case 0x9D: {
uint16_t effAddress = absAddress(cpu.X);
memory[effAddress] = cpu.A;
break;
}
// STA Absolute, Y
case 0x99: {
uint16_t effAddress = absAddress(cpu.Y);
memory[effAddress] = cpu.A;
break;
}
//STA - indirect X
case 0x81: {
uint16_t effAddress = X_indirectAddress(); //pass the index
memory[effAddress] = cpu.A;
break;
}
//STA - indirect Y
case 0x91: {
uint16_t effAddress = Y_indirectAddress(); //pass the index
memory[effAddress] = cpu.A;
break;
}
//STX - zero page
case 0x86: {
uint16_t address = memory[cpu.PC++];
memory[address] = cpu.X;
break;
}
//STX - zero page, Y
case 0x96: {
uint16_t effAddress = ZP_Address(cpu.Y);
memory[effAddress] = cpu.X;
break;
}
//STX - absolute
case 0x8E: {
uint16_t address = absAddress(0); //0 - for no index
memory[address] = cpu.X;
break;
}
//STY - zero page
case 0x84: {
uint16_t address = memory[cpu.PC++];
memory[address] = cpu.Y;
break;
}
//STY - zero page, X
case 0x94: {
uint16_t effAddress = ZP_Address(cpu.X);
memory[effAddress] = cpu.Y;
break;
}
//STY - absolute
case 0x8C: {
uint16_t address = absAddress(0); //0 - for no index
memory[address] = cpu.Y;
break;
}
/*Register Transfer*/
//TAX - Transfer Accumulator to X
case 0xAA: {
Transfer(cpu.A, cpu.X);
break;
}
//TAY - Transfer Accumulator to Y
case 0xA8: {
Transfer(cpu.A, cpu.Y);
break;
}
//TXA - Transfer X Accumulator
case 0x8A: {
Transfer(cpu.X, cpu.A);
break;
}
//TYA - Transfer Y to Accumulator
case 0x98: {
Transfer(cpu.Y, cpu.A);
break;
}
/*Stack Operations*/
//TXS - Transfer X to Stack Pointer
case 0x9A: {
cpu.SP = cpu.X; //this opcode does not change flags
break;
}
//TSX - Transfer Stack Pointer to X
case 0xBA: {
Transfer(cpu.SP, cpu.X);
break;
}
//PHA - Push Accumulator on Stack
case 0x48: {
pushStack(cpu.A);
break;
}
//PLA - pull accumulator from stack
case 0x68: {
cpu.A = pullStack();
// Update N and Z flags
cpu.P.bits.N = (cpu.A & 0x80) != 0;
cpu.P.bits.Z = (cpu.A == 0);
break;
}
//PHP - push processor status on stack
case 0x08: {
uint8_t status = cpu.P.byte | 0x30;
pushStack(status); //bit 4,5 always pushed as 1
break;
}
//PLP - pull processor status from stack
case 0x28: {
uint8_t stack = pullStack();
cpu.P.byte = (stack & 0xCF) | 0x20; //clear bit 4,5 and then set bit-5
break;
}
/*Arithmetics*/
// ADC Immediate
case 0x69: {
uint8_t operand = memory[cpu.PC++];
ADC(operand); //add operand to A with carry
break;
}
//ADC - Zero page
case 0x65: {
uint16_t address = memory[cpu.PC++];
uint8_t operand = memory[address];
ADC(operand); //Add operand to A with carry
break;
}
//ADC - Zero page, X
case 0x75: {
uint16_t effAddress = ZP_Address(cpu.X);
ADC(memory[effAddress]);
break;
}
//ADC- Absolute
case 0x6D: {
uint16_t effAddress = absAddress(0);
ADC(memory[effAddress]); //operand is located at effAddress
break;
}
//ADC - Absolute, X
case 0x7D: {
uint16_t effAddress = absAddress(cpu.X);
ADC(memory[effAddress]);
break;
}
//ADC - Absolute, Y
case 0x79:{
uint16_t effAddress = absAddress(cpu.Y);
ADC(memory[effAddress]);
break;
}
//ADC - Indirect, X
case 0x61: {
uint16_t effAddress = X_indirectAddress();
ADC(memory[effAddress]);
break;
}
//ADC - Indirect, Y
case 0x71: {
uint16_t effAddress = Y_indirectAddress();
ADC(memory[effAddress]);
break;
}
//SBC - Immediate
case 0xE9: {
uint8_t operand = memory[cpu.PC++];
SBC(operand);
break;
}
//SBC Zero Page
case 0xE5: {
uint8_t address = memory[cpu.PC++];
SBC(memory[address]); //operand is at memory address
break;
}
//SBC - Zero page, X
case 0xF5: {
uint16_t effAddress = ZP_Address(cpu.X);
SBC(memory[effAddress]); //operand is at memory address
break;
}
//SBC- Absolute
case 0xED: {
uint16_t effAddress = absAddress(0);
SBC(memory[effAddress]); //operand is located at effAddress
break;
}
//SBC - Absolute, X
case 0xFD: {
uint16_t effAddress = absAddress(cpu.X);
SBC(memory[effAddress]);
break;
}
//SBC - Absolute, Y
case 0xF9: {
uint16_t effAddress = absAddress(cpu.Y);
SBC(memory[effAddress]);
break;
}
//SBC - Indirect, X
case 0xE1: {
uint16_t effAddress = X_indirectAddress();
SBC(memory[effAddress]);
break;
}
//SBC - Indirect, Y
case 0xF1: {
uint16_t effAddress = Y_indirectAddress();
SBC(memory[effAddress]);
break;
}
/*Logical operations*/
//AND - immediate
case 0x29: {
uint8_t operand = memory[cpu.PC++];
AND(operand);
break;
}
//AND - Zero page
case 0x25: {
uint8_t address = memory[cpu.PC++];
uint8_t operand = memory[address];
AND(operand);
break;
}
//AND - Zero page, X
case 0x35: {
uint16_t address = ZP_Address(cpu.X);
uint8_t operand = memory[address];
AND(operand);
break;
}
//AND - Absolute
case 0x2D: {
uint16_t address = absAddress(0);
uint8_t operand = memory[address];
AND(operand);
break;
}
//AND - Absolute, X
case 0x3D: {
uint16_t address = absAddress(cpu.X);
uint8_t operand = memory[address];
AND(operand);
break;
}
//AND - Absolute, Y
case 0x39: {
uint16_t address = absAddress(cpu.Y);
uint8_t operand = memory[address];
AND(operand);
break;
}
//AND - Indirect, X
case 0x21: {
uint16_t address = X_indirectAddress();
uint8_t operand = memory[address];
AND(operand);
break;
}
//AND - Indirect, Y
case 0x31: {
uint16_t address = Y_indirectAddress();
uint8_t operand = memory[address];
AND(operand);
break;
}
//EOR - immediate
case 0x49: {
uint8_t operand = memory[cpu.PC++];
EOR(operand);
break;
}
//EOR - Zero page
case 0x45: {
uint8_t address = memory[cpu.PC++];
uint8_t operand = memory[address];
EOR(operand);
break;
}
//EOR - Zero page, X
case 0x55: {
uint16_t address = ZP_Address(cpu.X);
uint8_t operand = memory[address];
EOR(operand);
break;
}
//EOR - Absolute
case 0x4D: {
uint16_t address = absAddress(0);
uint8_t operand = memory[address];
EOR(operand);
break;
}
//EOr - Absolute, X
case 0x5D: {
uint16_t address = absAddress(cpu.X);
uint8_t operand = memory[address];
EOR(operand);
break;
}
//EOR - Absolute, Y
case 0x59: {
uint16_t address = absAddress(cpu.Y);
uint8_t operand = memory[address];
EOR(operand);
break;
}
//EOR - Indirect, X
case 0x41: {
uint16_t address = X_indirectAddress();
uint8_t operand = memory[address];
EOR(operand);
break;
}
//EOR - Indirect, Y
case 0x51: {
uint16_t address = Y_indirectAddress();
uint8_t operand = memory[address];
EOR(operand);
break;
}
//ORA - immediate
case 0x09: {
uint8_t operand = memory[cpu.PC++];
ORA(operand);
break;
}
//ORA - Zero page
case 0x05: {
uint8_t address = memory[cpu.PC++];
uint8_t operand = memory[address];
ORA(operand);
break;
}
//ORA - Zero page, X
case 0x15: {
uint16_t address = ZP_Address(cpu.X);
uint8_t operand = memory[address];
ORA(operand);
break;
}
//ORA - Absolute
case 0x0D: {
uint16_t address = absAddress(0);
uint8_t operand = memory[address];
ORA(operand);
break;
}
//ORA - Absolute, X
case 0x1D: {
uint16_t address = absAddress(cpu.X);
uint8_t operand = memory[address];
ORA(operand);