Debian Codegraph @

terps/glulxe/vm.c

raw symbols SCIP
1 /* vm.c: Glulxe code related to the VM overall. Also miscellaneous stuff.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8
9 /* The memory blocks which contain VM main memory and the stack. */
10 unsigned char *memmap = NULL;
11 unsigned char *stack = NULL;
12
13 /* Various memory addresses which are useful. These are loaded in from
14 the game file header. */
15 glui32 ramstart;
16 glui32 endgamefile;
17 glui32 origendmem;
18 glui32 stacksize;
19 glui32 startfuncaddr;
20 glui32 origstringtable;
21 glui32 checksum;
22
23 /* The VM registers. */
24 glui32 stackptr;
25 glui32 frameptr;
26 glui32 pc;
27 glui32 stringtable;
28 glui32 valstackbase;
29 glui32 localsbase;
30 glui32 endmem;
31 glui32 protectstart, protectend;
32
33 /* This is not needed for VM operation, but it may be needed for
34 autosave/autorestore. */
35 glui32 prevpc;
36
37 void (*stream_char_handler)(unsigned char ch);
38 void (*stream_unichar_handler)(glui32 ch);
39
40 /* setup_vm():
41 Read in the game file and build the machine, allocating all the memory
42 necessary.
43 */
44 void setup_vm()
45 {
46 unsigned char buf[4 * 7];
47 int res;
48
49 pc = 0; /* Clear this, so that error messages are cleaner. */
50 prevpc = 0;
51
52 /* Read in all the size constants from the game file header. */
53
54 stream_char_handler = NULL;
55 stream_unichar_handler = NULL;
56
57 glk_stream_set_position(gamefile, gamefile_start+8, seekmode_Start);
58 res = glk_get_buffer_stream(gamefile, (char *)buf, 4 * 7);
59 if (res != 4 * 7) {
60 fatal_error("The game file header is too short.");
61 }
62
63 ramstart = Read4(buf+0);
64 endgamefile = Read4(buf+4);
65 origendmem = Read4(buf+8);
66 stacksize = Read4(buf+12);
67 startfuncaddr = Read4(buf+16);
68 origstringtable = Read4(buf+20);
69 checksum = Read4(buf+24);
70
71 /* Set the protection range to (0, 0), meaning "off". */
72 protectstart = 0;
73 protectend = 0;
74
75 /* Do a few sanity checks. */
76
77 if ((ramstart & 0xFF)
78 || (endgamefile & 0xFF)
79 || (origendmem & 0xFF)
80 || (stacksize & 0xFF)) {
81 nonfatal_warning("One of the segment boundaries in the header is not "
82 "256-byte aligned.");
83 }
84
85 if (endgamefile != gamefile_len) {
86 nonfatal_warning("The gamefile length does not match the header "
87 "endgamefile length.");
88 }
89
90 if (ramstart < 0x100 || endgamefile < ramstart || origendmem < endgamefile) {
91 fatal_error("The segment boundaries in the header are in an impossible "
92 "order.");
93 }
94 if (stacksize < 0x100) {
95 fatal_error("The stack size in the header is too small.");
96 }
97
98 /* Allocate main memory and the stack. This is where memory allocation
99 errors are most likely to occur. */
100 endmem = origendmem;
101 memmap = (unsigned char *)glulx_malloc(origendmem);
102 if (!memmap) {
103 fatal_error("Unable to allocate Glulx memory space.");
104 }
105 stack = (unsigned char *)glulx_malloc(stacksize);
106 if (!stack) {
107 glulx_free(memmap);
108 memmap = NULL;
109 fatal_error("Unable to allocate Glulx stack space.");
110 }
111 stringtable = 0;
112
113 /* Initialize various other things in the terp. */
114 init_operands();
115 init_accel();
116 init_serial();
117
118 /* Set up the initial machine state. */
119 vm_restart();
120
121 /* If the debugger is compiled in, check that the debug data matches
122 the game. (This only prints warnings for mismatch.) */
123 debugger_check_story_file();
124 /* Also, set up any start-time debugger state. This may do a block-
125 and-debug, if the user has requested that. */
126 debugger_setup_start_state();
127 }
128
129 /* finalize_vm():
130 Deallocate all the memory and shut down the machine.
131 */
132 void finalize_vm()
133 {
134 stream_set_table(0);
135
136 if (memmap) {
137 glulx_free(memmap);
138 memmap = NULL;
139 }
140 if (stack) {
141 glulx_free(stack);
142 stack = NULL;
143 }
144
145 final_serial();
146 }
147
148 /* vm_restart():
149 Put the VM into a state where it's ready to begin executing the
150 game. This is called both at startup time, and when the machine
151 performs a "restart" opcode.
152 */
153 void vm_restart()
154 {
155 glui32 lx;
156 int res;
157 int bufpos;
158 char buf[0x100];
159
160 /* Deactivate the heap (if it was active). */
161 heap_clear();
162
163 /* Reset memory to the original size. */
164 lx = change_memsize(origendmem, FALSE);
165 if (lx)
166 fatal_error("Memory could not be reset to its original size.");
167
168 /* Load in all of main memory. We do this in 256-byte chunks, because
169 why rely on OS stream buffering? */
170 glk_stream_set_position(gamefile, gamefile_start, seekmode_Start);
171 bufpos = 0x100;
172
173 for (lx=0; lx<endgamefile; lx++) {
174 if (bufpos >= 0x100) {
175 int count = glk_get_buffer_stream(gamefile, buf, 0x100);
176 if (count != 0x100) {
177 fatal_error("The game file ended unexpectedly.");
178 }
179 bufpos = 0;
180 }
181
182 res = buf[bufpos++];
183 if (lx >= protectstart && lx < protectend)
184 continue;
185 memmap[lx] = res;
186 }
187 for (lx=endgamefile; lx<origendmem; lx++) {
188 memmap[lx] = 0;
189 }
190
191 /* Reset all the registers */
192 stackptr = 0;
193 frameptr = 0;
194 pc = 0;
195 prevpc = 0;
196 stream_set_iosys(0, 0);
197 stream_set_table(origstringtable);
198 valstackbase = 0;
199 localsbase = 0;
200
201 /* Note that we do not reset the protection range. */
202
203 /* Push the first function call. (No arguments.) */
204 enter_function(startfuncaddr, 0, NULL);
205
206 /* We're now ready to execute. */
207 }
208
209 /* change_memsize():
210 Change the size of the memory map. This may not be available at
211 all; #define FIXED_MEMSIZE if you want the interpreter to
212 unconditionally refuse. The internal flag should be true only when
213 the heap-allocation system is calling.
214 Returns 0 for success; otherwise, the operation failed.
215 */
216 glui32 change_memsize(glui32 newlen, int internal)
217 {
218 long lx;
219 unsigned char *newmemmap;
220
221 if (newlen == endmem)
222 return 0;
223
224 #ifdef FIXED_MEMSIZE
225 return 1;
226 #else /* FIXED_MEMSIZE */
227
228 if ((!internal) && heap_is_active())
229 fatal_error("Cannot resize Glulx memory space while heap is active.");
230
231 if (newlen < origendmem)
232 fatal_error("Cannot resize Glulx memory space smaller than it started.");
233
234 if (newlen & 0xFF)
235 fatal_error("Can only resize Glulx memory space to a 256-byte boundary.");
236
237 newmemmap = (unsigned char *)glulx_realloc(memmap, newlen);
238 if (!newmemmap) {
239 /* The old block is still in place, unchanged. */
240 return 1;
241 }
242 memmap = newmemmap;
243
244 if (newlen > endmem) {
245 for (lx=endmem; lx<newlen; lx++) {
246 memmap[lx] = 0;
247 }
248 }
249
250 endmem = newlen;
251
252 return 0;
253
254 #endif /* FIXED_MEMSIZE */
255 }
256
257 /* pop_arguments():
258 If addr is 0, pop N arguments off the stack, and put them in an array.
259 If non-0, take N arguments from that main memory address instead.
260 This has to dynamically allocate if there are more than 32 arguments,
261 but that shouldn't be a problem.
262 */
263 glui32 *pop_arguments(glui32 count, glui32 addr)
264 {
265 int ix;
266 glui32 argptr;
267 glui32 *array;
268
269 /* This shouldn't happen. */
270 if (count & 0x80000000)
271 fatal_error("Argument count is negative");
272
273 #define MAXARGS (32)
274 static glui32 statarray[MAXARGS];
275 static glui32 *dynarray = NULL;
276 static glui32 dynarray_size = 0;
277
278 if (count == 0)
279 return NULL;
280
281 if (count <= MAXARGS) {
282 /* Store in the static array. */
283 array = statarray;
284 }
285 else {
286 if (!dynarray) {
287 dynarray_size = count+8;
288 dynarray = glulx_malloc(sizeof(glui32) * dynarray_size);
289 if (!dynarray)
290 fatal_error("Unable to allocate function arguments.");
291 array = dynarray;
292 }
293 else {
294 if (dynarray_size >= count) {
295 /* It fits. */
296 array = dynarray;
297 }
298 else {
299 dynarray_size = count+8;
300 dynarray = glulx_realloc(dynarray, sizeof(glui32) * dynarray_size);
301 if (!dynarray)
302 fatal_error("Unable to reallocate function arguments.");
303 array = dynarray;
304 }
305 }
306 }
307
308 if (!addr) {
309 if (stackptr < valstackbase+4*count)
310 fatal_error("Stack underflow in arguments.");
311 stackptr -= 4*count;
312 for (ix=0; ix<count; ix++) {
313 argptr = stackptr+4*((count-1)-ix);
314 array[ix] = Stk4(argptr);
315 }
316 }
317 else {
318 for (ix=0; ix<count; ix++) {
319 array[ix] = Mem4(addr);
320 addr += 4;
321 }
322 }
323
324 return array;
325 }
326
327 /* verify_address():
328 Make sure that count bytes beginning with addr all fall within the
329 current memory map. This is called at every memory (read) access if
330 VERIFY_MEMORY_ACCESS is defined in the header file.
331 */
332 void verify_address(glui32 addr, glui32 count)
333 {
334 if (addr >= endmem)
335 fatal_error_i("Memory access out of range", addr);
336 if (count > 1) {
337 addr += (count-1);
338 if (addr >= endmem)
339 fatal_error_i("Memory access out of range", addr);
340 }
341 }
342
343 /* verify_address_write():
344 Make sure that count bytes beginning with addr all fall within RAM.
345 This is called at every memory write if VERIFY_MEMORY_ACCESS is
346 defined in the header file.
347 */
348 void verify_address_write(glui32 addr, glui32 count)
349 {
350 if (addr < ramstart)
351 fatal_error_i("Memory write to read-only address", addr);
352 if (addr >= endmem)
353 fatal_error_i("Memory access out of range", addr);
354 if (count > 1) {
355 addr += (count-1);
356 if (addr >= endmem)
357 fatal_error_i("Memory access out of range", addr);
358 }
359 }
360
361 /* verify_address_stack():
362 Make sure that count bytes beginning with stackpos all fall
363 within the stack. This is called at every stack access if
364 VERIFY_MEMORY_ACCESS is defined in the header file.
365 */
366 void verify_address_stack(glui32 stackpos, glui32 count)
367 {
368 if (stackpos >= stacksize || stackpos+count > stacksize)
369 fatal_error_i("Stack access out of range", stackpos);
370
371 switch (count) {
372 case 1:
373 break;
374 case 2:
375 if (stackpos & 1)
376 fatal_error_i("Unaligned stack access (2)", stackpos);
377 break;
378 case 4:
379 if (stackpos & 3)
380 fatal_error_i("Unaligned stack access (4)", stackpos);
381 break;
382 default:
383 fatal_error_i("Invalid stack access size", count);
384 }
385 }
386
387 /* verify_array_addresses():
388 Make sure that an array of count elements (size bytes each),
389 starting at addr, does not fall outside the memory map. This goes
390 to some trouble that verify_address() does not, because we need
391 to be wary of lengths near -- or beyond -- 0x7FFFFFFF.
392 */
393 void verify_array_addresses(glui32 addr, glui32 count, glui32 size)
394 {
395 glui32 bytecount;
396 if (addr >= endmem)
397 fatal_error_i("Memory access out of range", addr);
398
399 if (count == 0)
400 return;
401 bytecount = count*size;
402
403 /* If just multiplying by the element size overflows, we have trouble. */
404 if (bytecount < count)
405 fatal_error_i("Memory access way too long", addr);
406
407 /* If the byte length by itself is too long, or if its end overflows,
408 we have trouble. */
409 if (bytecount > endmem || addr+bytecount < addr)
410 fatal_error_i("Memory access much too long", addr);
411 /* The simple length test. */
412 if (addr+bytecount > endmem)
413 fatal_error_i("Memory access too long", addr);
414 }
415