forked from markoni985/NesCat
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNesCat.ino
5783 lines (4866 loc) · 172 KB
/
NesCat.ino
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
//MEOW?
// ▄▄
// ▓▀██▄ ▄▓█▀▓▓
// ▐▌░ ▀███▄ ▄███▀ ░▓▒
// ▓▌ ░░░▒██████████████▓░▒░░░▓▌
// ▄▄▄██████▄▓▌ ░▒▒▒▒▀██████████████▄░░░▓▌
// ▄█████████████▌░▒▒▒▒▒▄████████████████▒▐▌
// ▄████████████████▌▄▄▄█████████▒░█████████▓▒
// ▐████████████████████████████▓ ▀████████░
// ▐███████▀▒▒▄▒ ▓██████████▓ ▀▓██████▒
// ▐▌ ▒▒ ░▓▒ ▓███████▀▀▀▓▓▄ ▄▓▓▀▀███▌
// ▐▓▌ ░▒▒▓▓▒░ ▀▀ ▀▀ ▐▓▌
// ▐██▌ ▒▒▒▓▓▓▒ ▒▒ ▄▓█▓▒
// ▐████▒ ▒▓▒ ░▓▓▓▓▒▒▒▒▒▒▒▓▓▓▄▓▓▓▓███▀░░░░░░
// ▐██████▓▄▄▄▄▄▄▄▄▄▄▓▓▄▄░ ░▓▒ ▒░░▓▓░░░░░
// ░▓█████████████████████▓▒▄▄▄▄▄▄▄▄▓▓▓▓▓▓▓▓▓█▓░░░
//▒▒▒░▒▒▓██████████████████▀▀▀▀▀▀▀▀▀▀▀▀▀▀▒▒▒ ░░ ▒▒
//▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░▄▄▒▒▒▒▒▒▒▒░ ░░░░ ▒▓▒
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▄▓██████▓▒ ░ ░▓▓▀
//▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓███████▀ ░ ▄▓▓▓
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄▒▒▒▒▒▒▒▒▒▒░▓██████▌▒▒▒▒░ ▒▓▒▒
// ▓▓▓▓▓▓▓█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓██████▌▒▒▒▒▒ ▒▓▒▒
// ▀▀▀▀▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██████▌▒▒▒▒▒░ ▓▒▒░
// ▀▀▀▀▓▓▓▓▓▓▓▓▓▓▓███████▓▓▓▓▓▓▒▒
// ▀▀▀▓▓▓███████▓▓▓▓▓▒
// ▀▀▀▀▓▓▓▓▒
//********************************************************************************
//* *
//* NESCAT by Nathalis *
//* prototype version: 0.5 (c)2020 *
//* Enhanced version Nintendo Emulator for ESP32 *
//* *
//* Requirements: ESP32 WEMOS D1 MINI, SD7789 LCD, MICROSD CARD slot, *
//* PCM5102 I2S AUDIO MODULE, OTHER PARTS... *
//* *
//* Only for personal & educational use! *
//* (GPL-3.0 License) *
//* *
//********************************************************************************
//* *
//* features: *
//* - MicroSD card support *
//* - 240*240 1.3" LCD SD7789 display *
//* - Composite TV OUT Video PAL *
//* - I2S AUDIO support PCM5102 module *
//* - PS2 (USB) KEYBOARD support (wireless not work) *
//* - huge NES ROMs up to 512kB (read from FLASH) *
//* - PS4 DualShock BlueTooth Gamepad (experimental) *
//* *
//* to do: *
//* - ESP32-WROVER PSRAM module support *
//* - battery install *
//* - ESP32 MP3-Player support *
//* *
//* *
//********************************************************************************
#define NES_FOLDER ("/NES/")
#define BLUETOOTH_ENABLED
#define KEYBOARD_ENABLED
#define LCD_ENABLED true
#define SOUND_ENABLED true
#define COMPOSITE_VIDEO_ENABLED
#define DEBUG true //Serial debugging enable.
#define DEBUGEXTRA false //Extra Serial debugging enable.
//================================================================================
#define PIN_UP 39 //SVN
#define PIN_DOWN 35 //IO35
#define PIN_LEFT 36 //SVP
#define PIN_RIGHT 34 //IO34
#define PIN_A 2 //IO2
#define PIN_B 14 //TMS
#define PIN_START 15 //TDO
#define PIN_SELECT 13 //TCK
///!!! do not forget 1KOHM resistors
#define KEYBOARD_DATA 4 /// ---[ 1K ]--- // -D
#define KEYBOARD_CLK 0 /// ---[ 1K ]--- // +D
//COMPOSITE_VIDEO: - //DAC_GPIO25_CHANNEL or DAC_GPIO26_CHANNEL
#define VIDEO_OUT (DAC_GPIO26_CHANNEL)
//AUDIO_i2S:
#define I2S_BCK_IO (GPIO_NUM_27) //BCK
#define I2S_WS_IO (GPIO_NUM_32) //LCK
#define I2S_DO_IO (GPIO_NUM_25) //DIN
#define I2S_DI_IO (-1)
//LCD_ST7789:
#define TFT_CS -1 // define chip select pin
#define TFT_DC 5 // define data/command pin
#define TFT_RST 19
#define TFT_MOSI 23 // Data out (SDA) //better not change
#define TFT_SCLK 18 // Clock out (SCL) //better not change
//micro_SD_Card:
#define SOFTSD_MOSI_PIN (GPIO_NUM_17)
#define SOFTSD_MISO_PIN (GPIO_NUM_16)
#define SOFTSD_SCK_PIN (GPIO_NUM_21)
#define SD_CS_PIN GPIO_NUM_22
//================================================================================
//********************************************************************************
//MAIN LIBRARIES:
#include <esp_task_wdt.h>
#include "esp_types.h"
#include "esp_heap_caps.h"
#include "esp_attr.h"
#include "esp_intr_alloc.h"
#include "esp_err.h"
#include "soc/gpio_reg.h"
#include "soc/rtc.h"
#include "soc/soc.h"
#include "soc/i2s_struct.h"
#include "soc/i2s_reg.h"
#include "soc/ledc_struct.h"
#include "soc/rtc_io_reg.h"
#include "soc/io_mux_reg.h"
#include "rom/gpio.h"
#include "rom/lldesc.h"
#include "driver/periph_ctrl.h"
#include "driver/dac.h"
#include "driver/gpio.h"
//********************************************************************************
//--------------------------------------------------------------------------------
//SPI FLASH MEMORY ACCESS:
#include "esp_spi_flash.h"
#define SPI_FLASH_ADDRESS 0x00300000 //0x00300000 working (empty flash area)
#define SPI_FLASH_SECTOR_SIZE 0x1000 //4kB = better not change
uint32_t FILE_ROM_SIZE = 0;
uint32_t FLASH_ROM_SIZE = 0;
//constant data pointer for direct access
const void *ROM;
spi_flash_mmap_handle_t handle1;
uint8_t flashdata[4096] = {0}; //4kB buffer
//--------------------------------------------------------------------------------
//DRAM MEMORY ACCESS:
//--------------------------------------------------------------------------------
char* ROMFILENAME; //NES load File name
unsigned char *rom = 0; //Actual ROM pointer
//********************************************************************************
//Allocated MEMORY variables:
uint8_t* SCREENMEMORY[256]; //256*256 bytes
uint16_t SCREENBUFFER[256]; //512 bytes
//********************************************************************************
//********************************************************************************
//PALETTES:
const uint32_t nes_32bit[64] = { //4colors Green-Yellow-Red-Blue
0x2D253469, 0x000000D9, 0x000000A1, 0x0F111CA1, 0x001F3F70, 0x0024491C, 0x06264900, 0x061F3B00,
0x12162300, 0x2D0B0000, 0x27090000, 0x21080000, 0x1806004D, 0x14141414, 0x14141414, 0x14141414,
0x453950A1, 0x2D0B00D9, 0x210800D9, 0x181C2DD9, 0x002F5EAF, 0x0031624D, 0x153B6C00, 0x2139620E,
0x2D304900, 0x45110000, 0x3F0F0000, 0x3F0F0038, 0x330C0077, 0x14141414, 0x14141414, 0x14141414,
0x5D4D6CD9, 0x451D18D9, 0x33232DD9, 0x2D2C42D9, 0x2D416CD9, 0x213E6C85, 0x2D416C4D, 0x3C456C38,
0x45476C00, 0x5D3F5015, 0x51272646, 0x5D2A2685, 0x571500BD, 0x2D253469, 0x14141414, 0x14141414,
0x5D4D6CD9, 0x543846D9, 0x453950D9, 0x45405ED9, 0x45476CD9, 0x3C456CA8, 0x4E48699A, 0x544B6C93,
0x514A6C69, 0x5D465E69, 0x5D3F50A1, 0x5D3F50BD, 0x5D1700D9, 0x514A6CD9, 0x14141414, 0x14141414,
};
const uint16_t nes_16bit[64] = {
0x7BEF, 0x001F, 0x0017, 0x4157, 0x9010, 0xA804, 0xA880, 0x88A0,
0x5180, 0x03C0, 0x0340, 0x02C0, 0x020B, 0x0000, 0x0000, 0x0000,
0xBDF7, 0x03DF, 0x02DF, 0x6A3F, 0xD819, 0xE00B, 0xF9C0, 0xE2E2,
0xABE0, 0x05C0, 0x0540, 0x0548, 0x0451, 0x0000, 0x0000, 0x0000,
0xFFDF, 0x3DFF, 0x6C5F, 0x9BDF, 0xFBDF, 0xFAD3, 0xFBCB, 0xFD08,
0xFDC0, 0xBFC3, 0x5ECA, 0x5FD3, 0x075B, 0x7BCF, 0x0000, 0x0000,
0xFFFF, 0xA73F, 0xBDDF, 0xDDDF, 0xFDDF, 0xFD38, 0xF696, 0xFF15,
0xFECF, 0xDFCF, 0xBFD7, 0xBFDB, 0x07FF, 0xFEDF, 0x0000, 0x0000,
};
//********************************************************************************
//********************************************************************************
#ifdef BLUETOOTH_ENABLED
//Bluetooth_PS4_DualShock_support_for_ESP32:
#include "src/hid_server/hid_server.h"
#endif
//COMPOSITE_VIDEO_OUT
#ifdef COMPOSITE_VIDEO_ENABLED
#include "src/compositevideo/video.h"
#endif
//********************************************************************************
//********************************************************************************
//LIBRARIES:
//LCD_ST7789:
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
//micro_SD_Card:
#include <SdFat.h>
///#define ENABLE_SOFTWARE_SPI_CLASS 1 // Must be set in SdFat/SdFatConfig.h !!!
//AUDIO_i2S:
#ifdef SOUND_ENABLED
#include "driver/i2s.h"
#endif
//********************************************************************************
//CONFIGS:
//--------------------------------------------------------------------------------
//LCD_ST7789
#define ST7789_DRIVER // Configure all registers
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
#define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
//micro_SD_Card
const uint8_t SOFT_MOSI_PIN = SOFTSD_MOSI_PIN;
const uint8_t SOFT_MISO_PIN = SOFTSD_MISO_PIN;
const uint8_t SOFT_SCK_PIN = SOFTSD_SCK_PIN;
SdFatSoftSpi<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> SD;
File fp;
SdFile dirFile; //for root directory file listing
SdFile file;
//********************************************************************************
//********************************************************************************
//********************************************************************************
//--------------------------------------------------------------------------------
///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))
// 16Bit Color set
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x102E
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
//--------------------------------------------------------------------------------
//================================================================================
//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;
//********************************************************************************
QueueHandle_t vidQueue;
//********************************************************************************
//********************************************************************************
uint8_t JOY_UP = 0;
uint8_t JOY_DOWN = 0;
uint8_t JOY_LEFT = 0;
uint8_t JOY_RIGHT = 0;
uint8_t JOY_CROSS = 0;
uint8_t JOY_SQUARE = 0;
uint8_t JOY_CIRCLE = 0;
uint8_t JOY_TRIANGLE = 0;
uint8_t JOY_SHARE = 0; //(START)
uint8_t JOY_OPTIONS = 0; //(SELECT)
//********************************************************************************
//********************************************************************************
uint8_t scancode = 0;
boolean keyup = false;
byte keymap[256];
IRAM_ATTR void USB_KEYBOARD() {
#ifdef KEYBOARD_ENABLED
if (keymap[0x75] == 0) {
JOY_UP = 1;
}
else JOY_UP = 0;
if (keymap[0x72] == 0) {
JOY_DOWN = 1;
}
else JOY_DOWN = 0;
if (keymap[0x6B] == 0) {
JOY_LEFT = 1;
}
else JOY_LEFT = 0;
if (keymap[0x74] == 0) {
JOY_RIGHT = 1;
}
else JOY_RIGHT = 0;
if (keymap[0x11] == 0 || keymap[0x1A] == 0) JOY_CROSS = 1; //ALT or X
else JOY_CROSS = 0;
if (keymap[0x14] == 0 || keymap[0x22] == 0) JOY_SQUARE = 1; //CTRL or Z
else JOY_SQUARE = 0;
if (keymap[0x5A] == 0) JOY_SHARE = 1;
else JOY_SHARE = 0;
if (keymap[0x66] == 0) JOY_OPTIONS = 1;
else JOY_OPTIONS = 0;
if (keymap[0x76] == 0) {
JOY_SHARE = 1;
JOY_OPTIONS = 1;
} else if (JOY_SHARE == 1 && JOY_OPTIONS == 1 ) {
JOY_SHARE = 0;
JOY_OPTIONS = 0;
}
if (DEBUGEXTRA) {
if (JOY_UP) Serial.print("UP.");
if (JOY_DOWN) Serial.print("DOWN.");
if (JOY_LEFT) Serial.print("LEFT.");
if (JOY_RIGHT) Serial.print("RIGHT.");
if (JOY_SHARE) Serial.print("START.");
if (JOY_OPTIONS) Serial.print("SELECT.");
if (JOY_CROSS) Serial.print("A.");
if (JOY_SQUARE) Serial.print("B.");
Serial.println();
}
#endif
}
//********************************************************************************
void IRAM_ATTR kb_interruptHandler(void) {
#ifdef KEYBOARD_ENABLED
static uint8_t bitcount = 0;
uint8_t val;
int clock = digitalRead(KEYBOARD_CLK);
if (clock == 0) {
return;
}
val = digitalRead(KEYBOARD_DATA);
if (DEBUGEXTRA) Serial.print(".");
if (DEBUGEXTRA) Serial.print(val);
bitcount++;
if (bitcount > 1 && bitcount < 10) { //8bits
scancode |= ((val & 1) << (bitcount - 2));
}
if (bitcount > 10) {
if (keyup == true) {
keymap[scancode] = 1;
if (DEBUGEXTRA) {
Serial.print(" {");
Serial.print(scancode, HEX);
Serial.print("} ");
}
keyup = false;
} else {
keymap[scancode] = 0;
}
if (scancode == 0xF0 ) {
keyup = true;
} else {
keyup = false;
}
if (DEBUGEXTRA) {
Serial.print("[");
Serial.print(scancode, HEX);
Serial.println("]");
}
USB_KEYBOARD();
bitcount = 0;
scancode = 0;
}
#endif
}
void kb_begin() {
#ifdef KEYBOARD_ENABLED
pinMode(KEYBOARD_DATA, INPUT_PULLUP);
pinMode(KEYBOARD_CLK, INPUT_PULLUP);
digitalWrite(KEYBOARD_DATA, true);
digitalWrite(KEYBOARD_CLK, true);
attachInterrupt(digitalPinToInterrupt(KEYBOARD_CLK), kb_interruptHandler, RISING);
memset(keymap, 1, sizeof(keymap));
#endif
}
//********************************************************************************
//********************************************************************************
uint8_t buf[64] = {0}; //JoyRead buffer...
int num_get = 0;
IRAM_ATTR static void PS4_JOY() {
#ifdef BLUETOOTH_ENABLED
// update the bluetooth edr/hid stack
///hid_update();
//read data from JoyStick
num_get = hid_get(buf, sizeof(buf) - 1); // called from emulation loop
//********************************************************************************
if ((num_get != -1) && buf[0] == 161) { // If connected then read JoyStick
uint8_t JOYBUTTON = buf[6];
uint8_t JOYBUTTON2 = buf[7];
///Serial.println(JOYBUTTON);
if ( JOYBUTTON % 8 == 0) {
JOY_UP = 1;
}
else if ( JOYBUTTON % 8 == 1) {
JOY_UP = 1;
JOY_RIGHT = 1;
JOY_DOWN = 0;
JOY_LEFT = 0;
}
else if ( JOYBUTTON % 8 == 2) {
JOY_RIGHT = 1;
JOY_DOWN = 0;
JOY_LEFT = 0;
JOY_UP = 0;
}
else if ( JOYBUTTON % 8 == 3) {
JOY_RIGHT = 1;
JOY_DOWN = 1;
JOY_LEFT = 0;
JOY_UP = 0;
}
else if ( JOYBUTTON % 8 == 4) {
JOY_DOWN = 1;
JOY_LEFT = 0;
JOY_RIGHT = 0;
JOY_UP = 0;
}
else if ( JOYBUTTON % 8 == 5) {
JOY_DOWN = 1;
JOY_LEFT = 1;
JOY_RIGHT = 0;
JOY_UP = 0;
}
else if ( JOYBUTTON % 8 == 6) {
JOY_LEFT = 1;
JOY_UP = 0;
JOY_DOWN = 0;
JOY_RIGHT = 0;
}
else if ( JOYBUTTON % 8 == 7) {
JOY_LEFT = 1;
JOY_UP = 1;
JOY_DOWN = 0;
JOY_RIGHT = 0;
}
if (((JOYBUTTON >> 3) & 1) != 0) {
JOY_UP = 0;
JOY_DOWN = 0;
JOY_LEFT = 0;
JOY_RIGHT = 0;
}
if ( JOYBUTTON > 8 && ((JOYBUTTON >> 4) & 1) != 0) {
JOY_SQUARE = 1;
}
else if (((JOYBUTTON >> 4) & 1) == 0) {
JOY_SQUARE = 0;
}
if ( JOYBUTTON > 8 && ((JOYBUTTON >> 4) & 2) != 0) {
JOY_CROSS = 1;
}
else if (((JOYBUTTON >> 4) & 2) == 0) {
JOY_CROSS = 0;
}
if ( JOYBUTTON > 8 && ((JOYBUTTON >> 4) & 4) != 0) {
JOY_CIRCLE = 1;
}
else if (((JOYBUTTON >> 4) & 4) == 0) {
JOY_CIRCLE = 0;
}
if ( JOYBUTTON > 8 && ((JOYBUTTON >> 4) & 8) != 0) {
JOY_TRIANGLE = 1;
}
else if (((JOYBUTTON >> 8) & 1) == 0) {
JOY_TRIANGLE = 0;
}
if (JOYBUTTON2 == 0) {
JOY_SHARE = 0;
JOY_OPTIONS = 0;
}
if ( JOYBUTTON2 > 8 && ((JOYBUTTON2 >> 4) & 1) != 0) {
JOY_SHARE = 1;
}
if ( JOYBUTTON2 > 8 && ((JOYBUTTON2 >> 4) & 2) != 0) {
JOY_OPTIONS = 1;
}
}
if (DEBUGEXTRA) {
if (JOY_UP) Serial.print("UP.");
if (JOY_DOWN) Serial.print("DOWN.");
if (JOY_LEFT) Serial.print("LEFT.");
if (JOY_RIGHT) Serial.print("RIGHT.");
if (JOY_SHARE) Serial.print("START.");
if (JOY_OPTIONS) Serial.print("SELECT.");
if (JOY_CROSS) Serial.print("A.");
if (JOY_SQUARE) Serial.print("B.");
Serial.println();
}
#endif
}
//********************************************************************************
/* 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, 10pin 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) NES_POWER = 0; //EXIT
//EJECT EMULATOR
if (JOY_SHARE == 1 && JOY_OPTIONS == 1) {
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));
}
//********************************************************************************
//********************************************************************************
//BUFFER TEXT DRAW FUNCTIONS
#include "Retro8x16.c" //used font...
void screenmemory_fillscreen(uint8_t COLOR) {
for (uint16_t Ypos = 0; Ypos < DEFAULT_HEIGHT; Ypos++)
for (uint16_t Xpos = 0; Xpos < DEFAULT_WIDTH; Xpos++) {
SCREENMEMORY[Ypos][Xpos] = COLOR;
}
if (LCD_ENABLED) xQueueSend(vidQueue, &SCREENMEMORY, 0); //refresh LCD
}
void screenmemory_drawpixel(uint16_t X, uint16_t Y, uint8_t COLOR) {
if (Y < DEFAULT_HEIGHT && X < DEFAULT_WIDTH) SCREENMEMORY[Y][X] = COLOR;
}
void screenmemory_drawfillrectangle(uint16_t X, uint16_t Y, uint16_t Width, uint16_t Height, uint8_t COLOR) {
for (uint16_t Ypos = Y; Ypos < Y + Height; Ypos++)
for (uint16_t Xpos = X; Xpos < X + Width; Xpos++) {
screenmemory_drawpixel(Xpos, Ypos, COLOR);
}
if (LCD_ENABLED) xQueueSend(vidQueue, &SCREENMEMORY, 0); //refresh LCD
}
uint8_t draw_char_xy(uint16_t Main_x, uint16_t Main_y, char Main_char, const char* font) {
uint8_t XcharSize = font[0]; //x char size
uint8_t YcharSize = font[1]; //y char size
uint8_t CHAROFFSET = font[2]; //char start offset
font[3]; //char count
if (Main_char != '\n' && Main_char != '\r') for (uint16_t Ypos = 0; Ypos < YcharSize; Ypos++)
for (uint16_t Xpos = 0; Xpos < XcharSize; Xpos += 8) {
uint8_t CHARLINE = font[(Main_char - CHAROFFSET) * (YcharSize * (XcharSize / 8)) + (Ypos) * (XcharSize / 8) + Xpos / 8 + 4];
if ((Xpos + 0 < XcharSize) && (CHARLINE & 0b10000000) != 0) screenmemory_drawpixel(Main_x + Xpos + 0, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 0, Main_y + Ypos, 63);
if ((Xpos + 1 < XcharSize) && (CHARLINE & 0b01000000) != 0) screenmemory_drawpixel(Main_x + Xpos + 1, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 1, Main_y + Ypos, 63);
if ((Xpos + 2 < XcharSize) && (CHARLINE & 0b00100000) != 0) screenmemory_drawpixel(Main_x + Xpos + 2, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 2, Main_y + Ypos, 63);
if ((Xpos + 3 < XcharSize) && (CHARLINE & 0b00010000) != 0) screenmemory_drawpixel(Main_x + Xpos + 3, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 3, Main_y + Ypos, 63);
if ((Xpos + 4 < XcharSize) && (CHARLINE & 0b00001000) != 0) screenmemory_drawpixel(Main_x + Xpos + 4, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 4, Main_y + Ypos, 63);
if ((Xpos + 5 < XcharSize) && (CHARLINE & 0b00000100) != 0) screenmemory_drawpixel(Main_x + Xpos + 5, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 5, Main_y + Ypos, 63);
if ((Xpos + 6 < XcharSize) && (CHARLINE & 0b00000010) != 0) screenmemory_drawpixel(Main_x + Xpos + 6, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 6, Main_y + Ypos, 63);
if ((Xpos + 7 < XcharSize) && (CHARLINE & 0b00000001) != 0) screenmemory_drawpixel(Main_x + Xpos + 7, Main_y + Ypos, 48);
else screenmemory_drawpixel(Main_x + Xpos + 7, Main_y + Ypos, 63);
}
return XcharSize;
}
//*******************************************************************************//
uint16_t draw_string_xy(uint16_t x, uint16_t y, char *c, const char* font)
{
uint8_t width;
uint8_t XcharSize = font[0]; //x char size
uint8_t YcharSize = font[1]; //y char size
uint16_t textwidth = 0;
while (*c) {
width = draw_char_xy(x, y, *c, font);
textwidth += (width);
x += (width);
c++;
}
if (LCD_ENABLED) xQueueSend(vidQueue, &SCREENMEMORY, 0);
return textwidth;
}
//*******************************************************************************
const char* DisplayFontSet = NULL;
uint16_t XPOS_CHAR = 0;
uint16_t YPOS_CHAR = 0;
void set_font(const char* font) {
DisplayFontSet = font;
}
void set_font_XY(uint16_t x, uint16_t y) {
XPOS_CHAR = x;
YPOS_CHAR = y;
}
void draw_string(char *c) {
if (c[strlen(c) - 1] == '\n') {
draw_string_xy(XPOS_CHAR, YPOS_CHAR, c, DisplayFontSet);
YPOS_CHAR += (uint8_t)(DisplayFontSet[1]);
XPOS_CHAR = 0;
} else {
XPOS_CHAR += draw_string_xy(XPOS_CHAR, YPOS_CHAR, c, DisplayFontSet);
}
}
//================================================================================
void (*audio_callback)(void *buffer, int length) = NULL;
//********************************************************************************
//********************************************************************************
//********************************************************************************
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;
}
//********************************************************************************
//--------------------------------------------------------------------------------
// HID timer ISR
volatile int time_ticks = 0;
IRAM_ATTR static void timer_isr(void)
{
/*#ifdef BLUETOOTH_ENABLED
/// hid_update();
///PS4_JOY();
#endif*/ //this causes crashing
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));
}
//--------------------------------------------------------------------------------
IRAM_ATTR void MEMORY_STATUS() {
if (DEBUG) {
Serial.println();
Serial.println("--------------------------------");
Serial.print("TOTAL HEAP: ");
Serial.println(ESP.getHeapSize());
Serial.print("FREE HEAP: ");
Serial.println(ESP.getFreeHeap());
Serial.print("heap_caps_get_free_size: ");
Serial.println(heap_caps_get_free_size(MALLOC_CAP_8BIT));
Serial.println("--------------------------------");
}
}
//********************************************************************************
//................................................................................
//AUDIO SETUP
typedef struct sndinfo_s
{
int sample_rate;
int bps;
} sndinfo_t;
#ifdef 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),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 /*| ESP_INTR_FLAG_IRAM /* | ESP_INTR_FLAG_SHARED*/, ///(DO NOT USE: ESP_INTR_FLAG_IRAM = BLUETOOTH PROBLEM)
.dma_buf_count = 6,
// .dma_buf_len = 2 * DEFAULT_FRAGSIZE,
.dma_buf_len = 4 * DEFAULT_FRAGSIZE,
.use_apll = false
};
#endif
int init_sound(void)
{
#ifdef SOUND_ENABLED
i2s_driver_install(I2S_NUM_1, &audio_cfg, 0, NULL);
i2s_set_pin(I2S_NUM_1, &pin_config);
#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
}
//--------------------------------------------------------------------------------
//================================================================================
static void lcd_write_frame(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height)
{
uint8_t n, l;
uint16_t i, len;
uint16_t w = TFT_WIDTH;
uint16_t h = TFT_HEIGHT;
uint16_t X_ = 0;
for (i = 0; i < height; i++) {
uint16_t horizontal_offset = 8;
for (X_ = 0; X_ < 256; X_++) {
SCREENBUFFER[X_] = nes_16bit[(0x3F & ((uint8_t *)SCREENMEMORY[i])[X_])];
}
///PAL optimalisation in this case:
tft.drawRGBBitmap(0, i, (uint16_t *)(SCREENBUFFER), 48, 1);
tft.drawRGBBitmap(48, i, (uint16_t *)(SCREENBUFFER + 48), 48, 1);
tft.drawRGBBitmap(96, i, (uint16_t *)(SCREENBUFFER + 96), 48, 1);
tft.drawRGBBitmap(144, i, (uint16_t *)(SCREENBUFFER + 144), 48, 1);
tft.drawRGBBitmap(192, i, (uint16_t *)(SCREENBUFFER + 192), 48, 1);
}
}
//================================================================================
//--------------------------------------------------------------------------------
///QueueHandle_t vidQueue;
static void videoTask(void *arg)
{
int x = (320 - DEFAULT_WIDTH) / 2;
int y = ((240 - DEFAULT_HEIGHT) / 2);
while (1)
{
xQueueReceive(vidQueue, &SCREENMEMORY, portMAX_DELAY);
lcd_write_frame(x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT);
///delay(1); //1ms (prevent crash???)
}
}
//--------------------------------------------------------------------------------
int audiovideo_init() ///osd_init()
{
// disable Core 0 WDT
TaskHandle_t idle_0 = xTaskGetIdleTaskHandleForCPU(0);
esp_task_wdt_delete(idle_0);
init_sound(); //START AUDIO
vidQueue = xQueueCreate( 1, sizeof( unsigned int* ));
///lcd_write_frame(0, 0, 320, 240);
xTaskCreatePinnedToCore(&videoTask, "videoTask", 3072, NULL, 0, NULL, 0);
if (DEBUG) Serial.println("videoTask Pinned To Core 0...");
return 0;
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
#define MAXFILES 256
#define MAXFILENAME_LENGTH 255
void sortStrings(char arr[][MAXFILENAME_LENGTH], int n)
{
char temp[MAXFILENAME_LENGTH];
// Sorting strings using bubble sort
for (int j = 0; j < n - 1; j++)
{
for (int i = j + 1; i < n; i++)
{
if (strcmp(arr[j], arr[i]) > 0)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[i]);
strcpy(arr[i], temp);
}
}
}
}
//================================================================================
// Max of ten files since files are selected with a single digit.
const uint16_t nMaxfiles = MAXFILES; //max. 256 NES roms
char filename[nMaxfiles][MAXFILENAME_LENGTH] = {0};
char fileext[nMaxfiles][4] = {0};
char* NESMENU() {
uint8_t num = 0;
uint8_t loadedFileNames = 0;
screenmemory_fillscreen(63); //black color
set_font_XY(0, 10);
//--------------------------------------------------------------------------------
//LOAD FILENAMES INTO MEMORY...
num = 0;
//Load List files in root directory.
///if (!dirFile.open("/", O_READ)) {
if (!dirFile.open(NES_FOLDER, O_READ)) {
SD.errorHalt("open root failed");
while (1) {};
}
while (num < nMaxfiles && file.openNext(&dirFile, O_READ)) {
// Skip directories and hidden files.
if (!file.isSubDir() && !file.isHidden()) {
for (uint8_t i = sizeof(filename[num]); i > 3; i--) filename[num][i] = 0;
file.getName(filename[num], MAXFILENAME_LENGTH);
for (uint8_t i = sizeof(filename[num]); i > 3; i--) {
if (filename[num][i] != 0) {
fileext[num][3] = '\0';
fileext[num][2] = filename[num][i];
fileext[num][1] = filename[num][i - 1];
fileext[num][0] = filename[num][i - 2];