forked from libretro/xmil-libretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsave.c
750 lines (612 loc) · 13.8 KB
/
statsave.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
#include "compiler.h"
#include "strres.h"
#include "dosio.h"
#include "scrnmng.h"
#include "soundmng.h"
#include "timemng.h"
#include "z80core.h"
#include "pccore.h"
#include "iocore.h"
#include "nevent.h"
#include "ievent.h"
#include "calendar.h"
#include "statsave.h"
#include "vram.h"
#include "palettes.h"
#include "makescrn.h"
#include "sound.h"
#include "sndctrl.h"
#include "fddfile.h"
#if defined(MACOS)
#define CRCONST str_cr
#elif defined(WIN32) || defined(X11) || defined(SLZAURUS)
#define CRCONST str_lf
#else
#define CRCONST str_crlf
#endif
typedef struct {
char name[16];
char vername[28];
UINT32 ver;
} NP2FHDR;
typedef struct {
char index[10];
UINT16 ver;
UINT32 size;
} NP2FENT;
enum {
STATFLAG_BIN = 0,
STATFLAG_TERM,
STATFLAG_DISK,
STATFLAG_EVT
};
typedef struct {
UINT32 id;
INTPTR proc;
} PROCTBL;
typedef struct {
UINT32 id;
UINT evt;
} ENUMTBL;
#define PROCID(a, b, c, d) (((d) << 24) + ((c) << 16) + ((b) << 8) + (a))
#include "statsave.tbl"
/* ---- */
/* 関数ポインタを intに変更。 */
static UINT32 proc2id(INTPTR func, const PROCTBL *tbl, UINT count) {
while(count) {
count--;
if (func == tbl->proc) {
return(tbl->id);
}
tbl++;
}
return(0);
}
static INTPTR id2proc(UINT32 id, const PROCTBL *tbl, UINT count) {
while(count) {
count--;
if (id == tbl->id) {
return(tbl->proc);
}
tbl++;
}
return((INTPTR)NULL);
}
/* ---- */
enum {
SFFILEH_WRITE = 0x0001,
SFFILEH_BLOCK = 0x0002,
SFFILEH_ERROR = 0x0004
};
typedef struct {
_STFLAGH sfh;
UINT stat;
FILEH fh;
UINT secpos;
NP2FHDR f;
} _SFFILEH, *SFFILEH;
static SFFILEH statflag_open(FILEH fh,
OEMCHAR *err, UINT errlen) {
SFFILEH ret;
ret = (SFFILEH)_MALLOC(sizeof(_SFFILEH), filename);
if (ret == NULL) {
goto sfo_err2;
}
if ((file_read(fh, &ret->f, sizeof(NP2FHDR)) == sizeof(NP2FHDR)) &&
(!memcmp(&ret->f, &np2flagdef, sizeof(np2flagdef)))) {
ZeroMemory(ret, sizeof(_SFFILEH));
ret->fh = fh;
ret->secpos = sizeof(NP2FHDR);
if ((err) && (errlen > 0)) {
err[0] = '\0';
ret->sfh.err = err;
ret->sfh.errlen = errlen;
}
return(ret);
}
_MFREE(ret);
sfo_err2:
file_close(fh);
sfo_err1:
return(NULL);
}
static int statflag_closesection(SFFILEH sffh) {
UINT leng;
UINT8 zero[16];
if (sffh == NULL) {
goto sfcs_err1;
}
if (sffh->stat == (SFFILEH_BLOCK | SFFILEH_WRITE)) {
leng = (0 - sffh->sfh.hdr.size) & 15;
if (leng) {
ZeroMemory(zero, sizeof(zero));
if (file_write(sffh->fh, zero, leng) != leng) {
goto sfcs_err2;
}
}
if ((file_seek(sffh->fh, (long)sffh->secpos, FSEEK_SET)
!= (long)sffh->secpos) ||
(file_write(sffh->fh, &sffh->sfh.hdr, sizeof(sffh->sfh.hdr))
!= sizeof(sffh->sfh.hdr))) {
goto sfcs_err2;
}
}
if (sffh->stat & SFFILEH_BLOCK) {
sffh->stat &= ~SFFILEH_BLOCK;
sffh->secpos += sizeof(sffh->sfh.hdr) +
((sffh->sfh.hdr.size + 15) & (~15));
if (file_seek(sffh->fh, (long)sffh->secpos, FSEEK_SET)
!= (long)sffh->secpos) {
goto sfcs_err2;
}
}
return(STATFLAG_SUCCESS);
sfcs_err2:
sffh->stat = SFFILEH_ERROR;
sfcs_err1:
return(STATFLAG_FAILURE);
}
static int statflag_readsection(SFFILEH sffh) {
int ret;
ret = statflag_closesection(sffh);
if (ret != STATFLAG_SUCCESS) {
return(ret);
}
if ((sffh->stat == 0) &&
(file_read(sffh->fh, &sffh->sfh.hdr, sizeof(sffh->sfh.hdr))
== sizeof(sffh->sfh.hdr))) {
sffh->stat = SFFILEH_BLOCK;
sffh->sfh.pos = 0;
return(STATFLAG_SUCCESS);
}
sffh->stat = SFFILEH_ERROR;
return(STATFLAG_FAILURE);
}
int statflag_read(STFLAGH sfh, void *buf, UINT size) {
if ((sfh == NULL) || (buf == NULL) ||
((sfh->pos + size) > sfh->hdr.size)) {
goto sfr_err;
}
if (size) {
if (file_read(((SFFILEH)sfh)->fh, buf, size) != size) {
goto sfr_err;
}
sfh->pos += size;
}
return(STATFLAG_SUCCESS);
sfr_err:
return(STATFLAG_FAILURE);
}
static SFFILEH statflag_create(FILEH fh) {
SFFILEH ret;
ret = (SFFILEH)_MALLOC(sizeof(_SFFILEH), filename);
if (ret == NULL) {
goto sfc_err1;
}
if (file_write(fh, &np2flagdef, sizeof(NP2FHDR)) == sizeof(NP2FHDR)) {
ZeroMemory(ret, sizeof(_SFFILEH));
ret->stat = SFFILEH_WRITE;
ret->fh = fh;
ret->secpos = sizeof(NP2FHDR);
return(ret);
}
file_close(fh);
sfc_err2:
_MFREE(ret);
sfc_err1:
return(NULL);
}
static int statflag_createsection(SFFILEH sffh, const SFENTRY *tbl) {
int ret;
ret = statflag_closesection(sffh);
if (ret != STATFLAG_SUCCESS) {
return(ret);
}
if (sffh->stat != SFFILEH_WRITE) {
sffh->stat = SFFILEH_ERROR;
return(STATFLAG_FAILURE);
}
CopyMemory(sffh->sfh.hdr.index, tbl->index, sizeof(sffh->sfh.hdr.index));
sffh->sfh.hdr.ver = tbl->ver;
sffh->sfh.hdr.size = 0;
return(STATFLAG_SUCCESS);
}
int statflag_write(STFLAGH sfh, const void *buf, UINT size) {
SFFILEH sffh;
if (sfh == NULL) {
goto sfw_err1;
}
sffh = (SFFILEH)sfh;
if (!(sffh->stat & SFFILEH_WRITE)) {
goto sfw_err2;
}
if (!(sffh->stat & SFFILEH_BLOCK)) {
sffh->stat |= SFFILEH_BLOCK;
sfh->pos = 0;
if (file_write(sffh->fh, &sfh->hdr, sizeof(sfh->hdr))
!= sizeof(sfh->hdr)) {
goto sfw_err2;
}
}
if (size) {
if ((buf == NULL) || (file_write(sffh->fh, buf, size) != size)) {
goto sfw_err2;
}
sfh->pos += size;
if (sfh->hdr.size < sfh->pos) {
sfh->hdr.size = sfh->pos;
}
}
return(STATFLAG_SUCCESS);
sfw_err2:
sffh->stat = SFFILEH_ERROR;
sfw_err1:
return(STATFLAG_FAILURE);
}
static void statflag_close(SFFILEH sffh) {
if (sffh) {
statflag_closesection(sffh);
file_close(sffh->fh);
_MFREE(sffh);
}
}
void statflag_seterr(STFLAGH sfh, const OEMCHAR *str) {
if ((sfh) && (sfh->errlen)) {
milstr_ncat(sfh->err, str, sfh->errlen);
milstr_ncat(sfh->err, CRCONST, sfh->errlen);
}
}
/* common */
static int flagsave_common(STFLAGH sfh, const SFENTRY *tbl) {
return(statflag_write(sfh, tbl->arg1, tbl->arg2));
}
static int flagload_common(STFLAGH sfh, const SFENTRY *tbl) {
return(statflag_read(sfh, tbl->arg1, tbl->arg2));
}
/* event */
typedef struct {
UINT32 next;
SINT32 clock;
SINT32 baseclock;
UINT32 proc;
} NEVTITEM;
typedef struct {
NEVTITEM item[NEVENT_MAXEVENTS];
UINT32 first;
} NEVTSAVE;
static UINT32 evt2id(NEVENTITEM next) {
if (next == NEVENTITEM_NONE) {
return((UINT32)-2);
}
else if (next == NEVENTITEM_TERM) {
return((UINT32)-1);
}
else {
return((UINT32)(next - nevent.item));
}
}
static NEVENTITEM id2evt(UINT32 n) {
if (n == (UINT32)-2) {
return(NEVENTITEM_NONE);
}
else if (n == (UINT32)-1) {
return(NEVENTITEM_TERM);
}
else if (n < NEVENT_MAXEVENTS) {
return(nevent.item + n);
}
else {
/* error! */
return(NEVENTITEM_TERM);
}
}
static int flagsave_evt(STFLAGH sfh, const SFENTRY *tbl) {
NEVTSAVE nevt;
UINT i;
for (i=0; i<NEVENT_MAXEVENTS; i++) {
nevt.item[i].next = evt2id(nevent.item[i].next);
nevt.item[i].clock = nevent.item[i].clock;
nevt.item[i].baseclock = nevent.item[i].baseclock;
nevt.item[i].proc = proc2id((INTPTR)nevent.item[i].proc,
evtproc, NELEMENTS(evtproc));
}
nevt.first = evt2id(nevent.first);
(void)tbl;
return(statflag_write(sfh, &nevt, sizeof(nevt)));
}
static int flagload_evt(STFLAGH sfh, const SFENTRY *tbl) {
NEVTSAVE nevt;
int ret;
UINT i;
ret = statflag_read(sfh, &nevt, sizeof(nevt));
for (i=0; i<NEVENT_MAXEVENTS; i++) {
nevent.item[i].next = id2evt(nevt.item[i].next);
nevent.item[i].clock = nevt.item[i].clock;
nevent.item[i].baseclock = nevt.item[i].baseclock;
nevent.item[i].proc = (NEVENTCB)id2proc(nevt.item[i].proc,
evtproc, NELEMENTS(evtproc));
}
nevent.first = id2evt(nevt.first);
(void)tbl;
return(ret);
}
/* disk */
typedef struct {
OEMCHAR fname[MAX_PATH];
UINT32 ftype;
UINT readonly;
DOSDATE date;
DOSTIME time;
} STATDISK;
static const OEMCHAR str_fddx[] = OEMTEXT("FDD%u");
static const OEMCHAR str_updated[] = OEMTEXT("%s: updated");
static const OEMCHAR str_notfound[] = OEMTEXT("%s: not found");
static int disksave(STFLAGH sfh, const _FDDFILE *fdd) {
STATDISK st;
FILEH fh;
ZeroMemory(&st, sizeof(st));
if (fdd->type != DISKTYPE_NOTREADY) {
file_cpyname(st.fname, fdd->fname, NELEMENTS(st.fname));
st.ftype = fdd->ftype;
st.readonly = fdd->protect;
fh = file_open_rb(st.fname);
if (fh != FILEH_INVALID) {
file_getdatetime(fh, &st.date, &st.time);
file_close(fh);
}
}
return(statflag_write(sfh, &st, sizeof(st)));
}
static int flagsave_disk(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
REG8 i;
ret = STATFLAG_SUCCESS;
for (i=0; i<MAX_FDDFILE; i++) {
ret |= disksave(sfh, fddfile + i);
}
(void)tbl;
return(ret);
}
static int diskcheck(STFLAGH sfh, const OEMCHAR *name) {
int ret;
FILEH fh;
STATDISK st;
OEMCHAR buf[256];
DOSDATE date;
DOSTIME time;
ret = statflag_read(sfh, &st, sizeof(st));
if (st.fname[0]) {
fh = file_open_rb(st.fname);
if (fh != FILEH_INVALID) {
file_getdatetime(fh, &date, &time);
file_close(fh);
if ((memcmp(&st.date, &date, sizeof(date))) ||
(memcmp(&st.time, &time, sizeof(time)))) {
ret |= STATFLAG_DISKCHG;
OEMSPRINTF(buf, str_updated, name);
statflag_seterr(sfh, buf);
}
}
else {
ret |= STATFLAG_DISKCHG;
OEMSPRINTF(buf, str_notfound, name);
statflag_seterr(sfh, buf);
}
}
return(ret);
}
static int flagcheck_disk(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
int i;
OEMCHAR buf[8];
ret = 0;
for (i=0; i<MAX_FDDFILE; i++) {
OEMSPRINTF(buf, str_fddx, i+1);
ret |= diskcheck(sfh, buf);
}
(void)tbl;
return(ret);
}
static int flagload_disk(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
UINT8 i;
STATDISK st;
ret = STATFLAG_SUCCESS;
for (i=0; i<MAX_FDDFILE; i++) {
ret |= statflag_read(sfh, &st, sizeof(st));
if (st.fname[0]) {
fddfile_set(i, st.fname, st.ftype, st.readonly);
}
}
(void)tbl;
return(ret);
}
/* checker */
static int flagcheck_versize(STFLAGH sfh, const SFENTRY *tbl) {
if ((sfh->hdr.ver == tbl->ver) && (sfh->hdr.size == tbl->arg2)) {
return(STATFLAG_SUCCESS);
}
return(STATFLAG_FAILURE);
}
static int flagcheck_veronly(STFLAGH sfh, const SFENTRY *tbl) {
if (sfh->hdr.ver == tbl->ver) {
return(STATFLAG_SUCCESS);
}
return(STATFLAG_FAILURE);
}
/* interface */
int statsave_save_fh(FILEH fh) {
SFFILEH sffh;
int ret;
const SFENTRY *tbl;
const SFENTRY *tblterm;
sffh = statflag_create(fh);
if (sffh == NULL) {
return(STATFLAG_FAILURE);
}
ret = STATFLAG_SUCCESS;
tbl = xmiltbl;
tblterm = tbl + (sizeof(xmiltbl)/sizeof(SFENTRY));
while(tbl < tblterm) {
ret |= statflag_createsection(sffh, tbl);
switch(tbl->type) {
case STATFLAG_BIN:
case STATFLAG_TERM:
ret |= flagsave_common(&sffh->sfh, tbl);
break;
case STATFLAG_DISK:
ret |= flagsave_disk(&sffh->sfh, tbl);
break;
case STATFLAG_EVT:
ret |= flagsave_evt(&sffh->sfh, tbl);
break;
}
tbl++;
}
statflag_close(sffh);
return(ret);
}
#ifndef __LIBRETRO__
int statsave_save(const OEMCHAR *filename) {
FILEH fh;
fh = file_create(filename);
if (fh == FILEH_INVALID) {
return(STATFLAG_FAILURE);
}
int ret = statsave_save_fh(fh);
if (ret < 0)
file_delete(filename);
return ret;
}
#endif
int statsave_check_fh(FILEH fh, OEMCHAR *buf, UINT size) {
SFFILEH sffh;
int ret;
BOOL done;
const SFENTRY *tbl;
const SFENTRY *tblterm;
sffh = statflag_open(fh, buf, size);
if (sffh == NULL) {
return(STATFLAG_FAILURE);
}
done = FALSE;
ret = STATFLAG_SUCCESS;
while((!done) && (ret != STATFLAG_FAILURE)) {
ret |= statflag_readsection(sffh);
tbl = xmiltbl;
tblterm = tbl + (sizeof(xmiltbl)/sizeof(SFENTRY));
while(tbl < tblterm) {
if (!memcmp(sffh->sfh.hdr.index, tbl->index, 10)) {
break;
}
tbl++;
}
if (tbl < tblterm) {
switch(tbl->type) {
case STATFLAG_BIN:
ret |= flagcheck_versize(&sffh->sfh, tbl);
break;
case STATFLAG_TERM:
done = TRUE;
break;
case STATFLAG_EVT:
ret |= flagcheck_veronly(&sffh->sfh, tbl);
break;
case STATFLAG_DISK:
ret |= flagcheck_disk(&sffh->sfh, tbl);
break;
default:
ret |= STATFLAG_WARNING;
break;
}
}
else {
ret |= STATFLAG_WARNING;
}
}
statflag_close(sffh);
return(ret);
}
int statsave_check(const OEMCHAR *filename, OEMCHAR *buf, UINT size) {
FILEH fh = file_open_rb(filename);
if (fh == FILEH_INVALID) {
return(STATFLAG_FAILURE);
}
return statsave_check_fh(fh, buf, size);
}
int statsave_load_fh(FILEH fh) {
SFFILEH sffh;
int ret;
BOOL done;
const SFENTRY *tbl;
const SFENTRY *tblterm;
sffh = statflag_open(fh, NULL, 0);
if (sffh == NULL) {
return(STATFLAG_FAILURE);
}
/* PCCORE read! */
ret = statflag_readsection(sffh);
if ((ret != STATFLAG_SUCCESS) ||
(memcmp(sffh->sfh.hdr.index, xmiltbl[0].index, 10))) {
statflag_close(sffh);
return(STATFLAG_FAILURE);
}
soundmng_stop();
ret |= flagload_common(&sffh->sfh, xmiltbl);
sound_changeclock();
sound_reset();
Z80_RESET();
nevent_allreset();
ievent_reset();
calendar_reset();
iocore_reset();
done = FALSE;
while((!done) && (ret != STATFLAG_FAILURE)) {
ret |= statflag_readsection(sffh);
tbl = xmiltbl + 1;
tblterm = xmiltbl + (sizeof(xmiltbl)/sizeof(SFENTRY));
while(tbl < tblterm) {
if (!memcmp(sffh->sfh.hdr.index, tbl->index, 10)) {
break;
}
tbl++;
}
if (tbl < tblterm) {
switch(tbl->type) {
case STATFLAG_BIN:
ret |= flagload_common(&sffh->sfh, tbl);
break;
case STATFLAG_TERM:
done = TRUE;
break;
case STATFLAG_DISK:
ret |= flagload_disk(&sffh->sfh, tbl);
break;
case STATFLAG_EVT:
ret |= flagload_evt(&sffh->sfh, tbl);
break;
default:
ret |= STATFLAG_WARNING;
break;
}
}
else {
ret |= STATFLAG_WARNING;
}
}
statflag_close(sffh);
memio_update();
crtc_update();
sndboard_update();
pal_reset();
makescrn_reset();
soundmng_play();
return(ret);
}
int statsave_load(const OEMCHAR *filename) {
FILEH fh = file_open_rb(filename);
if (fh == FILEH_INVALID) {
return(STATFLAG_FAILURE);
}
return statsave_load_fh(fh);
}