comparison gst.c @ 962:f52cb02a1466

Fixed loading save states from menu
author Michael Pavone <pavone@retrodev.com>
date Mon, 18 Apr 2016 19:26:47 -0700
parents 750995b587a0
children e34334e6c682
comparison
equal deleted inserted replaced
961:750995b587a0 962:f52cb02a1466
64 dst[1] = val; 64 dst[1] = val;
65 } 65 }
66 66
67 uint32_t m68k_load_gst(m68k_context * context, FILE * gstfile) 67 uint32_t m68k_load_gst(m68k_context * context, FILE * gstfile)
68 { 68 {
69 uint8_t buffer[4096]; 69 uint8_t buffer[GST_68K_REG_SIZE];
70 fseek(gstfile, GST_68K_REGS, SEEK_SET); 70 fseek(gstfile, GST_68K_REGS, SEEK_SET);
71 if (fread(buffer, 1, GST_68K_REG_SIZE, gstfile) != GST_68K_REG_SIZE) { 71 if (fread(buffer, 1, GST_68K_REG_SIZE, gstfile) != GST_68K_REG_SIZE) {
72 fputs("Failed to read 68K registers from savestate\n", stderr); 72 fputs("Failed to read 68K registers from savestate\n", stderr);
73 return 0; 73 return 0;
74 } 74 }
91 if (context->status & (1 << 5)) { 91 if (context->status & (1 << 5)) {
92 context->aregs[8] = read_le_32(buffer + GST_68K_USP_OFFSET); 92 context->aregs[8] = read_le_32(buffer + GST_68K_USP_OFFSET);
93 } else { 93 } else {
94 context->aregs[8] = read_le_32(buffer + GST_68K_SSP_OFFSET); 94 context->aregs[8] = read_le_32(buffer + GST_68K_SSP_OFFSET);
95 } 95 }
96 fseek(gstfile, GST_68K_RAM, SEEK_SET); 96
97 for (int i = 0; i < (32*1024);) {
98 //FIXME: Need to deal with code in RAM that has potentially changed after this
99 if (fread(buffer, 1, sizeof(buffer), gstfile) != sizeof(buffer)) {
100 fputs("Failed to read 68K RAM from savestate\n", stderr);
101 return 0;
102 }
103 for(curpos = buffer; curpos < (buffer + sizeof(buffer)); curpos += sizeof(uint16_t)) {
104 ram[i++] = read_be_16(curpos);
105 }
106 }
107 return pc; 97 return pc;
108 } 98 }
109 99
110 uint8_t m68k_save_gst(m68k_context * context, uint32_t pc, FILE * gstfile) 100 uint8_t m68k_save_gst(m68k_context * context, uint32_t pc, FILE * gstfile)
111 { 101 {
112 uint8_t buffer[4096]; 102 uint8_t buffer[GST_68K_REG_SIZE];
113 uint8_t * curpos = buffer; 103 uint8_t * curpos = buffer;
114 for (int i = 0; i < 8; i++) { 104 for (int i = 0; i < 8; i++) {
115 write_le_32(curpos, context->dregs[i]); 105 write_le_32(curpos, context->dregs[i]);
116 curpos += sizeof(uint32_t); 106 curpos += sizeof(uint32_t);
117 } 107 }
137 if (fwrite(buffer, 1, GST_68K_REG_SIZE, gstfile) != GST_68K_REG_SIZE) { 127 if (fwrite(buffer, 1, GST_68K_REG_SIZE, gstfile) != GST_68K_REG_SIZE) {
138 fputs("Failed to write 68K registers to savestate\n", stderr); 128 fputs("Failed to write 68K registers to savestate\n", stderr);
139 return 0; 129 return 0;
140 } 130 }
141 131
142 fseek(gstfile, GST_68K_RAM, SEEK_SET);
143 for (int i = 0; i < (32*1024);) {
144 for(curpos = buffer; curpos < (buffer + sizeof(buffer)); curpos += sizeof(uint16_t)) {
145 write_be_16(curpos, ram[i++]);
146 }
147 if (fwrite(buffer, 1, sizeof(buffer), gstfile) != sizeof(buffer)) {
148 fputs("Failed to write 68K RAM to savestate\n", stderr);
149 return 0;
150 }
151 }
152 return 1; 132 return 1;
153 } 133 }
154 134
155 uint8_t z80_load_gst(z80_context * context, FILE * gstfile) 135 uint8_t z80_load_gst(z80_context * context, FILE * gstfile)
156 { 136 {
216 context->mem_pointers[1] = context->mem_pointers[2] + bank; 196 context->mem_pointers[1] = context->mem_pointers[2] + bank;
217 } else { 197 } else {
218 context->mem_pointers[1] = NULL; 198 context->mem_pointers[1] = NULL;
219 } 199 }
220 context->bank_reg = bank >> 15; 200 context->bank_reg = bank >> 15;
201 uint8_t buffer[Z80_RAM_BYTES];
221 fseek(gstfile, GST_Z80_RAM, SEEK_SET); 202 fseek(gstfile, GST_Z80_RAM, SEEK_SET);
222 if(fread(context->mem_pointers[0], 1, 8*1024, gstfile) != (8*1024)) { 203 if(fread(buffer, 1, sizeof(buffer), gstfile) != (8*1024)) {
223 fputs("Failed to read Z80 RAM from savestate\n", stderr); 204 fputs("Failed to read Z80 RAM from savestate\n", stderr);
224 return 0; 205 return 0;
206 }
207 for (int i = 0; i < Z80_RAM_BYTES; i++)
208 {
209 if (context->mem_pointers[0][i] != buffer[i]) {
210 context->mem_pointers[0][i] = buffer[i];
211 z80_handle_code_write(i, context);
212 }
225 } 213 }
226 return 1; 214 return 1;
227 } 215 }
228 216
229 uint8_t vdp_load_gst(vdp_context * context, FILE * state_file) 217 uint8_t vdp_load_gst(vdp_context * context, FILE * state_file)
412 return 1; 400 return 1;
413 } 401 }
414 402
415 uint32_t load_gst(genesis_context * gen, char * fname) 403 uint32_t load_gst(genesis_context * gen, char * fname)
416 { 404 {
405 char buffer[4096];
417 FILE * gstfile = fopen(fname, "rb"); 406 FILE * gstfile = fopen(fname, "rb");
418 if (!gstfile) { 407 if (!gstfile) {
419 fprintf(stderr, "Could not open file %s for reading\n", fname); 408 fprintf(stderr, "Could not open file %s for reading\n", fname);
420 goto error; 409 goto error;
421 } 410 }
430 } 419 }
431 uint32_t pc = m68k_load_gst(gen->m68k, gstfile); 420 uint32_t pc = m68k_load_gst(gen->m68k, gstfile);
432 if (!pc) { 421 if (!pc) {
433 goto error_close; 422 goto error_close;
434 } 423 }
424
435 if (!vdp_load_gst(gen->vdp, gstfile)) { 425 if (!vdp_load_gst(gen->vdp, gstfile)) {
436 goto error_close; 426 goto error_close;
437 } 427 }
438 if (!ym_load_gst(gen->ym, gstfile)) { 428 if (!ym_load_gst(gen->ym, gstfile)) {
439 goto error_close; 429 goto error_close;
441 if (!z80_load_gst(gen->z80, gstfile)) { 431 if (!z80_load_gst(gen->z80, gstfile)) {
442 goto error_close; 432 goto error_close;
443 } 433 }
444 gen->ports[0].control = 0x40; 434 gen->ports[0].control = 0x40;
445 gen->ports[1].control = 0x40; 435 gen->ports[1].control = 0x40;
436
437 fseek(gstfile, GST_68K_RAM, SEEK_SET);
438 for (int i = 0; i < (32*1024);) {
439 if (fread(buffer, 1, sizeof(buffer), gstfile) != sizeof(buffer)) {
440 fputs("Failed to read 68K RAM from savestate\n", stderr);
441 return 0;
442 }
443 for(char *curpos = buffer; curpos < (buffer + sizeof(buffer)); curpos += sizeof(uint16_t)) {
444 uint16_t word = read_be_16(curpos);
445 if (word != gen->work_ram[i]) {
446 gen->work_ram[i] = word;
447 m68k_handle_code_write(0xFF0000 | (i << 1), gen->m68k);
448 }
449 i++;
450 }
451 }
446 fclose(gstfile); 452 fclose(gstfile);
447 return pc; 453 return pc;
448 454
449 error_close: 455 error_close:
450 fclose(gstfile); 456 fclose(gstfile);
452 return 0; 458 return 0;
453 } 459 }
454 460
455 uint8_t save_gst(genesis_context * gen, char *fname, uint32_t m68k_pc) 461 uint8_t save_gst(genesis_context * gen, char *fname, uint32_t m68k_pc)
456 { 462 {
463 char buffer[4096];
457 FILE * gstfile = fopen(fname, "wb"); 464 FILE * gstfile = fopen(fname, "wb");
458 if (!gstfile) { 465 if (!gstfile) {
459 fprintf(stderr, "Could not open %s for writing\n", fname); 466 fprintf(stderr, "Could not open %s for writing\n", fname);
460 goto error; 467 goto error;
461 } 468 }
473 goto error_close; 480 goto error_close;
474 } 481 }
475 if (!ym_save_gst(gen->ym, gstfile)) { 482 if (!ym_save_gst(gen->ym, gstfile)) {
476 goto error_close; 483 goto error_close;
477 } 484 }
485 fseek(gstfile, GST_68K_RAM, SEEK_SET);
486 for (int i = 0; i < (32*1024);) {
487 for(char *curpos = buffer; curpos < (buffer + sizeof(buffer)); curpos += sizeof(uint16_t)) {
488 write_be_16(curpos, gen->work_ram[i++]);
489 }
490 if (fwrite(buffer, 1, sizeof(buffer), gstfile) != sizeof(buffer)) {
491 fputs("Failed to write 68K RAM to savestate\n", stderr);
492 return 0;
493 }
494 }
478 return 1; 495 return 1;
479 496
480 error_close: 497 error_close:
481 fclose(gstfile); 498 fclose(gstfile);
482 error: 499 error: