aboutsummaryrefslogtreecommitdiff
path: root/engines/dreamweb/runtime.h
blob: 9b70d164a665b582c52d100c9386b5b838fe7b59 (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
/* 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.
 *
 */

#ifndef DREAMGEN_RUNTIME_H
#define DREAMGEN_RUNTIME_H

#include <assert.h>
#include "common/scummsys.h"
#include "common/array.h"
#include "common/debug.h"

#include "dreamweb/segment.h"

namespace DreamGen {

struct Register {
	union {
		uint16 _value;
		uint8 _part[2];
	};
	inline Register(): _value() {}
	inline Register& operator=(uint16 v) { _value = v; return *this; }
	inline operator uint16&() { return _value; }
};

template<int kIndex> //from low to high
struct RegisterPart {
	uint8			&_value;

	explicit inline RegisterPart(Register &reg) : _value(reg._part[kIndex]) {}

	inline operator uint8&() {
		return _value;
	}

	inline RegisterPart& operator=(const RegisterPart& o) {
		_value = o._value;
		return *this;
	}

	inline RegisterPart& operator=(uint8 v) {
		_value = v;
		return *this;
	}
};

#ifdef SCUMM_LITTLE_ENDIAN
	typedef RegisterPart<0> LowPartOfRegister;
	typedef RegisterPart<1> HighPartOfRegister;
#else
	typedef RegisterPart<1> LowPartOfRegister;
	typedef RegisterPart<0> HighPartOfRegister;
#endif

struct Flags {
	bool _z, _c, _s, _o;
	inline Flags(): _z(true), _c(false), _s(false), _o(false) {}

	inline bool z() const	{ return _z; }
	inline bool c() const	{ return _c; }
	inline bool s() const	{ return _s; }

	inline bool l() const	{ return _o != _s; }
	inline bool le() const	{ return _o != _s|| _z; }

	inline void update_zs(uint8 v) {
		_s = v & 0x80;
		_z = v == 0;
	}

	inline void update_zs(uint16 v) {
		_s = v & 0x8000;
		_z = v == 0;
	}

	inline void update_o(uint8 v, uint8 a, uint8 b) {
		uint8 s1 = a & 0x80, s2 = b & 0x80;
		_o = (s1 == s2) && (v & 0x80) != s1;
	}

	inline void update_o(uint16 v, uint16 a, uint16 b) {
		uint16 s1 = a & 0x8000, s2 = b & 0x8000;
		_o = (s1 == s2) && (v & 0x8000) != s1;
	}
};

class Context {
public:
	Register ax, dx, bx, cx, si, di;
	LowPartOfRegister	al;
	HighPartOfRegister	ah;
	LowPartOfRegister	bl;
	HighPartOfRegister	bh;
	LowPartOfRegister	cl;
	HighPartOfRegister	ch;
	LowPartOfRegister	dl;
	HighPartOfRegister	dh;

	SegmentRef cs;
	MutableSegmentRef ds;
	MutableSegmentRef es;
	Flags flags;

	Context(SegmentManager *segMan): al(ax), ah(ax), bl(bx), bh(bx), cl(cx), ch(cx), dl(dx), dh(dx),
		cs(segMan->data),
		ds(segMan, segMan->data),
		es(segMan, segMan->data) {

	}

	inline void _cmp(uint8 a, uint8 b) {
		_sub(a, b);
	}

	inline void _cmp(uint16 a, uint16 b) {
		_sub(a, b);
	}

	inline void _test(uint8 a, uint8 b) {
		_and(a, b);
	}

	inline void _test(uint16 a, uint16 b) {
		_and(a, b);
	}

	inline void _add(uint8 &dst, uint8 src) {
		unsigned r = (unsigned)dst + src;
		flags.update_o((uint8)r, dst, src);
		flags._c = r >= 0x100;
		dst = r;
		flags.update_zs(dst);
	}

	inline void _add(uint16 &dst, uint16 src) {
		unsigned r = (unsigned)dst + src;
		flags.update_o((uint16)r, dst, src);
		flags._c = r >= 0x10000;
		dst = r;
		flags.update_zs(dst);
	}

	inline void _sub(uint8 &dst, uint8 src) {
		flags.update_o(uint8(dst - src), dst, (uint8)-src);
		flags._c = dst < src;
		dst -= src;
		flags.update_zs(dst);
	}

	inline void _sub(uint16 &dst, uint16 src) {
		flags.update_o(uint16(dst - src), dst, (uint16)-src);
		flags._c = dst < src;
		dst -= src;
		flags.update_zs(dst);
	}

	inline void _inc(uint8 &dst) {
		flags.update_o((uint8)(dst + 1), dst, 1);
		++dst;
		flags.update_zs(dst);
	}

	inline void _inc(uint16 &dst) {
		flags.update_o((uint16)(dst + 1), dst, 1);
		++dst;
		flags.update_zs(dst);
	}

	inline void _dec(uint8 &dst) {
		flags.update_o(uint8(dst - 1), dst, 1);
		--dst;
		flags.update_zs(dst);
	}

	inline void _dec(uint16 &dst) {
		flags.update_o(uint16(dst - 1), dst, 1);
		--dst;
		flags.update_zs(dst);
	}

	inline void _and(uint8 &dst, uint8 src) {
		dst &= src;
		flags.update_zs(dst);
		flags._c = flags._o = false;
	}

	inline void _and(uint16 &dst, uint16 src) {
		dst &= src;
		flags.update_zs(dst);
		flags._c = flags._o = false;
	}

	inline void _or(uint8 &dst, uint8 src) {
		dst |= src;
		flags.update_zs(dst);
		flags._c = flags._o = false;
	}

	inline void _or(uint16 &dst, uint16 src) {
		dst |= src;
		flags.update_zs(dst);
		flags._c = flags._o = false;
	}

	inline void _xor(uint8 &dst, uint8 src) {
		dst ^= src;
		flags.update_zs(dst);
		flags._c = flags._o = false;
	}

	inline void _xor(uint16 &dst, uint16 src) {
		dst ^= src;
		flags.update_zs(dst);
		flags._c = flags._o = false;
	}

	inline void _shr(uint8 &dst, uint8 src) {
		src &= 0x1f;
		if (src > 0) {
			dst >>= (src - 1);
			flags._c = dst & 1;
			dst >>= 1;
			flags.update_zs(dst);
		}
		if (src == 1)
			flags._o = dst & 0x80;
	}

	inline void _shr(uint16 &dst, uint8 src) {
		src &= 0x1f;
		if (src > 0) {
			dst >>= (src - 1);
			flags._c = dst & 1;
			dst >>= 1;
			flags.update_zs(dst);
		}
		if (src == 1)
			flags._o = dst & 0x8000;
	}

	inline void _shl(uint8 &dst, uint8 src) {
		src &= 0x1f;
		if (src > 0) {
			dst <<= (src - 1);
			flags._c = dst & 0x80;
			dst <<= 1;
			flags.update_zs(dst);
		}
		if (src == 1)
			flags._o = ((dst & 0x80) != 0) == flags._c;
	}
	inline void _shl(uint16 &dst, uint8 src) {
		src &= 0x1f;
		if (src > 0) {
			dst <<= (src - 1);
			flags._c = dst & 0x8000;
			dst <<= 1;
			flags.update_zs(dst);
		}
		if (src == 1)
			flags._o = ((dst & 0x8000) != 0) == flags._c;
	}

	inline void _mul(uint8 src) {
		unsigned r = unsigned(al) * src;
		ax = (uint16)r;
		flags._c = r >= 0x10000;
		flags._z = r == 0;
		flags._s = r & 0x8000;
		flags._o = ah != 0;
	}

	inline void _mul(uint16 src) {
		unsigned r = unsigned(ax) * src; //assuming here that we have at least 32 bits
		dx = (r >> 16) & 0xffff;
		ax = r & 0xffff;
		flags._c = false;
		flags._z = r == 0;
		flags._s = r & 0x80000000;
		flags._o = dx != 0;
	}

	inline void _neg(uint8 &src) {
		uint8 r = 0;
		_sub(r, src);
		src = r;
	}

	inline void _neg(uint16 &src) {
		uint16 r = 0;
		_sub(r, src);
		src = r;
	}

	inline void _lodsb() {
		al = ds.byte(si++);
	}

	inline void _lodsw() {
		ax = ds.word(si);
		si += 2;
	}

	inline void _movsb() {
		es.byte(di++) = ds.byte(si++);
	}

	inline void _movsb(uint size, bool clear_cx = false) {
		assert(size != 0xffff);
		//fixme: add overlap and segment boundary check and rewrite
		while (size--)
			_movsb();
		if (clear_cx)
			cx = 0;
	}

	inline void _movsw() {
		_movsb();
		_movsb();
	}

	inline void _movsw(uint size, bool clear_cx = false) {
		assert(size != 0xffff);
		_movsb(size * 2, clear_cx);
	}

	inline void _stosb() {
		es.byte(di++) = al;
	}

	inline void _stosb(uint size, bool clear_cx = false) {
		assert(size != 0xffff);
		uint8 *dst = es.ptr(di, size);
		memset(dst, al, size);
		di += size;
		if (clear_cx)
			cx = 0;
	}

	inline void _stosw() {
		es.byte(di++) = al;
		es.byte(di++) = ah;
	}

	inline void _stosw(uint size, bool clear_cx = false) {
		assert(size != 0xffff);
		uint8 *dst = es.ptr(di, size * 2);
		di += 2 * size;
		while (size--) {
			*dst++ = al;
			*dst++ = ah;
		}
		if (clear_cx)
			cx = 0;
	}

	inline void _xchg(uint16 &a, uint16 &b) {
		uint16 x = a;
		a = b;
		b = x;
	}

	inline void _xchg(uint8 &a, uint8 &b) {
		uint8 t = a;
		a = b;
		b = t;
	}

	Common::Array<uint16> stack;
	inline void push(uint16 v) {
		stack.push_back(v);
	}

	inline uint16 pop() {
		assert(!stack.empty());
		uint16 v = stack.back();
		stack.pop_back();
		return v;
	}
};

class StackChecker {
	const Context	&_context;
	const uint		_stackDepth;

public:
	StackChecker(const Context &context): _context(context), _stackDepth(context.stack.size()) {}
	~StackChecker() { assert(_context.stack.size() == _stackDepth); }
};

#ifndef NDEBUG
#	define STACK_CHECK  StackChecker checker(*this)
#else
#	define STACK_CHECK  do {} while (0)
#endif

} // End of namespace DreamGen

#endif