-
Notifications
You must be signed in to change notification settings - Fork 24
/
Processor.cs
2533 lines (2295 loc) · 71.2 KB
/
Processor.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 NLog;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
namespace Processor
{
/// <summary>
/// An Implementation of a 6502 Processor
/// </summary>
[Serializable]
public class Processor
{
#region Fields
private static readonly ILogger _logger = LogManager.GetLogger("Processor");
private int _programCounter;
private int _stackPointer;
private int _cycleCount;
private bool _previousInterrupt;
private bool _interrupt;
#endregion
//All of the properties here are public and read only to facilitate ease of debugging and testing.
#region Properties
/// <summary>
/// The memory
/// </summary>
protected byte[] Memory { get; private set; }
/// <summary>
/// The Accumulator. This value is implemented as an integer intead of a byte.
/// This is done so we can detect wrapping of the value and set the correct number of cycles.
/// </summary>
public int Accumulator { get; protected set; }
/// <summary>
/// The X Index Register
/// </summary>
public int XRegister { get; private set; }
/// <summary>
/// The Y Index Register
/// </summary>
public int YRegister { get; private set; }
/// <summary>
/// The Current Op Code being executed by the system
/// </summary>
public int CurrentOpCode { get; private set; }
/// <summary>
/// The disassembly of the current operation. This value is only set when the CPU is built in debug mode.
/// </summary>
public Disassembly CurrentDisassembly { get; private set; }
/// <summary>
/// Points to the Current Address of the instruction being executed by the system.
/// The PC wraps when the value is greater than 65535, or less than 0.
/// </summary>
public int ProgramCounter
{
get { return _programCounter; }
private set { _programCounter = WrapProgramCounter(value); }
}
/// <summary>
/// Points to the Current Position of the Stack.
/// This value is a 00-FF value but is offset to point to the location in memory where the stack resides.
/// </summary>
public int StackPointer
{
get { return _stackPointer; }
private set
{
if (value > 0xFF)
_stackPointer = value - 0x100;
else if (value < 0x00)
_stackPointer = value + 0x100;
else
_stackPointer = value;
}
}
/// <summary>
/// An external action that occurs when the cycle count is incremented
/// </summary>
public Action CycleCountIncrementedAction { get; set; }
//Status Registers
/// <summary>
/// This is the carry flag. when adding, if the result is greater than 255 or 99 in BCD Mode, then this bit is enabled.
/// In subtraction this is reversed and set to false if a borrow is required IE the result is less than 0
/// </summary>
public bool CarryFlag { get; protected set; }
/// <summary>
/// Is true if one of the registers is set to zero.
/// </summary>
public bool ZeroFlag { get; private set; }
/// <summary>
/// This determines if Interrupts are currently disabled.
/// This flag is turned on during a reset to prevent an interrupt from occuring during startup/Initialization.
/// If this flag is true, then the IRQ pin is ignored.
/// </summary>
public bool DisableInterruptFlag { get; private set; }
/// <summary>
/// Binary Coded Decimal Mode is set/cleared via this flag.
/// when this mode is in effect, a byte represents a number from 0-99.
/// </summary>
public bool DecimalFlag { get; private set; }
/// <summary>
/// This property is set when an overflow occurs. An overflow happens if the high bit(7) changes during the operation. Remember that values from 128-256 are negative values
/// as the high bit is set to 1.
/// Examples:
/// 64 + 64 = -128
/// -128 + -128 = 0
/// </summary>
public bool OverflowFlag { get; protected set; }
/// <summary>
/// Set to true if the result of an operation is negative in ADC and SBC operations.
/// Remember that 128-256 represent negative numbers when doing signed math.
/// In shift operations the sign holds the carry.
/// </summary>
public bool NegativeFlag { get; private set; }
/// <summary>
/// Set to true when an NMI should occur
/// </summary>
public bool TriggerNmi { get; set; }
/// Set to true when an IRQ has occurred and is being processed by the CPU
public bool TriggerIRQ { get; private set; }
#endregion
#region Public Methods
/// <summary>
/// Default Constructor, Instantiates a new instance of the processor.
/// </summary>
public Processor()
{
Memory = new byte[0x10000];
StackPointer = 0x100;
CycleCountIncrementedAction = () => { };
}
/// <summary>
/// Initializes the processor to its default state.
/// </summary>
public void Reset()
{
ResetCycleCount();
StackPointer = 0x1FD;
//Set the Program Counter to the Reset Vector Address.
ProgramCounter = 0xFFFC;
//Reset the Program Counter to the Address contained in the Reset Vector
ProgramCounter = ( Memory[ProgramCounter] | ( Memory[ProgramCounter + 1] << 8));;
CurrentOpCode = Memory[ProgramCounter];
//SetDisassembly();
DisableInterruptFlag = true;
_previousInterrupt = false;
TriggerNmi = false;
TriggerIRQ = false;
}
/// <summary>
/// Performs the next step on the processor
/// </summary>
public void NextStep()
{
SetDisassembly();
//Have to read this first otherwise it causes tests to fail on a NES
CurrentOpCode = ReadMemoryValue(ProgramCounter);
ProgramCounter++;
ExecuteOpCode();
if (_previousInterrupt)
{
if (TriggerNmi)
{
ProcessNMI();
TriggerNmi = false;
}
else if (TriggerIRQ)
{
ProcessIRQ();
TriggerIRQ = false;
}
}
}
/// <summary>
/// Loads a program into the processors memory
/// </summary>
/// <param name="offset">The offset in memory when loading the program.</param>
/// <param name="program">The program to be loaded</param>
/// <param name="initialProgramCounter">The initial PC value, this is the entry point of the program</param>
public void LoadProgram(int offset, byte[] program, int initialProgramCounter)
{
LoadProgram(offset, program);
var bytes = BitConverter.GetBytes(initialProgramCounter);
//Write the initialProgram Counter to the reset vector
WriteMemoryValue(0xFFFC,bytes[0]);
WriteMemoryValue(0xFFFD, bytes[1]);
//Reset the CPU
Reset();
}
/// <summary>
/// Loads a program into the processors memory
/// </summary>
/// <param name="offset">The offset in memory when loading the program.</param>
/// <param name="program">The program to be loaded</param>
public void LoadProgram(int offset, byte[] program)
{
if (offset > Memory.Length)
throw new InvalidOperationException("Offset '{0}' is larger than memory size '{1}'");
if (program.Length + offset > Memory.Length)
throw new InvalidOperationException(string.Format("Program Size '{0}' Cannot be Larger than Memory Size '{1}' plus offset '{2}'", program.Length, Memory.Length, offset));
for (var i = 0; i < program.Length; i++)
{
Memory[i + offset] = program[i];
}
Reset();
}
/// <summary>
/// The InterruptRequest or IRQ
/// </summary>
public void InterruptRequest()
{
TriggerIRQ = true;
}
/// <summary>
/// Clears the memory
/// </summary>
public void ClearMemory()
{
for (var i = 0; i < Memory.Length; i++)
Memory[i] = 0x00;
}
/// <summary>
/// Returns the byte at the given address.
/// </summary>
/// <param name="address">The address to return</param>
/// <returns>the byte being returned</returns>
public virtual byte ReadMemoryValue(int address)
{
var value = Memory[address];
IncrementCycleCount();
return value;
}
/// <summary>
/// Returns the byte at a given address without incrementing the cycle. Useful for test harness.
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public virtual byte ReadMemoryValueWithoutCycle(int address)
{
var value = Memory[address];
return value;
}
/// <summary>
/// Writes data to the given address.
/// </summary>
/// <param name="address">The address to write data to</param>
/// <param name="data">The data to write</param>
public virtual void WriteMemoryValue(int address, byte data)
{
IncrementCycleCount();
Memory[address] = data;
}
/// <summary>
/// Gets the Number of Cycles that have elapsed
/// </summary>
/// <returns>The number of elapsed cycles</returns>
public int GetCycleCount()
{
return _cycleCount;
}
/// <summary>
/// Increments the Cycle Count, causes a CycleCountIncrementedAction to fire.
/// </summary>
protected void IncrementCycleCount()
{
_cycleCount++;
CycleCountIncrementedAction();
_previousInterrupt = _interrupt;
_interrupt = TriggerNmi || (TriggerIRQ && !DisableInterruptFlag);
}
/// <summary>
/// Resets the Cycle Count back to 0
/// </summary>
public void ResetCycleCount()
{
_cycleCount = 0;
}
/// <summary>
/// Dumps the entire memory object. Used when saving the memory state
/// </summary>
/// <returns></returns>
public byte[] DumpMemory()
{
return Memory;
}
#endregion
#region Private Methods
/// <summary>
/// Executes an Opcode
/// </summary>
private void ExecuteOpCode()
{
//The x+ cycles denotes that if a page wrap occurs, then an additional cycle is consumed.
//The x++ cycles denotes that 1 cycle is added when a branch occurs and it on the same page, and two cycles are added if its on a different page./
//This is handled inside the GetValueFromMemory Method
switch (CurrentOpCode)
{
#region Add / Subtract Operations
//ADC Add With Carry, Immediate, 2 Bytes, 2 Cycles
case 0x69:
{
AddWithCarryOperation(AddressingMode.Immediate);
break;
}
//ADC Add With Carry, Zero Page, 2 Bytes, 3 Cycles
case 0x65:
{
AddWithCarryOperation(AddressingMode.ZeroPage);
break;
}
//ADC Add With Carry, Zero Page X, 2 Bytes, 4 Cycles
case 0x75:
{
AddWithCarryOperation(AddressingMode.ZeroPageX);
break;
}
//ADC Add With Carry, Absolute, 3 Bytes, 4 Cycles
case 0x6D:
{
AddWithCarryOperation(AddressingMode.Absolute);
break;
}
//ADC Add With Carry, Absolute X, 3 Bytes, 4+ Cycles
case 0x7D:
{
AddWithCarryOperation(AddressingMode.AbsoluteX);
break;
}
//ADC Add With Carry, Absolute Y, 3 Bytes, 4+ Cycles
case 0x79:
{
AddWithCarryOperation(AddressingMode.AbsoluteY);
break;
}
//ADC Add With Carry, Indexed Indirect, 2 Bytes, 6 Cycles
case 0x61:
{
AddWithCarryOperation(AddressingMode.IndirectX);
break;
}
//ADC Add With Carry, Indexed Indirect, 2 Bytes, 5+ Cycles
case 0x71:
{
AddWithCarryOperation(AddressingMode.IndirectY);
break;
}
//SBC Subtract with Borrow, Immediate, 2 Bytes, 2 Cycles
case 0xE9:
{
SubtractWithBorrowOperation(AddressingMode.Immediate);
break;
}
//SBC Subtract with Borrow, Zero Page, 2 Bytes, 3 Cycles
case 0xE5:
{
SubtractWithBorrowOperation(AddressingMode.ZeroPage);
break;
}
//SBC Subtract with Borrow, Zero Page X, 2 Bytes, 4 Cycles
case 0xF5:
{
SubtractWithBorrowOperation(AddressingMode.ZeroPageX);
break;
}
//SBC Subtract with Borrow, Absolute, 3 Bytes, 4 Cycles
case 0xED:
{
SubtractWithBorrowOperation(AddressingMode.Absolute);
break;
}
//SBC Subtract with Borrow, Absolute X, 3 Bytes, 4+ Cycles
case 0xFD:
{
SubtractWithBorrowOperation(AddressingMode.AbsoluteX);
break;
}
//SBC Subtract with Borrow, Absolute Y, 3 Bytes, 4+ Cycles
case 0xF9:
{
SubtractWithBorrowOperation(AddressingMode.AbsoluteY);
break;
}
//SBC Subtract with Borrow, Indexed Indirect, 2 Bytes, 6 Cycles
case 0xE1:
{
SubtractWithBorrowOperation(AddressingMode.IndirectX);
break;
}
//SBC Subtract with Borrow, Indexed Indirect, 2 Bytes, 5+ Cycles
case 0xF1:
{
SubtractWithBorrowOperation(AddressingMode.IndirectY);
break;
}
#endregion
#region Branch Operations
//BCC Branch if Carry is Clear, Relative, 2 Bytes, 2++ Cycles
case 0x90:
{
BranchOperation(!CarryFlag);
break;
}
//BCS Branch if Carry is Set, Relative, 2 Bytes, 2++ Cycles
case 0xB0:
{
BranchOperation(CarryFlag);
break;
}
//BEQ Branch if Zero is Set, Relative, 2 Bytes, 2++ Cycles
case 0xF0:
{
BranchOperation(ZeroFlag);
break;
}
// BMI Branch if Negative Set
case 0x30:
{
BranchOperation(NegativeFlag);
break;
}
//BNE Branch if Zero is Not Set, Relative, 2 Bytes, 2++ Cycles
case 0xD0:
{
BranchOperation(!ZeroFlag);
break;
}
// BPL Branch if Negative Clear, 2 Bytes, 2++ Cycles
case 0x10:
{
BranchOperation(!NegativeFlag);
break;
}
// BVC Branch if Overflow Clear, 2 Bytes, 2++ Cycles
case 0x50:
{
BranchOperation(!OverflowFlag);
break;
}
// BVS Branch if Overflow Set, 2 Bytes, 2++ Cycles
case 0x70:
{
BranchOperation(OverflowFlag);
break;
}
#endregion
#region BitWise Comparison Operations
//AND Compare Memory with Accumulator, Immediate, 2 Bytes, 2 Cycles
case 0x29:
{
AndOperation(AddressingMode.Immediate);
break;
}
//AND Compare Memory with Accumulator, Zero Page, 2 Bytes, 3 Cycles
case 0x25:
{
AndOperation(AddressingMode.ZeroPage);
break;
}
//AND Compare Memory with Accumulator, Zero PageX, 2 Bytes, 3 Cycles
case 0x35:
{
AndOperation(AddressingMode.ZeroPageX);
break;
}
//AND Compare Memory with Accumulator, Absolute, 3 Bytes, 4 Cycles
case 0x2D:
{
AndOperation(AddressingMode.Absolute);
break;
}
//AND Compare Memory with Accumulator, AbsolueteX 3 Bytes, 4+ Cycles
case 0x3D:
{
AndOperation(AddressingMode.AbsoluteX);
break;
}
//AND Compare Memory with Accumulator, AbsoluteY, 3 Bytes, 4+ Cycles
case 0x39:
{
AndOperation(AddressingMode.AbsoluteY);
break;
}
//AND Compare Memory with Accumulator, IndexedIndirect, 2 Bytes, 6 Cycles
case 0x21:
{
AndOperation(AddressingMode.IndirectX);
break;
}
//AND Compare Memory with Accumulator, IndirectIndexed, 2 Bytes, 5 Cycles
case 0x31:
{
AndOperation(AddressingMode.IndirectY);
break;
}
//BIT Compare Memory with Accumulator, Zero Page, 2 Bytes, 3 Cycles
case 0x24:
{
BitOperation(AddressingMode.ZeroPage);
break;
}
//BIT Compare Memory with Accumulator, Absolute, 2 Bytes, 4 Cycles
case 0x2C:
{
BitOperation(AddressingMode.Absolute);
break;
}
//EOR Exclusive OR Memory with Accumulator, Immediate, 2 Bytes, 2 Cycles
case 0x49:
{
EorOperation(AddressingMode.Immediate);
break;
}
//EOR Exclusive OR Memory with Accumulator, Zero Page, 2 Bytes, 3 Cycles
case 0x45:
{
EorOperation(AddressingMode.ZeroPage);
break;
}
//EOR Exclusive OR Memory with Accumulator, Zero Page X, 2 Bytes, 4 Cycles
case 0x55:
{
EorOperation(AddressingMode.ZeroPageX);
break;
}
//EOR Exclusive OR Memory with Accumulator, Absolute, 3 Bytes, 4 Cycles
case 0x4D:
{
EorOperation(AddressingMode.Absolute);
break;
}
//EOR Exclusive OR Memory with Accumulator, Absolute X, 3 Bytes, 4+ Cycles
case 0x5D:
{
EorOperation(AddressingMode.AbsoluteX);
break;
}
//EOR Exclusive OR Memory with Accumulator, Absolute Y, 3 Bytes, 4+ Cycles
case 0x59:
{
EorOperation(AddressingMode.AbsoluteY);
break;
}
//EOR Exclusive OR Memory with Accumulator, IndexedIndirect, 2 Bytes 6 Cycles
case 0x41:
{
EorOperation(AddressingMode.IndirectX);
break;
}
//EOR Exclusive OR Memory with Accumulator, IndirectIndexed, 2 Bytes 5 Cycles
case 0x51:
{
EorOperation(AddressingMode.IndirectY);
break;
}
//ORA Compare Memory with Accumulator, Immediate, 2 Bytes, 2 Cycles
case 0x09:
{
OrOperation(AddressingMode.Immediate);
break;
}
//ORA Compare Memory with Accumulator, Zero Page, 2 Bytes, 2 Cycles
case 0x05:
{
OrOperation(AddressingMode.ZeroPage);
break;
}
//ORA Compare Memory with Accumulator, Zero PageX, 2 Bytes, 4 Cycles
case 0x15:
{
OrOperation(AddressingMode.ZeroPageX);
break;
}
//ORA Compare Memory with Accumulator, Absolute, 3 Bytes, 4 Cycles
case 0x0D:
{
OrOperation(AddressingMode.Absolute);
break;
}
//ORA Compare Memory with Accumulator, AbsolueteX 3 Bytes, 4+ Cycles
case 0x1D:
{
OrOperation(AddressingMode.AbsoluteX);
break;
}
//ORA Compare Memory with Accumulator, AbsoluteY, 3 Bytes, 4+ Cycles
case 0x19:
{
OrOperation(AddressingMode.AbsoluteY);
break;
}
//ORA Compare Memory with Accumulator, IndexedIndirect, 2 Bytes, 6 Cycles
case 0x01:
{
OrOperation(AddressingMode.IndirectX);
break;
}
//ORA Compare Memory with Accumulator, IndirectIndexed, 2 Bytes, 5 Cycles
case 0x11:
{
OrOperation(AddressingMode.IndirectY);
break;
}
#endregion
#region Clear Flag Operations
//CLC Clear Carry Flag, Implied, 1 Byte, 2 Cycles
case 0x18:
{
CarryFlag = false;
IncrementCycleCount();
break;
}
//CLD Clear Decimal Flag, Implied, 1 Byte, 2 Cycles
case 0xD8:
{
DecimalFlag = false;
IncrementCycleCount();
break;
}
//CLI Clear Interrupt Flag, Implied, 1 Byte, 2 Cycles
case 0x58:
{
DisableInterruptFlag = false;
IncrementCycleCount();
break;
}
//CLV Clear Overflow Flag, Implied, 1 Byte, 2 Cycles
case 0xB8:
{
OverflowFlag = false;
IncrementCycleCount();
break;
}
#endregion
#region Compare Operations
//CMP Compare Accumulator with Memory, Immediate, 2 Bytes, 2 Cycles
case 0xC9:
{
CompareOperation(AddressingMode.Immediate, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Zero Page, 2 Bytes, 3 Cycles
case 0xC5:
{
CompareOperation(AddressingMode.ZeroPage, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Zero Page x, 2 Bytes, 4 Cycles
case 0xD5:
{
CompareOperation(AddressingMode.ZeroPageX, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Absolute, 3 Bytes, 4 Cycles
case 0xCD:
{
CompareOperation(AddressingMode.Absolute, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Absolute X, 2 Bytes, 4 Cycles
case 0xDD:
{
CompareOperation(AddressingMode.AbsoluteX, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Absolute Y, 2 Bytes, 4 Cycles
case 0xD9:
{
CompareOperation(AddressingMode.AbsoluteY, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Indirect X, 2 Bytes, 6 Cycles
case 0xC1:
{
CompareOperation(AddressingMode.IndirectX, Accumulator);
break;
}
//CMP Compare Accumulator with Memory, Indirect Y, 2 Bytes, 5 Cycles
case 0xD1:
{
CompareOperation(AddressingMode.IndirectY, Accumulator);
break;
}
//CPX Compare Accumulator with X Register, Immediate, 2 Bytes, 2 Cycles
case 0xE0:
{
CompareOperation(AddressingMode.Immediate, XRegister);
break;
}
//CPX Compare Accumulator with X Register, Zero Page, 2 Bytes, 3 Cycles
case 0xE4:
{
CompareOperation(AddressingMode.ZeroPage, XRegister);
break;
}
//CPX Compare Accumulator with X Register, Absolute, 3 Bytes, 4 Cycles
case 0xEC:
{
CompareOperation(AddressingMode.Absolute, XRegister);
break;
}
//CPY Compare Accumulator with Y Register, Immediate, 2 Bytes, 2 Cycles
case 0xC0:
{
CompareOperation(AddressingMode.Immediate, YRegister);
break;
}
//CPY Compare Accumulator with Y Register, Zero Page, 2 Bytes, 3 Cycles
case 0xC4:
{
CompareOperation(AddressingMode.ZeroPage, YRegister);
break;
}
//CPY Compare Accumulator with Y Register, Absolute, 3 Bytes, 4 Cycles
case 0xCC:
{
CompareOperation(AddressingMode.Absolute, YRegister);
break;
}
#endregion
#region Increment/Decrement Operations
//DEC Decrement Memory by One, Zero Page, 2 Bytes, 5 Cycles
case 0xC6:
{
ChangeMemoryByOne(AddressingMode.ZeroPage, true);
break;
}
//DEC Decrement Memory by One, Zero Page X, 2 Bytes, 6 Cycles
case 0xD6:
{
ChangeMemoryByOne(AddressingMode.ZeroPageX, true);
break;
}
//DEC Decrement Memory by One, Absolute, 3 Bytes, 6 Cycles
case 0xCE:
{
ChangeMemoryByOne(AddressingMode.Absolute, true);
break;
}
//DEC Decrement Memory by One, Absolute X, 3 Bytes, 7 Cycles
case 0xDE:
{
ChangeMemoryByOne(AddressingMode.AbsoluteX, true);
IncrementCycleCount();
break;
}
//DEX Decrement X Register by One, Implied, 1 Bytes, 2 Cycles
case 0xCA:
{
ChangeRegisterByOne(true, true);
break;
}
//DEY Decrement Y Register by One, Implied, 1 Bytes, 2 Cycles
case 0x88:
{
ChangeRegisterByOne(false, true);
break;
}
//INC Increment Memory by One, Zero Page, 2 Bytes, 5 Cycles
case 0xE6:
{
ChangeMemoryByOne(AddressingMode.ZeroPage, false);
break;
}
//INC Increment Memory by One, Zero Page X, 2 Bytes, 6 Cycles
case 0xF6:
{
ChangeMemoryByOne(AddressingMode.ZeroPageX, false);
break;
}
//INC Increment Memory by One, Absolute, 3 Bytes, 6 Cycles
case 0xEE:
{
ChangeMemoryByOne(AddressingMode.Absolute, false);
break;
}
//INC Increment Memory by One, Absolute X, 3 Bytes, 7 Cycles
case 0xFE:
{
ChangeMemoryByOne(AddressingMode.AbsoluteX, false);
IncrementCycleCount();
break;
}
//INX Increment X Register by One, Implied, 1 Bytes, 2 Cycles
case 0xE8:
{
ChangeRegisterByOne(true, false);
break;
}
//INY Increment Y Register by One, Implied, 1 Bytes, 2 Cycles
case 0xC8:
{
ChangeRegisterByOne(false, false);
break;
}
#endregion
#region GOTO and GOSUB Operations
//JMP Jump to New Location, Absolute 3 Bytes, 3 Cycles
case 0x4C:
{
ProgramCounter = GetAddressByAddressingMode(AddressingMode.Absolute);
break;
}
//JMP Jump to New Location, Indirect 3 Bytes, 5 Cycles
case 0x6C:
{
ProgramCounter = GetAddressByAddressingMode(AddressingMode.Absolute);
if ((ProgramCounter & 0xFF) == 0xFF)
{
//Get the first half of the address
int address = ReadMemoryValue(ProgramCounter);
//Get the second half of the address, due to the issue with page boundary it reads from the wrong location!
address += 256 * ReadMemoryValue(ProgramCounter - 255);
ProgramCounter = address;
}
else
{
ProgramCounter = GetAddressByAddressingMode(AddressingMode.Absolute);
}
break;
}
//JSR Jump to SubRoutine, Absolute, 3 Bytes, 6 Cycles
case 0x20:
{
JumpToSubRoutineOperation();
break;
}
//BRK Simulate IRQ, Implied, 1 Byte, 7 Cycles
case 0x00:
{
BreakOperation(true, 0xFFFE);
break;
}
//RTI Return From Interrupt, Implied, 1 Byte, 6 Cycles
case 0x40:
{
ReturnFromInterruptOperation();
break;
}
//RTS Return From Subroutine, Implied, 1 Byte, 6 Cycles
case 0x60:
{
ReturnFromSubRoutineOperation();
break;
}
#endregion
#region Load Value From Memory Operations
//LDA Load Accumulator with Memory, Immediate, 2 Bytes, 2 Cycles
case 0xA9:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.Immediate));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Zero Page, 2 Bytes, 3 Cycles
case 0xA5:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.ZeroPage));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Zero Page X, 2 Bytes, 4 Cycles
case 0xB5:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.ZeroPageX));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Absolute, 3 Bytes, 4 Cycles
case 0xAD:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.Absolute));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Absolute X, 3 Bytes, 4+ Cycles
case 0xBD:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.AbsoluteX));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Absolute Y, 3 Bytes, 4+ Cycles
case 0xB9:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.AbsoluteY));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Index Indirect, 2 Bytes, 6 Cycles
case 0xA1:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.IndirectX));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDA Load Accumulator with Memory, Indirect Index, 2 Bytes, 5+ Cycles
case 0xB1:
{
Accumulator =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.IndirectY));
SetZeroFlag(Accumulator);
SetNegativeFlag(Accumulator);
break;
}
//LDX Load X with memory, Immediate, 2 Bytes, 2 Cycles
case 0xA2:
{
XRegister =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.Immediate));
SetZeroFlag(XRegister);
SetNegativeFlag(XRegister);
break;
}
//LDX Load X with memory, Zero Page, 2 Bytes, 3 Cycles
case 0xA6:
{
XRegister =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.ZeroPage));
SetZeroFlag(XRegister);
SetNegativeFlag(XRegister);
break;
}
//LDX Load X with memory, Zero Page Y, 2 Bytes, 4 Cycles
case 0xB6:
{
XRegister =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.ZeroPageY));
SetZeroFlag(XRegister);
SetNegativeFlag(XRegister);
break;
}
//LDX Load X with memory, Absolute, 3 Bytes, 4 Cycles
case 0xAE:
{
XRegister =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.Absolute));
SetZeroFlag(XRegister);
SetNegativeFlag(XRegister);
break;
}
//LDX Load X with memory, Absolute Y, 3 Bytes, 4+ Cycles
case 0xBE:
{
XRegister =ReadMemoryValue(GetAddressByAddressingMode(AddressingMode.AbsoluteY));