DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
runscript.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 15
4
5#include "runscript.hpp"
6
7#include <cmath>
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11
12#include "battle_globals.hpp"
13
14void runerror(const char *message) {
15 fprintf(stderr, "RUNTIME ERROR: %s\n", message);
16 exit__2(-1);
17}
18
19void stkoverflow() {
20 runerror("stack overflow");
21}
22
23int chk_int(RS_STACKDATA data, funcdata *function) {
24 if (data.type == RS_INT) {
25 return data.i;
26 }
27
28 fprintf(stderr, "RUNTIME ERROR: %s: operand is not integer\n", function->name);
29 exit__2(-1);
30 return 0;
31}
32
33int is_true(RS_STACKDATA data) {
34 return !(data.type == RS_INT && data.i == 0);
35}
36
37void divby0error() {
38 runerror("Divide by 0");
39}
40
41void modby0error() {
42 runerror("Modulo by 0");
43}
44
45void print(RS_STACKDATA *data, int count) {
46 for (int index = 0; index < count; index++, data++) {
47 if (data->type == RS_INT) {
48 printf("%d", data->i);
49 } else if (data->type == RS_STR) {
50 printf("%s", data->s);
51 } else if (data->type == RS_FLOAT) {
52 printf("%f", data->f);
53 }
54
55 fflush(stdout);
56 }
57}
58
60 sp = stack;
62 call_sp = call;
63 call_end = call;
64 pc = 0;
65 ext_func_num = 0;
66 stack_num = 0;
67 call_num = 0;
68 skip_wait = 0;
69}
70
72 if (sp >= stack_end) {
73 stkoverflow();
74 }
75}
76
79 *sp++ = value;
80}
81
82void CRunScript::push_int(int value) {
84 sp->type = RS_INT;
85 sp++->i = value;
86}
87
88void CRunScript::push_str(char *string) {
90 sp->type = RS_STR;
91 sp++->s = string;
92}
93
96 sp->type = RS_PTR;
97 sp++->p = pointer;
98}
99
100void CRunScript::push_float(float value) {
101 check_stack();
102 sp->type = RS_FLOAT;
103 sp++->f = value;
104}
105
107 return *--sp;
108}
109
110/* A call takes its arguments from the operand stack in place: the new frame starts where the
111 caller left them, so nothing is copied and only the locals past them have to be cleared. */
113 if (call_sp >= call_end) {
114 printf("\202\261\202\352\210\310\217\343\212\326\220\224\214\304\202\321\217\157\202\265"
115 "\202\252\202\305\202\253\202\334\202\271\202\361\201\102\n");
116 exit__2(-2);
117 }
118
119 call_sp->ret = return_pc;
120 call_sp->func = func;
122 frame = sp - callee->arg;
123 sp = frame + callee->local;
124 func = callee;
125 call_sp++;
126 memset(frame + func->arg, 0, (func->local - func->arg) * sizeof(RS_STACKDATA));
127 check_stack();
128 return (vmcode_t *) (code + (int) callee->addr);
129}
130
132 call_sp--;
134 func = call_sp->func;
135 return call_sp->ret;
136}
137
138void CRunScript::ext(RS_STACKDATA *args, int argc) {
139 if (args->i < 0 || args->i >= ext_func_num) {
140 printf("not found ext %d\n", args->i);
141 } else if (ext_func_table[args->i] == 0) {
142 printf("not found ext %d\n", args->i);
143 } else if ((*ext_func_table[args->i])(args + 1, argc - 1) == 0) {
144 printf("illegal function call ext %d\n", args->i);
145 }
146}
147
148void CRunScript::load(RS_PROG_HEADER *prog, RS_STACKDATA *stack, int stack_num, RS_CALLDATA *call, int call_num) {
149 this->stack = stack;
150 this->stack_num = stack_num;
151 this->call = call;
152 this->call_num = call_num;
153 this->stack_end = this->stack + stack_num;
154 this->call_end = this->call + call_num;
155 this->prog = prog;
156 this->code = (char *) prog + prog->code;
157}
158
160 this->prog = prog;
161 code = (char *) prog + prog->code;
162}
163
164void CRunScript::ext_func(int (**table)(RS_STACKDATA *, int), int count) {
165 ext_func_table = table;
166 ext_func_num = count;
167}
168
170 if (pc) {
171 exe(pc);
172 }
173}
174
175int CRunScript::run(int no) {
176 RS_PROGDATA *entry;
177 int i;
178 RS_PROG_HEADER *header;
179 vmcode_t *start;
180
181 if (prog == 0) {
182 return -1;
183 }
184
185 sp = stack;
186 call_sp = call;
187 func = 0;
188
189 if (no < 0) {
190 func = (funcdata *) ((char *) prog + prog->main);
191 } else {
192 header = prog;
193 entry = (RS_PROGDATA *) ((char *) header + header->prog);
194
195 for (i = 0; i < header->prog_num; i++, entry++) {
196 if (entry->no == no) {
197 func = (funcdata *) ((char *) header + entry->func);
198 break;
199 }
200 }
201 }
202
203 if (func == 0) {
204 printf("not found program %d\n", no);
205 return -1;
206 }
207
208 frame = sp - func->arg;
209 sp = frame + func->local;
210 check_stack();
211 memset(frame + func->arg, 0, (func->local - func->arg) * sizeof(RS_STACKDATA));
212 start = (vmcode_t *) (code + (int) func->addr);
213 end = 0;
214 skip_wait = 0;
215 exe(start);
216 return end != 0 ? 0 : 1;
217}
218
220 RS_PROGDATA *entry;
221 int i;
222 RS_PROG_HEADER *header;
223
224 header = prog;
225 entry = (RS_PROGDATA *) ((char *) header + header->prog);
226
227 for (i = 0; i < header->prog_num; i++, entry++) {
228 if (entry->no == no) {
229 return 1;
230 }
231 }
232
233 return 0;
234}
235
237 skip_wait = 1;
238 resume();
239}
240
241/* The loop runs on pc rather than on the argument, because three of the opcodes leave the
242 machine suspended and the only thing that makes a later resume possible is the program counter
243 being in the object when they return. Every case that does not redirect control falls out of
244 the switch to the step at the bottom, so an opcode that jumps writes pc and continues. */
246 RS_STACKDATA value;
247 RS_STACKDATA rhs;
248 RS_STACKDATA lhs;
249 float rhs_f;
250 float lhs_f;
251 int rhs_i;
252 int lhs_i;
253
254 pc = entry;
255
256 for (;;) {
257 switch (pc->op) {
258 case RS_OP_LOAD:
259 switch (pc->arg2) {
260 case RS_ADDR_LOCAL:
261 push(*(frame + pc->arg1));
262 break;
263 case RS_ADDR_LOCAL_INDEX:
264 push(*(frame + pc->arg1 + chk_int(pop(), func)));
265 break;
266 case RS_ADDR_POINTER_INDEX:
267 push(*(frame[pc->arg1].p + chk_int(pop(), func)));
268 break;
269 case RS_ADDR_LOCAL_FLOAT:
270 (frame + pc->arg1)->type = RS_FLOAT;
271 push(*(frame + pc->arg1));
272 break;
273 case RS_ADDR_LOCAL_INDEX_FLOAT:
274 rhs_i = chk_int(pop(), func);
275 (frame + pc->arg1 + rhs_i)->type = RS_FLOAT;
276 push(*(frame + pc->arg1 + rhs_i));
277 break;
278 case RS_ADDR_POINTER_INDEX_FLOAT:
279 rhs_i = chk_int(pop(), func);
280 (frame[pc->arg1].p + rhs_i)->type = RS_FLOAT;
281 push(*(frame[pc->arg1].p + rhs_i));
282 break;
283 }
284
285 break;
286 case RS_OP_LOAD_ADDR:
287 switch (pc->arg2) {
288 case RS_ADDR_LOCAL:
289 push_ptr(frame + pc->arg1);
290 break;
291 case RS_ADDR_LOCAL_INDEX:
292 push_ptr(frame + pc->arg1 + chk_int(pop(), func));
293 break;
294 case RS_ADDR_POINTER_INDEX:
295 push_ptr(frame[pc->arg1].p + chk_int(pop(), func));
296 break;
297 case RS_ADDR_LOCAL_FLOAT:
298 push_ptr(frame + pc->arg1);
299 break;
300 case RS_ADDR_LOCAL_INDEX_FLOAT:
301 push_ptr(frame + pc->arg1 + chk_int(pop(), func));
302 break;
303 case RS_ADDR_POINTER_INDEX_FLOAT:
304 push_ptr(frame[pc->arg1].p + chk_int(pop(), func));
305 break;
306 }
307
308 break;
309 case RS_OP_STORE:
310 value = pop();
311 *pop().p = value;
312 push(value);
313 break;
314 case RS_OP_PUSH_CONST:
315 if (pc->arg1 == 1) {
316 push_int(pc->arg2);
317 } else if (pc->arg1 == 3) {
318 push_str(code + (int) pc->arg2);
319 } else if (pc->arg1 == 2) {
320 push_float(*(float *) &pc->arg2);
321 }
322
323 break;
324 case RS_OP_POP:
325 sp--;
326 break;
327 case RS_OP_JMP:
328 if (!skip_wait) {
329 pc = (vmcode_t *) (code + (int) pc->arg1);
330 continue;
331 }
332
333 break;
334 case RS_OP_JMP_TRUE:
335 if (!skip_wait) {
336 if (is_true(pop())) {
337 if (pc->arg2) {
338 push_int(1);
339 }
340
341 pc = (vmcode_t *) (code + (int) pc->arg1);
342 continue;
343 }
344 }
345
346 break;
347 case RS_OP_JMP_FALSE:
348 if (!skip_wait) {
349 if (!is_true(pop())) {
350 if (pc->arg2) {
351 push_int(0);
352 }
353
354 pc = (vmcode_t *) (code + (int) pc->arg1);
355 continue;
356 }
357 }
358
359 break;
360 case RS_OP_CMP:
361 rhs = pop();
362 lhs = pop();
363
364 if (lhs.type == RS_INT && rhs.type == RS_INT) {
365 rhs_i = rhs.i;
366 lhs_i = lhs.i;
367
368 switch (pc->arg1) {
369 case RS_CMP_EQ:
370 push_int(rhs_i == lhs_i);
371 break;
372 case RS_CMP_NE:
373 push_int(rhs_i != lhs_i);
374 break;
375 case RS_CMP_LT:
376 push_int(lhs_i < rhs_i);
377 break;
378 case RS_CMP_LE:
379 push_int(lhs_i <= rhs_i);
380 break;
381 case RS_CMP_GT:
382 push_int(lhs_i > rhs_i);
383 break;
384 case RS_CMP_GE:
385 push_int(lhs_i >= rhs_i);
386 break;
387 }
388 } else {
389 if (lhs.type == RS_FLOAT && rhs.type == RS_FLOAT) {
390 rhs_f = rhs.f;
391 lhs_f = lhs.f;
392 } else if (lhs.type == RS_INT && rhs.type == RS_FLOAT) {
393 rhs_f = rhs.f;
394 lhs_f = lhs.i;
395 } else if (lhs.type == RS_FLOAT && rhs.type == RS_INT) {
396 rhs_f = rhs.i;
397 lhs_f = lhs.f;
398 } else {
399 fprintf(stderr, "RUNTIME ERROR at _CMP: %s: operand is not number\n", func->name);
400 exit__2(-1);
401 }
402
403 switch (pc->arg1) {
404 case RS_CMP_EQ:
405 push_int(rhs_f == lhs_f);
406 break;
407 case RS_CMP_NE:
408 push_int(rhs_f != lhs_f);
409 break;
410 case RS_CMP_LT:
411 push_int(lhs_f < rhs_f);
412 break;
413 case RS_CMP_LE:
414 push_int(lhs_f <= rhs_f);
415 break;
416 case RS_CMP_GT:
417 push_int(lhs_f > rhs_f);
418 break;
419 case RS_CMP_GE:
420 push_int(lhs_f >= rhs_f);
421 break;
422 }
423 }
424
425 break;
426 case RS_OP_ADD:
427 rhs = pop();
428 lhs = pop();
429
430 if (lhs.type == RS_INT && rhs.type == RS_INT) {
431 push_int(lhs.i + rhs.i);
432 } else if (lhs.type == RS_FLOAT && rhs.type == RS_FLOAT) {
433 push_float(lhs.f + rhs.f);
434 } else if (lhs.type == RS_INT && rhs.type == RS_FLOAT) {
435 push_float(lhs.i + rhs.f);
436 } else if (lhs.type == RS_FLOAT && rhs.type == RS_INT) {
437 push_float(lhs.f + rhs.i);
438 } else {
439 fprintf(stderr, "RUNTIME ERROR at _ADD: %s: operand is not number\n", func->name);
440 exit__2(-1);
441 }
442
443 break;
444 case RS_OP_SUB:
445 rhs = pop();
446 lhs = pop();
447
448 if (lhs.type == RS_INT && rhs.type == RS_INT) {
449 push_int(lhs.i - rhs.i);
450 } else if (lhs.type == RS_FLOAT && rhs.type == RS_FLOAT) {
451 push_float(lhs.f - rhs.f);
452 } else if (lhs.type == RS_INT && rhs.type == RS_FLOAT) {
453 push_float(lhs.i - rhs.f);
454 } else if (lhs.type == RS_FLOAT && rhs.type == RS_INT) {
455 push_float(lhs.f - rhs.i);
456 } else {
457 fprintf(stderr, "RUNTIME ERROR at _SUB: %s: operand is not number\n", func->name);
458 exit__2(-1);
459 }
460
461 break;
462 case RS_OP_MUL:
463 rhs = pop();
464 lhs = pop();
465
466 if (lhs.type == RS_INT && rhs.type == RS_INT) {
467 push_int(lhs.i * rhs.i);
468 } else if (lhs.type == RS_FLOAT && rhs.type == RS_FLOAT) {
469 push_float(lhs.f * rhs.f);
470 } else if (lhs.type == RS_INT && rhs.type == RS_FLOAT) {
471 push_float(lhs.i * rhs.f);
472 } else if (lhs.type == RS_FLOAT && rhs.type == RS_INT) {
473 push_float(lhs.f * rhs.i);
474 } else {
475 fprintf(stderr, "RUNTIME ERROR _MUL: %s: operand is not number\n", func->name);
476 exit__2(-1);
477 }
478
479 break;
480 case RS_OP_DIV:
481 rhs = pop();
482
483 if (rhs.i == 0) {
484 divby0error();
485 }
486
487 lhs = pop();
488
489 if (lhs.type == RS_INT && rhs.type == RS_INT) {
490 push_int(lhs.i / rhs.i);
491 } else if (lhs.type == RS_FLOAT && rhs.type == RS_FLOAT) {
492 push_float(lhs.f / rhs.f);
493 } else if (lhs.type == RS_INT && rhs.type == RS_FLOAT) {
494 push_float(lhs.i / rhs.f);
495 } else if (lhs.type == RS_FLOAT && rhs.type == RS_INT) {
496 push_float(lhs.f / rhs.i);
497 } else {
498 fprintf(stderr, "RUNTIME ERROR at _DIV: %s: operand is not number\n", func->name);
499 exit__2(-1);
500 }
501
502 break;
503 case RS_OP_MOD:
504 rhs_i = chk_int(pop(), func);
505
506 if (rhs_i == 0) {
507 modby0error();
508 }
509
510 push_int(chk_int(pop(), func) % rhs_i);
511 break;
512 case RS_OP_AND:
513 rhs_i = chk_int(pop(), func);
514 push_int(rhs_i & chk_int(pop(), func));
515 break;
516 case RS_OP_OR:
517 rhs_i = chk_int(pop(), func);
518 push_int(rhs_i | chk_int(pop(), func));
519 break;
520 case RS_OP_NEG:
521 rhs = pop();
522
523 if (rhs.type == RS_INT) {
524 push_int(-rhs.i);
525 } else if (rhs.type == RS_FLOAT) {
526 push_float(-rhs.f);
527 } else {
528 fprintf(stderr, "RUNTIME ERROR at _INVT: %s: operand is not number\n", func->name);
529 exit__2(-1);
530 }
531
532 break;
533 case RS_OP_SIN:
534 rhs = pop();
535
536 if (rhs.type == RS_INT) {
537 push_float(sinf(rhs.i));
538 } else if (rhs.type == RS_FLOAT) {
539 push_float(sinf(rhs.f));
540 } else {
541 fprintf(stderr, "RUNTIME ERROR at _SIN: %s: operand is not number\n", func->name);
542 exit__2(-1);
543 }
544
545 break;
546 case RS_OP_COS:
547 rhs = pop();
548
549 if (rhs.type == RS_INT) {
550 push_float(cosf(rhs.i));
551 } else if (rhs.type == RS_FLOAT) {
552 push_float(cosf(rhs.f));
553 } else {
554 fprintf(stderr, "RUNTIME ERROR at _COS: %s: operand is not number\n", func->name);
555 exit__2(-1);
556 }
557
558 break;
559 case RS_OP_NOT:
560 rhs = pop();
561
562 if (rhs.type == RS_INT) {
563 push_int(!rhs.i);
564 } else {
565 fprintf(stderr, "RUNTIME ERROR: %s: \220\256\220\224\202\305\202\310\202\242\203\111"
566 "\203\171\203\211\203\223\203\150\n",
567 func->name);
568 exit__2(-1);
569 }
570
571 break;
572 case RS_OP_ITOF:
573 rhs = pop();
574
575 if (rhs.type == RS_INT) {
576 push_float(rhs.i);
577 } else if (rhs.type == RS_FLOAT) {
578 push_float(rhs.f);
579 } else {
580 fprintf(stderr, "RUNTIME ERROR at _ITOF: %s: operand is not number\n", func->name);
581 exit__2(-1);
582 }
583
584 break;
585 case RS_OP_FTOI:
586 rhs = pop();
587
588 if (rhs.type == RS_INT) {
589 push_int((int) rhs.i);
590 } else if (rhs.type == RS_FLOAT) {
591 push_int((int) rhs.f);
592 } else {
593 fprintf(stderr, "RUNTIME ERROR at _FTOI: %s: operand is not number\n", func->name);
594 exit__2(-1);
595 }
596
597 break;
598 case RS_OP_PRINT:
599 sp -= pc->arg1;
600 print(sp, pc->arg1);
601 break;
602 case RS_OP_EXT:
603 sp -= pc->arg1;
604
605 if (!skip_wait) {
606 ext(sp, pc->arg1);
607 }
608
609 break;
610 case RS_OP_END:
611 end = 1;
612 pc = 0;
613 return;
614 case RS_OP_CALL:
615 pc = call_func((funcdata *) (code + (int) pc->arg2), pc);
616 pc--;
617 break;
618 case RS_OP_RET:
619 value = pop();
620
621 if (call_sp != call) {
622 sp = frame;
623 pc = ret_func();
624 } else {
625 result = value.i;
626 push(value);
627 pc = 0;
628 end = 1;
629 return;
630 }
631
632 push(value);
633 break;
634 case RS_OP_WAIT:
635 if (!skip_wait) {
636 pc++;
637 return;
638 }
639
640 break;
641 case RS_OP_SKIP_END:
642 if (skip_wait) {
643 skip_wait = 0;
644 pc++;
645 return;
646 }
647
648 break;
649 }
650
651 pc++;
652 }
653}
vmcode_t * ret_func()
RS_STACKDATA * sp
void skip()
void ext(RS_STACKDATA *args, int argc)
RS_STACKDATA * stack_end
vmcode_t * call_func(funcdata *callee, vmcode_t *return_pc)
void exe(vmcode_t *entry)
funcdata * func
RS_CALLDATA * call
RS_CALLDATA * call_end
RS_STACKDATA * stack
void check_stack()
Definition runscript.cpp:71
void reload(RS_PROG_HEADER *prog)
RS_STACKDATA pop()
RS_PROG_HEADER * prog
RS_STACKDATA * frame
void resume()
void push_ptr(RS_STACKDATA *pointer)
Definition runscript.cpp:94
int run(int no)
void ext_func(int(**table)(RS_STACKDATA *, int), int count)
int check_program(int no)
vmcode_t * pc
RS_CALLDATA * call_sp
void push_int(int value)
Definition runscript.cpp:82
int(** ext_func_table)(RS_STACKDATA *, int)
void push_float(float value)
char * code
void load(RS_PROG_HEADER *prog, RS_STACKDATA *stack, int stack_num, RS_CALLDATA *call, int call_num)
void push(RS_STACKDATA value)
Definition runscript.cpp:77
void push_str(char *string)
Definition runscript.cpp:88
RS_STACKDATA * frame
vmcode_t * ret
funcdata * func
RS_STACKDATA * p
Definition runscript.hpp:83
char * name