| 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 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#include <stdint.h> |
| 45 |
typedef uint32_t glui32; |
| 46 |
typedef int32_t glsi32; |
| 47 |
|
| 48 |
#include "opcodes.h" |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#ifndef TRUE |
| 53 |
#define TRUE 1 |
| 54 |
#endif |
| 55 |
#ifndef FALSE |
| 56 |
#define FALSE 0 |
| 57 |
#endif |
| 58 |
#ifndef NULL |
| 59 |
#define NULL 0 |
| 60 |
#endif |
| 61 |
|
| 62 |
#define Read4(ptr) \ |
| 63 |
( (glui32)(((unsigned char *)(ptr))[0] << 24) \ |
| 64 |
| (glui32)(((unsigned char *)(ptr))[1] << 16) \ |
| 65 |
| (glui32)(((unsigned char *)(ptr))[2] << 8) \ |
| 66 |
| (glui32)(((unsigned char *)(ptr))[3])) |
| 67 |
#define Read2(ptr) \ |
| 68 |
( (glui32)(((unsigned char *)(ptr))[0] << 8) \ |
| 69 |
| (glui32)(((unsigned char *)(ptr))[1])) |
| 70 |
#define Read1(ptr) \ |
| 71 |
((unsigned char)(((unsigned char *)(ptr))[0])) |
| 72 |
|
| 73 |
#define Mem1(adr) (Read1(memmap+(adr))) |
| 74 |
#define Mem2(adr) (Read2(memmap+(adr))) |
| 75 |
#define Mem4(adr) (Read4(memmap+(adr))) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#define nop_gc 0 |
| 81 |
#define add_gc 1 |
| 82 |
#define sub_gc 2 |
| 83 |
#define mul_gc 3 |
| 84 |
#define div_gc 4 |
| 85 |
#define mod_gc 5 |
| 86 |
#define neg_gc 6 |
| 87 |
#define bitand_gc 7 |
| 88 |
#define bitor_gc 8 |
| 89 |
#define bitxor_gc 9 |
| 90 |
#define bitnot_gc 10 |
| 91 |
#define shiftl_gc 11 |
| 92 |
#define sshiftr_gc 12 |
| 93 |
#define ushiftr_gc 13 |
| 94 |
#define jump_gc 14 |
| 95 |
#define jz_gc 15 |
| 96 |
#define jnz_gc 16 |
| 97 |
#define jeq_gc 17 |
| 98 |
#define jne_gc 18 |
| 99 |
#define jlt_gc 19 |
| 100 |
#define jge_gc 20 |
| 101 |
#define jgt_gc 21 |
| 102 |
#define jle_gc 22 |
| 103 |
#define jltu_gc 23 |
| 104 |
#define jgeu_gc 24 |
| 105 |
#define jgtu_gc 25 |
| 106 |
#define jleu_gc 26 |
| 107 |
#define call_gc 27 |
| 108 |
#define return_gc 28 |
| 109 |
#define catch_gc 29 |
| 110 |
#define throw_gc 30 |
| 111 |
#define tailcall_gc 31 |
| 112 |
#define copy_gc 32 |
| 113 |
#define copys_gc 33 |
| 114 |
#define copyb_gc 34 |
| 115 |
#define sexs_gc 35 |
| 116 |
#define sexb_gc 36 |
| 117 |
#define aload_gc 37 |
| 118 |
#define aloads_gc 38 |
| 119 |
#define aloadb_gc 39 |
| 120 |
#define aloadbit_gc 40 |
| 121 |
#define astore_gc 41 |
| 122 |
#define astores_gc 42 |
| 123 |
#define astoreb_gc 43 |
| 124 |
#define astorebit_gc 44 |
| 125 |
#define stkcount_gc 45 |
| 126 |
#define stkpeek_gc 46 |
| 127 |
#define stkswap_gc 47 |
| 128 |
#define stkroll_gc 48 |
| 129 |
#define stkcopy_gc 49 |
| 130 |
#define streamchar_gc 50 |
| 131 |
#define streamnum_gc 51 |
| 132 |
#define streamstr_gc 52 |
| 133 |
#define gestalt_gc 53 |
| 134 |
#define debugtrap_gc 54 |
| 135 |
#define getmemsize_gc 55 |
| 136 |
#define setmemsize_gc 56 |
| 137 |
#define jumpabs_gc 57 |
| 138 |
#define random_gc 58 |
| 139 |
#define setrandom_gc 59 |
| 140 |
#define quit_gc 60 |
| 141 |
#define verify_gc 61 |
| 142 |
#define restart_gc 62 |
| 143 |
#define save_gc 63 |
| 144 |
#define restore_gc 64 |
| 145 |
#define saveundo_gc 65 |
| 146 |
#define restoreundo_gc 66 |
| 147 |
#define protect_gc 67 |
| 148 |
#define glk_gc 68 |
| 149 |
#define getstringtbl_gc 69 |
| 150 |
#define setstringtbl_gc 70 |
| 151 |
#define getiosys_gc 71 |
| 152 |
#define setiosys_gc 72 |
| 153 |
#define linearsearch_gc 73 |
| 154 |
#define binarysearch_gc 74 |
| 155 |
#define linkedsearch_gc 75 |
| 156 |
#define callf_gc 76 |
| 157 |
#define callfi_gc 77 |
| 158 |
#define callfii_gc 78 |
| 159 |
#define callfiii_gc 79 |
| 160 |
#define streamunichar_gc 80 |
| 161 |
#define mzero_gc 81 |
| 162 |
#define mcopy_gc 82 |
| 163 |
#define malloc_gc 83 |
| 164 |
#define mfree_gc 84 |
| 165 |
#define accelfunc_gc 85 |
| 166 |
#define accelparam_gc 86 |
| 167 |
#define numtof_gc 87 |
| 168 |
#define ftonumz_gc 88 |
| 169 |
#define ftonumn_gc 89 |
| 170 |
#define ceil_gc 90 |
| 171 |
#define floor_gc 91 |
| 172 |
#define fadd_gc 92 |
| 173 |
#define fsub_gc 93 |
| 174 |
#define fmul_gc 94 |
| 175 |
#define fdiv_gc 95 |
| 176 |
#define fmod_gc 96 |
| 177 |
#define sqrt_gc 97 |
| 178 |
#define exp_gc 98 |
| 179 |
#define log_gc 99 |
| 180 |
#define pow_gc 100 |
| 181 |
#define sin_gc 101 |
| 182 |
#define cos_gc 102 |
| 183 |
#define tan_gc 103 |
| 184 |
#define asin_gc 104 |
| 185 |
#define acos_gc 105 |
| 186 |
#define atan_gc 106 |
| 187 |
#define atan2_gc 107 |
| 188 |
#define jfeq_gc 108 |
| 189 |
#define jfne_gc 109 |
| 190 |
#define jflt_gc 110 |
| 191 |
#define jfle_gc 111 |
| 192 |
#define jfgt_gc 112 |
| 193 |
#define jfge_gc 113 |
| 194 |
#define jisnan_gc 114 |
| 195 |
#define jisinf_gc 115 |
| 196 |
|
| 197 |
typedef struct opcode_struct |
| 198 |
{ |
| 199 |
char *name; |
| 200 |
long code; |
| 201 |
int flags; |
| 202 |
int op_rules; |
| 203 |
int no; |
| 204 |
} opcode_t; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
#define St 1 |
| 210 |
#define Br 2 |
| 211 |
#define Rf 4 |
| 212 |
|
| 213 |
#define St2 8 |
| 214 |
|
| 215 |
|
| 216 |
#define GOP_Unicode 1 |
| 217 |
#define GOP_MemHeap 2 |
| 218 |
#define GOP_Acceleration 4 |
| 219 |
#define GOP_Float 8 |
| 220 |
|
| 221 |
#define uchar char |
| 222 |
|
| 223 |
static opcode_t opcodes_table[] = |
| 224 |
{ |
| 225 |
{ (uchar *) "nop", 0x00, 0, 0, 0 }, |
| 226 |
{ (uchar *) "add", 0x10, St, 0, 3 }, |
| 227 |
{ (uchar *) "sub", 0x11, St, 0, 3 }, |
| 228 |
{ (uchar *) "mul", 0x12, St, 0, 3 }, |
| 229 |
{ (uchar *) "div", 0x13, St, 0, 3 }, |
| 230 |
{ (uchar *) "mod", 0x14, St, 0, 3 }, |
| 231 |
{ (uchar *) "neg", 0x15, St, 0, 2 }, |
| 232 |
{ (uchar *) "bitand", 0x18, St, 0, 3 }, |
| 233 |
{ (uchar *) "bitor", 0x19, St, 0, 3 }, |
| 234 |
{ (uchar *) "bitxor", 0x1A, St, 0, 3 }, |
| 235 |
{ (uchar *) "bitnot", 0x1B, St, 0, 2 }, |
| 236 |
{ (uchar *) "shiftl", 0x1C, St, 0, 3 }, |
| 237 |
{ (uchar *) "sshiftr", 0x1D, St, 0, 3 }, |
| 238 |
{ (uchar *) "ushiftr", 0x1E, St, 0, 3 }, |
| 239 |
{ (uchar *) "jump", 0x20, Br|Rf, 0, 1 }, |
| 240 |
{ (uchar *) "jz", 0x22, Br, 0, 2 }, |
| 241 |
{ (uchar *) "jnz", 0x23, Br, 0, 2 }, |
| 242 |
{ (uchar *) "jeq", 0x24, Br, 0, 3 }, |
| 243 |
{ (uchar *) "jne", 0x25, Br, 0, 3 }, |
| 244 |
{ (uchar *) "jlt", 0x26, Br, 0, 3 }, |
| 245 |
{ (uchar *) "jge", 0x27, Br, 0, 3 }, |
| 246 |
{ (uchar *) "jgt", 0x28, Br, 0, 3 }, |
| 247 |
{ (uchar *) "jle", 0x29, Br, 0, 3 }, |
| 248 |
{ (uchar *) "jltu", 0x2A, Br, 0, 3 }, |
| 249 |
{ (uchar *) "jgeu", 0x2B, Br, 0, 3 }, |
| 250 |
{ (uchar *) "jgtu", 0x2C, Br, 0, 3 }, |
| 251 |
{ (uchar *) "jleu", 0x2D, Br, 0, 3 }, |
| 252 |
{ (uchar *) "call", 0x30, St, 0, 3 }, |
| 253 |
{ (uchar *) "return", 0x31, Rf, 0, 1 }, |
| 254 |
{ (uchar *) "catch", 0x32, Br|St, 0, 2 }, |
| 255 |
{ (uchar *) "throw", 0x33, Rf, 0, 2 }, |
| 256 |
{ (uchar *) "tailcall", 0x34, Rf, 0, 2 }, |
| 257 |
{ (uchar *) "copy", 0x40, St, 0, 2 }, |
| 258 |
{ (uchar *) "copys", 0x41, St, 0, 2 }, |
| 259 |
{ (uchar *) "copyb", 0x42, St, 0, 2 }, |
| 260 |
{ (uchar *) "sexs", 0x44, St, 0, 2 }, |
| 261 |
{ (uchar *) "sexb", 0x45, St, 0, 2 }, |
| 262 |
{ (uchar *) "aload", 0x48, St, 0, 3 }, |
| 263 |
{ (uchar *) "aloads", 0x49, St, 0, 3 }, |
| 264 |
{ (uchar *) "aloadb", 0x4A, St, 0, 3 }, |
| 265 |
{ (uchar *) "aloadbit", 0x4B, St, 0, 3 }, |
| 266 |
{ (uchar *) "astore", 0x4C, 0, 0, 3 }, |
| 267 |
{ (uchar *) "astores", 0x4D, 0, 0, 3 }, |
| 268 |
{ (uchar *) "astoreb", 0x4E, 0, 0, 3 }, |
| 269 |
{ (uchar *) "astorebit", 0x4F, 0, 0, 3 }, |
| 270 |
{ (uchar *) "stkcount", 0x50, St, 0, 1 }, |
| 271 |
{ (uchar *) "stkpeek", 0x51, St, 0, 2 }, |
| 272 |
{ (uchar *) "stkswap", 0x52, 0, 0, 0 }, |
| 273 |
{ (uchar *) "stkroll", 0x53, 0, 0, 2 }, |
| 274 |
{ (uchar *) "stkcopy", 0x54, 0, 0, 1 }, |
| 275 |
{ (uchar *) "streamchar", 0x70, 0, 0, 1 }, |
| 276 |
{ (uchar *) "streamnum", 0x71, 0, 0, 1 }, |
| 277 |
{ (uchar *) "streamstr", 0x72, 0, 0, 1 }, |
| 278 |
{ (uchar *) "gestalt", 0x0100, St, 0, 3 }, |
| 279 |
{ (uchar *) "debugtrap", 0x0101, 0, 0, 1 }, |
| 280 |
{ (uchar *) "getmemsize", 0x0102, St, 0, 1 }, |
| 281 |
{ (uchar *) "setmemsize", 0x0103, St, 0, 2 }, |
| 282 |
{ (uchar *) "jumpabs", 0x0104, Rf, 0, 1 }, |
| 283 |
{ (uchar *) "random", 0x0110, St, 0, 2 }, |
| 284 |
{ (uchar *) "setrandom", 0x0111, 0, 0, 1 }, |
| 285 |
{ (uchar *) "quit", 0x0120, Rf, 0, 0 }, |
| 286 |
{ (uchar *) "verify", 0x0121, St, 0, 1 }, |
| 287 |
{ (uchar *) "restart", 0x0122, 0, 0, 0 }, |
| 288 |
{ (uchar *) "save", 0x0123, St, 0, 2 }, |
| 289 |
{ (uchar *) "restore", 0x0124, St, 0, 2 }, |
| 290 |
{ (uchar *) "saveundo", 0x0125, St, 0, 1 }, |
| 291 |
{ (uchar *) "restoreundo", 0x0126, St, 0, 1 }, |
| 292 |
{ (uchar *) "protect", 0x0127, 0, 0, 2 }, |
| 293 |
{ (uchar *) "glk", 0x0130, St, 0, 3 }, |
| 294 |
{ (uchar *) "getstringtbl", 0x0140, St, 0, 1 }, |
| 295 |
{ (uchar *) "setstringtbl", 0x0141, 0, 0, 1 }, |
| 296 |
{ (uchar *) "getiosys", 0x0148, St|St2, 0, 2 }, |
| 297 |
{ (uchar *) "setiosys", 0x0149, 0, 0, 2 }, |
| 298 |
{ (uchar *) "linearsearch", 0x0150, St, 0, 8 }, |
| 299 |
{ (uchar *) "binarysearch", 0x0151, St, 0, 8 }, |
| 300 |
{ (uchar *) "linkedsearch", 0x0152, St, 0, 7 }, |
| 301 |
{ (uchar *) "callf", 0x0160, St, 0, 2 }, |
| 302 |
{ (uchar *) "callfi", 0x0161, St, 0, 3 }, |
| 303 |
{ (uchar *) "callfii", 0x0162, St, 0, 4 }, |
| 304 |
{ (uchar *) "callfiii", 0x0163, St, 0, 5 }, |
| 305 |
{ (uchar *) "streamunichar", 0x73, 0, GOP_Unicode, 1 }, |
| 306 |
{ (uchar *) "mzero", 0x170, 0, GOP_MemHeap, 2 }, |
| 307 |
{ (uchar *) "mcopy", 0x171, 0, GOP_MemHeap, 3 }, |
| 308 |
{ (uchar *) "malloc", 0x178, St, GOP_MemHeap, 2 }, |
| 309 |
{ (uchar *) "mfree", 0x179, 0, GOP_MemHeap, 1 }, |
| 310 |
{ (uchar *) "accelfunc", 0x180, 0, GOP_Acceleration, 2 }, |
| 311 |
{ (uchar *) "accelparam", 0x181, 0, GOP_Acceleration, 2 }, |
| 312 |
{ (uchar *) "numtof", 0x190, St, GOP_Float, 2 }, |
| 313 |
{ (uchar *) "ftonumz", 0x191, St, GOP_Float, 2 }, |
| 314 |
{ (uchar *) "ftonumn", 0x192, St, GOP_Float, 2 }, |
| 315 |
{ (uchar *) "ceil", 0x198, St, GOP_Float, 2 }, |
| 316 |
{ (uchar *) "floor", 0x199, St, GOP_Float, 2 }, |
| 317 |
{ (uchar *) "fadd", 0x1A0, St, GOP_Float, 3 }, |
| 318 |
{ (uchar *) "fsub", 0x1A1, St, GOP_Float, 3 }, |
| 319 |
{ (uchar *) "fmul", 0x1A2, St, GOP_Float, 3 }, |
| 320 |
{ (uchar *) "fdiv", 0x1A3, St, GOP_Float, 3 }, |
| 321 |
{ (uchar *) "fmod", 0x1A4, St|St2, GOP_Float, 4 }, |
| 322 |
{ (uchar *) "sqrt", 0x1A8, St, GOP_Float, 2 }, |
| 323 |
{ (uchar *) "exp", 0x1A9, St, GOP_Float, 2 }, |
| 324 |
{ (uchar *) "log", 0x1AA, St, GOP_Float, 2 }, |
| 325 |
{ (uchar *) "pow", 0x1AB, St, GOP_Float, 3 }, |
| 326 |
{ (uchar *) "sin", 0x1B0, St, GOP_Float, 2 }, |
| 327 |
{ (uchar *) "cos", 0x1B1, St, GOP_Float, 2 }, |
| 328 |
{ (uchar *) "tan", 0x1B2, St, GOP_Float, 2 }, |
| 329 |
{ (uchar *) "asin", 0x1B3, St, GOP_Float, 2 }, |
| 330 |
{ (uchar *) "acos", 0x1B4, St, GOP_Float, 2 }, |
| 331 |
{ (uchar *) "atan", 0x1B5, St, GOP_Float, 2 }, |
| 332 |
{ (uchar *) "atan2", 0x1B6, St, GOP_Float, 3 }, |
| 333 |
{ (uchar *) "jfeq", 0x1C0, Br, GOP_Float, 4 }, |
| 334 |
{ (uchar *) "jfne", 0x1C1, Br, GOP_Float, 4 }, |
| 335 |
{ (uchar *) "jflt", 0x1C2, Br, GOP_Float, 3 }, |
| 336 |
{ (uchar *) "jfle", 0x1C3, Br, GOP_Float, 3 }, |
| 337 |
{ (uchar *) "jfgt", 0x1C4, Br, GOP_Float, 3 }, |
| 338 |
{ (uchar *) "jfge", 0x1C5, Br, GOP_Float, 3 }, |
| 339 |
{ (uchar *) "jisnan", 0x1C8, Br, GOP_Float, 2 }, |
| 340 |
{ (uchar *) "jisinf", 0x1C9, Br, GOP_Float, 2 }, |
| 341 |
}; |
| 342 |
|
| 343 |
static int read_header(FILE *fl); |
| 344 |
static int findopcode(int opnum); |
| 345 |
void print_string(glui32 pos); |
| 346 |
void print_proptable(glui32 pos); |
| 347 |
void dump_ram(void); |
| 348 |
void dump_objs(void); |
| 349 |
void dump_action_table(void); |
| 350 |
void dump_dict_table(void); |
| 351 |
void dump_grammar_table(void); |
| 352 |
|
| 353 |
int dumpfuncs = FALSE; |
| 354 |
int dumpstrings = FALSE; |
| 355 |
int dumpobjs = FALSE; |
| 356 |
int dumpheader = FALSE; |
| 357 |
int dumpactiontbl = FALSE; |
| 358 |
int dumpdicttbl = FALSE; |
| 359 |
int dumpgrammartbl = FALSE; |
| 360 |
glui32 posactiontbl = 0; |
| 361 |
glui32 posdicttbl = 0; |
| 362 |
glui32 posgrammartbl = 0; |
| 363 |
unsigned char *memmap = NULL; |
| 364 |
|
| 365 |
glui32 version; |
| 366 |
glui32 ramstart; |
| 367 |
glui32 endgamefile; |
| 368 |
glui32 endmem; |
| 369 |
glui32 stacksize; |
| 370 |
glui32 startfuncaddr; |
| 371 |
glui32 stringtable; |
| 372 |
glui32 checksum; |
| 373 |
|
| 374 |
int main(int argc, char *argv[]) |
| 375 |
{ |
| 376 |
FILE *fl; |
| 377 |
int ix; |
| 378 |
glui32 val; |
| 379 |
char *filename = NULL; |
| 380 |
|
| 381 |
if (argc < 2) { |
| 382 |
printf("Usage: glulxdump file\n"); |
| 383 |
exit(1); |
| 384 |
} |
| 385 |
|
| 386 |
for (ix=1; ix<argc; ix++) { |
| 387 |
if (!strcmp(argv[ix], "-f")) |
| 388 |
dumpfuncs = TRUE; |
| 389 |
else if (!strcmp(argv[ix], "-s")) |
| 390 |
dumpstrings = TRUE; |
| 391 |
else if (!strcmp(argv[ix], "-o")) |
| 392 |
dumpobjs = TRUE; |
| 393 |
else if (!strcmp(argv[ix], "-h")) |
| 394 |
dumpheader = TRUE; |
| 395 |
else if (!strcmp(argv[ix], "-a")) { |
| 396 |
ix++; |
| 397 |
if (ix >= argc || (val = strtol(argv[ix], NULL, 16)) == 0) { |
| 398 |
printf("-a must be followed by address.\n"); |
| 399 |
exit(1); |
| 400 |
} |
| 401 |
posactiontbl = val; |
| 402 |
dumpactiontbl = TRUE; |
| 403 |
} |
| 404 |
else if (!strcmp(argv[ix], "-d")) { |
| 405 |
ix++; |
| 406 |
if (ix >= argc || (val = strtol(argv[ix], NULL, 16)) == 0) { |
| 407 |
printf("-d must be followed by address.\n"); |
| 408 |
exit(1); |
| 409 |
} |
| 410 |
posdicttbl = val; |
| 411 |
dumpdicttbl = TRUE; |
| 412 |
} |
| 413 |
else if (!strcmp(argv[ix], "-g")) { |
| 414 |
ix++; |
| 415 |
if (ix >= argc || (val = strtol(argv[ix], NULL, 16)) == 0) { |
| 416 |
printf("-g must be followed by address.\n"); |
| 417 |
exit(1); |
| 418 |
} |
| 419 |
posgrammartbl = val; |
| 420 |
dumpgrammartbl = TRUE; |
| 421 |
} |
| 422 |
else |
| 423 |
filename = argv[ix]; |
| 424 |
} |
| 425 |
|
| 426 |
if (!filename) { |
| 427 |
printf("No file given\n"); |
| 428 |
exit(1); |
| 429 |
} |
| 430 |
|
| 431 |
if (!dumpfuncs && !dumpstrings && !dumpobjs && !dumpheader |
| 432 |
&& !dumpactiontbl && !dumpdicttbl && !dumpgrammartbl) { |
| 433 |
dumpfuncs = TRUE; |
| 434 |
dumpstrings = TRUE; |
| 435 |
dumpobjs = TRUE; |
| 436 |
dumpheader = TRUE; |
| 437 |
} |
| 438 |
|
| 439 |
fl = fopen(filename, "r"); |
| 440 |
if (!fl) { |
| 441 |
perror("unable to open file"); |
| 442 |
exit(1); |
| 443 |
} |
| 444 |
|
| 445 |
ix = read_header(fl); |
| 446 |
if (!ix) { |
| 447 |
fclose(fl); |
| 448 |
exit(1); |
| 449 |
} |
| 450 |
|
| 451 |
memmap = (unsigned char *)malloc(endgamefile); |
| 452 |
rewind(fl); |
| 453 |
ix = fread(memmap, 1, endgamefile, fl); |
| 454 |
if (ix != endgamefile) { |
| 455 |
printf("File too short.\n"); |
| 456 |
fclose(fl); |
| 457 |
exit(1); |
| 458 |
} |
| 459 |
|
| 460 |
fclose(fl); |
| 461 |
fl = NULL; |
| 462 |
|
| 463 |
if (dumpheader) { |
| 464 |
printf("Version: %08lx\n", (long)version); |
| 465 |
printf("RAMSTART: %08lx\n", (long)ramstart); |
| 466 |
printf("ENDGAME: %08lx\n", (long)endgamefile); |
| 467 |
printf("ENDMEM: %08lx\n", (long)endmem); |
| 468 |
printf("STACKSIZE: %08lx\n", (long)stacksize); |
| 469 |
printf("StartFunc: %08lx\n", (long)startfuncaddr); |
| 470 |
printf("StringTbl: %08lx\n", (long)stringtable); |
| 471 |
printf("CheckSum: %08lx\n", (long)checksum); |
| 472 |
} |
| 473 |
|
| 474 |
if (dumpstrings || dumpfuncs) |
| 475 |
dump_ram(); |
| 476 |
if (dumpobjs) |
| 477 |
dump_objs(); |
| 478 |
|
| 479 |
if (dumpactiontbl) |
| 480 |
dump_action_table(); |
| 481 |
if (dumpdicttbl) |
| 482 |
dump_dict_table(); |
| 483 |
if (dumpgrammartbl) |
| 484 |
dump_grammar_table(); |
| 485 |
|
| 486 |
exit(0); |
| 487 |
} |
| 488 |
|
| 489 |
static int read_header(FILE *fl) |
| 490 |
{ |
| 491 |
unsigned char buf[4 * 9]; |
| 492 |
int res; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
res = fread(buf, 1, 4 * 9, fl); |
| 497 |
if (res != 4 * 9) { |
| 498 |
printf("This file is too short.\n"); |
| 499 |
return FALSE; |
| 500 |
} |
| 501 |
|
| 502 |
if (buf[0] != 'G' || buf[1] != 'l' || buf[2] != 'u' || buf[3] != 'l') { |
| 503 |
printf("This does not appear to be a Glulx file.\n"); |
| 504 |
return FALSE; |
| 505 |
} |
| 506 |
|
| 507 |
version = Read4(buf+4); |
| 508 |
ramstart = Read4(buf+8); |
| 509 |
endgamefile = Read4(buf+12); |
| 510 |
endmem = Read4(buf+16); |
| 511 |
stacksize = Read4(buf+20); |
| 512 |
startfuncaddr = Read4(buf+24); |
| 513 |
stringtable = Read4(buf+28); |
| 514 |
checksum = Read4(buf+32); |
| 515 |
|
| 516 |
return TRUE; |
| 517 |
} |
| 518 |
|
| 519 |
void dump_ram() |
| 520 |
{ |
| 521 |
glui32 pos, startpos; |
| 522 |
unsigned char ch; |
| 523 |
int jx; |
| 524 |
|
| 525 |
pos = 4 * (9+5); |
| 526 |
|
| 527 |
while (pos < ramstart) { |
| 528 |
startpos = pos; |
| 529 |
ch = Mem1(pos); |
| 530 |
|
| 531 |
if (ch == 0) { |
| 532 |
|
| 533 |
pos++; |
| 534 |
} |
| 535 |
else if (ch == 0xE0) { |
| 536 |
if (dumpstrings) { |
| 537 |
printf("String (%08lx): ", (long)startpos); |
| 538 |
pos++; |
| 539 |
while (1) { |
| 540 |
ch = Mem1(pos); |
| 541 |
pos++; |
| 542 |
if (ch == '\0') |
| 543 |
break; |
| 544 |
putchar(ch); |
| 545 |
} |
| 546 |
printf("\n"); |
| 547 |
} |
| 548 |
else { |
| 549 |
while (1) { |
| 550 |
ch = Mem1(pos); |
| 551 |
pos++; |
| 552 |
if (ch == '\0') |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
else if (ch == 0xC0 || ch == 0xC1) { |
| 558 |
int loctype, locnum; |
| 559 |
int opcode; |
| 560 |
opcode_t *opco; |
| 561 |
glui32 exstart; |
| 562 |
int opmodes[16]; |
| 563 |
|
| 564 |
printf("Function (%08lx), ", (long)startpos); |
| 565 |
if (ch == 0xC0) |
| 566 |
printf("stack-called:"); |
| 567 |
else |
| 568 |
printf("locals-called:"); |
| 569 |
|
| 570 |
pos++; |
| 571 |
|
| 572 |
while (1) { |
| 573 |
loctype = Mem1(pos); |
| 574 |
pos++; |
| 575 |
locnum = Mem1(pos); |
| 576 |
pos++; |
| 577 |
if (loctype == 0) |
| 578 |
break; |
| 579 |
printf(" %d %d-byte local%s,", locnum, loctype, |
| 580 |
((locnum == 1) ? "" : "s")); |
| 581 |
} |
| 582 |
printf("\n"); |
| 583 |
|
| 584 |
ch = Mem1(pos); |
| 585 |
pos++; |
| 586 |
exstart = pos; |
| 587 |
|
| 588 |
while (ch != 0xC0 && ch != 0xC1 && ch != 0xE0) { |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
if ((ch & 0x80) == 0) { |
| 593 |
opcode = ch; |
| 594 |
} |
| 595 |
else if ((ch & 0x40) == 0) { |
| 596 |
opcode = (ch & 0x7F); |
| 597 |
ch = Mem1(pos); pos++; |
| 598 |
opcode = (opcode << 8) | ch; |
| 599 |
} |
| 600 |
else { |
| 601 |
opcode = (ch & 0x3F); |
| 602 |
ch = Mem1(pos); pos++; |
| 603 |
opcode = (opcode << 8) | ch; |
| 604 |
ch = Mem1(pos); pos++; |
| 605 |
opcode = (opcode << 8) | ch; |
| 606 |
ch = Mem1(pos); pos++; |
| 607 |
opcode = (opcode << 8) | ch; |
| 608 |
} |
| 609 |
opco = &opcodes_table[findopcode(opcode)]; |
| 610 |
printf(" %08lx: %12s ", (long)(pos-exstart), opco->name); |
| 611 |
|
| 612 |
for (jx=0; jx<opco->no; jx+=2) { |
| 613 |
ch = Mem1(pos); pos++; |
| 614 |
opmodes[jx+0] = (ch & 0x0F); |
| 615 |
opmodes[jx+1] = ((ch >> 4) & 0x0F); |
| 616 |
} |
| 617 |
|
| 618 |
for (jx=0; jx<opco->no; jx++) { |
| 619 |
int val = 0; |
| 620 |
printf(" "); |
| 621 |
switch (opmodes[jx]) { |
| 622 |
case 0: |
| 623 |
printf("zero"); |
| 624 |
break; |
| 625 |
case 8: |
| 626 |
printf("stackptr"); |
| 627 |
break; |
| 628 |
case 1: |
| 629 |
case 5: |
| 630 |
case 9: |
| 631 |
case 13: |
| 632 |
if ((opmodes[jx] & 0x0C) == 4) |
| 633 |
printf("*"); |
| 634 |
else if ((opmodes[jx] & 0x0C) == 8) |
| 635 |
printf("Fr:"); |
| 636 |
else if ((opmodes[jx] & 0x0C) == 12) |
| 637 |
printf("*R:"); |
| 638 |
ch = Mem1(pos); pos++; |
| 639 |
val = ch; |
| 640 |
if (val & 0x80) |
| 641 |
val |= 0xFFFFFF00; |
| 642 |
printf("%02x", val & 0xFF); |
| 643 |
break; |
| 644 |
case 2: |
| 645 |
case 6: |
| 646 |
case 10: |
| 647 |
case 14: |
| 648 |
if ((opmodes[jx] & 0x0C) == 4) |
| 649 |
printf("*"); |
| 650 |
else if ((opmodes[jx] & 0x0C) == 8) |
| 651 |
printf("Fr:"); |
| 652 |
else if ((opmodes[jx] & 0x0C) == 12) |
| 653 |
printf("*R:"); |
| 654 |
ch = Mem1(pos); pos++; |
| 655 |
val = ch; |
| 656 |
if (val & 0x80) |
| 657 |
val |= 0xFFFFFF00; |
| 658 |
ch = Mem1(pos); pos++; |
| 659 |
val = (val << 8) | ch; |
| 660 |
printf("%04x", val & 0xFFFF); |
| 661 |
break; |
| 662 |
case 3: |
| 663 |
case 7: |
| 664 |
case 11: |
| 665 |
case 15: |
| 666 |
if ((opmodes[jx] & 0x0C) == 4) |
| 667 |
printf("*"); |
| 668 |
else if ((opmodes[jx] & 0x0C) == 8) |
| 669 |
printf("Fr:"); |
| 670 |
else if ((opmodes[jx] & 0x0C) == 12) |
| 671 |
printf("*R:"); |
| 672 |
ch = Mem1(pos); pos++; |
| 673 |
val = ch; |
| 674 |
ch = Mem1(pos); pos++; |
| 675 |
val = (val << 8) | ch; |
| 676 |
ch = Mem1(pos); pos++; |
| 677 |
val = (val << 8) | ch; |
| 678 |
ch = Mem1(pos); pos++; |
| 679 |
val = (val << 8) | ch; |
| 680 |
printf("%08x", val); |
| 681 |
break; |
| 682 |
} |
| 683 |
if ((opco->flags & Br) && (jx == opco->no-1)) { |
| 684 |
if (val == 0) { |
| 685 |
printf(" (rfalse)"); |
| 686 |
} |
| 687 |
else if (val == 1) { |
| 688 |
printf(" (rtrue)"); |
| 689 |
} |
| 690 |
else { |
| 691 |
printf(" (%08lx)", (long)((pos-exstart)+val-2+1)); |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
printf("\n"); |
| 696 |
|
| 697 |
ch = Mem1(pos); pos++; |
| 698 |
} |
| 699 |
|
| 700 |
pos--; |
| 701 |
} |
| 702 |
else { |
| 703 |
printf("Unknown thing.\n"); |
| 704 |
pos++; |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
static int findopcode(int opnum) |
| 710 |
{ |
| 711 |
switch (opnum) { |
| 712 |
case op_nop: return nop_gc; |
| 713 |
case op_add: return add_gc; |
| 714 |
case op_sub: return sub_gc; |
| 715 |
case op_mul: return mul_gc; |
| 716 |
case op_div: return div_gc; |
| 717 |
case op_mod: return mod_gc; |
| 718 |
case op_neg: return neg_gc; |
| 719 |
case op_bitand: return bitand_gc; |
| 720 |
case op_bitor: return bitor_gc; |
| 721 |
case op_bitxor: return bitxor_gc; |
| 722 |
case op_bitnot: return bitnot_gc; |
| 723 |
case op_shiftl: return shiftl_gc; |
| 724 |
case op_sshiftr: return sshiftr_gc; |
| 725 |
case op_ushiftr: return ushiftr_gc; |
| 726 |
case op_jump: return jump_gc; |
| 727 |
case op_jz: return jz_gc; |
| 728 |
case op_jnz: return jnz_gc; |
| 729 |
case op_jeq: return jeq_gc; |
| 730 |
case op_jne: return jne_gc; |
| 731 |
case op_jlt: return jlt_gc; |
| 732 |
case op_jge: return jge_gc; |
| 733 |
case op_jgt: return jgt_gc; |
| 734 |
case op_jle: return jle_gc; |
| 735 |
case op_call: return call_gc; |
| 736 |
case op_return: return return_gc; |
| 737 |
case op_catch: return catch_gc; |
| 738 |
case op_throw: return throw_gc; |
| 739 |
case op_copy: return copy_gc; |
| 740 |
case op_copys: return copys_gc; |
| 741 |
case op_copyb: return copyb_gc; |
| 742 |
case op_sexs: return sexs_gc; |
| 743 |
case op_sexb: return sexb_gc; |
| 744 |
case op_aload: return aload_gc; |
| 745 |
case op_aloads: return aloads_gc; |
| 746 |
case op_aloadb: return aloadb_gc; |
| 747 |
case op_aloadbit: return aloadbit_gc; |
| 748 |
case op_astore: return astore_gc; |
| 749 |
case op_astores: return astores_gc; |
| 750 |
case op_astoreb: return astoreb_gc; |
| 751 |
case op_astorebit: return astorebit_gc; |
| 752 |
case op_stkcount: return stkcount_gc; |
| 753 |
case op_stkpeek: return stkpeek_gc; |
| 754 |
case op_stkswap: return stkswap_gc; |
| 755 |
case op_stkroll: return stkroll_gc; |
| 756 |
case op_stkcopy: return stkcopy_gc; |
| 757 |
case op_streamchar: return streamchar_gc; |
| 758 |
case op_streamunichar: return streamunichar_gc; |
| 759 |
case op_streamnum: return streamnum_gc; |
| 760 |
case op_streamstr: return streamstr_gc; |
| 761 |
case op_gestalt: return gestalt_gc; |
| 762 |
case op_random: return random_gc; |
| 763 |
case op_setrandom: return setrandom_gc; |
| 764 |
case op_quit: return quit_gc; |
| 765 |
case op_verify: return verify_gc; |
| 766 |
case op_restart: return restart_gc; |
| 767 |
case op_save: return save_gc; |
| 768 |
case op_restore: return restore_gc; |
| 769 |
case op_saveundo: return saveundo_gc; |
| 770 |
case op_restoreundo: return restoreundo_gc; |
| 771 |
case op_protect: return protect_gc; |
| 772 |
case op_glk: return glk_gc; |
| 773 |
default: |
| 774 |
printf("Unknown opcode %02x\n", opnum); |
| 775 |
return nop_gc; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
void dump_objs() |
| 780 |
{ |
| 781 |
glui32 startpos, pos, nextstartpos, proptablepos; |
| 782 |
unsigned char ch; |
| 783 |
int ix, jx; |
| 784 |
|
| 785 |
startpos = ramstart; |
| 786 |
while (startpos) { |
| 787 |
|
| 788 |
ch = Mem1(startpos); |
| 789 |
if (ch != 0x70) { |
| 790 |
printf("Non-object in object list (%08lx)\n", (long)startpos); |
| 791 |
return; |
| 792 |
} |
| 793 |
pos = startpos+1; |
| 794 |
|
| 795 |
printf("Object (%08lx):\n", (long)startpos); |
| 796 |
printf(" attrs:"); |
| 797 |
for (ix=0; ix<7; ix++) { |
| 798 |
ch = Mem1(pos); pos++; |
| 799 |
printf(" "); |
| 800 |
for (jx=0; jx<8; jx++) { |
| 801 |
printf("%c", (ch&1) ? '1' : '0'); |
| 802 |
ch >>= 1; |
| 803 |
} |
| 804 |
} |
| 805 |
printf("\n"); |
| 806 |
|
| 807 |
for (ix=0; ix<6; ix++) { |
| 808 |
static char *labellist[6] = { |
| 809 |
"next", "name", "props", "parent", "sibling", "child" |
| 810 |
}; |
| 811 |
long val; |
| 812 |
ch = Mem1(pos); pos++; |
| 813 |
val = ch; |
| 814 |
ch = Mem1(pos); pos++; |
| 815 |
val = (val << 8) | ch; |
| 816 |
ch = Mem1(pos); pos++; |
| 817 |
val = (val << 8) | ch; |
| 818 |
ch = Mem1(pos); pos++; |
| 819 |
val = (val << 8) | ch; |
| 820 |
printf(" %7s: ", labellist[ix]); |
| 821 |
printf("%08lx", val); |
| 822 |
switch (ix) { |
| 823 |
case 0: |
| 824 |
nextstartpos = val; |
| 825 |
break; |
| 826 |
case 1: |
| 827 |
printf(" "); |
| 828 |
print_string(val); |
| 829 |
break; |
| 830 |
case 2: |
| 831 |
proptablepos = val; |
| 832 |
break; |
| 833 |
} |
| 834 |
printf("\n"); |
| 835 |
} |
| 836 |
|
| 837 |
print_proptable(proptablepos); |
| 838 |
|
| 839 |
startpos = nextstartpos; |
| 840 |
printf("\n"); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
void print_string(glui32 pos) |
| 845 |
{ |
| 846 |
unsigned char ch; |
| 847 |
ch = Mem1(pos); |
| 848 |
if (ch != 0xE0) { |
| 849 |
printf("<nonstring %08lx>", (long)pos); |
| 850 |
return; |
| 851 |
} |
| 852 |
pos++; |
| 853 |
while (1) { |
| 854 |
ch = Mem1(pos); |
| 855 |
pos++; |
| 856 |
if (ch == '\0') |
| 857 |
return; |
| 858 |
putchar(ch); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
void print_proptable(glui32 pos) |
| 863 |
{ |
| 864 |
int ix, jx; |
| 865 |
int numprops; |
| 866 |
|
| 867 |
numprops = Mem4(pos); |
| 868 |
pos += 4; |
| 869 |
|
| 870 |
printf("%d properties:\n", numprops); |
| 871 |
|
| 872 |
for (ix=0; ix<numprops; ix++) { |
| 873 |
int propnum, proplen, propflags; |
| 874 |
glui32 addr; |
| 875 |
propnum = Mem2(pos); |
| 876 |
pos += 2; |
| 877 |
proplen = Mem2(pos); |
| 878 |
pos += 2; |
| 879 |
addr = Mem4(pos); |
| 880 |
pos += 4; |
| 881 |
propflags = Mem2(pos); |
| 882 |
pos += 2; |
| 883 |
printf(" num=%d, len=%d, flags=%d, addr=%08lx\n", |
| 884 |
propnum, proplen, propflags, (long)addr); |
| 885 |
printf(" :"); |
| 886 |
for (jx=0; jx<proplen; jx++) { |
| 887 |
glsi32 val = Mem4(addr); |
| 888 |
addr += 4; |
| 889 |
printf(" %lx", (long)val); |
| 890 |
} |
| 891 |
printf("\n"); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
void dump_action_table() |
| 896 |
{ |
| 897 |
glui32 lx, len, val; |
| 898 |
|
| 899 |
len = Mem4(posactiontbl); |
| 900 |
|
| 901 |
printf("Action table at %08lx: %ld entries\n", (long)posactiontbl, (long)len); |
| 902 |
for (lx=0; lx<len; lx++) { |
| 903 |
val = Mem4(posactiontbl + 4 + lx*4); |
| 904 |
printf("%ld: %08lx\n", (long)lx, (long)val); |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
void dump_dict_table() |
| 909 |
{ |
| 910 |
glui32 lx, len, val, addr; |
| 911 |
char ch; |
| 912 |
int ix; |
| 913 |
|
| 914 |
len = Mem4(posdicttbl); |
| 915 |
|
| 916 |
printf("Dictionary table at %08lx: %ld entries\n", (long)posdicttbl, (long)len); |
| 917 |
for (lx=0; lx<len; lx++) { |
| 918 |
addr = posdicttbl + 4 + lx*16; |
| 919 |
printf("%08lx: ", (long)addr); |
| 920 |
for (ix=0; ix<9; ix++) { |
| 921 |
ch = Mem1(addr+1+ix); |
| 922 |
if (ch == '\0') |
| 923 |
ch = ' '; |
| 924 |
printf("%c", ch); |
| 925 |
} |
| 926 |
printf(" : "); |
| 927 |
val = Mem2(addr+10); |
| 928 |
printf("flags=%04lx", (long)val); |
| 929 |
val = Mem2(addr+12); |
| 930 |
printf(", verbnum=%04lx", (long)val); |
| 931 |
val = Mem2(addr+14); |
| 932 |
printf(", filler=%lx", (long)val); |
| 933 |
printf("\n"); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
void dump_grammar_table() |
| 938 |
{ |
| 939 |
glui32 lx, len, val, addr; |
| 940 |
int numlines; |
| 941 |
int jx; |
| 942 |
|
| 943 |
len = Mem4(posgrammartbl); |
| 944 |
|
| 945 |
printf("Grammar table at %08lx: %ld entries\n", (long)posgrammartbl, (long)len); |
| 946 |
|
| 947 |
for (lx=0; lx<len; lx++) { |
| 948 |
addr = posgrammartbl + 4 + lx*4; |
| 949 |
addr = Mem4(addr); |
| 950 |
printf("%03ld: %08lx: ", (long)lx, (long)addr); |
| 951 |
numlines = Mem1(addr); |
| 952 |
addr++; |
| 953 |
printf("%d lines:\n", numlines); |
| 954 |
for (jx=0; jx<numlines; jx++) { |
| 955 |
glui32 action, toktype, tokdata; |
| 956 |
action = Mem2(addr); |
| 957 |
addr += 2; |
| 958 |
val = Mem1(addr); |
| 959 |
addr++; |
| 960 |
printf(" ac %04lx; fl %02lx :", (long)action, (long)val); |
| 961 |
while (1) { |
| 962 |
toktype = Mem1(addr); |
| 963 |
addr++; |
| 964 |
if (toktype == 15) { |
| 965 |
printf(" .\n"); |
| 966 |
break; |
| 967 |
} |
| 968 |
tokdata = Mem4(addr); |
| 969 |
addr += 4; |
| 970 |
printf(" %02lx(%04lx)", (long)toktype, (long)tokdata); |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
} |