aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/frotz/mem.cpp
blob: 6ee67816906711586f138f6f3e5fea743e7d1eff (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* 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 {

const Header::StoryEntry Header::RECORDS[25] = {
	{       SHERLOCK,  21, "871214" },
	{       SHERLOCK,  26, "880127" },
	{    BEYOND_ZORK,  47, "870915" },
	{    BEYOND_ZORK,  49, "870917" },
	{    BEYOND_ZORK,  51, "870923" },
	{    BEYOND_ZORK,  57, "871221" },
	{      ZORK_ZERO, 296, "881019" },
	{      ZORK_ZERO, 366, "890323" },
	{      ZORK_ZERO, 383, "890602" },
	{      ZORK_ZERO, 393, "890714" },
	{         SHOGUN, 292, "890314" },
	{         SHOGUN, 295, "890321" },
	{         SHOGUN, 311, "890510" },
	{         SHOGUN, 322, "890706" },
	{         ARTHUR,  54, "890606" },
	{         ARTHUR,  63, "890622" },
	{         ARTHUR,  74, "890714" },
	{        JOURNEY,  26, "890316" },
	{        JOURNEY,  30, "890322" },
	{        JOURNEY,  77, "890616" },
	{        JOURNEY,  83, "890706" },
	{ LURKING_HORROR, 203, "870506" },
	{ LURKING_HORROR, 219, "870912" },
	{ LURKING_HORROR, 221, "870918" },
	{        UNKNOWN,   0, "------" }
};

void Header::loadHeader(Common::SeekableReadStream &f) {
	h_version = f.readByte();
	h_config = f.readByte();

	if (h_version < V1 || h_version > V8)
		error("Unknown Z-code version");

	if (h_version == V6)
		error("Cannot play Z-code version 6");

	if (h_version == V3 && (h_config & CONFIG_BYTE_SWAPPED))
		error("Byte swapped story file");

	h_release = f.readUint16BE();
	h_resident_size = f.readUint16BE();
	h_start_pc = f.readUint16BE();
	h_dictionary = f.readUint16BE();
	h_objects = f.readUint16BE();
	h_globals = f.readUint16BE();
	h_dynamic_size = f.readUint16BE();
	h_flags = f.readUint16BE();
	f.read(h_serial, 6);
	
	/* Auto-detect buggy story files that need special fixes */
	_storyId = UNKNOWN;

	for (int i = 0; RECORDS[i]._storyId != UNKNOWN; ++i) {
		if (h_release == RECORDS[i]._release) {
			if (!strncmp((const char *)h_serial, RECORDS[i]._serial, 6)) {
				_storyId = RECORDS[i]._storyId;
				break;
			}
		}
	}

	h_abbreviations = f.readUint16BE();
	h_file_size = f.readUint16BE();
	h_checksum = f.readUint16BE();
	
	f.seek(H_FUNCTIONS_OFFSET);
	h_functions_offset = f.readUint16BE();
	h_strings_offset = f.readUint16BE();
	f.seek(H_TERMINATING_KEYS);
	h_terminating_keys = f.readUint16BE();
	f.seek(H_ALPHABET);
	h_alphabet = f.readUint16BE();
	h_extension_table = f.readUint16BE();


	// Zork Zero Macintosh doesn't have the graphics flag set
	if (_storyId == ZORK_ZERO && h_release == 296)
		h_flags |= GRAPHICS_FLAG;
}

/*--------------------------------------------------------------------------*/

Mem::Mem() : story_fp(nullptr), blorb_ofs(0), blorb_len(0), story_size(0),
		first_undo(nullptr), last_undo(nullptr), curr_undo(nullptr),
		undo_mem(nullptr), prev_zmp(nullptr), undo_diff(nullptr),
		undo_count(0), reserve_mem(0) {
}

void Mem::initialize() {
	initializeStoryFile();
	initializeUndo();
	loadGameHeader();

	// 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");
	}

	// 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() {
	Common::SeekableReadStream *f = story_fp;
	giblorb_map_t *map;
	giblorb_result_t res;
	uint32 magic;

	magic = f->readUint32BE();

	if (magic == MKTAG('F', 'O', 'R', 'M')) {
		if (g_vm->giblorb_set_resource_map(f))
			error("This Blorb file seems to be invalid.");

		map = g_vm->giblorb_get_resource_map();

		if (g_vm->giblorb_load_resource(map, giblorb_method_FilePos, &res, giblorb_ID_Exec, 0))
			error("This Blorb file does not contain an executable chunk.");
		if (res.chunktype != MKTAG('Z', 'C', 'O', 'D'))
			error("This Blorb file contains an executable chunk, but it is not a Z-code file.");

		blorb_ofs = res.data.startpos;
		blorb_len = res.length;
	} else {
		blorb_ofs = 0;
		blorb_len = f->size();
	}

	if (blorb_len < 64)
		error("This file is too small to be a Z-code file.");
}

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 != 0)
		delete reserved;
}

void Mem::loadGameHeader() {
	// Load header
	zmp = new byte[64];
	story_fp->seek(blorb_ofs);
	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 = blorb_len;
	}
}

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 = NULL;
	else
		last_undo = NULL;
}

void Mem::reset_memory() {
	story_fp = nullptr;
	blorb_ofs = 0;
	blorb_len = 0;

	if (undo_mem) {
		free_undo(undo_count);
		delete undo_mem;
	}

	undo_mem = nullptr;
	undo_count = 0;
	delete[] 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 Scott
} // End of namespace Glk