-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsave.c
1940 lines (1643 loc) · 41.3 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
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
/**
* @file statsave.cpp
* @brief Implementation of State save
*/
#include "compiler.h"
#include "statsave.h"
#include "strres.h"
#include "dosio.h"
#include "commng.h"
#include "scrnmng.h"
#include "soundmng.h"
#include "timemng.h"
#include "cpucore.h"
#include "pccore.h"
#include "iocore.h"
#include "gdc_sub.h"
#include "cbuscore.h"
#include "ideio.h"
#include "sasiio.h"
#include "scsiio.h"
#include "pc9861k.h"
#include "mpu98ii.h"
#if defined(SUPPORT_SMPU98)
#include "smpu98.h"
#endif
#include "board14.h"
#include "amd98.h"
#include "bios/bios.h"
#include "vram.h"
#include "palettes.h"
#include "maketext.h"
#include "sound.h"
#include "fmboard.h"
#ifdef SUPPORT_SOUND_SB16
#include "ct1741io.h"
#endif
#include "beep.h"
#include "diskimage/fddfile.h"
#include "fdd/fdd_mtr.h"
#include "fdd/sxsi.h"
#include "font/font.h"
#include "keydisp.h"
#include "hostdrv.h"
#include "calendar.h"
#include "keystat.h"
#if defined(SUPPORT_WAB)
#include "wab/wab.h"
#endif
#if defined(SUPPORT_CL_GD5430)
#include "wab/cirrus_vga_extern.h"
#endif
#if defined(SUPPORT_NET)
#include "network/net.h"
#endif
#if defined(SUPPORT_LGY98)
#include "network/lgy98.h"
#include "network/lgy98dev.h"
#endif
#if defined(CPUCORE_IA32)
#include "ia32/instructions/fpu/fp.h"
#endif
#if defined(BIOS_IO_EMULATION)
#include "bios/bios.h"
#endif
#if defined(SUPPORT_IA32_HAXM)
#include "i386hax/haxfunc.h"
#include "i386hax/haxcore.h"
#endif
#ifdef USE_MAME
UINT8 YMF262Read(void *chip, INT a);
INT YMF262Write(void *chip, INT a, INT v);
int YMF262FlagSave(void *chip, void *dstbuf);
int YMF262FlagLoad(void *chip, void *srcbuf, int size);
#endif
extern int sxsi_unittbl[];
#if defined(MACOS)
#define CRCONST str_cr
#elif defined(WIN32) || defined(X11)
#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;
/**
* @brief handle
*/
struct TagStatFlagHandle
{
NP2FENT hdr;
UINT pos;
OEMCHAR *err;
int errlen;
};
typedef struct TagStatFlagHandle _STFLAGH; /* define */
enum
{
STATFLAG_BIN = 0,
STATFLAG_TERM,
STATFLAG_COM,
STATFLAG_DMA,
STATFLAG_EGC,
STATFLAG_EPSON,
STATFLAG_EVT,
STATFLAG_EXT,
STATFLAG_FDD,
STATFLAG_FM,
STATFLAG_GIJ,
#if defined(SUPPORT_HOSTDRV)
STATFLAG_HDRV,
#endif
STATFLAG_MEM,
STATFLAG_SXSI,
STATFLAG_MASK = 0x3fff,
STATFLAG_BWD_COMPATIBLE = 0x4000, // このフラグが立っているとき、古いバージョンのステートセーブと互換性がある(足りないデータは0で埋められるので注意する)いまのところSTATFLAG_BINのみサポート
STATFLAG_FWD_COMPATIBLE = 0x8000, // このフラグが立っているとき、新しいバージョンのステートセーブと互換性がある(足りないデータは無かったことになるので注意する)いまのところSTATFLAG_BINのみサポート
};
typedef struct {
UINT32 id;
void *proc;
} PROCTBL;
typedef struct {
UINT32 id;
NEVENTID num;
} ENUMTBL;
#define PROCID(a, b, c, d) (((d) << 24) + ((c) << 16) + ((b) << 8) + (a))
#define PROC2NUM(a, b) proc2num(&(a), (b), sizeof(b)/sizeof(PROCTBL))
#define NUM2PROC(a, b) num2proc(&(a), (b), sizeof(b)/sizeof(PROCTBL))
#include "statsave.tbl"
extern COMMNG cm_mpu98;
#if defined(SUPPORT_SMPU98)
extern COMMNG cm_smpu98[];
#endif
extern COMMNG cm_rs232c;
typedef struct {
OEMCHAR *buf;
int remain;
} ERR_BUF;
// ----
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(const OEMCHAR *filename, OEMCHAR *err, int errlen) {
FILEH fh;
SFFILEH ret;
fh = file_open_rb(filename);
if (fh == FILEH_INVALID) {
goto sfo_err1;
}
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(const OEMCHAR *filename) {
SFFILEH ret;
FILEH fh;
ret = (SFFILEH)_MALLOC(sizeof(_SFFILEH), filename);
if (ret == NULL) {
goto sfc_err1;
}
fh = file_create(filename);
if (fh == FILEH_INVALID) {
goto sfc_err2;
}
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);
file_delete(filename);
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);
}
}
// ---- function
// 関数ポインタを intに変更。
static BRESULT proc2num(void *func, const PROCTBL *tbl, int size) {
int i;
for (i=0; i<size; i++) {
if (*(INTPTR *)func == (INTPTR)tbl->proc) {
*(INTPTR *)func = (INTPTR)tbl->id;
return(SUCCESS);
}
tbl++;
}
return(FAILURE);
}
static BRESULT num2proc(void *func, const PROCTBL *tbl, int size) {
int i;
for (i=0; i<size; i++) {
if (*(INTPTR *)func == (INTPTR)tbl->id) {
*(INTPTR *)func = (INTPTR)tbl->proc;
return(SUCCESS);
}
tbl++;
}
return(FAILURE);
}
// ---- file
typedef struct {
OEMCHAR path[MAX_PATH];
UINT ftype;
int readonly;
DOSDATE date;
DOSTIME time;
} STATPATH;
static const OEMCHAR str_updated[] = OEMTEXT("%s: updated");
static const OEMCHAR str_notfound[] = OEMTEXT("%s: not found");
static int statflag_writepath(STFLAGH sfh, const OEMCHAR *path,
UINT ftype, int readonly) {
STATPATH sp;
FILEH fh;
ZeroMemory(&sp, sizeof(sp));
if ((path) && (path[0])) {
file_cpyname(sp.path, path, NELEMENTS(sp.path));
sp.ftype = ftype;
sp.readonly = readonly;
fh = file_open_rb(path);
if (fh != FILEH_INVALID) {
file_getdatetime(fh, &sp.date, &sp.time);
file_close(fh);
}
}
return(statflag_write(sfh, &sp, sizeof(sp)));
}
static int statflag_checkpath(STFLAGH sfh, const OEMCHAR *dvname) {
int ret;
STATPATH sp;
FILEH fh;
OEMCHAR buf[256];
DOSDATE dosdate;
DOSTIME dostime;
ret = statflag_read(sfh, &sp, sizeof(sp));
if (sp.path[0]) {
fh = file_open_rb(sp.path);
if (fh != FILEH_INVALID) {
file_getdatetime(fh, &dosdate, &dostime);
file_close(fh);
if ((memcmp(&sp.date, &dosdate, sizeof(dosdate))) ||
(memcmp(&sp.time, &dostime, sizeof(dostime)))) {
ret |= STATFLAG_DISKCHG;
OEMSPRINTF(buf, str_updated, dvname);
statflag_seterr(sfh, buf);
}
}
else {
ret |= STATFLAG_DISKCHG;
OEMSPRINTF(buf, str_notfound, dvname);
statflag_seterr(sfh, buf);
}
}
return(ret);
}
// ---- 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) {
memset(tbl->arg1, 0, tbl->arg2);
return(statflag_read(sfh, tbl->arg1, min(tbl->arg2, sfh->hdr.size)));
}
// ---- memory
static int flagsave_mem(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
ret = statflag_write(sfh, mem, 0x110000);
ret |= statflag_write(sfh, mem + VRAM1_B, 0x18000);
ret |= statflag_write(sfh, mem + VRAM1_E, 0x8000);
(void)tbl;
return(ret);
}
static int flagload_mem(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
ret = statflag_read(sfh, mem, 0x110000);
ret |= statflag_read(sfh, mem + VRAM1_B, 0x18000);
ret |= statflag_read(sfh, mem + VRAM1_E, 0x8000);
(void)tbl;
return(ret);
}
// ---- dma
static int flagsave_dma(STFLAGH sfh, const SFENTRY *tbl) {
int i;
_DMAC dmabak;
dmabak = dmac;
for (i=0; i<4; i++) {
if ((PROC2NUM(dmabak.dmach[i].proc.outproc, dmaproc)) ||
(PROC2NUM(dmabak.dmach[i].proc.inproc, dmaproc)) ||
(PROC2NUM(dmabak.dmach[i].proc.extproc, dmaproc))) {
return(STATFLAG_FAILURE);
}
}
(void)tbl;
return(statflag_write(sfh, &dmabak, sizeof(dmabak)));
}
static int flagload_dma(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
int i;
ret = statflag_read(sfh, &dmac, sizeof(dmac));
for (i=0; i<4; i++) {
if (NUM2PROC(dmac.dmach[i].proc.outproc, dmaproc)) {
dmac.dmach[i].proc.outproc = dma_dummyout;
ret |= STATFLAG_WARNING;
}
if (NUM2PROC(dmac.dmach[i].proc.inproc, dmaproc)) {
dmac.dmach[i].proc.inproc = dma_dummyin;
ret |= STATFLAG_WARNING;
}
if (NUM2PROC(dmac.dmach[i].proc.extproc, dmaproc)) {
dmac.dmach[i].proc.extproc = dma_dummyproc;
ret |= STATFLAG_WARNING;
}
}
(void)tbl;
return(ret);
}
// ---- egc
static int flagsave_egc(STFLAGH sfh, const SFENTRY *tbl) {
_EGC egcbak;
egcbak = egc;
egcbak.inptr -= (INTPTR)egc.buf;
egcbak.outptr -= (INTPTR)egc.buf;
(void)tbl;
return(statflag_write(sfh, &egcbak, sizeof(egcbak)));
}
static int flagload_egc(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
ret = statflag_read(sfh, &egc, sizeof(egc));
egc.inptr += (INTPTR)egc.buf;
egc.outptr += (INTPTR)egc.buf;
(void)tbl;
return(ret);
}
// ---- epson
static int flagsave_epson(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
if (!(pccore.model & PCMODEL_EPSON)) {
return(STATFLAG_SUCCESS);
}
ret = statflag_write(sfh, &epsonio, sizeof(epsonio));
ret |= statflag_write(sfh, mem + 0x1c0000, 0x8000);
ret |= statflag_write(sfh, mem + 0x1e8000, 0x18000);
(void)tbl;
return(ret);
}
static int flagload_epson(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
ret = statflag_read(sfh, &epsonio, sizeof(epsonio));
ret |= statflag_read(sfh, mem + 0x1c0000, 0x8000);
ret |= statflag_read(sfh, mem + 0x1e8000, 0x18000);
(void)tbl;
return(ret);
}
// ---- event
typedef struct {
UINT readyevents;
UINT waitevents;
} NEVTSAVE;
typedef struct {
UINT32 id;
SINT32 clock;
UINT32 flag;
NEVENTCB proc;
} NEVTITEM;
static int nevent_write(STFLAGH sfh, NEVENTID num) {
NEVTITEM nit;
UINT i;
ZeroMemory(&nit, sizeof(nit));
for (i=0; i<NELEMENTS(evtnum); i++) {
if (evtnum[i].num == num) {
nit.id = evtnum[i].id;
break;
}
}
nit.clock = g_nevent.item[num].clock;
nit.flag = g_nevent.item[num].flag;
nit.proc = g_nevent.item[num].proc;
if (PROC2NUM(nit.proc, evtproc)) {
nit.proc = NULL;
}
return(statflag_write(sfh, &nit, sizeof(nit)));
}
static int flagsave_evt(STFLAGH sfh, const SFENTRY *tbl) {
NEVTSAVE nevt;
int ret;
UINT i;
nevt.readyevents = g_nevent.readyevents;
nevt.waitevents = g_nevent.waitevents;
ret = statflag_write(sfh, &nevt, sizeof(nevt));
for (i=0; i<nevt.readyevents; i++) {
ret |= nevent_write(sfh, g_nevent.level[i]);
}
for (i=0; i<nevt.waitevents; i++) {
ret |= nevent_write(sfh, g_nevent.waitevent[i]);
}
(void)tbl;
return(ret);
}
static int nevent_read(STFLAGH sfh, NEVENTID *tbl, UINT *pos) {
int ret;
NEVTITEM nit;
UINT i;
NEVENTID num;
ret = statflag_read(sfh, &nit, sizeof(nit));
for (i=0; i<NELEMENTS(evtnum); i++) {
if (nit.id == evtnum[i].id) {
break;
}
}
if (i < NELEMENTS(evtnum)) {
num = evtnum[i].num;
g_nevent.item[num].clock = nit.clock;
g_nevent.item[num].flag = nit.flag;
g_nevent.item[num].proc = nit.proc;
if (NUM2PROC(g_nevent.item[num].proc, evtproc)) {
ret |= STATFLAG_WARNING;
}
else {
tbl[*pos] = num;
(*pos)++;
}
}
else {
ret |= STATFLAG_WARNING;
}
return(ret);
}
static int flagload_evt(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
NEVTSAVE nevt;
UINT i;
ret = statflag_read(sfh, &nevt, sizeof(nevt));
g_nevent.readyevents = 0;
g_nevent.waitevents = 0;
for (i=0; i<nevt.readyevents; i++) {
ret |= nevent_read(sfh, g_nevent.level, &g_nevent.readyevents);
}
for (i=0; i<nevt.waitevents; i++) {
ret |= nevent_read(sfh, g_nevent.waitevent, &g_nevent.waitevents);
}
(void)tbl;
return(ret);
}
// ---- extmem
static int flagsave_ext(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
ret = STATFLAG_SUCCESS;
if (CPU_EXTMEM) {
ret = statflag_write(sfh, CPU_EXTMEM, CPU_EXTMEMSIZE);
}
(void)tbl;
return(ret);
}
static int flagload_ext(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
ret = STATFLAG_SUCCESS;
if (CPU_EXTMEM) {
ret = statflag_read(sfh, CPU_EXTMEM, CPU_EXTMEMSIZE);
}
(void)tbl;
return(ret);
}
// ---- gaiji
static int flagsave_gij(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
int i;
int j;
const UINT8 *fnt;
ret = STATFLAG_SUCCESS;
for (i=0; i<2; i++) {
fnt = fontrom + ((0x56 + (i << 7)) << 4);
for (j=0; j<0x80; j++) {
ret |= statflag_write(sfh, fnt, 32);
fnt += 0x1000;
}
}
(void)tbl;
return(ret);
}
static int flagload_gij(STFLAGH sfh, const SFENTRY *tbl) {
int ret;
int i;
int j;
UINT8 *fnt;
ret = 0;
for (i=0; i<2; i++) {
fnt = fontrom + ((0x56 + (i << 7)) << 4);
for (j=0; j<0x80; j++) {
ret |= statflag_read(sfh, fnt, 32);
fnt += 0x1000;
}
}
(void)tbl;
return(ret);
}
// ---- FM
#if !defined(DISABLE_SOUND)
/**
* chip flags
*/
enum
{
FLAG_MG = 0x0001,
FLAG_OPNA1 = 0x0002,
FLAG_OPNA2 = 0x0004,
#if defined(SUPPORT_PX)
FLAG_OPNA3 = 0x0008,
FLAG_OPNA4 = 0x0010,
FLAG_OPNA5 = 0x0020,
#endif /* defined(SUPPORT_PX) */
FLAG_AMD98 = 0x0040,
FLAG_PCM86 = 0x0080,
FLAG_CS4231 = 0x0100,
FLAG_OPL3 = 0x0200,
FLAG_SB16 = 0x0400
};
/**
* Gets flags
* @param[in] nSoundID The sound ID
* @return The flags
*/
static UINT GetSoundFlags(SOUNDID nSoundID)
{
switch (nSoundID)
{
case SOUNDID_PC_9801_14:
return FLAG_MG;
case SOUNDID_PC_9801_26K:
return FLAG_OPNA1;
case SOUNDID_PC_9801_86:
return FLAG_OPNA1 | FLAG_PCM86;
case SOUNDID_PC_9801_86_26K:
return FLAG_OPNA1 | FLAG_OPNA2 | FLAG_PCM86;
case SOUNDID_PC_9801_118:
return FLAG_OPNA1 | FLAG_OPL3 | FLAG_CS4231;
case SOUNDID_PC_9801_86_WSS:
return FLAG_OPNA1 | FLAG_PCM86 | FLAG_CS4231;
case SOUNDID_PC_9801_86_118:
return FLAG_OPNA1 | FLAG_OPNA2 | FLAG_OPL3 | FLAG_PCM86 | FLAG_CS4231;
case SOUNDID_MATE_X_PCM:
return FLAG_OPNA1 | FLAG_CS4231;
case SOUNDID_PC_9801_86_ADPCM:
return FLAG_OPNA1 | FLAG_PCM86;
case SOUNDID_WAVESTAR:
return FLAG_OPNA1 | FLAG_PCM86 | FLAG_CS4231;
case SOUNDID_SPEAKBOARD:
return FLAG_OPNA1;
case SOUNDID_SPARKBOARD:
return FLAG_OPNA1 | FLAG_OPNA2;
case SOUNDID_AMD98:
return FLAG_AMD98;
case SOUNDID_SOUNDORCHESTRA:
case SOUNDID_SOUNDORCHESTRAV:
return FLAG_OPNA1 | FLAG_OPL3;
#if defined(SUPPORT_SOUND_SB16)
case SOUNDID_SB16:
return FLAG_OPL3 | FLAG_SB16;
case SOUNDID_PC_9801_86_SB16:
return FLAG_OPNA1 | FLAG_PCM86 | FLAG_OPL3 | FLAG_SB16;
case SOUNDID_WSS_SB16:
return FLAG_CS4231 | FLAG_OPL3 | FLAG_SB16;
case SOUNDID_PC_9801_86_WSS_SB16:
return FLAG_OPNA1 | FLAG_PCM86 | FLAG_CS4231 | FLAG_OPL3 | FLAG_SB16;
case SOUNDID_PC_9801_118_SB16:
return FLAG_OPNA1 | FLAG_OPNA2 | FLAG_OPL3 | FLAG_PCM86 | FLAG_CS4231 | FLAG_SB16;
case SOUNDID_PC_9801_86_118_SB16:
return FLAG_OPNA1 | FLAG_OPNA2 | FLAG_PCM86 | FLAG_CS4231 | FLAG_OPL3 | FLAG_SB16;
#endif // defined(SUPPORT_SOUND_SB16)
#if defined(SUPPORT_PX)
case SOUNDID_PX1:
return FLAG_OPNA1 | FLAG_OPNA2 | FLAG_OPNA3 | FLAG_OPNA4;
case SOUNDID_PX2:
return FLAG_OPNA1 | FLAG_OPNA2 | FLAG_OPNA3 | FLAG_OPNA4 | FLAG_OPNA5 | FLAG_PCM86;
#endif /* defined(SUPPORT_PX) */
default:
return 0;
}
}
static int flagsave_fm(STFLAGH sfh, const SFENTRY *tbl)
{
int ret;
UINT nSaveFlags;
UINT i;
SOUNDID invalidSoundID = SOUNDID_INVALID;
UINT32 datalen;
ret = statflag_write(sfh, &invalidSoundID, sizeof(invalidSoundID));
ret = statflag_write(sfh, &g_nSoundID, sizeof(g_nSoundID));
nSaveFlags = GetSoundFlags(g_nSoundID);
if (nSaveFlags & FLAG_MG)
{
datalen = sizeof(g_musicgen);
ret |= statflag_write(sfh, &datalen, sizeof(datalen));
ret |= statflag_write(sfh, &g_musicgen, sizeof(g_musicgen));
}
for (i = 0; i < NELEMENTS(g_opna); i++)
{
if (nSaveFlags & (FLAG_OPNA1 << i))
{
ret |= opna_sfsave(&g_opna[i], sfh, tbl);
}
}
if (nSaveFlags & FLAG_PCM86)
{
datalen = sizeof(g_pcm86);
ret |= statflag_write(sfh, &datalen, sizeof(datalen));
ret |= statflag_write(sfh, &g_pcm86, sizeof(g_pcm86));
}
if (nSaveFlags & FLAG_CS4231)
{
datalen = sizeof(cs4231);
ret |= statflag_write(sfh, &datalen, sizeof(datalen));
ret |= statflag_write(sfh, &cs4231, sizeof(cs4231));
}
if (nSaveFlags & FLAG_AMD98)
{
ret |= amd98_sfsave(sfh, tbl);
}
if (nSaveFlags & FLAG_OPL3)
{
for (i = 0; i < NELEMENTS(g_opl3); i++)
{
ret |= opl3_sfsave(&g_opl3[i], sfh, tbl);
}
#ifdef USE_MAME
{
void* buffer;
SINT32 bufsize = 0;
bufsize = YMF262FlagSave(NULL, NULL);
buffer = malloc(bufsize);
for (i = 0; i < NELEMENTS(g_mame_opl3); i++)
{
if(g_mame_opl3[i]){
YMF262FlagSave(g_mame_opl3[i], buffer);
ret |= statflag_write(sfh, &bufsize, sizeof(SINT32));
ret |= statflag_write(sfh, buffer, bufsize);
}else{
SINT32 tmpsize = 0;
ret |= statflag_write(sfh, &tmpsize, sizeof(SINT32));
}
}
free(buffer);
}
#endif
}
#if defined(SUPPORT_SOUND_SB16)
if (nSaveFlags & FLAG_SB16)
{
datalen = sizeof(g_sb16);
ret |= statflag_write(sfh, &datalen, sizeof(datalen));
ret |= statflag_write(sfh, &g_sb16, sizeof(g_sb16));
}
#endif
return ret;
}
static int flagload_fm(STFLAGH sfh, const SFENTRY *tbl)
{
int ret;
SOUNDID nSoundID;