aboutsummaryrefslogtreecommitdiff
path: root/saga/sthread.cpp
blob: c8be77c7fc9ed4c47a5fe54f730786db16b22e4b (plain)
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

// Scripting module thread management component
#include "saga/saga.h"
#include "saga/yslib.h"

#include "saga/gfx.h"
#include "saga/actor.h"
#include "saga/console.h"

#include "saga/script.h"

#include "saga/sdata.h"

namespace Saga {

void Script::setFramePtr(SCRIPT_THREAD *thread, int newPtr) {
	thread->framePtr = newPtr;
	dataBuffer(3)->len = ARRAYSIZE(thread->stackBuf) - thread->framePtr;
	dataBuffer(3)->data = (SDataWord_T *) &(thread->stackBuf[newPtr]);
}

SCRIPT_THREAD *Script::SThreadCreate() {
	YS_DL_NODE *new_node;
	SCRIPT_THREAD *new_thread;

	if (!isInitialized()) {
		return NULL;
	}

	new_thread = (SCRIPT_THREAD *)calloc(1, sizeof *new_thread);
	if (new_thread == NULL) {
		return NULL;
	}

	new_node = ys_dll_add_head(threadList(), new_thread, sizeof *new_thread);
	free(new_thread);

	new_thread = (SCRIPT_THREAD *)ys_dll_get_data(new_node);

	new_thread->stackPtr = ARRAYSIZE(new_thread->stackBuf) - 1;
	setFramePtr(new_thread, new_thread->stackPtr);

	new_thread->flags = kTFlagWaiting;
	new_thread->waitType = kTWaitPause;

	dataBuffer(4)->len = ARRAYSIZE(new_thread->threadVars);
	dataBuffer(4)->data = new_thread->threadVars;

	return new_thread;
}

int Script::SThreadDestroy(SCRIPT_THREAD *thread) {
	YS_DL_NODE *walk_p;
	SCRIPT_THREAD *th;

	if (thread == NULL) {
		return FAILURE;
	}

	for (walk_p = ys_dll_head(threadList()); walk_p != NULL; walk_p = ys_dll_next(walk_p)) {
		th = (SCRIPT_THREAD *)ys_dll_get_data(walk_p);
		if (thread == th) {
			ys_dll_delete(walk_p);
			break;
		}
	}

	return SUCCESS;
}

int Script::SThreadExecThreads(uint msec) {
	YS_DL_NODE *walk_p, *next_p;
	SCRIPT_THREAD *thread;

	if (!isInitialized()) {
		return FAILURE;
	}

	walk_p = ys_dll_head(threadList());

	while (walk_p != NULL) {
		next_p = ys_dll_next(walk_p);

		thread = (SCRIPT_THREAD *)ys_dll_get_data(walk_p);

		if (thread->flags & (kTFlagFinished | kTFlagAborted)) {
			//if (thread->flags & kTFlagFinished) // FIXME. Missing function

			SThreadDestroy(thread);
			walk_p = next_p;
			continue;
		}

		if (thread->flags & kTFlagWaiting) {
			switch(thread->waitType) {
			case kTWaitDelay:
				if (thread->sleepTime < msec) {
					thread->sleepTime = 0;
				} else {
					thread->sleepTime -= msec;
				}

				if (thread->sleepTime == 0)
					thread->flags &= ~kTFlagWaiting;
				break;
			}
		}

		if (!(thread->flags & kTFlagWaiting))
			SThreadRun(thread, STHREAD_TIMESLICE);

		walk_p = next_p;
	}

	return SUCCESS;
}

void Script::SThreadCompleteThread(void) {
	for (int i = 0; i < 40 && (ys_dll_head(threadList()) != NULL); i++)
		SThreadExecThreads(0);
}

int Script::SThreadSetEntrypoint(SCRIPT_THREAD *thread, int ep_num) {
	SCRIPT_BYTECODE *bytecode;
	int max_entrypoint;

	assert(isInitialized());

	bytecode = currentScript()->bytecode;
	max_entrypoint = bytecode->n_entrypoints;

	if ((ep_num < 0) || (ep_num >= max_entrypoint)) {
		return FAILURE;
	}

	thread->ep_num = ep_num;
	thread->ep_offset = bytecode->entrypoints[ep_num].offset;

	return SUCCESS;
}

int Script::SThreadExecute(SCRIPT_THREAD *thread, int ep_num) {
	assert(isInitialized());

	if ((currentScript() == NULL) || (!currentScript()->loaded)) {
		return FAILURE;
	}

	SThreadSetEntrypoint(thread, ep_num);

	thread->i_offset = thread->ep_offset;
	thread->flags = kTFlagNone;

	return SUCCESS;
}

void Script::SThreadAbortAll(void) {
	// TODO: stop current speech

	if (_abortEnabled) 
		_skipSpeeches = true;

	for (int i = 0; i < 10; i++)
		_vm->_script->SThreadExecThreads(0);
}

unsigned char *Script::SThreadGetReadPtr(SCRIPT_THREAD *thread) {
	return currentScript()->bytecode->bytecode_p + thread->i_offset;
}

unsigned long Script::SThreadGetReadOffset(const byte *read_p) {
	return (unsigned long)(read_p - (unsigned char *)currentScript()->bytecode->bytecode_p);
}

size_t Script::SThreadGetReadLen(SCRIPT_THREAD *thread) {
	return currentScript()->bytecode->bytecode_len - thread->i_offset;
}


int Script::SThreadHoldSem(SEMAPHORE *sem) {
	if (sem == NULL) {
		return FAILURE;
	}

	sem->hold_count++;

	return SUCCESS;
}

int Script::SThreadReleaseSem(SEMAPHORE *sem) {
	if (sem == NULL) {
		return FAILURE;
	}

	sem->hold_count--;
	if (sem->hold_count < 0) {
		sem->hold_count = 0;
	}

	return SUCCESS;
}

int Script::SThreadDebugStep() {
	if (_dbg_singlestep) {
		_dbg_dostep = 1;
	}

	return SUCCESS;
}

int Script::SThreadRun(SCRIPT_THREAD *thread, int instr_limit) {
	int instr_count;
	uint32 saved_offset;
	SDataWord_T param1;
	SDataWord_T param2;
	long iparam1;
	long iparam2;
	long iresult;

	SDataWord_T data;
	SDataWord_T scriptRetVal = 0;
	int debug_print = 0;
	int n_buf;
	int bitstate;
	int in_char;
	int i;
	int unhandled = 0;

	// Handle debug single-stepping
	if ((thread == _dbg_thread) && _dbg_singlestep) {
		if (_dbg_dostep) {
			debug_print = 1;
			thread->sleepTime = 0;
			instr_limit = 1;
			_dbg_dostep = 0;
		} else {
			return SUCCESS;
		}
	}

	MemoryReadStream scriptS(currentScript()->bytecode->bytecode_p, currentScript()->bytecode->bytecode_len);

	dataBuffer(2)->len = currentScript()->bytecode->bytecode_len / sizeof(SDataWord_T);
	dataBuffer(2)->data = (SDataWord_T *) currentScript()->bytecode->bytecode_p;

	scriptS.seek(thread->i_offset);

	for (instr_count = 0; instr_count < instr_limit; instr_count++) {
		if (thread->sem.hold_count)
			break;                                                  

		saved_offset = thread->i_offset;
		in_char = scriptS.readByte();

		debug(1, "Executing thread offset: %lu (%x) stack: %d", thread->i_offset, in_char, thread->stackSize());

		switch (in_char) {
		case 0x01: // nextblock
			// Some sort of "jump to the start of the next memory
			// page" instruction, I think.
			thread->i_offset = 1024 * ((thread->i_offset / 1024) + 1);
			break;

// STACK INSTRUCTIONS
		case 0x02: // Dup top element (DUP)
			thread->push(thread->stackTop());
			break;
		case 0x03: // Pop nothing (POPN)
			thread->pop();
			break;
		case 0x04: // Push false (PSHF)
			thread->push(0);
			break;
		case 0x05: // Push true (PSHT)
			thread->push(1);
			break;
		case 0x06: // Push word (PUSH)
		case 0x08: // Push word (PSHD) (dialogue string index)
			param1 = (SDataWord_T)scriptS.readUint16LE();
			thread->push(param1);
			break;

// DATA INSTRUCTIONS  

		case 0x0B: // Test flag (TSTF)
			n_buf = scriptS.readByte();
			param1 = (SDataWord_T)scriptS.readUint16LE();
			_vm->_sdata->getBit(n_buf, param1, &bitstate);
			thread->push(bitstate);
			break;
		case 0x0C: // Get word (GETW)
			n_buf = scriptS.readByte();
			param1 = scriptS.readUint16LE();
			_vm->_sdata->getWord(n_buf, param1, &data);
			thread->push(data);
			break;
		case 0x0F: // Modify flag (MODF)
			n_buf = scriptS.readByte();
			param1 = (SDataWord_T)scriptS.readUint16LE();
			bitstate = _vm->_sdata->readWordU(param1);
			data = thread->stackTop();
			if (bitstate) {
				_vm->_sdata->setBit(n_buf, data, 1);
			} else {
				_vm->_sdata->setBit(n_buf, data, 0);
			}
			break;
		case 0x10: // Put word (PUTW)
			n_buf = scriptS.readByte();
			param1 = (SDataWord_T)scriptS.readUint16LE();
			data = thread->stackTop();
			_vm->_sdata->putWord(n_buf, param1, data);
			break;
		case 0x13: // Modify flag and pop (MDFP)
			n_buf = scriptS.readByte();
			param1 = (SDataWord_T)scriptS.readUint16LE();
			param1 = thread->pop();
			bitstate = _vm->_sdata->readWordU(param1);
			if (bitstate) {
				_vm->_sdata->setBit(n_buf, param1, 1);
			} else {
				_vm->_sdata->setBit(n_buf, param1, 0);
			}
			break;
		case 0x14: // Put word and pop (PTWP)
			n_buf = scriptS.readByte();
			param1 = (SDataWord_T)scriptS.readUint16LE();
			data = thread->stackTop();
			_vm->_sdata->putWord(n_buf, param1, data);
			break;

// CONTROL INSTRUCTIONS    

		case 0x17: // (GOSB): Call subscript
			{
				int n_args;
				int temp;

				n_args = scriptS.readByte();
				temp = scriptS.readByte();
				if (temp != 2)
					error("Calling dynamically generated script? Wow");
				param1 = (SDataWord_T)scriptS.readUint16LE();
				data = scriptS.pos();
				thread->push(n_args);
				// NOTE: The original pushes the program
				// counter as a pointer here. But I don't think
				// we will have to do that.
				thread->push(data);
				thread->i_offset = (unsigned long)param1;
			}
			break;
		case 0x18: // (CALL): Call function
		case 0x19: // (CALL_V): Call function and discard return value
			{
				int n_args;
				uint16 func_num;
				int sfuncRetVal;
				SFunc_T sfunc;

				n_args = scriptS.readByte();
				func_num = scriptS.readUint16LE();
				if (func_num >= SFUNC_NUM) {
					_vm->_console->print(S_ERROR_PREFIX "Invalid script function number: (%X)\n", func_num);
					thread->flags |= kTFlagAborted;
					break;
				}

				sfunc = _SFuncList[func_num].sfunc_fp;
				if (sfunc == NULL) {
					_vm->_console->print(S_WARN_PREFIX "%X: Undefined script function number: (%X)\n",
							thread->i_offset, func_num);
					_vm->_console->print(S_WARN_PREFIX "Removing %d operand(s) from stack.\n", n_args);
					for (i = 0; i < n_args; i++) {
						thread->pop();
					}
				} else {
					sfuncRetVal = (this->*sfunc)(thread);
					if (sfuncRetVal != SUCCESS) {
						_vm->_console->print(S_WARN_PREFIX "%X: Script function %d failed.\n", thread->i_offset, func_num);
					}

					if (func_num == 16) { // SF_gotoScene
						instr_count = instr_limit; // break the loop
						break;
					}

					if (in_char == 0x18) // CALL function
						thread->push(thread->retVal);

					if (thread->flags & kTFlagAsleep)
						instr_count = instr_limit;	// break out of loop!
				}
			}
			break;
		case 0x1A: // (ENTR) Enter the dragon
			thread->push(thread->framePtr);
			setFramePtr(thread, thread->stackPtr);
			param1 = scriptS.readUint16LE();
			thread->stackPtr -= (param1 / 2);
			break;
		case 0x1B: // Return with value
			scriptRetVal = thread->pop();
			// FALL THROUGH
		case 0x1C: // Return with void
			thread->stackPtr = thread->framePtr;
			setFramePtr(thread, thread->pop());
			if (thread->stackSize() == 0) {
				_vm->_console->print("Script execution complete.");
				thread->flags |= kTFlagFinished;
			} else {
				thread->i_offset = thread->pop();
				/* int n_args = */ thread->pop();
				if (in_char == 0x1B)
					thread->push(scriptRetVal);
			}
			break;

// BRANCH INSTRUCTIONS    

			// (JMP): Unconditional jump
		case 0x1D:
			param1 = scriptS.readUint16LE();
			thread->i_offset = (unsigned long)param1;
			break;
			// (JNZP): Jump if nonzero + POP
		case 0x1E:
			param1 = scriptS.readUint16LE();
			data = thread->pop();
			if (data) {
				thread->i_offset = (unsigned long)param1;
			}
			break;
			// (JZP): Jump if zero + POP
		case 0x1F:
			param1 = scriptS.readUint16LE();
			data = thread->pop();
			if (!data) {
				thread->i_offset = (unsigned long)param1;
			}
			break;
			// (JNZ): Jump if nonzero
		case 0x20:
			param1 = scriptS.readUint16LE();
			data = thread->stackTop();
			if (data) {
				thread->i_offset = (unsigned long)param1;
			}
			break;
			// (JZ): Jump if zero
		case 0x21:
			param1 = scriptS.readUint16LE();
			data = thread->stackTop();
			if (!data) {
				thread->i_offset = (unsigned long)param1;
			}
			break;
			// (SWCH): Switch
		case 0x22:
			{
				int n_switch;
				unsigned int switch_num;
				unsigned int switch_jmp;
				unsigned int default_jmp;
				int case_found = 0;

				data = thread->pop();
				n_switch = scriptS.readUint16LE();
				for (i = 0; i < n_switch; i++) {
					switch_num = scriptS.readUint16LE();
					switch_jmp = scriptS.readUint16LE();
					// Found the specified case
					if (data == (SDataWord_T) switch_num) {
						thread->i_offset = switch_jmp;
						case_found = 1;
						break;
					}
				}

				// Jump to default case
				if (!case_found) {
					default_jmp = scriptS.readUint16LE();
					thread->i_offset = default_jmp;
				}
			}
			break;
			// (RJMP): Random branch
		case 0x24:
			{
				int n_branch;
				unsigned int branch_wt;
				unsigned int branch_jmp;
				int rand_sel = 0;
				int branch_found = 0;

				// Ignored?
				scriptS.readUint16LE();
				n_branch = scriptS.readUint16LE();
				for (i = 0; i < n_branch; i++) {
					branch_wt = scriptS.readUint16LE();
					branch_jmp = scriptS.readUint16LE();
					if (rand_sel == i) {
						thread->i_offset = branch_jmp;
						branch_found = 1;
						break;
					}
				}
				if (!branch_found) {
					_vm->_console->print(S_ERROR_PREFIX "%X: Random jump target out of " "bounds.", thread->i_offset);
				}
			}
			break;

// UNARY INSTRUCTIONS

			// (NEG) Negate stack by 2's complement
		case 0x25:
			data = thread->pop();
			data = ~data;
			data++;
			thread->push(data);
			break;
			// (TSTZ) Test for zero
		case 0x26:
			data = thread->pop();
			data = data ? 0 : 1;
			thread->push(data);
			break;
			// (NOT) Binary not
		case 0x27:
			data = thread->pop();
			data = ~data;
			thread->push(data);
			break;
		case 0x28: // inc_v increment, don't push
			unhandled = 1;
			printf("??? ");
			scriptS.readByte();
			scriptS.readUint16LE();
			break;
		case 0x29: // dec_v decrement, don't push
			unhandled = 1;
			printf("??? ");
			scriptS.readByte();
			scriptS.readUint16LE();
			break;
		case 0x2A: // postinc
			unhandled = 1;
			printf("??? ");
			scriptS.readByte();
			scriptS.readUint16LE();
			break;
		case 0x2B: // postdec
			unhandled = 1;
			printf("??? ");
			scriptS.readByte();
			scriptS.readUint16LE();
			break;

// ARITHMETIC INSTRUCTIONS    

			// (ADD): Addition
		case 0x2C:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			iresult = iparam1 + iparam2;
			thread->push((SDataWord_T) iresult);
			break;
			// (SUB): Subtraction
		case 0x2D:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			iresult = iparam1 - iparam2;
			thread->push((SDataWord_T) iresult);
			break;
			// (MULT): Integer multiplication
		case 0x2E:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			iresult = iparam1 * iparam2;
			thread->push((SDataWord_T) iresult);
			break;
			// (DIV): Integer division
		case 0x2F:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			iresult = iparam1 / iparam2;
			thread->push((SDataWord_T) iresult);
			break;
			// (MOD) Modulus
		case 0x30:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			iresult = iparam1 % iparam2;
			thread->push((SDataWord_T) iresult);
			break;
			// (EQU) Test equality
		case 0x33:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			data = (iparam1 == iparam2) ? 1 : 0;
			thread->push(data);
			break;
			// (NEQU) Test inequality
		case 0x34:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			data = (iparam1 != iparam2) ? 1 : 0;
			thread->push(data);
			break;
			// (GRT) Test Greater-than
		case 0x35:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			data = (iparam1 > iparam2) ? 1 : 0;
			thread->push(data);
			break;
			// (LST) Test Less-than
		case 0x36:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			data = (iparam1 < iparam2) ? 1 : 0;
			thread->push(data);
			break;
			// (GRTE) Test Greater-than or Equal to
		case 0x37:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			data = (iparam1 >= iparam2) ? 1 : 0;
			thread->push(data);
			break;
			// (LSTE) Test Less-than or Equal to
		case 0x38:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			iparam1 = (long)param1;
			data = (iparam1 <= iparam2) ? 1 : 0;
			thread->push(data);
			break;

// BITWISE INSTRUCTIONS   

			// (SHR): Arithmetic binary shift right
		case 0x3F:
			param2 = thread->pop();
			param1 = thread->pop();
			iparam2 = (long)param2;
			// Preserve most significant bit
			data = (0x01 << ((sizeof param1 * CHAR_BIT) - 1)) & param1;
			for (i = 0; i < (int)iparam2; i++) {
				param1 >>= 1;
				param1 |= data;
			}
			thread->push(param1);
			break;
			// (SHL) Binary shift left
		case 0x40:
			param2 = thread->pop();
			param1 = thread->pop();
			param1 <<= param2;
			thread->push(param1);
			break;
			// (AND) Binary AND
		case 0x41:
			param2 = thread->pop();
			param1 = thread->pop();
			param1 &= param2;
			thread->push(param1);
			break;
			// (OR) Binary OR
		case 0x42:
			param2 = thread->pop();
			param1 = thread->pop();
			param1 |= param2;
			thread->push(param1);
			break;
			// (XOR) Binary XOR
		case 0x43:
			param2 = thread->pop();
			param1 = thread->pop();
			param1 ^= param2;
			thread->push(param1);
			break;

// BOOLEAN LOGIC INSTRUCTIONS     

			// (LAND): Logical AND
		case 0x44:
			param2 = thread->pop();
			param1 = thread->pop();
			data = (param1 && param2) ? 1 : 0;
			thread->push(data);
			break;
			// (LOR): Logical OR
		case 0x45:
			param2 = thread->pop();
			param1 = thread->pop();
			data = (param1 || param2) ? 1 : 0;
			thread->push(data);
			break;
			// (LXOR): Logical XOR
		case 0x46:
			param2 = thread->pop();
			param1 = thread->pop();
			data = ((param1) ? !(param2) : !!(param2));
			thread->push(data);
			break;

// GAME INSTRUCTIONS  

			// (DLGP): Play Character Dialogue
		case 0x53:
			{
				int n_voices;
				int a_index;
				int voice_rn;

				n_voices = scriptS.readByte();
				param1 = (SDataWord_T) scriptS.readUint16LE();
				// ignored ?
				scriptS.readByte();
				scriptS.readUint16LE();

				a_index = _vm->_actor->getActorIndex(param1);
				if (a_index < 0) {
					_vm->_console->print(S_WARN_PREFIX "%X: DLGP Actor id not found.", thread->i_offset);
				}

				for (i = 0; i < n_voices; i++) {
					data = thread->pop();
					if (a_index < 0)
						continue;
					if (!isVoiceLUTPresent()) {
						voice_rn = -1;
					} else {
						voice_rn = currentScript()->voice->voices[data];
					}
					_vm->_actor->speak(a_index, currentScript()->diag->str[data], voice_rn, &thread->sem);
				}
			}
			break;
			// (DLGS): Initialize dialogue interface
		case 0x54:
			warning("dialog_begin opcode: stub");
			break;
			// (DLGX): Run dialogue interface
		case 0x55:
			warning("dialog_end opcode: stub");
			break;
			// (DLGO): Add a dialogue option to interface
		case 0x56:
			{
				printf("DLGO | ");
				param1 = scriptS.readByte();
				param2 = scriptS.readByte();
				printf("%02X %02X ", param1, param2);

				if (param2 > 0) {
					SDataWord_T param3 = scriptS.readUint16LE();
					printf("%04X", param3);
				}
			}
			break;
		case 0x57: // animate
			scriptS.readUint16LE();
			scriptS.readUint16LE();
			iparam1 = (long)scriptS.readByte();
			thread->i_offset += iparam1;
			break;

// End instruction list

		default:

			_vm->_console->print(S_ERROR_PREFIX "%X: Invalid opcode encountered: " "(%X).\n", thread->i_offset, in_char);
			thread->flags |= kTFlagAborted;
			break;
		}

		// Set instruction offset only if a previous instruction didn't branch
		if (saved_offset == thread->i_offset) {
			thread->i_offset = scriptS.pos();
		} else {
			scriptS.seek(thread->i_offset);
		}
		if (unhandled) {
			_vm->_console->print(S_ERROR_PREFIX "%X: Unhandled opcode.\n", thread->i_offset);
			thread->flags |= kTFlagAborted;
		}
		if ((thread->flags == kTFlagNone) && debug_print) {
			SDebugPrintInstr(thread);
		}
	}

	return SUCCESS;
}

} // End of namespace Saga