aboutsummaryrefslogtreecommitdiff
path: root/engines/dreamweb/runtime.h
blob: fb634b6e05c80f4ef620ce0a298e2db38156ae77 (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
#ifndef ENGINES_DREAMGEN_RUNTIME_H__
#define ENGINES_DREAMGEN_RUNTIME_H__

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

namespace dreamgen {

//fixme: name clash
#undef random

enum { kLowPartOfRegister = 0, kHighPartOfRegister = 1 };

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; }
	inline void cbw() {
		if (_value & 0x80)
			_value |= 0xff00;
		else
			_value &= 0xff;
	}
};

template<unsigned PART>
struct RegisterPart {
	uint8 &_part;

	inline RegisterPart(Register &reg) : _part(reg._part[PART]) {}

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

class WordRef {
	Common::Array<uint8>	&_data;
	unsigned				_index;
	bool					_changed;
	uint16					_value;

public:

	inline WordRef(Common::Array<uint8> &data, unsigned index) : _data(data), _index(index), _changed(false) {
		assert(index + 1 < data.size());
		_value = _data[index] | (_data[index + 1] << 8);
	}
	inline WordRef& operator=(const WordRef &ref) {
		_changed = true;
		_value = ref._value;
		return *this;
	}
	inline WordRef& operator=(uint16 v) {
		_changed = true;
		_value = v;
		return *this;
	}
	inline operator uint16() const {
		return _value;
	}
	inline operator uint16&() {
		return _value;
	}
	inline ~WordRef() {
		if (_changed) {
			_data[_index] = _value & 0xff;
			_data[_index + 1] = _value >> 8;
		}
	}
};

struct Segment {
	Common::Array<uint8> data;
	
	inline void assign(const uint8 *b, const uint8 *e) {
		data.assign(b, e);
	}
	
	inline uint8 &byte(unsigned index) {
		assert(index < data.size());
		return data[index];
	}
	
	inline uint16 word(unsigned index) const {
		assert(index + 1 < data.size());
		return data[index] | (data[index + 1] << 8);
	}

	inline WordRef word(unsigned index) {
		return WordRef(data, index);
	}
};

class Context;

class SegmentRef {
	Context			*_context;
	uint16			_value;
	Segment			*_segment;

public:
	SegmentRef(Context *ctx): _context(ctx), _value(), _segment() {
	}

	inline void reset(uint16 value) {
		
	}

	inline SegmentRef& operator=(const uint16 id) {
		return *this;
	}

	inline uint8 &byte(unsigned index) {
		assert(_segment != 0);
		return _segment->byte(index);
	}

	inline uint16 word(unsigned index) const {
		assert(_segment != 0);
		return _segment->word(index);
	}
	
	inline operator uint16() const {
		return _value;
	}

	inline WordRef word(unsigned index) {
		assert(_segment != 0);
		return _segment->word(index);
	}
};

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; }
	//complex flags:
	inline bool g() const { return !_z && _s == _o; }
	inline bool ge() const { return _z || _s == _o; }
	inline bool l() const { return !_z && _s != _o; }
	inline bool le() const { return _z || _s != _o; }
	
	inline void update_sign(uint8 v) {
		bool new_s = v & 0x80;
		_o = new_s != _s;
		_s = new_s;
		_z = v == 0;
	}

	inline void update(uint16 v) {
		bool new_s = v & 0x8000;
		_o = new_s != _s;
		_s = new_s;
		_z = v == 0;
	}
};

class Context {
	Common::HashMap<uint16, Segment> _segments;
	
public:
	Register ax, dx, bx, cx, si, di;
	RegisterPart<kLowPartOfRegister> al;
	RegisterPart<kHighPartOfRegister> ah;
	RegisterPart<kLowPartOfRegister> bl;
	RegisterPart<kHighPartOfRegister> bh;
	RegisterPart<kLowPartOfRegister> cl;
	RegisterPart<kHighPartOfRegister> ch;
	RegisterPart<kLowPartOfRegister> dl;
	RegisterPart<kHighPartOfRegister> dh;
	
	SegmentRef cs, ds, es;
	Flags flags;

	inline Context(): al(ax), ah(ax), bl(bx), bh(bx), cl(cx), ch(cx), dl(dx), dh(dx), cs(this), ds(this), es(this) {
		
	}

	inline void _cmp(uint8 a, uint8 b) {
		uint8 x = a;
		_sub(x, b);
	}
	inline void _cmp(uint16 a, uint16 b) {
		uint16 x = a;
		_sub(x, b);
	}
	inline void _test(uint8 a, uint8 b) {
		uint8 x = a;
		_and(x, b);
	}
	inline void _test(uint16 a, uint16 b) {
		uint16 x = a;
		_and(x, b);
	}
	
	inline void _add(uint8 &dst, uint8 src) {
		flags._c = dst + src < dst;
		dst += src;
		flags.update(dst);
	}
	inline void _add(uint16 &dst, uint16 src) {
		flags._c = dst + src < dst;
		dst += src;
		flags.update(dst);
	}
	inline void _sub(uint8 &dst, uint8 src) {
		flags._c = dst < src;
		dst -= src;
		flags.update(dst);
	}
	inline void _sub(uint16 &dst, uint16 src) {
		flags._c = dst < src;
		dst -= src;
		flags.update(dst);
	}

	inline void _and(uint8 &dst, uint8 src) {
		dst &= src;
		flags._c = false;
		flags.update(dst);
	}
	inline void _and(uint16 &dst, uint16 src) {
		dst &= src;
		flags._c = false;
		flags.update(dst);
	}
	inline void _or(uint8 &dst, uint8 src) {
		dst |= src;
		flags._c = false;
		flags.update(dst);
	}
	inline void _or(uint16 &dst, uint16 src) {
		dst |= src;
		flags._c = false;
		flags.update(dst);
	}

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

	inline void _shr(uint8 &dst, uint8 src) {}
	inline void _shr(uint16 &dst, uint8 src) {}
	inline void _shl(uint8 &dst, uint8 src) {}
	inline void _shl(uint16 &dst, uint8 src) {}
	inline void _mul(uint8 src) {
		unsigned r = unsigned(al) * src;
		ax = (uint16)r;
		flags._c = r >= 0x10000;
		flags._z = r == 0;
		bool s = r & 0x8000;
		flags._o = s != flags._s;
		flags._s = s;
	}
	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;//fixme
		flags._z = r == 0;
		bool s = r & 0x80000000;
		flags._o = s != flags._s;
		flags._s = s;
	}
	inline void _neg(uint8 &src) {
		src = ~src;
		flags._c = false;
		flags.update(src);
	}
	inline void _neg(uint16 &src) {
		src = ~src;
		flags._c = false;
		flags.update(src);
	}

	inline void _movsb() {
		es.byte(di++) = ds.byte(si++);
	}
	inline void _movsw() {
		es.word(di) = ds.word(si);
		di += 2;
		si += 2;
	}
	inline void _lodsb() {
		al = ds.byte(si++);
	}
	inline void _lodsw() {
		ax = ds.word(si);
		si += 2;
	}
	inline void _stosb() {
		es.byte(di++) = al;
	}
	inline void _stosw() {
		es.word(di) = al;
		di += 2;
	}
	
	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() {
		uint16 v = stack.back();
		stack.pop_back();
		return v;
	}
	
	Segment data;
};

}

#endif