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 
28 namespace Sci {
29 
30 // Segment ID type
31 typedef uint16 SegmentId;
32 
33 enum {
34  kUninitializedSegment = 0x1FFF,
35  kSegmentMask = 0x1FFF,
36  kOffsetMask = 0x7FFFF
37 };
38 
39 struct reg_t {
40  // Segment and offset. These should never be accessed directly
41  SegmentId _segment;
42  uint16 _offset;
43 
44  SegmentId getSegment() const;
45  void setSegment(SegmentId segment);
46 
47  // speed optimization: inline due to frequent calling
48  uint32 getOffset() const {
49  if (getSciVersion() < SCI_VERSION_3) {
50  return _offset;
51  } else {
52  // Return the lower 16 bits from the offset, and the 17th and 18th bits from the segment
53  return ((_segment & 0xC000) << 2) | _offset;
54  }
55  }
56 
57  // speed optimization: inline due to frequent calling
58  void setOffset(uint32 offset) {
59  if (getSciVersion() < SCI_VERSION_3) {
60  _offset = offset;
61  } else {
62  // Store the lower 16 bits in the offset, and the 17th and 18th bits in the segment
63  _offset = offset & 0xFFFF;
64  _segment = ((offset & 0x30000) >> 2) | (_segment & 0x3FFF);
65  }
66  }
67 
68  inline void incOffset(int32 offset) {
69  setOffset(getOffset() + offset);
70  }
71 
72  inline bool isNull() const {
73  return (getOffset() | getSegment()) == 0;
74  }
75 
76  inline uint16 toUint16() const {
77  return (uint16)getOffset();
78  }
79 
80  inline int16 toSint16() const {
81  return (int16)getOffset();
82  }
83 
84  bool isNumber() const {
85  return getSegment() == 0;
86  }
87 
88  bool isPointer() const {
89  return getSegment() != 0 && getSegment() != kUninitializedSegment;
90  }
91 
92  uint16 requireUint16() const;
93  int16 requireSint16() const;
94 
95  inline bool isInitialized() const {
96  return getSegment() != kUninitializedSegment;
97  }
98 
99  // Comparison operators
100  bool operator==(const reg_t &x) const {
101  return (getOffset() == x.getOffset()) && (getSegment() == x.getSegment());
102  }
103 
104  bool operator!=(const reg_t &x) const {
105  return (getOffset() != x.getOffset()) || (getSegment() != x.getSegment());
106  }
107 
108  bool operator>(const reg_t right) const {
109  return cmp(right, false) > 0;
110  }
111 
112  bool operator>=(const reg_t right) const {
113  return cmp(right, false) >= 0;
114  }
115 
116  bool operator<(const reg_t right) const {
117  return cmp(right, false) < 0;
118  }
119 
120  bool operator<=(const reg_t right) const {
121  return cmp(right, false) <= 0;
122  }
123 
124  // Same as the normal operators, but perform unsigned
125  // integer checking
126  bool gtU(const reg_t right) const {
127  return cmp(right, true) > 0;
128  }
129 
130  bool geU(const reg_t right) const {
131  return cmp(right, true) >= 0;
132  }
133 
134  bool ltU(const reg_t right) const {
135  return cmp(right, true) < 0;
136  }
137 
138  bool leU(const reg_t right) const {
139  return cmp(right, true) <= 0;
140  }
141 
142  // Arithmetic operators
143  reg_t operator+(const reg_t right) const;
144  reg_t operator-(const reg_t right) const;
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 
151  reg_t operator+(int16 right) const;
152  reg_t operator-(int16 right) const;
153 
154  void operator+=(const reg_t &right) { *this = *this + right; }
155  void operator-=(const reg_t &right) { *this = *this - right; }
156  void operator+=(int16 right) { *this = *this + right; }
157  void operator-=(int16 right) { *this = *this - right; }
158 
159  // Boolean operators
160  reg_t operator&(const reg_t right) const;
161  reg_t operator|(const reg_t right) const;
162  reg_t operator^(const reg_t right) const;
163 
164 #ifdef ENABLE_SCI32
165  reg_t operator&(int16 right) const;
166  reg_t operator|(int16 right) const;
167  reg_t operator^(int16 right) const;
168 
169  void operator&=(const reg_t &right) { *this = *this & right; }
170  void operator|=(const reg_t &right) { *this = *this | right; }
171  void operator^=(const reg_t &right) { *this = *this ^ right; }
172  void operator&=(int16 right) { *this = *this & right; }
173  void operator|=(int16 right) { *this = *this | right; }
174  void operator^=(int16 right) { *this = *this ^ right; }
175 #endif
176 
177 private:
185  int cmp(const reg_t right, bool treatAsUnsigned) const;
186  reg_t lookForWorkaround(const reg_t right, const char *operation) const;
187  bool pointerComparisonWithInteger(const reg_t right) const;
188 
189 #ifdef ENABLE_SCI32
190  int sci32Comparison(const reg_t right) const;
191 #endif
192 };
193 
194 static inline reg_t make_reg(SegmentId segment, uint16 offset) {
195  reg_t r;
196  r.setSegment(segment);
197  r.setOffset(offset);
198  return r;
199 }
200 
201 static inline reg_t make_reg32(SegmentId segment, uint32 offset) {
202  reg_t r;
203  r.setSegment(segment);
204  r.setOffset(offset);
205  return r;
206 }
207 
208 #define PRINT_REG(r) (kSegmentMask) & (unsigned) (r).getSegment(), (unsigned) (r).getOffset()
209 
210 // Stack pointer type
211 typedef reg_t *StackPtr;
212 
213 enum {
219  SIGNAL_OFFSET = 0xffff
220 };
221 
222 extern const reg_t NULL_REG;
223 extern const reg_t SIGNAL_REG;
224 extern const reg_t TRUE_REG;
225 
226 // Selector ID
227 typedef int Selector;
228 
229 enum {
232 };
233 
234 // Opcode formats
235 enum 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: vm_types.h:219
Definition: console.h:28
Definition: vm_types.h:231
Definition: vm_types.h:39