aboutsummaryrefslogtreecommitdiff
path: root/engines/dreamweb/segment.h
blob: 65bc2335ea7606ff1d256c35ba1757711ab35aa1 (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
/* 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_SEGMENT_H
#define DREAMGEN_SEGMENT_H

#include "common/array.h"
#include "common/ptr.h"
#include "common/hashmap.h"
#include "common/list.h"

namespace DreamGen {

class WordRef {
	uint8		*_data;
	unsigned	_index;
	uint16		_value;

public:
	inline WordRef(Common::Array<uint8> &data, unsigned index) : _data(data.begin() + index), _index(index) {
		assert(index + 1 < data.size());
		_value = _data[0] | (_data[1] << 8);
	}

	inline WordRef& operator=(const WordRef &ref) {
		_value = ref._value;
		return *this;
	}

	inline WordRef& operator=(uint16 v) {
		_value = v;
		return *this;
	}

	inline operator uint16&() {
		return _value;
	}

	inline ~WordRef() {
		// FIXME: This is _too late_ to write back the
		// value. Example: in the call
		// printDirect(data.word(kLastxpos), .....)
		// the destructor isn't called until after printDirect returns. This
		// destroys the modifications to lastXPos that printDirect makes.
		_data[0] = _value & 0xff;
		_data[1] = _value >> 8;
		_value = _data[0] | (_data[1] << 8);
	}
};

class Segment {
	Common::Array<uint8> data;

public:
	Segment(uint size = 0) {
		if (size > 0)
			data.resize(size);
	}

	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 WordRef word(unsigned index) {
		return WordRef(data, index);
	}

	inline uint8 *ptr(unsigned index, unsigned size) {
		assert(index + size <= data.size());
		return data.begin() + index;
	}
};

typedef Common::SharedPtr<Segment> SegmentPtr;

class SegmentRef {
	uint16		_value;
	SegmentPtr	_segment;

public:
	SegmentRef(uint16 value = 0, SegmentPtr segment = SegmentPtr())
	: _value(value), _segment(segment) {
	}

	inline operator uint16() const {
		return _value;
	}

	SegmentPtr getSegmentPtr() const {
		return _segment;
	}

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

	inline WordRef word(unsigned index) {
		//debug(1, "getting word ref for %04x:%d", _value, index);
		assert(_segment != 0);
		return _segment->word(index);
	}

	inline void assign(const uint8 *b, const uint8 *e) {
		assert(_segment != 0);
		_segment->assign(b, e);
	}

	inline uint8 *ptr(unsigned index, unsigned size) {
		assert(_segment != 0);
		return _segment->ptr(index, size);
	}

protected:
	SegmentRef &operator=(const SegmentRef &seg) {
		_value = seg._value;
		_segment = seg._segment;
		return *this;
	}

};

class SegmentManager;

class MutableSegmentRef : public SegmentRef {
protected:
	SegmentManager		*_segMan;

public:
	MutableSegmentRef(SegmentManager *segMan, uint16 value = 0, SegmentPtr segment = SegmentPtr())
	: _segMan(segMan), SegmentRef(value, segment) {
	}

	MutableSegmentRef(SegmentManager *segMan, SegmentRef seg)
	: _segMan(segMan), SegmentRef(seg) {
	}

	inline MutableSegmentRef& operator=(const uint16 id);

};


class SegmentManager {
private:
	typedef Common::HashMap<uint16, SegmentPtr> SegmentMap;
	SegmentMap _segments;

	typedef Common::List<uint16> FreeSegmentList;
	FreeSegmentList _freeSegments;

	enum { kDefaultDataSegment = 0x1000 };

public:

	SegmentPtr _realData;	///< the primary data segment, points to a huge blob of binary data
	SegmentRef data;	///< fake segment register always pointing to data segment

public:
	SegmentManager() :
		_realData(new Segment()),
		data(kDefaultDataSegment, _realData) {

		_segments[kDefaultDataSegment] = data.getSegmentPtr();
	}

	SegmentRef getSegment(uint16 value) {
		SegmentMap::iterator i = _segments.find(value);
		if (i != _segments.end())
			return SegmentRef(value, i->_value);
		else
			return SegmentRef(value);
	}

	SegmentRef allocateSegment(uint size) {
		unsigned id;
		if (_freeSegments.empty())
			id = kDefaultDataSegment + _segments.size();
		else {
			id = _freeSegments.front();
			_freeSegments.pop_front();
		}
		assert(!_segments.contains(id));
		SegmentPtr seg(new Segment(size));
		_segments[id] = seg;
		return SegmentRef(id, seg);
	}

	void deallocateSegment(uint16 id) {
		SegmentMap::iterator i = _segments.find(id);
		if(i != _segments.end()) {
			_segments.erase(i);
			_freeSegments.push_back(id);
		} else {
			debug("Deallocating non existent segment! Client code should be fixed.");
		}
	}

};


inline MutableSegmentRef& MutableSegmentRef::operator=(const uint16 id) {
	SegmentRef::operator=(_segMan->getSegment(id));
	return *this;
}

} // End of namespace DreamGen

#endif