/
Machine.dart
1701 lines (1282 loc) · 42.1 KB
/
Machine.dart
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
/**
* Base machine that is compatible with Z-Machine V1.
*
*/
class Machine
{
static final int STACK_MARKER = -0x10000;
/// Z-Machine False = 0
static final int FALSE = 0;
/// Z-Machine True = 1
static final int TRUE = 1;
static final int SP = 0;
final _Stack stack;
final _Stack callStack;
DRandom r;
String pcHex([int offset = 0]) => '[0x${(pc + offset).toRadixString(16)}]';
/// Z-Machine Program Counter
int pc = 0;
_MemoryMap mem;
Map<String, Function> ops;
int get propertyDefaultsTableSize() => 31;
/**
* Takes any Dart int between -32768 & 32767 and makes a machine-readable
* 16-bit signed 'word' from it.
*
* ref(2.2)
*/
static int dartSignedIntTo16BitSigned(int val){
assert(val >= -32768 && val <= 32767);
if (val >= 0) return val;
val = val.abs();
return 65536 - val;
}
/**
* Converts a game 16-bit 'word' into a signed Dart int.
*
* ref(2.2)
*/
static int toSigned(int val){
if (val == 0) return val;
// game 16-bit word is always positive number to Dart
assert(val > 0);
// convert to signed if 16-bit MSB is set
return (val & 0x8000) == 0x8000
? -(65536 - val)
: val;
}
ZVersion get version() => ZVersion.V1;
// Kb
int get maxFileLength() => 128;
int unpack(int packedAddr){
return packedAddr << 1;
}
int pack(int unpackedAddr){
return unpackedAddr >> 1;
}
int fileLengthMultiplier() => 2;
void visitRoutine(List<int> params){
//V3
if (callStack.length == 0){
//main routine
pc--;
}
Debugger.verbose(' Calling Routine at ${pc.toRadixString(16)}');
// assign any params passed to locals and push locals onto the call stack
var locals = readb();
stack.push(STACK_MARKER);
Debugger.verbose(' # Locals: ${locals}');
if (locals > 16){
throw new GameException('Maximum local variable allocations (16) exceeded.');
}
//set the routine to default locals (V3...)
if (locals > 0){
for(int i = 1; i <= locals; i++){
if (i <= params.length){
//if param avail, store it
callStack.push(params[i - 1]);
Debugger.verbose(' Local ${i}: 0x${(params[i-1]).toRadixString(16)}');
//mem.storew(pc, params[i - 1]);
}else{
//push otherwise push the local
callStack.push(mem.loadw(pc));
Debugger.verbose(' Local ${i}: 0x${mem.loadw(pc).toRadixString(16)}');
}
pc += 2;
}
}
//push total locals onto the call stack
callStack.push(locals);
}
void callVS(){
Debugger.verbose('${pcHex(-1)} [call_vs]');
var operands = this.visitOperandsVar(4, true);
var resultStore = readb();
var returnAddr = pc;
if (operands.isEmpty())
throw new GameException('Call function address not given.');
//unpack function address
operands[0].rawValue = this.unpack(operands[0].value);
if (operands[0].value == 0){
//calling routine at address 0x00 automatically returns FALSE (ref 6.4.3)
writeVariable(resultStore, Machine.FALSE);
}else{
//move to the routine address
pc = operands[0].rawValue;
//setup the routine stack frame and locals
visitRoutine(new List.from(operands.getRange(1, operands.length - 1).map((o) => o.value)));
//push the result store address onto the call stack
callStack.push(resultStore);
//push the return address onto the call stack
callStack.push(returnAddr);
}
}
void doReturn(){
// pop the return value from whoever is returning
var result = callStack.pop();
// return address
var returnAddr = callStack.pop();
// result store address byte
var resultAddrByte = callStack.pop();
if (returnAddr == 0)
throw new GameException('Illegal return from entry routine.');
// unwind the locals from the stack
var frameSize = callStack.peek();
Debugger.verbose('(unwinding stack 1 frame)');
//unwind locals
while(frameSize >= 0){
callStack.pop();
frameSize--;
}
//unwind game stack
var gs = stack.pop();
while(gs != STACK_MARKER){
gs = stack.pop();
}
writeVariable(resultAddrByte, result);
pc = returnAddr;
}
void visitInstruction(){
var i = readb();
if (ops.containsKey('$i')){
if (Debugger.enableDebug){
if (Debugger.enableTrace && !Z.inBreak){
if (opCodes.containsKey('$i')){
Debugger.debug('>>> (0x${(pc - 1).toRadixString(16)}) ${opCodes[i.toString()]} ($i)');
}else{
Debugger.debug('>>> (0x${(pc - 1).toRadixString(16)}) UNKNOWN ($i)');
}
Debugger.debug('${Debugger.dumpLocals()}');
}
if (Debugger.enableStackTrace){
Debugger.debug('Call Stack: $callStack');
Debugger.debug('Game Stack: $stack');
}
if (Debugger.isBreakPoint(pc - 1)){
Z.inBreak = true;
Debugger.debugStartAddr = pc - 1;
}
}
ops['$i']();
}else{
notFound();
}
}
void notFound(){
throw new GameException('Unsupported Op Code: ${mem.loadb(pc - 1)}');
}
void branch(bool testResult)
{
//calculates the local jump offset (ref 4.7)
int jumpToLabelOffset(int jumpByte){
if (BinaryHelper.isSet(jumpByte, 6)){
//single byte offset
return BinaryHelper.bottomBits(jumpByte, 6);
}else{
//create the 14-bit offset value with next byte
var val = (BinaryHelper.bottomBits(jumpByte, 6) << 8) | readb();
//convert to Dart signed int (14-bit MSB is the sign bit)
return ((val & 0x2000) == 0x2000)
? -(16384 - val)
: val;
}
}
var jumpByte = readb();
bool branchOn = BinaryHelper.isSet(jumpByte, 7);
Debugger.verbose(' (branch condition: $branchOn)');
if (testResult == null || testResult is! bool){
throw new GameException('Test function must return a boolean value.');
}
var offset = jumpToLabelOffset(jumpByte);
if ((branchOn && testResult) || (!branchOn && !testResult)){
// If the offset is 0 or 1 (FALSE or TRUE), perform a return
// operation.
if (offset == Machine.FALSE){
Debugger.verbose(' (branch returning FALSE)');
callStack.push(Machine.FALSE);
doReturn();
return;
}
if (offset == Machine.TRUE){
Debugger.verbose(' (branch returning TRUE)');
callStack.push(Machine.TRUE);
doReturn();
return;
}
//jump to the offset and continue...
pc += offset - 2;
Debugger.verbose(' (branching to 0x${pc.toRadixString(16)})');
return;
}
//otherwise just continue to the next instruction...
Debugger.verbose(' (continuing to next instruction)');
}
String getStatusJSON(){
var statusList = new List<String>();
statusList.add('STATUS');
statusList.add(Header.isScoreGame() ? 'SCORE' : 'TIME');
var locObject = new GameObjectV3(readVariable(0x10));
statusList.add(locObject.shortName);
statusList.add(readVariable(0x11).toString());
statusList.add(readVariable(0x12).toString());
return JSON.stringify(statusList);
}
void read(){
Debugger.verbose('${pcHex(-1)} [read]');
Z.inInput = true;
Z._printBuffer();
Z.sbuff.add(getStatusJSON());
Z._printBuffer();
var operands = this.visitOperandsVar(4, true);
var maxBytes = mem.loadb(operands[0].value);
var textBuffer = operands[0].value + 1;
var maxWords = mem.loadb(operands[1].value);
var parseBuffer = operands[1].value + 1;
void processLine(String line){
line = line.trim().toLowerCase();
Debugger.verbose(' (processing: "$line")');
if (line.length > maxBytes - 1){
line = line.substring(0, maxBytes - 2);
Debugger.verbose(' (text buffer truncated to "$line")');
}
var zChars = ZSCII.toZCharList(line);
//store the zscii chars in text buffer
for(final c in zChars){
mem.storeb(textBuffer++, c);
}
//terminator
mem.storeb(textBuffer, 0);
var tokens = Z.machine.mem.dictionary.tokenize(line);
Debugger.verbose(' (tokenized: $tokens)');
var parsed = Z.machine.mem.dictionary.parse(tokens, line);
//Debugger.debug('$parsed');
var maxParseBufferBytes = (4 * maxWords) + 2;
var i = 0;
for(final p in parsed){
i++;
if (i > maxParseBufferBytes) break;
mem.storeb(parseBuffer++, p);
}
// Z._io.callAsync(Z._runIt);
}
Future<String> line = Z.IOConfig.getLine();
doIt(foo){
if (line.isComplete){
Z.inInput = false;
if (line == '/!'){
Z.inBreak = true;
// Z._io.callAsync(Debugger.startBreak);
}else{
processLine(line.value);
}
}else{
line.then((String l){
Z.inInput = false;
if (l == '/!'){
Z.inBreak = true;
Debugger.debugStartAddr = pc - 1;
Debugger.startBreak(null);
// Z._io.callAsync(Debugger.startBreak);
}else{
processLine(l);
Z.runIt(null);
}
});
}
}
doIt(null);
// Z._io.callAsync(doIt);
}
void random(){
Debugger.verbose('${pcHex(-1)} [random]');
Math.random();
var operands = this.visitOperandsVar(1, false);
var resultTo = readb();
var range = operands[0].value;
//default return value in first two cases
var result = 0;
if (range < 0){
r = new DRandom.withSeed(range);
Debugger.verbose(' (set RNG to seed: $range)');
}else if(range == 0){
r = new DRandom.withSeed(new Date.now().milliseconds);
Debugger.verbose(' (set RNG to random seed)');
}else{
result = r.NextFromMax(range) + 1;
Debugger.verbose(' (Rolled [1 - $range] number: $result)');
}
writeVariable(resultTo, result);
}
void pull(){
Debugger.verbose('${pcHex(-1)} [pull]');
var operand = this.visitOperandsVar(1, false);
var value = stack.pop();
Debugger.verbose(' Pulling 0x${value.toRadixString(16)} from to the stack.');
writeVariable(operand[0].rawValue, value);
}
void push(){
Debugger.verbose('${pcHex(-1)} [push]');
var operand = this.visitOperandsVar(1, false);
Debugger.verbose(' Pushing 0x${operand[0].value.toRadixString(16)} to the stack.');
stack.push(operand[0].value);
// if (operand[0].rawValue == 0){
// //pushing SP into SP would be counterintuitive...
// stack.push(0);
// }else{
// stack.push(this.readVariable(operand[0].value));
// }
}
void ret_popped(){
Debugger.verbose('${pcHex(-1)} [ret_popped]');
var v = stack.pop();
assertNotMarker(v);
Debugger.verbose(' Popping 0x${v.toRadixString(16)} from the stack and returning.');
callStack.push(v);
doReturn();
}
assertNotMarker(m) {
if (m == Machine.STACK_MARKER){
throw new GameException('Stack Underflow.');
}
}
void rtrue(){
Debugger.verbose('${pcHex(-1)} [rtrue]');
callStack.push(Machine.TRUE);
doReturn();
}
void rfalse(){
Debugger.verbose('${pcHex(-1)} [rfalse]');
callStack.push(Machine.FALSE);
doReturn();
}
void jz(){
Debugger.verbose('${pcHex(-1)} [jz]');
var operand = this.visitOperandsShortForm();
branch(operand.value == 0);
}
void get_sibling(){
Debugger.verbose('${pcHex(-1)} [get_sibling]');
var operand = this.visitOperandsShortForm();
var resultTo = readb();
GameObjectV3 obj = new GameObjectV3(operand.value);
writeVariable(resultTo, obj.sibling);
branch(obj.sibling != 0);
}
void get_child(){
Debugger.verbose('${pcHex(-1)} [get_child]');
var operand = this.visitOperandsShortForm();
var resultTo = readb();
GameObjectV3 obj = new GameObjectV3(operand.value);
writeVariable(resultTo, obj.child);
branch(obj.child != 0);
}
void inc(){
Debugger.verbose('${pcHex(-1)} [inc]');
var operand = this.visitOperandsShortForm();
var value = toSigned(readVariable(operand.rawValue)) + 1;
writeVariable(operand.rawValue, value);
}
void dec(){
Debugger.verbose('${pcHex(-1)} [dec]');
var operand = this.visitOperandsShortForm();
var value = toSigned(readVariable(operand.rawValue)) - 1;
writeVariable(operand.rawValue, value);
}
void load(){
Debugger.verbose('${pcHex(-1)} [load]');
var operand = this.visitOperandsShortForm();
var resultTo = readb();
var v = readVariable(operand.rawValue);
writeVariable(resultTo, v);
}
void test(){
Debugger.verbose('${pcHex(-1)} [test]');
var pp = pc - 1;
var operands = this.visitOperandsLongForm();
var jumpByte = mem.loadb(pc);
bool branchOn = BinaryHelper.isSet(jumpByte, 7);
var bitmap = operands[0].value;
var flags = operands[1].value;
Debugger.verbose(' [0x${pp.toRadixString(16)}] testing bitmap($branchOn) "${bitmap.toRadixString(2)}" against "${flags.toRadixString(2)}" ${(bitmap & flags) == flags}');
branch((bitmap & flags) == flags);
}
void dec_chk(){
Debugger.verbose('${pcHex(-1)} [dec_chk]');
var operands = this.visitOperandsLongForm();
var value = toSigned(readVariable(operands[0].rawValue)) - 1;
//(ref http://www.gnelson.demon.co.uk/zspec/sect14.html notes #5)
writeVariable(operands[0].rawValue, value);
branch(value < toSigned(operands[1].value));
}
void inc_chkV(){
Debugger.verbose('${pcHex(-1)} [inc_chk]');
var operands = this.visitOperandsVar(2, false);
var value = toSigned(readVariable(operands[0].rawValue)) + 1;
//(ref http://www.gnelson.demon.co.uk/zspec/sect14.html notes #5)
writeVariable(operands[0].rawValue, value);
branch(value > toSigned(operands[1].value));
}
void inc_chk(){
Debugger.verbose('${pcHex(-1)} [inc_chk]');
var operands = this.visitOperandsLongForm();
// var value = toSigned(readVariable(operands[0].rawValue)) + 1;
var varValue = readVariable(operands[0].rawValue);
var value = toSigned(readVariable(operands[0].rawValue)) + 1;
//(ref http://www.gnelson.demon.co.uk/zspec/sect14.html notes #5)
writeVariable(operands[0].rawValue, value);
branch(value > toSigned(operands[1].value));
}
void test_attr(){
Debugger.verbose('${pcHex(-1)} [test_attr]');
var operands = this.visitOperandsLongForm();
GameObjectV3 obj = new GameObjectV3(operands[0].value);
Debugger.verbose(' (test Attribute) >>> object: ${obj.shortName}(${obj.id}) ${operands[1].value}: ${obj.isFlagBitSet(operands[1].value)}');
branch(obj.isFlagBitSet(operands[1].value));
}
void jin() {
Debugger.verbose('${pcHex(-1)} [jin]');
var operands = this.visitOperandsLongForm();
var child = new GameObjectV3(operands[0].value);
var parent = new GameObjectV3(operands[1].value);
branch(child.parent == parent.id);
}
void jeV(){
Debugger.verbose('${pcHex(-1)} [jeV]');
var operands = this.visitOperandsVar(4, true);
if (operands.length < 2){
throw new GameException('At least 2 operands required for jeV instruction.');
}
var foundMatch = false;
var testVal = toSigned(operands[0].value);
for(int i = 1; i < operands.length; i++){
if (foundMatch == true) break;
var against = toSigned(operands[i].value);
if (testVal == against){
foundMatch = true;
}
}
branch(foundMatch);
}
void quit(){
Debugger.verbose('${pcHex(-1)} [quit]');
Z.quit = true;
}
void restart(){
Debugger.verbose('${pcHex(-1)} [restart]');
Z.softReset();
var obj = new GameObjectV3(4);
assert(obj.child == 0);
// visit the main 'routine'
visitRoutine([]);
//push dummy result store onto the call stack
callStack.push(0);
//push dummy return address onto the call stack
callStack.push(0);
Z.IOConfig.callAsync(Z.runIt);
}
void jl(){
Debugger.verbose('${pcHex(-1)} [jl]');
var operands = visitOperandsLongForm();
branch(toSigned(operands[0].value) < toSigned(operands[1].value));
}
void jgv(){
Debugger.verbose('${pcHex(-1)} [jgv]');
var operands = this.visitOperandsVar(2, false);
branch(toSigned(operands[0].value) > toSigned(operands[1].value));
}
void jg(){
Debugger.verbose('${pcHex(-1)} [jg]');
var operands = this.visitOperandsLongForm();
branch(toSigned(operands[0].value) > toSigned(operands[1].value));
}
void je(){
Debugger.verbose('${pcHex(-1)} [je]');
var operands = this.visitOperandsLongForm();
branch(toSigned(operands[0].value) == toSigned(operands[1].value));
}
void newline(){
Debugger.verbose('${pcHex(-1)} [newline]');
Z.sbuff.add('\n');
}
void print_obj(){
Debugger.verbose('${pcHex(-1)} [print_obj]');
var operand = this.visitOperandsShortForm();
var obj = new GameObjectV3(operand.value);
Z.sbuff.add(obj.shortName);
}
void print_addr(){
Debugger.verbose('${pcHex(-1)} [print_addr]');
var operand = this.visitOperandsShortForm();
var addr = operand.value;
var str = ZSCII.readZStringAndPop(addr);
//print('${pcHex()} "$str"');
Z.sbuff.add(str);
}
void print_paddr(){
Debugger.verbose('${pcHex(-1)} [print_paddr]');
var operand = this.visitOperandsShortForm();
var addr = this.unpack(operand.value);
var str = ZSCII.readZStringAndPop(addr);
//print('${pcHex()} "$str"');
Z.sbuff.add(str);
}
void print_char(){
Debugger.verbose('${pcHex(-1)} [print_char]');
var operands = this.visitOperandsVar(1, false);
var z = operands[0].value;
if (z < 0 || z > 1023){
throw new GameException('ZSCII char is out of bounds.');
}
Z.sbuff.add(ZSCII.ZCharToChar(z));
}
void print_num(){
Debugger.verbose('${pcHex(-1)} [print_num]');
var operands = this.visitOperandsVar(1, false);
Z.sbuff.add('${toSigned(operands[0].value)}');
}
void print_ret(){
Debugger.verbose('${pcHex(-1)} [print_ret]');
var str = ZSCII.readZStringAndPop(pc);
Z.sbuff.add('${str}\n');
// print('${pcHex()} "$str"');
callStack.push(Machine.TRUE);
doReturn();
}
void printf(){
Debugger.verbose('${pcHex(-1)} [print]');
var str = ZSCII.readZString(pc);
Z.sbuff.add(str);
pc = callStack.pop();
}
void insertObj(){
Debugger.verbose('${pcHex(-1)} [insert_obj]');
var operands = this.visitOperandsLongForm();
GameObjectV3 from = new GameObjectV3(operands[0].value);
GameObjectV3 to = new GameObjectV3(operands[1].value);
Debugger.verbose('Insert Object ${from.id}(${from.shortName}) into ${to.id}(${to.shortName})');
from.insertTo(to.id);
}
void remove_obj(){
Debugger.verbose('${pcHex(-1)} [remove_obj]');
var operand = this.visitOperandsShortForm();
GameObjectV3 o = new GameObjectV3(operand.value);
Debugger.verbose('Removing Object ${o.id}(${o.shortName}) from object tree.');
o.removeFromTree();
}
void storev(){
Debugger.verbose('${pcHex(-1)} [storev]');
var operands = this.visitOperandsVar(2, false);
assert(operands[0].rawValue <= 0xff);
writeVariable(operands[0].rawValue, operands[1].value);
}
void store(){
Debugger.verbose('${pcHex(-1)} [store]');
var operands = this.visitOperandsLongForm();
assert(operands[0].rawValue <= 0xff);
if (operands[0].rawValue == Machine.SP){
operands[0].rawValue = readVariable(Machine.SP);
}
writeVariable(operands[0].rawValue, operands[1].value);
}
void jump(){
Debugger.verbose('${pcHex(-1)} [jump]');
var operand = this.visitOperandsShortForm();
var offset = toSigned(operand.value) - 2;
pc += offset;
Debugger.verbose(' (jumping to ${pcHex()})');
}
void ret(){
Debugger.verbose('${pcHex(-1)} [ret]');
var operand = this.visitOperandsShortForm();
Debugger.verbose(' returning 0x${operand.peekValue.toRadixString(16)}');
callStack.push(operand.value);
doReturn();
}
void get_parent(){
Debugger.verbose('${pcHex(-1)} [get_parent]');
var operand = this.visitOperandsShortForm();
var resultTo = readb();
GameObjectV3 obj = new GameObjectV3(operand.value);
writeVariable(resultTo, obj.parent);
}
void clear_attr(){
Debugger.verbose('${pcHex(-1)} [clear_attr]');
var operands = this.visitOperandsLongForm();
GameObjectV3 obj = new GameObjectV3(operands[0].value);
obj.unsetFlagBit(operands[1].value);
Debugger.verbose(' (clear Attribute) >>> object: ${obj.shortName}(${obj.id}) ${operands[1].value}: ${obj.isFlagBitSet(operands[1].value)}');
}
void set_attr(){
Debugger.verbose('${pcHex(-1)} [set_attr]');
var operands = this.visitOperandsLongForm();
GameObjectV3 obj = new GameObjectV3(operands[0].value);
obj.setFlagBit(operands[1].value);
Debugger.verbose(' (set Attribute) >>> object: ${obj.shortName}(${obj.id}) ${operands[1].value}: ${obj.isFlagBitSet(operands[1].value)}');
}
void andV(){
Debugger.verbose('${pcHex(-1)} [andV]');
var operands = this.visitOperandsVar(2, false);
var resultTo = readb();
writeVariable(resultTo, (operands[0].value & operands[1].value));
}
void orV(){
Debugger.verbose('${pcHex(-1)} [orV]');
var operands = this.visitOperandsVar(2, false);
var resultTo = readb();
writeVariable(resultTo, (operands[0].value | operands[1].value));
}
void or(){
Debugger.verbose('${pcHex(-1)} [or]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
writeVariable(resultTo, (operands[0].value | operands[1].value));
}
void and(){
Debugger.verbose('${pcHex(-1)} [and]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
writeVariable(resultTo, (operands[0].value & operands[1].value));
}
void sub(){
Debugger.verbose('${pcHex(-1)} [sub]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
var result = toSigned(operands[0].value) - toSigned(operands[1].value);
Debugger.verbose(' >>> (sub ${pc.toRadixString(16)}) ${operands[0].value}(${toSigned(operands[0].value)}) - ${operands[1].value}(${toSigned(operands[1].value)}) = $result');
writeVariable(resultTo, result);
}
void add(){
Debugger.verbose('${pcHex(-1)} [add]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
var result = toSigned(operands[0].value) + toSigned(operands[1].value);
Debugger.verbose(' >>> (add ${pc.toRadixString(16)}) ${operands[0].value}(${toSigned(operands[0].value)}) + ${operands[1].value}(${toSigned(operands[1].value)}) = $result');
writeVariable(resultTo, result);
}
void mul(){
Debugger.verbose('${pcHex(-1)} [mul]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
var result = toSigned(operands[0].value) * toSigned(operands[1].value);
Debugger.verbose(' >>> (mul ${pc.toRadixString(16)}) ${operands[0].value}(${toSigned(operands[0].value)}) * ${operands[1].value}(${toSigned(operands[1].value)}) = $result');
writeVariable(resultTo, result);
}
void div(){
Debugger.verbose('${pcHex(-1)} [div]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
if (operands[1].value == 0){
throw new GameException('Divide by 0.');
}
var result = (toSigned(operands[0].value) / toSigned(operands[1].value)).floor().toInt();
Debugger.verbose(' >>> (div ${pc.toRadixString(16)}) ${operands[0].value}(${toSigned(operands[0].value)}) / ${operands[1].value}(${toSigned(operands[1].value)}) = $result');
// if (result < 0){
// result = -(result.abs().floor());
// }else{
// result = result.floor();
// }
writeVariable(resultTo, result);
}
void mod(){
Debugger.verbose('${pcHex(-1)} [mod]');
var operands = this.visitOperandsLongForm();
var resultTo = readb();
if (operands[1].peekValue == 0){
throw new GameException('Divide by 0.');