-
Notifications
You must be signed in to change notification settings - Fork 16
/
cpu.c
1632 lines (1513 loc) · 46.9 KB
/
cpu.c
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
/*
* Copyright (C) 2017 - 2018 FIX94
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "mapper.h"
#include "mem.h"
#include "ppu.h"
#include "apu.h"
#include "cpu.h"
#include "mapper_h/nsf.h"
#define P_FLAG_CARRY (1<<0)
#define P_FLAG_ZERO (1<<1)
#define P_FLAG_IRQ_DISABLE (1<<2)
#define P_FLAG_DECIMAL (1<<3)
#define P_FLAG_S1 (1<<4)
#define P_FLAG_S2 (1<<5)
#define P_FLAG_OVERFLOW (1<<6)
#define P_FLAG_NEGATIVE (1<<7)
#define CPU_OAM_DMA (1<<0)
#define CPU_DMC_DMA (1<<1)
extern bool nesPause;
//used externally
uint8_t interrupt;
bool cpu_odd_cycle;
bool cpuWriteTMP;
static struct {
const uint8_t *action_arr;
uint16_t pc, pc_nsf_bak, absAddr, indVal;
uint8_t p,p_nsf_bak,a,x,y,s,tmp;
uint8_t p_irq_req;
uint8_t arr_pos;
uint8_t instr;
uint8_t irq;
uint8_t irqMask;
uint8_t dma;
bool boot;
bool reset;
bool needsIndFix;
bool allow_update_irq;
bool currently_dma;
bool oam_ready;
bool dmc_dma_dummyread;
bool dmc_halted;
bool oam_halted;
bool dmc_halt_attempt;
bool oam_dma_pause;
uint16_t oam_dma_addr;
uint16_t dmc_dma_addr;
uint8_t oam_dma_ptr;
uint8_t oam_dma_val;
} cpu;
static void cpuSetStartArray();
void cpuInit()
{
cpuSetStartArray();
cpu.boot = true; //sets up P
cpu.reset = true;
cpu.allow_update_irq = false;
cpu.needsIndFix = false;
cpu.tmp = 0;
cpu.absAddr = 0;
cpu.pc = 0;
cpu.pc_nsf_bak = 0;
cpu.indVal = 0;
cpu.p = 0;
cpu.p_nsf_bak = 0;
cpu.a = 0;
cpu.x = 0;
cpu.y = 0;
cpu.s = 0;
cpu.p_irq_req = 0;
cpu.instr = 0;
cpu.irq = 0;
cpu.irqMask = 0;
cpu.dma = 0;
cpu.currently_dma = false;
cpu.oam_ready = false;
cpu.dmc_dma_dummyread = false;
cpu.dmc_halted = false;
cpu.oam_halted = false;
cpu.dmc_halt_attempt = false;
cpu.oam_dma_pause = false;
cpu.oam_dma_addr = 0;
cpu.dmc_dma_addr = 0;
cpu.oam_dma_ptr = 0;
cpu.oam_dma_val = 0;
interrupt = 0;
cpu_odd_cycle = false;
cpuWriteTMP = false;
//may still be set going from nsf to nes file
//in retroarch, so clear those
nsf_startPlayback = false;
nsf_endPlayback = false;
}
FIXNES_ALWAYSINLINE inline static void setRegStats(uint8_t reg)
{
if(reg == 0)
{
cpu.p |= P_FLAG_ZERO;
cpu.p &= ~P_FLAG_NEGATIVE;
}
else
{
if(reg & (1<<7))
cpu.p |= P_FLAG_NEGATIVE;
else
cpu.p &= ~P_FLAG_NEGATIVE;
cpu.p &= ~P_FLAG_ZERO;
}
}
FIXNES_ALWAYSINLINE inline static void cpuSetARRRegs()
{
if((cpu.a & ((1<<5) | (1<<6))) == ((1<<5) | (1<<6)))
{
cpu.p |= P_FLAG_CARRY;
cpu.p &= ~P_FLAG_OVERFLOW;
}
else if((cpu.a & ((1<<5) | (1<<6))) == 0)
{
cpu.p &= ~P_FLAG_CARRY;
cpu.p &= ~P_FLAG_OVERFLOW;
}
else if(cpu.a & (1<<5))
{
cpu.p &= ~P_FLAG_CARRY;
cpu.p |= P_FLAG_OVERFLOW;
}
else if(cpu.a & (1<<6))
{
cpu.p |= P_FLAG_CARRY;
cpu.p |= P_FLAG_OVERFLOW;
}
}
/* Helper functions for updating reg sets */
FIXNES_ALWAYSINLINE inline static void cpuSetA(uint8_t val)
{
cpu.a = val;
setRegStats(cpu.a);
}
FIXNES_ALWAYSINLINE inline static void cpuSetX(uint8_t val)
{
cpu.x = val;
setRegStats(cpu.x);
}
FIXNES_ALWAYSINLINE inline static void cpuSetY(uint8_t val)
{
cpu.y = val;
setRegStats(cpu.y);
}
FIXNES_ALWAYSINLINE inline static uint8_t cpuSetTMP(uint8_t val)
{
setRegStats(val);
return val;
}
/* Various Instructions used multiple times */
FIXNES_ALWAYSINLINE inline static void cpuAND()
{
cpu.a &= cpu.tmp;
setRegStats(cpu.a);
}
FIXNES_ALWAYSINLINE inline static void cpuORA()
{
cpu.a |= cpu.tmp;
setRegStats(cpu.a);
}
FIXNES_ALWAYSINLINE inline static void cpuEOR()
{
cpu.a ^= cpu.tmp;
setRegStats(cpu.a);
}
FIXNES_ALWAYSINLINE inline static uint8_t cpuASL(uint8_t val)
{
if(val & (1<<7))
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
val <<= 1;
setRegStats(val);
return val;
}
FIXNES_ALWAYSINLINE inline static void cpuASLa() { cpuSetA(cpuASL(cpu.a)); };
FIXNES_ALWAYSINLINE inline static void cpuASLt() { cpu.tmp = cpuASL(cpu.tmp); };
FIXNES_ALWAYSINLINE inline static uint8_t cpuLSR(uint8_t val)
{
if(val & (1<<0))
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
val >>= 1;
setRegStats(val);
return val;
}
FIXNES_ALWAYSINLINE inline static void cpuLSRa() { cpuSetA(cpuLSR(cpu.a)); };
FIXNES_ALWAYSINLINE inline static void cpuLSRt() { cpu.tmp = cpuLSR(cpu.tmp); };
FIXNES_ALWAYSINLINE inline static uint8_t cpuROL(uint8_t val)
{
uint8_t oldP = cpu.p;
if(val & (1<<7))
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
val<<=1;
if(oldP & P_FLAG_CARRY)
val |= (1<<0); //bit 0
setRegStats(val);
return val;
}
FIXNES_ALWAYSINLINE inline static void cpuROLa() { cpuSetA(cpuROL(cpu.a)); };
FIXNES_ALWAYSINLINE inline static void cpuROLt() { cpu.tmp = cpuROL(cpu.tmp); };
FIXNES_ALWAYSINLINE inline static uint8_t cpuROR(uint8_t val)
{
uint8_t oldP = cpu.p;
if(val & (1<<0))
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
val>>=1;
if(oldP & P_FLAG_CARRY)
val |= (1<<7); //bit 7
setRegStats(val);
return val;
}
FIXNES_ALWAYSINLINE inline static void cpuRORa() { cpuSetA(cpuROR(cpu.a)); };
FIXNES_ALWAYSINLINE inline static void cpuRORt() { cpu.tmp = cpuROR(cpu.tmp); };
FIXNES_ALWAYSINLINE inline static void cpuKIL()
{
printf("Processor Requested Lock-Up at %04x\n", cpu.pc-1);
nesPause = true;
}
FIXNES_ALWAYSINLINE inline static void cpuADCv(uint8_t in)
{
//use uint16_t here to easly detect carry
uint16_t res = cpu.a + in;
if(cpu.p & P_FLAG_CARRY)
res++;
if(res > 0xFF)
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
if(!(cpu.a & (1<<7)) && !(in & (1<<7)) && (res & (1<<7)))
cpu.p |= P_FLAG_OVERFLOW;
else if((cpu.a & (1<<7)) && (in & (1<<7)) && !(res & (1<<7)))
cpu.p |= P_FLAG_OVERFLOW;
else
cpu.p &= ~P_FLAG_OVERFLOW;
cpuSetA(res);
}
FIXNES_ALWAYSINLINE inline static void cpuADC() { cpuADCv(cpu.tmp); }
FIXNES_ALWAYSINLINE inline static void cpuSBC() { cpuADCv(~cpu.tmp); }
FIXNES_ALWAYSINLINE inline static void cpuISC() { cpu.tmp = cpuSetTMP(cpu.tmp+1); cpuSBC(); }
FIXNES_ALWAYSINLINE inline static uint8_t cpuCMP(uint8_t reg)
{
if(reg >= cpu.tmp)
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
uint8_t cmpVal = (reg - cpu.tmp);
setRegStats(cmpVal);
return cmpVal;
}
FIXNES_ALWAYSINLINE inline static void cpuCMPa() { cpuCMP(cpu.a); }
FIXNES_ALWAYSINLINE inline static void cpuCMPx() { cpuCMP(cpu.x); }
FIXNES_ALWAYSINLINE inline static void cpuCMPy() { cpuCMP(cpu.y); }
FIXNES_ALWAYSINLINE inline static void cpuCMPax() { cpu.x = cpuCMP(cpu.a&cpu.x); }
FIXNES_ALWAYSINLINE inline static void cpuDCP() { cpu.tmp = cpuSetTMP(cpu.tmp-1); cpuCMPa(); }
FIXNES_ALWAYSINLINE inline static void cpuBIT()
{
if((cpu.a & cpu.tmp) == 0)
cpu.p |= P_FLAG_ZERO;
else
cpu.p &= ~P_FLAG_ZERO;
if(cpu.tmp & P_FLAG_OVERFLOW)
cpu.p |= P_FLAG_OVERFLOW;
else
cpu.p &= ~P_FLAG_OVERFLOW;
if(cpu.tmp & P_FLAG_NEGATIVE)
cpu.p |= P_FLAG_NEGATIVE;
else
cpu.p &= ~P_FLAG_NEGATIVE;
}
FIXNES_ALWAYSINLINE inline static void cpuSLO() { cpuASLt(); cpuORA(); }
FIXNES_ALWAYSINLINE inline static void cpuRLA() { cpuROLt(); cpuAND(); }
FIXNES_ALWAYSINLINE inline static void cpuSRE() { cpuLSRt(); cpuEOR(); }
FIXNES_ALWAYSINLINE inline static void cpuRRA() { cpuRORt(); cpuADC(); }
FIXNES_ALWAYSINLINE inline static void cpuASR() { cpuAND(); cpuLSRa(); }
FIXNES_ALWAYSINLINE inline static void cpuARR()
{
cpuAND();
cpuRORa();
cpuSetARRRegs();
}
FIXNES_ALWAYSINLINE inline static void cpuAAC()
{
cpuAND();
if(cpu.p & P_FLAG_NEGATIVE)
cpu.p |= P_FLAG_CARRY;
else
cpu.p &= ~P_FLAG_CARRY;
}
FIXNES_ALWAYSINLINE inline static void cpuCLC() { cpu.p &= ~P_FLAG_CARRY; }
FIXNES_ALWAYSINLINE inline static void cpuSEC() { cpu.p |= P_FLAG_CARRY; }
FIXNES_ALWAYSINLINE inline static void cpuCLI() { cpu.p_irq_req = 2; }
FIXNES_ALWAYSINLINE inline static void cpuSEI() { cpu.p_irq_req = 1; }
FIXNES_ALWAYSINLINE inline static void cpuCLV() { cpu.p &= ~P_FLAG_OVERFLOW; }
FIXNES_ALWAYSINLINE inline static void cpuCLD() { cpu.p &= ~P_FLAG_DECIMAL; }
FIXNES_ALWAYSINLINE inline static void cpuSED() { cpu.p |= P_FLAG_DECIMAL; }
FIXNES_ALWAYSINLINE inline static void cpuINX() { cpuSetX(cpu.x+1); }
FIXNES_ALWAYSINLINE inline static void cpuINY() { cpuSetY(cpu.y+1); }
FIXNES_ALWAYSINLINE inline static void cpuINCt() { cpu.tmp = cpuSetTMP(cpu.tmp+1); }
FIXNES_ALWAYSINLINE inline static void cpuDEX() { cpuSetX(cpu.x-1); }
FIXNES_ALWAYSINLINE inline static void cpuDEY() { cpuSetY(cpu.y-1); }
FIXNES_ALWAYSINLINE inline static void cpuDECt() { cpu.tmp = cpuSetTMP(cpu.tmp-1); }
FIXNES_ALWAYSINLINE inline static void cpuTXA() { cpuSetA(cpu.x); }
FIXNES_ALWAYSINLINE inline static void cpuTYA() { cpuSetA(cpu.y); }
FIXNES_ALWAYSINLINE inline static void cpuTSX() { cpuSetX(cpu.s); }
FIXNES_ALWAYSINLINE inline static void cpuTXS() { cpu.s = cpu.x; }
FIXNES_ALWAYSINLINE inline static void cpuTAY() { cpuSetY(cpu.a); }
FIXNES_ALWAYSINLINE inline static void cpuTAX() { cpuSetX(cpu.a); }
FIXNES_ALWAYSINLINE inline static void cpuLDA() { cpuSetA(cpu.tmp); }
FIXNES_ALWAYSINLINE inline static void cpuLDX() { cpuSetX(cpu.tmp); }
FIXNES_ALWAYSINLINE inline static void cpuLDY() { cpuSetY(cpu.tmp); }
//written out XAA and ATX to what they could technically do depending on the magic (fixed to 0xFF here)
FIXNES_ALWAYSINLINE inline static void cpuXAA() { cpuSetA(/*(cpu.a|0xFF)&*/cpu.x&cpu.tmp); }
FIXNES_ALWAYSINLINE inline static void cpuATX() { cpuSetA(/*(cpu.a|0xFF)&*/cpu.tmp); cpuSetX(cpu.a); }
FIXNES_ALWAYSINLINE inline static void cpuLAX() { cpuSetA(cpu.tmp); cpuSetX(cpu.tmp); }
FIXNES_ALWAYSINLINE inline static void cpuLAR() { cpu.a = cpu.s; cpuAND(); cpuSetX(cpu.a); cpu.s = cpu.a; }
/* For Interrupt Handling */
#define DEBUG_INTR 0
#define DEBUG_JSR 0
/* Set up all CPU State Definitions */
typedef void (*cpu_action_t)(void);
enum {
CPU_GET_INSTRUCTION = 0,
CPU_NULL_READ8_PC,
CPU_NULL_READ8_PC_INC,
CPU_NULL_READ8_PC_CHK,
CPU_NULL_READ8_PC_ACTION,
CPU_NULL_READ8_PC_ADDR_ADDX_ZP,
CPU_NULL_READ8_PC_ADDR_ADDY_ZP,
CPU_NULL_READ8_PC_TMP_ADDX,
CPU_NULL_READ8_PC_STACK_INC,
CPU_NULL_READ8_PC_STACK_DEC,
CPU_NULL_READ8_PC_STACK_DEC_SET_S1_S2_I,
CPU_NULL_READ8_PC_STACK_DEC_SET_I,
CPU_TMP_READ8_PC_INC,
CPU_TMP_READ8_PC_INC_ACTION,
CPU_TMP_READ8_PC_INC_CHECK_BCC,
CPU_TMP_READ8_PC_INC_CHECK_BCS,
CPU_TMP_READ8_PC_INC_CHECK_BNE,
CPU_TMP_READ8_PC_INC_CHECK_BEQ,
CPU_TMP_READ8_PC_INC_CHECK_BPL,
CPU_TMP_READ8_PC_INC_CHECK_BMI,
CPU_TMP_READ8_PC_INC_CHECK_BVC,
CPU_TMP_READ8_PC_INC_CHECK_BVS,
CPU_BRANCH_SETUP,
CPU_PCL_FROM_TMP_PCH_READ8_PC,
CPU_ADDR_READ8_PC_INC,
CPU_ADDRL_READ8_PC_INC,
CPU_ADDRH_READ8_PC_INC,
CPU_ADDRH_READ8_PC_INC_ADDX,
CPU_ADDRH_READ8_PC_INC_ADDY,
CPU_ADDRL_READ8_TMP_INC,
CPU_ADDRH_READ8_TMP,
CPU_ADDRH_READ8_TMP_ADDY,
CPU_ADDR_READ8,
CPU_ADDR_READ8_CHK,
CPU_ADDR_READ8_ACTION,
CPU_ADDR_READ8_ACTION_CHK,
CPU_INC_PAGE_ADDR_READ8_SET_PC,
CPU_ADDR_WRITE8,
CPU_ADDR_WRITE8_A,
CPU_ADDR_WRITE8_X,
CPU_ADDR_WRITE8_Y,
CPU_ADDR_WRITE8_AX,
CPU_ADDR_WRITE8_AXA,
CPU_ADDR_WRITE8_XAS,
CPU_ADDR_WRITE8_SYA,
CPU_ADDR_WRITE8_SXA,
CPU_ADDR_WRITE8_ACTION,
CPU_STACK_GET_A,
CPU_STACK_GET_P,
CPU_STACK_GET_P_INC,
CPU_STACK_GET_PCL_INC,
CPU_STACK_GET_PCH,
CPU_STACK_STORE_A_DEC,
CPU_STACK_STORE_P_DEC,
CPU_STACK_STORE_PCH_DEC,
CPU_STACK_STORE_PCL_DEC,
CPU_STACK_SET_S1_S2_STORE_P_DEC_SET_I,
CPU_STACK_CLEAR_S1_SET_S2_STORE_P_DEC_SET_I,
CPU_READ_RESETVEC_PCL,
CPU_READ_RESETVEC_PCH,
CPU_READ_NMI_IRQ_VEC_PCL,
CPU_READ_NMI_IRQ_VEC_PCH,
CPU_STATE_END,
};
enum {
CPU_READ_PC = 0,
CPU_READ_CPUTMP,
CPU_READ_ABSADDR,
CPU_READ_STACK,
CPU_READ_RESETVECL,
CPU_READ_RESETVECH,
CPU_READ_NMI_IRQ_VECL,
CPU_READ_NMI_IRQ_VECH,
CPU_WRITE_ADDR,
CPU_WRITE_STACK,
};
// every cpu state does cpu.a mem read/write, this defines all
static const uint8_t cpu_mem_type[CPU_STATE_END] = {
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_PC,
CPU_READ_CPUTMP,
CPU_READ_CPUTMP,
CPU_READ_CPUTMP,
CPU_READ_ABSADDR,
CPU_READ_ABSADDR,
CPU_READ_ABSADDR,
CPU_READ_ABSADDR,
CPU_READ_ABSADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_WRITE_ADDR,
CPU_READ_STACK,
CPU_READ_STACK,
CPU_READ_STACK,
CPU_READ_STACK,
CPU_READ_STACK,
CPU_WRITE_STACK,
CPU_WRITE_STACK,
CPU_WRITE_STACK,
CPU_WRITE_STACK,
CPU_WRITE_STACK,
CPU_WRITE_STACK,
CPU_READ_RESETVECL,
CPU_READ_RESETVECH,
CPU_READ_NMI_IRQ_VECL,
CPU_READ_NMI_IRQ_VECH,
};
static const uint8_t cpu_start_arr[1] = { CPU_GET_INSTRUCTION };
FIXNES_ALWAYSINLINE inline static void cpuSetStartArray()
{
cpu.action_arr = cpu_start_arr;
cpu.arr_pos = 0;
}
/* arrays for non-callable instructions */
static const uint8_t cpu_boot_arr[6] = { CPU_NULL_READ8_PC_STACK_DEC, CPU_NULL_READ8_PC_STACK_DEC, CPU_NULL_READ8_PC_STACK_DEC_SET_S1_S2_I, CPU_READ_RESETVEC_PCL, CPU_READ_RESETVEC_PCH, CPU_GET_INSTRUCTION };
static const uint8_t cpu_reset_arr[6] = { CPU_NULL_READ8_PC_STACK_DEC, CPU_NULL_READ8_PC_STACK_DEC, CPU_NULL_READ8_PC_STACK_DEC_SET_I, CPU_READ_RESETVEC_PCL, CPU_READ_RESETVEC_PCH, CPU_GET_INSTRUCTION };
static const uint8_t cpu_irq_arr[7] = { CPU_NULL_READ8_PC, CPU_STACK_STORE_PCH_DEC, CPU_STACK_STORE_PCL_DEC, CPU_STACK_CLEAR_S1_SET_S2_STORE_P_DEC_SET_I, CPU_READ_NMI_IRQ_VEC_PCL, CPU_READ_NMI_IRQ_VEC_PCH, CPU_GET_INSTRUCTION };
static const uint8_t cpu_kill_arr[2] = { CPU_NULL_READ8_PC_ACTION, CPU_GET_INSTRUCTION };
/* arrays for single special instructions */
static const uint8_t cpu_brk_arr[7] = { CPU_NULL_READ8_PC_INC, CPU_STACK_STORE_PCH_DEC, CPU_STACK_STORE_PCL_DEC, CPU_STACK_SET_S1_S2_STORE_P_DEC_SET_I, CPU_READ_NMI_IRQ_VEC_PCL, CPU_READ_NMI_IRQ_VEC_PCH, CPU_GET_INSTRUCTION };
static const uint8_t cpu_rti_arr[6] = { CPU_NULL_READ8_PC, CPU_NULL_READ8_PC_STACK_INC, CPU_STACK_GET_P_INC, CPU_STACK_GET_PCL_INC, CPU_STACK_GET_PCH, CPU_GET_INSTRUCTION };
static const uint8_t cpu_rts_arr[6] = { CPU_NULL_READ8_PC, CPU_NULL_READ8_PC_STACK_INC, CPU_STACK_GET_PCL_INC, CPU_STACK_GET_PCH, CPU_NULL_READ8_PC_INC, CPU_GET_INSTRUCTION };
static const uint8_t cpu_php_arr[3] = { CPU_NULL_READ8_PC, CPU_STACK_STORE_P_DEC, CPU_GET_INSTRUCTION };
static const uint8_t cpu_pha_arr[3] = { CPU_NULL_READ8_PC, CPU_STACK_STORE_A_DEC, CPU_GET_INSTRUCTION };
static const uint8_t cpu_plp_arr[4] = { CPU_NULL_READ8_PC, CPU_NULL_READ8_PC_STACK_INC, CPU_STACK_GET_P, CPU_GET_INSTRUCTION };
static const uint8_t cpu_pla_arr[4] = { CPU_NULL_READ8_PC, CPU_NULL_READ8_PC_STACK_INC, CPU_STACK_GET_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_jsr_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_NULL_READ8_PC, CPU_STACK_STORE_PCH_DEC, CPU_STACK_STORE_PCL_DEC, CPU_PCL_FROM_TMP_PCH_READ8_PC, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absjmp_arr[3] = { CPU_TMP_READ8_PC_INC, CPU_PCL_FROM_TMP_PCH_READ8_PC, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indjmp_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_READ8, CPU_INC_PAGE_ADDR_READ8_SET_PC, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpsta_arr[3] = { CPU_ADDR_READ8_PC_INC, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpstx_arr[3] = { CPU_ADDR_READ8_PC_INC, CPU_ADDR_WRITE8_X, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpsty_arr[3] = { CPU_ADDR_READ8_PC_INC, CPU_ADDR_WRITE8_Y, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpaax_arr[3] = { CPU_ADDR_READ8_PC_INC, CPU_ADDR_WRITE8_AX, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpXsta_arr[4] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDX_ZP, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpXsty_arr[4] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDX_ZP, CPU_ADDR_WRITE8_Y, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpYstx_arr[4] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDY_ZP, CPU_ADDR_WRITE8_X, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpYaax_arr[4] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDY_ZP, CPU_ADDR_WRITE8_AX, CPU_GET_INSTRUCTION };
static const uint8_t cpu_abssta_arr[4] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absstx_arr[4] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_WRITE8_X, CPU_GET_INSTRUCTION };
static const uint8_t cpu_abssty_arr[4] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_WRITE8_Y, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absaax_arr[4] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_WRITE8_AX, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absXsta_arr[7] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDX, CPU_ADDR_READ8_CHK, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absXsya_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDX, CPU_ADDR_READ8, CPU_ADDR_WRITE8_SYA, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absYsta_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDY, CPU_ADDR_READ8_CHK, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absYaxa_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDY, CPU_ADDR_READ8_CHK, CPU_ADDR_WRITE8_AXA, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absYxas_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDY, CPU_ADDR_READ8, CPU_ADDR_WRITE8_XAS, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absYsxa_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDY, CPU_ADDR_READ8, CPU_ADDR_WRITE8_SXA, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indXsta_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_NULL_READ8_PC_TMP_ADDX, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indXaax_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_NULL_READ8_PC_TMP_ADDX, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP, CPU_ADDR_WRITE8_AX, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indYsta_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP_ADDY, CPU_ADDR_READ8_CHK, CPU_ADDR_WRITE8_A, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indYaxa_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP_ADDY, CPU_ADDR_READ8_CHK, CPU_ADDR_WRITE8_AXA, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bcc_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BCC, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bcs_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BCS, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bne_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BNE, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_beq_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BEQ, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bpl_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BPL, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bmi_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BMI, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bvc_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BVC, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
static const uint8_t cpu_bvs_arr[4] = { CPU_TMP_READ8_PC_INC_CHECK_BVS, CPU_BRANCH_SETUP, CPU_NULL_READ8_PC_CHK, CPU_GET_INSTRUCTION };
FIXNES_ALWAYSINLINE inline static void cpuCheckIrq()
{
cpu.irq |= (interrupt & cpu.irqMask) | ppuNMI();
}
/* useful for the branch checks */
FIXNES_ALWAYSINLINE inline static void cpuBranchCheck(bool takeBranch)
{
cpuCheckIrq();
if(!takeBranch) //get next instruction
cpuSetStartArray();
}
FIXNES_ALWAYSINLINE inline static void cpuBranchSetup()
{
cpu.indVal = cpu.pc + (int8_t)cpu.tmp;
//only need extra cycle if it needs fixup
if((cpu.pc&0xFF00) != (cpu.indVal&0xFF00))
{
//printf("branch %04x %04x %02x\n", cpu.pc, cpu.indVal, cpu.tmp);
cpu.needsIndFix = true;
}
else
cpu.arr_pos++;
cpu.pc = (cpu.pc&0xFF00)|(cpu.indVal&0x00FF);
}
/* arrays for multiple similar instructions */
static const uint8_t cpu_direct_arr[2] = { CPU_NULL_READ8_PC_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_imm_arr[2] = { CPU_TMP_READ8_PC_INC_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpread_arr[3] = { CPU_ADDR_READ8_PC_INC, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpreadwrite_arr[5] = { CPU_ADDR_READ8_PC_INC, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpXread_arr[4] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDX_ZP, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpYread_arr[4] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDY_ZP, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_zpXreadwrite_arr[6] = { CPU_ADDR_READ8_PC_INC, CPU_NULL_READ8_PC_ADDR_ADDX_ZP, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absread_arr[4] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absreadwrite_arr[6] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absXread_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDX, CPU_ADDR_READ8_ACTION_CHK, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absYread_arr[5] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDY, CPU_ADDR_READ8_ACTION_CHK, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absXreadwrite_arr[7] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDX, CPU_ADDR_READ8_CHK, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
static const uint8_t cpu_absYreadwrite_arr[7] = { CPU_ADDRL_READ8_PC_INC, CPU_ADDRH_READ8_PC_INC_ADDY, CPU_ADDR_READ8_CHK, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indXread_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_NULL_READ8_PC_TMP_ADDX, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indXreadwrite_arr[8] = { CPU_TMP_READ8_PC_INC, CPU_NULL_READ8_PC_TMP_ADDX, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indYread_arr[6] = { CPU_TMP_READ8_PC_INC, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP_ADDY, CPU_ADDR_READ8_ACTION_CHK, CPU_ADDR_READ8_ACTION, CPU_GET_INSTRUCTION };
static const uint8_t cpu_indYreadwrite_arr[8] = { CPU_TMP_READ8_PC_INC, CPU_ADDRL_READ8_TMP_INC, CPU_ADDRH_READ8_TMP_ADDY, CPU_ADDR_READ8_CHK, CPU_ADDR_READ8, CPU_ADDR_WRITE8_ACTION, CPU_ADDR_WRITE8, CPU_GET_INSTRUCTION };
/* Set up array pointers to all instruction types */
static const uint8_t *cpu_instr_arr[256] = {
cpu_brk_arr, cpu_indXread_arr, cpu_kill_arr, cpu_indXreadwrite_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpreadwrite_arr, cpu_zpreadwrite_arr,
cpu_php_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_absread_arr, cpu_absread_arr, cpu_absreadwrite_arr, cpu_absreadwrite_arr,
cpu_bpl_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYreadwrite_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpXreadwrite_arr, cpu_zpXreadwrite_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYreadwrite_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absXreadwrite_arr, cpu_absXreadwrite_arr,
cpu_jsr_arr, cpu_indXread_arr, cpu_kill_arr, cpu_indXreadwrite_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpreadwrite_arr, cpu_zpreadwrite_arr,
cpu_plp_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_absread_arr, cpu_absread_arr, cpu_absreadwrite_arr, cpu_absreadwrite_arr,
cpu_bmi_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYreadwrite_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpXreadwrite_arr, cpu_zpXreadwrite_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYreadwrite_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absXreadwrite_arr, cpu_absXreadwrite_arr,
cpu_rti_arr, cpu_indXread_arr, cpu_kill_arr, cpu_indXreadwrite_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpreadwrite_arr, cpu_zpreadwrite_arr,
cpu_pha_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_absjmp_arr, cpu_absread_arr, cpu_absreadwrite_arr, cpu_absreadwrite_arr,
cpu_bvc_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYreadwrite_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpXreadwrite_arr, cpu_zpXreadwrite_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYreadwrite_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absXreadwrite_arr, cpu_absXreadwrite_arr,
cpu_rts_arr, cpu_indXread_arr, cpu_kill_arr, cpu_indXreadwrite_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpreadwrite_arr, cpu_zpreadwrite_arr,
cpu_pla_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_indjmp_arr, cpu_absread_arr, cpu_absreadwrite_arr, cpu_absreadwrite_arr,
cpu_bvs_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYreadwrite_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpXreadwrite_arr, cpu_zpXreadwrite_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYreadwrite_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absXreadwrite_arr, cpu_absXreadwrite_arr,
cpu_imm_arr, cpu_indXsta_arr, cpu_imm_arr, cpu_indXaax_arr,
cpu_zpsty_arr, cpu_zpsta_arr, cpu_zpstx_arr, cpu_zpaax_arr,
cpu_direct_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_abssty_arr, cpu_abssta_arr, cpu_absstx_arr, cpu_absaax_arr,
cpu_bcc_arr, cpu_indYsta_arr, cpu_kill_arr, cpu_indYaxa_arr,
cpu_zpXsty_arr, cpu_zpXsta_arr, cpu_zpYstx_arr, cpu_zpYaax_arr,
cpu_direct_arr, cpu_absYsta_arr, cpu_direct_arr, cpu_absYxas_arr,
cpu_absXsya_arr, cpu_absXsta_arr, cpu_absYsxa_arr, cpu_absYaxa_arr,
cpu_imm_arr, cpu_indXread_arr, cpu_imm_arr, cpu_indXread_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpread_arr, cpu_zpread_arr,
cpu_direct_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_absread_arr, cpu_absread_arr, cpu_absread_arr, cpu_absread_arr,
cpu_bcs_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYread_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpYread_arr, cpu_zpYread_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYread_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absYread_arr, cpu_absYread_arr,
cpu_imm_arr, cpu_indXread_arr, cpu_imm_arr, cpu_indXreadwrite_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpreadwrite_arr, cpu_zpreadwrite_arr,
cpu_direct_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_absread_arr, cpu_absread_arr, cpu_absreadwrite_arr, cpu_absreadwrite_arr,
cpu_bne_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYreadwrite_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpXreadwrite_arr, cpu_zpXreadwrite_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYreadwrite_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absXreadwrite_arr, cpu_absXreadwrite_arr,
cpu_imm_arr, cpu_indXread_arr, cpu_imm_arr, cpu_indXreadwrite_arr,
cpu_zpread_arr, cpu_zpread_arr, cpu_zpreadwrite_arr, cpu_zpreadwrite_arr,
cpu_direct_arr, cpu_imm_arr, cpu_direct_arr, cpu_imm_arr,
cpu_absread_arr, cpu_absread_arr, cpu_absreadwrite_arr, cpu_absreadwrite_arr,
cpu_beq_arr, cpu_indYread_arr, cpu_kill_arr, cpu_indYreadwrite_arr,
cpu_zpXread_arr, cpu_zpXread_arr, cpu_zpXreadwrite_arr, cpu_zpXreadwrite_arr,
cpu_direct_arr, cpu_absYread_arr, cpu_direct_arr, cpu_absYreadwrite_arr,
cpu_absXread_arr, cpu_absXread_arr, cpu_absXreadwrite_arr, cpu_absXreadwrite_arr,
};
static bool cpuHandleIrqUpdates()
{
//update irq flag if requested
if(cpu.p_irq_req)
{
if(cpu.p_irq_req == 1)
{
#if DEBUG_INTR
printf("Setting irq disable %02x %02x %d %d %d %d\n", cpu.p, P_FLAG_IRQ_DISABLE, cpu.irq, apu_interrupt,
!(cpu.p & P_FLAG_IRQ_DISABLE), (cpu.p & P_FLAG_IRQ_DISABLE) == 0);
#endif
cpu.p |= P_FLAG_IRQ_DISABLE;
cpu.irqMask = 0;
}
else
{
#if DEBUG_INTR
printf("Clearing irq disable %02x %02x %d %d %d %d\n", cpu.p, P_FLAG_IRQ_DISABLE, cpu.irq, apu_interrupt,
!(cpu.p & P_FLAG_IRQ_DISABLE), (cpu.p & P_FLAG_IRQ_DISABLE) == 0);
#endif
cpu.p &= ~P_FLAG_IRQ_DISABLE;
cpu.irqMask = IRQ_MASK;
}
cpu.p_irq_req = 0;
}
//handle incoming IRQs
if(cpu.reset)
{
//for consistent apu reset times
cpu_odd_cycle = true;
if(cpu.boot)
{
apuBoot();
cpu.action_arr = cpu_boot_arr;
}
else
{
apuReset();
ppuReset();
mapperReset();
cpu.action_arr = cpu_reset_arr;
}
cpu.arr_pos = 0;
//cpu.instr = 0;
cpu.boot = false;
cpu.reset = false;
return true;
}
else if(cpu.irq)
{
cpu.action_arr = cpu_irq_arr;
cpu.arr_pos = 0;
//cpu.instr = 0;
#if DEBUG_INTR
printf("NMI/INTR from instr %02x cpu.p %02x cpu.pc %04x\n",instr,cpu.p,cpu.pc);
#endif
return true;
}
//acts similar to an irq
if(nsf_startPlayback)
{
cpuStartPlayNSF();
nsf_startPlayback = false;
return true;
}
else if(nsf_endPlayback)
{
cpuEndPlayNSF();
nsf_endPlayback = false;
return true;
}
return false;
}
FIXNES_ALWAYSINLINE inline static void cpuSetAddrIndFix()
{
//first read will be at wrong pos
//so let cpu know it needs fixup
if((cpu.absAddr&0xFF00) != (cpu.indVal&0xFF00))
{
//printf("%04x %04x\n", cpu.absAddr, cpu.indVal);
cpu.needsIndFix = true;
}
}
FIXNES_ALWAYSINLINE inline static bool cpuDoAddrIndFix()
{
if(cpu.needsIndFix)
{
cpu.absAddr = cpu.indVal;
cpu.needsIndFix = false;
return true;
}
else
return false;
}
extern bool nesPAL;
static bool cpuDMATryHalt()
{
bool ret = true;
uint8_t cpuType = cpu_mem_type[cpu.action_arr[cpu.arr_pos]];
switch(cpuType)
{
case CPU_READ_PC:
if(nesPAL) break;
memGet8(cpu.pc);
break;
case CPU_READ_CPUTMP:
if(nesPAL) break;
memGet8(cpu.tmp);
break;
case CPU_READ_ABSADDR:
if(nesPAL) break;
memGet8(cpu.absAddr);
break;
case CPU_READ_STACK:
if(nesPAL) break;
memGet8(0x100+cpu.s);
break;
case CPU_READ_RESETVECL:
if(nesPAL) break;
memGet8(0xFFFC);
break;
case CPU_READ_RESETVECH:
if(nesPAL) break;
memGet8(0xFFFD);
break;
case CPU_READ_NMI_IRQ_VECL:
if(nesPAL) break;
if(cpu.irq & PPU_NMI) //NMI
memGet8(0xFFFA);
else //IRQ
memGet8(0xFFFE);
break;
case CPU_READ_NMI_IRQ_VECH:
if(nesPAL) break;
if(cpu.irq & PPU_NMI) //NMI
memGet8(0xFFFB);
else //IRQ
memGet8(0xFFFF);
break;
default: //CPU_WRITE
ret = false;
break;
}
return ret;
}
static bool cpuTryTakeover()
{
//printf("DMA Takeover tick\n");
bool cpu_is_read = cpuDMATryHalt();
if(cpu_is_read)
{
//printf("DMA Takeover done\n");
cpu.currently_dma = true;
//reset OAM DMA pointer
cpu.oam_dma_ptr = 0;
//set OAM ready to false
cpu.oam_ready = false;
//set dmc dummy read to true
cpu.dmc_dma_dummyread = true;
return true;
}
return false;
}
FIXNES_NOINLINE static void cpuDoDMA()
{
if(cpu.dma & CPU_DMC_DMA)
{
if(cpu.currently_dma && cpu.dmc_halted)
{
if(cpu.dmc_dma_dummyread) //1st read always dummy read
{
cpu.dmc_dma_dummyread = false;
if(!(cpu.dma&CPU_OAM_DMA) || cpu.oam_dma_pause)
{
cpuDMATryHalt();
return;
}
}
else if(!cpu_odd_cycle) //READ on even
{
uint8_t dmc_dma_val = memGet8(cpu.dmc_dma_addr);
apuWriteDMCBuf(dmc_dma_val);
cpu.dma &= ~CPU_DMC_DMA;
if(!cpu.oam_halted) //done with DMA
cpu.currently_dma = false;
//for next dmc dma
cpu.dmc_halt_attempt = false;
cpu.dmc_halted = false;
return;
}
else //WRITE on odd, possibly another dummy read
{
if(!(cpu.dma&CPU_OAM_DMA) || cpu.oam_dma_pause)
{
cpuDMATryHalt();
return;
}
}
}
else
{
//First DMC halt attempt ALWAYS during odd (write) cycle
//Second (or 3rd) halt attempt during any cycle
if(cpu_odd_cycle || cpu.dmc_halt_attempt)
{
if(cpu.currently_dma || cpuTryTakeover())
{
cpu.dmc_halted = true;
//fully ignore OAM DMA if this is out 2nd/3rd attempt
if((cpu.dma&CPU_OAM_DMA) && cpu.dmc_halt_attempt)
cpu.oam_dma_pause = true;
else //was first halt attempt, possibly allows next OAM DMA cycle
cpu.oam_dma_pause = false;
}
else
cpu.dmc_halt_attempt = true;
}
}
}
if(cpu.dma&CPU_OAM_DMA)
{
if(cpu.currently_dma && cpu.oam_halted)
{
if(!cpu_odd_cycle) //READ on even
{
//printf("OAM DMA Read %04x\n",cpu.oam_dma_addr|cpu.oam_dma_ptr);
cpu.oam_dma_val = memGet8(cpu.oam_dma_addr|cpu.oam_dma_ptr);
cpu.oam_ready = true;
}
else //WRITE on odd
{
if(cpu.oam_ready)
{
cpu.oam_ready = false;
//printf("OAM DMA Write %02x\n", cpu.oam_dma_val);
memSet8(0x2004,cpu.oam_dma_val);
cpu.oam_dma_ptr++;
//wrapped back, done with DMA
if(cpu.oam_dma_ptr == 0)
{
//printf("OAM DMA Done\n");
cpu.dma &= ~CPU_OAM_DMA;
if(!cpu.dmc_halted) //done with DMA
cpu.currently_dma = false;
cpu.oam_halted = false;
}
}
else //alignment cycle
cpuDMATryHalt();
}
}
else if(cpu.currently_dma || cpuTryTakeover())
cpu.oam_halted = true;