-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNESemulator_part1.h
4483 lines (3770 loc) · 127 KB
/
NESemulator_part1.h
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
//--------------------------------------------------------------------------------
///TYPEDEFS:
// Define this if running on little-endian (x86) systems
#define HOST_LITTLE_ENDIAN //!!! important for Arduino
#define ZERO_LENGTH 0
// quell stupid compiler warnings
#define UNUSED(x) ((x) = (x))
//================================================================================
//AUDIO_SETUP
#define DEFAULT_SAMPLERATE 24000
#define DEFAULT_FRAGSIZE 60 //max.256, default 240
//VIDEO_SETUP
// Visible (NTSC) screen height
#ifndef NES_VISIBLE_HEIGHT
#define NES_VISIBLE_HEIGHT 240
#endif // !NES_VISIBLE_HEIGHT
#define NES_SCREEN_WIDTH 256
#define NES_SCREEN_HEIGHT 240
#define PAL
// NTSC = 60Hz, PAL = 50Hz
#ifdef PAL
#define NES_REFRESH_RATE 50
#else
#define NES_REFRESH_RATE 60
#endif
#define MAX_MEM_HANDLERS 32
#define DEFAULT_WIDTH NES_SCREEN_WIDTH
#define DEFAULT_HEIGHT NES_VISIBLE_HEIGHT
#define NES_CLOCK_DIVIDER 12 //default: 12
//#define NES_MASTER_CLOCK 21477272.727272727272
#define NES_MASTER_CLOCK (236250000 / 11)
#define NES_SCANLINE_CYCLES (1364.0 / NES_CLOCK_DIVIDER)
#define NES_FIQ_PERIOD (NES_MASTER_CLOCK / NES_CLOCK_DIVIDER / 60)
#define NES_RAMSIZE 0x800
#define NES6502_NUMBANKS 16
#define NES6502_BANKSHIFT 12
#define NES6502_BANKSIZE (0x10000 / NES6502_NUMBANKS)
#define NES6502_BANKMASK (NES6502_BANKSIZE - 1)
uint8_t NES_POWER = 1;
void (*audio_callback)(void *buffer, int length) = NULL;
//********************************************************************************
/* read counters */
int pad0_readcount, pad1_readcount, ppad_readcount, ark_readcount;
void input_strobe(void) {
pad0_readcount = 0;
pad1_readcount = 0;
ppad_readcount = 0;
ark_readcount = 0;
}
uint8_t get_pad0(void) {
uint8_t value;
/// value = (uint8_t) retrieve_type(INP_JOYPAD0);
value = 0;
// 12, 33, 9, 10 pin not usable
if (digitalRead(PIN_A) == 1) value |= 1; //A
if (digitalRead(PIN_B) == 1) value |= 2; //B
if (digitalRead(PIN_SELECT) == 1) value |= 4; //SELECT
if (digitalRead(PIN_START) == 1) value |= 8; //START
if (digitalRead(PIN_UP) == 1) value |= 16; //UP
if (digitalRead(PIN_DOWN) == 1) value |= 32; //DOWN
if (digitalRead(PIN_LEFT) == 1) value |= 64; //LEFT
if (digitalRead(PIN_RIGHT) == 1) value |= 128; //RIGHT
if (digitalRead(PIN_SELECT) == 1 && digitalRead(PIN_START) == 1) {
///osd_stopsound();
///audio_callback = NULL;
NES_POWER = 0; //EXIT
}
//EJECT EMULATOR
if (JOY_SHARE == 1 && JOY_OPTIONS == 1) {
///osd_stopsound();
JOY_SHARE = 0;
JOY_OPTIONS = 0;
NES_POWER = 0;
}
if (JOY_SHARE == 1) value |= 8; //START
if (JOY_OPTIONS == 1) value |= 4; //SELECT
if (JOY_UP == 1) value |= 16; //UP
if (JOY_DOWN == 1) value |= 32; //DOWN
if (JOY_LEFT == 1) value |= 64; //LEFT
if (JOY_RIGHT == 1) value |= 128; //RIGHT
if (JOY_CROSS == 1) value |= 1; //A
if (JOY_SQUARE == 1) value |= 2; //B
/// if (JOY_CIRCLE==1) Serial.println("JOY_CIRCLE");
/// if (JOY_TRIANGLE==1) Serial.println("JOY_TRIANGLE");
// mask out left/right simultaneous keypresses
/// if ((value & JOY_UP) && (value & JOY_DOWN)) value &= ~(JOY_UP | JOY_DOWN);
/// if ((value & JOY_LEFT) && (value & JOY_RIGHT)) value &= ~(JOY_LEFT | JOY_RIGHT);
// return (0x40 | value) due to bus conflicts
///return (0x40 | ((value >> pad0_readcount++) & 1));
return (((value >> pad0_readcount++) & 1));
}
//********************************************************************************
//================================================================================
//********************************************************************************
//********************************************************************************
//********************************************************************************
TimerHandle_t timer_1;
//Seemingly, this will be called only once. Should call func with a freq of frequency,
IRAM_ATTR int osd_installtimer_1(int frequency, void *func, int funcsize, void *counter, int countersize)
{
if (DEBUG) Serial.print("Timer install, freq=");
if (DEBUG) Serial.println(frequency);
delay(1000);
timer_1 = xTimerCreate("nes_hid", configTICK_RATE_HZ / frequency, pdTRUE, NULL, (TimerCallbackFunction_t)func);
xTimerStart(timer_1, 0);
return 0;
}
//********************************************************************************
//................................................................................
//AUDIO SETUP
typedef struct sndinfo_s
{
int sample_rate;
int bps;
} sndinfo_t;
#if SOUND_ENABLED
intr_handle_t i2s_interrupt;
i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCK_IO,
.ws_io_num = I2S_WS_IO,
.data_out_num = I2S_DO_IO,
.data_in_num = I2S_DI_IO //Not used
};
i2s_config_t audio_cfg = {
.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX /*| I2S_MODE_DAC_BUILT_IN*/),
.sample_rate = DEFAULT_SAMPLERATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
///.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
/// .communication_format = static_cast<i2s_comm_format_t>(I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_I2S_MSB),
.communication_format = static_cast<i2s_comm_format_t>(I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 /*| ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_SHARED*/,
.dma_buf_count = 6,
// .dma_buf_len = 2 * DEFAULT_FRAGSIZE,
.dma_buf_len = 4 * DEFAULT_FRAGSIZE,
.use_apll = false
};
#endif
int init_sound(void)
{
#if SOUND_ENABLED
i2s_driver_install(I2S_NUM_1, &audio_cfg, 0, NULL);
i2s_set_pin(I2S_NUM_1, &pin_config);
i2s_set_sample_rates(I2S_NUM_1, DEFAULT_SAMPLERATE);
#endif
audio_callback = NULL;
return 0;
}
void osd_setsound(void (*playfunc)(void *buffer, int length))
{
//Indicates we should call playfunc() to get more data.
audio_callback = playfunc;
}
void osd_stopsound(void)
{
audio_callback = NULL;
}
void osd_getsoundinfo(sndinfo_t *info);
void osd_getsoundinfo(sndinfo_t *info)
{
info->sample_rate = DEFAULT_SAMPLERATE;
info->bps = 16;
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
#if SOUND_ENABLED
QueueHandle_t queue;
// uint16_t audio_frame[DEFAULT_FRAGSIZE];
uint16_t audio_frame[2 * DEFAULT_FRAGSIZE];
#endif
void do_audio_frame()
{
///printf("do_audio_frame\n");
#if SOUND_ENABLED
int left = DEFAULT_SAMPLERATE / NES_REFRESH_RATE;
while (left)
{
int n = DEFAULT_FRAGSIZE;
if (n > left)
n = left;
audio_callback(audio_frame, n); //get more data
//16 bit mono -> 32-bit (16 bit r+l)
for (int i = n - 1; i >= 0; i--)
{
// audio_frame[i] = audio_frame[i] + 0x8000;
/// uint16_t a = (audio_frame[i] >> 3); //VEEERY BAD!
uint16_t a = (audio_frame[i] >> 0);
audio_frame[i * 2 + 1] = 0x8000 + a;
audio_frame[i * 2] = 0x8000 - a;
///Serial.print(audio_frame[i]);
}
size_t i2s_bytes_write;
// i2s_write(I2S_NUM_0, (const char *)audio_frame, 2 * n, &i2s_bytes_write, portMAX_DELAY);
// left -= i2s_bytes_write / 2;
i2s_write(I2S_NUM_1, (const char *)audio_frame, 4 * n, &i2s_bytes_write, portMAX_DELAY);
left -= i2s_bytes_write / 4;
}
#endif
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// HID timer ISR
volatile int time_ticks = 0;
IRAM_ATTR static void timer_isr(void)
{
if (SOUND_ENABLED)
if (audio_callback && NES_POWER == 1) do_audio_frame();
time_ticks++;
}
IRAM_ATTR static void timer_isr_end(void) {}
IRAM_ATTR int install_timer(int hertz) {
return osd_installtimer_1(hertz, (void *) timer_isr, (int) timer_isr_end - (int) timer_isr, (void *) &time_ticks, sizeof(time_ticks));
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
char* NESEXPLORE(char* PATH) {
uint8_t num = 0;
uint8_t loadedFileNames = 0;
//clear memory variables
for (uint16_t tmp = 0; tmp < MAXFILES; tmp++) memset (filename[tmp], 0, sizeof(filename[tmp]));
fileext[0] = 0;
fileext[1] = 0;
fileext[2] = 0;
fileext[3] = 0;
num = 0;
loadedFileNames = 0;
//LOAD FILENAMES INTO MEMORY...
//Load List files in root directory.
///if (!dirFile.open("/", O_READ)) {
if (!dirFile.open(PATH, O_READ)) {
while (1) {};
}
while (num < MAXFILES && file.openNext(&dirFile, O_READ)) {
// Skip hidden files.
if (!file.isHidden()) {
for (uint8_t i = strlen(filename[num]); i > 3; i--) filename[num][i] = 0;
file.getName(filename[num], MAXFILENAME_LENGTH);
if (file.isSubDir()) {
sprintf(filename[num], "%s/", filename[num]);
num++;
} else {
for (uint8_t i = strlen(filename[num]); i > 3; i--) {
if (filename[num][i] != 0) {
fileext[3] = '\0';
fileext[2] = filename[num][i];
fileext[1] = filename[num][i - 1];
fileext[0] = filename[num][i - 2];
break;
}
}
}
if (DEBUG) {
///Serial.println(fileext[num]);
///Serial.println(strlen(filename[num]));
}
//check NES File extension, then increase index
if ((fileext[0] == 'N' || fileext[0] == 'n')
&& (fileext[1] == 'E' || fileext[1] == 'e')
&& (fileext[2] == 'S' || fileext[2] == 's')) {
num++;
}
}
loadedFileNames = num;
file.close();
}
dirFile.close();
if (DEBUG) {
Serial.println("--------------------------------------");
Serial.print("Count of loaded File Names:");
Serial.println(loadedFileNames);
}
sortStrings(filename, loadedFileNames);
//DRAW FILENAMES INTO BUFFER
uint8_t CURSOR = 0;
uint8_t PAGE = 0;
bool NamesDisplayed = false;
while (1) {
#if BLUETOOTH_ENABLED
/// hid_update();
/// PS4_JOY();
#endif
PAGE = CURSOR / FILESPERPAGE;
if (!NamesDisplayed) {
screenmemory_fillscreen(63); //black color
set_font_XY(16, 24 );
draw_string(PATH);
for (num = PAGE * FILESPERPAGE; num < ((PAGE + 1)*FILESPERPAGE) && num < loadedFileNames; num++) {
set_font_XY(40, 48 + 20 * (num % FILESPERPAGE));
///draw_string(filename[num],48);
if (filename[num][strlen(filename[num]) - 1] == '/') draw_string(filename[num], 23);
else draw_string(filename[num], 48);
delay(1);
}
NamesDisplayed = true;
}
//Draw Cursor
set_font_XY(16, 48 + (20 * (CURSOR % FILESPERPAGE)));
draw_string("->", 48);
delay(200);
//PROCESS CURSOR SELECTION
while (JOY_CROSS == 0 && JOY_SQUARE == 0 && JOY_OPTIONS == 0 && JOY_SHARE == 0 && JOY_UP == 0 && JOY_DOWN == 0 && JOY_LEFT == 0 && JOY_RIGHT == 0) {
if (digitalRead(PIN_A) == 1) {
JOY_CROSS = 1; //A
delay(25);
}
if (digitalRead(PIN_B) == 1) {
JOY_SQUARE = 1; //B
delay(25);
}
if (digitalRead(PIN_SELECT) == 1) {
JOY_OPTIONS = 1; //SELECT
delay(25);
}
if (digitalRead(PIN_START) == 1) {
JOY_SHARE = 1; //START
delay(25);
}
if (digitalRead(PIN_UP) == 1) {
JOY_UP = 1;
delay(25);
}
if (digitalRead(PIN_DOWN) == 1) {
JOY_DOWN = 1; //DOWN
delay(25);
}
if (digitalRead(PIN_LEFT) == 1) {
JOY_LEFT = 1; //LEFT
delay(25);
}
if (digitalRead(PIN_RIGHT) == 1) {
JOY_RIGHT = 1; //RIGHT
delay(25);
}
}
//Empty Cursor
set_font_XY(16, 48 + (20 * (CURSOR % FILESPERPAGE)));
draw_string(" ", 48);
if (JOY_SHARE == 1 && JOY_OPTIONS == 1) {
JOY_SHARE = 0;
JOY_OPTIONS = 0;
EXIT = true;
/// PLAYING=false;
NES_POWER = 0;
return MAINPATH;
}
if (JOY_UP == 1 ) {
if (CURSOR % FILESPERPAGE == 0) NamesDisplayed = false; //changed page
if (CURSOR == 0 && loadedFileNames > 0) CURSOR = loadedFileNames - 1;
else if (CURSOR > 0 && loadedFileNames > 0) CURSOR--;
JOY_UP = 0;
}
if (JOY_DOWN == 1 ) {
if (CURSOR % FILESPERPAGE == FILESPERPAGE - 1 || CURSOR == loadedFileNames - 1) NamesDisplayed = false; //changed page
if (CURSOR == loadedFileNames - 1 && loadedFileNames > 0) CURSOR = 0;
else if (CURSOR < loadedFileNames - 1 && loadedFileNames > 0) CURSOR++;
JOY_DOWN = 0;
}
if (JOY_LEFT == 1) {
if (CURSOR > FILESPERPAGE - 1) CURSOR -= FILESPERPAGE;
NamesDisplayed = false;
JOY_LEFT = 0;
}
if (JOY_RIGHT == 1) {
if (CURSOR / FILESPERPAGE < loadedFileNames / FILESPERPAGE) CURSOR += FILESPERPAGE;
if (CURSOR > loadedFileNames - 1) CURSOR = loadedFileNames - 1;
NamesDisplayed = false;
JOY_RIGHT = 0;
}
if (JOY_OPTIONS == 1) {
//do nothing = unused
JOY_OPTIONS = 0;
}
if ((JOY_CROSS == 1 || JOY_SHARE == 1) && JOY_OPTIONS == 0) {
dirFile.close();
JOY_CROSS = 0;
JOY_SHARE = 0;
JOY_OPTIONS = 0;
delay(25);
/// PLAYINGFILE=CURSOR;
/// TOTALFILES=loadedFileNames;
sprintf(MAINPATH, "%s%s", PATH, filename[CURSOR]);
if (DEBUG) Serial.println(MAINPATH);
/// sprintf(TRACKNAME, "%s", filename[CURSOR]);
return MAINPATH ; //START //A
}
if ((JOY_SQUARE == 1 ) && JOY_OPTIONS == 0) {
dirFile.close();
JOY_SQUARE = 0;
JOY_SHARE = 0;
JOY_OPTIONS = 0;
delay(25);
if (DEBUG) Serial.println(PATH);
if (DEBUG) Serial.println(strlen(PATH));
sprintf(MAINPATH, "%s", PATH);
if (strlen(MAINPATH) > 1) {
MAINPATH[strlen(MAINPATH) - 1] = '\0';
for (uint8_t strpos = strlen(MAINPATH) - 1; strpos > 0; strpos--) {
if (MAINPATH[strpos] == '/') break;
MAINPATH[strpos] = '\0';
}
}
if (DEBUG) Serial.println(MAINPATH);
if (DEBUG) Serial.println(strlen(MAINPATH));
return MAINPATH ;
}
};
}
//################################################################################
//********************************************************************************
char* NESBrowse(char* PATH) {
if (PATH[strlen(PATH) - 1] != '/')
if (strlen(PATH) > 1) {
PATH[strlen(PATH) - 1] = '\0';
for (uint8_t strpos = strlen(PATH) - 1; strpos > 0; strpos--) {
if (PATH[strpos] == '/') break;
PATH[strpos] = '\0';
}
}
for (uint16_t tmp = 0; tmp < MAXFILES; tmp++) {
/// filename[tmp] = (char*)malloc(MAXFILENAME_LENGTH);
/// fileext[tmp] = (char*)malloc(4);
}
EXIT = false;
//................................................................................
while (EXIT == false && PATH[strlen(PATH) - 1] == '/') {
PATH = NESEXPLORE(PATH);
Serial.println(PATH);
}
//................................................................................
for (uint16_t tmp = 0; tmp < MAXFILES; tmp++) {
/// free(filename[tmp]);
/// free(fileext[tmp]);
}
return PATH;
}
//________________________________________________________________________________
//--------------------------------------------------------------------------------
//CPU.H:
// Define this to enable decimal mode in ADC / SBC (not needed in NES)
//#define NES6502_DECIMAL
// P (flag) register bitmasks
#define N_FLAG 0x80
#define V_FLAG 0x40
#define R_FLAG 0x20 // Reserved, always 1
#define B_FLAG 0x10
#define D_FLAG 0x08
#define I_FLAG 0x04
#define Z_FLAG 0x02
#define C_FLAG 0x01
// Vector addresses
#define NMI_VECTOR 0xFFFA
#define RESET_VECTOR 0xFFFC
#define IRQ_VECTOR 0xFFFE
// cycle counts for interrupts
#define INT_CYCLES 7
#define RESET_CYCLES 6
#define NMI_MASK 0x01
#define IRQ_MASK 0x02
// Stack is located on 6502 page 1
#define STACK_OFFSET 0x0100
typedef struct {
uint32_t min_range, max_range;
uint8_t (*read_func)(uint32_t address);
} nes6502_memread;
typedef struct {
uint32_t min_range, max_range;
void (*write_func)(uint32_t address, uint8_t value);
} nes6502_memwrite;
typedef struct {
uint8_t *mem_page[NES6502_NUMBANKS]; // memory page pointers
nes6502_memread *read_handler;
nes6502_memwrite *write_handler;
uint32_t pc_reg;
uint8_t a_reg, p_reg;
uint8_t x_reg, y_reg;
uint8_t s_reg;
uint8_t jammed; // is processor jammed?
uint8_t int_pending, int_latency;
int32_t total_cycles, burn_cycles;
} nes6502_context;
//--------------------------------------------------------------------------------
//PPU.H:
// PPU register defines
#define PPU_CTRL0 0x2000
#define PPU_CTRL1 0x2001
#define PPU_STAT 0x2002
#define PPU_OAMADDR 0x2003
#define PPU_OAMDATA 0x2004
#define PPU_SCROLL 0x2005
#define PPU_VADDR 0x2006
#define PPU_VDATA 0x2007
#define PPU_OAMDMA 0x4014
#define PPU_JOY0 0x4016
#define PPU_JOY1 0x4017
// $2000
#define PPU_CTRL0F_NMI 0x80
#define PPU_CTRL0F_OBJ16 0x20
#define PPU_CTRL0F_BGADDR 0x10
#define PPU_CTRL0F_OBJADDR 0x08
#define PPU_CTRL0F_ADDRINC 0x04
#define PPU_CTRL0F_NAMETAB 0x03
// $2001
#define PPU_CTRL1F_OBJON 0x10
#define PPU_CTRL1F_BGON 0x08
#define PPU_CTRL1F_OBJMASK 0x04
#define PPU_CTRL1F_BGMASK 0x02
// $2002
#define PPU_STATF_VBLANK 0x80
#define PPU_STATF_STRIKE 0x40
#define PPU_STATF_MAXSPRITE 0x20
// Sprite attribute byte bitmasks
#define OAMF_VFLIP 0x80
#define OAMF_HFLIP 0x40
#define OAMF_BEHIND 0x20
// Maximum number of sprites per horizontal scanline
#define PPU_MAXSPRITE 8
// some mappers do *dumb* things
typedef void (*ppulatchfunc_t)(uint32_t address, uint8_t value);
typedef void (*ppuvromswitch_t)(uint8_t value);
typedef struct ppu_s {
// big nasty memory chunks
uint8_t nametab[0x1000];
uint8_t oam[256];
uint8_t palette[32];
uint8_t *page[16];
// hardware registers
uint8_t ctrl0, ctrl1, stat, oam_addr;
uint32_t vaddr, vaddr_latch;
int tile_xofs, flipflop;
int vaddr_inc;
uint32_t tile_nametab;
uint8_t obj_height;
uint32_t obj_base, bg_base;
bool bg_on, obj_on;
bool obj_mask, bg_mask;
uint8_t latch, vdata_latch;
uint8_t strobe;
bool strikeflag;
uint32_t strike_cycle;
// callbacks for naughty mappers
ppulatchfunc_t latchfunc;
ppuvromswitch_t vromswitch;
bool vram_accessible;
bool vram_present;
bool drawsprites;
} ppu_t;
//--------------------------------------------------------------------------------
//APU.H:
// define this for realtime generated noise
#define REALTIME_NOISE
#define APU_WRA0 0x4000
#define APU_WRA1 0x4001
#define APU_WRA2 0x4002
#define APU_WRA3 0x4003
#define APU_WRB0 0x4004
#define APU_WRB1 0x4005
#define APU_WRB2 0x4006
#define APU_WRB3 0x4007
#define APU_WRC0 0x4008
#define APU_WRC2 0x400A
#define APU_WRC3 0x400B
#define APU_WRD0 0x400C
#define APU_WRD2 0x400E
#define APU_WRD3 0x400F
#define APU_WRE0 0x4010
#define APU_WRE1 0x4011
#define APU_WRE2 0x4012
#define APU_WRE3 0x4013
#define APU_SMASK 0x4015
// length of generated noise
#define APU_NOISE_32K 0x7FFF
#define APU_NOISE_93 93
#define APU_BASEFREQ 1789772.7272727272727272
// channel structures: As much data as possible is precalculated, to keep the sample processing as lean as possible
typedef struct rectangle_s {
uint8_t regs[4];
bool enabled;
float accum;
int32_t freq;
int32_t output_vol;
bool fixed_envelope;
bool holdnote;
uint8_t volume;
int32_t sweep_phase;
int32_t sweep_delay;
bool sweep_on;
uint8_t sweep_shifts;
uint8_t sweep_length;
bool sweep_inc;
// this may not be necessary in the future
int32_t freq_limit;
int32_t env_phase;
int32_t env_delay;
uint8_t env_vol;
int vbl_length;
uint8_t adder;
int duty_flip;
} rectangle_t;
typedef struct triangle_s {
uint8_t regs[3];
bool enabled;
float accum;
int32_t freq;
int32_t output_vol;
uint8_t adder;
bool holdnote;
bool counter_started;
// quasi-hack
int write_latency;
int vbl_length;
int linear_length;
} triangle_t;
typedef struct noise_s {
uint8_t regs[3];
bool enabled;
float accum;
int32_t freq;
int32_t output_vol;
int32_t env_phase;
int32_t env_delay;
uint8_t env_vol;
bool fixed_envelope;
bool holdnote;
uint8_t volume;
int vbl_length;
#ifdef REALTIME_NOISE
uint8_t xor_tap;
#else
bool short_sample;
int cur_pos;
#endif // REALTIME_NOISE
} noise_t;
typedef struct dmc_s
{
uint8_t regs[4];
// bodge for timestamp queue
bool enabled;
float accum;
int32_t freq;
int32_t output_vol;
uint32_t address;
uint32_t cached_addr;
int dma_length;
int cached_dmalength;
uint8_t cur_byte;
bool looping;
bool irq_gen;
bool irq_occurred;
} dmc_t;
enum
{
APU_FILTER_NONE,
APU_FILTER_LOWPASS,
APU_FILTER_WEIGHTED
};
typedef struct
{
uint32_t min_range, max_range;
uint8_t (*read_func)(uint32_t address);
} apu_memread;
typedef struct
{
uint32_t min_range, max_range;
void (*write_func)(uint32_t address, uint8_t value);
} apu_memwrite;
// external sound chip stuff
typedef struct apuext_s
{
int (*init)(void);
void (*shutdown)(void);
void (*reset)(void);
int32_t (*process)(void);
apu_memread *mem_read;
apu_memwrite *mem_write;
} apuext_t;
typedef struct apu_s
{
rectangle_t rectangle[2];
triangle_t triangle;
noise_t noise;
dmc_t dmc;
uint8_t enable_reg;
void *buffer; // pointer to output buffer
int num_samples;
uint8_t mix_enable;
int filter_type;
double base_freq;
float cycle_rate;
int sample_rate;
int sample_bits;
int refresh_rate;
void (*process)(void *buffer, int num_samples);
void (*irq_callback)(void);
uint8_t (*irqclear_callback)(void);
// external sound chip
apuext_t *ext;
} apu_t;
//--------------------------------------------------------------------------------
//ROM.H:
typedef enum {
MIRROR_HORIZ = 0,
MIRROR_VERT = 1
} mirror_t;
#define ROM_FLAG_BATTERY 0x01
#define ROM_FLAG_TRAINER 0x02
#define ROM_FLAG_FOURSCREEN 0x04
#define ROM_FLAG_VERSUS 0x08
typedef struct rominfo_s
{
// pointers to ROM and VROM
uint8_t *rom, *vrom;
// pointers to SRAM and VRAM
uint8_t *sram, *vram;
// number of banks
int rom_banks, vrom_banks;
int sram_banks, vram_banks;
int mapper_number;
mirror_t mirror;
uint8_t flags;
} rominfo_t;
typedef struct inesheader_s
{
uint8_t ines_magic[4];
uint8_t rom_banks;
uint8_t vrom_banks;
uint8_t rom_type;
uint8_t mapper_hinybble;
uint8_t reserved[8];
} inesheader_t;
//--------------------------------------------------------------------------------
//LIBSNS.H:
#define TAG_LENGTH 4
struct mapper1Data {
unsigned char registers[4];
unsigned char latch;
unsigned char numberOfBits;
};
struct mapper4Data {
unsigned char irqCounter;
unsigned char irqLatchCounter;
unsigned char irqCounterEnabled;
unsigned char last8000Write;
};
struct mapper5Data {
unsigned char dummy; // needed for some compilers; remove if any members are added
};
struct mapper6Data {
unsigned char irqCounter;
unsigned char irqLatchCounter;
unsigned char irqCounterEnabled;
unsigned char last43FEWrite;
unsigned char last4500Write;
};
struct mapper9Data {
unsigned char latch[2];
unsigned char lastB000Write;
unsigned char lastC000Write;
unsigned char lastD000Write;
unsigned char lastE000Write;
};
struct mapper10Data {
unsigned char latch[2];
unsigned char lastB000Write;
unsigned char lastC000Write;
unsigned char lastD000Write;
unsigned char lastE000Write;
};
struct mapper16Data {
unsigned char irqCounterLowByte;
unsigned char irqCounterHighByte;
unsigned char irqCounterEnabled;
};
struct mapper17Data {
unsigned char irqCounterLowByte;
unsigned char irqCounterHighByte;
unsigned char irqCounterEnabled;
};
struct mapper18Data {
unsigned char irqCounterLowByte;
unsigned char irqCounterHighByte;
unsigned char irqCounterEnabled;
};
struct mapper19Data {
unsigned char irqCounterLowByte;
unsigned char irqCounterHighByte;
unsigned char irqCounterEnabled;
};
struct mapper21Data {
unsigned char irqCounter;
unsigned char irqCounterEnabled;
};
struct mapper24Data {
unsigned char irqCounter;
unsigned char irqCounterEnabled;
};
struct mapper40Data {
unsigned char irqCounter;
unsigned char irqCounterEnabled;
};
struct mapper69Data {
unsigned char irqCounterLowByte;
unsigned char irqCounterHighByte;
unsigned char irqCounterEnabled;
};
struct mapper90Data {
unsigned char irqCounter;
unsigned char irqLatchCounter;
unsigned char irqCounterEnabled;
};
struct mapper224Data {
unsigned char chrRamWriteable;
};
struct mapper225Data {
unsigned char prgSize;
unsigned char registers[4];
};
struct mapper226Data {
unsigned char chrRamWriteable;
};