Import Upstream version 1.23
[debian-dgen.git] / save.cpp
blob20a6c1c24e50cb702c46c5759388685edd7c98e2
1 // DGen v1.10+
2 // Megadrive C++ module saving and loading
4 #include <stdio.h>
5 #include <string.h>
6 #include "md.h"
8 /*
9 gs0 genecyst save file INfo
11 GST\0 to start with
13 80-9f = d0-d7 almost certain
14 a0-bf = a0-a7 almost certain
15 c8 = pc fairly certain
16 d0 = sr fairly certain
19 112 Start of cram len 0x80
20 192 Start of vsram len 0x50
21 1e2-474 UNKNOWN sound info?
22 Start of z80 ram at 474 (they store 2000)
23 Start of RAM at 2478 almost certain (BYTE SWAPPED)
24 Start of VRAM at 12478
25 end of VRAM
28 // NB - for load and save you don't need to use star_mz80_on/off
29 // Because stars/mz80 isn't actually used
30 #define fput(x,y) { if (fwrite(x,1,y,hand)!=y) goto save_error; }
31 #define fget(x,y) if (fread(x,1,y,hand)!=y) goto load_error;
33 extern int byteswap_memory(unsigned char *start,int len);
35 int md::import_gst(FILE *hand)
37 #ifdef COMPILE_WITH_STAR
38 if (cpu_emu==0)
40 fseek(hand,0x80,SEEK_SET);
41 fget(cpu.dreg,8*4);
42 fget(cpu.areg,8*4);
43 fseek(hand,0xc8,SEEK_SET);
44 fget(&cpu.pc,4);
45 fseek(hand,0xd0,SEEK_SET);
46 fget(&cpu.sr,4);
48 #endif
50 #ifdef COMPILE_WITH_MUSA
51 if (cpu_emu==1)
53 int i,t;
54 fseek(hand,0x80,SEEK_SET);
55 for (i=0;i<8;i++)
56 { fget(&t,4); m68k_poke_dr(i,t); }
57 for (i=0;i<8;i++)
58 { fget(&t,4); m68k_poke_ar(i,t); }
60 fseek(hand,0xc8,SEEK_SET);
61 fget(&t,4); m68k_poke_pc(t);
63 fseek(hand,0xd0,SEEK_SET);
64 fget(&t,4); m68k_poke_sr(t);
66 #endif
68 fseek(hand,0xfa,SEEK_SET);
69 fget(vdp.reg,0x18);
71 fseek(hand,0x112,SEEK_SET);
72 fget(vdp.cram ,0x00080);
73 byteswap_memory(vdp.cram,0x80);
74 fget(vdp.vsram,0x00050);
75 byteswap_memory(vdp.vsram,0x50);
77 fseek(hand,0x474,SEEK_SET);
78 fget(z80ram, 0x02000);
80 fseek(hand,0x2478,SEEK_SET);
81 fget(ram, 0x10000);
82 byteswap_memory(ram,0x10000);
84 fget(vdp.vram ,0x10000);
86 memset(vdp.dirt,0xff,0x35); // mark everything as changed
88 return 0;
89 load_error:
90 return 1;
94 int md::export_gst(FILE *hand)
96 int i;
97 static unsigned char gst_head[0x80]=
99 0x47,0x53,0x54,0,0,0,0xe0,0x40
101 // 00 00 00 00 00 00 00 00 00 00 00 00 00 21 80 fa <.............!..>
103 unsigned char *zeros=gst_head+0x40; // 0x40 zeros
105 fseek(hand,0x00,SEEK_SET);
106 // Make file size 0x22478 with zeros
107 for (i=0;i<0x22440;i+=0x40) fput(zeros,0x40);
108 fput(zeros,0x38);
110 fseek(hand,0x00,SEEK_SET);
111 fput(gst_head,0x80);
113 #ifdef COMPILE_WITH_STAR
114 if (cpu_emu==0)
116 fseek(hand,0x80,SEEK_SET);
117 fput(cpu.dreg,8*4);
118 fput(cpu.areg,8*4);
119 fseek(hand,0xc8,SEEK_SET);
120 fput(&cpu.pc,4);
121 fseek(hand,0xd0,SEEK_SET);
122 fput(&cpu.sr,4);
124 #endif
125 #ifdef COMPILE_WITH_MUSA
126 if (cpu_emu==1)
128 int i,t;
129 fseek(hand,0x80,SEEK_SET);
130 for (i=0;i<8;i++)
131 { t=m68k_peek_dr(i); fput(&t,4);}
132 for (i=0;i<8;i++)
133 { t=m68k_peek_ar(i); fput(&t,4);}
135 fseek(hand,0xc8,SEEK_SET);
136 t=m68k_peek_pc(); fput(&t,4);
138 fseek(hand,0xd0,SEEK_SET);
139 t=m68k_peek_sr(); fput(&t,4);
141 #endif
143 fseek(hand,0xfa,SEEK_SET);
144 fput(vdp.reg,0x18);
146 fseek(hand,0x112,SEEK_SET);
147 byteswap_memory(vdp.cram,0x80);
148 fput(vdp.cram ,0x00080);
149 byteswap_memory(vdp.cram,0x80);
150 byteswap_memory(vdp.vsram,0x50);
151 fput(vdp.vsram,0x00050);
152 byteswap_memory(vdp.vsram,0x50);
154 fseek(hand,0x474,SEEK_SET);
155 fput(z80ram, 0x02000);
157 fseek(hand,0x2478,SEEK_SET);
158 byteswap_memory(ram,0x10000);
159 fput(ram, 0x10000);
160 byteswap_memory(ram,0x10000);
162 fput(vdp.vram ,0x10000);
164 return 0;
165 save_error:
166 return 1;