forked from macifom/macifom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NES6502Interpreter.m
1546 lines (1297 loc) · 83.2 KB
/
NES6502Interpreter.m
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
/* NES6502Interpreter.m
*
* Copyright (c) 2010 Auston Stewart
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "NES6502Interpreter.h"
#import "NESCartridge.h"
#import "NESPPUEmulator.h"
#import "NESAPUEmulator.h"
#define NO_PENDING_IRQ 0xffffffff
static void _ADC(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t oldAccumulator = cpuRegisters->accumulator;
uint16_t result = (uint16_t)oldAccumulator + operand + cpuRegisters->statusCarry;
cpuRegisters->accumulator = (uint8_t)result;
cpuRegisters->statusCarry = result >> 8;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusOverflow = ((oldAccumulator ^ cpuRegisters->accumulator) & (operand ^ cpuRegisters->accumulator)) / 128;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static void _AND(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->accumulator &= operand;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static void _ASL(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->statusCarry = cpuRegisters->accumulator >> 7;
cpuRegisters->accumulator <<= 1;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static uint8_t _ASL_RMW(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->statusCarry = operand >> 7;
operand <<= 1;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
return operand;
}
static void _BIT(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusOverflow = ((operand / 64) & 1);
cpuRegisters->statusZero = !(cpuRegisters->accumulator & operand);
}
static void _CMP(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t result = cpuRegisters->accumulator - operand;
cpuRegisters->statusCarry = (operand <= cpuRegisters->accumulator); // Should be an unsigned comparison
cpuRegisters->statusNegative = result >> 7;
cpuRegisters->statusZero = !result;
}
static void _CPX(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t result = cpuRegisters->indexRegisterX - operand;
cpuRegisters->statusCarry = (operand <= cpuRegisters->indexRegisterX); // Should be an unsigned comparison
cpuRegisters->statusNegative = result >> 7;
cpuRegisters->statusZero = !result;
}
static void _CPY(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t result = cpuRegisters->indexRegisterY - operand;
cpuRegisters->statusCarry = (operand <= cpuRegisters->indexRegisterY); // Should be an unsigned comparison
cpuRegisters->statusNegative = result >> 7;
cpuRegisters->statusZero = !result;
}
static uint8_t _DEC(CPURegisters *cpuRegisters, uint8_t operand) {
operand--;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
return operand;
}
static uint8_t _INC(CPURegisters *cpuRegisters, uint8_t operand) {
operand++;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
return operand;
}
static void _EOR(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->accumulator ^= operand;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static void _LDA(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->accumulator = operand;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
}
static void _LDX(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->indexRegisterX = operand;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
}
static void _LDY(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->indexRegisterY = operand;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
}
static void _LSR(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->statusCarry = (cpuRegisters->accumulator & 1);
cpuRegisters->accumulator >>= 1;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static uint8_t _LSR_RMW(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->statusCarry = (operand & 1);
operand >>= 1;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
return operand;
}
static void _ORA(CPURegisters *cpuRegisters, uint8_t operand) {
cpuRegisters->accumulator |= operand;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static void _ROL(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t oldCarry = cpuRegisters->statusCarry;
cpuRegisters->statusCarry = cpuRegisters->accumulator >> 7;
cpuRegisters->accumulator <<= 1;
cpuRegisters->accumulator |= oldCarry;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static uint8_t _ROL_RMW(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t oldCarry = cpuRegisters->statusCarry;
cpuRegisters->statusCarry = operand >> 7;
operand <<= 1;
operand |= oldCarry;
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
return operand;
}
static void _ROR(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t oldCarry = cpuRegisters->statusCarry;
cpuRegisters->statusCarry = (cpuRegisters->accumulator & 1);
cpuRegisters->accumulator >>= 1;
cpuRegisters->accumulator |= (oldCarry << 7);
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static uint8_t _ROR_RMW(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t oldCarry = cpuRegisters->statusCarry;
cpuRegisters->statusCarry = (operand & 1);
operand >>= 1;
operand |= (oldCarry << 7);
cpuRegisters->statusNegative = operand >> 7;
cpuRegisters->statusZero = !operand;
return operand;
}
static void _SBC(CPURegisters *cpuRegisters, uint8_t operand) {
uint8_t oldAccumulator = cpuRegisters->accumulator;
operand = ~operand; // invert operand bits, used to do this below but it must happen BEFORE promotion to uin16_t
uint16_t result = (uint16_t)oldAccumulator + operand + cpuRegisters->statusCarry;
cpuRegisters->accumulator = (uint8_t)result;
cpuRegisters->statusCarry = result >> 8;
cpuRegisters->statusNegative = cpuRegisters->accumulator >> 7;
cpuRegisters->statusOverflow = ((oldAccumulator ^ cpuRegisters->accumulator) & (operand ^ cpuRegisters->accumulator)) / 128;
cpuRegisters->statusZero = !cpuRegisters->accumulator;
}
static uint8_t _GetAccumulator(CPURegisters *cpuRegisters, uint8_t operand) {
return cpuRegisters->accumulator;
}
static uint8_t _GetIndexRegisterX(CPURegisters *cpuRegisters, uint8_t operand) {
return cpuRegisters->indexRegisterX;
}
static uint8_t _GetIndexRegisterY(CPURegisters *cpuRegisters, uint8_t operand) {
return cpuRegisters->indexRegisterY;
}
@implementation NES6502Interpreter
@synthesize encounteredBreakpoint = _encounteredBreakpoint;
- (void)_clearRegisters
{
_cpuRegisters->accumulator = 0;
_cpuRegisters->indexRegisterX = 0;
_cpuRegisters->indexRegisterY = 0;
_cpuRegisters->programCounter = 0;
_cpuRegisters->stackPointer = 0xFF; // FIXME: http://nesdevwiki.org/wiki/Power-Up_State says this should be $FD
_cpuRegisters->statusCarry = 0;
_cpuRegisters->statusZero = 0;
_cpuRegisters->statusIRQDisable = 1; // Tepples indicates that IRQs are disabled on boot-up, as though SEI was invoked
_cpuRegisters->statusDecimal = 0;
_cpuRegisters->statusBreak = 0; // FIXME: http://nesdevwiki.org/wiki/Power-Up_State says this should be on
_cpuRegisters->statusOverflow = 0;
_cpuRegisters->statusNegative = 0;
}
- (void)_clearCPUMemory
{
int counter;
// Taken from http://nesdevwiki.org/wiki/Power-Up_State
for (counter = 0; counter < 2048; counter++) _zeroPage[counter] = 0xFF;
_zeroPage[0x0008] = 0xF7;
_zeroPage[0x0009] = 0xEF;
_zeroPage[0x000a] = 0xDF;
_zeroPage[0x000f] = 0xBF;
}
- (void)_clearStatus
{
_cpuRegisters->cycle = 0;
_nextIRQ = NO_PENDING_IRQ;
breakPoint = 0;
_encounteredUnsupportedOpcode = NO;
_encounteredBreakpoint = NO;
_controllers[0] = 0x0001FF00; // Should indicate one controller on $4016 per nestech.txt
_controllers[1] = 0x0002FF00; // Should indicate one controller on $4017 per nestech.txt
_controller0ReadIndex = 0;
_controller1ReadIndex = 0;
}
- (uint8_t)readByteFromCPUAddressSpace:(uint16_t)address
{
if (address >= 0x8000) return _prgromBankPointers[(address & 0x7FFF) / PRGROM_BANK_SIZE][address & (PRGROM_BANK_SIZE - 1)];
else if (address < 0x2000) return _zeroPage[address & 0x07FF];
else if (address >= 0x6000) return _wram[address & (WRAM_SIZE - 1)];
else if (address >= 0x4020) return 0;
else if (address >= 0x4000) {
switch (address) {
case 0x4015:
return [apu readAPUStatusOnCycle:_cpuRegisters->cycle];
break;
case 0x4016:
return ((_controllers[0] >> _controller0ReadIndex++) & 0x1);
break;
case 0x4017:
return ((_controllers[1] >> _controller1ReadIndex++) & 0x1);
break;
default:
break;
}
}
else return [ppu readByteFromCPUAddress:address onCycle:_cpuRegisters->cycle];
return 0;
}
- (uint16_t)readAddressFromCPUAddressSpace:(uint16_t)address
{
if (address >= 0x8000) return _prgromBankPointers[(address & 0x7FFF) / PRGROM_BANK_SIZE][address & (PRGROM_BANK_SIZE - 1)] + ((uint16_t)_prgromBankPointers[((address + 1) & 0x7FFF) / PRGROM_BANK_SIZE][(address + 1) & (PRGROM_BANK_SIZE - 1)] * 256);
return _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),address) + ((uint16_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),address + 1) * 256);
}
- (void)writeByte:(uint8_t)byte toCPUAddress:(uint16_t)address
{
uint8_t *DMAorigin;
if (address < 0x2000) {
_zeroPage[address & 0x07FF] = byte;
}
else if (address < 0x4000) [ppu writeByte:byte toPPUFromCPUAddress:address onCycle:_cpuRegisters->cycle];
else if (address < 0x4020) {
if (address == 0x4014) {
// Resolve base address for DMA origin
if (byte < 32) {
// NSLog(@"Initiating DMA SPRRAM transfer from CPU RAM: 0x%4.4x", (0x100 * byte));
DMAorigin = _zeroPage + (0x100 * (byte & 0x7));
}
else if (byte >= 128) {
DMAorigin = _prgromBankPointers[(0x100 * (byte & 0x7F)) / PRGROM_BANK_SIZE] + ((0x100 * (byte & 0x7F)) & (PRGROM_BANK_SIZE - 1));
}
else if (byte >= 96) {
// NSLog(@"Initiating DMA SPRRAM transfer from WRAM: 0x%4.4x", (0x100 * byte));
DMAorigin = _wram + (0x100 * (byte & 0x1F));
}
else {
// NSLog(@"Initiating DMA SPRRAM transfer from place I don't have a pointer to: 0x%4.4x", (0x100 * byte));
DMAorigin = NULL; // Crap! Don't know what to do about DMA transfers from registers
}
[ppu DMAtransferToSPRRAM:DMAorigin onCycle:_cpuRegisters->cycle];
_cpuRegisters->cycle += 512; // DMA transfer to SPRRAM requires 512 CPU cycles
}
else if (address == 0x4016) {
_controller0ReadIndex = 0; // FIXME: Really, I should be resetting this when 1 then 0 is written
_controller1ReadIndex = 0;
}
else {
// Write to APU Register (0x4000-0x4017, except 0x4014 and 0x4016)
[apu writeByte:byte toAPUFromCPUAddress:address onCycle:_cpuRegisters->cycle];
}
}
else if (address < 0x6000) return;
else if (address < 0x8000) {
[cartridge writeByte:byte toWRAMwithCPUAddress:address onCycle:_cpuRegisters->cycle];
}
else {
[cartridge writeByte:byte toPRGROMwithCPUAddress:address onCycle:_cpuRegisters->cycle];
}
}
- (void)_unsupportedOpcode:(uint8_t)opcode
{
NSLog(@"Encountered unsupported opcode %2.2x at program counter %4.4x on cycle %d",opcode,_cpuRegisters->programCounter,_cpuRegisters->cycle);
_encounteredUnsupportedOpcode = YES;
}
- (void)_performImpliedOperation:(uint8_t)opcode
{
_standardOperations[opcode](_cpuRegisters,opcode);
_cpuRegisters->cycle += 2;
}
- (void)_performOperationAsImmediate:(uint8_t)opcode
{
_standardOperations[opcode](_cpuRegisters,_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++));
_cpuRegisters->cycle += 2;
}
- (void)_performOperationAsAbsolute:(uint8_t)opcode
{
uint8_t operand;
// FIXME: I'm not sure if I split the cycles correctly here, need to read the 6502 reference
_cpuRegisters->cycle += 3;
operand = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),[self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter]);
_cpuRegisters->cycle += 1;
_standardOperations[opcode](_cpuRegisters,operand);
_cpuRegisters->programCounter += 2;
}
// FIXME: Should accurately model all reads
- (void)_performOperationAsAbsoluteX:(uint8_t)opcode
{
uint16_t absoluteAddress = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
uint16_t indexedAddress = absoluteAddress + _cpuRegisters->indexRegisterX;
_standardOperations[opcode](_cpuRegisters,_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),indexedAddress));
_cpuRegisters->programCounter += 2;
_cpuRegisters->cycle += 4 + ((absoluteAddress >> 8) != (indexedAddress >> 8) ? 1 : 0);
}
// FIXME: Should accurately model all reads
- (void)_performOperationAsAbsoluteY:(uint8_t)opcode
{
uint16_t absoluteAddress = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
uint16_t indexedAddress = absoluteAddress + _cpuRegisters->indexRegisterY;
_standardOperations[opcode](_cpuRegisters,_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),indexedAddress));
_cpuRegisters->programCounter += 2;
_cpuRegisters->cycle += 4 + ((absoluteAddress >> 8) != (indexedAddress >> 8) ? 1 : 0);
}
- (void)_performOperationAsZeroPage:(uint8_t)opcode
{
_standardOperations[opcode](_cpuRegisters,_zeroPage[_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++)]);
_cpuRegisters->cycle += 3;
}
- (void)_performOperationAsZeroPageX:(uint8_t)opcode
{
_standardOperations[opcode](_cpuRegisters,_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterX)]); // hopefully this add will be done as uint8_t, causing it to wrap
_cpuRegisters->cycle += 4;
}
- (void)_performOperationAsZeroPageY:(uint8_t)opcode
{
_standardOperations[opcode](_cpuRegisters,_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterY)]); // hopefully this add will be done as uint8_t, causing it to wrap
_cpuRegisters->cycle += 4;
}
/* _performOperationAsIndirectX
*
* Description: Performs an operation obtaining the operand through an indexed indirect fetch relative to X.
*
* First ADL is fetched using the byte following the opcode, offset by IndexRegisterX, to index page zero.
* ADH is fetched using the same address plus one. This should wrap due to overflow if reading beyond page zero.
*/
- (void)_performOperationAsIndirectX:(uint8_t)opcode
{
uint16_t effectiveAddress = _zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + _cpuRegisters->indexRegisterX)]; // Fetch ADL
effectiveAddress += (_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterX + 1)] << 8); // Fetch ADH
uint8_t operand = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),effectiveAddress);
_standardOperations[opcode](_cpuRegisters,operand);
_cpuRegisters->cycle += 6;
}
/* _performOperationAsIndirectY
*
* Description: Performs an operation obtaining the operand through an indirect indexed fetch relative to Y.
*
* Note: Page crossing will result in a cycle beind added.
*/
- (void)_performOperationAsIndirectY:(uint8_t)opcode
{
uint16_t effectiveAddress = 0;
uint16_t absoluteAddress = _zeroPage[_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter)]; // Fetch BAL
absoluteAddress += (_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + 1)] << 8); // Fetch BAH
effectiveAddress = absoluteAddress + _cpuRegisters->indexRegisterY; // Add IndexRegisterY to BAH,BAL, potential page crossing
uint8_t operand = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),effectiveAddress);
_standardOperations[opcode](_cpuRegisters,operand);
_cpuRegisters->cycle += (5 + ((absoluteAddress >> 8) != (effectiveAddress >> 8) ? 1 : 0)); // Check for page crossing
}
- (void)_performWriteOperationWithAbsolute:(uint8_t)opcode
{
uint16_t address = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
_cpuRegisters->programCounter += 2;
_cpuRegisters->cycle += 4;
[self writeByte:_writeOperations[opcode](_cpuRegisters,opcode) toCPUAddress:address];
}
- (void)_performWriteOperationWithAbsoluteX:(uint8_t)opcode
{
uint16_t absoluteAddress = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
uint16_t indexedAddress = absoluteAddress + _cpuRegisters->indexRegisterX;
_cpuRegisters->cycle += 5;
_cpuRegisters->programCounter += 2;
[self writeByte:_writeOperations[opcode](_cpuRegisters,opcode) toCPUAddress:indexedAddress];
}
- (void)_performWriteOperationWithAbsoluteY:(uint8_t)opcode
{
uint16_t absoluteAddress = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
uint16_t indexedAddress = absoluteAddress + _cpuRegisters->indexRegisterY;
_cpuRegisters->cycle += 5;
_cpuRegisters->programCounter += 2;
[self writeByte:_writeOperations[opcode](_cpuRegisters,opcode) toCPUAddress:indexedAddress];
}
- (void)_performWriteOperationWithZeroPage:(uint8_t)opcode
{
_zeroPage[_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++)] = _writeOperations[opcode](_cpuRegisters,opcode);
_cpuRegisters->cycle += 3;
}
- (void)_performWriteOperationWithZeroPageX:(uint8_t)opcode
{
_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterX)] = _writeOperations[opcode](_cpuRegisters,opcode); // hopefully this add will be done as uint8_t, causing it to wrap
_cpuRegisters->cycle += 4;
}
- (void)_performWriteOperationWithZeroPageY:(uint8_t)opcode
{
_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterY)] = _writeOperations[opcode](_cpuRegisters,opcode); // hopefully this add will be done as uint8_t, causing it to wrap
_cpuRegisters->cycle += 4;
}
/* _performOperationAsIndirectX
*
* Description: Performs an operation obtaining the operand through an indexed indirect fetch relative to X.
*
* Note: This does a separate fetch for each byte in the effective address. This is slower but prevents endianess issues.
*/
- (void)_performWriteOperationWithIndirectX:(uint8_t)opcode
{
uint16_t effectiveAddress = _zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + _cpuRegisters->indexRegisterX)]; // Fetch ADL
effectiveAddress += (_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterX + 1)] << 8); // Fetch ADH
[self writeByte:_writeOperations[opcode](_cpuRegisters,opcode) toCPUAddress:effectiveAddress];
_cpuRegisters->cycle += 6;
}
/* _performOperationAsIndirectY
*
* Description: Performs an operation obtaining the operand through an indirect indexed fetch relative to Y.
*
* Note: This does a separate fetch for each byte in the effective address. This is slower but prevents endianess issues.
*/
- (void)_performWriteOperationWithIndirectY:(uint8_t)opcode
{
uint16_t effectiveAddress = 0;
uint16_t absoluteAddress = _zeroPage[_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter)]; // Fetch BAL
absoluteAddress += (_zeroPage[(uint8_t)(_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + 1)] << 8); // Fetch BAH
effectiveAddress = absoluteAddress + _cpuRegisters->indexRegisterY; // Add IndexRegisterY to BAH,BAL, potential page crossing
[self writeByte:_writeOperations[opcode](_cpuRegisters,opcode) toCPUAddress:effectiveAddress];
_cpuRegisters->cycle += 6;
}
// FIXME: Should emulate all reads and writes for RMW
- (void)_performOperationAsRMWAbsolute:(uint8_t)opcode
{
uint16_t address = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
uint8_t value = _writeOperations[opcode](_cpuRegisters,_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),address));
_cpuRegisters->programCounter += 2;
[self writeByte:value toCPUAddress:address];
_cpuRegisters->cycle += 6; // Read-Modify-Write Absolute operations take 6 cycles
}
// FIXME: Should emulate all reads and writes for RMW
- (void)_performOperationAsRMWAbsoluteX:(uint8_t)opcode
{
uint16_t absoluteAddress = [self readAddressFromCPUAddressSpace:_cpuRegisters->programCounter];
uint16_t indexedAddress = absoluteAddress + _cpuRegisters->indexRegisterX;
uint8_t value = _writeOperations[opcode](_cpuRegisters,_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),indexedAddress));
_cpuRegisters->programCounter += 2;
[self writeByte:value toCPUAddress:indexedAddress];
_cpuRegisters->cycle += 7; // Read-Modify-Write ZeroPage operations take a full 7 cycles
}
// FIXME: Should emulate all reads and writes for RMW
- (void)_performOperationAsRMWZeroPage:(uint8_t)opcode
{
uint8_t offset = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++);
_zeroPage[offset] = _writeOperations[opcode](_cpuRegisters,_zeroPage[offset]);
_cpuRegisters->cycle += 5; // Read-Modify-Write ZeroPage operations take 5 cycles
}
- (void)_performOperationAsRMWZeroPageX:(uint8_t)opcode
{
uint8_t offset = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) + _cpuRegisters->indexRegisterX;
_zeroPage[offset] = _writeOperations[opcode](_cpuRegisters,_zeroPage[offset]);
_cpuRegisters->cycle += 6; // Read-Modify-Write ZeroPage Indexed operations take 6 cycles
}
- (void)_performClearCarry:(uint8_t)opcode
{
_cpuRegisters->statusCarry = 0;
_cpuRegisters->cycle += 2;
}
- (void)_performSetCarry:(uint8_t)opcode
{
_cpuRegisters->statusCarry = 1;
_cpuRegisters->cycle += 2;
}
- (void)_performClearInterrupt:(uint8_t)opcode
{
_cpuRegisters->statusIRQDisable = 0;
_cpuRegisters->cycle += 2;
}
- (void)_performSetInterrupt:(uint8_t)opcode
{
_cpuRegisters->statusIRQDisable = 1;
_cpuRegisters->cycle += 2;
}
- (void)_performClearOverflow:(uint8_t)opcode
{
_cpuRegisters->statusOverflow = 0;
_cpuRegisters->cycle += 2;
}
- (void)_performClearDecimal:(uint8_t)opcode
{
_cpuRegisters->statusDecimal = 0;
_cpuRegisters->cycle += 2;
}
- (void)_performSetDecimal:(uint8_t)opcode
{
_cpuRegisters->statusDecimal = 1;
_cpuRegisters->cycle += 2;
}
/*
* TSX
*/
- (void)_transferStackPointerToIndexRegisterX:(uint8_t)opcode
{
_cpuRegisters->indexRegisterX = _cpuRegisters->stackPointer;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterX >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterX;
_cpuRegisters->cycle += 2;
}
/*
* TXS
*/
- (void)_transferIndexRegisterXToStackPointer:(uint8_t)opcode
{
_cpuRegisters->stackPointer = _cpuRegisters->indexRegisterX;
_cpuRegisters->cycle += 2;
}
/*
* TAX
*/
- (void)_transferAccumulatorToIndexRegisterX:(uint8_t)opcode
{
_cpuRegisters->indexRegisterX = _cpuRegisters->accumulator;
_cpuRegisters->statusNegative = (_cpuRegisters->accumulator >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->accumulator;
_cpuRegisters->cycle += 2;
}
/*
* TXA
*/
- (void)_transferIndexRegisterXToAccumulator:(uint8_t)opcode
{
_cpuRegisters->accumulator = _cpuRegisters->indexRegisterX;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterX >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterX;
_cpuRegisters->cycle += 2;
}
/*
* TAY
*/
- (void)_transferAccumulatorToIndexRegisterY:(uint8_t)opcode
{
_cpuRegisters->indexRegisterY = _cpuRegisters->accumulator;
_cpuRegisters->statusNegative = (_cpuRegisters->accumulator >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->accumulator;
_cpuRegisters->cycle += 2;
}
/*
* TYA
*/
- (void)_transferIndexRegisterYToAccumulator:(uint8_t)opcode
{
_cpuRegisters->accumulator = _cpuRegisters->indexRegisterY;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterY >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterY;
_cpuRegisters->cycle += 2;
}
/*
* DEX
*/
- (void)_decrementIndexRegisterX:(uint8_t)opcode
{
_cpuRegisters->indexRegisterX--;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterX >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterX;
_cpuRegisters->cycle += 2;
}
/*
* INX
*/
- (void)_incrementIndexRegisterX:(uint8_t)opcode
{
_cpuRegisters->indexRegisterX++;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterX >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterX;
_cpuRegisters->cycle += 2;
}
/*
* DEY
*/
- (void)_decrementIndexRegisterY:(uint8_t)opcode
{
_cpuRegisters->indexRegisterY--;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterY >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterY;
_cpuRegisters->cycle += 2;
}
/*
* INY
*/
- (void)_incrementIndexRegisterY:(uint8_t)opcode
{
_cpuRegisters->indexRegisterY++;
_cpuRegisters->statusNegative = (_cpuRegisters->indexRegisterY >> 7);
_cpuRegisters->statusZero = !_cpuRegisters->indexRegisterY;
_cpuRegisters->cycle += 2;
}
/* Branching Instructions
*
* BPL, BMI, BVC, BVS, BCC, BCS, BNE, BEQ
*/
- (void)_performBranchOnPositive:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusNegative ? 1 : (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1 ;
_cpuRegisters->cycle += (_cpuRegisters->statusNegative ? 2 : (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)));
}
- (void)_performBranchOnNegative:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusNegative ? (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1 : 1;
_cpuRegisters->cycle += (_cpuRegisters->statusNegative ? (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)) : 2);
}
- (void)_performBranchOnOverflowSet:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusOverflow ? (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1 : 1;
_cpuRegisters->cycle += (_cpuRegisters->statusOverflow ? (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)) : 2);
}
- (void)_performBranchOnOverflowClear:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusOverflow ? 1 : (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1;
_cpuRegisters->cycle += (_cpuRegisters->statusOverflow ? 2 : (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)));
}
- (void)_performBranchOnCarrySet:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusCarry ? (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1 : 1;
_cpuRegisters->cycle += (_cpuRegisters->statusCarry ? (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)) : 2);
}
- (void)_performBranchOnCarryClear:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusCarry ? 1 : (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1;
_cpuRegisters->cycle += (_cpuRegisters->statusCarry ? 2 : (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)));
}
- (void)_performBranchOnZeroSet:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusZero ? (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1 : 1;
_cpuRegisters->cycle += (_cpuRegisters->statusZero ? (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)) : 2);
}
- (void)_performBranchOnZeroClear:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter + 1; // Page crossing occurs if branch destination is on a page other than that of the next opcode
_cpuRegisters->programCounter += _cpuRegisters->statusZero ? 1 : (int8_t)_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter) + 1;
_cpuRegisters->cycle += (_cpuRegisters->statusZero ? 2 : (3 + ((oldProgramCounter >> 8) != (_cpuRegisters->programCounter >> 8) ? 1 : 0)));
}
- (void)_performAbsoluteJump:(uint8_t)opcode
{
uint16_t newProgramCounter = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++); // Read new PCL
newProgramCounter += (_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) << 8); // add new PCH
_cpuRegisters->programCounter = newProgramCounter; // Set new Program Counter
_cpuRegisters->cycle += 3;
}
/* JMP Indirect
* Additional code is introduced here to implement a 6502 bug. If the indirect vector of JMP begins on the last byte of a page then
* the fetch of the second will erroneously occur within the page containing the first byte.
*/
- (void)_performIndirectJump:(uint8_t)opcode
{
uint16_t offset = 0;
uint8_t offsetLowByte = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++); // Low byte of address of new PC
offset = (_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),_cpuRegisters->programCounter++) << 8); // High byte of address of new PC
_cpuRegisters->programCounter = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),(offset | offsetLowByte)); // Load low byte of new PC into PC
_cpuRegisters->programCounter += (_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),(offset | ((uint8_t)(offsetLowByte + 1)))) << 8); // Increment low byte before odding with high byte to allow overflow
_cpuRegisters->cycle += 5;
}
- (void)_performJumpToSubroutine:(uint8_t)opcode
{
uint16_t oldProgramCounter = _cpuRegisters->programCounter;
_cpuRegisters->programCounter = _readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),oldProgramCounter++);
_cpuRegisters->programCounter += (_readByteFromCPUAddressSpace(self,@selector(readByteFromCPUAddressSpace:),oldProgramCounter) << 8); // Don't increment PC so last byte of JSR add is on stack
_stack[_cpuRegisters->stackPointer--] = (oldProgramCounter >> 8); // store program counter high byte on stack
_stack[_cpuRegisters->stackPointer--] = oldProgramCounter; // store program counter low byte on stack
_cpuRegisters->cycle += 6;
}
- (void)_performReturnFromSubroutine:(uint8_t)opcode
{
uint16_t newProgramCounter = _stack[++(_cpuRegisters->stackPointer)];
newProgramCounter += (_stack[++(_cpuRegisters->stackPointer)] << 8);
_cpuRegisters->programCounter = newProgramCounter + 1; // Add one to look past the second byte of the address stored from JSR
_cpuRegisters->cycle += 6;
}
- (void)_performNoOperation:(uint8_t)opcode
{
_cpuRegisters->cycle += 2;
}
- (void)_pushAccumulatorToStack:(uint8_t)opcode
{
_stack[_cpuRegisters->stackPointer--] = _cpuRegisters->accumulator;
_cpuRegisters->cycle += 3;
}
- (void)_popAccumulatorFromStack:(uint8_t)opcode
{
_cpuRegisters->accumulator = _stack[++(_cpuRegisters->stackPointer)];
_cpuRegisters->statusZero = !_cpuRegisters->accumulator;
_cpuRegisters->statusNegative = _cpuRegisters->accumulator >> 7;
_cpuRegisters->cycle += 4;
}
- (void)_pushProcessorStatusToStack:(uint8_t)opcode
{
uint8_t processorStatusByte = (1 << 5);
// The fake break flag value pushed is 1 for PHP/BRK and 0 for IRQ/NMI:
// http://www.6502.org/tutorials/register_preservation.html
// See also http://nesdev.parodius.com/the%20'B'%20flag%20&%20BRK%20instruction.txt
if (opcode == 0x08) {
_cpuRegisters->statusBreak = 1; // If this is invoked as PHP directly, set the break flag
_cpuRegisters->cycle += 3; // and add three cycles
}
processorStatusByte |= (_cpuRegisters->statusNegative << 7);
processorStatusByte |= (_cpuRegisters->statusOverflow << 6);
processorStatusByte |= (_cpuRegisters->statusBreak << 4);
processorStatusByte |= (_cpuRegisters->statusDecimal << 3);
processorStatusByte |= (_cpuRegisters->statusIRQDisable << 2);
processorStatusByte |= (_cpuRegisters->statusZero << 1);
processorStatusByte |= _cpuRegisters->statusCarry;
_stack[_cpuRegisters->stackPointer--] = processorStatusByte;
}
- (void)_popProcessorStatusFromStack:(uint8_t)opcode
{
uint8_t processorStatusByte = _stack[++(_cpuRegisters->stackPointer)];
_cpuRegisters->statusNegative = processorStatusByte >> 7;
_cpuRegisters->statusOverflow = (processorStatusByte & (1 << 6)) >> 6;
_cpuRegisters->statusBreak = (processorStatusByte & (1 << 4)) >> 4;
_cpuRegisters->statusDecimal = (processorStatusByte & (1 << 3)) >> 3;
_cpuRegisters->statusIRQDisable = (processorStatusByte & (1 << 2)) >> 2;
_cpuRegisters->statusZero = (processorStatusByte & (1 << 1)) >> 1;
_cpuRegisters->statusCarry = processorStatusByte & 1;
if (opcode == 0x28) _cpuRegisters->cycle += 4; // Only add time if this was invokved by PLP
}
- (void)_performReturnFromInterrupt:(uint8_t)opcode
{
[self _popProcessorStatusFromStack:0xff];
_cpuRegisters->programCounter = _stack[++(_cpuRegisters->stackPointer)];
_cpuRegisters->programCounter |= (_stack[++(_cpuRegisters->stackPointer)] << 8);
_cpuRegisters->cycle += 6;
}
- (void)_performBreak:(uint8_t)opcode
{
// Brad Taylor is correct in stating that BRK is actually a two-byte opcode, the second being padding - http://nesdev.parodius.com/the%20'B'%20flag%20&%20BRK%20instruction.txt
_cpuRegisters->statusBreak = 1;
_cpuRegisters->programCounter++; // Increment the program counter here to account for padding byte read
_stack[_cpuRegisters->stackPointer--] = (_cpuRegisters->programCounter >> 8); // store program counter high byte on stack
_stack[_cpuRegisters->stackPointer--] = _cpuRegisters->programCounter; // store program counter low byte on stack
[self _pushProcessorStatusToStack:opcode]; // Finally, push the processor status register to the stack
_cpuRegisters->programCounter = [self readAddressFromCPUAddressSpace:0xfffe];
_cpuRegisters->statusIRQDisable = 1;
_cpuRegisters->cycle += 7;
}
- (void)_performInterrupt
{
_cpuRegisters->statusBreak = 0; // Interrupt clears the break flag http://www.6502.org/tutorials/register_preservation.html
_stack[_cpuRegisters->stackPointer--] = (_cpuRegisters->programCounter >> 8); // store program counter high byte on stack
_stack[_cpuRegisters->stackPointer--] = _cpuRegisters->programCounter; // store program counter low byte on stack
[self _pushProcessorStatusToStack:0xff]; // Finally, push the processor status register to the stack
_cpuRegisters->programCounter = [self readAddressFromCPUAddressSpace:0xfffe];
_cpuRegisters->statusIRQDisable = 1;
_cpuRegisters->cycle += 7;
}
- (void)_performNonMaskableInterrupt
{
_cpuRegisters->statusBreak = 0; // Break is not set for NMI http://www.6502.org/tutorials/register_preservation.html
_stack[_cpuRegisters->stackPointer--] = (_cpuRegisters->programCounter >> 8); // store program counter high byte on stack
_stack[_cpuRegisters->stackPointer--] = _cpuRegisters->programCounter; // store program counter low byte on stack
[self _pushProcessorStatusToStack:0xff]; // Finally, push the processor status register to the stack
_cpuRegisters->programCounter = [self readAddressFromCPUAddressSpace:0xfffa];
_cpuRegisters->statusIRQDisable = 1;
_cpuRegisters->cycle += 7;
}
- (id)initWithPPU:(NESPPUEmulator *)ppuEmu andAPU:(NESAPUEmulator *)apuEmu {
[super init];
cartridge = nil;
ppu = ppuEmu; // Non-retained reference;
apu = apuEmu; // Non-retained reference;