ScummVM API documentation
vm_types.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef SCI_ENGINE_VM_TYPES_H
23#define SCI_ENGINE_VM_TYPES_H
24
25#include "common/scummsys.h"
26#include "sci/version.h"
27
28namespace Sci {
29
30// Segment ID type
31typedef uint16 SegmentId;
32
33enum {
34 kUninitializedSegment = 0x1FFF,
35 kSegmentMask = 0x1FFF,
36 kOffsetMask = 0x7FFFF
37};
38
39struct reg_t {
40 // Segment and offset. These should never be accessed directly
41 SegmentId _segment;
42 uint16 _offset;
43
44 void init(SegmentId segment, uint32 offset);
45
46 SegmentId getSegment() const;
47 void setSegment(SegmentId segment);
48
49 // speed optimization: inline due to frequent calling
50 uint32 getOffset() const {
51 if (getSciVersion() < SCI_VERSION_3) {
52 return _offset;
53 } else {
54 // Return the lower 16 bits from the offset, and the 17th and 18th bits from the segment
55 return ((_segment & 0xC000) << 2) | _offset;
56 }
57 }
58
59 // speed optimization: inline due to frequent calling
60 void setOffset(uint32 offset) {
61 if (getSciVersion() < SCI_VERSION_3) {
62 _offset = offset;
63 } else {
64 // Store the lower 16 bits in the offset, and the 17th and 18th bits in the segment
65 _offset = offset & 0xFFFF;
66 _segment = ((offset & 0x30000) >> 2) | (_segment & 0x3FFF);
67 }
68 }
69
70 inline void incOffset(int32 offset) {
71 setOffset(getOffset() + offset);
72 }
73
74 inline bool isNull() const {
75 return (getOffset() | getSegment()) == 0;
76 }
77
78 inline uint16 toUint16() const {
79 return (uint16)getOffset();
80 }
81
82 inline int16 toSint16() const {
83 return (int16)getOffset();
84 }
85
86 bool isNumber() const {
87 return getSegment() == 0;
88 }
89
90 bool isPointer() const {
91 return getSegment() != 0 && getSegment() != kUninitializedSegment;
92 }
93
94 uint16 requireUint16() const;
95 int16 requireSint16() const;
96
97 inline bool isInitialized() const {
98 return getSegment() != kUninitializedSegment;
99 }
100
101 // Comparison operators
102 bool operator==(const reg_t &x) const {
103 return (getOffset() == x.getOffset()) && (getSegment() == x.getSegment());
104 }
105
106 bool operator!=(const reg_t &x) const {
107 return (getOffset() != x.getOffset()) || (getSegment() != x.getSegment());
108 }
109
110 bool operator>(const reg_t right) const {
111 return cmp(right, false) > 0;
112 }
113
114 bool operator>=(const reg_t right) const {
115 return cmp(right, false) >= 0;
116 }
117
118 bool operator<(const reg_t right) const {
119 return cmp(right, false) < 0;
120 }
121
122 bool operator<=(const reg_t right) const {
123 return cmp(right, false) <= 0;
124 }
125
126 // Same as the normal operators, but perform unsigned
127 // integer checking
128 bool gtU(const reg_t right) const {
129 return cmp(right, true) > 0;
130 }
131
132 bool geU(const reg_t right) const {
133 return cmp(right, true) >= 0;
134 }
135
136 bool ltU(const reg_t right) const {
137 return cmp(right, true) < 0;
138 }
139
140 bool leU(const reg_t right) const {
141 return cmp(right, true) <= 0;
142 }
143
144 // Arithmetic operators
145 reg_t operator+(const reg_t right) const;
146 reg_t operator-(const reg_t right) const;
147 reg_t operator*(const reg_t right) const;
148 reg_t operator/(const reg_t right) const;
149 reg_t operator%(const reg_t right) const;
150 reg_t operator>>(const reg_t right) const;
151 reg_t operator<<(const reg_t right) const;
152
153 reg_t operator+(int16 right) const;
154 reg_t operator-(int16 right) const;
155
156 void operator+=(const reg_t &right) { *this = *this + right; }
157 void operator-=(const reg_t &right) { *this = *this - right; }
158 void operator+=(int16 right) { *this = *this + right; }
159 void operator-=(int16 right) { *this = *this - right; }
160
161 // Boolean operators
162 reg_t operator&(const reg_t right) const;
163 reg_t operator|(const reg_t right) const;
164 reg_t operator^(const reg_t right) const;
165
166#ifdef ENABLE_SCI32
167 reg_t operator&(int16 right) const;
168 reg_t operator|(int16 right) const;
169 reg_t operator^(int16 right) const;
170
171 void operator&=(const reg_t &right) { *this = *this & right; }
172 void operator|=(const reg_t &right) { *this = *this | right; }
173 void operator^=(const reg_t &right) { *this = *this ^ right; }
174 void operator&=(int16 right) { *this = *this & right; }
175 void operator|=(int16 right) { *this = *this | right; }
176 void operator^=(int16 right) { *this = *this ^ right; }
177#endif
178
179private:
187 int cmp(const reg_t right, bool treatAsUnsigned) const;
188 reg_t lookForWorkaround(const reg_t right, const char *operation) const;
189 bool pointerComparisonWithInteger(const reg_t right) const;
190
191#ifdef ENABLE_SCI32
192 int sci32Comparison(const reg_t right) const;
193#endif
194};
195
196static inline reg_t make_reg(SegmentId segment, uint16 offset) {
197 reg_t r;
198 r.init(segment, offset);
199 return r;
200}
201
202static inline reg_t make_reg32(SegmentId segment, uint32 offset) {
203 reg_t r;
204 r.init(segment, offset);
205 return r;
206}
207
208#define PRINT_REG(r) (kSegmentMask) & (unsigned) (r).getSegment(), (unsigned) (r).getOffset()
209
210// Stack pointer type
211typedef reg_t *StackPtr;
212
213enum {
219 SIGNAL_OFFSET = 0xffff
221
222extern const reg_t NULL_REG;
223extern const reg_t SIGNAL_REG;
224extern const reg_t TRUE_REG;
225
226// Selector ID
227typedef int Selector;
228
229enum {
231 NULL_SELECTOR = -1
233
234// Opcode formats
235enum opcode_format {
236 Script_Invalid = -1,
237 Script_None = 0,
238 Script_Byte,
239 Script_SByte,
240 Script_Word,
241 Script_SWord,
242 Script_Variable,
243 Script_SVariable,
244 Script_SRelative,
245 Script_Property,
246 Script_Global,
247 Script_Local,
248 Script_Temp,
249 Script_Param,
250 Script_Offset,
251 Script_End
252};
253
254
255} // End of namespace Sci
256
257#endif // SCI_ENGINE_VM_TYPES_H
Definition: console.h:28
@ SIGNAL_OFFSET
Definition: vm_types.h:219
@ NULL_SELECTOR
Definition: vm_types.h:231
Definition: vm_types.h:39