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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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.
*
*/
#include "gargoyle/frotz/processor.h"
#include "gargoyle/frotz/frotz.h"
namespace Gargoyle {
namespace Frotz {
// TODO: Stubs to replace with actual code
zword save_undo() { return 0; }
zword restore_undo() { return 0; }
Opcode Processor::var_opcodes[64] = {
&Processor::__illegal__,
&Processor::z_je,
&Processor::z_jl,
&Processor::z_jg,
&Processor::z_dec_chk,
&Processor::z_inc_chk,
&Processor::z_jin,
&Processor::z_test,
&Processor::z_or,
&Processor::z_and,
&Processor::z_test_attr,
&Processor::z_set_attr,
&Processor::z_clear_attr,
&Processor::z_store,
&Processor::z_insert_obj,
&Processor::z_loadw,
&Processor::z_loadb,
&Processor::z_get_prop,
&Processor::z_get_prop_addr,
&Processor::z_get_next_prop,
&Processor::z_add,
&Processor::z_sub,
&Processor::z_mul,
&Processor::z_div,
&Processor::z_mod,
&Processor::z_call_s,
&Processor::z_call_n,
&Processor::z_set_colour,
&Processor::z_throw,
&Processor::__illegal__,
&Processor::__illegal__,
&Processor::__illegal__,
&Processor::z_call_s,
&Processor::z_storew,
&Processor::z_storeb,
&Processor::z_put_prop,
&Processor::z_read,
&Processor::z_print_char,
&Processor::z_print_num,
&Processor::z_random,
&Processor::z_push,
&Processor::z_pull,
&Processor::z_split_window,
&Processor::z_set_window,
&Processor::z_call_s,
&Processor::z_erase_window,
&Processor::z_erase_line,
&Processor::z_set_cursor,
&Processor::z_get_cursor,
&Processor::z_set_text_style,
&Processor::z_buffer_mode,
&Processor::z_output_stream,
&Processor::z_input_stream,
&Processor::z_sound_effect,
&Processor::z_read_char,
&Processor::z_scan_table,
&Processor::z_not,
&Processor::z_call_n,
&Processor::z_call_n,
&Processor::z_tokenise,
&Processor::z_encode_text,
&Processor::z_copy_table,
&Processor::z_print_table,
&Processor::z_check_arg_count
};
Opcode Processor::ext_opcodes[64] = {
&Processor::z_save,
&Processor::z_restore,
&Processor::z_log_shift,
&Processor::z_art_shift,
&Processor::z_set_font,
&Processor::__illegal__, // glkify - Processor::z_draw_picture,
&Processor::__illegal__, // glkify - Processor::z_picture_data,
&Processor::__illegal__, // glkify - Processor::z_erase_picture,
&Processor::__illegal__, // glkify - Processor::z_set_margins,
&Processor::z_save_undo,
&Processor::z_restore_undo,
&Processor::z_print_unicode,
&Processor::z_check_unicode,
&Processor::z_set_true_colour, // spec 1.1
&Processor::__illegal__,
&Processor::__illegal__,
&Processor::__illegal__, // glkify - Processor::z_move_window,
&Processor::__illegal__, // glkify - Processor::z_window_size,
&Processor::__illegal__, // glkify - Processor::z_window_style,
&Processor::__illegal__, // glkify - Processor::z_get_wind_prop,
&Processor::__illegal__, // glkify - Processor::z_scroll_window,
&Processor::z_pop_stack,
&Processor::__illegal__, // glkify - Processor::z_read_mouse,
&Processor::__illegal__, // glkify - Processor::z_mouse_window,
&Processor::z_push_stack,
&Processor::__illegal__, // glkify - Processor::z_put_wind_prop,
&Processor::z_print_form,
&Processor::z_make_menu,
&Processor::__illegal__, // glkify - Processor::z_picture_table
&Processor::z_buffer_screen, // spec 1.1
};
Processor::Processor(OSystem *syst, const GargoyleGameDescription *gameDesc) :
GlkInterface(syst, gameDesc), Errors(),
_finished(0), _sp(nullptr), _fp(nullptr), _frameCount(0),
zargc(0), _decoded(nullptr), _encoded(nullptr), _resolution(0),
_randomInterval(0), _randomCtr(0), first_restart(true), script_valid(false),
_bufPos(0), _locked(false), _prevC('\0'), script_width(0),
sfp(nullptr), rfp(nullptr), pfp(nullptr) {
static const Opcode OP0_OPCODES[16] = {
&Processor::z_rtrue,
&Processor::z_rfalse,
&Processor::z_print,
&Processor::z_print_ret,
&Processor::z_nop,
&Processor::z_save,
&Processor::z_restore,
&Processor::z_restart,
&Processor::z_ret_popped,
&Processor::z_catch,
&Processor::z_quit,
&Processor::z_new_line,
&Processor::z_show_status,
&Processor::z_verify,
&Processor::__extended__,
&Processor::z_piracy
};
static const Opcode OP1_OPCODES[16] = {
&Processor::z_jz,
&Processor::z_get_sibling,
&Processor::z_get_child,
&Processor::z_get_parent,
&Processor::z_get_prop_len,
&Processor::z_inc,
&Processor::z_dec,
&Processor::z_print_addr,
&Processor::z_call_s,
&Processor::z_remove_obj,
&Processor::z_print_obj,
&Processor::z_ret,
&Processor::z_jump,
&Processor::z_print_paddr,
&Processor::z_load,
&Processor::z_call_n
};
Common::copy(&OP0_OPCODES[0], &OP0_OPCODES[16], op0_opcodes);
Common::copy(&OP1_OPCODES[0], &OP1_OPCODES[16], op1_opcodes);
Common::fill(&_stack[0], &_stack[STACK_SIZE], 0);
Common::fill(&zargs[0], &zargs[8], 0);
Common::fill(&_buffer[0], &_buffer[TEXT_BUFFER_SIZE], '\0');
}
void Processor::initialize() {
Mem::initialize();
GlkInterface::initialize();
if (h_version <= V4) {
op0_opcodes[9] = &Processor::z_pop;
op1_opcodes[15] = &Processor::z_not;
} else {
op0_opcodes[9] = &Processor::z_catch;
op1_opcodes[15] = &Processor::z_call_n;
}
}
void Processor::load_operand(zbyte type) {
zword value;
if (type & 2) {
// variable
zbyte variable;
CODE_BYTE(variable);
if (variable == 0)
value = *_sp++;
else if (variable < 16)
value = *(_fp - variable);
else {
zword addr = h_globals + 2 * (variable - 16);
LOW_WORD(addr, value);
}
} else if (type & 1) {
// small constant
zbyte bvalue;
CODE_BYTE(bvalue);
value = bvalue;
} else {
// large constant
CODE_WORD(value);
}
zargs[zargc++] = value;
}
void Processor::load_all_operands(zbyte specifier) {
for (int i = 6; i >= 0; i -= 2) {
zbyte type = (specifier >> i) & 0x03;
if (type == 3)
break;
load_operand(type);
}
}
void Processor::interpret() {
do {
zbyte opcode;
CODE_BYTE(opcode);
zargc = 0;
if (opcode < 0x80) {
// 2OP opcodes
load_operand((zbyte)(opcode & 0x40) ? 2 : 1);
load_operand((zbyte)(opcode & 0x20) ? 2 : 1);
(*this.*var_opcodes[opcode & 0x1f])();
} else if (opcode < 0xb0) {
// 1OP opcodes
load_operand((zbyte)(opcode >> 4));
(*this.*op1_opcodes[opcode & 0x0f])();
} else if (opcode < 0xc0) {
// 0OP opcodes
(*this.*op0_opcodes[opcode - 0xb0])();
} else {
// VAR opcodes
zbyte specifier1;
zbyte specifier2;
if (opcode == 0xec || opcode == 0xfa) { // opcodes 0xec
CODE_BYTE(specifier1); // and 0xfa are
CODE_BYTE(specifier2); // call opcodes
load_all_operands(specifier1); // with up to 8
load_all_operands(specifier2); // arguments
} else {
CODE_BYTE(specifier1);
load_all_operands(specifier1);
}
(*this.*var_opcodes[opcode - 0xc0])();
}
#if defined(DJGPP) && defined(SOUND_SUPPORT)
if (end_of_sound_flag)
end_of_sound();
#endif
} while (!_finished);
_finished--;
}
void Processor::call(zword routine, int argc, zword *args, int ct) {
long pc;
zword value;
zbyte count;
int i;
if (_sp - _stack < 4)
runtimeError(ERR_STK_OVF);
GET_PC(pc);
*--_sp = (zword)(pc >> 9);
*--_sp = (zword)(pc & 0x1ff);
*--_sp = (zword)(_fp - _stack - 1);
*--_sp = (zword)(argc | (ct << (_save_quetzal ? 12 : 8)));
_fp = _sp;
_frameCount++;
// Calculate byte address of routine
if (h_version <= V3)
pc = (long)routine << 1;
else if (h_version <= V5)
pc = (long)routine << 2;
else if (h_version <= V7)
pc = ((long)routine << 2) + ((long)h_functions_offset << 3);
else if (h_version <= V8)
pc = (long)routine << 3;
else {
// h_version == V9
long indirect = (long)routine << 2;
HIGH_LONG(indirect, pc);
}
if ((uint)pc >= story_size)
runtimeError(ERR_ILL_CALL_ADDR);
SET_PC(pc);
// Initialise local variables
CODE_BYTE(count);
if (count > 15)
runtimeError(ERR_CALL_NON_RTN);
if (_sp - _stack < count)
runtimeError(ERR_STK_OVF);
if (_save_quetzal)
_fp[0] |= (zword)count << 8; // Save local var count for Quetzal.
value = 0;
for (i = 0; i < count; i++) {
if (h_version <= V4) // V1 to V4 games provide default
CODE_WORD(value); // values for all local variables
*--_sp = (zword)((argc-- > 0) ? args[i] : value);
}
// Start main loop for direct calls
if (ct == 2)
interpret();
}
void Processor::ret(zword value) {
long pc;
int ct;
if (_sp > _fp)
runtimeError(ERR_STK_UNDF);
_sp = _fp;
ct = *_sp++ >> (_save_quetzal ? 12 : 8);
_frameCount--;
_fp = _stack + 1 + *_sp++;
pc = *_sp++;
pc = ((long)*_sp++ << 9) | pc;
SET_PC(pc);
// Handle resulting value
if (ct == 0)
store(value);
if (ct == 2)
*--_sp = value;
// Stop main loop for direct calls
if (ct == 2)
_finished++;
}
void Processor::branch(bool flag) {
long pc;
zword offset;
zbyte specifier;
zbyte off1;
zbyte off2;
CODE_BYTE(specifier);
off1 = specifier & 0x3f;
if (!flag)
specifier ^= 0x80;
if (!(specifier & 0x40)) {
// it's a long branch
if (off1 & 0x20) // propagate sign bit
off1 |= 0xc0;
CODE_BYTE(off2);
offset = (off1 << 8) | off2;
} else {
// It's a short branch
offset = off1;
}
if (specifier & 0x80) {
if (offset > 1) {
// normal branch
GET_PC(pc);
pc += (short)offset - 2;
SET_PC(pc);
} else {
// special case, return 0 or 1
ret(offset);
}
}
}
void Processor::store(zword value) {
zbyte variable;
CODE_BYTE(variable);
if (variable == 0)
*--_sp = value;
else if (variable < 16)
*(_fp - variable) = value;
else {
zword addr = h_globals + 2 * (variable - 16);
SET_WORD(addr, value);
}
}
int Processor::direct_call(zword addr) {
zword saved_zargs[8];
int saved_zargc;
int i;
// Calls to address 0 return false
if (addr == 0)
return 0;
// Save operands and operand count
for (i = 0; i < 8; i++)
saved_zargs[i] = zargs[i];
saved_zargc = zargc;
// Call routine directly
call(addr, 0, 0, 2);
// Restore operands and operand count
for (i = 0; i < 8; i++)
zargs[i] = saved_zargs[i];
zargc = saved_zargc;
// Resulting value lies on top of the stack
return (short)*_sp++;
}
void Processor::seed_random(int value) {
if (value == 0) {
// Now using random values
_randomInterval = 0;
} else if (value < 1000) {
// special seed value
_randomCtr = 0;
_randomInterval = value;
} else {
// standard seed value
_random.setSeed(value);
_randomInterval = 0;
}
}
void Processor::__extended__() {
zbyte opcode;
zbyte specifier;
CODE_BYTE(opcode);
CODE_BYTE(specifier);
load_all_operands(specifier);
if (opcode < 0x1e) // extended opcodes from 0x1e on
(*this.*ext_opcodes[opcode])(); // are reserved for future spec'
}
void Processor::__illegal__() {
runtimeError(ERR_ILL_OPCODE);
}
void Processor::z_catch() {
store(_save_quetzal ? _frameCount : (zword)(_fp - _stack));
}
void Processor::z_throw() {
if (_save_quetzal) {
if (zargs[1] > _frameCount)
runtimeError(ERR_BAD_FRAME);
// Unwind the stack a frame at a time.
for (; _frameCount > zargs[1]; --_frameCount)
_fp = _stack + 1 + _fp[1];
} else {
if (zargs[1] > STACK_SIZE)
runtimeError(ERR_BAD_FRAME);
_fp = _stack + zargs[1];
}
ret(zargs[0]);
}
void Processor::z_call_n() {
if (zargs[0] != 0)
call(zargs[0], zargc - 1, zargs + 1, 1);
}
void Processor::z_call_s() {
if (zargs[0] != 0)
call(zargs[0], zargc - 1, zargs + 1, 0);
else
store(0);
}
void Processor::z_check_arg_count() {
if (_fp == _stack + STACK_SIZE)
branch(zargs[0] == 0);
else
branch(zargs[0] <= (*_fp & 0xff));
}
void Processor::z_jump() {
long pc;
GET_PC(pc);
pc += (short)zargs[0] - 2;
if ((uint)pc >= story_size)
runtimeError(ERR_ILL_JUMP_ADDR);
SET_PC(pc);
}
void Processor::z_nop() {
// Do nothing
}
void Processor::z_quit() {
_finished = 9999;
}
void Processor::z_ret() {
ret(zargs[0]);
}
void Processor::z_ret_popped() {
ret(*_sp++);
}
void Processor::z_rfalse() {
ret(0);
}
void Processor::z_rtrue() {
ret(1);
}
void Processor::z_random() {
if ((short) zargs[0] <= 0) {
// set random seed
seed_random(- (short) zargs[0]);
store(0);
} else {
// generate random number
zword result;
if (_randomInterval != 0) {
// ...in special mode
result = _randomCtr++;
if (_randomCtr == _randomInterval)
_randomCtr = 0;
} else {
// ...in standard mode
result = _random.getRandomNumber(0xffff);
}
store((zword)(result % zargs[0] + 1));
}
}
void Processor::z_sound_effect() {
zword number = zargs[0];
zword effect = zargs[1];
zword volume = zargs[2];
if (zargc < 1)
number = 0;
if (zargc < 2)
effect = EFFECT_PLAY;
if (zargc < 3)
volume = 8;
if (number >= 3 || number == 0) {
_soundLocked = true;
if (_storyId == LURKING_HORROR && (number == 9 || number == 16)) {
if (effect == EFFECT_PLAY) {
next_sample = number;
next_volume = volume;
_soundLocked = false;
if (!_soundPlaying)
start_next_sample();
} else {
_soundLocked = false;
}
return;
}
_soundPlaying = false;
switch (effect) {
case EFFECT_PREPARE:
os_prepare_sample (number);
break;
case EFFECT_PLAY:
start_sample(number, lo (volume), hi (volume), (zargc == 4) ? zargs[3] : 0);
break;
case EFFECT_STOP:
os_stop_sample (number);
break;
case EFFECT_FINISH_WITH:
os_finish_with_sample (number);
break;
}
_soundLocked = false;
} else {
os_beep(number);
}
}
void Processor::z_piracy() {
branch(!_piracy);
}
void Processor::z_save_undo(void) {
store((zword)save_undo());
}
void Processor::z_restore_undo(void) {
store((zword)restore_undo());
}
} // End of namespace Scott
} // End of namespace Gargoyle
|