forked from BrickBot/brickEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
savefile.c
199 lines (173 loc) · 5 KB
/
savefile.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
/* Emulator for LEGO RCX Brick, Copyright (C) 2003 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; see the file COPYING.LESSER. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: savefile.c 72 2004-04-26 07:25:12Z hoenicke $
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h> /* for htonx/ntohx */
#include <zlib.h>
#include "types.h"
#include "peripherals.h"
#include "memory.h"
#include "h8300.h"
#include "symbols.h"
#define MAX_PATHNAME_LEN 4096
#define SAVEFILE_MAGIC "BrEmuSF0"
/** \brief array to hold all peripherals
*
* This array is used to register the different
* kinds of peripherals (buttons, sensors etc.)
*/
extern peripheral_ops peripherals[100];
/** \brief number of registered peripherals
* This counter is incremented each time a
* peripheral is registered.
*/
extern int num_peripherals;
static void save_symbol(gzFile *file,
uint16 addr, int16 type, char* name)
{
struct {
uint16 addr;
int16 type;
int16 namelen;
} sym_entry;
int namelen = strlen(name);
sym_entry.addr = ntohs(addr);
sym_entry.type = ntohs(type);
sym_entry.namelen = ntohs(namelen);
gzwrite(file, &sym_entry, sizeof(sym_entry));
gzwrite(file, name, namelen);
}
static void load_symbols(gzFile *file)
{
struct {
uint16 addr;
int16 type;
int16 namelen;
} sym_entry;
char *name;
for (;;) {
gzread(file, &sym_entry, sizeof(sym_entry));
if (!sym_entry.addr && !sym_entry.type && !sym_entry.namelen)
break;
sym_entry.namelen = ntohs(sym_entry.namelen);
name = malloc(sym_entry.namelen + 1);
gzread(file, name, sym_entry.namelen);
name[sym_entry.namelen] = 0;
symbols_add(ntohs(sym_entry.addr), ntohs(sym_entry.type), name);
}
}
static void savefile_save(char *path) {
gzFile *file;
struct {
char pad;
char id;
uint16 len;
char buffer[512];
} block;
int i;
int len;
stop_time();
file = gzopen(path, "wb9");
gzwrite(file, SAVEFILE_MAGIC, 8);
gzwrite(file, memory, sizeof(memory));
gzwrite(file, memtype, sizeof(memtype));
symbols_iterate((symbols_iterate_func) save_symbol, file);
save_symbol(file, 0, 0, "");
for (i = 0; i < num_peripherals; i++) {
if (!peripherals[i].save_data)
continue;
len = peripherals[i].save_data(block.buffer, sizeof(block.buffer));
block.id = peripherals[i].id;
block.len = ntohs(len);
gzwrite(file, &block.id, len+sizeof(block.len)+sizeof(block.id));
}
block.id = 0;
gzwrite(file, &block.id, sizeof(block.id));
gzclose(file);
cont_time();
}
static void savefile_load(char *path) {
gzFile *file;
char buffer[512];
uint16 len;
int i;
stop_time();
file = gzopen(path, "rb");
gzread(file, buffer, 8);
if (memcmp(buffer, SAVEFILE_MAGIC, 8) != 0) {
printf("Wrong file type!\n");
gzclose(file);
return;
}
gzread(file, memory, sizeof(memory));
gzread(file, memtype, sizeof(memtype));
load_symbols(file);
while (!gzeof(file)) {
char id;
gzread(file, &id, 1);
if (id == 0)
break;
gzread(file, &len, sizeof(len));
len = ntohs(len);
if (len > sizeof(buffer)) {
printf("Too much info for peripheral %c", id);
goto exit;
}
gzread(file, &buffer, len);
for (i = 0; i < num_peripherals; i++) {
if (id == peripherals[i].id) {
peripherals[i].load_data(buffer, len);
break;
}
}
}
frame_init();
cont_time();
exit:
gzclose(file);
return;
}
static void savefile_read_fd(int fd) {
char buf[1];
char filename[MAX_PATHNAME_LEN+1];
/* read in next byte: id */
read(fd, buf, 1);
/* read in filename */
int len = 0;
do {
len += read(fd, filename + len, 1);
} while (filename[len-1] != '\n' && filename[len-1] != '\r');
filename[len-1] = 0;
switch (buf[0]) {
case 'L':
case 'l':
savefile_load(filename);
case 'S':
case 's':
savefile_save(filename);
}
}
static peripheral_ops savefile = {
id: 'C',
read_fd: savefile_read_fd
};
void savefile_init() {
register_peripheral(savefile);
}