-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCpu.cs
1330 lines (1097 loc) · 65.1 KB
/
Cpu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Extensions.Byte;
using Extensions.Enums;
using Hardware.Memory;
using MicroProcessor.Cpu6502.Attributes;
using MicroProcessor.Cpu6502.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MicroProcessor.Cpu6502
{
public class Cpu
{
public event EventHandler<OpCode> OnStep;
public const ushort NMI_VECTOR_ADDRESS = 0xFFFA;
public const ushort RESET_VECTOR_ADDRESS = 0xFFFC;
public const ushort IRQ_VECTOR_ADDRESS = 0xFFFE;
public const ushort BRK_VECTOR_ADDRESS = 0xFFFE;
/// <summary>
/// Keeps track of the remaining cycles to finish the current instruction.
/// Gets decremented on every CPU cycle.
/// </summary>
private int _cyclesRemainingCurrentInstruction = 0;
private TaskCompletionSource<bool> _tcsPause = new TaskCompletionSource<bool>();
private bool _isPaused = false;
private bool _pausedWaiting = false;
private bool interruptWaiting = false;
private bool nonMaskableInterruptWaiting = false;
public List<OpCode> OpCodes { get; private set; }
public Dictionary<byte, OpCode> OpCodeCache { get; set; }
/// <summary>
/// The total number of executed instructions.
/// </summary>
public ulong TotalInstructions { get; private set; }
/// <summary>
/// The total number of executed illegal instructions.
/// </summary>
public ulong TotalIllegalInstructions { get; set; }
/// <summary>
/// The total number of times the CPU has been cycled.
/// </summary>
public ulong TotalCycles { get; set; }
/// <summary>
/// The memory available for the CPU.
/// The CPU uses the PC to fetch its instructions from the memory.
/// </summary>
public IMemory<byte> Memory { get; private set; }
/// <summary>
/// The Program Counter.
/// Points to the current instruction or its operands.
/// </summary>
public ushort PC;
/// <summary>
/// Stack Pointer. The SP starts at 0xFF and is decremented on push.
/// The stack stores its values in address range 0x01FF - 0x0100.
/// </summary>
public byte SP;
/// <summary>
/// Accumulator register.
/// </summary>
public byte AR;
/// <summary>
/// X register.
/// </summary>
public byte XR;
/// <summary>
/// Y register.
/// </summary>
public byte YR;
/// <summary>
/// Status Register. This register contains a set of flags.
/// </summary>
public StatusRegister SR = new StatusRegister()
{
Reserved = true
};
public OpCode OpCode { get; set; }
public ushort OpCodeAddress;
/// <summary>
/// The current Address after applying the current addressing mode.
/// </summary>
public ushort Address;
/// <summary>
/// The current Value to operate on based on the Address or the Accumulator,
/// depending on the current addressing mode.
/// </summary>
public byte Value
{
get
{
return OpCode.AddressingMode == AddressingMode.Accumulator ? AR : Memory.Read(Address);
}
set
{
if (OpCode.AddressingMode == AddressingMode.Accumulator)
{
AR = value;
}
else
{
Memory.Write(Address, value);
}
}
}
/// <summary>
/// The MOS6502 CPU.
/// </summary>
/// <param name="memory"></param>
public Cpu(IMemory<byte> memory)
{
Memory = memory;
var opCodesMethods = GetType()
.GetMethods()
.SelectMany(m => m.GetCustomAttributes(typeof(OpCodeDefinitionAttribute), true)
.Select(a => new
{
Attribute = a as OpCodeDefinitionAttribute,
Method = m
}));
var addressingMethods = GetType()
.GetMethods()
.SelectMany(m => m.GetCustomAttributes(typeof(AddressingModeAttribute), true)
.Select(a => new
{
Attribute = a as AddressingModeAttribute,
Method = m
})).ToDictionary(x => x.Attribute.AddressingMode, x => x);
OpCodes = opCodesMethods
.Select(x => OpCode.FromOpCodeDefinitionAttribute(() => x.Method.Invoke(this, null), () => addressingMethods[x.Attribute.AddressingMode].Method.Invoke(this, null), x.Attribute))
.ToList();
OpCodeCache = OpCodes.ToDictionary(x => x.Code, x => x);
Reset();
}
/// <summary>
/// Pauses the CPU cycling after finishing all cycles for the current instruction.
/// </summary>
/// <returns></returns>
public Task<bool> Pause()
{
if (_pausedWaiting) return Task.FromResult(false);
_pausedWaiting = true;
return _tcsPause.Task;
}
/// <summary>
/// Resumes the CPU cycling.
/// </summary>
public void Resume()
{
if (_pausedWaiting) return;
_tcsPause = new TaskCompletionSource<bool>();
_isPaused = false;
}
/// <summary>
/// Cycles the CPU and makes sure to take up as many cycles as
/// the current instruction is supposed to.
/// </summary>
public void Cycle()
{
if (_isPaused) return;
DoCycle();
}
private void DoCycle()
{
if (_cyclesRemainingCurrentInstruction == 0)
{
if (_pausedWaiting)
{
_pausedWaiting = false;
_isPaused = true;
_tcsPause.SetResult(true);
return;
}
if (nonMaskableInterruptWaiting)
{
DoNonMaskableInterrupt();
}
else if (interruptWaiting)
{
DoInterrupt();
}
else
{
Step();
}
}
else
{
_cyclesRemainingCurrentInstruction--;
}
}
/// <summary>
/// Steps the CPU by executing one instruction.
/// </summary>
/// <param name="ignoreCycles"></param>
public void Step(bool ignoreCycles = false)
{
OpCode = OpCodeCache[Memory.Read(PC)];
OpCode.OpCodeAddress = PC;
if (OnStep != null)
{
var o = OpCode.FromOpCodeDefinitionAttribute(null, null, OpCode);
o.OpCodeAddress = PC;
o.Operands.Clear();
for (int j = 1; j < o.Length; j++)
{
o.Operands.Add(Memory.Read(PC + j));
}
OnStep?.Invoke(this, o);
}
PC++;
if (OpCode.AddressingMode != AddressingMode.Implied && OpCode.AddressingMode != AddressingMode.Accumulator)
{
OpCode.GetAddress();
}
OpCode.Run();
TotalInstructions++;
if (OpCode.IsIllegal) TotalIllegalInstructions++;
// Count total cycles
// This doesn't account for extra cycles caused by memory operations crossing pages
TotalCycles += OpCode.Cycles;
// Keeps track of needed cycles to complete the current instruction
// ignoreCycles = true is used to step the CPU with a debugger
if (!ignoreCycles) _cyclesRemainingCurrentInstruction += OpCode.Cycles - 1;
}
public void Reset()
{
AR = XR = YR = 0x00;
// SP should be 0xFD, I used to initialize it directly, but `-= 3` on a `byte` becomes 0xFD after roll over
// I'm not sure if this is correct on multiple resets yet
//SP -= 3;
SP = 0xFD;
SR.IrqDisable = true;
PC = (ushort)((Memory.Read(RESET_VECTOR_ADDRESS + 1) << 8) | Memory.Read(RESET_VECTOR_ADDRESS));
// Reset takes 8 cycles
TotalCycles += 8;
}
/// <summary>
/// Triggers a non maskable interrupt (NMI) before executing the next instruction.
/// </summary>
public void NonMaskableInterrupt()
{
nonMaskableInterruptWaiting = true;
}
/// <summary>
/// Triggers an interrupt (IRQ) before executing the next instruction.
/// </summary>
public void Interrupt()
{
if (SR.IrqDisable) return;
interruptWaiting = true;
}
public void DoNonMaskableInterrupt()
{
// From https://youtu.be/8XmxKPJDGU0?t=3362
PushStack((byte)(PC >> 8));
PushStack((byte)(PC & 0x00FF));
PushStack((byte)((SR.Register | (byte)ProcessorStatusFlags.Reserved) & (byte)~ProcessorStatusFlags.BreakCommand));
SR.IrqDisable = true;
PC = (ushort)((Memory.Read(NMI_VECTOR_ADDRESS + 1) << 8) | Memory.Read(NMI_VECTOR_ADDRESS));
// NMI takes 8 cycles
TotalCycles += 8;
_cyclesRemainingCurrentInstruction += 8;
nonMaskableInterruptWaiting = false;
}
private void DoInterrupt()
{
// From https://youtu.be/8XmxKPJDGU0?t=3362
PushStack((byte)(PC >> 8));
PushStack((byte)(PC & 0x00FF));
PushStack((byte)((SR.Register | (byte)ProcessorStatusFlags.Reserved) & (byte)~ProcessorStatusFlags.BreakCommand));
SR.IrqDisable = true;
PC = (ushort)((Memory.Read(IRQ_VECTOR_ADDRESS + 1) << 8) | Memory.Read(IRQ_VECTOR_ADDRESS));
// IRQ takes 7 cycles
TotalCycles += 7;
_cyclesRemainingCurrentInstruction += 7;
interruptWaiting = false;
}
/// <summary>
/// Pushes a value to the Stack and decrements the Stack-pointer.
/// </summary>
/// <param name="value"></param>
public void PushStack(byte value)
{
Memory.Write(0x0100 + SP, value);
SP--;
}
/// <summary>
/// Pops a value off the Stack and increments the Stack-pointer.
/// </summary>
/// <returns></returns>
public byte PopStack()
{
SP++;
var b = Memory.Read(0x0100 + SP);
return b;
}
[AddressingMode(AddressingMode = AddressingMode.Absolute)]
public void Absolute()
{
var lowAddressByte = Memory.Read(PC);
PC++;
var highAddressByte = Memory.Read(PC);
PC++;
Address = (ushort)((highAddressByte << 8) | lowAddressByte);
}
[AddressingMode(AddressingMode = AddressingMode.AbsoluteX)]
public void AbsoluteX()
{
var lowAddressByte = Memory.Read(PC);
PC++;
var highAddressByte = Memory.Read(PC);
PC++;
Address = (ushort)((highAddressByte << 8) | lowAddressByte);
Address += XR;
}
[AddressingMode(AddressingMode = AddressingMode.AbsoluteY)]
public void AbsoluteY()
{
var lowAddressByte = Memory.Read(PC);
PC++;
var highAddressByte = Memory.Read(PC);
PC++;
Address = (ushort)((highAddressByte << 8) | lowAddressByte);
Address += YR;
}
[AddressingMode(AddressingMode = AddressingMode.Immediate)]
public void Immediate()
{
Address = PC;
PC++;
}
[AddressingMode(AddressingMode = AddressingMode.Indirect)]
public void Indirect()
{
var lowAddressPointer = Memory.Read(PC);
PC++;
var highAddressPointer = Memory.Read(PC);
PC++;
var addressPointer = (ushort)((highAddressPointer << 8) | lowAddressPointer);
if (lowAddressPointer == 0xFF)
{
// This handles a hardware bug in the 6502
Address = (ushort)(Memory.Read(addressPointer & 0xFF00) << 8 | Memory.Read(addressPointer));
}
else
{
Address = (ushort)(Memory.Read(addressPointer + 1) << 8 | Memory.Read(addressPointer));
}
}
[AddressingMode(AddressingMode = AddressingMode.XIndirect)]
public void XIndirect()
{
// The ushort type below is used intentionally to allow byte rollover check
ushort pageZeroAddress = Memory.Read(PC);
PC++;
var lowAddress = Memory.Read((pageZeroAddress + XR) & 0x00FF);
var highAddress = Memory.Read((pageZeroAddress + XR + 1) & 0x00FF);
Address = (ushort)((highAddress << 8) | lowAddress);
}
[AddressingMode(AddressingMode = AddressingMode.IndirectY)]
public void IndirectY()
{
// The ushort type below is used intentionally to allow byte rollover check
ushort pageZeroAddress = Memory.Read(PC);
PC++;
var lowAddress = Memory.Read(pageZeroAddress & 0x00FF);
var highAddress = Memory.Read((pageZeroAddress + 1) & 0x00FF);
Address = (ushort)((highAddress << 8) | lowAddress);
Address += YR;
}
[AddressingMode(AddressingMode = AddressingMode.Relative)]
public void Relative()
{
sbyte rel_addr = (sbyte)Memory.Read(PC);
PC++;
Address = (ushort)(PC + rel_addr);
}
[AddressingMode(AddressingMode = AddressingMode.Zeropage)]
public void Zeropage()
{
Address = Memory.Read(PC);
PC++;
}
[AddressingMode(AddressingMode = AddressingMode.ZeropageX)]
public void ZeropageX()
{
// The ushort type below is used intentionally to allow byte rollover check
ushort a = (ushort)(Memory.Read(PC) + XR);
Address = (a &= 0x00FF);
PC++;
}
[AddressingMode(AddressingMode = AddressingMode.ZeropageY)]
public void ZeropageY()
{
// The ushort type below is used intentionally to allow byte rollover check
ushort a = (ushort)(Memory.Read(PC) + YR);
Address = (a &= 0x00FF);
PC++;
}
// STORAGE
[OpCodeDefinition(Name = nameof(LDA), Code = 0xA9, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Load Accumulator")]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xA5, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Load Accumulator")]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xB5, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Load Accumulator")]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xAD, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Load Accumulator")]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xBD, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Load Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xB9, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Load Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xA1, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Load Accumulator")]
[OpCodeDefinition(Name = nameof(LDA), Code = 0xB1, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Load Accumulator", AddCycleIfBoundaryCrossed = true)]
public void LDA()
{
AR = Value;
SR.SetNegative(AR);
SR.SetZero(AR);
}
[OpCodeDefinition(Name = nameof(LDX), Code = 0xA2, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Load X-register")]
[OpCodeDefinition(Name = nameof(LDX), Code = 0xA6, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Load X-register")]
[OpCodeDefinition(Name = nameof(LDX), Code = 0xB6, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageY, Description = "Load X-register")]
[OpCodeDefinition(Name = nameof(LDX), Code = 0xAE, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Load X-register")]
[OpCodeDefinition(Name = nameof(LDX), Code = 0xBE, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Load X-register", AddCycleIfBoundaryCrossed = true)]
public void LDX()
{
XR = Value;
SR.SetNegative(XR);
SR.SetZero(XR);
}
[OpCodeDefinition(Name = nameof(LDY), Code = 0xA0, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Load Y-register")]
[OpCodeDefinition(Name = nameof(LDY), Code = 0xA4, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Load Y-register")]
[OpCodeDefinition(Name = nameof(LDY), Code = 0xB4, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Load Y-register")]
[OpCodeDefinition(Name = nameof(LDY), Code = 0xAC, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Load Y-register")]
[OpCodeDefinition(Name = nameof(LDY), Code = 0xBC, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Load Y-register", AddCycleIfBoundaryCrossed = true)]
public void LDY()
{
YR = Value;
SR.SetNegative(YR);
SR.SetZero(YR);
}
[OpCodeDefinition(Name = nameof(STA), Code = 0x85, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Store Accumulator")]
[OpCodeDefinition(Name = nameof(STA), Code = 0x95, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Store Accumulator")]
[OpCodeDefinition(Name = nameof(STA), Code = 0x8D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Store Accumulator")]
[OpCodeDefinition(Name = nameof(STA), Code = 0x9D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Store Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(STA), Code = 0x99, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Store Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(STA), Code = 0x81, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Store Accumulator")]
[OpCodeDefinition(Name = nameof(STA), Code = 0x91, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Store Accumulator", AddCycleIfBoundaryCrossed = true)]
public void STA()
{
Value = AR;
}
[OpCodeDefinition(Name = nameof(STX), Code = 0x86, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Store X-register")]
[OpCodeDefinition(Name = nameof(STX), Code = 0x96, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageY, Description = "Store X-register")]
[OpCodeDefinition(Name = nameof(STX), Code = 0x8E, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Store X-register")]
public void STX()
{
Value = XR;
}
[OpCodeDefinition(Name = nameof(STY), Code = 0x84, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Store Y-register")]
[OpCodeDefinition(Name = nameof(STY), Code = 0x94, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Store Y-register")]
[OpCodeDefinition(Name = nameof(STY), Code = 0x8C, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Store Y-register")]
public void STY()
{
Value = YR;
}
[OpCodeDefinition(Name = nameof(TAX), Code = 0xAA, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Transfer A-register to X-register")]
public void TAX()
{
SR.SetNegative(AR);
SR.SetZero(AR);
XR = AR;
}
[OpCodeDefinition(Name = nameof(TAY), Code = 0xA8, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Transfer A-register to Y-register")]
public void TAY()
{
SR.SetNegative(AR);
SR.SetZero(AR);
YR = AR;
}
[OpCodeDefinition(Name = nameof(TSX), Code = 0xBA, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Transfer Stack-pointer to X-register")]
public void TSX()
{
SR.SetNegative(SP);
SR.SetZero(SP);
XR = SP;
}
[OpCodeDefinition(Name = nameof(TXA), Code = 0x8A, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Transfer X-register to A-register")]
public void TXA()
{
SR.SetNegative(XR);
SR.SetZero(XR);
AR = XR;
}
[OpCodeDefinition(Name = nameof(TXS), Code = 0x9A, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Transfer X-register to Stack-pointer")]
public void TXS()
{
SP = XR;
}
[OpCodeDefinition(Name = nameof(TYA), Code = 0x98, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Transfer Y-register to A-register")]
public void TYA()
{
SR.SetNegative(YR);
SR.SetZero(YR);
AR = YR;
}
// BITWISE
[OpCodeDefinition(Name = nameof(AND), Code = 0x29, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Bitwise AND with Accumulator")]
[OpCodeDefinition(Name = nameof(AND), Code = 0x25, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Bitwise AND with Accumulator")]
[OpCodeDefinition(Name = nameof(AND), Code = 0x35, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Bitwise AND with Accumulator")]
[OpCodeDefinition(Name = nameof(AND), Code = 0x2D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Bitwise AND with Accumulator")]
[OpCodeDefinition(Name = nameof(AND), Code = 0x3D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Bitwise AND with Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(AND), Code = 0x39, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Bitwise AND with Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(AND), Code = 0x21, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Bitwise AND with Accumulator")]
[OpCodeDefinition(Name = nameof(AND), Code = 0x31, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Bitwise AND with Accumulator", AddCycleIfBoundaryCrossed = true)]
public void AND()
{
AR = (byte)(Value & AR);
SR.SetNegative(AR);
SR.SetZero(AR);
}
[OpCodeDefinition(Name = nameof(ASL), Code = 0x0A, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Accumulator, Description = "Arithmetic Shift Left")]
[OpCodeDefinition(Name = nameof(ASL), Code = 0x06, Length = 2, Cycles = 5, AddressingMode = AddressingMode.Zeropage, Description = "Arithmetic Shift Left")]
[OpCodeDefinition(Name = nameof(ASL), Code = 0x16, Length = 2, Cycles = 6, AddressingMode = AddressingMode.ZeropageX, Description = "Arithmetic Shift Left")]
[OpCodeDefinition(Name = nameof(ASL), Code = 0x0E, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Arithmetic Shift Left")]
[OpCodeDefinition(Name = nameof(ASL), Code = 0x1E, Length = 3, Cycles = 7, AddressingMode = AddressingMode.AbsoluteX, Description = "Arithmetic Shift Left")]
public void ASL()
{
SR.Carry = Value.IsBitSet(BitFlag.BIT_7);
Value <<= 1;
SR.SetNegative(Value);
SR.SetZero(Value);
}
[OpCodeDefinition(Name = nameof(BIT), Code = 0x24, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Bit Test")]
[OpCodeDefinition(Name = nameof(BIT), Code = 0x2C, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Bit Test")]
public void BIT()
{
SR.SetZero((byte)(Value & AR));
SR.Negative = Value.IsBitSet((BitFlag)ProcessorStatusFlags.Negative);
SR.Overflow = Value.IsBitSet((BitFlag)ProcessorStatusFlags.Overflow);
}
[OpCodeDefinition(Name = nameof(EOR), Code = 0x49, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Bitwise Exclusive OR")]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x45, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Bitwise Exclusive OR")]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x55, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Bitwise Exclusive OR")]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x4D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Bitwise Exclusive OR")]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x5D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Bitwise Exclusive OR", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x59, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Bitwise Exclusive OR", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x41, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Bitwise Exclusive OR")]
[OpCodeDefinition(Name = nameof(EOR), Code = 0x51, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Bitwise Exclusive OR", AddCycleIfBoundaryCrossed = true)]
public void EOR()
{
AR = (byte)(Value ^ AR);
SR.SetNegative(AR);
SR.SetZero(AR);
}
[OpCodeDefinition(Name = nameof(LSR), Code = 0x4A, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Accumulator, Description = "Logical Shift Right")]
[OpCodeDefinition(Name = nameof(LSR), Code = 0x46, Length = 2, Cycles = 5, AddressingMode = AddressingMode.Zeropage, Description = "Logical Shift Right")]
[OpCodeDefinition(Name = nameof(LSR), Code = 0x56, Length = 2, Cycles = 6, AddressingMode = AddressingMode.ZeropageX, Description = "Logical Shift Right")]
[OpCodeDefinition(Name = nameof(LSR), Code = 0x4E, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Logical Shift Right")]
[OpCodeDefinition(Name = nameof(LSR), Code = 0x5E, Length = 3, Cycles = 7, AddressingMode = AddressingMode.AbsoluteX, Description = "Logical Shift Right")]
public void LSR()
{
SR.Carry = Value.IsBitSet(BitFlag.BIT_0);
Value >>= 1;
SR.SetNegative(Value);
SR.SetZero(Value);
}
[OpCodeDefinition(Name = nameof(ORA), Code = 0x09, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Bitwise OR With Accumulator")]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x05, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Bitwise OR With Accumulator")]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x15, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Bitwise OR With Accumulator")]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x0D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Bitwise OR With Accumulator")]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x1D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Bitwise OR With Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x19, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Bitwise OR With Accumulator", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x01, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Bitwise OR With Accumulator")]
[OpCodeDefinition(Name = nameof(ORA), Code = 0x11, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Bitwise OR With Accumulator", AddCycleIfBoundaryCrossed = true)]
public void ORA()
{
AR = (byte)(Value | AR);
SR.SetNegative(AR);
SR.SetZero(AR);
}
[OpCodeDefinition(Name = nameof(ROL), Code = 0x2A, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Accumulator, Description = "Rotate Left")]
[OpCodeDefinition(Name = nameof(ROL), Code = 0x26, Length = 2, Cycles = 5, AddressingMode = AddressingMode.Zeropage, Description = "Rotate Left")]
[OpCodeDefinition(Name = nameof(ROL), Code = 0x36, Length = 2, Cycles = 6, AddressingMode = AddressingMode.ZeropageX, Description = "Rotate Left")]
[OpCodeDefinition(Name = nameof(ROL), Code = 0x2E, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Rotate Left")]
[OpCodeDefinition(Name = nameof(ROL), Code = 0x3E, Length = 3, Cycles = 7, AddressingMode = AddressingMode.AbsoluteX, Description = "Rotate Left")]
public void ROL()
{
var c = SR.Carry;
SR.Carry = Value.IsBitSet(BitFlag.BIT_7);
Value <<= 1;
Value = Value.SetBit(BitFlag.BIT_0, c);
SR.SetNegative(Value);
SR.SetZero(Value);
}
[OpCodeDefinition(Name = nameof(ROR), Code = 0x6A, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Accumulator, Description = "Rotate Right")]
[OpCodeDefinition(Name = nameof(ROR), Code = 0x66, Length = 2, Cycles = 5, AddressingMode = AddressingMode.Zeropage, Description = "Rotate Right")]
[OpCodeDefinition(Name = nameof(ROR), Code = 0x76, Length = 2, Cycles = 6, AddressingMode = AddressingMode.ZeropageX, Description = "Rotate Right")]
[OpCodeDefinition(Name = nameof(ROR), Code = 0x6E, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Rotate Right")]
[OpCodeDefinition(Name = nameof(ROR), Code = 0x7E, Length = 3, Cycles = 7, AddressingMode = AddressingMode.AbsoluteX, Description = "Rotate Right")]
public void ROR()
{
var c = SR.Carry;
SR.Carry = Value.IsBitSet(BitFlag.BIT_0);
Value >>= 1;
Value = Value.SetBit(BitFlag.BIT_7, c);
SR.SetNegative(Value);
SR.SetZero(Value);
}
// MATH
[OpCodeDefinition(Name = nameof(ADC), Code = 0x69, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Add With Carry")]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x65, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Add With Carry")]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x75, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Add With Carry")]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x6D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Add With Carry")]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x7D, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Add With Carry", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x79, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Add With Carry", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x61, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Add With Carry")]
[OpCodeDefinition(Name = nameof(ADC), Code = 0x71, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Add With Carry", AddCycleIfBoundaryCrossed = true)]
public void ADC()
{
var temp = AR + Value + (SR.Carry ? 1 : 0);
SR.Carry = temp > 255;
SR.Overflow = ((~(AR ^ Value) & (AR ^ temp)) & 0b10000000) == 0b10000000;
SR.SetZero((byte)temp);
SR.SetNegative((byte)temp);
AR = (byte)temp;
}
[OpCodeDefinition(Name = nameof(SBC), Code = 0xE9, Length = 2, Cycles = 2, AddressingMode = AddressingMode.Immediate, Description = "Subtract With Carry")]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xE5, Length = 2, Cycles = 3, AddressingMode = AddressingMode.Zeropage, Description = "Subtract With Carry")]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xF5, Length = 2, Cycles = 4, AddressingMode = AddressingMode.ZeropageX, Description = "Subtract With Carry")]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xED, Length = 3, Cycles = 4, AddressingMode = AddressingMode.Absolute, Description = "Subtract With Carry")]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xFD, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteX, Description = "Subtract With Carry", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xF9, Length = 3, Cycles = 4, AddressingMode = AddressingMode.AbsoluteY, Description = "Subtract With Carry", AddCycleIfBoundaryCrossed = true)]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xE1, Length = 2, Cycles = 6, AddressingMode = AddressingMode.XIndirect, Description = "Subtract With Carry")]
[OpCodeDefinition(Name = nameof(SBC), Code = 0xF1, Length = 2, Cycles = 5, AddressingMode = AddressingMode.IndirectY, Description = "Subtract With Carry", AddCycleIfBoundaryCrossed = true)]
public void SBC()
{
var v = Value ^ 0x00FF;
var temp = AR + v + (SR.Carry ? 1 : 0);
SR.Carry = temp > 255;
SR.Overflow = ((~(AR ^ v) & (AR ^ temp)) & 0b10000000) == 0b10000000;
SR.SetZero((byte)temp);
SR.SetNegative((byte)temp);
AR = (byte)temp;
}
[OpCodeDefinition(Name = nameof(DEC), Code = 0xC6, Length = 2, Cycles = 5, AddressingMode = AddressingMode.Zeropage, Description = "Decrement Memory")]
[OpCodeDefinition(Name = nameof(DEC), Code = 0xD6, Length = 2, Cycles = 6, AddressingMode = AddressingMode.ZeropageX, Description = "Decrement Memory")]
[OpCodeDefinition(Name = nameof(DEC), Code = 0xCE, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Decrement Memory")]
[OpCodeDefinition(Name = nameof(DEC), Code = 0xDE, Length = 3, Cycles = 7, AddressingMode = AddressingMode.AbsoluteX, Description = "Decrement Memory")]
public void DEC()
{
Value--;
SR.SetNegative(Value);
SR.SetZero(Value);
}
[OpCodeDefinition(Name = nameof(DEX), Code = 0xCA, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Decrement X-register")]
public void DEX()
{
XR--;
SR.SetNegative(XR);
SR.SetZero(XR);
}
[OpCodeDefinition(Name = nameof(DEY), Code = 0x88, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Decrement Y-register")]
public void DEY()
{
YR--;
SR.SetNegative(YR);
SR.SetZero(YR);
}
[OpCodeDefinition(Name = nameof(INC), Code = 0xE6, Length = 2, Cycles = 5, AddressingMode = AddressingMode.Zeropage, Description = "Increment Memory")]
[OpCodeDefinition(Name = nameof(INC), Code = 0xF6, Length = 2, Cycles = 6, AddressingMode = AddressingMode.ZeropageX, Description = "Increment Memory")]
[OpCodeDefinition(Name = nameof(INC), Code = 0xEE, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Increment Memory")]
[OpCodeDefinition(Name = nameof(INC), Code = 0xFE, Length = 3, Cycles = 7, AddressingMode = AddressingMode.AbsoluteX, Description = "Increment Memory")]
public void INC()
{
Value++;
SR.SetNegative(Value);
SR.SetZero(Value);
}
[OpCodeDefinition(Name = nameof(INX), Code = 0xE8, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Increment X-register")]
public void INX()
{
XR++;
SR.SetNegative(XR);
SR.SetZero(XR);
}
[OpCodeDefinition(Name = nameof(INY), Code = 0xC8, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Increment Y-register")]
public void INY()
{
YR++;
SR.SetNegative(YR);
SR.SetZero(YR);
}
// REGISTERS
[OpCodeDefinition(Name = nameof(CLC), Code = 0x18, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Clear Carry")]
public void CLC()
{
SR.Carry = false;
}
[OpCodeDefinition(Name = nameof(SEC), Code = 0x38, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Set Carry")]
public void SEC()
{
SR.Carry = true;
}
[OpCodeDefinition(Name = nameof(CLI), Code = 0x58, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Clear Interrupt")]
public void CLI()
{
SR.IrqDisable = false;
}
[OpCodeDefinition(Name = nameof(SEI), Code = 0x78, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Set Interrupt")]
public void SEI()
{
SR.IrqDisable = true;
}
[OpCodeDefinition(Name = nameof(CLV), Code = 0xB8, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Clear Overflow")]
public void CLV()
{
SR.Overflow = false;
}
[OpCodeDefinition(Name = nameof(CLD), Code = 0xD8, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Clear Decimal")]
public void CLD()
{
SR.DecimalMode = false;
}
[OpCodeDefinition(Name = nameof(SED), Code = 0xF8, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "Set Decimal")]
public void SED()
{
SR.DecimalMode = true;
}
// SYSTEM
[OpCodeDefinition(Name = nameof(BRK), Code = 0x00, Length = 1, Cycles = 7, AddressingMode = AddressingMode.Implied, Description = "Break")]
public void BRK()
{
PushStack((byte)((PC) >> 8)); // MSB
PushStack((byte)((PC) & 0xFF)); // LSB
PushStack((byte)(SR.Register | (byte)ProcessorStatusFlags.BreakCommand));
SR.IrqDisable = true;
PC = (ushort)((Memory.Read(BRK_VECTOR_ADDRESS + 1) << 8) | Memory.Read(BRK_VECTOR_ADDRESS));
}
[OpCodeDefinition(Name = nameof(NOP), Code = 0xEA, Length = 1, Cycles = 2, AddressingMode = AddressingMode.Implied, Description = "No Operation")]
public void NOP()
{
}
// JUMP
[OpCodeDefinition(Name = nameof(JMP), Code = 0x4C, Length = 3, Cycles = 3, AddressingMode = AddressingMode.Absolute, Description = "Jump")]
[OpCodeDefinition(Name = nameof(JMP), Code = 0x6C, Length = 3, Cycles = 5, AddressingMode = AddressingMode.Indirect, Description = "Jump")]
public void JMP()
{
PC = Address;
}
[OpCodeDefinition(Name = nameof(JSR), Code = 0x20, Length = 3, Cycles = 6, AddressingMode = AddressingMode.Absolute, Description = "Jump to Sub Routine")]
public void JSR()
{
PC--;
PushStack((byte)((PC) >> 8)); // MSB
PushStack((byte)((PC) & 0xFF)); // LSB
PC = Address;
}
[OpCodeDefinition(Name = nameof(RTI), Code = 0x40, Length = 1, Cycles = 6, AddressingMode = AddressingMode.Implied, Description = "Return from Interrupt")]
public void RTI()
{
var poppedSR = new StatusRegister() { Register = PopStack() };
SR.Carry = poppedSR.Carry;
SR.Zero = poppedSR.Zero;
SR.IrqDisable = poppedSR.IrqDisable;
SR.DecimalMode = poppedSR.DecimalMode;
// This instruction ignores DecimalMode and Break
SR.Overflow = poppedSR.Overflow;
SR.Negative = poppedSR.Negative;
byte lowByte = PopStack();
byte highByte = PopStack();
PC = (ushort)(lowByte | (highByte << 8));
}
[OpCodeDefinition(Name = nameof(RTS), Code = 0x60, Length = 1, Cycles = 6, AddressingMode = AddressingMode.Implied, Description = "Return from Sub Routine")]
public void RTS()
{
byte lowByte = PopStack();
byte highByte = PopStack();
PC = (ushort)(lowByte | (highByte << 8));
PC++;
}
// STACK
[OpCodeDefinition(Name = nameof(PHA), Code = 0x48, Length = 1, Cycles = 3, AddressingMode = AddressingMode.Implied, Description = "Push Accumulator")]
public void PHA()
{
PushStack(AR);
}
[OpCodeDefinition(Name = nameof(PLA), Code = 0x68, Length = 1, Cycles = 4, AddressingMode = AddressingMode.Implied, Description = "Pull Accumulator")]
public void PLA()
{
AR = PopStack();
SR.SetNegative(AR);
SR.SetZero(AR);
}
[OpCodeDefinition(Name = nameof(PHP), Code = 0x08, Length = 1, Cycles = 3, AddressingMode = AddressingMode.Implied, Description = "Push Processor Status")]
public void PHP()
{
PushStack((byte)(SR.Register | (byte)ProcessorStatusFlags.BreakCommand));
}
[OpCodeDefinition(Name = nameof(PLP), Code = 0x28, Length = 1, Cycles = 4, AddressingMode = AddressingMode.Implied, Description = "Pull Processor Status")]
public void PLP()
{
var poppedSR = new StatusRegister() { Register = PopStack() };
SR.Carry = poppedSR.Carry;
SR.Zero = poppedSR.Zero;
SR.IrqDisable = poppedSR.IrqDisable;
SR.DecimalMode = poppedSR.DecimalMode;
// This instruction ignores DecimalMode and Break
SR.Overflow = poppedSR.Overflow;
SR.Negative = poppedSR.Negative;
}
// BRANCHING