-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathESPectrum.cpp
1861 lines (1478 loc) · 68.7 KB
/
ESPectrum.cpp
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
/*
ESPectrum, a Sinclair ZX Spectrum emulator for Espressif ESP32 SoC
Copyright (c) 2023, 2024 Víctor Iborra [Eremus] and 2023 David Crespo [dcrespo3d]
https://github.com/EremusOne/ZX-ESPectrum-IDF
Based on ZX-ESPectrum-Wiimote
Copyright (c) 2020, 2022 David Crespo [dcrespo3d]
https://github.com/dcrespo3d/ZX-ESPectrum-Wiimote
Based on previous work by Ramón Martinez and Jorge Fuertes
https://github.com/rampa069/ZX-ESPectrum
Original project by Pete Todd
https://github.com/retrogubbins/paseVGA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
To Contact the dev team you can write to zxespectrum@gmail.com or
visit https://zxespectrum.speccy.org/contacto
*/
#include <stdio.h>
#include <string>
#include "ESPectrum.h"
#include "Snapshot.h"
#include "Config.h"
#include "FileUtils.h"
#include "OSDMain.h"
#include "Ports.h"
#include "MemESP.h"
#include "cpuESP.h"
#include "Video.h"
#include "messages.h"
#include "AySound.h"
#include "Tape.h"
#include "Z80_JLS/z80.h"
#include "pwm_audio.h"
#include "fabgl.h"
#include "wd1793.h"
#include "ZXKeyb.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/timer.h"
#include "soc/timer_group_struct.h"
#include "esp_timer.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_efuse.h"
#include "soc/efuse_reg.h"
using namespace std;
//=======================================================================================
// KEYBOARD
//=======================================================================================
fabgl::PS2Controller ESPectrum::PS2Controller;
bool ESPectrum::ps2kbd2 = false;
//=======================================================================================
// AUDIO
//=======================================================================================
uint8_t ESPectrum::audioBuffer[ESP_AUDIO_SAMPLES_PENTAGON] = { 0 };
uint32_t* ESPectrum::overSamplebuf;
signed char ESPectrum::aud_volume;
uint32_t ESPectrum::audbufcnt = 0;
uint32_t ESPectrum::audbufcntover = 0;
uint32_t ESPectrum::faudbufcnt = 0;
uint32_t ESPectrum::audbufcntAY = 0;
uint32_t ESPectrum::faudbufcntAY = 0;
int ESPectrum::lastaudioBit = 0;
int ESPectrum::faudioBit = 0;
int ESPectrum::samplesPerFrame;
bool ESPectrum::AY_emu = false;
int ESPectrum::Audio_freq[4];
unsigned char ESPectrum::audioSampleDivider;
unsigned char ESPectrum::audioAYDivider;
unsigned char ESPectrum::audioOverSampleDivider;
static int audioBitBuf = 0;
static unsigned char audioBitbufCount = 0;
QueueHandle_t audioTaskQueue;
TaskHandle_t ESPectrum::audioTaskHandle;
uint8_t *param;
//=======================================================================================
// TAPE OSD
//=======================================================================================
int ESPectrum::TapeNameScroller = 0;
//=======================================================================================
// BETADISK
//=======================================================================================
bool ESPectrum::trdos = false;
WD1793 ESPectrum::Betadisk;
//=======================================================================================
// ARDUINO FUNCTIONS
//=======================================================================================
#define NOP() asm volatile ("nop")
IRAM_ATTR unsigned long millis()
{
return (unsigned long) (esp_timer_get_time() / 1000ULL);
}
IRAM_ATTR void delayMicroseconds(int64_t us)
{
int64_t m = esp_timer_get_time();
if(us){
int64_t e = (m + us);
if(m > e){ //overflow
while(esp_timer_get_time() > e){
NOP();
}
}
while(esp_timer_get_time() < e){
NOP();
}
}
}
//=======================================================================================
// TIMING / SYNC
//=======================================================================================
double ESPectrum::totalseconds = 0;
double ESPectrum::totalsecondsnodelay = 0;
int64_t ESPectrum::target[4];
int ESPectrum::sync_cnt = 0;
volatile bool ESPectrum::vsync = false;
int64_t ESPectrum::ts_start;
int64_t ESPectrum::elapsed;
int64_t ESPectrum::idle;
uint8_t ESPectrum::ESP_delay = 1; // EMULATION SPEED: 0-> MAX. SPEED (NO SOUND), 1-> 100% SPEED, 2-> 125% SPEED, 3-> 150% SPEED
int ESPectrum::ESPoffset = 0;
//=======================================================================================
// LOGGING / TESTING
//=======================================================================================
int ESPectrum::ESPtestvar = 0;
int ESPectrum::ESPtestvar1 = 0;
int ESPectrum::ESPtestvar2 = 0;
#define START_MSG_DURATION 20
void ShowStartMsg() {
fabgl::VirtualKeyItem Nextkey;
VIDEO::vga.clear(zxColor(7,0));
OSD::drawOSD(false);
int pos_x, pos_y;
if (Config::videomode == 2) {
pos_x = 82;
if (Config::arch[0] == 'T' && Config::ALUTK == 2) {
VIDEO::vga.fillRect( 56, 24, 240,50,zxColor(0, 0));
pos_y = 35;
} else {
VIDEO::vga.fillRect( 56, 48,240,50,zxColor(0, 0));
pos_y = 59;
}
} else {
VIDEO::vga.fillRect(Config::aspect_16_9 ? 60 : 40,Config::aspect_16_9 ? 12 : 32,240,50,zxColor(0, 0));
pos_x = Config::aspect_16_9 ? 86 : 66;
pos_y = Config::aspect_16_9 ? 23 : 43;
}
// Decode Logo in EBF8 format
uint8_t *logo = (uint8_t *)ESPectrum_logo;
int logo_w = (logo[5] << 8) + logo[4]; // Get Width
int logo_h = (logo[7] << 8) + logo[6]; // Get Height
logo+=8; // Skip header
for (int i=0; i < logo_h; i++)
for(int n=0; n<logo_w; n++)
VIDEO::vga.dotFast(pos_x + n,pos_y + i,logo[n+(i*logo_w)]);
OSD::osdAt(7, 1);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(1, 0));
VIDEO::vga.print(StartMsg[Config::lang]);
VIDEO::vga.setTextColor(zxColor(16,0), zxColor(1, 0));
OSD::osdAt(9, 1);
VIDEO::vga.print("ESP");
switch (Config::lang) {
case 0: OSD::osdAt(7, 25);
VIDEO::vga.print("ESP");
OSD::osdAt(13, 13);
VIDEO::vga.print("ESP");
OSD::osdAt(17, 4);
break;
case 1: OSD::osdAt(7, 28);
VIDEO::vga.print("ESP");
OSD::osdAt(13, 13);
VIDEO::vga.print("ESP");
OSD::osdAt(17, 4);
break;
case 2: OSD::osdAt(7, 27);
VIDEO::vga.print("ESP");
OSD::osdAt(13, 18);
VIDEO::vga.print("ESP");
OSD::osdAt(17, 15);
}
VIDEO::vga.setTextColor(zxColor(3, 1), zxColor(1, 0));
VIDEO::vga.print("patreon.com/ESPectrum");
char msg[38];
for (int i=START_MSG_DURATION; i >= 0; i--) {
OSD::osdAt(19, 1);
// sprintf(msg,Config::lang ? "Este mensaje se cerrar" "\xA0" " en %02d segundos" : "This message will close in %02d seconds",i);
sprintf(msg,STARTMSG_CLOSE[Config::lang],i);
VIDEO::vga.setTextColor(zxColor(7, 0), zxColor(1, 0));
VIDEO::vga.print(msg);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
VIDEO::vga.clear(zxColor(7,0));
// Disable StartMsg
Config::StartMsg = false;
// Save all keys after new flash or update
Config::save();
}
void ESPectrum::showMemInfo(const char* caption) {
string textout;
// // Get chip information
// esp_chip_info_t chip_info;
// esp_chip_info(&chip_info);
// printf(" ------------------------------------------------------------\n");
// printf(" Hardware info - %s \n", caption);
// printf(" ------------------------------------------------------------\n");
// // Chip models for ESP32
// textout = " Chip model : ";
// uint32_t chip_ver = esp_efuse_get_pkg_ver();
// uint32_t pkg_ver = chip_ver & 0x7;
// switch (pkg_ver) {
// case EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ6 :
// if (chip_info.revision == 3)
// textout += "ESP32-D0WDQ6-V3";
// else
// textout += "ESP32-D0WDQ6";
// break;
// case EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ5 :
// if (chip_info.revision == 3)
// textout += "ESP32-D0WD-V3";
// else
// textout += "ESP32-D0WD";
// break;
// case EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5 :
// textout += "ESP32-D2WD";
// break;
// case EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2 :
// textout += "ESP32-PICO-D2";
// break;
// case EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4 :
// textout += "ESP32-PICO-D4";
// break;
// case EFUSE_RD_CHIP_VER_PKG_ESP32PICOV302 :
// textout += "ESP32-PICO-V3-02";
// break;
// case EFUSE_RD_CHIP_VER_PKG_ESP32D0WDR2V3 :
// textout += "ESP32-D0WDR2-V3";
// break;
// default:
// textout += "Unknown";
// }
// textout += "\n";
// printf(textout.c_str());
// textout = " Chip cores : " + to_string(chip_info.cores) + "\n";
// printf(textout.c_str());
// textout = " Chip revision : " + to_string(chip_info.revision) + "\n";
// printf(textout.c_str());
// textout = " Flash size : " + to_string(spi_flash_get_chip_size() / (1024 * 1024)) + (chip_info.features & CHIP_FEATURE_EMB_FLASH ? "MB embedded" : "MB external") + "\n";
// printf(textout.c_str());
multi_heap_info_t info;
// heap_caps_get_info(&info, MALLOC_CAP_SPIRAM);
// uint32_t psramsize = (info.total_free_bytes + info.total_allocated_bytes) >> 10;
// textout = " PSRAM size : " + ( psramsize == 0 ? "N/A or disabled" : to_string(psramsize) + " MB") + "\n";
// printf(textout.c_str());
// textout = " IDF Version : " + (string)(esp_get_idf_version()) + "\n";
// printf(textout.c_str());
// printf("\n Memory info\n");
// printf(" ------------------------------------------------------------\n");
heap_caps_get_info(&info, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); // internal RAM, memory capable to store data or to create new task
textout = " Total free bytes : " + to_string(info.total_free_bytes) + "\n";
printf(textout.c_str());
textout = " Minimum free ever : " + to_string(info.minimum_free_bytes) + "\n";
printf(textout.c_str());
// textout = " Largest free block : " + to_string(info.largest_free_block) + "\n";
// printf(textout.c_str());
// textout = " Free (MALLOC_CAP_32BIT) : " + to_string(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_32BIT)) + "\n";
// printf(textout.c_str());
// UBaseType_t wm;
// wm = uxTaskGetStackHighWaterMark(NULL);
// textout = " Main Task Stack HWM : " + to_string(wm) + "\n";
// printf(textout.c_str());
// wm = uxTaskGetStackHighWaterMark(ESPectrum::audioTaskHandle);
// textout = " Audio Task Stack HWM : " + to_string(wm) + "\n";
// printf(textout.c_str());
// wm = uxTaskGetStackHighWaterMark(VIDEO::videoTaskHandle);
// textout = " Video Task Stack HWM : " + (Config::videomode ? to_string(wm) : "N/A") + "\n";
// printf(textout.c_str());
// printf("\n ------------------------------------------------------------\n\n");
}
//=======================================================================================
// BOOT KEYBOARD
//=======================================================================================
void ESPectrum::bootKeyboard() {
auto Kbd = PS2Controller.keyboard();
fabgl::VirtualKeyItem NextKey;
int i = 0;
string s = "00";
// printf("Boot kbd!\n");
for (; i < 200; i++) {
if (ZXKeyb::Exists) {
// Process physical keyboard
ZXKeyb::process();
// Detect and process physical kbd menu key combinations
if (!bitRead(ZXKeyb::ZXcols[3], 0)) { // 1
s[0]='1';
} else
if (!bitRead(ZXKeyb::ZXcols[3], 1)) { // 2
s[0]='2';
} else
if (!bitRead(ZXKeyb::ZXcols[3], 2)) { // 3
s[0]='3';
}
if (!bitRead(ZXKeyb::ZXcols[2], 0)) { // Q
s[1]='Q';
} else
if (!bitRead(ZXKeyb::ZXcols[2], 1)) { // W
s[1]='W';
}
}
while (Kbd->virtualKeyAvailable()) {
bool r = Kbd->getNextVirtualKey(&NextKey);
if (r && NextKey.down) {
// Check keyboard status
switch (NextKey.vk) {
case fabgl::VK_1:
s[0] = '1';
break;
case fabgl::VK_2:
s[0] = '2';
break;
case fabgl::VK_3:
s[0] = '3';
break;
case fabgl::VK_Q:
case fabgl::VK_q:
s[1] = 'Q';
break;
case fabgl::VK_W:
case fabgl::VK_w:
s[1] = 'W';
break;
}
}
}
if (s.find('0') == std::string::npos) break;
delayMicroseconds(1000);
}
// printf("Boot kbd end!\n");
if (i < 200) {
Config::videomode = (s[0] == '1') ? 0 : (s[0] == '2') ? 1 : 2;
if (Config::videomode == 2)
Config::aspect_16_9 = false; // Force 4:3 mode for CRT
else
Config::aspect_16_9 = (s[1] == 'Q') ? false : true;
Config::ram_file="none";
Config::save("videomode");
Config::save("asp169");
Config::save("ram");
// printf("%s\n", s.c_str());
}
}
//=======================================================================================
// SETUP
//=======================================================================================
void ESPectrum::setup()
{
if (Config::slog_on) {
printf("------------------------------------\n");
printf("| ESPectrum: booting |\n");
printf("------------------------------------\n");
showMemInfo();
}
//=======================================================================================
// PHYSICAL KEYBOARD (SINCLAIR 8 + 5 MEMBRANE KEYBOARD)
//=======================================================================================
ZXKeyb::setup();
//=======================================================================================
// LOAD CONFIG
//=======================================================================================
Config::load();
if (Config::StartMsg) Config::save(); // Firmware updated or reflashed: save all config data
// printf("---------------------------------\n");
// printf("Ram file: %s\n",Config::ram_file.c_str());
// printf("Arch: %s\n",Config::arch.c_str());
// printf("pref Arch: %s\n",Config::pref_arch.c_str());
// printf("romSet: %s\n",Config::romSet.c_str());
// printf("romSet48: %s\n",Config::romSet48.c_str());
// printf("romSet128: %s\n",Config::romSet128.c_str());
// printf("romSetTK90X: %s\n",Config::romSetTK90X.c_str());
// printf("romSetTK95: %s\n",Config::romSetTK95.c_str());
// printf("pref_romSet_48: %s\n",Config::pref_romSet_48.c_str());
// printf("pref_romSet_128: %s\n",Config::pref_romSet_128.c_str());
// printf("pref_romSet_TK90X: %s\n",Config::pref_romSet_TK90X.c_str());
// printf("pref_romSet_TK95: %s\n",Config::pref_romSet_TK95.c_str());
// Set arch if there's no snapshot to load
if (Config::ram_file == NO_RAM_FILE) {
if (Config::pref_arch.substr(Config::pref_arch.length()-1) == "R") {
Config::pref_arch.pop_back();
Config::save("pref_arch");
} else {
if (Config::pref_arch != "Last") Config::arch = Config::pref_arch;
if (Config::arch == "48K") {
if (Config::pref_romSet_48 != "Last")
Config::romSet = Config::pref_romSet_48;
else
Config::romSet = Config::romSet48;
} else if (Config::arch == "128K") {
if (Config::pref_romSet_128 != "Last")
Config::romSet = Config::pref_romSet_128;
else
Config::romSet = Config::romSet128;
} else if (Config::arch == "TK90X") {
if (Config::pref_romSet_TK90X != "Last")
Config::romSet = Config::pref_romSet_TK90X;
else
Config::romSet = Config::romSetTK90X;
} else if (Config::arch == "TK95") {
if (Config::pref_romSet_TK95 != "Last")
Config::romSet = Config::pref_romSet_TK95;
else
Config::romSet = Config::romSetTK95;
} else Config::romSet = "Pentagon";
printf("Arch: %s, Romset: %s\n",Config::arch.c_str(), Config::romSet.c_str());
}
}
// printf("---------------------------------\n");
// printf("Ram file: %s\n",Config::ram_file.c_str());
// printf("Arch: %s\n",Config::arch.c_str());
// printf("pref Arch: %s\n",Config::pref_arch.c_str());
// printf("romSet: %s\n",Config::romSet.c_str());
// printf("romSet48: %s\n",Config::romSet48.c_str());
// printf("romSet128: %s\n",Config::romSet128.c_str());
// printf("pref_romSet_48: %s\n",Config::pref_romSet_48.c_str());
// printf("pref_romSet_128: %s\n",Config::pref_romSet_128.c_str());
//=======================================================================================
// INIT PS/2 KEYBOARD
//=======================================================================================
ESPectrum::ps2kbd2 = (Config::ps2_dev2 != 0);
if (ZXKeyb::Exists) {
PS2Controller.begin(ps2kbd2 ? PS2Preset::KeyboardPort0 : PS2Preset::zxKeyb, KbdMode::CreateVirtualKeysQueue);
} else {
PS2Controller.begin(ps2kbd2 ? PS2Preset::KeyboardPort0_KeybJoystickPort1 : PS2Preset::KeyboardPort0, KbdMode::CreateVirtualKeysQueue);
}
ps2kbd2 &= !ZXKeyb::Exists;
// Set Scroll Lock Led as current CursorAsJoy value
PS2Controller.keyboard()->setLEDs(false, false, Config::CursorAsJoy);
if(ps2kbd2)
PS2Controller.keybjoystick()->setLEDs(false, false, Config::CursorAsJoy);
// Set TAB and GRAVEACCENT behaviour
if (Config::TABasfire1) {
ESPectrum::VK_ESPECTRUM_FIRE1 = fabgl::VK_TAB;
ESPectrum::VK_ESPECTRUM_FIRE2 = fabgl::VK_GRAVEACCENT;
ESPectrum::VK_ESPECTRUM_TAB = fabgl::VK_NONE;
ESPectrum::VK_ESPECTRUM_GRAVEACCENT = fabgl::VK_NONE;
} else {
ESPectrum::VK_ESPECTRUM_FIRE1 = fabgl::VK_NONE;
ESPectrum::VK_ESPECTRUM_FIRE2 = fabgl::VK_NONE;
ESPectrum::VK_ESPECTRUM_TAB = fabgl::VK_TAB;
ESPectrum::VK_ESPECTRUM_GRAVEACCENT = fabgl::VK_GRAVEACCENT;
}
if (Config::slog_on) {
showMemInfo("Keyboard started");
}
// Get chip information
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
Config::esp32rev = chip_info.revision;
if (Config::slog_on) {
printf("\n");
printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("IDF Version: %s\n",esp_get_idf_version());
printf("\n");
if (Config::slog_on) printf("Executing on core: %u\n", xPortGetCoreID());
showMemInfo();
}
//=======================================================================================
// BOOTKEYS: Read keyboard for 200 ms. checking boot keys
//=======================================================================================
// printf("Waiting boot keys\n");
bootKeyboard();
// printf("End Waiting boot keys\n");
//=======================================================================================
// MEMORY SETUP
//=======================================================================================
MemESP::Init();
// Load romset
Config::requestMachine(Config::arch, Config::romSet);
MemESP::Reset();
if (Config::slog_on) showMemInfo("RAM Initialized");
//=======================================================================================
// VIDEO
//=======================================================================================
VIDEO::Init();
VIDEO::Reset();
if (Config::slog_on) showMemInfo("VGA started");
if (Config::StartMsg) ShowStartMsg(); // Show welcome message
//=======================================================================================
// INIT FILESYSTEM
//=======================================================================================
FileUtils::initFileSystem();
if (Config::slog_on) showMemInfo("File system started");
//=======================================================================================
// AUDIO
//=======================================================================================
overSamplebuf = (uint32_t *) heap_caps_malloc(ESP_AUDIO_SAMPLES_PENTAGON << 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_32BIT);
if (overSamplebuf == NULL) printf("Can't allocate oversamplebuffer\n");
// Create Audio task
audioTaskQueue = xQueueCreate(1, sizeof(uint8_t *));
// Latest parameter = Core. In ESPIF, main task runs on core 0 by default. In Arduino, loop() runs on core 1.
xTaskCreatePinnedToCore(&ESPectrum::audioTask, "audioTask", 2048 /* 1024 /* 1536 */, NULL, configMAX_PRIORITIES - 1, &audioTaskHandle, 1);
// Set samples per frame and AY_emu flag depending on arch
if (Config::arch == "48K") {
samplesPerFrame=ESP_AUDIO_SAMPLES_48;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_48;
audioAYDivider = ESP_AUDIO_AY_DIV_48;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_48;
AY_emu = Config::AY48;
Audio_freq[0] = ESP_AUDIO_FREQ_48;
Audio_freq[1] = ESP_AUDIO_FREQ_48;
Audio_freq[2] = ESP_AUDIO_FREQ_48_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_48_150SPEED;
} else if (Config::arch == "TK90X" || Config::arch == "TK95") {
switch (Config::ALUTK) {
case 0:
samplesPerFrame=ESP_AUDIO_SAMPLES_48;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_48;
audioAYDivider = ESP_AUDIO_AY_DIV_48;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_48;
Audio_freq[0] = ESP_AUDIO_FREQ_48;
Audio_freq[1] = ESP_AUDIO_FREQ_48;
Audio_freq[2] = ESP_AUDIO_FREQ_48_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_48_150SPEED;
break;
case 1:
samplesPerFrame=ESP_AUDIO_SAMPLES_TK_50;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_TK_50;
audioAYDivider = ESP_AUDIO_AY_DIV_TK_50;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_TK_50;
Audio_freq[0] = ESP_AUDIO_FREQ_TK_50;
Audio_freq[1] = ESP_AUDIO_FREQ_TK_50;
Audio_freq[2] = ESP_AUDIO_FREQ_TK_50_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_TK_50_150SPEED;
break;
case 2:
samplesPerFrame=ESP_AUDIO_SAMPLES_TK_60;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_TK_60;
audioAYDivider = ESP_AUDIO_AY_DIV_TK_60;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_TK_60;
Audio_freq[0] = ESP_AUDIO_FREQ_TK_60;
Audio_freq[1] = ESP_AUDIO_FREQ_TK_60;
Audio_freq[2] = ESP_AUDIO_FREQ_TK_60_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_TK_60_150SPEED;
}
AY_emu = Config::AY48;
} else if (Config::arch == "128K") {
samplesPerFrame=ESP_AUDIO_SAMPLES_128;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_128;
audioAYDivider = ESP_AUDIO_AY_DIV_128;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_128;
AY_emu = true;
Audio_freq[0] = ESP_AUDIO_FREQ_128;
Audio_freq[1] = ESP_AUDIO_FREQ_128;
Audio_freq[2] = ESP_AUDIO_FREQ_128_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_128_150SPEED;
} else if (Config::arch == "Pentagon") {
samplesPerFrame=ESP_AUDIO_SAMPLES_PENTAGON;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_PENTAGON;
audioAYDivider = ESP_AUDIO_AY_DIV_PENTAGON;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_PENTAGON;
AY_emu = true;
Audio_freq[0] = ESP_AUDIO_FREQ_PENTAGON;
Audio_freq[1] = ESP_AUDIO_FREQ_PENTAGON;
Audio_freq[2] = ESP_AUDIO_FREQ_PENTAGON_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_PENTAGON_150SPEED;
}
if (Config::tape_player) {
AY_emu = false; // Disable AY emulation if tape player mode is set
ESPectrum::aud_volume = ESP_VOLUME_MAX;
} else
ESPectrum::aud_volume = Config::volume;
ESPoffset = 0;
// AY Sound
AySound::init();
AySound::set_sound_format(Audio_freq[ESP_delay],1,8);
AySound::set_stereo(AYEMU_MONO,NULL);
AySound::reset();
// Init tape
Tape::Init();
Tape::tapeFileName = "none";
Tape::tapeStatus = TAPE_STOPPED;
Tape::SaveStatus = SAVE_STOPPED;
Tape::romLoading = false;
if (Z80Ops::is128) { // Apply pulse length compensation for 128K
Tape::tapeCompensation = FACTOR128K;
} else if ((Config::arch=="TK90X" || Config::arch == "TK95") && Config::ALUTK > 0) { // Apply pulse length compensation for Microdigital ALU
Tape::tapeCompensation = FACTORALUTK;
} else
Tape::tapeCompensation = 1;
// Init CPU
Z80::create();
// Set Ports starting values
for (int i = 0; i < 128; i++) Ports::port[i] = 0xBF;
if (Config::joystick1 == JOY_KEMPSTON || Config::joystick2 == JOY_KEMPSTON || Config::joyPS2 == JOYPS2_KEMPSTON) Ports::port[0x1f] = 0; // Kempston
if (Config::joystick1 == JOY_FULLER || Config::joystick2 == JOY_FULLER || Config::joyPS2 == JOYPS2_FULLER) Ports::port[0x7f] = 0xff; // Fuller
// Read joystick default definition
for (int n = 0; n < 24; n++)
ESPectrum::JoyVKTranslation[n] = (fabgl::VirtualKey) Config::joydef[n];
// Init disk controller
Betadisk.Init();
// Reset cpu
CPU::reset();
// Load snapshot if present in Config::ram_file
if (Config::ram_file != NO_RAM_FILE) {
FileUtils::SNA_Path = Config::SNA_Path;
FileUtils::fileTypes[DISK_SNAFILE].begin_row = Config::SNA_begin_row;
FileUtils::fileTypes[DISK_SNAFILE].focus = Config::SNA_focus;
FileUtils::fileTypes[DISK_SNAFILE].fdMode = Config::SNA_fdMode;
FileUtils::fileTypes[DISK_SNAFILE].fileSearch = Config::SNA_fileSearch;
FileUtils::TAP_Path = Config::TAP_Path;
FileUtils::fileTypes[DISK_TAPFILE].begin_row = Config::TAP_begin_row;
FileUtils::fileTypes[DISK_TAPFILE].focus = Config::TAP_focus;
FileUtils::fileTypes[DISK_TAPFILE].fdMode = Config::TAP_fdMode;
FileUtils::fileTypes[DISK_TAPFILE].fileSearch = Config::TAP_fileSearch;
FileUtils::DSK_Path = Config::DSK_Path;
FileUtils::fileTypes[DISK_DSKFILE].begin_row = Config::DSK_begin_row;
FileUtils::fileTypes[DISK_DSKFILE].focus = Config::DSK_focus;
FileUtils::fileTypes[DISK_DSKFILE].fdMode = Config::DSK_fdMode;
FileUtils::fileTypes[DISK_DSKFILE].fileSearch = Config::DSK_fileSearch;
LoadSnapshot(Config::ram_file,"","",0xff);
Config::last_ram_file = Config::ram_file;
Config::ram_file = NO_RAM_FILE;
Config::save("ram");
}
if (Config::slog_on) showMemInfo("Setup finished.");
}
//=======================================================================================
// RESET
//=======================================================================================
void ESPectrum::reset()
{
// Ports
for (int i = 0; i < 128; i++) Ports::port[i] = 0xBF;
if (Config::joystick1 == JOY_KEMPSTON || Config::joystick2 == JOY_KEMPSTON || Config::joyPS2 == JOYPS2_KEMPSTON) Ports::port[0x1f] = 0; // Kempston
if (Config::joystick1 == JOY_FULLER || Config::joystick2 == JOY_FULLER || Config::joyPS2 == JOYPS2_FULLER) Ports::port[0x7f] = 0xff; // Fuller
// Read joystick default definition
for (int n = 0; n < 24; n++)
ESPectrum::JoyVKTranslation[n] = (fabgl::VirtualKey) Config::joydef[n];
MemESP::Reset(); // Reset Memory
VIDEO::Reset();
// Reinit disk controller
if (Config::DiskCtrl == 1 || Z80Ops::isPentagon) {
// Betadisk.ShutDown();
// Betadisk.Init();
Betadisk.EnterIdle();
}
// Tape::tapeFileName = "none";
// if (Tape::tape != NULL) {
// fclose(Tape::tape);
// Tape::tape = NULL;
// }
Tape::tapeStatus = TAPE_STOPPED;
Tape::tapePhase = TAPE_PHASE_STOPPED;
Tape::SaveStatus = SAVE_STOPPED;
Tape::romLoading = false;
if (Z80Ops::is128) { // Apply pulse length compensation for 128K
Tape::tapeCompensation = FACTOR128K;
} else if ((Config::arch=="TK90X" || Config::arch == "TK95") && Config::ALUTK > 0) { // Apply pulse length compensation for Microdigital ALU
Tape::tapeCompensation = FACTORALUTK;
} else
Tape::tapeCompensation = 1;
// Set block timings if there's a tape loaded and is a .tap
if (Tape::tape != NULL && Tape::tapeFileType == TAPE_FTYPE_TAP) {
Tape::TAP_setBlockTimings();
}
// Empty audio buffers
for (int i=0;i<ESP_AUDIO_SAMPLES_PENTAGON;i++) {
overSamplebuf[i]=0;
audioBuffer[i]=0;
AySound::SamplebufAY[i]=0;
}
lastaudioBit=0;
// Set samples per frame and AY_emu flag depending on arch
int prevAudio_freq = Audio_freq[ESP_delay];
if (Config::arch == "48K") {
samplesPerFrame=ESP_AUDIO_SAMPLES_48;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_48;
audioAYDivider = ESP_AUDIO_AY_DIV_48;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_48;
AY_emu = Config::AY48;
Audio_freq[0] = ESP_AUDIO_FREQ_48;
Audio_freq[1] = ESP_AUDIO_FREQ_48;
Audio_freq[2] = ESP_AUDIO_FREQ_48_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_48_150SPEED;
} else if (Config::arch == "TK90X" || Config::arch == "TK95") {
switch (Config::ALUTK) {
case 0:
samplesPerFrame=ESP_AUDIO_SAMPLES_48;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_48;
audioAYDivider = ESP_AUDIO_AY_DIV_48;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_48;
Audio_freq[0] = ESP_AUDIO_FREQ_48;
Audio_freq[1] = ESP_AUDIO_FREQ_48;
Audio_freq[2] = ESP_AUDIO_FREQ_48_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_48_150SPEED;
break;
case 1:
samplesPerFrame=ESP_AUDIO_SAMPLES_TK_50;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_TK_50;
audioAYDivider = ESP_AUDIO_AY_DIV_TK_50;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_TK_50;
Audio_freq[0] = ESP_AUDIO_FREQ_TK_50;
Audio_freq[1] = ESP_AUDIO_FREQ_TK_50;
Audio_freq[2] = ESP_AUDIO_FREQ_TK_50_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_TK_50_150SPEED;
break;
case 2:
samplesPerFrame=ESP_AUDIO_SAMPLES_TK_60;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_TK_60;
audioAYDivider = ESP_AUDIO_AY_DIV_TK_60;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_TK_60;
Audio_freq[0] = ESP_AUDIO_FREQ_TK_60;
Audio_freq[1] = ESP_AUDIO_FREQ_TK_60;
Audio_freq[2] = ESP_AUDIO_FREQ_TK_60_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_TK_60_150SPEED;
}
AY_emu = Config::AY48;
} else if (Config::arch == "128K") {
samplesPerFrame=ESP_AUDIO_SAMPLES_128;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_128;
audioAYDivider = ESP_AUDIO_AY_DIV_128;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_128;
AY_emu = true;
Audio_freq[0] = ESP_AUDIO_FREQ_128;
Audio_freq[1] = ESP_AUDIO_FREQ_128;
Audio_freq[2] = ESP_AUDIO_FREQ_128_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_128_150SPEED;
} else if (Config::arch == "Pentagon") {
samplesPerFrame=ESP_AUDIO_SAMPLES_PENTAGON;
audioOverSampleDivider = ESP_AUDIO_OVERSAMPLES_DIV_PENTAGON;
audioAYDivider = ESP_AUDIO_AY_DIV_PENTAGON;
audioSampleDivider = ESP_AUDIO_SAMPLES_DIV_PENTAGON;
AY_emu = true;
Audio_freq[0] = ESP_AUDIO_FREQ_PENTAGON;
Audio_freq[1] = ESP_AUDIO_FREQ_PENTAGON;
Audio_freq[2] = ESP_AUDIO_FREQ_PENTAGON_125SPEED;
Audio_freq[3] = ESP_AUDIO_FREQ_PENTAGON_150SPEED;
}
if (Config::tape_player) AY_emu = false; // Disable AY emulation if tape player mode is set
ESPoffset = 0;
// Readjust output pwmaudio frequency if needed
if (prevAudio_freq != Audio_freq[ESP_delay]) {
// printf("Resetting pwmaudio to freq: %d\n",Audio_freq);
esp_err_t res;
res = pwm_audio_set_sample_rate(Audio_freq[ESP_delay]);
if (res != ESP_OK) {
printf("Can't set sample rate\n");
}
}
// Reset AY emulation
AySound::init();
AySound::set_sound_format(Audio_freq[ESP_delay],1,8);
AySound::set_stereo(AYEMU_MONO,NULL);
AySound::reset();
CPU::reset();
}
//=======================================================================================
// KEYBOARD / KEMPSTON
//=======================================================================================
IRAM_ATTR bool ESPectrum::readKbd(fabgl::VirtualKeyItem *Nextkey) {
bool r = PS2Controller.keyboard()->getNextVirtualKey(Nextkey);
// Global keys
if (Nextkey->down) {
if (Nextkey->vk == fabgl::VK_PRINTSCREEN) { // Capture framebuffer to BMP file in SD Card (thx @dcrespo3d!)
CaptureToBmp();
r = false;
} else
if (Nextkey->vk == fabgl::VK_SCROLLLOCK) { // Change CursorAsJoy setting
Config::CursorAsJoy = !Config::CursorAsJoy;
PS2Controller.keyboard()->setLEDs(false,false,Config::CursorAsJoy);
if(ps2kbd2)
PS2Controller.keybjoystick()->setLEDs(false, false, Config::CursorAsJoy);
Config::save("CursorAsJoy");
r = false;
}
}
return r;
}
//
// Read second ps/2 port and inject on first queue
//
IRAM_ATTR void ESPectrum::readKbdJoy() {
if (ps2kbd2) {
fabgl::VirtualKeyItem NextKey;
auto KbdJoy = PS2Controller.keybjoystick();
while (KbdJoy->virtualKeyAvailable()) {
PS2Controller.keybjoystick()->getNextVirtualKey(&NextKey);
ESPectrum::PS2Controller.keyboard()->injectVirtualKey(NextKey.vk, NextKey.down, false);
}
}
}
fabgl::VirtualKey ESPectrum::JoyVKTranslation[24];
// fabgl::VK_FULLER_LEFT, // Left
// fabgl::VK_FULLER_RIGHT, // Right
// fabgl::VK_FULLER_UP, // Up
// fabgl::VK_FULLER_DOWN, // Down
// fabgl::VK_S, // Start
// fabgl::VK_M, // Mode
// fabgl::VK_FULLER_FIRE, // A
// fabgl::VK_9, // B
// fabgl::VK_SPACE, // C
// fabgl::VK_X, // X