-
Notifications
You must be signed in to change notification settings - Fork 20
/
scvm_op.c
2570 lines (2351 loc) · 64 KB
/
scvm_op.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ScummC
* Copyright (C) 2006 Alban Bedel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file scvm_op.c
* @ingroup scvm
* @brief SCVM op-code implementation
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include "scc_fd.h"
#include "scc_util.h"
#include "scc_cost.h"
#include "scc_box.h"
#include "scvm_res.h"
#include "scvm_thread.h"
#include "scvm.h"
static int scvm_op_dummy_v(scvm_t* vm, scvm_thread_t* thread);
static int scvm_op_dummy_vv(scvm_t* vm, scvm_thread_t* thread);
static int scvm_op_dummy_s(scvm_t* vm, scvm_thread_t* thread);
// Basic stack ops
int scvm_push(scvm_t* vm,int val) {
if(vm->stack_ptr + 1 >= vm->stack_size) {
scc_log(LOG_ERR,"Stack overflow!\n");
return SCVM_ERR_STACK_OVERFLOW;
}
scc_log(LOG_MSG,"Push %d\n",val);
vm->stack[vm->stack_ptr] = val;
vm->stack_ptr++;
return 0;
}
int scvm_pop(scvm_t* vm,unsigned* val) {
if(vm->stack_ptr < 1) {
scc_log(LOG_ERR,"Stack underflow!\n");
return SCVM_ERR_STACK_UNDERFLOW;
}
vm->stack_ptr--;
scc_log(LOG_MSG,"Pop %d\n",vm->stack[vm->stack_ptr]);
if(val) *val = vm->stack[vm->stack_ptr];
return 0;
}
int scvm_spop(scvm_t* vm,int* val) {
return scvm_pop(vm,(unsigned*)val);
}
int scvm_vpop(scvm_t* vm, ...) {
va_list ap;
unsigned* val;
int r;
va_start(ap,vm);
while((val = va_arg(ap,unsigned*)))
if((r = scvm_pop(vm,val))) return r;
va_end(ap);
return 0;
}
int scvm_peek(scvm_t* vm,int* val) {
if(vm->stack_ptr < 1) {
scc_log(LOG_ERR,"Stack underflow!\n");
return SCVM_ERR_STACK_UNDERFLOW;
}
if(val) *val = vm->stack[vm->stack_ptr-1];
return 0;
}
int scvm_thread_read_var(scvm_t* vm, scvm_thread_t* thread,
uint16_t addr, unsigned* val) {
// decode the address
if(addr & 0x8000) { // bit variable
addr &= 0x7FFF;
if(addr >= vm->num_bitvar) return SCVM_ERR_BAD_ADDR;
*val = (vm->bitvar[addr>>3]>>(addr&7))&1;
scc_log(LOG_MSG,"Read bit var %d: %d\n",addr,*val);
} else if(addr & 0x4000) { // thread local variable
addr &= 0x3FFF;
if(!thread || addr >= thread->num_var) return SCVM_ERR_BAD_ADDR;
*val = thread->var[addr];
scc_log(LOG_MSG,"Read local var %d: %d\n",addr,*val);
} else { // global variable
addr &= 0x3FFF;
if(addr >= vm->num_var) return SCVM_ERR_BAD_ADDR;
if(addr < 0x100 && vm->get_var[addr])
*val = vm->get_var[addr](vm,addr);
else
*val = vm->var_mem[addr];
scc_log(LOG_MSG,"Read global var %d: %d\n",addr,*val);
}
return 0;
}
int scvm_thread_write_var(scvm_t* vm, scvm_thread_t* thread,
uint16_t addr, unsigned val) {
// decode the address
if(addr & 0x8000) { // bit variable
addr &= 0x7FFF;
if(addr >= vm->num_bitvar) return SCVM_ERR_BAD_ADDR;
vm->bitvar[addr>>3] &= ~(1<<(addr&7));
vm->bitvar[addr>>3] |= (val&1)<<(addr&7);
scc_log(LOG_MSG,"Write bit var %d: %d\n",addr,val);
} else if(addr & 0x4000) { // thread local variable
addr &= 0x3FFF;
if(!thread || addr >= thread->num_var) return SCVM_ERR_BAD_ADDR;
thread->var[addr] = val;
scc_log(LOG_MSG,"Write local var %d: %d\n",addr,val);
} else { // global variable
addr &= 0x3FFF;
if(addr >= vm->num_var) return SCVM_ERR_BAD_ADDR;
if(addr < 0x100 && vm->set_var[addr])
vm->set_var[addr](vm,addr,val);
else
vm->var_mem[addr] = val;
scc_log(LOG_MSG,"Write global var %d: %d\n",addr,val);
}
return 0;
}
int scvm_thread_read_svar(scvm_t* vm, scvm_thread_t* thread,
uint16_t addr, int* val) {
return scvm_thread_read_var(vm,thread,addr,(unsigned*)val);
}
scvm_array_t* scvm_thread_get_array_var(scvm_t* vm, scvm_thread_t* thread,
uint16_t addr) {
unsigned id;
if(scvm_thread_read_var(vm,vm->current_thread,addr,&id) ||
id >= vm->num_array)
return NULL;
return vm->array[id].size ? &vm->array[id] : NULL;
}
unsigned char* scvm_thread_get_string_var(scvm_t* vm, scvm_thread_t* thread,
uint16_t addr) {
scvm_array_t* array = scvm_thread_get_array_var(vm,thread,addr);
return array ? array->data.byte : NULL;
}
int scvm_read_array(scvm_t* vm, unsigned addr, unsigned x, unsigned y, int* val) {
unsigned idx;
if(addr >= vm->num_array) return SCVM_ERR_BAD_ADDR;
idx = x+y*vm->array[addr].line_size;
if(idx >= vm->array[addr].size)
return SCVM_ERR_ARRAY_BOUND;
switch(vm->array[addr].type) {
case SCVM_ARRAY_WORD:
*val = vm->array[addr].data.word[idx];
break;
case SCVM_ARRAY_BYTE:
//case SCVM_ARRAY_CHAR:
*val = vm->array[addr].data.byte[idx];
break;
case SCVM_ARRAY_NIBBLE:
*val = (vm->array[addr].data.byte[idx>>2]>>((idx&1)<<2))&0xF;
break;
case SCVM_ARRAY_BIT:
*val = (vm->array[addr].data.byte[idx>>3]>>(idx&7))&1;
break;
default:
return SCVM_ERR_ARRAY_TYPE;
}
scc_log(LOG_MSG,"Read array %d[%d][%d]: %d\n",addr,x,y,*val);
return 0;
}
int scvm_write_array(scvm_t* vm, unsigned addr, unsigned x, unsigned y, int val) {
unsigned idx;
scc_log(LOG_MSG,"Write array %d[%d][%d]: %d\n",addr,x,y,val);
if(addr >= vm->num_array) return SCVM_ERR_BAD_ADDR;
idx = x+y*vm->array[addr].line_size;
if(idx >= vm->array[addr].size)
return SCVM_ERR_ARRAY_BOUND;
switch(vm->array[addr].type) {
case SCVM_ARRAY_WORD:
vm->array[addr].data.word[idx] = val;
break;
case SCVM_ARRAY_BYTE:
//case SCVM_ARRAY_CHAR:
val = vm->array[addr].data.byte[idx] = val;
break;
case SCVM_ARRAY_NIBBLE:
vm->array[addr].data.byte[idx>>2] &= 0xF<<((idx&1)<<2);
vm->array[addr].data.byte[idx>>2] |= (val&0xF)<<((idx&1)<<2);
break;
case SCVM_ARRAY_BIT:
vm->array[addr].data.byte[idx>>3] &= ~(1<<(idx&7));
vm->array[addr].data.byte[idx>>3] |= (val&1)<<(idx&7);
break;
default:
return SCVM_ERR_ARRAY_TYPE;
}
return 0;
}
int scvm_alloc_array(scvm_t* vm, unsigned type, unsigned x, unsigned y) {
scvm_array_t* array;
int addr;
switch(type) {
case SCVM_ARRAY_WORD:
case SCVM_ARRAY_BYTE:
break;
case SCVM_ARRAY_NIBBLE:
x = (x+1)&~1;
break;
case SCVM_ARRAY_BIT:
x = (x+7)&~7;
break;
default:
return SCVM_ERR_ARRAY_TYPE;
}
for(addr = 1 ; addr < vm->num_array ; addr++)
if(vm->array[addr].size == 0) break;
if(addr >= vm->num_array) return SCVM_ERR_OUT_OF_ARRAY;
array = &vm->array[addr];
array->type = type;
if(y) {
array->line_size = x;
array->size = x*y;
} else {
array->line_size = 0;
array->size = x;
}
switch(type) {
case SCVM_ARRAY_WORD:
array->data.word = calloc(2,array->size);
break;
case SCVM_ARRAY_BYTE:
array->data.byte = calloc(1,array->size);
break;
case SCVM_ARRAY_NIBBLE:
array->data.byte = calloc(1,array->size>>1);
break;
case SCVM_ARRAY_BIT:
array->data.byte = calloc(1,array->size>>3);
break;
}
return addr;
}
static int scvm_nuke_array(scvm_t* vm, unsigned addr) {
if(!addr || addr >= vm->num_array) return SCVM_ERR_BAD_ADDR;
if(vm->array[addr].data.byte) free(vm->array[addr].data.byte);
vm->array[addr].size = vm->array[addr].line_size = 0;
return 0;
}
// SCUMM ops
// 0x00
static int scvm_op_push_byte(scvm_t* vm, scvm_thread_t* thread) {
int r;
uint8_t byte;
if((r=scvm_thread_r8(thread,&byte))) return r;
return scvm_push(vm,byte);
}
// 0x01
static int scvm_op_push_word(scvm_t* vm, scvm_thread_t* thread) {
int r;
int16_t word;
if((r=scvm_thread_r16s(thread,&word))) return r;
return scvm_push(vm,word);
}
// 0x02
static int scvm_op_var_read_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint8_t addr;
if((r=scvm_thread_r8(thread,&addr)) ||
(r=scvm_thread_read_svar(vm,thread,addr,&val))) return r;
return scvm_push(vm,val);
}
// 0x03
static int scvm_op_var_read_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint16_t addr;
if((r=scvm_thread_r16(thread,&addr)) ||
(r=scvm_thread_read_svar(vm,thread,addr,&val))) return r;
return scvm_push(vm,val);
}
// 0x06
static int scvm_op_array_read_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint8_t vaddr;
if((r=scvm_thread_r8(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_pop(vm,&x)) ||
(r=scvm_read_array(vm,addr,x,0,&val)))
return r;
return scvm_push(vm,val);
}
// 0x07
static int scvm_op_array_read_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint16_t vaddr;
if((r=scvm_thread_r16(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_pop(vm,&x)) ||
(r=scvm_read_array(vm,addr,x,0,&val)))
return r;
return scvm_push(vm,val);
}
// 0x0A
static int scvm_op_array2_read_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x,y;
uint8_t vaddr;
if((r=scvm_thread_r8(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_vpop(vm,&x,&y,NULL)) ||
(r=scvm_read_array(vm,addr,x,y,&val)))
return r;
return scvm_push(vm,val);
}
// 0x0B
static int scvm_op_array2_read_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x,y;
uint16_t vaddr;
if((r=scvm_thread_r16(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_vpop(vm,&x,&y,NULL)) ||
(r=scvm_read_array(vm,addr,x,y,&val)))
return r;
return scvm_push(vm,val);
}
// 0x0C
static int scvm_op_dup(scvm_t* vm, scvm_thread_t* thread) {
int r,a;
if((r=scvm_peek(vm,&a))) return r;
return scvm_push(vm,a);
}
// 0x0D
static int scvm_op_not(scvm_t* vm, scvm_thread_t* thread) {
int r,a;
if((r=scvm_spop(vm,&a))) return r;
return scvm_push(vm,!a);
}
#define SCVM_BIN_OP(name,op) \
static int scvm_op_ ##name (scvm_t* vm, scvm_thread_t* thread) { \
int r,a,b; \
if((r=scvm_vpop(vm,&b,&a,NULL))) \
return r; \
return scvm_push(vm,(a op b)); \
}
// 0x0E ... 0x19
SCVM_BIN_OP(eq,==)
SCVM_BIN_OP(neq,!=)
SCVM_BIN_OP(gt,>)
SCVM_BIN_OP(lt,<)
SCVM_BIN_OP(ge,>=)
SCVM_BIN_OP(le,<=)
SCVM_BIN_OP(add,+)
SCVM_BIN_OP(sub,-)
SCVM_BIN_OP(mul,*)
SCVM_BIN_OP(div,/)
SCVM_BIN_OP(land,&&)
SCVM_BIN_OP(lor,||)
// 0xD6 0xD7
SCVM_BIN_OP(band,&)
SCVM_BIN_OP(bor,|)
// 0x1A
static int scvm_op_pop(scvm_t* vm, scvm_thread_t* thread) {
return scvm_pop(vm,NULL);
}
// 0x42
static int scvm_op_var_write_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint8_t addr;
if((r=scvm_thread_r8(thread,&addr)) ||
(r=scvm_spop(vm,&val))) return r;
return scvm_thread_write_var(vm,thread,addr,val);
}
// 0x43
static int scvm_op_var_write_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint16_t addr;
if((r=scvm_thread_r16(thread,&addr)) ||
(r=scvm_spop(vm,&val))) return r;
return scvm_thread_write_var(vm,thread,addr,val);
}
// 0x46
static int scvm_op_array_write_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint8_t vaddr;
if((r=scvm_thread_r8(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_vpop(vm,&val,&x,NULL)))
return r;
return scvm_write_array(vm,addr,x,0,val);
}
// 0x47
static int scvm_op_array_write_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint16_t vaddr;
if((r=scvm_thread_r16(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_vpop(vm,&val,&x,NULL)))
return r;
return scvm_write_array(vm,addr,x,0,val);
}
// 0x4A
static int scvm_op_array2_write_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x,y;
uint8_t vaddr;
if((r=scvm_thread_r8(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_vpop(vm,&val,&x,&y,NULL)))
return r;
return scvm_write_array(vm,addr,x,y,val);
}
// 0x4B
static int scvm_op_array2_write_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x,y;
uint16_t vaddr;
if((r=scvm_thread_r16(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_vpop(vm,&val,&x,&y,NULL)))
return r;
return scvm_write_array(vm,addr,x,y,val);
}
// 0x4E
static int scvm_op_var_inc_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint8_t addr;
if((r=scvm_thread_r8(thread,&addr)) ||
(r=scvm_thread_read_svar(vm,thread,addr,&val))) return r;
val++;
return scvm_thread_write_var(vm,thread,addr,val);
}
// 0x4F
static int scvm_op_var_inc_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint16_t addr;
if((r=scvm_thread_r16(thread,&addr)) ||
(r=scvm_thread_read_svar(vm,thread,addr,&val))) return r;
val++;
return scvm_thread_write_var(vm,thread,addr,val);
}
// 0x52
static int scvm_op_array_inc_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint8_t vaddr;
if((r=scvm_thread_r8(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_pop(vm,&x)) ||
(r=scvm_read_array(vm,addr,x,0,&val)))
return r;
val++;
return scvm_write_array(vm,addr,x,0,val);
}
// 0x53
static int scvm_op_array_inc_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint16_t vaddr;
if((r=scvm_thread_r16(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_pop(vm,&x)) ||
(r=scvm_read_array(vm,addr,x,0,&val)))
return r;
val++;
return scvm_write_array(vm,addr,x,0,val);
}
// 0x56
static int scvm_op_var_dec_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint8_t addr;
if((r=scvm_thread_r8(thread,&addr)) ||
(r=scvm_thread_read_svar(vm,thread,addr,&val))) return r;
val--;
return scvm_thread_write_var(vm,thread,addr,val);
}
// 0x57
static int scvm_op_var_dec_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
uint16_t addr;
if((r=scvm_thread_r16(thread,&addr)) ||
(r=scvm_thread_read_svar(vm,thread,addr,&val))) return r;
val--;
return scvm_thread_write_var(vm,thread,addr,val);
}
// 0x5A
static int scvm_op_array_dec_byte(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint8_t vaddr;
if((r=scvm_thread_r8(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_pop(vm,&x)) ||
(r=scvm_read_array(vm,addr,x,0,&val)))
return r;
val--;
return scvm_write_array(vm,addr,x,0,val);
}
// 0x5B
static int scvm_op_array_dec_word(scvm_t* vm, scvm_thread_t* thread) {
int r,val;
unsigned addr,x;
uint16_t vaddr;
if((r=scvm_thread_r16(thread,&vaddr)) ||
(r=scvm_thread_read_var(vm,thread,vaddr,&addr)) ||
(r=scvm_pop(vm,&x)) ||
(r=scvm_read_array(vm,addr,x,0,&val)))
return r;
val--;
return scvm_write_array(vm,addr,x,0,val);
}
// 0x5C
static int scvm_op_jmp_not_zero(scvm_t* vm, scvm_thread_t* thread) {
int r,ptr,val;
int16_t off;
if((r=scvm_thread_r16s(thread,&off)) ||
(r=scvm_spop(vm,&val))) return r;
ptr = thread->code_ptr+off;
if(ptr < 0 || ptr >= thread->script->size)
return SCVM_ERR_JUMP_BOUND;
if(val) thread->code_ptr = ptr;
return 0;
}
// 0x5D
static int scvm_op_jmp_zero(scvm_t* vm, scvm_thread_t* thread) {
int r,ptr,val;
int16_t off;
if((r=scvm_thread_r16s(thread,&off)) ||
(r=scvm_spop(vm,&val))) return r;
ptr = thread->code_ptr+off;
if(ptr < 0 || ptr >= thread->script->size)
return SCVM_ERR_JUMP_BOUND;
if(!val) thread->code_ptr = ptr;
return 0;
}
// 0x5E
static int scvm_op_start_script(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned num_args,script,flags;
if((r=scvm_pop(vm,&num_args))) return r;
else {
unsigned args[num_args+1];
args[0] = num_args;
while(num_args > 0) {
if((r=scvm_pop(vm,&args[num_args]))) return r;
num_args--;
}
if((r=scvm_vpop(vm,&script,&flags,NULL)) ||
(r=scvm_start_script(vm,flags,script,args)) < 0)
return r;
vm->next_thread = &vm->thread[r];
return SCVM_START_SCRIPT;
}
}
// 0x5F
static int scvm_op_start_script0(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned num_args,script;
if((r=scvm_pop(vm,&num_args))) return r;
else {
unsigned args[num_args+1];
args[0] = num_args;
while(num_args > 0) {
if((r=scvm_pop(vm,&args[num_args]))) return r;
num_args--;
}
if((r=scvm_pop(vm,&script)) ||
(r=scvm_start_script(vm,0,script,args)) < 0)
return r;
vm->next_thread = &vm->thread[r];
return SCVM_START_SCRIPT;
}
}
// 0xBF
static int scvm_op_start_script_recursive(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned num_args,script;
if((r=scvm_pop(vm,&num_args))) return r;
else {
unsigned args[num_args+1];
args[0] = num_args;
while(num_args > 0) {
if((r=scvm_pop(vm,&args[num_args]))) return r;
num_args--;
}
if((r=scvm_pop(vm,&script)) ||
(r=scvm_start_script(vm,SCVM_THREAD_RECURSIVE,script,args)) < 0)
return r;
vm->next_thread = &vm->thread[r];
return SCVM_START_SCRIPT;
}
}
// 0x60
static int scvm_op_start_object_script(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned num_args,flags,obj,verb;
if((r=scvm_pop(vm,&num_args))) return r;
else {
unsigned args[num_args+1];
args[0] = num_args;
while(num_args > 0) {
if((r=scvm_pop(vm,&args[num_args]))) return r;
num_args--;
}
if((r=scvm_vpop(vm,&verb,&obj,&flags,NULL)) ||
(r=scvm_start_object_script(vm,obj,verb,flags,args)) < 0)
return r;
vm->next_thread = &vm->thread[r];
return SCVM_START_SCRIPT;
}
}
// 0x61
static int scvm_op_draw_object(scvm_t* vm, scvm_thread_t* thread) {
int r,obj_id,state;
if((r = scvm_vpop(vm,&state,&obj_id,NULL)))
return r;
if(!vm->room) return SCVM_ERR_NO_ROOM;
// the lower address are actors
if(obj_id < 0x10 || obj_id >= vm->num_object)
return SCVM_ERR_BAD_OBJECT;
vm->object_pdata[obj_id].state = state > 0 ? state : 1;
return 0;
}
// 0x65, 0x66
static int scvm_op_stop_thread(scvm_t* vm, scvm_thread_t* thread) {
scvm_stop_thread(vm,thread);
return 0;
}
// 0x6B9C
static int scvm_op_init_charset(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned chset_id;
struct scc_charmap_st* chset;
if((r = scvm_pop(vm,&chset_id)))
return r;
if(!(chset = scvm_load_res(vm,SCVM_RES_CHARSET,chset_id)))
return SCVM_ERR_BAD_RESOURCE;
vm->current_charset = chset;
return 0;
}
// 0x6C
static int scvm_op_break_script(scvm_t* vm, scvm_thread_t* thread) {
thread->cycle = vm->cycle+1;
return 0;
}
// 0x6D
static int scvm_op_is_object_of_class(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned len;
if((r = scvm_pop(vm,&len))) return r;
else {
unsigned obj,i,vals[len];
for(i = 0 ; i < len ; i++)
if((r = scvm_pop(vm,&vals[i]))) return r;
if((r = scvm_pop(vm,&obj))) return r;
if(obj >= vm->num_object) return SCVM_ERR_BAD_OBJECT;
for(i = 0 ; i < len ; i++)
if(((vals[i] & 0x80) &&
!(vm->object_pdata[obj].klass & (1 << (vals[i]&0x7F)))) ||
(!(vals[i] & 0x80) &&
(vm->object_pdata[obj].klass & (1 << (vals[i]&0x7F))))) {
r = 1;
break;
}
}
return scvm_push(vm,!r);
}
// 0x6E
static int scvm_op_set_object_class(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned len;
if((r = scvm_pop(vm,&len))) return r;
else {
unsigned obj,i,vals[len];
for(i = 0 ; i < len ; i++)
if((r = scvm_pop(vm,&vals[i]))) return r;
if((r = scvm_pop(vm,&obj))) return r;
if(obj >= vm->num_object) return SCVM_ERR_BAD_OBJECT;
for(i = 0 ; i < len ; i++) {
if(vals[i] & 0x80)
vm->object_pdata[obj].klass |= 1 << (vals[i]&0x7F);
else
vm->object_pdata[obj].klass &= ~(1 << (vals[i]&0x7F));
}
}
return 0;
}
// 0x6F
static int scvm_op_get_object_state(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned obj;
if((r = scvm_pop(vm,&obj))) return r;
if(obj >= vm->num_object) return SCVM_ERR_BAD_OBJECT;
return scvm_push(vm,vm->object_pdata[obj].state);
}
// 0x70
static int scvm_op_set_object_state(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned obj, state;
if((r = scvm_vpop(vm,&state,&obj,NULL))) return r;
if(obj >= vm->num_object) return SCVM_ERR_BAD_OBJECT;
vm->object_pdata[obj].state = state;
return 0;
}
// 0x71
static int scvm_op_set_object_owner(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned obj, owner;
if((r = scvm_vpop(vm,&owner,&obj,NULL))) return r;
if(obj >= vm->num_object) return SCVM_ERR_BAD_OBJECT;
vm->object_pdata[obj].owner = owner;
return 0;
}
// 0x72
static int scvm_op_get_object_owner(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned obj;
if((r = scvm_pop(vm,&obj))) return r;
if(obj >= vm->num_object) return SCVM_ERR_BAD_OBJECT;
return scvm_push(vm,vm->object_pdata[obj].owner);
}
// 0x73
static int scvm_op_jmp(scvm_t* vm, scvm_thread_t* thread) {
int r,ptr;
int16_t off;
if((r=scvm_thread_r16s(thread,&off))) return r;
ptr = thread->code_ptr+off;
if(ptr < 0 || ptr >= thread->script->size)
return SCVM_ERR_JUMP_BOUND;
thread->code_ptr = ptr;
return 0;
}
// 0x78
static int scvm_op_pan_camera_to(scvm_t* vm, scvm_thread_t* thread) {
int r,x;
if((r=scvm_spop(vm,&x))) return r;
scvm_pan_camera_to(vm,x);
return 0;
}
// 0x79
static int scvm_op_camera_follow_actor(scvm_t* vm, scvm_thread_t* thread) {
unsigned a;
int r;
if((r=scvm_pop(vm,&a))) return r;
if(a >= vm->num_actor) return SCVM_ERR_BAD_ACTOR;
vm->view->follow = a;
return SCVM_CAMERA_FOLLOW_ACTOR;
}
// 0x7A
static int scvm_op_set_camera_at(scvm_t* vm, scvm_thread_t* thread) {
int r,x;
if((r=scvm_spop(vm,&x)) ||
(r=scvm_set_camera_at(vm,x)) < 0) return r;
// Stop following actors and panning
vm->view->follow = 0;
vm->view->flags &= ~ SCVM_VIEW_PAN;
if(r > 0) {
vm->next_thread = &vm->thread[r-1];
return SCVM_START_SCRIPT;
}
return 0;
}
// 0x7B
static int scvm_op_start_room(scvm_t* vm, scvm_thread_t* thread) {
int r;
if((r=scvm_pop(vm,&vm->next_room))) return r;
return SCVM_OPEN_ROOM;
}
// 0x7C
static int scvm_op_stop_script(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned id;
if((r=scvm_pop(vm,&id))) return r;
scvm_stop_script(vm,id);
return 0;
}
// 0x7D
static int scvm_op_walk_actor_to_object(scvm_t* vm, scvm_thread_t* thread) {
int r,x,y,dist;
unsigned a,o;
scvm_object_t* obj;
if((r=scvm_vpop(vm,&dist,&o,&a,NULL)))
return r;
if(a >= vm->num_actor) return SCVM_ERR_BAD_ACTOR;
if(!(obj = scvm_get_object(vm,o))) return SCVM_ERR_BAD_OBJECT;
x = obj->x, y = obj->y;
if(obj->hotspot) {
unsigned hs = obj->pdata->state;
if(hs > obj->num_hotspot) hs = obj->num_hotspot-1;
x += obj->hotspot[hs*2+0];
y += obj->hotspot[hs*2+1];
}
if(dist) {
if(vm->actor[a].x <= x)
x -= dist;
else
x += dist;
}
scvm_actor_walk_to(&vm->actor[a],x,y,obj->actor_dir);
return 0;
}
// 0x7E
static int scvm_op_walk_actor_to(scvm_t* vm, scvm_thread_t* thread) {
int r,a,x,y;
if((r=scvm_vpop(vm,&y,&x,&a,NULL)))
return r;
if(a < 0 || a >= vm->num_actor) return SCVM_ERR_BAD_ACTOR;
scvm_actor_walk_to(&vm->actor[a],x,y,-1);
return 0;
}
// 0x7F
static int scvm_op_put_actor_at(scvm_t* vm, scvm_thread_t* thread) {
int r,a,x,y,room;
if((r=scvm_vpop(vm,&room,&y,&x,&a,NULL)))
return r;
return scvm_put_actor_at(vm,a,x,y,room);
}
// 0x82
static int scvm_op_actor_animate(scvm_t* vm, scvm_thread_t* thread) {
int r,a,f;
if((r = scvm_vpop(vm,&f,&a,NULL)))
return r;
if(a < 0 || a >= vm->num_actor) return SCVM_ERR_BAD_ACTOR;
scvm_actor_animate(&vm->actor[a],f);
return 0;
}
// 0x83
static int scvm_op_do_sentence(scvm_t* vm, scvm_thread_t* thread) {
int r,vrb,obj_a,dummy,obj_b;
if((r = scvm_vpop(vm,&obj_b,&dummy,&obj_a,&vrb,NULL)))
return r;
scvm_do_sentence(vm,vrb,obj_a,obj_b);
return 0;
}
// 0x87
static int scvm_op_get_random_number(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned max;
if((r=scvm_pop(vm,&max))) return r;
vm->var->random_num = scvm_random(vm,0,max);
return scvm_push(vm,vm->var->random_num);
}
// 0x88
static int scvm_op_get_random_number_range(scvm_t* vm, scvm_thread_t* thread) {
int r,min,max;
if((r=scvm_vpop(vm,&max,&min,NULL)))
return r;
vm->var->random_num = scvm_random(vm,min,max);
return scvm_push(vm,vm->var->random_num);
}
// 0x8B
static int scvm_op_is_script_running(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned id;
if((r=scvm_pop(vm,&id))) return r;
r = scvm_is_script_running(vm,id);
return scvm_push(vm,r);
}
// 0x8C
static int scvm_op_get_actor_room(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned a;
if((r=scvm_pop(vm,&a))) return r;
if(a < 0 || a >= vm->num_actor) return SCVM_ERR_BAD_ACTOR;
return scvm_push(vm,vm->actor[a].room);
}
// 0x8D
static int scvm_op_get_object_x(scvm_t* vm, scvm_thread_t* thread) {
int r,x;
unsigned o;
if((r=scvm_pop(vm,&o)) ||
(r=scvm_get_object_position(vm,o,&x,NULL)) < 0) return r;
return scvm_push(vm,x);
}
// 0x8E
static int scvm_op_get_object_y(scvm_t* vm, scvm_thread_t* thread) {
int r,y;
unsigned o;
if((r=scvm_pop(vm,&o)) ||
(r=scvm_get_object_position(vm,o,NULL,&y)) < 0) return r;
return scvm_push(vm,y);
}
// 0x8F
static int scvm_op_get_object_direction(scvm_t* vm, scvm_thread_t* thread) {
int r;
unsigned a;
if((r=scvm_pop(vm,&a))) return r;
if(a < 0 || a >= vm->num_actor) return SCVM_ERR_BAD_ACTOR;
return scvm_push(vm,vm->actor[a].direction);