-
Notifications
You must be signed in to change notification settings - Fork 4
/
loadsave.c
38 lines (36 loc) · 1.19 KB
/
loadsave.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
#include "loadsave.h"
#include "memory_map.h"
#include <stdio.h>
void save(State* state, char* filename) {
FILE* f = fopen(filename, "wb");
//order of saving: state, memory ranges
fwrite(state, sizeof(State), 1, f);
//memory ranges - only physical
for (int i = 0; i < state->memory_map->n_phys_mem_range; i++) {
MemoryRange range = state->memory_map->phys_mem_range[i];
if (!range.is_ram)
continue;
fwrite(&range.address, 1, sizeof(range.address), f);
fwrite(&range.size, 1, sizeof(range.size), f);
fwrite(range.phys_mem_ptr, sizeof(byte), range.size, f);
}
fclose(f);
}
void load(State* state, char* filename) {
FILE* f = fopen(filename, "rb");
//order of loading: state, memory ranges
State oldState = *state;
fread(state, sizeof(State), 1, f);
//restore memory map from our old copy
state->memory_map = oldState.memory_map;
//memory ranges - only physical
for (int i = 0; i < state->memory_map->n_phys_mem_range; i++) {
MemoryRange range = state->memory_map->phys_mem_range[i];
if (!range.is_ram)
continue;
fread(&range.address, 1, sizeof(range.address), f);
fread(&range.size, 1, sizeof(range.size), f);
fread(range.phys_mem_ptr, sizeof(byte), range.size, f);
}
fclose(f);
}