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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/frotz/mem.h"
#include "glk/frotz/frotz.h"
#include "common/memstream.h"
#include "common/textconsole.h"
namespace Glk {
namespace Frotz {
Mem::Mem() : story_fp(nullptr), story_size(0), first_undo(nullptr), last_undo(nullptr),
curr_undo(nullptr), undo_mem(nullptr), zmp(nullptr), pcp(nullptr), prev_zmp(nullptr),
undo_diff(nullptr), undo_count(0), reserve_mem(0) {
}
void Mem::initialize() {
initializeStoryFile();
loadGameHeader();
loadMemory();
initializeUndo();
// Read header extension table
hx_table_size = get_header_extension(HX_TABLE_SIZE);
hx_unicode_table = get_header_extension(HX_UNICODE_TABLE);
hx_flags = get_header_extension(HX_FLAGS);
}
void Mem::initializeStoryFile() {
if (story_fp->size() < 64)
error("This file is too small to be a Z-code file.");
}
void Mem::loadGameHeader() {
// Load header
zmp = (byte *)malloc(64);
story_fp->seek(0);
story_fp->read(zmp, 64);
Common::MemoryReadStream h(zmp, 64);
loadHeader(h);
// Calculate story file size in bytes
if (h_file_size != 0) {
story_size = (long)2 * h_file_size;
if (h_version >= V4)
story_size *= 2;
if (h_version >= V6)
story_size *= 2;
} else {
// Some old games lack the file size entry
story_size = story_fp->size();
}
}
void Mem::loadMemory() {
// Allocate memory for story data
if ((zmp = (zbyte *)realloc(zmp, story_size)) == nullptr)
error("Out of memory");
// Load story file in chunks of 32KB
uint n = 0x8000;
for (uint size = 64; size < story_size; size += n) {
if (story_size - size < 0x8000)
n = story_size - size;
if (story_fp->read(zmp + size, n) != n)
error("Story file read error");
}
}
void Mem::initializeUndo() {
byte *reserved = nullptr;
if (reserve_mem != 0) {
if ((reserved = new byte[reserve_mem]) == nullptr)
return;
}
// Allocate h_dynamic_size bytes for previous dynamic zmp state
// + 1.5 h_dynamic_size for Quetzal diff + 2.
undo_mem = new zbyte[(h_dynamic_size * 5) / 2 + 2];
if (undo_mem != nullptr) {
prev_zmp = undo_mem;
undo_diff = undo_mem + h_dynamic_size;
memcpy(prev_zmp, zmp, h_dynamic_size);
} else {
_undo_slots = 0;
}
if (reserve_mem)
delete[] reserved;
}
zword Mem::get_header_extension(int entry) {
zword addr;
zword val;
if (h_extension_table == 0 || entry > hx_table_size)
return 0;
addr = h_extension_table + 2 * entry;
LOW_WORD(addr, val);
return val;
}
void Mem::set_header_extension(int entry, zword val) {
zword addr;
if (h_extension_table == 0 || entry > hx_table_size)
return;
addr = h_extension_table + 2 * entry;
SET_WORD(addr, val);
}
void Mem::restart_header(void) {
zword screen_x_size;
zword screen_y_size;
zbyte font_x_size;
zbyte font_y_size;
int i;
SET_BYTE(H_CONFIG, h_config);
SET_WORD(H_FLAGS, h_flags);
if (h_version >= V4) {
SET_BYTE(H_INTERPRETER_NUMBER, h_interpreter_number);
SET_BYTE(H_INTERPRETER_VERSION, h_interpreter_version);
SET_BYTE(H_SCREEN_ROWS, h_screen_rows);
SET_BYTE(H_SCREEN_COLS, h_screen_cols);
}
// It's less trouble to use font size 1x1 for V5 games, especially because of
// a bug in the unreleased German version of "Zork 1"
if (h_version != V6) {
screen_x_size = (zword)h_screen_cols;
screen_y_size = (zword)h_screen_rows;
font_x_size = 1;
font_y_size = 1;
} else {
screen_x_size = h_screen_width;
screen_y_size = h_screen_height;
font_x_size = h_font_width;
font_y_size = h_font_height;
}
if (h_version >= V5) {
SET_WORD(H_SCREEN_WIDTH, screen_x_size);
SET_WORD(H_SCREEN_HEIGHT, screen_y_size);
SET_BYTE(H_FONT_HEIGHT, font_y_size);
SET_BYTE(H_FONT_WIDTH, font_x_size);
SET_BYTE(H_DEFAULT_BACKGROUND, h_default_background);
SET_BYTE(H_DEFAULT_FOREGROUND, h_default_foreground);
}
if (h_version == V6)
for (i = 0; i < 8; i++)
storeb((zword)(H_USER_NAME + i), h_user_name[i]);
SET_BYTE(H_STANDARD_HIGH, h_standard_high);
SET_BYTE(H_STANDARD_LOW, h_standard_low);
set_header_extension(HX_FLAGS, hx_flags);
set_header_extension(HX_FORE_COLOUR, hx_fore_colour);
set_header_extension(HX_BACK_COLOUR, hx_back_colour);
}
void Mem::storeb(zword addr, zbyte value) {
if (addr >= h_dynamic_size)
runtimeError(ERR_STORE_RANGE);
if (addr == H_FLAGS + 1) {
// flags register is modified
h_flags &= ~(SCRIPTING_FLAG | FIXED_FONT_FLAG);
h_flags |= value & (SCRIPTING_FLAG | FIXED_FONT_FLAG);
flagsChanged(value);
}
SET_BYTE(addr, value);
}
void Mem::storew(zword addr, zword value) {
storeb((zword)(addr + 0), hi(value));
storeb((zword)(addr + 1), lo(value));
}
void Mem::free_undo(int count) {
undo_t *p;
if (count > undo_count)
count = undo_count;
while (count--) {
p = first_undo;
if (curr_undo == first_undo)
curr_undo = curr_undo->next;
first_undo = first_undo->next;
free(p);
undo_count--;
}
if (first_undo)
first_undo->prev = nullptr;
else
last_undo = nullptr;
}
void Mem::reset_memory() {
story_fp = nullptr;
if (undo_mem) {
free_undo(undo_count);
delete[] undo_mem;
}
undo_mem = nullptr;
undo_count = 0;
free(zmp);
zmp = nullptr;
}
long Mem::mem_diff(zbyte *a, zbyte *b, zword mem_size, zbyte *diff) {
unsigned size = mem_size;
zbyte *p = diff;
unsigned j;
zbyte c = 0;
for (;;) {
for (j = 0; size > 0 && (c = *a++ ^ *b++) == 0; j++)
size--;
if (size == 0) break;
size--;
if (j > 0x8000) {
*p++ = 0;
*p++ = 0xff;
*p++ = 0xff;
j -= 0x8000;
}
if (j > 0) {
*p++ = 0;
j--;
if (j <= 0x7f) {
*p++ = j;
} else {
*p++ = (j & 0x7f) | 0x80;
*p++ = (j & 0x7f80) >> 7;
}
}
*p++ = c;
*(b - 1) ^= c;
}
return p - diff;
}
void Mem::mem_undiff(zbyte *diff, long diff_length, zbyte *dest) {
zbyte c;
while (diff_length) {
c = *diff++;
diff_length--;
if (c == 0) {
unsigned runlen;
if (!diff_length)
return; // Incomplete run
runlen = *diff++;
diff_length--;
if (runlen & 0x80) {
if (!diff_length)
return; // Incomplete extended run
c = *diff++;
diff_length--;
runlen = (runlen & 0x7f) | (((unsigned)c) << 7);
}
dest += runlen + 1;
} else {
*dest++ ^= c;
}
}
}
} // End of namespace Frotz
} // End of namespace Glk
|