-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6502Emulator.cpp
More file actions
1209 lines (1149 loc) · 36.5 KB
/
6502Emulator.cpp
File metadata and controls
1209 lines (1149 loc) · 36.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
//==================================================================
//6502 Processor Emulator (Ricoh 2A03 and 2A07...) Written By Mahdi Zakariyazadeh(2025)
//Pluse Getting Help From The Internet!...
//Email : mahdi07860@gmail.com
//6502Emulator(Simple With High Accuracy) V1.0
//6502 8bit Data(Pin D0-D7) & 16bit Address(Pin A0-A15)
//This is a simple 6502 CPU simulator used as the base for classic systems like the (Atari2600,Atari8-bit,AppleII,Nintendo8Bit System,Commodore64,AtariLynx,BBC Micro and ...)
//==================================================================
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "Tools.h"
#include "6502C.h"
#include "Instruction6502C.h"
//==================================================================
Cpu6502 cpu;
//==================================================================
#define CYCLE //ON,OFF
#define BUG6502 //(only used by Indirect JMP)
#define STOPOPCODE "BRK" //Stop Emulator ,...
//==================================================================
//PageCrossing (C=ABX=ABY=IZY=0)&(C=REL=1) Branch instructions
//==================================================================
#define PageCrossing(A,B,C) if((A&0xFF00)==(B&0xFF00)){cpu->Opcode_Cycle+=(C);}else{cpu->Opcode_Cycle+=(C+1);}
//==================================================================
//
// PART1:CPU Memory & STACK Read/Write
//
//==================================================================
//==================================================================
// Read_Operation1Byte
//==================================================================
#define Read_Operand1Byte(cpu) (Cpu_Map_Bus_Read(cpu ,cpu->PC+1))
//==================================================================
// Read_Operation2Byte
//==================================================================
#define Read_Operand2Byte(cpu) (Cpu_Map_Bus_Read(cpu ,cpu->PC+1) | (Cpu_Map_Bus_Read(cpu ,cpu->PC+2) << 8))
//==================================================================
// STACK PUSH Write
//==================================================================
#define Push(cpu , data) (Cpu_Map_Bus_Write(cpu,STACK_BASE+(cpu->SP--),data))
//==================================================================
// STACK PULL Read
//==================================================================
#define Pull(cpu) (Cpu_Map_Bus_Read(cpu,STACK_BASE+(++cpu->SP)))
//==================================================================
// Cpu_Map_Bus_Read
//ORG 6502 Map Address
//RAM : 0000 - 3FFF
//I/O : 4000 - 7FFF
//ROM : 8000 - FFFF
//Other Devices Can be Mapped Here
//==================================================================
unsigned char Cpu_Map_Bus_Read(Cpu6502 *cpu, unsigned short address)
{
return cpu->Memory[address];
}
//==================================================================
// Cpu_Map_Bus_Write
//ORG 6502 Map Address
//RAM : 0000 - 3FFF
//I/O : 4000 - 7FFF
//ROM : 8000 - FFFF
//Other Devices Can be Mapped Here
//==================================================================
void Cpu_Map_Bus_Write(Cpu6502 *cpu, unsigned short address, unsigned char data)
{
cpu->Memory[address] = data;
}
//==================================================================
//
// PART2:CPU Opcodes
//
//==================================================================
//==================================================================
// Logical
//ORA 01 05 09 0D 11 15 19 1D
//AND 21 25 29 2D 31 35 39 3D
//EOR 41 45 49 4D 51 55 59 5D
//BIT 24 2C
//==================================================================
// AND (Logical AND with Accumulator)
static inline void AND(Cpu6502 *cpu)
{
Cpu_Data_Read(cpu);
cpu->Accumulator &= cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// EOR (Exclusive OR with Accumulator)
static inline void EOR(Cpu6502 *cpu)
{
Cpu_Data_Read(cpu);
cpu->Accumulator ^= cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// ORA (Logical OR with Accumulator)
static inline void ORA(Cpu6502 *cpu)
{
Cpu_Data_Read(cpu);
cpu->Accumulator |= cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// BIT (Test Bits)
static inline void BIT(Cpu6502 *cpu)
{
Cpu_Data_Read(cpu);
cpu->Zero = ((cpu->Accumulator & cpu->Data) == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
cpu->Overflow = (cpu->Data >> 6) & 1;
}
//==================================================================
// Arithmetic
//ADC 61 65 69 6D 71 75 79 7D
//SBC E1 E5 E9 ED F1 F5 F9 FD
//CMP C1 C5 C9 CD D1 D5 D9 DD
//CPY C0 C4 CC
//CPX E0 E4 EC
//==================================================================
//ADC (Add with Carry)
static inline void ADC(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
short value = cpu->Accumulator + cpu->Data + cpu->Carry;
cpu->Carry = (value & 0x100);
cpu->Overflow = (cpu->Accumulator ^ value) & (cpu->Data ^ value) & 0x80;;
cpu->Accumulator = value & 0xFF;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// SBC (Subtract with Borrow)
static inline void SBC(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
short value = cpu->Accumulator - cpu->Data - !cpu->Carry;
cpu->Carry = !(value & 0x100);
cpu->Overflow = (cpu->Accumulator ^ value) & (~cpu->Data ^ value) & 0x80;
cpu->Accumulator = value & 0xFF;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// CMP (Compare with Accumulator)
static inline void CMP(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
unsigned char value = cpu->Accumulator - cpu->Data;
cpu->Carry = (cpu->Accumulator >= cpu->Data);
cpu->Zero = (value == 0);
cpu->Negative = (value >> 7) & 1;
}
//==================================================================
// CPX (Compare with X Register)
static inline void CPX(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
unsigned char value = cpu->X - cpu->Data;
cpu->Carry = (cpu->X >= cpu->Data);
cpu->Zero = (value == 0);
cpu->Negative = (value >> 7) & 1;
}
//==================================================================
// CPY (Compare with Y Register)
static inline void CPY(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
unsigned char value = cpu->Y - cpu->Data;
cpu->Carry = (cpu->Y >= cpu->Data);
cpu->Zero = (value == 0);
cpu->Negative = (value >> 7) & 1;
}
//==================================================================
// Increments & Decrements
//INC E6 EE F6 FE
//INX E8
//INY C8
//DEC C6 CE D6 DE
//DEX CA
//DEY 88
//==================================================================
// INC (Increment Memory)
static inline void INC(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Data++;
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
Cpu_Data_Write(cpu);
}
//==================================================================
// INX (Increment X Register)
static inline void INX(Cpu6502* cpu)
{
cpu->X++;
cpu->Zero = (cpu->X == 0);
cpu->Negative = (cpu->X >> 7) & 1;
}
//==================================================================
// INY (Increment Y Register)
static inline void INY(Cpu6502* cpu)
{
cpu->Y++;
cpu->Zero = (cpu->Y == 0);
cpu->Negative = (cpu->Y >> 7) & 1;
}
//==================================================================
// DEC (Decrement Memory)
static inline void DEC(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Data--;
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
Cpu_Data_Write(cpu);
}
//==================================================================
// DEX (Decrement X Register)
static inline void DEX(Cpu6502 *cpu)
{
cpu->X--;
cpu->Zero = (cpu->X == 0);
cpu->Negative = (cpu->X >> 7) & 1;
}
//==================================================================
// DEY (Decrement Y Register)
static inline void DEY(Cpu6502* cpu)
{
cpu->Y--;
cpu->Zero = (cpu->Y == 0);
cpu->Negative = (cpu->Y >> 7) & 1;
}
//==================================================================
// Shifts
//ASL 06 0A 0E 16 1E
//LSR 46 4A 4E 56 5E
//ROL 26 2A 2E 36 3E
//ROR 66 6A 6E 6A 6E 76
//==================================================================
// ASL (Arithmetic Shift Left)
static inline void ASL(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Carry = (cpu->Data >> 7) & 1;
cpu->Data <<= 1;
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
Cpu_Data_Write(cpu);
}
//==================================================================
// LSR (Logical Shift Right)
static inline void LSR(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Carry = cpu->Data & 0x01;
cpu->Data = (cpu->Data >> 1);
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
Cpu_Data_Write(cpu);
}
//==================================================================
// ROL (Rotate Left)
static inline void ROL(Cpu6502 *cpu)
{
Cpu_Data_Read(cpu);
unsigned char old_carry = cpu->Carry;
cpu->Carry = (cpu->Data & 0x80);
cpu->Data = (cpu->Data << 1) | old_carry;
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
Cpu_Data_Write(cpu);
}
//==================================================================
// ROR (Rotate Right)
static inline void ROR(Cpu6502 *cpu)
{
Cpu_Data_Read(cpu);
unsigned char old_carry = cpu->Carry << 7;
cpu->Carry = cpu->Data & 0x01;
cpu->Data = (cpu->Data >> 1) | old_carry;
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
Cpu_Data_Write(cpu);
}
//==================================================================
// Jumps & Calls
//JMP 4C 6C
//JSR 20
//RTS 60
//==================================================================
// JMP (Jump)
static inline void JMP(Cpu6502* cpu)
{
cpu->PC = cpu->Address;
}
//==================================================================
//Jump to a subroutine
static inline void JSR(Cpu6502* cpu)
{
unsigned short ReturnAddress = cpu->PC;
Push(cpu, (ReturnAddress >> 8) & 0xFF);//low byte
Push(cpu, ReturnAddress & 0xFF); //high byte
cpu->PC = cpu->Address;
}
//==================================================================
// RTS (Return from Subroutine)
static inline void RTS(Cpu6502 *cpu)
{
unsigned char PCL = Pull(cpu);//sp++
unsigned char PCH = Pull(cpu);//sp++
cpu->PC = (PCL | (PCH << 8));
}
//==================================================================
// Branches
//BCC 90
//BCS B0
//BEQ F0
//BMI 30
//BNE D0
//BPL 10
//BVC 50
//BVS 70
//==================================================================
// BCC (Branch if Carry Clear)
static inline void BCC(Cpu6502* cpu)
{
if (cpu->Carry == 0) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
// BCS (Branch if Carry Set)
static inline void BCS(Cpu6502* cpu)
{
if (cpu->Carry == 1) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
// BEQ (Branch if Equal)
static inline void BEQ(Cpu6502* cpu)
{
if (cpu->Zero == 1) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
// BMI (Branch if Minus)
static inline void BMI(Cpu6502* cpu)
{
if (cpu->Negative == 1) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
// BNE (Branch if Not Equal)
static inline void BNE(Cpu6502* cpu)
{
if (cpu->Zero == 0) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
// BPL (Branch if Positive)
static inline void BPL(Cpu6502* cpu)
{
if (cpu->Negative == 0) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
//BVC Branch if overflow flag clea
static inline void BVC(Cpu6502* cpu)
{
if (cpu->Overflow == 0) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
//BVS(Branch if Overflow Set)
static inline void BVS(Cpu6502* cpu)
{
if (cpu->Overflow == 1) {
cpu->PC += (signed char)(cpu->Address);
PageCrossing(cpu->OldPC, cpu->PC, 1);
}
}
//==================================================================
// Status Flag Changes
//CLC 18
//SEC 38
//CLI 58
//SEI 78
//CLV B8
//CLD D8
//SED F8
//==================================================================
// CLC (Clear Carry Flag)
static inline void CLC(Cpu6502* cpu)
{
cpu->Carry = 0;
}
//==================================================================
// CLD (Clear Decimal Mode)
static inline void CLD(Cpu6502* cpu)
{
cpu->DecimalMode = 0;
}
//==================================================================
// CLI (Clear Interrupt Disable)
static inline void CLI(Cpu6502* cpu)
{
cpu->Interrupt = 0;
}
//==================================================================
// CLV (Clear Overflow Flag)
static inline void CLV(Cpu6502* cpu)
{
cpu->Overflow = 0;
}
//==================================================================
// SEC (Set Carry Flag)
static inline void SEC(Cpu6502 *cpu)
{
cpu->Carry = 1;
}
//==================================================================
// SED (Set Decimal Mode)
static inline void SED(Cpu6502 *cpu)
{
cpu->DecimalMode = 1;
}
//==================================================================
// SEI (Set Interrupt Disable)
static inline void SEI(Cpu6502 *cpu)
{
cpu->Interrupt = 1;
}
//==================================================================
// System Functions
//BRK 00
//NOP EA
//RTI 40
//==================================================================
// BRK (Break)
static inline void BRK(Cpu6502* cpu)
{
cpu->Interrupt = 1;
unsigned short ReturnAddress = cpu->PC;
Push(cpu, (ReturnAddress >> 8) & 0xFF);
Push(cpu, ReturnAddress & 0xFF);
unsigned char status = get_Status(cpu);
Push(cpu, status);
unsigned char PCL = Cpu_Map_Bus_Read(cpu, 0xFFFE);
unsigned char PCH = Cpu_Map_Bus_Read(cpu, 0xFFFF);
cpu->PC = PCL | (PCH << 8);
}
//==================================================================
// NOP (No Operation)
static inline void NOP(Cpu6502 *cpu)
{
//...
}
//==================================================================
// RTI (Return from Interrupt)
static inline void RTI(Cpu6502 *cpu)
{
unsigned char status = Pull(cpu);//sp++
set_Status(cpu, status);
unsigned char PCL = Pull(cpu);//sp++
unsigned char PCH = Pull(cpu);//sp++
cpu->PC = (PCL | (PCH << 8));
}
//==================================================================
// Register Transfers
//TXA 8A
//TYA 98
//TAY A8
//TAX AA
//==================================================================
// TAX (Transfer Accumulator to X)
static inline void TAX(Cpu6502 *cpu)
{
cpu->X = cpu->Accumulator;
cpu->Zero = (cpu->X == 0);
cpu->Negative = (cpu->X >> 7) & 1;
}
//==================================================================
// TAY (Transfer Accumulator to Y)
static inline void TAY(Cpu6502 *cpu)
{
cpu->Y = cpu->Accumulator;
cpu->Zero = (cpu->Y == 0);
cpu->Negative = (cpu->Y >> 7) & 1;
}
//==================================================================
// TXA (Transfer X to Accumulator)
static inline void TXA(Cpu6502 *cpu)
{
cpu->Accumulator = cpu->X;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// TYA (Transfer Y to Accumulator)
static inline void TYA(Cpu6502 *cpu)
{
cpu->Accumulator = cpu->Y;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// Stack Operations
//PHP 08
//PLP 28
//PHA 48
//PLA 68
//TXS 8A
//TSX BA
//==================================================================
// TSX (Transfer Stack Pointer to X)
static inline void TSX(Cpu6502 *cpu)
{
cpu->X = cpu->SP;
cpu->Zero = (cpu->X == 0);
cpu->Negative = (cpu->X >> 7) & 1;
}
//==================================================================
// TXS (Transfer X to Stack Pointer)
static inline void TXS(Cpu6502 *cpu)
{
cpu->SP = cpu->X;
}
//==================================================================
// PHA (Push Accumulator)
static inline void PHA(Cpu6502 *cpu)
{
Push(cpu, cpu->Accumulator);
}
//==================================================================
// PHP (Push Processor Status)
static inline void PHP(Cpu6502 *cpu)
{
unsigned char status = get_Status(cpu) | 0x30;
Push(cpu, status);
}
//==================================================================
// PLA (Pull Accumulator)
static inline void PLA(Cpu6502 *cpu)
{
cpu->Accumulator = Pull(cpu);
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// PLP (Pull Processor Status
static inline void PLP(Cpu6502 *cpu)
{
unsigned char status = Pull(cpu);
set_Status(cpu, status);
}
//==================================================================
// Load/Store Operations
//LDA A1 A5 A9 AD B1 B5 BD
//LDX A2 A6 AE B6 BE
//LDY A0 A4 AC B4 BC
//STA 81 85 8D 91 95 99 9D
//STX 86 8E 96
//STY 84 8C 94
//==================================================================
// LDA (Load Accumulator)
static inline void LDA(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Accumulator = cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// LDX (Load X Register)
static inline void LDX(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->X = cpu->Data;
cpu->Zero = (cpu->X == 0);
cpu->Negative = (cpu->X >> 7) & 1;
}
//==================================================================
// LDY (Load Y Register)
static inline void LDY(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Y = cpu->Data;
cpu->Zero = (cpu->Y == 0);
cpu->Negative = (cpu->Y >> 7) & 1;
}
//==================================================================
//STA Store Accumulator in Memory
static inline void STA(Cpu6502* cpu)
{
Cpu_Data_Write(cpu);
}
//==================================================================
// STX (Store RegisterX in Memory)
static inline void STX(Cpu6502 *cpu)
{
Cpu_Data_Write(cpu);
}
//==================================================================
// STY (Store RegisterY in Memory)
static inline void STY(Cpu6502 *cpu)
{
Cpu_Data_Write(cpu);
}
//==================================================================
// ...
//==================================================================
static inline void XXX(Cpu6502* cpu)
{
//...
}
//==================================================================
//==================================================================
// Unofficial Instructions
//LAX A3 A7 AF B3 B7 BF
//SAX 83 87 8F 97
//DCP C3 C7 CF D3 D7 DB DF
//ISB E3 E7 EF F3 F7 FB FF
//SLO 03 07 17 13 0F 1F 1B
//RLA 23 27 2F 33 37 3F 3B
//SRE 43 47 4F 53 57 5F 5B
//RRA 63 67 6F 73 77 7F 7B
//==================================================================
//LAX (Load Accumulator & X)
static inline void LAX(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Accumulator = cpu->Data;
cpu->X = cpu->Data;
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
}
//==================================================================
//SAX (Store A AND X)
static inline void SAX(Cpu6502* cpu)
{
cpu->Data = cpu->Accumulator & cpu->X;
Cpu_Data_Write(cpu);
}
//==================================================================
//DCP (Decrement then Compare)
static inline void DCP(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Data--;
Cpu_Data_Write(cpu);
cpu->Zero = (cpu->Data == 0);
cpu->Negative = (cpu->Data >> 7) & 1;
cpu->Carry = (cpu->Accumulator >= cpu->Data);
}
//==================================================================
//ISB (Increment then Subtract with Carry)
static inline void ISB(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Data++;
Cpu_Data_Write(cpu);
unsigned short value = cpu->Accumulator - cpu->Data - !cpu->Carry;
cpu->Carry = !(value & 0x100);
cpu->Overflow = (cpu->Accumulator ^ value) & (~cpu->Data ^ value) & 0x80;
cpu->Accumulator = value & 0xFF;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
//SLO (Shift Left then OR)
static inline void SLO(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Carry = (cpu->Data & 0x80);
cpu->Data <<= 1;
Cpu_Data_Write(cpu);
cpu->Accumulator |= cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
//RLA (Rotate Left then AND)
static inline void RLA(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
unsigned char old_carry = cpu->Carry;
cpu->Carry = cpu->Data & 0x80;
cpu->Data = (cpu->Data << 1) | old_carry;
Cpu_Data_Write(cpu);
cpu->Accumulator &= cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
//SRE (Shift Right then EOR)
static inline void SRE(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
cpu->Carry = cpu->Data & 0x01;
cpu->Data = (cpu->Data >> 1) & 0x7F;
Cpu_Data_Write(cpu);
cpu->Accumulator ^= cpu->Data;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
//RRA (Rotate Right then Add with Carry)
static inline void RRA(Cpu6502* cpu)
{
Cpu_Data_Read(cpu);
unsigned char old_carry = cpu->Carry << 7;
cpu->Carry = cpu->Data & 0x01;
unsigned char valueA = old_carry | (cpu->Data >> 1);
Cpu_Data_Write(cpu);
unsigned char valueB = cpu->Accumulator + valueA + cpu->Carry;
cpu->Carry = (valueB & 0x100);
cpu->Overflow = (cpu->Accumulator ^ valueB) & (valueA ^ valueB) & 0x80;
cpu->Accumulator = valueB;
cpu->Zero = (cpu->Accumulator == 0);
cpu->Negative = (cpu->Accumulator >> 7) & 1;
}
//==================================================================
// CPU_IRQ
//==================================================================
void CPU_IRQ(Cpu6502 *cpu)
{
if (cpu->Interrupt == 1) return;
unsigned short ReturnAddress = cpu->PC;
#ifdef DEBUG_CPU
cpu_print("CPU_IRQ->ReturnAddress[%04X]\n", ReturnAddress);
#endif
// Push PC onto stack (high then low)
Push(cpu, (ReturnAddress >> 8) & 0xFF);
Push(cpu, (ReturnAddress & 0xFF));
unsigned char status = get_Status(cpu) | 0x30;
Push(cpu, status);
unsigned char PCL = Cpu_Map_Bus_Read(cpu, 0xFFFE);
unsigned char PCH = Cpu_Map_Bus_Read(cpu, 0xFFFF);
cpu->PC = PCL | (PCH << 8);
#ifdef DEBUG_CPU
cpu_print("CPU_IRQ->NextAddress[%04X]\n", cpu->PC);
#endif
cpu->Cycle += 7;
}
//==================================================================
// CPU_NMI
//==================================================================
void CPU_NMI(Cpu6502 *cpu)
{
unsigned short ReturnAddress = cpu->PC;
#ifdef DEBUG_CPU
cpu_print("CPU_NMI->ReturnAddress[%04X]\n", ReturnAddress);
#endif
// Push PC onto stack (high byte then low byte)
Push(cpu, (ReturnAddress >> 8) & 0xFF);
Push(cpu, (ReturnAddress & 0xFF));
unsigned char status = get_Status(cpu) | 0x30;
Push(cpu, status);
unsigned char PCL = Cpu_Map_Bus_Read(cpu, 0xFFFA);
unsigned char PCH = Cpu_Map_Bus_Read(cpu, 0xFFFB);
cpu->PC = PCL | (PCH << 8);
#ifdef DEBUG_CPU
cpu_print("CPU_NMI->NextAddress[%04X]\n", cpu->PC);
#endif
cpu->Cycle += 8;
}
//==================================================================
void CPU_IRQ_DISABLE(Cpu6502 *cpu)
{
cpu->Interrupt = 1;
cpu->IRQ_Enable = DISABLE;
}
//==================================================================
void CPU_IRQ_ENABLE(Cpu6502 *cpu)
{
cpu->Interrupt = 0;
cpu->IRQ_Enable = ENABLE;
}
//==================================================================
void CPU_NMI_DISABLE(Cpu6502 *cpu)
{
cpu->Interrupt = 1;
cpu->NMI_Enable = DISABLE;
}
//==================================================================
void CPU_NMI_ENABLE(Cpu6502 *cpu)
{
cpu->Interrupt = 0;
cpu->NMI_Enable = ENABLE;
}
//==================================================================
//
// PART3:Data Write/Read and Addressing Mode
//
//==================================================================
//==================================================================
// Cpu_Data_Write
//==================================================================
void Cpu_Data_Write(Cpu6502* cpu)
{
//===============================
// Register Write
//===============================
if (cpu->Mode == ACC || cpu->Mode == IMP) //ACC(ASL,ROL,LSR,ROR) | IMP(SBC)
{
cpu->Accumulator = cpu->Data;
return;
}
//===============================
// Memory & Register Write
//===============================
if (cpu->OpcodeString == "STA")
{
Cpu_Map_Bus_Write(cpu, cpu->Address, cpu->Accumulator);
return;
}
if (cpu->OpcodeString == "STX")
{
Cpu_Map_Bus_Write(cpu, cpu->Address, cpu->X);
return;
}
if (cpu->OpcodeString == "STY")
{
Cpu_Map_Bus_Write(cpu, cpu->Address, cpu->Y);
return;
}
//===============================
// Memory Write
//===============================
Cpu_Map_Bus_Write(cpu, cpu->Address, cpu->Data);
}
//==================================================================
// Cpu_Data_Read
//==================================================================
void Cpu_Data_Read(Cpu6502* cpu)
{
//===============================
// Register Read
//===============================
if (cpu->Mode == ACC || cpu->Mode == IMP) //ACC(ASL,ROL,LSR,ROR) | IMP(SBC)
{
cpu->Data = cpu->Accumulator;
return;
}
//===============================
// Memory Read
//===============================
cpu->Data = Cpu_Map_Bus_Read(cpu, cpu->Address);
}
//==================================================================
// Addressing Mode ,Return *OpcodeStep*
//==================================================================
static inline void Addressing_Mode(Cpu6502 *cpu)
{
unsigned char low, high;
unsigned short Operand_address;
cpu->Mode = Find_AddressMode(cpu);
cpu->OldPC = cpu->PC;
switch (cpu->Mode)
{
//===============================
// Zero Page
//===============================
case ZP0: // Zero Page
cpu->Address = Read_Operand1Byte(cpu);
cpu->OpcodeStep = 2;
break;
case ZPX: // Zero Page,X
cpu->Address = Read_Operand1Byte(cpu);
cpu->Address = (cpu->Address + cpu->X) & 0xFF;
cpu->OpcodeStep = 2;
break;
case ZPY: // Zero Page,Y
cpu->Address = Read_Operand1Byte(cpu);
cpu->Address = (cpu->Address + cpu->Y) & 0xFF;
cpu->OpcodeStep = 2;
break;
//===============================
// Absolute
//===============================
case ABS: // Absolute
cpu->Address = Read_Operand2Byte(cpu);
cpu->OpcodeStep = 3;
break;
case ABX: // Absolute,X
Operand_address = Read_Operand2Byte(cpu);
cpu->Address = Operand_address + cpu->X;
PageCrossing(Operand_address, cpu->Address, 0);
cpu->OpcodeStep = 3;
break;
case ABY: // Absolute,Y
Operand_address = Read_Operand2Byte(cpu);
cpu->Address = Operand_address + cpu->Y;
PageCrossing(Operand_address, cpu->Address, 0);
cpu->OpcodeStep = 3;
break;
//===============================
// Indirect
//===============================
case IND: // Indirect (only used by JMP)
cpu->Address = Read_Operand2Byte(cpu);
if ((cpu->Address & 0x00FF) == 0x00FF) {
low = Cpu_Map_Bus_Read(cpu, cpu->Address);
high = Cpu_Map_Bus_Read(cpu, cpu->Address & 0xFF00);
cpu->Address = low | (high << 8);
}
else {
low = Cpu_Map_Bus_Read(cpu, cpu->Address);
high = Cpu_Map_Bus_Read(cpu, cpu->Address + 1);
cpu->Address = low | (high << 8);
}
cpu->OpcodeStep = 3;
break;
case IZX: // IZX (Indirect,X)
cpu->Address = Read_Operand1Byte(cpu);
low = Cpu_Map_Bus_Read(cpu, (cpu->Address + cpu->X) & 0xFF);
high = Cpu_Map_Bus_Read(cpu, (cpu->Address + cpu->X + 1) & 0xFF);
cpu->Address = low | (high << 8);
cpu->OpcodeStep = 2;
break;
case IZY: // IZY (Indirect),Y
cpu->Address = Read_Operand1Byte(cpu);
low = Cpu_Map_Bus_Read(cpu, (cpu->Address & 0xFF));
high = Cpu_Map_Bus_Read(cpu, (cpu->Address + 1) & 0xFF);
Operand_address = (low | (high << 8));
cpu->Address = Operand_address + cpu->Y;
PageCrossing(Operand_address, cpu->Address, 0);
cpu->OpcodeStep = 2;
break;
//===============================
// REL ,IMM ,IMP ,ACC
//===============================
case REL: // Relative (for branch instructions)
cpu->Address = Read_Operand1Byte(cpu);
cpu->OpcodeStep = 2;
break;
case IMM: // Immediate
cpu->Address = (cpu->PC + 1);
cpu->OpcodeStep = 2;
break;
case IMP: // Implied
cpu->Address = cpu->PC;
cpu->OpcodeStep = 1;
break;