30 static const Uint8 ep128emu_snapshot_marker[] = {
31 0x5D, 0x12, 0xE4, 0xF4, 0xC9, 0xDA, 0xB6, 0x42, 0x01, 0x33, 0xDE, 0x07, 0xD2, 0x34, 0xF2, 0x22
44 static const char *block_names[
BLOCK_TYPES] = {
"Z80",
"MEM",
"IO",
"DAVE",
"NICK",
"MACHINE" };
46 static const Uint32 block_types[
BLOCK_TYPES] = { 0x45508001, 0x45508002, 0x45508003, 0x45508004, 0x45508005, 0x45508009 };
47 static const int block_versions[
BLOCK_TYPES] = { 0x1000002, 0x1000000, 0x1000000, 0x1000000, 0x4000000, -1 };
51 static Uint8 *snap = NULL;
72 char pathbuffer[PATH_MAX + 1];
80 FILE *f = fdopen(
fd,
"rb");
86 if (fseek(f, 0, SEEK_END)) {
87 ERROR_WINDOW(
"Cannot seek snapshot file: %s", pathbuffer);
92 if (snapsize < 16 || snapsize >= 0x500000) {
93 ERROR_WINDOW(
"Too short or long snapshot file: %s", pathbuffer);
97 snap = malloc(snapsize);
102 if (fread(snap, snapsize, 1, f) != 1) {
108 if (memcmp(snap, ep128emu_snapshot_marker,
sizeof ep128emu_snapshot_marker)) {
109 ERROR_WINDOW(
"Invalid ep128emu snapshot (identifier not found)");
113 block_offsets[pos] = 0;
118 if (pos + 8 >= snapsize) {
119 ERROR_WINDOW(
"Invalid snapshot file: truncated (during block header)");
122 btype = getSnapDword(snap, pos);
125 bsize = getSnapDword(snap, pos + 4);
127 if (pos + bsize + 4 >= snapsize) {
128 ERROR_WINDOW(
"Invalid snapshot file: truncated (during block data)");
131 for (bindex = 0 ;; bindex++)
133 ERROR_WINDOW(
"Invalid snapshot file: Unknown block type: %Xh", btype);
135 }
else if (btype == block_types[bindex])
137 if (block_offsets[bindex]) {
138 ERROR_WINDOW(
"Invalid snapshot file: duplicated block %Xh", block_types[bindex]);
141 if (block_versions[bindex] != -1) {
142 Uint32 bver = getSnapDword(snap, pos);
143 if (bver != block_versions[bindex]) {
144 ERROR_WINDOW(
"Invalid snapshot file: bad block version %Xh (should be %Xh) for block %Xh",
145 bver, block_versions[bindex], block_types[bindex]
152 block_offsets[bindex] = pos;
153 block_sizes[bindex] = bsize;
154 DEBUG(
"SNAPSHOT: block #%d %Xh(%s) @%06Xh" NL, bindex, block_types[bindex], block_names[bindex], block_offsets[bindex]);
159 if (block_versions[pos] != -1 && !block_offsets[pos]) {
160 ERROR_WINDOW(
"Invalid snapshot file: missing block %Xh", block_types[pos]);
164 if ((block_sizes[
BLOCK_MEM] - 4) % 0x4002) {
165 ERROR_WINDOW(
"Invalid snapshot file: memory dump size is not multiple of 16Ks");
174 memset(
memory, 0xFF, 0x400000);
175 for (pos = 0; pos < 0x100; pos++) {
183 if (snap[pos] < 0xFC)
189 memcpy(
memory + (snap[pos] << 14), snap + pos + 2, 0x4000);
208 Z80_PC = getSnapWord(snap, pos);
214 Z80_SP = getSnapWord(snap, pos + 10);
221 Z80_I = snap[pos + 24];
222 Z80_R = snap[pos + 25] & 127;
226 Z80_R7 = snap[pos + 32] & 128;
229 for (pos = 0; pos < 256; pos++)
230 ports[pos] = snap[block_offsets[
BLOCK_IO] + pos];
231 for (pos = 0xA0; pos <= 0xBF; pos++)
233 for (pos = 0; pos < 4; pos++) {