-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathOSDMain.cpp
5595 lines (4604 loc) · 224 KB
/
OSDMain.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 "OSDMain.h"
#include "FileUtils.h"
#include "cpuESP.h"
#include "Video.h"
#include "ESPectrum.h"
#include "messages.h"
#include "Config.h"
#include "Snapshot.h"
#include "MemESP.h"
#include "Tape.h"
#include "ZXKeyb.h"
#include "AySound.h"
#include "pwm_audio.h"
#include "Z80_JLS/z80.h"
#include "roms.h"
#include "esp_system.h"
#include "esp_ota_ops.h"
#include "esp_efuse.h"
#include "soc/efuse_reg.h"
#include "fabgl.h"
#include "soc/rtc_wdt.h"
#include "esp_int_wdt.h"
#include "esp_task_wdt.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// #include "driver/uart.h"
// #include "esp_log.h"
// #include "esp_err.h"
#include <string>
using namespace std;
#define MENU_REDRAW true
#define MENU_UPDATE false
#define OSD_ERROR true
#define OSD_NORMAL false
#define OSD_W 248
#define OSD_H 184
#define OSD_MARGIN 4
extern Font Font6x8;
uint8_t OSD::cols; // Maximum columns
uint8_t OSD::mf_rows; // File menu maximum rows
unsigned short OSD::real_rows; // Real row count
uint8_t OSD::virtual_rows; // Virtual maximum rows on screen
uint16_t OSD::w; // Width in pixels
uint16_t OSD::h; // Height in pixels
uint16_t OSD::x; // X vertical position
uint16_t OSD::y; // Y horizontal position
uint16_t OSD::prev_y[5]; // Y prev. position
unsigned short OSD::menu_prevopt = 1;
string OSD::menu; // Menu string
unsigned short OSD::begin_row = 1; // First real displayed row
uint8_t OSD::focus = 1; // Focused virtual row
uint8_t OSD::last_focus = 0; // To check for changes
unsigned short OSD::last_begin_row = 0; // To check for changes
uint8_t OSD::menu_level = 0;
bool OSD::menu_saverect = false;
unsigned short OSD::menu_curopt = 1;
unsigned int OSD::SaveRectpos = 0;
unsigned short OSD::scrW = 320;
unsigned short OSD::scrH = 240;
char OSD::stats_lin1[25]; // "CPU: 00000 / IDL: 00000 ";
char OSD::stats_lin2[25]; // "FPS:000.00 / FND:000.00 ";
// // X origin to center an element with pixel_width
unsigned short OSD::scrAlignCenterX(unsigned short pixel_width) { return (scrW / 2) - (pixel_width / 2); }
// // Y origin to center an element with pixel_height
unsigned short OSD::scrAlignCenterY(unsigned short pixel_height) { return (scrH / 2) - (pixel_height / 2); }
uint8_t OSD::osdMaxRows() { return (OSD_H - (OSD_MARGIN * 2)) / OSD_FONT_H; }
uint8_t OSD::osdMaxCols() { return (OSD_W - (OSD_MARGIN * 2)) / OSD_FONT_W; }
unsigned short OSD::osdInsideX() { return scrAlignCenterX(OSD_W) + OSD_MARGIN; }
unsigned short OSD::osdInsideY() { return scrAlignCenterY(OSD_H) + OSD_MARGIN; }
static const uint8_t click48[12]={ 0,8,32,32,32,32,32,32,32,32,8,0 };
static const uint8_t click128[116] = { 0,8,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,8,0
};
IRAM_ATTR void OSD::click() {
size_t written;
if (Config::tape_player) return; // Disable interface click on tape player mode
pwm_audio_set_volume(ESP_VOLUME_MAX);
if (Z80Ops::is48)
pwm_audio_write((uint8_t *) click48, 12, &written, 5 / portTICK_PERIOD_MS);
else
pwm_audio_write((uint8_t *) click128, 116, &written, 5 / portTICK_PERIOD_MS);
pwm_audio_set_volume(ESPectrum::aud_volume);
}
void OSD::esp_hard_reset() {
// RESTART ESP32 (This is the most similar way to hard resetting it)
rtc_wdt_protect_off();
rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_RTC);
rtc_wdt_set_time(RTC_WDT_STAGE0, 100);
rtc_wdt_enable();
rtc_wdt_protect_on();
while (true);
}
// // Cursor to OSD first row,col
void OSD::osdHome() { VIDEO::vga.setCursor(osdInsideX(), osdInsideY()); }
// // Cursor positioning
void OSD::osdAt(uint8_t row, uint8_t col) {
if (row > osdMaxRows() - 1)
row = 0;
if (col > osdMaxCols() - 1)
col = 0;
unsigned short y = (row * OSD_FONT_H) + osdInsideY();
unsigned short x = (col * OSD_FONT_W) + osdInsideX();
VIDEO::vga.setCursor(x, y);
}
void OSD::drawWindow(uint16_t width, uint16_t height, string top, string bottom, bool clear) {
unsigned short x = scrAlignCenterX(width);
unsigned short y = scrAlignCenterY(height);
if (clear) VIDEO::vga.fillRect(x, y, width, height, zxColor(0, 0));
VIDEO::vga.rect(x, y, width , height , zxColor(0, 0));
VIDEO::vga.rect(x + 1, y + 1, width - 2, height - 2, zxColor(7, 0));
if (top != "") {
VIDEO::vga.rect(x + 3, y + 3, width - 6, 9, zxColor(5, 0));
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
VIDEO::vga.setFont(Font6x8);
VIDEO::vga.setCursor(x + 3, y + 4);
VIDEO::vga.print(top.c_str());
}
if (bottom != "") {
VIDEO::vga.rect(x + 3, y + height - 12, width - 6, 9, zxColor(5, 0));
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
VIDEO::vga.setFont(Font6x8);
VIDEO::vga.setCursor(x + 3, y + height - 11);
VIDEO::vga.print(bottom.c_str());
}
}
void OSD::drawOSD(bool bottom_info) {
unsigned short x = scrAlignCenterX(OSD_W);
unsigned short y = scrAlignCenterY(OSD_H);
VIDEO::vga.fillRect(x, y, OSD_W, OSD_H, zxColor(1, 0));
VIDEO::vga.rect(x, y, OSD_W, OSD_H, zxColor(0, 0));
VIDEO::vga.rect(x + 1, y + 1, OSD_W - 2, OSD_H - 2, zxColor(7, 0));
VIDEO::vga.setTextColor(zxColor(0, 0), zxColor(5, 1));
VIDEO::vga.setFont(Font6x8);
osdHome();
VIDEO::vga.print(OSD_TITLE);
osdAt(21, 0);
if (bottom_info) {
string bottom_line;
switch(Config::videomode) {
case 0: bottom_line = " Video mode: Standard VGA "; break;
case 1: bottom_line = Config::arch[0] == 'T' && Config::ALUTK == 2 ? " Video mode: VGA 60hz " : " Video mode: VGA 50hz "; break;
case 2: bottom_line = Config::arch[0] == 'T' && Config::ALUTK == 2 ? " Video mode: CRT 60hz " : " Video mode: CRT 50hz "; break;
}
VIDEO::vga.print(bottom_line.append(EMU_VERSION).c_str());
} else VIDEO::vga.print(OSD_BOTTOM);
osdHome();
}
void OSD::drawKbdLayout(uint8_t layout) {
uint8_t *layoutdata;
string vmode;
string bottom[5]={
" P PS/2 | T TK | Z ZX | 8 ZX81 ", // ZX Spectrum 48K layout
" 4 48K | P PS/2 | Z ZX | 8 ZX81 ", // TK 90x layout
" 4 48K | T TK | Z ZX | 8 ZX81 ", // PS/2 kbd help
" 4 48K | T TK | P PS/2 | 8 ZX81 ", // ZX kbd help
" 4 48K | T TK | Z ZX | P PS/2 " // ZX81+ layout
};
switch(Config::videomode) {
case 0: vmode = " Mode VGA "; break;
case 1: vmode = Config::arch[0] == 'T' && Config::ALUTK == 2 ? "Mode VGA60 " : "Mode VGA50 "; break;
case 2: vmode = Config::arch[0] == 'T' && Config::ALUTK == 2 ? "Mode CRT60 " : "Mode CRT50 "; break;
}
for(int i=0; i<5; i++) bottom[i] += vmode;
fabgl::VirtualKeyItem Nextkey;
drawWindow(256 + 8, 176 + 18, "", bottom[layout], true);
while (1) {
// Decode Logo in EBF8 format
switch (layout) {
case 0:
layoutdata = (uint8_t *)Layout_ZX;
break;
case 1:
layoutdata = (uint8_t *)Layout_TK;
break;
case 2:
layoutdata = (uint8_t *)PS2_Kbd;
break;
case 3:
layoutdata = (uint8_t *)ZX_Kbd;
break;
case 4:
layoutdata = (uint8_t *)Layout_ZX81;
break;
}
int pos_x, pos_y;
if (Config::videomode == 2) {
if (Config::arch[0] == 'T' && Config::ALUTK == 2) {
pos_x = 48;
pos_y = 19;
} else {
pos_x = 48;
pos_y = 43;
}
} else {
pos_x = Config::aspect_16_9 ? 52 : 32;
pos_y = Config::aspect_16_9 ? 7 : 27;
}
int l_w = (layoutdata[5] << 8) + layoutdata[4]; // Get Width
int l_h = (layoutdata[7] << 8) + layoutdata[6]; // Get Height
layoutdata += 8; // Skip header
for (int i=0; i < l_h; i++)
for(int n=0; n<l_w; n++)
VIDEO::vga.dotFast(pos_x + n,pos_y + i,layoutdata[n+(i*l_w)]);
while (1) {
if (ZXKeyb::Exists) ZXKeyb::ZXKbdRead();
ESPectrum::readKbdJoy();
if (ESPectrum::PS2Controller.keyboard()->virtualKeyAvailable()) {
if (ESPectrum::readKbd(&Nextkey)) {
if(!Nextkey.down) continue;
if (Nextkey.vk == fabgl::VK_F1 ||
Nextkey.vk == fabgl::VK_ESCAPE ||
Nextkey.vk == fabgl::VK_RETURN ||
Nextkey.vk == fabgl::VK_JOY1A ||
Nextkey.vk == fabgl::VK_JOY1B ||
Nextkey.vk == fabgl::VK_JOY2A ||
Nextkey.vk == fabgl::VK_JOY2B ||
Nextkey.vk == fabgl::VK_4 ||
Nextkey.vk == fabgl::VK_8 ||
Nextkey.vk == fabgl::VK_T ||
Nextkey.vk == fabgl::VK_t ||
Nextkey.vk == fabgl::VK_P ||
Nextkey.vk == fabgl::VK_p ||
Nextkey.vk == fabgl::VK_Z ||
Nextkey.vk == fabgl::VK_z
) break;
}
}
vTaskDelay(5 / portTICK_PERIOD_MS);
}
if (Nextkey.vk == fabgl::VK_F1 || Nextkey.vk == fabgl::VK_ESCAPE || Nextkey.vk == fabgl::VK_RETURN || Nextkey.vk == fabgl::VK_JOY1A || Nextkey.vk == fabgl::VK_JOY1B || Nextkey.vk == fabgl::VK_JOY2A || Nextkey.vk == fabgl::VK_JOY2B) break;
switch(Nextkey.vk) {
case fabgl::VK_4:
layout = 0;
break;
case fabgl::VK_8:
layout = 4;
break;
case fabgl::VK_T:
case fabgl::VK_t:
layout = 1;
break;
case fabgl::VK_P:
case fabgl::VK_p:
layout = 2;
break;
case fabgl::VK_Z:
case fabgl::VK_z:
layout = 3;
};
drawWindow(256 + 8, 176 + 18, "", bottom[layout], false);
}
click();
if (VIDEO::OSD) OSD::drawStats(); // Redraw stats for 16:9 modes
}
// void OSD::UART_test() {
// string bottom= " UART TEST ";
// fabgl::VirtualKeyItem Nextkey;
// FILE *fichero;
// string f_uart = FileUtils::MountPoint + "/uartest.tap";
// fichero = fopen(f_uart.c_str(), "wb");
// if (fichero == NULL)
// {
// return;
// }
// drawWindow(256 + 8, 176 + 18, "", bottom, true);
// #define UART_BUF_SIZE 1024
// static const char *TAG = "UART TEST";
// /* Configure parameters of an UART driver,
// * communication pins and install the driver */
// uart_config_t uart_config = {
// .baud_rate = 460800,
// .data_bits = UART_DATA_8_BITS,
// .parity = UART_PARITY_DISABLE,
// .stop_bits = UART_STOP_BITS_1,
// .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
// .source_clk = UART_SCLK_APB,
// };
// int intr_alloc_flags = 0;
// #if CONFIG_UART_ISR_IN_IRAM
// intr_alloc_flags = ESP_INTR_FLAG_IRAM;
// #endif
// ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, UART_BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
// ESP_ERROR_CHECK(uart_param_config(UART_NUM_0, &uart_config));
// // ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, 4, 5, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
// uint8_t *data;
// // Configure a temporary buffer for the incoming data
// data = (uint8_t *) malloc(UART_BUF_SIZE);
// while (1) {
// // Read data from the UART
// int len = uart_read_bytes(UART_NUM_0, data, (UART_BUF_SIZE - 1), 100 / portTICK_PERIOD_MS);
// // // Write data back to the UART
// // uart_write_bytes(UART_NUM_0, (const char *) data, len);
// if (len) {
// // Write data to file
// fwrite(data, 1, len, fichero);
// // data[len] = '\0';
// // ESP_LOGI(TAG, "Recv str: %s", (char *) data);
// }
// if (ZXKeyb::Exists) ZXKeyb::ZXKbdRead();
// ESPectrum::readKbdJoy();
// if (ESPectrum::PS2Controller.keyboard()->virtualKeyAvailable()) {
// if (ESPectrum::readKbd(&Nextkey)) {
// if(!Nextkey.down) continue;
// if (Nextkey.vk == fabgl::VK_F1 ||
// Nextkey.vk == fabgl::VK_ESCAPE ||
// Nextkey.vk == fabgl::VK_RETURN ||
// Nextkey.vk == fabgl::VK_JOY1A ||
// Nextkey.vk == fabgl::VK_JOY1B ||
// Nextkey.vk == fabgl::VK_JOY2A ||
// Nextkey.vk == fabgl::VK_JOY2B
// ) break;
// }
// }
// vTaskDelay(5 / portTICK_PERIOD_MS);
// if (Nextkey.vk == fabgl::VK_F1 || Nextkey.vk == fabgl::VK_ESCAPE || Nextkey.vk == fabgl::VK_RETURN || Nextkey.vk == fabgl::VK_JOY1A || Nextkey.vk == fabgl::VK_JOY1B || Nextkey.vk == fabgl::VK_JOY2A || Nextkey.vk == fabgl::VK_JOY2B) break;
// }
// free(data);
// uart_driver_delete(UART_NUM_0);
// fclose(fichero);
// click();
// if (VIDEO::OSD) OSD::drawStats(); // Redraw stats for 16:9 modes
// }
void OSD::drawStats() {
unsigned short x,y;
if (Config::aspect_16_9) {
x = 156;
y = 176;
} else {
x = 168;
y = VIDEO::brdlin_osdstart;
}
VIDEO::vga.setTextColor(zxColor(7, 0), zxColor( ESPectrum::ESP_delay, 0));
VIDEO::vga.setFont(Font6x8);
VIDEO::vga.setCursor(x,y);
VIDEO::vga.print(stats_lin1);
VIDEO::vga.setCursor(x,y+8);
VIDEO::vga.print(stats_lin2);
}
static bool persistSave(uint8_t slotnumber) {
struct stat stat_buf;
char persistfname[sizeof(DISK_PSNA_FILE) + 7];
char persistfinfo[sizeof(DISK_PSNA_FILE) + 7];
// printf(DISK_PSNA_FILE "%u.sna\n",slotnumber);
// printf(DISK_PSNA_FILE "%u.esp\n",slotnumber);
sprintf(persistfname,DISK_PSNA_FILE "%u.sna",slotnumber);
sprintf(persistfinfo,DISK_PSNA_FILE "%u.esp",slotnumber);
string finfo = FileUtils::MountPoint + DISK_PSNA_DIR + "/" + persistfinfo;
// Create dir if it doesn't exist
string dir = FileUtils::MountPoint + DISK_PSNA_DIR;
if (stat(dir.c_str(), &stat_buf) != 0) {
if (mkdir(dir.c_str(),0775) != 0) {
printf("persistSave: problem creating persist save dir\n");
return false;
}
}
// Slot isn't void
if (stat(finfo.c_str(), &stat_buf) == 0) {
string title = OSD_PSNA_SAVE[Config::lang];
string msg = OSD_PSNA_EXISTS[Config::lang];
uint8_t res = OSD::msgDialog(title,msg);
if (res != DLG_YES) return false;
}
OSD::osdCenteredMsg(OSD_PSNA_SAVING, LEVEL_INFO, 0);
// Save info file
FILE *f = fopen(finfo.c_str(), "w");
if (f == NULL) {
printf("Error opening %s\n",persistfinfo);
return false;
} else {
string persist_ALUTK = "255";
if (Config::arch[0] == 'T') persist_ALUTK = std::to_string(Config::ALUTK);
fputs((Config::arch + "\n" + Config::romSet + "\n" + persist_ALUTK + "\n").c_str(),f); // Put architecture, romset and ALUTK on info file
fclose(f);
if (!FileSNA::save(FileUtils::MountPoint + DISK_PSNA_DIR + "/" + persistfname)) OSD::osdCenteredMsg(OSD_PSNA_SAVE_ERR, LEVEL_WARN);
}
return true;
}
static bool persistLoad(uint8_t slotnumber) {
char persistfname[sizeof(DISK_PSNA_FILE) + 7];
char persistfinfo[sizeof(DISK_PSNA_FILE) + 7];
sprintf(persistfname,DISK_PSNA_FILE "%u.sna",slotnumber);
sprintf(persistfinfo,DISK_PSNA_FILE "%u.esp",slotnumber);
if (!FileSNA::isPersistAvailable(FileUtils::MountPoint + DISK_PSNA_DIR + "/" + persistfname)) {
OSD::osdCenteredMsg(OSD_PSNA_NOT_AVAIL, LEVEL_INFO);
return false;
} else {
// Read info file
string finfo = FileUtils::MountPoint + DISK_PSNA_DIR + "/" + persistfinfo;
FILE *f = fopen(finfo.c_str(), "r");
if (f == NULL) {
OSD::osdCenteredMsg(OSD_PSNA_LOAD_ERR, LEVEL_WARN);
// printf("Error opening %s\n",persistfinfo);
return false;
}
char buf[256];
char *result;
string persist_arch = "";
string persist_romset = "";
string persist_ALUTK = "255";
if ((result = fgets(buf, sizeof(buf),f)) != NULL) {
persist_arch = buf;
persist_arch.pop_back();
printf("[%s]\n",persist_arch.c_str());
}
if ((result = fgets(buf, sizeof(buf),f)) != NULL) {
persist_romset = buf;
persist_romset.pop_back();
printf("[%s]\n",persist_romset.c_str());
}
if ((result = fgets(buf, sizeof(buf),f)) != NULL) {
persist_ALUTK = buf;
persist_ALUTK.pop_back();
printf("[%s]\n",persist_ALUTK.c_str());
}
fclose(f);
if (!LoadSnapshot(FileUtils::MountPoint + DISK_PSNA_DIR + "/" + persistfname, persist_arch, persist_romset, std::stoi(persist_ALUTK))) {
OSD::osdCenteredMsg(OSD_PSNA_LOAD_ERR, LEVEL_WARN);
return false;
} else {
Config::ram_file = FileUtils::MountPoint + DISK_PSNA_DIR + "/" + persistfname;
Config::last_ram_file = Config::ram_file;
}
}
return true;
}
#ifdef PERSIST_RENAME // WIP
static string getStringPersistCatalog()
{
char buffer[33] = {0}; // Buffer to store each line, extra char for null-terminator
string catalog = "";
const std::string catalogPath = FileUtils::MountPoint + DISK_PSNA_DIR + "/" + "catalog";
FILE *catalogFile = fopen(catalogPath.c_str(), "rb+");
if (!catalogFile) {
catalogFile = fopen(catalogPath.c_str(), "wb+");
}
fseek(catalogFile, 0, SEEK_END);
long catalogSize = ftell( catalogFile );
for(int i=1; i <= 100; i++) {
// Move to the correct position in the catalog file
if (fseek(catalogFile, (i - 1) * 32, SEEK_SET) == 0) {
size_t bytesRead = fread(buffer, 1, 32, catalogFile);
if ( feof( catalogFile ) || ferror( catalogFile ) ) bytesRead = 0;
buffer[bytesRead] = '\0'; // Ensure null-terminated string
int readed = bytesRead > 0;
while( bytesRead-- ) {
if ( buffer[ bytesRead ] != ' ' &&
buffer[ bytesRead ] != '\t' &&
buffer[ bytesRead ] != '\n' ) break;
buffer[ bytesRead ] = '\0';
}
if ( buffer[0] ) {
catalog += std::string(buffer) + "\n";
} else {
string item = (Config::lang == 1 ? "<Ranura " : "<Slot ") + to_string(i) + ">";
if ( !readed ) {
memset( buffer, '\0', sizeof( buffer ));
fwrite(buffer, 1, 32, catalogFile);
}
catalog += item + "\n";
}
}
}
/*
std::string persistPath = FileUtils::MountPoint + DISK_PSNA_DIR + "/" + "persist" + to_string(i);
struct stat buffer;
if (!stat((persistPath + ".sna").c_str(), &buffer)) {
*/
fclose(catalogFile);
return catalog;
}
#else
static string getStringPersistCatalog() {
string catalog = "";
for(int i=1; i <= 100; i++) {
catalog += (Config::lang == 1 ? "Ranura " : "Slot ") + to_string(i) + "\n";
}
return catalog;
}
#endif
// *******************************************************************************************************
// PREFERRED ROM MENU
// *******************************************************************************************************
void OSD::pref_rom_menu() {
menu_curopt = 1;
menu_saverect = true;
while (1) {
menu_level = 2;
uint8_t opt2 = menuRun(MENU_ROM_PREF[Config::lang]);
if (opt2) {
menu_level = 3;
menu_curopt = 1;
menu_saverect = true;
if (opt2 == 1) {
const string menu_res[] = {"48K","48Kes","48Kcs","Last"};
while (1) {
string rpref_menu = MENU_ROM_PREF_48[Config::lang];
menu_curopt = prepare_checkbox_menu(rpref_menu,Config::pref_romSet_48);
int opt3 = menuRun(rpref_menu);
menu_saverect = false;
if (opt3 == 0) break;
if (opt3 != menu_curopt) {
Config::pref_romSet_48 = menu_res[opt3 - 1];
Config::save("pref_romSet_48");
}
}
} else if (opt2 == 2) {
const string menu_res[] = {"128K","128Kes","+2","+2es","ZX81+","128Kcs","Last"};
while (1) {
string rpref_menu = MENU_ROM_PREF_128[Config::lang];
menu_curopt = prepare_checkbox_menu(rpref_menu,Config::pref_romSet_128);
int opt3 = menuRun(rpref_menu);
menu_saverect = false;
if (opt3 == 0) break;
if (opt3 != menu_curopt) {
Config::pref_romSet_128 = menu_res[opt3 - 1];
Config::save("pref_romSet_128");
}
}
} else if (opt2 == 3) {
const string menu_res[] = {"v1es","v1pt","v2es","v2pt","v3es","v3pt","v3en","TKcs","Last"};
while (1) {
string rpref_menu = MENU_ROM_PREF_TK90X[Config::lang];
menu_curopt = prepare_checkbox_menu(rpref_menu,Config::pref_romSet_TK90X);
int opt3 = menuRun(rpref_menu);
menu_saverect = false;
if (opt3 == 0) break;
if (opt3 != menu_curopt) {
Config::pref_romSet_TK90X = menu_res[opt3 - 1];
Config::save("pref_romSet_TK90X");
}
}
} else if (opt2 == 4) {
const string menu_res[] = {"95es","95pt","Last"};
while (1) {
string rpref_menu = MENU_ROM_PREF_TK95[Config::lang];
menu_curopt = prepare_checkbox_menu(rpref_menu,Config::pref_romSet_TK95);
int opt3 = menuRun(rpref_menu);
menu_saverect = false;
if (opt3 == 0) break;
if (opt3 != menu_curopt) {
Config::pref_romSet_TK95 = menu_res[opt3 - 1];
Config::save("pref_romSet_TK95");
}
}
}
menu_curopt = opt2;
menu_saverect = false;
} else
break;
}
menu_curopt = 3;
}
// OSD Main Loop
void OSD::do_OSD(fabgl::VirtualKey KeytoESP, bool CTRL, bool SHIFT) {
static uint8_t last_sna_row = 0;
fabgl::VirtualKeyItem Nextkey;
if (SHIFT && !CTRL) {
if (KeytoESP == fabgl::VK_F1) { // Show H/W info
OSD::HWInfo();
} else
// if (KeytoESP == fabgl::VK_F5) { // UART test
// OSD::UART_test();
// } else
if (KeytoESP == fabgl::VK_F6) { // Eject tape
// Eject Tape
click();
if (Tape::tapeFileName=="none") {
OSD::osdCenteredMsg(OSD_TAPE_SELECT_ERR[Config::lang], LEVEL_WARN);
menu_saverect = false;
} else {
Tape::Eject();
osdCenteredMsg(OSD_TAPE_EJECT[Config::lang], LEVEL_INFO, 1000);
}
}
} else if (CTRL && !SHIFT) {
// if (KeytoESP == fabgl::VK_F11) { // Toggle snow effect
// VIDEO::snow_toggle ^= 0X01;
// if (VIDEO::snow_toggle) {
// VIDEO::Draw = &VIDEO::MainScreen_Blank_Snow;
// VIDEO::Draw_Opcode = &VIDEO::MainScreen_Blank_Snow_Opcode;
// } else {
// VIDEO::Draw = &VIDEO::MainScreen_Blank;
// VIDEO::Draw_Opcode = &VIDEO::MainScreen_Blank_Opcode;
// }
// } else
if (KeytoESP == fabgl::VK_F1) { // Show kbd layout
uint8_t layout = 0;
if (Config::arch[0] == 'T') {
layout = 1;
} else if (Config::arch == "128K" && Config::romSet == "ZX81+") {
layout = 4;
}
OSD::drawKbdLayout(layout);
} else
if (KeytoESP == fabgl::VK_F2) { // Turbo mode
++ESPectrum::ESP_delay &= 0x03;
if (ESPectrum::ESP_delay) {
// Empty audio buffers
for (int i=0;i<ESP_AUDIO_SAMPLES_PENTAGON;i++) {
ESPectrum::overSamplebuf[i]=0;
ESPectrum::audioBuffer[i]=0;
AySound::SamplebufAY[i]=0;
}
ESPectrum::lastaudioBit=0;
ESPectrum::ESPoffset = 0;
// printf("Resetting pwmaudio to freq: %d\n",Audio_freq);
esp_err_t res;
res = pwm_audio_set_sample_rate(ESPectrum::Audio_freq[ESPectrum::ESP_delay]);
if (res != ESP_OK) {
printf("Can't set sample rate\n");
}
// Reset AY emulation
AySound::init();
AySound::set_sound_format(ESPectrum::Audio_freq[ESPectrum::ESP_delay],1,8);
AySound::set_stereo(AYEMU_MONO,NULL);
AySound::reset();
}
} else
if (KeytoESP == fabgl::VK_F9) { // Input Poke
pokeDialog();
} else
if (KeytoESP == fabgl::VK_F10) { // NMI
Z80::triggerNMI();
} else
if (KeytoESP == fabgl::VK_F11) { // Reset to TR-DOS (48K only)
if (Config::DiskCtrl || Z80Ops::isPentagon) {
if (Config::ram_file != NO_RAM_FILE) {
Config::ram_file = NO_RAM_FILE;
}
Config::last_ram_file = NO_RAM_FILE;
ESPectrum::reset();
if (Z80Ops::is128 || Z80Ops::isPentagon) MemESP::romLatch = 1;
MemESP::romInUse = 4;
MemESP::ramCurrent[0] = MemESP::rom[MemESP::romInUse];
ESPectrum::trdos = true;
} else {
OSD::osdCenteredMsg(TRDOS_RESET_ERR[Config::lang], LEVEL_ERROR, 1500 );
}
} else
// if (KeytoESP == fabgl::VK_F3) {
// // Test variable decrease
// ESPectrum::ESPtestvar -= 1;
// printf("ESPtestvar: %d\n",ESPectrum::ESPtestvar);
// } else
// if (KeytoESP == fabgl::VK_F4) {
// // Test variable increase
// ESPectrum::ESPtestvar += 1;
// printf("ESPtestvar: %d\n",ESPectrum::ESPtestvar);
// } else
// if (KeytoESP == fabgl::VK_F5) {
// // Test variable decrease
// ESPectrum::ESPtestvar1 -= 1;
// printf("ESPtestvar1: %d\n",ESPectrum::ESPtestvar1);
// } else
// if (KeytoESP == fabgl::VK_F6) {
// // Test variable increase
// ESPectrum::ESPtestvar1 += 1;
// printf("ESPtestvar1: %d\n",ESPectrum::ESPtestvar1);
// } else
// if (KeytoESP == fabgl::VK_F7) {
// // Test variable decrease
// ESPectrum::ESPtestvar2 -= 1;
// printf("ESPtestvar2: %d\n",ESPectrum::ESPtestvar2);
// } else
// if (KeytoESP == fabgl::VK_F8) {
// // Test variable increase
// ESPectrum::ESPtestvar2 += 1;
// printf("ESPtestvar2: %d\n",ESPectrum::ESPtestvar2);
// }
if (KeytoESP == fabgl::VK_F5) {
if (Config::CenterH > -16) Config::CenterH--;
Config::save("CenterH");
osdCenteredMsg("Horiz. center: " + to_string(Config::CenterH), LEVEL_INFO, 375);
} else
if (KeytoESP == fabgl::VK_F6) {
if (Config::CenterH < 16) Config::CenterH++;
Config::save("CenterH");
osdCenteredMsg("Horiz. center: " + to_string(Config::CenterH), LEVEL_INFO, 375);
} else
if (KeytoESP == fabgl::VK_F7) {
if (Config::CenterV > -16) Config::CenterV--;
Config::save("CenterV");
osdCenteredMsg("Vert. center: " + to_string(Config::CenterV), LEVEL_INFO, 375);
} else
if (KeytoESP == fabgl::VK_F8) {
if (Config::CenterV < 16) Config::CenterV++;
Config::save("CenterV");
osdCenteredMsg("Vert. center: " + to_string(Config::CenterV), LEVEL_INFO, 375);
}
} else {
if (KeytoESP == fabgl::VK_PAUSE) {
click();
osdCenteredMsg(OSD_PAUSE[Config::lang], LEVEL_INFO, 1000);
while (1) {
if (ZXKeyb::Exists) ZXKeyb::ZXKbdRead();
ESPectrum::readKbdJoy();
if (ESPectrum::PS2Controller.keyboard()->virtualKeyAvailable()) {
if (ESPectrum::readKbd(&Nextkey)) {
if(!Nextkey.down) continue;
if (Nextkey.vk == fabgl::VK_RETURN || Nextkey.vk == fabgl::VK_ESCAPE || Nextkey.vk == fabgl::VK_JOY1A || Nextkey.vk == fabgl::VK_JOY2A || Nextkey.vk == fabgl::VK_JOY1B || Nextkey.vk == fabgl::VK_JOY2B || Nextkey.vk == fabgl::VK_PAUSE) {
click();
break;
} else
osdCenteredMsg(OSD_PAUSE[Config::lang], LEVEL_INFO, 500);
}
}
vTaskDelay(5 / portTICK_PERIOD_MS);
}
}
else if (KeytoESP == fabgl::VK_F2) {
menu_level = 0;
menu_saverect = false;
if (FileUtils::isSDReady()) {
// ESPectrum::showMemInfo("Before F2 file dialog");
string mFile = fileDialog(FileUtils::SNA_Path, MENU_SNA_TITLE[Config::lang],DISK_SNAFILE,51,22);
// ESPectrum::showMemInfo("After F2 file dialog");
if (mFile != "") {
string fprefix = mFile.substr(0,1);
if (fprefix == "S") FileZ80::keepArch = true;
mFile.erase(0, 1);
string fname = FileUtils::MountPoint + FileUtils::SNA_Path + "/" + mFile;
LoadSnapshot(fname,"","",0xff);
Config::ram_file = fname;