-
Notifications
You must be signed in to change notification settings - Fork 6
/
savestate.c
147 lines (111 loc) · 2.29 KB
/
savestate.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
// Save state support.
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "savestate.h"
#include "md.h"
#include "vram.h"
#include "cartridge.h"
int save_blob(void* ptr, size_t size, FILE* f)
{
size_t written = fwrite (ptr, 1, size, f);
if (written != size)
return -1;
return 0;
}
int load_blob(void* ptr, size_t size, FILE* f)
{
size_t readed = fread(ptr, 1, size, f);
if (readed != size)
return -1;
return 0;
}
int save_state(const char* filename)
{
FILE* f;
f = fopen(filename, "wb");
if (!f)
return -1;
// Header
NukedSaveHeader hdr = { 0 };
hdr.type[0] = 'M';
hdr.type[1] = 'D';
strcpy(hdr.version, VERSION);
if (save_blob(&hdr, sizeof(hdr), f))
return -1;
// Board
if (save_blob(&md, sizeof(md), f))
return -1;
if (save_blob(&mcycles, sizeof(mcycles), f))
return -1;
if (save_blob(&ram, sizeof(ram), f))
return -1;
if (save_blob(&zram, sizeof(zram), f))
return -1;
if (vram_save(f))
return -1;
// 68K
if (save_blob(&m68k, sizeof(m68k), f))
return -1;
// Z80
if (save_blob(&z80, sizeof(z80), f))
return -1;
// Chipset
if (save_blob(&ym, sizeof(ym), f))
return -1;
if (save_blob(&tmss_rom, sizeof(tmss_rom), f))
return -1;
// Cart
if (cart_save(f))
return -1;
printf("Saved state: %s\n", filename);
fclose(f);
return 0;
}
int load_state(const char* filename)
{
FILE* f;
f = fopen(filename, "rb");
if (!f)
return -1;
// Check version (Header)
NukedSaveHeader hdr = { 0 };
if (load_blob(&hdr, sizeof(hdr), f))
return -1;
if (!(hdr.type[0] == 'M' && hdr.type[1] == 'D')) {
printf("Invalid save type\n");
return -2;
}
if (strcmp(hdr.version, VERSION) != 0) {
printf("Invalid save version");
return -3;
}
// Board
if (load_blob(&md, sizeof(md), f))
return -1;
if (load_blob(&mcycles, sizeof(mcycles), f))
return -1;
if (load_blob(&ram, sizeof(ram), f))
return -1;
if (load_blob(&zram, sizeof(zram), f))
return -1;
if (vram_load(f))
return -1;
// 68K
if (load_blob(&m68k, sizeof(m68k), f))
return -1;
// Z80
if (load_blob(&z80, sizeof(z80), f))
return -1;
// Chipset
if (load_blob(&ym, sizeof(ym), f))
return -1;
if (load_blob(&tmss_rom, sizeof(tmss_rom), f))
return -1;
// Cart
if (cart_load(f))
return -1;
printf("Loaded state: %s\n", filename);
fclose(f);
return 0;
}