-
Notifications
You must be signed in to change notification settings - Fork 0
/
gst.c
513 lines (487 loc) · 13.1 KB
/
gst.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
/*
Copyright 2013 Michael Pavone
This file is part of BlastEm.
BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
*/
#include "genesis.h"
#include "gst.h"
#include <string.h>
#include <stdio.h>
#define GST_68K_REGS 0x80
#define GST_68K_REG_SIZE (0xDA-GST_68K_REGS)
#define GST_68K_PC_OFFSET (0xC8-GST_68K_REGS)
#define GST_68K_SR_OFFSET (0xD0-GST_68K_REGS)
#define GST_68K_USP_OFFSET (0xD2-GST_68K_REGS)
#define GST_68K_SSP_OFFSET (0xD6-GST_68K_REGS)
#define GST_68K_RAM 0x2478
#define GST_Z80_REGS 0x404
#define GST_Z80_REG_SIZE (0x440-GST_Z80_REGS)
#define GST_Z80_RAM 0x474
#define GST_VDP_REGS 0xFA
#define GST_VDP_MEM 0x12478
#define GST_YM_OFFSET 0x1E4
#define GST_YM_SIZE (0x3E4-GST_YM_OFFSET)
uint32_t read_le_32(uint8_t * data)
{
return data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0];
}
uint16_t read_le_16(uint8_t * data)
{
return data[1] << 8 | data[0];
}
uint16_t read_be_16(uint8_t * data)
{
return data[0] << 8 | data[1];
}
void write_le_32(uint8_t * dst, uint32_t val)
{
dst[0] = val;
dst[1] = val >> 8;
dst[2] = val >> 16;
dst[3] = val >> 24;
}
void write_le_16(uint8_t * dst, uint16_t val)
{
dst[0] = val;
dst[1] = val >> 8;
}
void write_be_32(uint8_t * dst, uint32_t val)
{
dst[0] = val >> 24;
dst[1] = val >> 16;
dst[2] = val >> 8;
dst[3] = val;
}
void write_be_16(uint8_t * dst, uint16_t val)
{
dst[0] = val >> 8;
dst[1] = val;
}
uint32_t m68k_load_gst(m68k_context * context, FILE * gstfile)
{
uint8_t buffer[GST_68K_REG_SIZE];
fseek(gstfile, GST_68K_REGS, SEEK_SET);
if (fread(buffer, 1, GST_68K_REG_SIZE, gstfile) != GST_68K_REG_SIZE) {
fputs("Failed to read 68K registers from savestate\n", stderr);
return 0;
}
uint8_t * curpos = buffer;
for (int i = 0; i < 8; i++) {
context->dregs[i] = read_le_32(curpos);
curpos += sizeof(uint32_t);
}
for (int i = 0; i < 8; i++) {
context->aregs[i] = read_le_32(curpos);
curpos += sizeof(uint32_t);
}
uint32_t pc = read_le_32(buffer + GST_68K_PC_OFFSET);
uint16_t sr = read_le_16(buffer + GST_68K_SR_OFFSET);
context->status = sr >> 8;
for (int flag = 4; flag >= 0; flag--) {
context->flags[flag] = sr & 1;
sr >>= 1;
}
if (context->status & (1 << 5)) {
context->aregs[8] = read_le_32(buffer + GST_68K_USP_OFFSET);
} else {
context->aregs[8] = read_le_32(buffer + GST_68K_SSP_OFFSET);
}
return pc;
}
uint8_t m68k_save_gst(m68k_context * context, uint32_t pc, FILE * gstfile)
{
uint8_t buffer[GST_68K_REG_SIZE];
uint8_t * curpos = buffer;
for (int i = 0; i < 8; i++) {
write_le_32(curpos, context->dregs[i]);
curpos += sizeof(uint32_t);
}
for (int i = 0; i < 8; i++) {
write_le_32(curpos, context->aregs[i]);
curpos += sizeof(uint32_t);
}
write_le_32(buffer + GST_68K_PC_OFFSET, pc);
uint16_t sr = context->status << 3;
for (int flag = 4; flag >= 0; flag--) {
sr <<= 1;
sr |= context->flags[flag];
}
write_le_16(buffer + GST_68K_SR_OFFSET, sr);
if (context->status & (1 << 5)) {
write_le_32(buffer + GST_68K_USP_OFFSET, context->aregs[8]);
write_le_32(buffer + GST_68K_SSP_OFFSET, context->aregs[7]);
} else {
write_le_32(buffer + GST_68K_USP_OFFSET, context->aregs[7]);
write_le_32(buffer + GST_68K_SSP_OFFSET, context->aregs[8]);
}
fseek(gstfile, GST_68K_REGS, SEEK_SET);
if (fwrite(buffer, 1, GST_68K_REG_SIZE, gstfile) != GST_68K_REG_SIZE) {
fputs("Failed to write 68K registers to savestate\n", stderr);
return 0;
}
return 1;
}
uint8_t z80_load_gst(z80_context * context, FILE * gstfile)
{
uint8_t regdata[GST_Z80_REG_SIZE];
fseek(gstfile, GST_Z80_REGS, SEEK_SET);
if (fread(regdata, 1, sizeof(regdata), gstfile) != sizeof(regdata)) {
fputs("Failed to read Z80 registers from savestate\n", stderr);
return 0;
}
uint8_t * curpos = regdata;
uint8_t f = *(curpos++);
#ifndef NEW_CORE
context->flags[ZF_C] = f & 1;
f >>= 1;
context->flags[ZF_N] = f & 1;
f >>= 1;
context->flags[ZF_PV] = f & 1;
f >>= 2;
context->flags[ZF_H] = f & 1;
f >>= 2;
context->flags[ZF_Z] = f & 1;
f >>= 1;
context->flags[ZF_S] = f;
context->regs[Z80_A] = *curpos;
curpos += 3;
for (int reg = Z80_C; reg <= Z80_IYH; reg++) {
context->regs[reg++] = *(curpos++);
context->regs[reg] = *curpos;
curpos += 3;
}
context->pc = read_le_16(curpos);
curpos += 4;
context->sp = read_le_16(curpos);
curpos += 4;
f = *(curpos++);
context->alt_flags[ZF_C] = f & 1;
f >>= 1;
context->alt_flags[ZF_N] = f & 1;
f >>= 1;
context->alt_flags[ZF_PV] = f & 1;
f >>= 2;
context->alt_flags[ZF_H] = f & 1;
f >>= 2;
context->alt_flags[ZF_Z] = f & 1;
f >>= 1;
context->alt_flags[ZF_S] = f;
context->alt_regs[Z80_A] = *curpos;
curpos += 3;
for (int reg = Z80_C; reg <= Z80_H; reg++) {
context->alt_regs[reg++] = *(curpos++);
context->alt_regs[reg] = *curpos;
curpos += 3;
}
context->regs[Z80_I] = *curpos;
curpos += 2;
context->iff1 = context->iff2 = *curpos;
curpos += 2;
context->reset = !*(curpos++);
context->busreq = *curpos;
curpos += 3;
uint32_t bank = read_le_32(curpos);
if (bank < 0x400000) {
context->mem_pointers[1] = context->mem_pointers[2] + bank;
} else {
context->mem_pointers[1] = NULL;
}
context->bank_reg = bank >> 15;
#endif
uint8_t buffer[Z80_RAM_BYTES];
fseek(gstfile, GST_Z80_RAM, SEEK_SET);
if(fread(buffer, 1, sizeof(buffer), gstfile) != (8*1024)) {
fputs("Failed to read Z80 RAM from savestate\n", stderr);
return 0;
}
for (int i = 0; i < Z80_RAM_BYTES; i++)
{
if (context->mem_pointers[0][i] != buffer[i]) {
context->mem_pointers[0][i] = buffer[i];
#ifndef NEW_CORE
z80_handle_code_write(i, context);
#endif
}
}
#ifndef NEW_CORE
context->native_pc = NULL;
context->extra_pc = NULL;
#endif
return 1;
}
uint8_t vdp_load_gst(vdp_context * context, FILE * state_file)
{
uint8_t tmp_buf[VRAM_SIZE];
fseek(state_file, GST_VDP_REGS, SEEK_SET);
if (fread(tmp_buf, 1, VDP_REGS, state_file) != VDP_REGS) {
fputs("Failed to read VDP registers from savestate\n", stderr);
return 0;
}
for (uint16_t i = 0; i < VDP_REGS; i++)
{
vdp_control_port_write(context, 0x8000 | (i << 8) | tmp_buf[i]);
}
if (fread(tmp_buf, 1, CRAM_SIZE*2, state_file) != CRAM_SIZE*2) {
fputs("Failed to read CRAM from savestate\n", stderr);
return 0;
}
for (int i = 0; i < CRAM_SIZE; i++) {
uint16_t value;
write_cram_internal(context, i, (tmp_buf[i*2+1] << 8) | tmp_buf[i*2]);
}
if (fread(tmp_buf, 2, VSRAM_SIZE, state_file) != VSRAM_SIZE) {
fputs("Failed to read VSRAM from savestate\n", stderr);
return 0;
}
for (int i = 0; i < VSRAM_SIZE; i++) {
context->vsram[i] = (tmp_buf[i*2+1] << 8) | tmp_buf[i*2];
}
fseek(state_file, GST_VDP_MEM, SEEK_SET);
if (fread(tmp_buf, 1, VRAM_SIZE, state_file) != VRAM_SIZE) {
fputs("Failed to read VRAM from savestate\n", stderr);
return 0;
}
for (int i = 0; i < VRAM_SIZE; i++) {
context->vdpmem[i] = tmp_buf[i];
vdp_check_update_sat_byte(context, i, tmp_buf[i]);
}
return 1;
}
uint8_t vdp_save_gst(vdp_context * context, FILE * outfile)
{
uint8_t tmp_buf[CRAM_SIZE*2];
fseek(outfile, GST_VDP_REGS, SEEK_SET);
if(fwrite(context->regs, 1, VDP_REGS, outfile) != VDP_REGS) {
fputs("Error writing VDP regs to savestate\n", stderr);
return 0;
}
for (int i = 0; i < CRAM_SIZE; i++)
{
tmp_buf[i*2] = context->cram[i];
tmp_buf[i*2+1] = context->cram[i] >> 8;
}
if (fwrite(tmp_buf, 1, sizeof(tmp_buf), outfile) != sizeof(tmp_buf)) {
fputs("Error writing CRAM to savestate\n", stderr);
return 0;
}
for (int i = 0; i < VSRAM_SIZE; i++)
{
tmp_buf[i*2] = context->vsram[i];
tmp_buf[i*2+1] = context->vsram[i] >> 8;
}
if (fwrite(tmp_buf, 2, VSRAM_SIZE, outfile) != VSRAM_SIZE) {
fputs("Error writing VSRAM to savestate\n", stderr);
return 0;
}
fseek(outfile, GST_VDP_MEM, SEEK_SET);
if (fwrite(context->vdpmem, 1, VRAM_SIZE, outfile) != VRAM_SIZE) {
fputs("Error writing VRAM to savestate\n", stderr);
return 0;
}
return 1;
}
uint8_t z80_save_gst(z80_context * context, FILE * gstfile)
{
uint8_t regdata[GST_Z80_REG_SIZE];
uint8_t * curpos = regdata;
memset(regdata, 0, sizeof(regdata));
#ifndef NEW_CORE
uint8_t f = context->flags[ZF_S];
f <<= 1;
f |= context->flags[ZF_Z] ;
f <<= 2;
f |= context->flags[ZF_H];
f <<= 2;
f |= context->flags[ZF_PV];
f <<= 1;
f |= context->flags[ZF_N];
f <<= 1;
f |= context->flags[ZF_C];
*(curpos++) = f;
*curpos = context->regs[Z80_A];
curpos += 3;
for (int reg = Z80_C; reg <= Z80_IYH; reg++) {
*(curpos++) = context->regs[reg++];
*curpos = context->regs[reg];
curpos += 3;
}
write_le_16(curpos, context->pc);
curpos += 4;
write_le_16(curpos, context->sp);
curpos += 4;
f = context->alt_flags[ZF_S];
f <<= 1;
f |= context->alt_flags[ZF_Z] ;
f <<= 2;
f |= context->alt_flags[ZF_H];
f <<= 2;
f |= context->alt_flags[ZF_PV];
f <<= 1;
f |= context->alt_flags[ZF_N];
f <<= 1;
f |= context->alt_flags[ZF_C];
*(curpos++) = f;
*curpos = context->alt_regs[Z80_A];
curpos += 3;
for (int reg = Z80_C; reg <= Z80_H; reg++) {
*(curpos++) = context->alt_regs[reg++];
*curpos = context->alt_regs[reg];
curpos += 3;
}
*curpos = context->regs[Z80_I];
curpos += 2;
*curpos = context->iff1;
curpos += 2;
*(curpos++) = !context->reset;
*curpos = context->busreq;
curpos += 3;
uint32_t bank = context->bank_reg << 15;
write_le_32(curpos, bank);
#endif
fseek(gstfile, GST_Z80_REGS, SEEK_SET);
if (fwrite(regdata, 1, sizeof(regdata), gstfile) != sizeof(regdata)) {
return 0;
}
fseek(gstfile, GST_Z80_RAM, SEEK_SET);
if(fwrite(context->mem_pointers[0], 1, 8*1024, gstfile) != (8*1024)) {
fputs("Failed to write Z80 RAM to savestate\n", stderr);
return 0;
}
return 1;
}
uint8_t ym_load_gst(ym2612_context * context, FILE * gstfile)
{
uint8_t regdata[GST_YM_SIZE];
fseek(gstfile, GST_YM_OFFSET, SEEK_SET);
if (fread(regdata, 1, sizeof(regdata), gstfile) != sizeof(regdata)) {
return 0;
}
for (int i = 0; i < sizeof(regdata); i++) {
if (i & 0x100) {
ym_address_write_part2(context, i & 0xFF);
} else {
ym_address_write_part1(context, i);
}
ym_data_write(context, regdata[i]);
}
return 1;
}
uint8_t ym_save_gst(ym2612_context * context, FILE * gstfile)
{
uint8_t regdata[GST_YM_SIZE];
for (int i = 0; i < sizeof(regdata); i++) {
if (i & 0x100) {
int reg = (i & 0xFF);
if (reg >= YM_PART2_START && reg < YM_REG_END) {
regdata[i] = context->part2_regs[reg-YM_PART2_START];
} else {
regdata[i] = 0xFF;
}
} else {
if (i >= YM_PART1_START && i < YM_REG_END) {
regdata[i] = context->part1_regs[i-YM_PART1_START];
} else {
regdata[i] = 0xFF;
}
}
}
fseek(gstfile, GST_YM_OFFSET, SEEK_SET);
if (fwrite(regdata, 1, sizeof(regdata), gstfile) != sizeof(regdata)) {
return 0;
}
return 1;
}
uint32_t load_gst(genesis_context * gen, char * fname)
{
char buffer[4096];
FILE * gstfile = fopen(fname, "rb");
if (!gstfile) {
fprintf(stderr, "Could not open file %s for reading\n", fname);
goto error;
}
char ident[5];
if (fread(ident, 1, sizeof(ident), gstfile) != sizeof(ident)) {
fprintf(stderr, "Could not read ident code from %s\n", fname);
goto error_close;
}
if (memcmp(ident, "GST\x40\xE0", 3) != 0) {
fprintf(stderr, "%s doesn't appear to be a GST savestate. The ident code is %c%c%c\\x%X\\x%X instead of GST\\x40\\xE0.\n", fname, ident[0], ident[1], ident[2], ident[3], ident[4]);
goto error_close;
}
uint32_t pc = m68k_load_gst(gen->m68k, gstfile);
if (!pc) {
goto error_close;
}
if (!vdp_load_gst(gen->vdp, gstfile)) {
goto error_close;
}
if (!ym_load_gst(gen->ym, gstfile)) {
goto error_close;
}
if (!z80_load_gst(gen->z80, gstfile)) {
goto error_close;
}
gen->io.ports[0].control = 0x40;
gen->io.ports[1].control = 0x40;
fseek(gstfile, GST_68K_RAM, SEEK_SET);
for (int i = 0; i < (32*1024);) {
if (fread(buffer, 1, sizeof(buffer), gstfile) != sizeof(buffer)) {
fputs("Failed to read 68K RAM from savestate\n", stderr);
return 0;
}
for(char *curpos = buffer; curpos < (buffer + sizeof(buffer)); curpos += sizeof(uint16_t)) {
uint16_t word = read_be_16(curpos);
if (word != gen->work_ram[i]) {
gen->work_ram[i] = word;
m68k_handle_code_write(0xFF0000 | (i << 1), gen->m68k);
}
i++;
}
}
fclose(gstfile);
return pc;
error_close:
fclose(gstfile);
error:
return 0;
}
uint8_t save_gst(genesis_context * gen, char *fname, uint32_t m68k_pc)
{
char buffer[4096];
FILE * gstfile = fopen(fname, "wb");
if (!gstfile) {
fprintf(stderr, "Could not open %s for writing\n", fname);
goto error;
}
if (fwrite("GST\x40\xE0", 1, 5, gstfile) != 5) {
fputs("Error writing signature to savestate\n", stderr);
goto error_close;
}
if (!m68k_save_gst(gen->m68k, m68k_pc, gstfile)) {
goto error_close;
}
if (!z80_save_gst(gen->z80, gstfile)) {
goto error_close;
}
if (!vdp_save_gst(gen->vdp, gstfile)) {
goto error_close;
}
if (!ym_save_gst(gen->ym, gstfile)) {
goto error_close;
}
fseek(gstfile, GST_68K_RAM, SEEK_SET);
for (int i = 0; i < (32*1024);) {
for(char *curpos = buffer; curpos < (buffer + sizeof(buffer)); curpos += sizeof(uint16_t)) {
write_be_16(curpos, gen->work_ram[i++]);
}
if (fwrite(buffer, 1, sizeof(buffer), gstfile) != sizeof(buffer)) {
fputs("Failed to write 68K RAM to savestate\n", stderr);
return 0;
}
}
return 1;
error_close:
fclose(gstfile);
error:
return 0;
}