This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gnusto-engine.js
4886 lines (4078 loc) · 148 KB
/
gnusto-engine.js
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
// gnusto-lib.js || -*- Mode: Javascript; tab-width: 2; -*-
// The Gnusto JavaScript Z-machine library.
// $Header: /cvs/gnusto/src/xpcom/engine/gnusto-engine.js,v 1.116 2005/04/26 01:50:32 naltrexone42 Exp $
//
// Copyright (c) 2003-2009 The Gnusto Contributors
//
// The latest code is available at http://github.com/curiousdannii/gnusto/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License
// as published by the Free Software Foundation.
//
// 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 be able to view the GNU General Public License at
// http://www.gnu.org/copyleft/gpl.html ; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
var CVS_VERSION = '$Date: 2005/04/26 01:50:32 $';
var ENGINE_DESCRIPTION = "Gnusto's interactive fiction engine";
////////////////////////////////////////////////////////////////
//
// PART THE FIRST
//
// STUFF FROM GNUSTO-LIB WHICH STILL NEEDS MERGING IN
//
////////////////////////////////////////////////////////////////
var default_unicode_translation_table = {
155:0xe4, // a-diaeresis
156:0xf6, // o-diaeresis
157:0xfc, // u-diaeresis
158:0xc4, // A-diaeresis
159:0xd6, // O-diaeresis
160:0xdc, // U-diaeresis
161:0xdf, // German "sz" ligature
162:0xbb, // right quotation marks
163:0xab, // left quotation marks
164:0xeb, // e-diaeresis
165:0xef, // i-diaeresis
166:0xff, // y-diaeresis
167:0xcb, // E-diaeresis
168:0xcf, // I-diaeresis
169:0xe1, // a-acute
170:0xe9, // e-acute
171:0xed, // i-acute
172:0xf3, // o-acute
173:0xfa, // u-acute
174:0xfd, // y-acute
175:0xc1, // A-acute
176:0xc9, // E-acute
177:0xcd, // I-acute
178:0xd3, // O-acute
179:0xda, // U-acute
180:0xdd, // Y-acute
181:0xe0, // a-grave
182:0xe8, // e-grave
183:0xec, // i-grave
184:0xf2, // o-grave
185:0xf9, // u-grave
186:0xc0, // A-grave
187:0xc8, // E-grave
188:0xcc, // I-grave
189:0xd2, // O-grave
190:0xd9, // U-grave
191:0xe2, // a-circumflex
192:0xea, // e-circumflex
193:0xee, // i-circumflex
194:0xf4, // o-circumflex
195:0xfb, // u-circumflex
196:0xc2, // A-circumflex
197:0xca, // E-circumflex
198:0xce, // I-circumflex
199:0xd4, // O-circumflex
200:0xdb, // U-circumflex
201:0xe5, // a-ring
202:0xc5, // A-ring
203:0xf8, // o-slash
204:0xd8, // O-slash
205:0xe3, // a-tilde
206:0xf1, // n-tilde
207:0xf5, // o-tilde
208:0xc3, // A-tilde
209:0xd1, // N-tilde
210:0xd5, // O-tilde
211:0xe6, // ae-ligature
212:0xc6, // AE-ligature
213:0xe7, // c-cedilla
214:0xc7, // C-cedilla
215:0xfe, // thorn
216:0xf0, // eth
217:0xde, // Thorn
218:0xd0, // Eth
219:0xa3, // pound sterling sign
220:0x153, // oe-ligature
221:0x152, // OE-ligature
222:0xa1, // inverted pling
223:0xbf // inverted query
};
var reverse_unicode_table = {};
var isNotConst = /\D/;
var temp_var = 0;
var PARENT_REC = 0;
var SIBLING_REC = 1;
var CHILD_REC = 2;
var CALLED_FROM_INTERRUPT = 0;
if (!window) {
// Not everywhere has "window" defined
var window = {};
}
// Temporary variables used in JITspace; they need to be
// defined for QML, though browser JS would allow them to be
// properties of the global object.
var dummy;
var t;
var t2;
var PARCHMENT_SECURITY_OVERRIDE = window.PARCHMENT_SECURITY_OVERRIDE;
// Placeholder when decoding arguments for opcodes to indicate that
// an argument needs to be popped from the stack.
//var ARG_STACK_POP = "SP";
////////////////////////////////////////////////////////////////
// Effect codes, returned from run(). See the explanation below
// for |handlers|.
// Returned when we're expecting a line of keyboard input.
//
// Answer with the string the user has entered.
var GNUSTO_EFFECT_INPUT = '"RS"';
// Returned when we're expecting a single keypress (or mouse click).
// TODO: The lowest nibble may be 1 if the Z-machine has asked
// for timed input.
//
// Answer with the ZSCII code for the key pressed (see the Z-spec).
var GNUSTO_EFFECT_INPUT_CHAR = '"RC"';
// Returned when the Z-machine requests we save the game.
// Answer as in the Z-spec: 0 if we can't save, 1 if we can, or
// 2 if we've just restored.
var GNUSTO_EFFECT_SAVE = '"DS"';
// Returned when the Z-machine requests we load a game.
// Answer 0 if we can't load. (If we can, we won't be around to answer.)
var GNUSTO_EFFECT_RESTORE = '"DR"';
// Returned when the Z-machine requests we quit.
// Not to be answered, obviously.
var GNUSTO_EFFECT_QUIT = '"QU"';
// Returned when the Z-machine requests that we restart a game.
// Assumedly, we won't be around to answer it.
var GNUSTO_EFFECT_RESTART = '"NU"';
// Returned if we've run for more than a certain number of iterations.
// This means that the environment gets a chance to do some housekeeping
// if we're stuck deep in computation, or to break an infinite loop
// within the Z-code.
//
// Any value may be used as an answer; it will be ignored.
var GNUSTO_EFFECT_WIMP_OUT = '"WO"';
// Returned if we hit a breakpoint.
// Any value may be used as an answer; it will be ignored.
var GNUSTO_EFFECT_BREAKPOINT = '"BP"';
// Returned if either of the two header bits which
// affect printing have changed since last time
// (or if either of them is set on first printing).
var GNUSTO_EFFECT_FLAGS_CHANGED = '"XC"';
// Returned if the story wants to check whether it's been pirated.
// Answer 1 if it is, or 0 if it isn't.
// You probably just want to return 0.
var GNUSTO_EFFECT_PIRACY = '"CP"';
// Returned if the story wants to set the text style.
// effect_parameters() will return a list:
// [0] = a bitcoded text style, as in the Z-spec,
// or -1 not to set the style.
// [1] = the foreground colour to use, as in the Z-spec
// [2] = the background colour to use, as in the Z-spec
// Any value may be used as an answer; it will be ignored.
var GNUSTO_EFFECT_STYLE = '"SS"';
// Returned if the story wants to cause a sound effect.
// effect_parameters() will return a list, whose
// vales aren't fully specified at present.
// (Just go "bleep" for now.)
//
// Any value may be used as an answer; it will be ignored.
var GNUSTO_EFFECT_SOUND = '"FX"';
var GNUSTO_EFFECT_SPLITWINDOW = '"TW"';
var GNUSTO_EFFECT_SETWINDOW = '"SW"';
var GNUSTO_EFFECT_ERASEWINDOW = '"YW"';
var GNUSTO_EFFECT_ERASELINE = '"YL"';
// Returned if the story wants to set the position of
// the cursor in the upper window. The upper window should
// be currently active.
//
// effect_parameters() will return a list:
// [0] = the new Y coordinate
// [1] = the new X coordinate
// Any value may be used as an answer; it will be ignored.
var GNUSTO_EFFECT_SETCURSOR = '"SC"';
var GNUSTO_EFFECT_SETBUFFERMODE = '"SB"';
var GNUSTO_EFFECT_SETINPUTSTREAM = '"SI"';
var GNUSTO_EFFECT_GETCURSOR = '"GC"';
// Returned if the story wants to print a table, as with
// @print_table. (This is complicated enough to get its
// own effect code, rather than just using an internal buffer
// as most printing does.)
//
// effect_parameters() will return a list of lines to print.
//
// Any value may be used as an answer; it will be ignored.
var GNUSTO_EFFECT_PRINTTABLE = '"PT"';
////////////////////////////////////////////////////////////////
//
// PART THE SECOND
//
// THE HANDLERS AND HANDLER ARRAYS
//
////////////////////////////////////////////////////////////////
// JavaScript seems to have a problem with pointers to methods.
// We solve this in a Pythonesque manner. Each instruction handler
// is a simple function which takes two parameters: 1) the engine
// asking the question (i.e. the value which would be "this" if
// the function was a method), and 2) the list of actual arguments
// given in the z-code for that function.
function handleZ_je(engine, a) {
if (a.length<2) {
return ''; // it's a no-op
} else if (a.length==2) {
return engine._brancher(a[0]+'=='+a[1]);
} else {
var condition = '';
for (var i=1; i<a.length; i++) {
if (i!=1) condition = condition + '||';
condition = condition + 't=='+a[i];
}
return 't='+a[0]+';'+engine._brancher(condition);
}
}
function handleZ_jl(engine, a) {
// Convert both arguments to signed for the comparison.
var t1code, t2code;
if (isNotConst.test(a[0]))
t1code = 't=' + a[0] + ';t=((t & 0x8000 ? ~0xFFFF : 0) | t);';
else
t1code = 't=' + engine._unsigned2signed(a[0]) + ';';
if (isNotConst.test(a[1]))
t2code = 't2=' + a[1] + ';t2=((t2 & 0x8000 ? ~0xFFFF : 0) | t2);';
else
t2code = 't2=' + engine._unsigned2signed(a[1]) + ';';
return t1code + t2code + engine._brancher('t<t2');
}
function handleZ_jg(engine, a) {
// Convert both arguments to signed for the comparison.
var t1code, t2code;
if (isNotConst.test(a[0]))
t1code = 't=' + a[0] + ';t=((t & 0x8000 ? ~0xFFFF : 0) | t);';
else
t1code = 't=' + engine._unsigned2signed(a[0]) + ';';
if (isNotConst.test(a[1]))
t2code = 't2=' + a[1] + ';t2=((t2 & 0x8000 ? ~0xFFFF : 0) | t2);';
else
t2code = 't2=' + engine._unsigned2signed(a[1]) + ';';
return t1code + t2code + engine._brancher('t>t2');
}
/***
function handleZ_dec_chk(engine, a) {
return 't='+a[0]+';t2=_varcode_get(t)-1;_varcode_set(t2,t);'+engine._brancher('t2<'+a[1]);
}
function handleZ_inc_chk(engine, a) {
return 't='+a[0]+';t2=_varcode_get(t)+1;_varcode_set(t2,t);'+engine._brancher('t2>'+a[1]);
}
***/
// Increment/decrement a variable and branch
// Calls the generic function and adds a brancher to the end
function handleZ_inc_chk(engine, a)
{
var tcode = handleZ_inc(engine, a);
tcode += '{var t1=incdec; t1 = ((t1 & 0x8000 ? ~0xFFFF : 0) | t1); var t2=' + a[1] + '; t2=((t2 & 0x8000 ? ~0xFFFF : 0) | t2);' + engine._brancher('t1 > t2') + '}';
return tcode;
/*
var tcode, t2code;
tcode = handleZ_incdec(engine, a[0], '+', 1);
// Convert both arguments to signed for the comparison.
if (isNotConst.test(a[1]))
t2code = 't2=' + a[1] + ';t2=((t2 & 0x8000 ? ~0xFFFF : 0) | t2);';
else
t2code = 't2=' + engine._unsigned2signed(a[1]) + ';';
return tcode + t2code + 'tmp_'+temp_var+'=(('+'tmp_'+temp_var+' & 0x8000 ? ~0xFFFF : 0) | '+'tmp_'+temp_var+');' + engine._brancher('tmp_'+temp_var+' > t2');
*/
}
function handleZ_dec_chk(engine, a)
{
var tcode = handleZ_dec(engine, a);
tcode += '{var t1=incdec; t1 = ((t1 & 0x8000 ? ~0xFFFF : 0) | t1); var t2=' + a[1] + '; t2=((t2 & 0x8000 ? ~0xFFFF : 0) | t2);' + engine._brancher('t1 < t2') + '}';
return tcode;
/*
var tcode, t2code;
tcode = handleZ_incdec(engine, a[0], '-', 1);
// Convert both arguments to signed for the comparison.
if (isNotConst.test(a[1]))
t2code = 't2=' + a[1] + ';t2=((t2 & 0x8000 ? ~0xFFFF : 0) | t2);';
else
t2code = 't2=' + engine._unsigned2signed(a[1]) + ';';
return tcode + t2code + 'tmp_'+temp_var+'=(('+'tmp_'+temp_var+' & 0x8000 ? ~0xFFFF : 0) | '+'tmp_'+temp_var+');' + engine._brancher('tmp_'+temp_var+' < t2');
*/
}
function handleZ_jin(engine, a) {
return engine._brancher("_obj_in("+a[0]+','+a[1]+')');
}
function handleZ_test(engine, a) {
return 't='+a[1]+';'+engine._brancher('('+a[0]+'&t)==t');
}
function handleZ_or(engine, a) {
return engine._storer('('+a[0]+'|'+a[1]+')');
}
function handleZ_and(engine, a) {
return engine._storer('('+a[0]+'&'+a[1]+')');
}
function handleZ_test_attr(engine, a) {
return engine._brancher('_test_attr('+a[0]+','+a[1]+')');
}
function handleZ_set_attr(engine, a) {
return '_set_attr('+a[0]+','+a[1]+')';
}
function handleZ_clear_attr(engine, a) {
return '_clear_attr('+a[0]+','+a[1]+')';
}
/***
function handleZ_store(engine, a) {
return "_varcode_set("+a[1]+","+a[0]+")";
}
***/
// Store a variable
// Rather than calling _varcode_set() this function now access the variables directly
// a[0] is interpreted as in ZSD 4.2.2:
// 0 = top of game stack
// 1-15 = local variables
// 16 up = global variables
function handleZ_store(engine, a)
{
var code;
if (isNaN(a[0])) { // branch at runtime
code = '(' + a[0] + ' == 0) ? m_gamestack[m_gamestack.length - 1] = ' + a[1] + ' : (' + a[0] + ' < 0x10) ? m_locals[( ' + a[0] + ' - 1 )] = ' + a[1] + ' : setWord(' + a[1] + ', m_vars_start + ( ' + a[0] + ' - 16 ) * 2 )';
} else {
if (a[0] == 0) {
code = 'm_gamestack[m_gamestack.length - 1] = ' + a[1];
} else if (a[0] < 0x10) {
code = 'm_locals[( ' + a[0] + ' - 1 )] = ' + a[1];
} else {
code = 'setWord(' + a[1] + ', m_vars_start + ( ' + a[0] + ' - 16 ) * 2 )';
}
}
return code;
/*
;;; if (isNotConst.test(a[0])) { engine.logger('Z_store', a[0]); }
;;; if (a[0] == null || a[0] === true || a[0] === false || a[0] < 0 || a[0] > 0xFFFF) { engine.logger('Z_store address', a[0]); }
;;; if (a[1] == null || a[1] === true || a[1] === false || a[1] < 0 || a[1] > 0xFFFF) { engine.logger('Z_store value', a[1]); }
if (a[0] == 0)
return 'm_gamestack[m_gamestack.length - 1] = ' + a[1] + ';';
else if (a[0] < 16)
return 'm_locals[' + (a[0]-1) + '] = ' + a[1];
else
{
// If the variable is a function rather than a constant it will have to be determined at run time
if (isNotConst.test(a[0]))
var code = 'var high = m_vars_start + (' + a[0] + ' - 16) * 2, low = high + 1;', high = 'high', low = 'low';
else
var code = '', high = engine.m_vars_start + (a[0] - 16) * 2, low = high + 1;
// If we are setting a constant get the high and low bytes at compile time
if (!isNotConst.test(a[1]))
{
var value = a[1];
return code + 'm_memory[' + high + '] = ' + ((value >> 8) & 0xFF) + ';' +
'm_memory[' + low + '] = ' + (value & 0xFF);
}
else
{
var tmp = 'tmp_' + (++temp_var);
return code + 'var ' + tmp + ' = ' + a[1] + ';' +
'm_memory[' + high + '] = (' + tmp + ' >> 8) & 0xFF;' +
'm_memory[' + low + '] = ' + tmp + ' & 0xFF;';
}
}*/
}
function handleZ_insert_obj(engine, a) {
return "_insert_obj("+a[0]+','+a[1]+")";
}
// Load an array word
function handleZ_loadw(engine, a)
{
// Inline this getUnsignedWord call
//return engine._storer("getUnsignedWord(("+a[0]+"+2*"+a[1]+")&0xFFFF)");
// Calculate the address
// BUG: fails to wrap if the address is split across addresses 0xFFFF
// and 0x0000. I don't care. --Z
if (isNotConst.test(a[0]) || isNotConst.test(a[1]))
var code = 'var tmp_' + (++temp_var) + ' = (' + a[0] + ' + 2 * ' + a[1] + ') & 0xFFFF, ',
addr = 'tmp_' + temp_var,
addr1 = addr+'+1';
else
var code = 'var ',
addr = (a[0] + 2 * a[1]) & 0xFFFF,
addr1 = addr+1;
// Get the value and store it
var tmp = 'tmp_' + (++temp_var);
return code + tmp + ' = (m_memory[' + addr + '] << 8) | m_memory[' + addr1 + '];' +
engine._storer(tmp);
}
function handleZ_loadb(engine, a) {
return engine._storer("m_memory[0xFFFF&("+a[0]+"+"+a[1]+")]");
}
function handleZ_get_prop(engine, a) {
return engine._storer("_get_prop("+a[0]+','+a[1]+')');
}
function handleZ_get_prop_addr(engine, a) {
return engine._storer("_get_prop_addr("+a[0]+','+a[1]+')');
}
function handleZ_get_next_prop(engine, a) {
return engine._storer("_get_next_prop("+a[0]+','+a[1]+')');
}
// Should add/mul/div also check for overflows?
function handleZ_add(engine, a) {
return engine._storer( '(' + a[0]+' + '+a[1] + ') & 0xFFFF'); }
/***
function handleZ_sub(engine, a) {
return engine._storer(a[0]+'-'+a[1]); }
***/
// Subtract and store
function handleZ_sub(engine, a)
{
return engine._storer( '(' + a[0] + ' - ' + a[1] + ') & 0xFFFF' );
}
function handleZ_mul(engine, a) {
return engine._storer( '(' + a[0]+'*'+a[1] + ') & 0xFFFF'); }
function handleZ_div(engine, a) {
return engine._storer('_trunc_divide('+a[0]+','+a[1]+')');
}
function handleZ_mod(engine, a) {
return engine._storer('_trunc_modulo('+a[0]+','+a[1]+')');
}
function handleZ_set_colour(engine, a) {
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_STYLE+",-1,"+a[0]+','+a[1]+"];return";
}
function handleZ_throw(engine, a) {
engine.m_compilation_running = 0;
return "_throw_stack_frame("+a[0]+");return";
}
function handleZ_jz(engine, a) {
return engine._brancher(a[0]+'==0');
}
function handleZ_get_sibling(engine, a) {
return "t=_get_sibling("+a[0]+");"+engine._storer("t")+";"+engine._brancher("t");
}
function handleZ_get_child(engine, a) {
return "t=_get_child("+a[0]+");"+
engine._storer("t")+";"+
engine._brancher("t");
}
function handleZ_get_parent(engine, a) {
return engine._storer("_get_parent("+a[0]+")");
}
function handleZ_get_prop_len(engine, a) {
return engine._storer("_get_prop_len("+a[0]+')');
}
/***
function handleZ_inc(engine, a) {
return "t="+a[0]+';_varcode_set(_varcode_get(t)+1, t)';
}
function handleZ_dec(engine, a) {
return "t="+a[0]+';_varcode_set(_varcode_get(t)-1, t)';
}
***/
// Increment and decrement opcodes
// Calls the following generic function
function handleZ_inc(engine, a)
{
// return handleZ_incdec(engine, a[0], '+');
var code = 'var incdec; ';
if (isNaN(a[0])) { // branch at runtime
code += 'var incdec; if (' + a[0] + ' == 0) { incdec = m_gamestack[m_gamestack.length - 1] = (m_gamestack[m_gamestack.length - 1] + 1) & 0xFFFF; } else if (' + a[0] + ' < 0x10) { incdec = m_locals[( ' + a[0] + ' - 1 )] = (m_locals[( ' + a[0] + ' - 1 )] + 1) & 0xFFFF; } else { var val = getUnsignedWord(m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); val++; setWord(val, m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); incdec = val; }';
} else {
if (a[0] == 0) {
code += 'incdec = m_gamestack[m_gamestack.length - 1] = (m_gamestack[m_gamestack.length - 1] + 1) & 0xFFFF;';
} else if (a[0] < 0x10) {
code += 'incdec = m_locals[( ' + a[0] + ' - 1 )] = (m_locals[( ' + a[0] + ' - 1 )] + 1) & 0xFFFF;';
} else {
code += '{var val = getWord(m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); val++; setWord(val, m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); incdec = val;}';
}
}
return code;
}
function handleZ_dec(engine, a)
{
// return handleZ_incdec(engine, a[0], '-');
var code = 'var incdec; ';
if (isNaN(a[0])) { // branch at runtime
code += 'if (' + a[0] + ' == 0) { incdec = m_gamestack[m_gamestack.length - 1] = (m_gamestack[m_gamestack.length - 1] - 1) & 0xFFFF; } else if (' + a[0] + ' < 0x10) { incdec = m_locals[( ' + a[0] + ' - 1 )] = (m_locals[( ' + a[0] + ' - 1 )] - 1) & 0xFFFF; } else { var val = getUnsignedWord(m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); val--; setWord(val, m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); incdec = val; }';
} else {
if (a[0] == 0) {
code += 'incdec = m_gamestack[m_gamestack.length - 1] = (m_gamestack[m_gamestack.length - 1] - 1) & 0xFFFF;';
} else if (a[0] < 0x10) {
code += 'incdec = m_locals[( ' + a[0] + ' - 1 )] = (m_locals[( ' + a[0] + ' - 1 )] - 1) & 0xFFFF;';
} else {
code += '{var val = getUnsignedWord(m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); val--; setWord(val, m_vars_start + ( ' + a[0] + ' - 16 ) * 2 ); incdec = val;}';
}
}
return code;
}
// Increment and decrement variables
// A generic function used by dec, dec_chk, inc and inc_chk
// Rather than calling _varcode_set() and _varcode_get() this function now accesses the variables directly
// variable is interpreted as in ZSD 4.2.2:
// 0 = top of game stack
// 1-15 = local variables
// 16 up = global variables
/*
function handleZ_incdec(engine, variable, sign, varRequired)
{
if (isNotConst.test(variable))
engine.logger('Z_incdec', variable);
var tmp = 'tmp_' + (++temp_var);
if (variable == 0)
return (varRequired ? 'var ' + tmp + ' = ' : '') + '(m_gamestack[m_gamestack.length - 1] = (m_gamestack[m_gamestack.length - 1]'+sign+'1)&0xFFFF);';
else if (variable < 0x10)
return (varRequired ? 'var ' + tmp + ' = ' : '') + '(m_locals[' + (variable-1) + '] = (m_locals[' + (variable-1) + ']'+sign+'1)&0xFFFF);';
else
{
// If the variable is a function rather than a constant it will have to be determined at run time
if (isNotConst.test(variable))
var code = 'var add = m_vars_start + (' + variable + ' - 16) * 2, ', add = 'add';
else
var code = 'var ', add = engine.m_vars_start + (variable - 16) * 2;
// Get the value from memory and inc/dec it!
return code + tmp + ' = (m_memory[' + add + '] << 8) | m_memory[' + add + ' + 1];' +
tmp + ' = (' + tmp + ' ' + sign + ' 1) & 0xFFFF;' +
'm_memory[' + add + '] = (' + tmp + ' >> 8) & 0xFF;' +
'm_memory[' + add + ' + 1] = ' + tmp + ' & 0xFF;';
}
}
*/
function handleZ_print_addr(engine, a) {
return engine._handler_zOut('_zscii_from('+a[0]+')',0);
}
function handleZ_remove_obj(engine, a) {
return "_remove_obj("+a[0]+','+a[1]+")";
}
function handleZ_print_obj(engine, a) {
return engine._handler_zOut("_name_of_object("+a[0]+")",0);
}
function handleZ_ret(engine, a) {
engine.m_compilation_running=0;
return "_func_return("+a[0]+');return';
}
function handleZ_jump(engine, a) {
engine.m_compilation_running=0;
if (a[0] & 0x8000) {
a[0] = (~0xFFFF) | a[0];
}
var addr=(a[0] + engine.m_pc) - 2;
return "m_pc="+addr+";return";
}
function handleZ_print_paddr(engine, a) {
return engine._handler_zOut("_zscii_from("+engine.m_pc_translate_for_string(a[0])+")",0);
}
function handleZ_load( engine, a )
{
var code;
if (isNaN(a[0])) { // branch at runtime
code = '(' + a[0] + ' == 0) ? m_gamestack[m_gamestack.length - 1] : (' + a[0] + ' < 0x10) ? m_locals[( ' + a[0] + ' - 1 )] : getUnsignedWord( m_vars_start + ( ' + a[0] + ' - 16 ) * 2 )';
} else {
if (a[0] == 0) {
code = 'm_gamestack[m_gamestack.length - 1]';
} else if (a[0] < 0x10) {
code = 'm_locals[( ' + a[0] + ' - 1 )]';
} else {
code = 'getUnsignedWord( m_vars_start + ( ' + a[0] + ' - 16 ) * 2 )';
}
}
return engine._storer( code );
//return engine._storer('_varcode_get('+a[0]+')');
/* var code;
if ( a[0] == 0 ) {
code = 'm_gamestack[m_gamestack.length - 1]';
} else if ( a[0] < 0x10 ) {
code = 'm_locals[( ' + a[0] + ' - 1 )]';
} else {
code = 'getUnsignedWord( m_vars_start + ( ' + a[0] + ' - 16 ) * 2 )';
}
return engine._storer( code );
*/
}
function handleZ_rtrue(engine, a) {
engine.m_compilation_running=0;
return "_func_return(1);return";
}
function handleZ_rfalse(engine, a) {
engine.m_compilation_running=0;
return "_func_return(0);return";
}
function handleZ_print(engine, a) {
return engine._handler_print('', 0);
}
function handleZ_print_ret(engine, a) {
engine.m_compilation_running = 0;
return engine._handler_print('\n', 1)+';_func_return(1);return';
}
function handleZ_nop(engine, a) {
return "";
}
function handleZ_restart(engine, a) {
engine.m_compilation_running=0;
return "m_effects=["+GNUSTO_EFFECT_RESTART+"];return";
}
function handleZ_ret_popped(engine, a) {
engine.m_compilation_running=0;
return "_func_return(m_gamestack.pop());return";
}
function handleZ_catch(engine, a) {
// The stack frame cookie is specified by Quetzal 1.3b s6.2
// to be the number of frames on the stack.
return engine._storer("m_call_stack.length");
}
function handleZ_pop(engine, a) {
return "m_gamestack.pop()";
}
function handleZ_quit(engine, a) {
engine.m_compilation_running=0;
return "m_effects=["+GNUSTO_EFFECT_QUIT+"];return";
}
function handleZ_new_line(engine, a) {
return engine._handler_zOut("'\\n'",0);
}
function handleZ_show_status(engine, a){ //(illegal from V4 onward)
engine._handler_zOut(''); //chalk forces repaint of status bar
return "";
}
function handleZ_verify(engine, a) {
return engine._brancher('_verify()');
}
function handleZ_illegal_extended(engine, a) {
// 190 can't be generated; it's the start of an extended opcode
gnusto_error(199);
}
function handleZ_piracy(engine, a) {
engine.m_compilation_running = 0;
var setter = 'm_rebound=function(){'+engine._brancher('(!0)')+'};';//m_answers[0])')+'};';
return "m_pc="+engine.m_pc+";"+setter+"m_effects=["+GNUSTO_EFFECT_PIRACY+"];return";
}
////////////////////////////////////////////////////////////////
//
// Call handlers:
//
// Gosub-generating functions, in increasing order of
// arity (no args, one arg, many args), with the
// no-store versions first. The "*_vs2" instructions
// are conceptually identical to the corresponding
// "*_vs" instructions, and share the same handlers.
//
// naltrexone-- I've removed the VERBOSE lines
// which were rendered incorrect by this. If you need to turn
// them on again, I'll put them back in in the new form.
function handleZ_call_1n(engine, a) {
return engine._generate_gosub(a[0], '', 0);
}
function handleZ_call_1s(engine, a) {
return engine._generate_gosub(a[0], '', 1);
}
function handleZ_call_2n(engine, a) {
return engine._generate_gosub(a[0], a[1], 0);
}
function handleZ_call_2s(engine, a) {
return engine._generate_gosub(a[0], a[1], 1);
}
function handleZ_call_vn(engine, a) {
return engine._generate_gosub(a[0], a.slice(1), 0);
}
function handleZ_call_vs(engine, a) {
return engine._generate_gosub(a[0], a.slice(1), 1);
}
////////////////////////////////////////////////////////////////
/***
function handleZ_store_w(engine, a) {
return "setWord("+a[2]+","+a[0]+"+2*"+a[1]+")";
}
***/
// Store a value in an array
function handleZ_store_w(engine, a)
{
// Calculate the address
if (isNotConst.test(a[0]) || isNotConst.test(a[1]))
var code = 'var tmp_' + (++temp_var) + ' = (' + a[0] + ' + 2 * ' + a[1] + ') & 0xFFFF;', addr = 'tmp_' + temp_var, addr1 = addr+'+1';
else
var code = '', addr = (a[0] + 2 * a[1]) & 0xFFFF, addr1 = addr+1;
// If we are setting a constant get the high and low bytes at compile time
if (!isNotConst.test(a[2]))
{
if (engine.m_value_asserts) {
if (a[2] == null || a[2] === true || a[2] === false || a[2] < 0 || a[2] > 0xFFFF)
engine.logger('Z_store_w value', a[2]);
}
return code + 'm_memory[' + addr + '] = ' + ((a[2] >> 8) & 0xFF) + ';' +
'm_memory[' + addr1 + '] = ' + (a[2] & 0xFF);
}
else
{
var tmp = 'tmp_' + (++temp_var);
return code + 'var ' + tmp + ' = ' + a[2] + ';' +
'm_memory[' + addr + '] = (' + tmp + ' >> 8) & 0xFF;' +
'm_memory[' + addr1 + '] = ' + tmp + ' & 0xFF;';
}
}
function handleZ_storeb(engine, a) {
return "setByte("+a[2]+",("+a[0]+"+"+a[1]+")&0xFFFF)";
}
function handleZ_putprop(engine, a) {
return "_put_prop("+a[0]+','+a[1]+','+a[2]+')';
}
// read, aread, sread, whatever it's called today.
// That's something that we can't deal with within gnusto:
// ask the environment to magic something up for us.
function handleZ_read(engine, a) {
// JS representing number of deciseconds to wait before a
// timeout should occur, or 0 if there shouldn't be one.
var timeout_deciseconds;
// JS representing the address of the timeout routine,
// or 0 if there isn't one.
var address_of_timeout_routine;
engine.m_compilation_running = 0;
// Since a[0] (address of the text buffer) is referenced so often,
// we introduce a variable |a0| into JITspace with the same value.
// A JS string telling us what to do if there isn't a timeout.
var rebound_for_no_timeout =
"_aread(m_answers[0],m_rebound_args[1],"+
"m_rebound_args[2],m_answers[1])";
// A JS string telling how to get the number of characters to "recap".
var recaps_getter;
// A JS string telling us how to get the number of characters the
// text buffer can hold.
var char_count_getter;
if (engine.m_version>=5) {
// In z5-z8, @read is a store instruction.
rebound_for_no_timeout = engine._storer(rebound_for_no_timeout);
} // Otherwise we just leave the call to _aread() as it is.
if (engine.m_version>=5) {
// z5+ use two header bytes at the start of the table.
recaps_getter = "m_memory[0xFFFF&a0+1]";
char_count_getter = "m_memory[0xFFFF&a0]";
} else {
// z1-z4 only use one. (They don't have recaps.)
recaps_getter = '0';
char_count_getter = "m_memory[0xFFFF&a0]+1";
}
if (a[2] && a[3] && (engine.m_version>=4)) {
// This is a timed routine.
// a[3] is the routine to call after a[2] deciseconds.
timeout_deciseconds = a[2];
address_of_timeout_routine = engine.m_pc_translate_for_routine(a[3]);
} else {
// No timeout.
timeout_deciseconds = '0';
address_of_timeout_routine = '0';
// Optimisation: In this case we could optimise rebound_setter
// so that it doesn't check whether to call the interrupt
// service routine. We haven't done this here for simplicity,
// but we did it in the simpler @read_char.
}
// JS for a function to handle the answer to this effect.
// The answer will be one integer; if the integer is zero,
// it's a request for a timeout; if it's nonzero, it's a
// keycode to be stored as the present instruction dictates.
var rebound_setter = "m_rebound=function(){"+
"var t=1*m_answers[0];" +
"if(t<0){"+
"_func_interrupt(m_rebound_args[0],onISRReturn_for_read);"+ // -ve: timeout
"}else{"+
rebound_for_no_timeout + ";" +
"}"+
"};";
var rebound_args_setter =
"m_rebound_args=["+
address_of_timeout_routine + "," + // Where to jump on timeout
"a0,"+ // Address of text buffer
a[1]+","+ // Address of parse buffer
"];";
/****************************************************************/
return "var a0=eval("+ a[0] + ");" +
"m_pc=" + engine.m_pc + ";" +
rebound_args_setter +
rebound_setter +
"m_effects=["+
GNUSTO_EFFECT_INPUT + "," +
timeout_deciseconds + "," +
recaps_getter + "," +
char_count_getter + "," +
"_terminating_characters()];return";
}
function handleZ_print_char(engine, a) {
return engine._handler_zOut('_zscii_char_to_ascii('+a[0]+')',0);
}
function handleZ_print_num(engine, a) {
return engine._handler_zOut('_unsigned2signed('+a[0]+')',0);
}
function handleZ_random(engine, a) {
return engine._storer("_random_number("+a[0]+")");
}
function handleZ_push(engine, a) {
return 'm_gamestack.push('+a[0]+')';
}
function handleZ_pull(engine, a)
{
// return '_varcode_set(m_gamestack.pop(),'+a[0]+')';
var code = 'var pull = m_gamestack.pop(); ';
return code += handleZ_store(engine, [a[0], 'pull']);
}
function handleZ_split_window(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_SPLITWINDOW+","+a[0]+"];return";
}
function handleZ_set_window(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_SETWINDOW+","+a[0]+"];return";
}
function handleZ_erase_window(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_ERASEWINDOW+","+engine._unsigned2signed(a[0])+"];return";
}
function handleZ_erase_line(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_ERASELINE+","+a[0]+"];return";
}
function handleZ_set_cursor(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_SETCURSOR+","+a[0]+","+a[1]+"];return";
}
function handleZ_get_cursor(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_GETCURSOR+","+a[0]+"];return";
}
function handleZ_set_text_style(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_STYLE+","+a[0]+",0,0];return";
}
function handleZ_buffer_mode(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_SETBUFFERMODE+","+a[0]+"];return";
}
function handleZ_output_stream(engine, a) {
return '_set_output_stream('+a[0]+','+a[1]+')';
}
function handleZ_input_stream(engine, a) {
engine.m_compilation_running=0;
return "m_pc="+engine.m_pc+";m_effects=["+GNUSTO_EFFECT_SETINPUTSTREAM+","+a[0]+"];return";
}
function handleZ_sound_effect(engine, a) {
// We're rather glossing over whether and how we
// deal with callbacks at present.
engine.m_compilation_running=0;
while (a.length < 5) { a.push(0); }
return "m_pc="+engine.m_pc+';m_effects=['+GNUSTO_EFFECT_SOUND+','+a[0]+','+a[1]+','+a[2]+','+a[3]+','+a[4]+'];return';
}
// Maybe factor out "read" and this?
function handleZ_read_char(engine, a) {
// JS representing number of deciseconds to wait before a
// timeout should occur, or 0 if there shouldn't be one.
var timeout_deciseconds;
// JS to set m_rebound_args to show where to jump if there's
// a timeout. If there's not going to be a timeout, this should
// be blank.
var rebound_args_setter;
// JS for a function to handle the answer to this effect.
// The answer will be one integer; if the integer is zero,
// it's a request for a timeout; if it's nonzero, it's a
// keycode to be stored as the present instruction dictates.
var rebound_setter;
// Stop the engine! We want to get off!
engine.m_compilation_running = 0;
// a[0] is always 1; probably not worth checking for this.
if (a[1] && a[2] && (engine.m_version>=4)) {
// This is a timed routine.