-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdl_loadsave.c
1349 lines (1209 loc) · 46.6 KB
/
sdl_loadsave.c
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
/* sz81 Copyright (C) 2007-2011 Thunor <[email protected]>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Includes */
#include "sdl_engine.h"
#include "z80/z80.h"
#include "z80/z80_macros.h"
#include "zx81config.h"
#include "zx81.h"
load_file_dialog_ load_file_dialog;
save_state_dialog_ save_state_dialog;
extern MACHINE machine;
extern ZX81 zx81;
#ifdef ZXMORE
extern unsigned int zxmoffset;
#else
#define zxmoffset 0
#endif
/* for shared memory */
extern BYTE *sz81mem;
/* for state load/save */
extern int NMI_generator, hsync_counter, int_pending, nmi_pending;
/* Defines */
/* Variables */
/* Function prototypes */
char *strtoupper(char *original);
char *strzx81_to_ascii(int memaddr);
char *strzx80_to_ascii(int memaddr);
void fwrite_unsigned_short_little_endian(unsigned short *source, FILE *fp);
void fwrite_int_little_endian(int *source, FILE *fp);
void fwrite_unsigned_long_little_endian(unsigned long *source, FILE *fp);
void fread_unsigned_short_little_endian(unsigned short *target, FILE *fp);
void fread_int_little_endian(int *target, FILE *fp);
void fread_unsigned_long_little_endian(unsigned long *target, FILE *fp);
/***************************************************************************
* Load File Dialog Directory List Populate *
***************************************************************************/
/* This is called from multiple places.
*
* On entry: int refresh = TRUE if this is just a directory refresh
* else FALSE */
void load_file_dialog_dirlist_populate(int refresh) {
int filetypes;
if (*sdl_emulator.model == MODEL_ZX80) {
filetypes = DIRLIST_FILETYPE_ZX80;
} else {
filetypes = DIRLIST_FILETYPE_ZX81;
}
/* Repopulate the load file dialog's directory list */
dirlist_populate(load_file_dialog.dir,
&load_file_dialog.dirlist, &load_file_dialog.dirlist_sizeof,
&load_file_dialog.dirlist_count, filetypes);
/* If this is just a refresh then don't reset these */
if (!refresh) {
load_file_dialog.dirlist_top = 0;
load_file_dialog.dirlist_selected = 0;
}
}
/***************************************************************************
* Save State Dialog Slots Populate *
***************************************************************************/
/* This function will open the currently loaded program's save state folder
* and create it if it's not found to exist. It will then scan it looking
* for savsta1.ss{o|p} to savsta9.ss{o|p} */
int save_state_dialog_slots_populate(void) {
struct dirent *direntry;
char foldername[256];
char parentname[256];
int retval = FALSE;
int count, index;
DIR *dirstream;
/* Wipe the slots ready for fresh data */
for (count = 0; count < 9; count++)
save_state_dialog.slots[count] = 0;
/* Build a path to the currently loaded program's save state folder */
#if defined(PLATFORM_GP2X) || defined(__amigaos4__) || defined(_WIN32)
strcpy(foldername, LOCAL_DATA_DIR);
#else
strcpy(foldername, getenv ("HOME"));
strcatdelimiter(foldername);
strcat(foldername, LOCAL_DATA_DIR);
#endif
strcatdelimiter(foldername);
strcat(foldername, LOCAL_SAVSTA_DIR);
strcatdelimiter(foldername);
/* Here I'm going to create a single letter parent directory which
* is the first letter of the program name. Due to some nonsense
* experienced with my GP2X FAT32 Sandisk SD card creating m instead
* of M, showing m in termula2x but M plugged into my card reader on
* my Linux desktop computer I'm going to force the parent to lower-
* case so that MazezaM.p will save as local/m/MazezaM.p and fingers-
* crossed it'll universally work */
foldername[index = strlen(foldername)] =
tolower(file_dialog_basename(load_file_dialog.loaded)[0]);
foldername[++index] = 0;
strcpy(parentname, foldername);
/* parentname will now be something like local/m and
* foldername will now be something like local/m/MazezaM.p */
strcatdelimiter(foldername);
strcat(foldername, file_dialog_basename(load_file_dialog.loaded));
/* Firstly attempt to open the folder */
if ((dirstream = opendir(foldername)) == NULL) {
/* The path doesn't yet exist so attempt to create everything
* from parentname and onwards */
#ifndef Win32
mkdir(parentname, 0755);
mkdir(foldername, 0755);
#else
mkdir(parentname);
mkdir(foldername);
#endif
/* Attempt to open the newly created folder */
dirstream = opendir(foldername);
}
if (dirstream != NULL) {
while ((direntry = readdir(dirstream))) {
if (strlen(direntry->d_name) >= 5 &&
direntry->d_name[strlen(direntry->d_name) - 5] >= '1' &&
direntry->d_name[strlen(direntry->d_name) - 5] <= '9' &&
((*sdl_emulator.model == MODEL_ZX80 &&
sdl_filetype_casecmp(direntry->d_name, ".sso") == 0) ||
(*sdl_emulator.model == MODEL_ZX81 &&
sdl_filetype_casecmp(direntry->d_name, ".ssp") == 0))) {
save_state_dialog.slots[direntry->d_name
[strlen(direntry->d_name) - 5] - '1'] = TRUE;
}
}
closedir(dirstream);
} else {
retval = TRUE;
fprintf(stderr, "%s: Cannot read from directory %s\n", __func__,
foldername);
}
return retval;
}
/***************************************************************************
* Save File *
***************************************************************************/
/* This function replaces z81's save_p.
*
* On entry: if method = SAVE_FILE_METHOD_NAMEDSAVE then parameter holds
* the contents of the hl register pair which points to the
* area in the ZX81's memory that contains the program name.
* if method = SAVE_FILE_METHOD_STATESAVE then parameter holds
* the slot number 0 to 8.
* for all other methods parameter is ignored.
* On exit: returns TRUE on error
* else FALSE */
int sdl_save_file(int parameter, int method) {
char fullpath[256], filename[256];
struct Notification notification;
int retval = FALSE;
int index;
FILE *fp;
char *scp = NULL;
int addr, slen;
#ifndef __amigaos4__
/*struct tm *timestruct; Redundant
time_t rightnow;*/
int idxend, vars;
#endif
if (method == SAVE_FILE_METHOD_NAMEDSAVE) {
/* Build a path from the last entered directory */
strcpy(fullpath, load_file_dialog.dir);
/* Add a directory delimiter if required */
strcatdelimiter(fullpath);
#if 0
/* Add the local program dir. This will only really work
* on platforms that aren't changing load_file_dialog.dir */
#ifdef __amigaos4__
strcat(fullpath, LOCAL_PROGRM_DIR);
strcatdelimiter(fullpath);
#endif
#endif
/* Add translated program name */
strcat(fullpath, strzx81_to_ascii(parameter));
scp = strrchr(fullpath,';');
if (scp) {
addr = 0;
slen = 1;
sscanf(scp+1,"%i,%i",&addr,&slen);
strcpy(scp,"");
}
/* Add a file extension if one hasn't already been affixed */
if (sdl_filetype_casecmp(fullpath, ".p") != 0 &&
sdl_filetype_casecmp(fullpath, ".81") != 0 && !scp)
strcat(fullpath, ".p");
} else if (method == SAVE_FILE_METHOD_UNNAMEDSAVE) {
#ifdef __amigaos4__
/* This will return NULL if the user cancelled */
if ((amiga_file_request_retval = amiga_file_request("SZ81: Save file", TRUE)) != NULL) {
strcpy(fullpath, amiga_file_request_retval);
} else {
retval = TRUE;
}
#else
/* Build a path from the last entered directory */
strcpy(fullpath, load_file_dialog.dir);
/* Add a directory delimiter if required */
strcatdelimiter(fullpath);
/* At the moment I'm not looking to develop a Save As dialog
* just for ZX80 files, but I'm happy to implement some other
* methods to name the files:
* 1. Date and time stamped files e.g. zx80-20110115-234836.o
* (the GP2X won't support this but everything else is OK)
* 2. Incremented filenames e.g. zx80-0001.o, zx80-0002.o etc.
* 3. Filenames embedded within a xxxx REM SAVE "progname"
* (works on any platform and is actually quit cool :) ) */
index = 0x4028; /* Start of user program area */
vars = mem[0x4009] << 8 | mem[0x4008]; /* VARS */
while (index < vars) {
if (mem[index] == 0xfe && mem[index + 1] == 0xea &&
mem[index + 2] == 0x01) { /* REM SAVE " */
idxend = index = index + 3; /* Position on first char */
while (mem[idxend] != 0x01 && mem[idxend] < 0x80 &&
mem[idxend] != 0x76 && idxend < vars) idxend++;
if (index < idxend) {
mem[--idxend] |= 0x80; /* +80h marks last char */
strcpy(filename, strzx80_to_ascii(index));
mem[idxend] &= ~0x80; /* Remove +80h */
index = vars; /* Exit loop */
}
}
index++;
}
if (index == vars) {
/* Create a unique filename using the date and time Redundant
rightnow = time(NULL);
timestruct = localtime(&rightnow);
strftime(filename, sizeof(filename), "zx80-%Y%m%d-%H%M%S.o", timestruct);*/
/* Create a unique filename using the next highest number
* (it'll return 0 if the directory couldn't be opened or 1 as
* the base number when no files exist that match the pattern) */
index = get_filename_next_highest(fullpath, "zx80prog%4d");
sprintf(filename, "zx80prog%04i.o", index);
}
/* Add program name */
strcat(fullpath, filename);
#endif
if (!retval) {
/* Add a file extension if one hasn't already been affixed */
if (sdl_filetype_casecmp(fullpath, ".o") != 0 &&
sdl_filetype_casecmp(fullpath, ".80") != 0)
strcat(fullpath, ".o");
}
} else if (method == SAVE_FILE_METHOD_STATESAVE) {
/* Build a path to the currently loaded program's save state folder */
#if defined(PLATFORM_GP2X) || defined(__amigaos4__) || defined(_WIN32)
strcpy(fullpath, LOCAL_DATA_DIR);
#else
strcpy(fullpath, getenv ("HOME"));
strcatdelimiter(fullpath);
strcat(fullpath, LOCAL_DATA_DIR);
#endif
strcatdelimiter(fullpath);
strcat(fullpath, LOCAL_SAVSTA_DIR);
strcatdelimiter(fullpath);
fullpath[index = strlen(fullpath)] =
tolower(file_dialog_basename(load_file_dialog.loaded)[0]);
fullpath[++index] = 0;
strcatdelimiter(fullpath);
strcat(fullpath, file_dialog_basename(load_file_dialog.loaded));
strcatdelimiter(fullpath);
/* Form an appropriate filename */
if (*sdl_emulator.model == MODEL_ZX80) {
sprintf(filename, "savsta%i.sso", parameter + 1);
} else if (*sdl_emulator.model == MODEL_ZX81) {
sprintf(filename, "savsta%i.ssp", parameter + 1);
}
/* Append filename to fullpath */
strcat(fullpath, filename);
}
printf("Creating file %s...\n",fullpath);
if (!retval) {
/* Attempt to open the file */
if ((fp = fopen(fullpath, "wb")) != NULL) {
if (method == SAVE_FILE_METHOD_STATESAVE) {
/* Printer variables are reinitialised when a new file
* is opened on output so saving them is futile.
* Saving keyports and sound variables is unneccessary.
* signal_int_flag is being updated by the SDL timer
* likely in another thread so no point in saving that.
* refresh_screen I'm forcing to 1 anyway.
*
* To make these files platform independent I'm saving
* and restoring everything by the byte in little-endian
* format. These are the integer sizes on my development
* computer (GNU/Linux 32bit):
*
* sizeof(long) = 4 bytes
* sizeof(int) = 4 bytes
* sizeof(short) = 2 bytes
* sizeof(char) = 1 byte */
/* The entire contents of memory */
fwrite(mem, 1, 64 * 1024, fp); /* unsigned char */
/* The processor */
fwrite(&A, 1, 1, fp); /* unsigned char */
fwrite(&F, 1, 1, fp);
fwrite(&B, 1, 1, fp);
fwrite(&C, 1, 1, fp);
fwrite(&D, 1, 1, fp);
fwrite(&E, 1, 1, fp);
fwrite(&H, 1, 1, fp);
fwrite(&L, 1, 1, fp);
fwrite(&A_, 1, 1, fp);
fwrite(&F_, 1, 1, fp);
fwrite(&B_, 1, 1, fp);
fwrite(&C_, 1, 1, fp);
fwrite(&D_, 1, 1, fp);
fwrite(&E_, 1, 1, fp);
fwrite(&H_, 1, 1, fp);
fwrite(&L_, 1, 1, fp);
fwrite(&I, 1, 1, fp);
fwrite(&R, 1, 1, fp);
fwrite(&R7, 1, 1, fp);
fwrite(&IFF1, 1, 1, fp);
fwrite(&IFF2, 1, 1, fp);
fwrite(&IM, 1, 1, fp);
fwrite_unsigned_short_little_endian(&PC, fp);
fwrite_unsigned_short_little_endian(&IX, fp);
fwrite_unsigned_short_little_endian(&IY, fp);
fwrite_unsigned_short_little_endian(&SP, fp);
/* Hardware */
fwrite_int_little_endian(&NMI_generator, fp);
fwrite_int_little_endian(&hsync_counter, fp);
fwrite_int_little_endian(&int_pending, fp);
fwrite_int_little_endian(&nmi_pending, fp);
/* 65654/0x10076 bytes to here for 2.1.7 */
} else {
/* Write up to and including E_LINE */
if (*sdl_emulator.model == MODEL_ZX80) {
fwrite(mem + 0x4000 + zxmoffset, 1, (mem[0x400b+zxmoffset] << 8 | mem[0x400a+zxmoffset]) - 0x4000, fp);
} else if (*sdl_emulator.model == MODEL_ZX81) {
if (scp)
fwrite(mem + addr + zxmoffset, 1, slen, fp);
else
fwrite(mem + 0x4009 + zxmoffset, 1, (mem[0x4015+zxmoffset] << 8 | mem[0x4014+zxmoffset]) - 0x4009, fp);
}
/* Copy fullpath across to the load file dialog as
* then we have a record of what was last saved */
strcpy(load_file_dialog.loaded, fullpath);
}
/* Close the file now as we've finished with it */
fclose(fp);
} else {
retval = TRUE;
/* Warn the user via the GUI that the save failed */
if (method == SAVE_FILE_METHOD_STATESAVE) {
strcpy(notification.title, "Save State");
} else {
strcpy(notification.title, "Save");
}
strcpy(notification.text, "Failed");
notification.timeout = NOTIFICATION_TIMEOUT_1250;
notification_show(NOTIFICATION_SHOW, ¬ification);
}
}
if (!retval) {
if (method != SAVE_FILE_METHOD_STATESAVE) {
/* Refresh the load file dialog's directory list */
load_file_dialog_dirlist_populate(TRUE);
}
}
return retval;
}
/***************************************************************************
* Load File *
***************************************************************************/
/* This function replaces z81's load_p.
*
* On entry: if method = LOAD_FILE_METHOD_NAMEDLOAD then parameter holds
* the contents of the hl register pair which points to the
* area in the ZX81's memory that contains the program name.
* if method = LOAD_FILE_METHOD_STATELOAD then parameter holds
* the slot number 0 to 8.
* for all other methods parameter is ignored.
* On exit: returns TRUE on error
* else FALSE */
int sdl_load_file(int parameter, int method) {
char fullpath[256], filename[256];
struct Notification notification;
int retval = FALSE;
int count, index;
int ramsize;
FILE *fp;
char *scp = NULL;
char *fsp = NULL;
int addr;
#ifdef ZXMORE
int flash=0;
#endif
/* If requested, read and set the preset method instead */
if (method == LOAD_FILE_METHOD_DETECT) {
method = load_file_dialog.method;
load_file_dialog.method = LOAD_FILE_METHOD_NONE;
}
if (method == LOAD_FILE_METHOD_AUTOLOAD) {
/* Check that the file type is compatible with the machine model */
if ((*sdl_emulator.model == MODEL_ZX80 &&
(sdl_filetype_casecmp(sdl_com_line.filename, ".o") == 0 ||
sdl_filetype_casecmp(sdl_com_line.filename, ".80") == 0)) ||
(*sdl_emulator.model == MODEL_ZX81 &&
(sdl_filetype_casecmp(sdl_com_line.filename, ".p") == 0 ||
sdl_filetype_casecmp(sdl_com_line.filename, ".81") == 0))) {
/* Copy the filename to fullpath which we'll be using below */
strcpy(fullpath, sdl_com_line.filename);
} else {
fprintf(stderr, "%s: File type is incompatible with machine model.\n",
__func__);
retval = TRUE;
}
} else if (method == LOAD_FILE_METHOD_FORCEDLOAD) {
#ifdef __amigaos4__
strcpy(fullpath, amiga_file_request_retval);
#else
/* Build a path from the last entered directory */
strcpy(fullpath, load_file_dialog.dir);
/* Add a directory delimiter if required */
strcatdelimiter(fullpath);
/* Add load_file_dialog.selected item */
strcat(fullpath, load_file_dialog.dirlist +
load_file_dialog.dirlist_selected * load_file_dialog.dirlist_sizeof);
#endif
} else if (method == LOAD_FILE_METHOD_SELECTLOAD) {
/* Let the load file dialog know what we are doing */
load_file_dialog.method = method;
#ifdef __amigaos4__
/* This will return NULL if the user cancelled */
if ((amiga_file_request_retval = amiga_file_request("SZ81: Load file", FALSE)) != NULL)
load_file_dialog.method = LOAD_FILE_METHOD_SELECTLOADOK;
#else
/* Show the load file dialog */
toggle_ldfile_state();
/* Wait for the user to select Load or Exit
* which will close the dialog */
emulator_hold(&load_file_dialog.state);
#endif
/* If Load was selected then the method will have been updated */
if (load_file_dialog.method == LOAD_FILE_METHOD_SELECTLOADOK) {
#ifdef __amigaos4__
strcpy(fullpath, amiga_file_request_retval);
#else
/* Build a path from the last entered directory */
strcpy(fullpath, load_file_dialog.dir);
/* Add a directory delimiter if required */
strcatdelimiter(fullpath);
/* Add load_file_dialog.selected item */
strcat(fullpath, load_file_dialog.dirlist +
load_file_dialog.dirlist_selected * load_file_dialog.dirlist_sizeof);
#endif
} else {
retval = TRUE;
}
} else if (method == LOAD_FILE_METHOD_STATELOAD) {
/* Build a path to the currently loaded program's save state folder */
#if defined(PLATFORM_GP2X) || defined(__amigaos4__) || defined(_WIN32)
strcpy(fullpath, LOCAL_DATA_DIR);
#else
strcpy(fullpath, getenv ("HOME"));
strcatdelimiter(fullpath);
strcat(fullpath, LOCAL_DATA_DIR);
#endif
strcatdelimiter(fullpath);
strcat(fullpath, LOCAL_SAVSTA_DIR);
strcatdelimiter(fullpath);
fullpath[index = strlen(fullpath)] =
tolower(file_dialog_basename(load_file_dialog.loaded)[0]);
fullpath[++index] = 0;
strcatdelimiter(fullpath);
strcat(fullpath, file_dialog_basename(load_file_dialog.loaded));
strcatdelimiter(fullpath);
/* Form an appropriate filename */
if (*sdl_emulator.model == MODEL_ZX80) {
sprintf(filename, "savsta%i.sso", parameter + 1);
} else if (*sdl_emulator.model == MODEL_ZX81) {
sprintf(filename, "savsta%i.ssp", parameter + 1);
}
/* Append filename to fullpath */
strcat(fullpath, filename);
}
if (!retval) {
for (count = 0; count < 2; count++) {
if (method == LOAD_FILE_METHOD_NAMEDLOAD) {
/* Attempt to open the file firstly in lowercase and then
* in uppercase as program files are obtained in either */
/* Get translated program name */
strcpy(filename, strzx81_to_ascii(parameter));
scp = strrchr(filename,';');
if (scp) {
addr = 0;
#ifdef ZXMORE
if (scp[1]=='#') {
flash = 1;
sscanf(scp+2,"%i",&addr);
} else
#endif
sscanf(scp+1,"%i",&addr);
strcpy(scp,"");
}
/* Add a file extension if one hasn't already been affixed */
if (sdl_filetype_casecmp(filename, ".p") != 0 &&
sdl_filetype_casecmp(filename, ".81") != 0 && !scp)
strcat(filename, ".p");
/* Convert filename to uppercase on second attempt */
if (count == 1) strcpy(filename, strtoupper(filename));
/* Build a path from the last entered directory */
strcpy(fullpath, load_file_dialog.dir);
/* Add a directory delimiter if required */
strcatdelimiter(fullpath);
#if 0
/* Add the local program dir. This will only really work
* on platforms that aren't changing load_file_dialog.dir */
#ifdef __amigaos4__
strcat(fullpath, LOCAL_PROGRM_DIR);
strcatdelimiter(fullpath);
#endif
#endif
/* Add translated program name */
strcat(fullpath, filename);
}
/* auto-change directory */
if (!strlen(load_file_dialog.loaded)) if (strrchr(fullpath,'/')) {
strcpy(load_file_dialog.dir,fullpath);
fsp = strrchr(load_file_dialog.dir,'/');
if (fsp) strcpy(fsp,"");
}
printf("Opening file %s...\n",fullpath);
/* Attempt to open the file */
if ((fp = fopen(fullpath, "rb")) != NULL) {
if (method == LOAD_FILE_METHOD_STATELOAD) {
/* Printer variables are reinitialised when a new file
* is opened on output so saving them is futile.
* Saving keyports and sound variables is unneccessary.
* signal_int_flag is being updated by the SDL timer
* likely in another thread so no point in saving that.
* refresh_screen I'm forcing to 1 anyway.
*
* To make these files platform independent I'm saving
* and restoring everything by the byte in little-endian
* format. These are the integer sizes on my development
* computer (GNU/Linux 32bit):
*
* sizeof(long) = 4 bytes
* sizeof(int) = 4 bytes
* sizeof(short) = 2 bytes
* sizeof(char) = 1 byte */
/* The entire contents of memory */
fread(mem, 1, 64 * 1024, fp); /* unsigned char */
/* The processor */
fread(&A, 1, 1, fp); /* unsigned char */
fread(&F, 1, 1, fp);
fread(&B, 1, 1, fp);
fread(&C, 1, 1, fp);
fread(&D, 1, 1, fp);
fread(&E, 1, 1, fp);
fread(&H, 1, 1, fp);
fread(&L, 1, 1, fp);
fread(&A_, 1, 1, fp);
fread(&F_, 1, 1, fp);
fread(&B_, 1, 1, fp);
fread(&C_, 1, 1, fp);
fread(&D_, 1, 1, fp);
fread(&E_, 1, 1, fp);
fread(&H_, 1, 1, fp);
fread(&L_, 1, 1, fp);
fread(&I, 1, 1, fp);
fread(&R, 1, 1, fp);
fread(&R7, 1, 1, fp);
fread(&IFF1, 1, 1, fp);
fread(&IFF2, 1, 1, fp);
fread(&IM, 1, 1, fp);
fread_unsigned_short_little_endian(&PC, fp);
fread_unsigned_short_little_endian(&IX, fp);
fread_unsigned_short_little_endian(&IY, fp);
fread_unsigned_short_little_endian(&SP, fp);
/* Hardware */
fread_int_little_endian(&NMI_generator, fp);
fread_int_little_endian(&hsync_counter, fp);
fread_int_little_endian(&int_pending, fp);
fread_int_little_endian(&nmi_pending, fp);
/* 65654/0x10076 bytes to here for 2.1.7 */
} else {
if (method == LOAD_FILE_METHOD_AUTOLOAD ||
method == LOAD_FILE_METHOD_FORCEDLOAD) {
/* To duplicate these values: in mainloop in z80.c around
* line 189, change #if 0 to #if 1 and recompile. Run
* the emulator, load a suitably sized program by typing
* LOAD or LOAD "something" and view the console output.
*
* It's likely I've been a bit too thorough recording these
* values because I know from looking at the ZX81 ROM that
* DE points to the program name in memory which would get
* overwritten on LOAD and make it redundant, and HL and A
* are modified soon after anyway, but there's no harm done.
*
* Note that the ZX81's RAMTOP won't be greater than 0x8000
* unless POKEd by the user.
*/
if (*sdl_emulator.model == MODEL_ZX80) {
/* Registers (common values) */
A = 0x00; F = 0x44; B = 0x00; C = 0x00;
D = 0x07; E = 0xae; H = 0x40; L = 0x2a;
PC = 0x0283;
IX = 0x0000; IY = 0x4000; I = 0x0e; R7 = 0xdd;
A_ = 0x00; F_ = 0x00; B_ = 0x00; C_ = 0x21;
D_ = 0xd8; E_ = 0xf0; H_ = 0xd8; L_ = 0xf0;
IFF1 = 0x00; IFF2 = 0x00; IM = 1;
R = 0x6a;
/* Machine Stack (common values) */
if (sdl_emulator.ramsize >= 16) {
SP = 0x8000 - 4;
} else {
SP = 0x4000 - 4 + sdl_emulator.ramsize * 1024;
}
mem[SP + 0] = 0x47;
mem[SP + 1] = 0x04;
mem[SP + 2] = 0xba;
mem[SP + 3] = 0x3f;
/* Now override if RAM configuration changes things
* (there's a possibility these changes are unimportant) */
if (sdl_emulator.ramsize == 16) {
mem[SP + 2] = 0x22;
}
} else if (*sdl_emulator.model == MODEL_ZX81 && !scp) {
#ifdef ZXMORE
PC = 0x0676;
#else
/* Registers (common values) */
A = 0x0b; F = 0x00; B = 0x00; C = 0x02;
D = 0x40; E = 0x9b; H = 0x40; L = 0x99;
PC = zx81.machine==MACHINELAMBDA ? 0x0221 : 0x0207;
IX = 0x0281; IY = 0x4000; I = 0x1e; R7 = 0xdd;
A_ = 0xf8; F_ = 0xa9; B_ = 0x00; C_ = 0x00;
D_ = 0x00; E_ = 0x2b; H_ = 0x00; L_ = 0x00;
IFF1 = 0; IFF2 = 0; IM = 1;
R = 0xa4;
/* GOSUB Stack (common values) */
if (sdl_emulator.ramsize >= 16) {
SP = 0x8000 - 4;
} else {
SP = 0x4000 - 4 + sdl_emulator.ramsize * 1024;
}
mem[SP + 0] = 0x76;
mem[SP + 1] = 0x06;
mem[SP + 2] = 0x00;
mem[SP + 3] = 0x3e;
#if 0
// SP = mem[0x4004] + (mem[0x4005]<<8) - 4;
SP = 0xc000 - 4;
mem[SP + 0] = 0x85;
mem[SP + 1] = 0x07;
#endif
/* Now override if RAM configuration changes things
* (there's a possibility these changes are unimportant) */
if (sdl_emulator.ramsize >= 4 && 0) {
D = 0x43; H = 0x43;
A_ = 0xec; B_ = 0x81; C_ = 0x02;
R = 0xa9;
}
/* System variables */
mem[0x4000] = 0xff; /* ERR_NR */
mem[0x4001] = 0x80; /* FLAGS */
mem[0x4002] = SP & 0xff; /* ERR_SP lo */
mem[0x4003] = SP >> 8; /* ERR_SP hi */
mem[0x4004] = (SP + 4) & 0xff; /* RAMTOP lo */
mem[0x4005] = (SP + 4) >> 8; /* RAMTOP hi */
mem[0x4006] = 0x00; /* MODE */
mem[0x4007] = 0xfe; /* PPC lo */
mem[0x4008] = 0xff; /* PPC hi */
#endif
}
}
/* Read in up to 48K of data */
if (sdl_emulator.ramsize > 48) {
ramsize = 48;
} else {
ramsize = sdl_emulator.ramsize;
}
if (*sdl_emulator.model == MODEL_ZX80) {
fread(mem + 0x4000 + zxmoffset, 1, ramsize * 1024, fp);
} else if (*sdl_emulator.model == MODEL_ZX81) {
if (scp) {
#ifdef ZXMORE
if (flash)
fread(mem + addr + zxmoffset - 0x80000, 1, ramsize * 1024, fp);
else
fread(mem + addr + zxmoffset, 1, ramsize * 1024, fp);
#else
fread(mem + addr, 1, ramsize * 1024, fp);
#endif
} else {
fread(mem + 0x4009 + zxmoffset, 1, ramsize * 1024 - 9, fp);
if (!sdl_com_line.nxtlin) mem[0x4029] = mem[0x402a] = 0;
if (sdl_com_line.wsz81mem==TRUE) memcpy(sz81mem, mem+0x4000, 0x4000);
}
}
/* Copy fullpath across to the load file dialog as
* then we have a record of what was last loaded */
strcpy(load_file_dialog.loaded, fullpath);
}
/* Close the file now as we've finished with it */
fclose(fp);
if (scp && addr==0) {
z80_reset();
}
break;
} else {
if (!(method == LOAD_FILE_METHOD_NAMEDLOAD && count == 0)) {
retval = TRUE;
break;
}
}
}
if (retval) {
if (method == LOAD_FILE_METHOD_AUTOLOAD) {
fprintf(stderr, "%s: Cannot read from %s\n", __func__, fullpath);
} else if (method == LOAD_FILE_METHOD_NAMEDLOAD ||
method == LOAD_FILE_METHOD_SELECTLOAD ||
method == LOAD_FILE_METHOD_FORCEDLOAD ||
method == LOAD_FILE_METHOD_STATELOAD) {
/* Warn the user via the GUI that the load failed */
if (method == LOAD_FILE_METHOD_STATELOAD) {
strcpy(notification.title, "Load State");
} else {
strcpy(notification.title, "Load");
}
strcpy(notification.text, "Failed");
notification.timeout = NOTIFICATION_TIMEOUT_1250;
notification_show(NOTIFICATION_SHOW, ¬ification);
}
}
}
if (method != LOAD_FILE_METHOD_STATELOAD) {
/* We've finished with the load file dialog now */
load_file_dialog.method = LOAD_FILE_METHOD_NONE;
}
return retval;
}
/***************************************************************************
* Append Directory Delimiter to String *
***************************************************************************/
/* This will append a directory delimiter to a string if one doesn't already
* exist.
* The point of this function is to make it easier to change the delimiter
* character for other platforms. See also #define DIR_DELIMITER_CHAR.
*
* On entry: char *toappendto = the string to append to
* On exit: toappendto will have had a delimiter appended if required */
void strcatdelimiter(char *toappendto) {
static char delimiter[2] = {DIR_DELIMITER_CHAR, 0};
#if defined(__amigaos4__)
if (toappendto[strlen(toappendto) - 1] != DIR_DELIMITER_CHAR &&
toappendto[strlen(toappendto) - 1] != ':')
strcat(toappendto, delimiter);
#else
if (toappendto[strlen(toappendto) - 1] != DIR_DELIMITER_CHAR)
strcat(toappendto, delimiter);
#endif
}
/***************************************************************************
* File Dialog Basename *
***************************************************************************/
/* This will return the basename of a path e.g. "/moo/bah" returns "bah".
* The paths being passed are coming from getcwd or something other than
* user written so it is not expecting a trailing directory delimiter.
* getcwd for example will not add a trailing delimiter unless the path
* is at the root, therefore that's how it will be interpreted here.
*
* On entry: char *dir = the path to extract the basename from
* On exit: returns a pointer to a string containing the extracted basename */
char *file_dialog_basename(char *dir) {
static char basename[256];
int index;
strcpy(basename, dir); /* We'll work with a copy */
if ((index = strlen(dir))) {
/* Move leftwards up to a delimiter, root or nothing */
while (index >= 1 &&
basename[index - 1] != DIR_DELIMITER_CHAR
#if defined(__amigaos4__)
&& basename[index - 1] != ':'
#endif
) index--;
/* Do we need to return root? */
if (index >= 1 && basename[index] == 0 &&
#if defined(__amigaos4__)
basename[index - 1] == ':'
#else
basename[index - 1] == DIR_DELIMITER_CHAR
#endif
) index--;
}
return basename + index;
}
/***************************************************************************
* File Dialog Change Directory *
***************************************************************************/
/* This function will update a target directory string with another source
* string which could be a relative subdirectory or parent directory "..".
* The source directory can be wrapped within brackets which will be removed
* if found e.g. "(sz81)", "sz81", "(..)", "..".
*
* This function is not expecting dir to have a trailing directory delimiter
* unless it represents *nix root. C functions don't add these and neither
* do sz81 functions so I'm choosing not to handle them.
*
* On entry: char *dir = the directory string to update
* char *direntry = the relative directory to change to
* On exit: char *dir will be updated */
void file_dialog_cd(char *dir, char *direntry) {
char foldername[256];
int index;
/* Copy the direntry and strip the surrounding brackets if found */
if (*direntry == '(') {
strcpy(foldername, direntry + 1);
foldername[strlen(foldername) - 1] = 0;
} else {
strcpy(foldername, direntry);
}
/* Is this the parent directory? */
if (strcmp(foldername, "..") == 0) {
/* Go back to the parent directory */
if ((index = strlen(dir))) {
/* Truncate leftwards up to a delimiter, root or nothing */
while (index >= 1 &&
dir[index - 1] != DIR_DELIMITER_CHAR
#if defined(__amigaos4__) || defined(_WIN32)
&& dir[index - 1] != ':'
#endif
) dir[--index] = 0;
/* Do we need to cut the directory delimiter? */
if (index >= 2 &&
dir[index - 1] == DIR_DELIMITER_CHAR
#if defined(_WIN32)
&& dir[index - 2] != ':'
#endif
) dir[--index] = 0;
}
} else {
/* It's a subdirectory */
/* Add a directory delimiter if required */
strcatdelimiter(dir);
strcat(dir, foldername);
}
}
/***************************************************************************
* Directory List Populate *
***************************************************************************/
/* This function will record the current directory, change to the requested
* directory, create a sorted list of its contents and then change back to
* the original directory.
*
* Directory names are wrapped within brackets and a parent directory
* "(..)" is forced to always be present (dirlist will never return NULL).
*
* dirlist will return a pointer to the dynamically allocated memory
* containing the list and should be freed before program exit.
* When calling this function, any existing list created from a previous
* call will be freed automatically so the caller need not do this.
*
* On entry: char *dir = a string containing the directory to list
* int filetypes = an OR'd combination of file types to list
* On exit: char **dirlist will point to the dynamically allocated
* directory list
* int *dirlist_sizeof will contain the size of each item
* int *dirlist_count will contain the number of items found */
void dirlist_populate(char *dir, char **dirlist, int *dirlist_sizeof,
int *dirlist_count, int filetypes) {
char cwd[256], *swap=NULL, *realloclist;
int parentfound = FALSE, offset = 0;
int count, found, swapped;
struct dirent *direntry;
struct stat filestatus;
DIR *dirstream;
/* [Re]initialise the list */
if (*dirlist) {
free(*dirlist); *dirlist = NULL;
}
*dirlist_sizeof = 0; *dirlist_count = 0;
/* Record the current working directory before changing it to dir
* (I've found that stat doesn't work unless the dir is changed) */
strcpy(cwd, ""); getcwd(cwd, 256); cwd[255] = 0; chdir(dir);
/* NOTE TO PORTERS: eventually you'll hit root ('/' on *nix, ':' on
* __amigaos4__, '\' on _WIN32 and as I'm developing this on Linux I
* can't populate the list with drive specifiers etc., so possibly
* you could have a virtual area above root that you fill yourself,
* or you could populate the root list with "(c:)", "(d:)" etc. and
* when the user selects one, you could change to that drive and set
* dir equal to root. Other functions you should take a look at are
* file_dialog_cd, the code within sdl_input.c:2555 that extracts the
* directory from the list to change to and file_dialog_basename. And
* DIR_DELIMITER_CHAR and strcatdelimiter are useful. That's it :) */
if ((dirstream = opendir(dir))) {
while ((direntry = readdir(dirstream))) {
/* Store the size of the list element once */
if (*dirlist_sizeof == 0) {
*dirlist_sizeof = sizeof(direntry->d_name) + 2;
swap = malloc(*dirlist_sizeof);
}
/* Get directory entry status information */
if (stat(direntry->d_name, &filestatus)) {
fprintf(stderr, "%s: Cannot stat %s\n", __func__, direntry->d_name);