| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "glk.h" |
| 7 |
#include "glulxe.h" |
| 8 |
|
| 9 |
|
| 10 |
unsigned char *memmap = NULL; |
| 11 |
unsigned char *stack = NULL; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
glui32 ramstart; |
| 16 |
glui32 endgamefile; |
| 17 |
glui32 origendmem; |
| 18 |
glui32 stacksize; |
| 19 |
glui32 startfuncaddr; |
| 20 |
glui32 origstringtable; |
| 21 |
glui32 checksum; |
| 22 |
|
| 23 |
|
| 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 |
|
| 34 |
|
| 35 |
glui32 prevpc; |
| 36 |
|
| 37 |
void (*stream_char_handler)(unsigned char ch); |
| 38 |
void (*stream_unichar_handler)(glui32 ch); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
void setup_vm() |
| 45 |
{ |
| 46 |
unsigned char buf[4 * 7]; |
| 47 |
int res; |
| 48 |
|
| 49 |
pc = 0; |
| 50 |
prevpc = 0; |
| 51 |
|
| 52 |
|
| 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 |
|
| 72 |
protectstart = 0; |
| 73 |
protectend = 0; |
| 74 |
|
| 75 |
|
| 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 |
|
| 99 |
|
| 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 |
|
| 114 |
init_operands(); |
| 115 |
init_accel(); |
| 116 |
init_serial(); |
| 117 |
|
| 118 |
|
| 119 |
vm_restart(); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
debugger_check_story_file(); |
| 124 |
|
| 125 |
|
| 126 |
debugger_setup_start_state(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void vm_restart() |
| 154 |
{ |
| 155 |
glui32 lx; |
| 156 |
int res; |
| 157 |
int bufpos; |
| 158 |
char buf[0x100]; |
| 159 |
|
| 160 |
|
| 161 |
heap_clear(); |
| 162 |
|
| 163 |
|
| 164 |
lx = change_memsize(origendmem, FALSE); |
| 165 |
if (lx) |
| 166 |
fatal_error("Memory could not be reset to its original size."); |
| 167 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 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 |
|
| 202 |
|
| 203 |
|
| 204 |
enter_function(startfuncaddr, 0, NULL); |
| 205 |
|
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 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 |
| 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 |
|
| 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 |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
glui32 *pop_arguments(glui32 count, glui32 addr) |
| 264 |
{ |
| 265 |
int ix; |
| 266 |
glui32 argptr; |
| 267 |
glui32 *array; |
| 268 |
|
| 269 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 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 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 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 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 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 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 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 |
|
| 404 |
if (bytecount < count) |
| 405 |
fatal_error_i("Memory access way too long", addr); |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if (bytecount > endmem || addr+bytecount < addr) |
| 410 |
fatal_error_i("Memory access much too long", addr); |
| 411 |
|
| 412 |
if (addr+bytecount > endmem) |
| 413 |
fatal_error_i("Memory access too long", addr); |
| 414 |
} |
| 415 |
|