-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathSnapshot.cpp
1678 lines (1273 loc) · 55.3 KB
/
Snapshot.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 "Snapshot.h"
#include "hardconfig.h"
#include "FileUtils.h"
#include "Config.h"
#include "cpuESP.h"
#include "Video.h"
#include "MemESP.h"
#include "ESPectrum.h"
#include "Ports.h"
#include "messages.h"
#include "OSDMain.h"
#include "Tape.h"
#include "AySound.h"
#include "pwm_audio.h"
#include "Z80_JLS/z80.h"
#include "Config.h"
#include "Tape.h"
#include "pwm_audio.h"
#include "AySound.h"
#include "loaders.h"
#include "ZXKeyb.h"
#include "Config.h"
#include <sys/unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <inttypes.h>
#include <string>
using namespace std;
// Change running snapshot
bool LoadSnapshot(string filename, string force_arch, string force_romset, uint8_t force_ALU) {
bool res = false;
uint8_t OSDprev = VIDEO::OSD;
if (FileUtils::hasSNAextension(filename)) {
// OSD::osdCenteredMsg(MSG_LOADING_SNA + (string) ": " + filename.substr(filename.find_last_of("/") + 1), LEVEL_INFO, 0);
res = FileSNA::load(filename, force_arch, force_romset, force_ALU);
} else if (FileUtils::hasZ80extension(filename)) {
// OSD::osdCenteredMsg(MSG_LOADING_Z80 + (string) ": " + filename.substr(filename.find_last_of("/") + 1), LEVEL_INFO, 0);
res = FileZ80::load(filename);
} else if (FileUtils::hasExtension(filename, "sp")) {
res = FileSP::load(filename);
} else if (FileUtils::hasPextension(filename)) {
res = FileP::load(filename);
}
if (res && OSDprev) {
VIDEO::OSD = OSDprev;
if (Config::aspect_16_9)
VIDEO::Draw_OSD169 = VIDEO::MainScreen_OSD;
else
VIDEO::Draw_OSD43 = Z80Ops::isPentagon ? VIDEO::BottomBorder_OSD_Pentagon : VIDEO::BottomBorder_OSD;
ESPectrum::TapeNameScroller = 0;
}
return res;
}
// ///////////////////////////////////////////////////////////////////////////////
bool FileSNA::load(string sna_fn, string force_arch, string force_romset, uint8_t force_ALU) {
FILE *file;
int sna_size;
string snapshotArch;
file = fopen(sna_fn.c_str(), "rb");
if (file==NULL)
{
printf("FileSNA: Error opening %s\n",sna_fn.c_str());
return false;
}
printf("Force arch: %s, Force romset: %s, Force ALU: %d\n", force_arch.c_str(),force_romset.c_str(),force_ALU);
fseek(file,0,SEEK_END);
sna_size = ftell(file);
rewind (file);
// Check snapshot arch
if (sna_size == SNA_48K_SIZE || sna_size == SNA_48K_WITH_ROM_SIZE) {
if (force_arch == "" && !Z80Ops::is48) {
force_arch = "48K";
}
// // If using some 48K arch it keeps unmodified. If not, we choose 48k because is SNA format default
// if (ConfigZ80Ops::is48)
// snapshotArch = Config::arch;
// else
// snapshotArch = "48K";
} else if ((sna_size == SNA_128K_SIZE1) || (sna_size == SNA_128K_SIZE2)) {
if (force_arch == "" && Z80Ops::is48) {
force_arch = "Pentagon";
}
// // If using some 128K arch it keeps unmodified. If not, we choose Pentagon because is SNA format default
// if (!Z80Ops::is48)
// snapshotArch = Config::arch;
// else
// snapshotArch = "Pentagon";
} else {
printf("FileSNA::load: bad SNA %s: size = %d\n", sna_fn.c_str(), sna_size);
return false;
}
// Change arch if needed
if (force_arch != "" && force_arch != Config::arch) {
bool vreset = Config::videomode;
// If switching between TK models there's no need to reset in vidmodes > 0
if (force_arch[0] == 'T' && Config::arch[0] == 'T') vreset = false;
Config::requestMachine(force_arch, force_romset);
// Condition this to 50hz mode
if (vreset) {
Config::SNA_Path = FileUtils::SNA_Path;
Config::SNA_begin_row = FileUtils::fileTypes[DISK_SNAFILE].begin_row;
Config::SNA_focus = FileUtils::fileTypes[DISK_SNAFILE].focus;
Config::SNA_fdMode = FileUtils::fileTypes[DISK_SNAFILE].fdMode;
Config::SNA_fileSearch = FileUtils::fileTypes[DISK_SNAFILE].fileSearch;
Config::TAP_Path = FileUtils::TAP_Path;
Config::TAP_begin_row = FileUtils::fileTypes[DISK_TAPFILE].begin_row;
Config::TAP_focus = FileUtils::fileTypes[DISK_TAPFILE].focus;
Config::TAP_fdMode = FileUtils::fileTypes[DISK_TAPFILE].fdMode;
Config::TAP_fileSearch = FileUtils::fileTypes[DISK_TAPFILE].fileSearch;
Config::DSK_Path = FileUtils::DSK_Path;
Config::DSK_begin_row = FileUtils::fileTypes[DISK_DSKFILE].begin_row;
Config::DSK_focus = FileUtils::fileTypes[DISK_DSKFILE].focus;
Config::DSK_fdMode = FileUtils::fileTypes[DISK_DSKFILE].fdMode;
Config::DSK_fileSearch = FileUtils::fileTypes[DISK_DSKFILE].fileSearch;
Config::ram_file = sna_fn;
Config::save();
OSD::esp_hard_reset();
}
} else if (force_romset != "" && force_romset != Config::romSet) {
Config::requestMachine(Config::arch, force_romset);
}
// // Manage arch change
// if (Z80Ops::is128) { // If we are on 128K machine
// if (snapshotArch == "48K") {
// Config::requestMachine("48K", force_romset);
// // Condition this to 50hz mode
// if(Config::videomode) {
// Config::SNA_Path = FileUtils::SNA_Path;
// Config::SNA_begin_row = FileUtils::fileTypes[DISK_SNAFILE].begin_row;
// Config::SNA_focus = FileUtils::fileTypes[DISK_SNAFILE].focus;
// Config::SNA_fdMode = FileUtils::fileTypes[DISK_SNAFILE].fdMode;
// Config::SNA_fileSearch = FileUtils::fileTypes[DISK_SNAFILE].fileSearch;
// Config::TAP_Path = FileUtils::TAP_Path;
// Config::TAP_begin_row = FileUtils::fileTypes[DISK_TAPFILE].begin_row;
// Config::TAP_focus = FileUtils::fileTypes[DISK_TAPFILE].focus;
// Config::TAP_fdMode = FileUtils::fileTypes[DISK_TAPFILE].fdMode;
// Config::TAP_fileSearch = FileUtils::fileTypes[DISK_TAPFILE].fileSearch;
// Config::DSK_Path = FileUtils::DSK_Path;
// Config::DSK_begin_row = FileUtils::fileTypes[DISK_DSKFILE].begin_row;
// Config::DSK_focus = FileUtils::fileTypes[DISK_DSKFILE].focus;
// Config::DSK_fdMode = FileUtils::fileTypes[DISK_DSKFILE].fdMode;
// Config::DSK_fileSearch = FileUtils::fileTypes[DISK_DSKFILE].fileSearch;
// Config::ram_file = sna_fn;
// Config::save();
// OSD::esp_hard_reset();
// }
// } else {
// if ((force_arch != "") && ((Config::arch != force_arch) || (Config::romSet != force_romset))) {
// snapshotArch = force_arch;
// Config::requestMachine(force_arch, force_romset);
// // Condition this to 50hz mode
// if(Config::videomode) {
// Config::SNA_Path = FileUtils::SNA_Path;
// Config::SNA_begin_row = FileUtils::fileTypes[DISK_SNAFILE].begin_row;
// Config::SNA_focus = FileUtils::fileTypes[DISK_SNAFILE].focus;
// Config::SNA_fdMode = FileUtils::fileTypes[DISK_SNAFILE].fdMode;
// Config::SNA_fileSearch = FileUtils::fileTypes[DISK_SNAFILE].fileSearch;
// Config::TAP_Path = FileUtils::TAP_Path;
// Config::TAP_begin_row = FileUtils::fileTypes[DISK_TAPFILE].begin_row;
// Config::TAP_focus = FileUtils::fileTypes[DISK_TAPFILE].focus;
// Config::TAP_fdMode = FileUtils::fileTypes[DISK_TAPFILE].fdMode;
// Config::TAP_fileSearch = FileUtils::fileTypes[DISK_TAPFILE].fileSearch;
// Config::DSK_Path = FileUtils::DSK_Path;
// Config::DSK_begin_row = FileUtils::fileTypes[DISK_DSKFILE].begin_row;
// Config::DSK_focus = FileUtils::fileTypes[DISK_DSKFILE].focus;
// Config::DSK_fdMode = FileUtils::fileTypes[DISK_DSKFILE].fdMode;
// Config::DSK_fileSearch = FileUtils::fileTypes[DISK_DSKFILE].fileSearch;
// Config::ram_file = sna_fn;
// Config::save();
// OSD::esp_hard_reset();
// }
// }
// }
// } else if (Z80Ops::is48) {
// if (snapshotArch == "Pentagon") {
// if (force_arch == "")
// Config::requestMachine("Pentagon", "");
// else {
// snapshotArch = force_arch;
// Config::requestMachine(force_arch, force_romset);
// }
// // Condition this to 50hz mode
// if(Config::videomode) {
// Config::SNA_Path = FileUtils::SNA_Path;
// Config::SNA_begin_row = FileUtils::fileTypes[DISK_SNAFILE].begin_row;
// Config::SNA_focus = FileUtils::fileTypes[DISK_SNAFILE].focus;
// Config::SNA_fdMode = FileUtils::fileTypes[DISK_SNAFILE].fdMode;
// Config::SNA_fileSearch = FileUtils::fileTypes[DISK_SNAFILE].fileSearch;
// Config::TAP_Path = FileUtils::TAP_Path;
// Config::TAP_begin_row = FileUtils::fileTypes[DISK_TAPFILE].begin_row;
// Config::TAP_focus = FileUtils::fileTypes[DISK_TAPFILE].focus;
// Config::TAP_fdMode = FileUtils::fileTypes[DISK_TAPFILE].fdMode;
// Config::TAP_fileSearch = FileUtils::fileTypes[DISK_TAPFILE].fileSearch;
// Config::DSK_Path = FileUtils::DSK_Path;
// Config::DSK_begin_row = FileUtils::fileTypes[DISK_DSKFILE].begin_row;
// Config::DSK_focus = FileUtils::fileTypes[DISK_DSKFILE].focus;
// Config::DSK_fdMode = FileUtils::fileTypes[DISK_DSKFILE].fdMode;
// Config::DSK_fileSearch = FileUtils::fileTypes[DISK_DSKFILE].fileSearch;
// Config::ram_file = sna_fn;
// Config::save();
// OSD::esp_hard_reset();
// }
// }
// }
// Change ALU to snapshot one if present
if (force_ALU != 0xff && force_ALU != Config::ALUTK) {
Config::ALUTK = force_ALU;
// Condition this to 50hz mode
if(Config::videomode) {
Config::SNA_Path = FileUtils::SNA_Path;
Config::SNA_begin_row = FileUtils::fileTypes[DISK_SNAFILE].begin_row;
Config::SNA_focus = FileUtils::fileTypes[DISK_SNAFILE].focus;
Config::SNA_fdMode = FileUtils::fileTypes[DISK_SNAFILE].fdMode;
Config::SNA_fileSearch = FileUtils::fileTypes[DISK_SNAFILE].fileSearch;
Config::TAP_Path = FileUtils::TAP_Path;
Config::TAP_begin_row = FileUtils::fileTypes[DISK_TAPFILE].begin_row;
Config::TAP_focus = FileUtils::fileTypes[DISK_TAPFILE].focus;
Config::TAP_fdMode = FileUtils::fileTypes[DISK_TAPFILE].fdMode;
Config::TAP_fileSearch = FileUtils::fileTypes[DISK_TAPFILE].fileSearch;
Config::DSK_Path = FileUtils::DSK_Path;
Config::DSK_begin_row = FileUtils::fileTypes[DISK_DSKFILE].begin_row;
Config::DSK_focus = FileUtils::fileTypes[DISK_DSKFILE].focus;
Config::DSK_fdMode = FileUtils::fileTypes[DISK_DSKFILE].fdMode;
Config::DSK_fileSearch = FileUtils::fileTypes[DISK_DSKFILE].fileSearch;
Config::ram_file = sna_fn;
Config::save();
OSD::esp_hard_reset();
}
}
ESPectrum::reset();
// printf("FileSNA::load: Opening %s: size = %d\n", sna_fn.c_str(), sna_size);
// Read in the registers
Z80::setRegI(readByteFile(file));
Z80::setRegHLx(readWordFileLE(file));
Z80::setRegDEx(readWordFileLE(file));
Z80::setRegBCx(readWordFileLE(file));
Z80::setRegAFx(readWordFileLE(file));
Z80::setRegHL(readWordFileLE(file));
Z80::setRegDE(readWordFileLE(file));
Z80::setRegBC(readWordFileLE(file));
Z80::setRegIY(readWordFileLE(file));
Z80::setRegIX(readWordFileLE(file));
uint8_t inter = readByteFile(file);
Z80::setIFF2(inter & 0x04 ? true : false);
Z80::setIFF1(Z80::isIFF2());
Z80::setRegR(readByteFile(file));
Z80::setRegAF(readWordFileLE(file));
Z80::setRegSP(readWordFileLE(file));
Z80::setIM((Z80::IntMode)(readByteFile(file)));
VIDEO::borderColor = readByteFile(file);
VIDEO::brd = VIDEO::border32[VIDEO::borderColor];
if (Z80Ops::is48 && sna_size == SNA_48K_WITH_ROM_SIZE) {
// Load ROM if present
readBlockFile(file, MemESP::ram[1], 0x4000);
MemESP::ramCurrent[0] = MemESP::ram[1];
MemESP::SPRom = true;
}
// read 48K memory
readBlockFile(file, MemESP::ram[5], 0x4000);
readBlockFile(file, MemESP::ram[2], 0x4000);
readBlockFile(file, MemESP::ram[0], 0x4000);
if (Z80Ops::is48) {
// in 48K mode, pop PC from stack
uint16_t SP = Z80::getRegSP();
Z80::setRegPC(MemESP::readword(SP));
Z80::setRegSP(SP + 2);
} else {
// in 128K mode, recover stored PC
uint16_t sna_PC = readWordFileLE(file);
Z80::setRegPC(sna_PC);
// tmp_port contains page switching status, including current page number (latch)
uint8_t tmp_port = readByteFile(file);
uint8_t tmp_latch = tmp_port & 0x07;
// copy what was read into page 0 to correct page
memcpy(MemESP::ram[tmp_latch], MemESP::ram[0], 0x4000);
uint8_t tr_dos = readByteFile(file); // Check if TR-DOS is paged
// read remaining pages
for (int page = 0; page < 8; page++) {
if (page != tmp_latch && page != 2 && page != 5) {
readBlockFile(file, MemESP::ram[page], 0x4000);
}
}
// decode tmp_port
MemESP::videoLatch = bitRead(tmp_port, 3);
MemESP::romLatch = bitRead(tmp_port, 4);
MemESP::pagingLock = bitRead(tmp_port, 5);
MemESP::bankLatch = tmp_latch;
if (tr_dos) {
MemESP::romInUse = 4;
ESPectrum::trdos = true;
} else {
MemESP::romInUse = MemESP::romLatch;
ESPectrum::trdos = false;
}
MemESP::ramCurrent[0] = MemESP::rom[MemESP::romInUse];
MemESP::ramCurrent[3] = MemESP::ram[MemESP::bankLatch];
MemESP::ramContended[3] = Z80Ops::isPentagon ? false : (MemESP::bankLatch & 0x01 ? true: false);
VIDEO::grmem = MemESP::videoLatch ? MemESP::ram[7] : MemESP::ram[5];
// if (Z80Ops::isPentagon) CPU::tstates = 22; // Pentagon SNA load fix... still dunno why this works but it works
}
fclose(file);
return true;
}
// ///////////////////////////////////////////////////////////////////////////////
bool FileSNA::isPersistAvailable(string filename) {
FILE *f = fopen(filename.c_str(), "rb");
if (f == NULL)
return false;
else
fclose(f);
return true;
}
// ///////////////////////////////////////////////////////////////////////////////
bool check_and_create_directory(const char* path) {
struct stat st;
if (stat(path, &st) == 0) {
if ((st.st_mode & S_IFDIR) != 0) {
// printf("Directory exists\n");
return true;
} else {
// printf("Path exists but it is not a directory\n");
// Create the directory
if (mkdir(path, 0755) == 0) {
// printf("Directory created\n");
return true;
} else {
printf("Failed to create directory\n");
return false;
}
}
} else {
// printf("Directory does not exist\n");
// Create the directory
if (mkdir(path, 0755) == 0) {
// printf("Directory created\n");
return true;
} else {
printf("Failed to create directory\n");
return false;
}
}
}
// ///////////////////////////////////////////////////////////////////////////////
static bool writeMemPage(uint8_t page, FILE *file, bool blockMode)
{
page = page & 0x07;
uint8_t* buffer = MemESP::ram[page];
// printf("writing page %d in [%s] mode\n", page, blockMode ? "block" : "byte");
if (blockMode) {
// Writing blocks should be faster, but fails sometimes when flash is getting full.
for (int offset = 0; offset < MEM_PG_SZ; offset+=0x4000) {
// printf("writing block from page %d at offset %d\n", page, offset);
if (1 != fwrite(&buffer[offset],0x4000,1,file)) {
printf("error writing block from page %d at offset %d\n", page, offset);
return false;
}
}
}
else {
for (int offset = 0; offset < MEM_PG_SZ; offset++) {
uint8_t b = buffer[offset];
if (1 != fwrite(&b,1,1,file)) {
printf("error writing byte from page %d at offset %d\n", page, offset);
return false;
}
}
}
return true;
}
// ///////////////////////////////////////////////////////////////////////////////
bool FileSNA::save(string sna_file) {
// Try to save using pages
if (FileSNA::save(sna_file, true)) return true;
OSD::osdCenteredMsg(OSD_PSNA_SAVE_WARN, LEVEL_WARN);
// Try to save byte-by-byte
return FileSNA::save(sna_file, false);
}
// ///////////////////////////////////////////////////////////////////////////////
bool FileSNA::save(string sna_file, bool blockMode) {
FILE *file;
file = fopen(sna_file.c_str(), "wb");
if (file==NULL)
{
printf("FileSNA: Error opening %s for writing",sna_file.c_str());
return false;
}
// write registers: begin with I
writeByteFile(Z80::getRegI(), file);
writeWordFileLE(Z80::getRegHLx(), file);
writeWordFileLE(Z80::getRegDEx(), file);
writeWordFileLE(Z80::getRegBCx(), file);
writeWordFileLE(Z80::getRegAFx(), file);
writeWordFileLE(Z80::getRegHL(), file);
writeWordFileLE(Z80::getRegDE(), file);
writeWordFileLE(Z80::getRegBC(), file);
writeWordFileLE(Z80::getRegIY(), file);
writeWordFileLE(Z80::getRegIX(), file);
uint8_t inter = Z80::isIFF2() ? 0x04 : 0;
writeByteFile(inter, file);
writeByteFile(Z80::getRegR(), file);
writeWordFileLE(Z80::getRegAF(), file);
uint16_t SP = Z80::getRegSP();
// if (Config::arch == "48K" || Config::arch == "TK90X" || Config::arch == "TK95") {
if (Z80Ops::is48) {
// decrement stack pointer it for pushing PC to stack, only on 48K
SP -= 2;
MemESP::writeword(SP, Z80::getRegPC());
}
writeWordFileLE(SP, file);
writeByteFile(Z80::getIM(), file);
uint8_t bordercol = VIDEO::borderColor;
writeByteFile(bordercol, file);
// write 16K ROM page if we've loaded it previously
if (Z80Ops::is48 && MemESP::SPRom) {
if (!writeMemPage(1, file, blockMode)) {
fclose(file);
return false;
}
}
// write RAM pages in 48K address space (0x4000 - 0xFFFF)
uint8_t pages[3] = {5, 2, 0};
if (Z80Ops::is128 || Z80Ops::isPentagon)
pages[2] = MemESP::bankLatch;
for (uint8_t ipage = 0; ipage < 3; ipage++) {
uint8_t page = pages[ipage];
if (!writeMemPage(page, file, blockMode)) {
fclose(file);
return false;
}
}
if (Z80Ops::is128 || Z80Ops::isPentagon) {
// if (Config::arch != "48K") {
// write pc
writeWordFileLE( Z80::getRegPC(), file);
// printf("PC: %u\n",(unsigned int)Z80::getRegPC());
// write memESP bank control port
uint8_t tmp_port = MemESP::bankLatch;
bitWrite(tmp_port, 3, MemESP::videoLatch);
bitWrite(tmp_port, 4, MemESP::romLatch);
bitWrite(tmp_port, 5, MemESP::pagingLock);
writeByteFile(tmp_port, file);
// printf("7FFD: %u\n",(unsigned int)tmp_port);
if (ESPectrum::trdos)
writeByteFile(1, file); // TR-DOS paged
else
writeByteFile(0, file); // TR-DOS not paged
// write remaining ram pages
for (int page = 0; page < 8; page++) {
if (page != MemESP::bankLatch && page != 2 && page != 5) {
if (!writeMemPage(page, file, blockMode)) {
fclose(file);
return false;
}
}
}
}
fclose(file);
return true;
}
static uint16_t mkword(uint8_t lobyte, uint8_t hibyte) {
return lobyte | (hibyte << 8);
}
bool FileZ80::keepArch = false;
bool FileZ80::load(string z80_fn) {
FILE *file;
file = fopen(z80_fn.c_str(), "rb");
if (file == NULL) {
printf("FileZ80: Error opening %s\n",z80_fn.c_str());
return false;
}
// Check Z80 version and arch
uint8_t z80version;
uint8_t mch;
string z80_arch = "";
uint16_t ahb_len;
fseek(file,6,SEEK_SET);
if (mkword(readByteFile(file),readByteFile(file)) != 0) { // Version 1
z80version = 1;
mch = 0;
z80_arch = "48K";
} else { // Version 2 o 3
fseek(file,30,SEEK_SET);
ahb_len = mkword(readByteFile(file),readByteFile(file));
// additional header block length
if (ahb_len == 23)
z80version = 2;
else if (ahb_len == 54 || ahb_len == 55)
z80version = 3;
else {
OSD::osdCenteredMsg("Z80 load: unknown version", LEVEL_ERROR);
printf("Z80.load: unknown version, ahblen = %u\n", (unsigned int) ahb_len);
fclose(file);
return false;
}
fseek(file,34,SEEK_SET);
mch = readByteFile(file); // Machine
if (z80version == 2) {
if (mch == 0) z80_arch = "48K";
if (mch == 1) z80_arch = "48K"; // + if1
// if (mch == 2) z80_arch = "SAMRAM";
if (mch == 3) z80_arch = "128K";
if (mch == 4) z80_arch = "128K"; // + if1
}
else if (z80version == 3) {
if (mch == 0) z80_arch = "48K";
if (mch == 1) z80_arch = "48K"; // + if1
// if (mch == 2) z80_arch = "SAMRAM";
if (mch == 3) z80_arch = "48K"; // + mgt
if (mch == 4) z80_arch = "128K";
if (mch == 5) z80_arch = "128K"; // + if1
if (mch == 6) z80_arch = "128K"; // + mgt
if (mch == 7) z80_arch = "128K"; // Spectrum +3
if (mch == 9) z80_arch = "Pentagon";
if (mch == 12) z80_arch = "128K"; // Spectrum +2
if (mch == 13) z80_arch = "128K"; // Spectrum +2A
}
}
// printf("Z80 version %u, AHB Len: %u, machine code: %u\n",(unsigned char)z80version,(unsigned int)ahb_len, (unsigned char)mch);
if (z80_arch == "") {
OSD::osdCenteredMsg("Z80 load: unknown machine", LEVEL_ERROR);
printf("Z80.load: unknown machine, machine code = %u\n", (unsigned char)mch);
fclose(file);
return false;
}
// Force keepArch for testing
// keepArch = true;
if (keepArch) {
if (z80_arch == "48K") {
if (Config::arch == "128K" || Config::arch == "Pentagon") keepArch = false;
} else {
if (Config::arch == "48K" || Config::arch == "TK90X" || Config::arch == "TK95") keepArch = false;
}
}
// printf("fileTypes -> Path: %s, begin_row: %d, focus: %d\n",FileUtils::SNA_Path.c_str(),FileUtils::fileTypes[DISK_SNAFILE].begin_row,FileUtils::fileTypes[DISK_SNAFILE].focus);
// printf("Config -> Path: %s, begin_row: %d, focus: %d\n",Config::Path.c_str(),(int)Config::begin_row,(int)Config::focus);
// Manage arch change
if (Config::arch != z80_arch) {
if (!keepArch) {
string z80_romset = "";
// printf("z80_arch: %s mch: %d pref_romset48: %s pref_romset128: %s z80_romset: %s\n",z80_arch.c_str(),mch,Config::pref_romSet_48.c_str(),Config::pref_romSet_128.c_str(),z80_romset.c_str());
if (z80_arch == "48K") {
if (Config::pref_romSet_48 == "48K" || Config::pref_romSet_48 == "48Kes")
z80_romset = Config::pref_romSet_48;
} else
if (z80_arch == "128K") {
if (mch == 12) { // +2
if (Config::pref_romSet_128 == "+2" || Config::pref_romSet_128 == "+2es")
z80_romset = Config::pref_romSet_128;
else
z80_romset = "+2";
} else {
if (Config::pref_romSet_128 == "128K" || Config::pref_romSet_128 == "128Kes")
z80_romset = Config::pref_romSet_128;
}
}
// printf("z80_arch: %s mch: %d pref_romset48: %s pref_romset128: %s z80_romset: %s\n",z80_arch.c_str(),mch,Config::pref_romSet_48.c_str(),Config::pref_romSet_128.c_str(),z80_romset.c_str());
Config::requestMachine(z80_arch, z80_romset);
// Condition this to 50hz mode
if(Config::videomode) {
Config::SNA_Path = FileUtils::SNA_Path;
Config::SNA_begin_row = FileUtils::fileTypes[DISK_SNAFILE].begin_row;
Config::SNA_focus = FileUtils::fileTypes[DISK_SNAFILE].focus;
Config::SNA_fdMode = FileUtils::fileTypes[DISK_SNAFILE].fdMode;
Config::SNA_fileSearch = FileUtils::fileTypes[DISK_SNAFILE].fileSearch;
Config::TAP_Path = FileUtils::TAP_Path;
Config::TAP_begin_row = FileUtils::fileTypes[DISK_TAPFILE].begin_row;
Config::TAP_focus = FileUtils::fileTypes[DISK_TAPFILE].focus;
Config::TAP_fdMode = FileUtils::fileTypes[DISK_TAPFILE].fdMode;
Config::TAP_fileSearch = FileUtils::fileTypes[DISK_TAPFILE].fileSearch;
Config::DSK_Path = FileUtils::DSK_Path;
Config::DSK_begin_row = FileUtils::fileTypes[DISK_DSKFILE].begin_row;
Config::DSK_focus = FileUtils::fileTypes[DISK_DSKFILE].focus;
Config::DSK_fdMode = FileUtils::fileTypes[DISK_DSKFILE].fdMode;
Config::DSK_fileSearch = FileUtils::fileTypes[DISK_DSKFILE].fileSearch;
Config::ram_file = z80_fn;
Config::save();
OSD::esp_hard_reset();
}
}
} else {
if (z80_arch == "128K") {
string z80_romset = "";
// printf("z80_arch: %s mch: %d pref_romset48: %s pref_romset128: %s z80_romset: %s\n",z80_arch.c_str(),mch,Config::pref_romSet_48.c_str(),Config::pref_romSet_128.c_str(),z80_romset.c_str());
if (mch == 12) { // +2
if (Config::romSet != "+2" && Config::romSet != "+2es" && Config::romSet != "128Kcs") {
if (Config::pref_romSet_128 == "+2" || Config::pref_romSet_128 == "+2es")
z80_romset = Config::pref_romSet_128;
else
z80_romset = "+2";
Config::requestMachine(z80_arch, z80_romset);
}
} else {
if (Config::romSet != "128K" && Config::romSet != "128Kes" && Config::romSet != "128Kcs") {
if (Config::pref_romSet_128 == "128K" || Config::pref_romSet_128 == "128Kes")
z80_romset = Config::pref_romSet_128;
else
z80_romset = "128K";
Config::requestMachine(z80_arch, z80_romset);
}
}
// printf("z80_arch: %s mch: %d pref_romset48: %s pref_romset128: %s z80_romset: %s\n",z80_arch.c_str(),mch,Config::pref_romSet_48.c_str(),Config::pref_romSet_128.c_str(),z80_romset.c_str());
}
}
ESPectrum::reset();
keepArch = false;
// Get file size
fseek(file,0,SEEK_END);
uint32_t file_size = ftell(file);
rewind (file);
uint32_t dataOffset = 0;
// stack space for header, should be enough for
// version 1 (30 bytes)
// version 2 (55 bytes) (30 + 2 + 23)
// version 3 (87 bytes) (30 + 2 + 55) or (86 bytes) (30 + 2 + 54)
uint8_t header[87];
// read first 30 bytes
for (uint8_t i = 0; i < 30; i++) {
header[i] = readByteFile(file);
dataOffset ++;
}
// additional vars
uint8_t b12, b29;
// begin loading registers
Z80::setRegA ( header[0]);
Z80::setFlags ( header[1]);
// Z80::setRegBC (mkword(header[2], header[3]));
Z80::setRegC(header[2]);
Z80::setRegB(header[3]);
// Z80::setRegHL (mkword(header[4], header[5]));
Z80::setRegL(header[4]);
Z80::setRegH(header[5]);
Z80::setRegPC (mkword(header[6], header[7]));
Z80::setRegSP (mkword(header[8], header[9]));
Z80::setRegI ( header[10]);
// Z80::setRegR ( header[11]);
uint8_t regR = header[11] & 0x7f;
if ((header[12] & 0x01) != 0) {
regR |= 0x80;
}
Z80::setRegR(regR);
b12 = header[12];
VIDEO::borderColor = (b12 >> 1) & 0x07;
VIDEO::brd = VIDEO::border32[VIDEO::borderColor];
// Z80::setRegDE (mkword(header[13], header[14]));
Z80::setRegE(header[13]);
Z80::setRegD(header[14]);
// Z80::setRegBCx(mkword(header[15], header[16]));
Z80::setRegCx(header[15]);
Z80::setRegBx(header[16]);
// Z80::setRegDEx(mkword(header[17], header[18]));
Z80::setRegEx(header[17]);
Z80::setRegDx(header[18]);
// Z80::setRegHLx(mkword(header[19], header[20]));
Z80::setRegLx(header[19]);
Z80::setRegHx(header[20]);
// Z80::setRegAFx(mkword(header[22], header[21])); // watch out for order!!!
Z80::setRegAx(header[21]);
Z80::setRegFx(header[22]);
Z80::setRegIY (mkword(header[23], header[24]));
Z80::setRegIX (mkword(header[25], header[26]));
Z80::setIFF1 ( header[27] ? true : false);
Z80::setIFF2 ( header[28] ? true : false);
b29 = header[29];
Z80::setIM((Z80::IntMode)(b29 & 0x03));
// spectrum.setIssue2((z80Header1[29] & 0x04) != 0); // TO DO: Implement this
uint16_t RegPC = Z80::getRegPC();
bool dataCompressed = (b12 & 0x20) ? true : false;
if (z80version == 1) {
// version 1, the simplest, 48K only.
uint32_t memRawLength = file_size - dataOffset;
if (dataCompressed) {
// assuming stupid 00 ED ED 00 terminator present, should check for it instead of assuming
uint16_t dataLen = (uint16_t)(memRawLength - 4);
// load compressed data into memory
loadCompressedMemData(file, dataLen, 0x4000, 0xC000);
} else {
uint16_t dataLen = (memRawLength < 0xC000) ? memRawLength : 0xC000;
// load uncompressed data into memory
for (int i = 0; i < dataLen; i++)
MemESP::writebyte(0x4000 + i, readByteFile(file));
}
// latches for 48K
MemESP::romLatch = 0;
MemESP::romInUse = 0;
MemESP::bankLatch = 0;
MemESP::pagingLock = 1;
MemESP::videoLatch = 0;
} else {
// read 2 more bytes
for (uint8_t i = 30; i < 32; i++) {
header[i] = readByteFile(file);
dataOffset ++;
}
// additional header block length
uint16_t ahblen = mkword(header[30], header[31]);
// read additional header block
for (uint8_t i = 32; i < 32 + ahblen; i++) {
header[i] = readByteFile(file);
dataOffset ++;
}
// program counter
RegPC = mkword(header[32], header[33]);
Z80::setRegPC(RegPC);
if (z80_arch == "48K") {
MemESP::romLatch = 0;
MemESP::romInUse = 0;
MemESP::bankLatch = 0;
MemESP::pagingLock = 1;
MemESP::videoLatch = 0;
uint16_t pageStart[12] = {0, 0, 0, 0, 0x8000, 0xC000, 0, 0, 0x4000, 0, 0};
uint32_t dataLen = file_size;
while (dataOffset < dataLen) {
uint8_t hdr0 = readByteFile(file); dataOffset ++;
uint8_t hdr1 = readByteFile(file); dataOffset ++;
uint8_t hdr2 = readByteFile(file); dataOffset ++;
uint16_t compDataLen = mkword(hdr0, hdr1);