2004-04-12 21:40:49 +00:00
|
|
|
/* 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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Scripting module thread management component
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
|
|
|
#include "saga/yslib.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/gfx.h"
|
|
|
|
#include "saga/actor.h"
|
2004-08-10 18:31:33 +00:00
|
|
|
#include "saga/console.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-12 21:39:11 +00:00
|
|
|
#include "saga/script.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/sdata.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2004-10-09 07:39:46 +00:00
|
|
|
void Script::setFramePtr(R_SCRIPT_THREAD *thread, int newPtr) {
|
|
|
|
thread->framePtr = newPtr;
|
|
|
|
dataBuffer(3)->len = 2 * (ARRAYSIZE(thread->stackBuf) - thread->framePtr);
|
|
|
|
dataBuffer(3)->data = (SDataWord_T *) &(thread->stackBuf[newPtr]);
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
R_SCRIPT_THREAD *Script::SThreadCreate() {
|
2004-04-12 21:40:49 +00:00
|
|
|
YS_DL_NODE *new_node;
|
|
|
|
R_SCRIPT_THREAD *new_thread;
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
if (!isInitialized()) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_thread = (R_SCRIPT_THREAD *)calloc(1, sizeof *new_thread);
|
|
|
|
if (new_thread == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-09-23 06:46:44 +00:00
|
|
|
new_thread->stackPtr = ARRAYSIZE(new_thread->stackBuf) - 1;
|
2004-10-09 07:39:46 +00:00
|
|
|
setFramePtr(new_thread, new_thread->stackPtr);
|
|
|
|
|
|
|
|
dataBuffer(4)->len = sizeof(new_thread->threadVars);
|
|
|
|
dataBuffer(4)->data = new_thread->threadVars;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
new_node = ys_dll_add_head(threadList(), new_thread, sizeof *new_thread);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
free(new_thread);
|
|
|
|
|
|
|
|
return (R_SCRIPT_THREAD *)ys_dll_get_data(new_node);
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadDestroy(R_SCRIPT_THREAD *thread) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (thread == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadExecThreads(int msec) {
|
2004-04-12 21:40:49 +00:00
|
|
|
YS_DL_NODE *walk_p;
|
|
|
|
R_SCRIPT_THREAD *thread;
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
if (!isInitialized()) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
for (walk_p = ys_dll_head(threadList()); walk_p != NULL; walk_p = ys_dll_next(walk_p)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
thread = (R_SCRIPT_THREAD *)ys_dll_get_data(walk_p);
|
|
|
|
if (thread->executing) {
|
2004-08-12 23:57:45 +00:00
|
|
|
SThreadRun(thread, STHREAD_DEF_INSTR_COUNT, msec);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
void Script::SThreadCompleteThread(void) {
|
|
|
|
for (int i = 0; i < 40 && (ys_dll_head(threadList()) != NULL); i++)
|
|
|
|
SThreadExecThreads(0);
|
2004-08-12 01:11:11 +00:00
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadSetEntrypoint(R_SCRIPT_THREAD *thread, int ep_num) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_SCRIPT_BYTECODE *bytecode;
|
|
|
|
int max_entrypoint;
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
assert(isInitialized());
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
bytecode = currentScript()->bytecode;
|
2004-04-12 21:40:49 +00:00
|
|
|
max_entrypoint = bytecode->n_entrypoints;
|
|
|
|
|
|
|
|
if ((ep_num < 0) || (ep_num >= max_entrypoint)) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->ep_num = ep_num;
|
|
|
|
thread->ep_offset = bytecode->entrypoints[ep_num].offset;
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadExecute(R_SCRIPT_THREAD *thread, int ep_num) {
|
|
|
|
assert(isInitialized());
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
if ((currentScript() == NULL) || (!currentScript()->loaded)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
SThreadSetEntrypoint(thread, ep_num);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
thread->i_offset = thread->ep_offset;
|
|
|
|
thread->executing = 1;
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
unsigned char *Script::SThreadGetReadPtr(R_SCRIPT_THREAD *thread) {
|
|
|
|
return currentScript()->bytecode->bytecode_p + thread->i_offset;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
unsigned long Script::SThreadGetReadOffset(const byte *read_p) {
|
|
|
|
return (unsigned long)(read_p - (unsigned char *)currentScript()->bytecode->bytecode_p);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
size_t Script::SThreadGetReadLen(R_SCRIPT_THREAD *thread) {
|
|
|
|
return currentScript()->bytecode->bytecode_len - thread->i_offset;
|
2004-05-04 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadHoldSem(R_SEMAPHORE *sem) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (sem == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sem->hold_count++;
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadReleaseSem(R_SEMAPHORE *sem) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (sem == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sem->hold_count--;
|
|
|
|
if (sem->hold_count < 0) {
|
|
|
|
sem->hold_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadDebugStep() {
|
|
|
|
if (_dbg_singlestep) {
|
|
|
|
_dbg_dostep = 1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadRun(R_SCRIPT_THREAD *thread, int instr_limit, int msec) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int instr_count;
|
2004-04-30 23:02:23 +00:00
|
|
|
uint32 saved_offset;
|
2004-04-12 21:40:49 +00:00
|
|
|
SDataWord_T param1;
|
|
|
|
SDataWord_T param2;
|
|
|
|
long iparam1;
|
|
|
|
long iparam2;
|
|
|
|
long iresult;
|
|
|
|
|
|
|
|
SDataWord_T data;
|
2004-10-03 17:11:23 +00:00
|
|
|
SDataWord_T scriptRetVal = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
int debug_print = 0;
|
|
|
|
int n_buf;
|
|
|
|
int bitstate;
|
|
|
|
int in_char;
|
|
|
|
int i;
|
|
|
|
int unhandled = 0;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Handle debug single-stepping
|
2004-08-12 23:57:45 +00:00
|
|
|
if ((thread == _dbg_thread) && _dbg_singlestep) {
|
|
|
|
if (_dbg_dostep) {
|
2004-04-12 21:40:49 +00:00
|
|
|
debug_print = 1;
|
|
|
|
thread->sleep_time = 0;
|
|
|
|
instr_limit = 1;
|
2004-08-12 23:57:45 +00:00
|
|
|
_dbg_dostep = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-19 14:49:00 +00:00
|
|
|
MemoryReadStream scriptS(currentScript()->bytecode->bytecode_p, currentScript()->bytecode->bytecode_len);
|
|
|
|
|
2004-10-09 07:39:46 +00:00
|
|
|
dataBuffer(2)->len = currentScript()->bytecode->bytecode_len;
|
|
|
|
dataBuffer(2)->data = (SDataWord_T *) currentScript()->bytecode->bytecode_p;
|
|
|
|
|
2004-09-19 14:49:00 +00:00
|
|
|
scriptS.seek(thread->i_offset);
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
for (instr_count = 0; instr_count < instr_limit; instr_count++) {
|
|
|
|
if ((!thread->executing) || (thread->sem.hold_count)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->sleep_time -= msec;
|
|
|
|
if (thread->sleep_time < 0) {
|
|
|
|
thread->sleep_time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread->sleep_time) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_offset = thread->i_offset;
|
2004-08-22 18:28:42 +00:00
|
|
|
in_char = scriptS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-19 16:51:26 +00:00
|
|
|
debug(1, "Executing thread offset: %lu (%x) stack: %d", thread->i_offset, in_char, thread->stackSize());
|
2004-08-12 01:11:11 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
switch (in_char) {
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x01: // nextblock
|
2004-09-21 06:35:00 +00:00
|
|
|
// Some sort of "jump to the start of the next memory
|
|
|
|
// page" instruction, I think.
|
|
|
|
thread->i_offset = 1024 * ((thread->i_offset / 1024) + 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// STACK INSTRUCTIONS
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x02: // Dup top element (DUP)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(thread->stackTop());
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x03: // Pop nothing (POPN)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x04: // Push false (PSHF)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(0);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x05: // Push true (PSHT)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x06: // Push word (PUSH)
|
|
|
|
case 0x08: // Push word (PSHD) (dialogue string index)
|
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// DATA INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x0B: // Test flag (TSTF)
|
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->getBit(n_buf, param1, &bitstate);
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(bitstate);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x0C: // Get word (GETW)
|
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = scriptS.readUint16LE();
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->getWord(n_buf, param1, &data);
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x0F: // Modify flag (MODF)
|
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
2004-08-01 19:53:29 +00:00
|
|
|
bitstate = _vm->_sdata->readWordU(param1);
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (bitstate) {
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->setBit(n_buf, data, 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->setBit(n_buf, data, 0);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x10: // Put word (PUTW)
|
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->putWord(n_buf, param1, data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x13: // Modify flag and pop (MDFP)
|
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
param1 = thread->pop();
|
2004-08-01 19:53:29 +00:00
|
|
|
bitstate = _vm->_sdata->readWordU(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (bitstate) {
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->setBit(n_buf, param1, 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->setBit(n_buf, param1, 0);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x14: // Put word and pop (PTWP)
|
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-08-01 19:53:29 +00:00
|
|
|
_vm->_sdata->putWord(n_buf, param1, data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// CONTROL INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-03 17:11:23 +00:00
|
|
|
case 0x17: // (GOSB): Call subscript
|
2004-04-12 21:40:49 +00:00
|
|
|
{
|
2004-09-21 06:35:00 +00:00
|
|
|
int n_args;
|
2004-04-12 21:40:49 +00:00
|
|
|
int temp;
|
|
|
|
|
2004-09-21 06:35:00 +00:00
|
|
|
n_args = scriptS.readByte();
|
2004-08-22 18:28:42 +00:00
|
|
|
temp = scriptS.readByte();
|
2004-09-21 06:35:00 +00:00
|
|
|
if (temp != 2)
|
|
|
|
error("Calling dynamically generated script? Wow");
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = (SDataWord_T)scriptS.readUint16LE();
|
|
|
|
data = scriptS.pos();
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(n_args);
|
2004-09-21 06:35:00 +00:00
|
|
|
// NOTE: The original pushes the program
|
|
|
|
// counter as a pointer here. But I don't think
|
|
|
|
// we will have to do that.
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->i_offset = (unsigned long)param1;
|
|
|
|
}
|
|
|
|
break;
|
2004-10-03 17:11:23 +00:00
|
|
|
case 0x18: // (CALL): Call function
|
|
|
|
case 0x19: // (CALL_V): Call function and discard return value
|
2004-04-12 21:40:49 +00:00
|
|
|
{
|
|
|
|
int n_args;
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 func_num;
|
2004-10-03 17:11:23 +00:00
|
|
|
int sfuncRetVal;
|
2004-04-12 21:40:49 +00:00
|
|
|
SFunc_T sfunc;
|
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
n_args = scriptS.readByte();
|
|
|
|
func_num = scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (func_num >= R_SFUNC_NUM) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_ERROR_PREFIX "Invalid script function number: (%X)\n", func_num);
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->executing = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
sfunc = _SFuncList[func_num].sfunc_fp;
|
2004-04-12 21:40:49 +00:00
|
|
|
if (sfunc == NULL) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_WARN_PREFIX "%X: Undefined script function number: (%X)\n",
|
2004-05-01 16:15:55 +00:00
|
|
|
thread->i_offset, func_num);
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_WARN_PREFIX "Removing %d operand(s) from stack.\n", n_args);
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < n_args; i++) {
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-10-03 17:11:23 +00:00
|
|
|
sfuncRetVal = (this->*sfunc)(thread);
|
|
|
|
if (sfuncRetVal != R_SUCCESS) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_WARN_PREFIX "%X: Script function %d failed.\n", thread->i_offset, func_num);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-10-03 17:11:23 +00:00
|
|
|
|
|
|
|
if (in_char == 0x18)
|
|
|
|
thread->push(thread->retVal);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-08-22 18:28:42 +00:00
|
|
|
case 0x1A: // (ENTR) Enter the dragon
|
2004-10-03 17:11:23 +00:00
|
|
|
thread->push(thread->framePtr);
|
2004-10-09 07:39:46 +00:00
|
|
|
setFramePtr(thread, thread->stackPtr);
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-10-03 17:11:23 +00:00
|
|
|
thread->stackPtr -= (param1 / 2);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x1B: // Return with value
|
2004-10-03 17:11:23 +00:00
|
|
|
scriptRetVal = thread->pop();
|
|
|
|
// FALL THROUGH
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x1C: // Return with void
|
2004-10-03 17:11:23 +00:00
|
|
|
thread->stackPtr = thread->framePtr;
|
2004-10-09 07:39:46 +00:00
|
|
|
setFramePtr(thread, thread->pop());
|
2004-09-23 06:46:44 +00:00
|
|
|
if (thread->stackSize() == 0) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print("Script execution complete.");
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->executing = 0;
|
|
|
|
} else {
|
2004-10-03 17:11:23 +00:00
|
|
|
thread->i_offset = thread->pop();
|
2004-09-23 06:46:44 +00:00
|
|
|
/* int n_args = */ thread->pop();
|
2004-10-03 17:11:23 +00:00
|
|
|
if (in_char == 0x1B)
|
|
|
|
thread->push(scriptRetVal);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// BRANCH INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JMP): Unconditional jump
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x1D:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->i_offset = (unsigned long)param1;
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JNZP): Jump if nonzero + POP
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x1E:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (data) {
|
|
|
|
thread->i_offset = (unsigned long)param1;
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JZP): Jump if zero + POP
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x1F:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!data) {
|
|
|
|
thread->i_offset = (unsigned long)param1;
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JNZ): Jump if nonzero
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x20:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (data) {
|
|
|
|
thread->i_offset = (unsigned long)param1;
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JZ): Jump if zero
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x21:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!data) {
|
|
|
|
thread->i_offset = (unsigned long)param1;
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SWCH): Switch
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x22:
|
|
|
|
{
|
|
|
|
int n_switch;
|
|
|
|
unsigned int switch_num;
|
|
|
|
unsigned int switch_jmp;
|
|
|
|
unsigned int default_jmp;
|
|
|
|
int case_found = 0;
|
|
|
|
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-08-22 18:28:42 +00:00
|
|
|
n_switch = scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < n_switch; i++) {
|
2004-08-22 18:28:42 +00:00
|
|
|
switch_num = scriptS.readUint16LE();
|
|
|
|
switch_jmp = scriptS.readUint16LE();
|
2004-05-01 16:15:55 +00:00
|
|
|
// Found the specified case
|
2004-04-12 21:40:49 +00:00
|
|
|
if (data == (SDataWord_T) switch_num) {
|
|
|
|
thread->i_offset = switch_jmp;
|
|
|
|
case_found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Jump to default case
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!case_found) {
|
2004-08-22 18:28:42 +00:00
|
|
|
default_jmp = scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->i_offset = default_jmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (RJMP): Random branch
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x24:
|
|
|
|
{
|
|
|
|
int n_branch;
|
|
|
|
unsigned int branch_wt;
|
|
|
|
unsigned int branch_jmp;
|
|
|
|
int rand_sel = 0;
|
|
|
|
int branch_found = 0;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Ignored?
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readUint16LE();
|
|
|
|
n_branch = scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < n_branch; i++) {
|
2004-08-22 18:28:42 +00:00
|
|
|
branch_wt = scriptS.readUint16LE();
|
|
|
|
branch_jmp = scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (rand_sel == i) {
|
|
|
|
thread->i_offset = branch_jmp;
|
|
|
|
branch_found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!branch_found) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_ERROR_PREFIX "%X: Random jump target out of " "bounds.", thread->i_offset);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-08-26 23:28:10 +00:00
|
|
|
// UNARY INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (NEG) Negate stack by 2's complement
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x25:
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = ~data;
|
|
|
|
data++;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (TSTZ) Test for zero
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x26:
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = data ? 0 : 1;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (NOT) Binary not
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x27:
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = ~data;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x28: // inc_v increment, don't push
|
2004-04-12 21:40:49 +00:00
|
|
|
unhandled = 1;
|
|
|
|
printf("??? ");
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readByte();
|
|
|
|
scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x29: // dec_v decrement, don't push
|
2004-04-12 21:40:49 +00:00
|
|
|
unhandled = 1;
|
|
|
|
printf("??? ");
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readByte();
|
|
|
|
scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x2A: // postinc
|
2004-04-12 21:40:49 +00:00
|
|
|
unhandled = 1;
|
|
|
|
printf("??? ");
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readByte();
|
|
|
|
scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x2B: // postdec
|
2004-04-12 21:40:49 +00:00
|
|
|
unhandled = 1;
|
|
|
|
printf("??? ");
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readByte();
|
|
|
|
scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// ARITHMETIC INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (ADD): Addition
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2C:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 + iparam2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push((SDataWord_T) iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SUB): Subtraction
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2D:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 - iparam2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push((SDataWord_T) iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (MULT): Integer multiplication
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2E:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 * iparam2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push((SDataWord_T) iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-13 01:41:23 +00:00
|
|
|
// (DIV): Integer division
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2F:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 / iparam2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push((SDataWord_T) iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (MOD) Modulus
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x30:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 % iparam2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push((SDataWord_T) iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (EQU) Test equality
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x33:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 == iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (NEQU) Test inequality
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x34:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 != iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (GRT) Test Greater-than
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x35:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 > iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LST) Test Less-than
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x36:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 < iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (GRTE) Test Greater-than or Equal to
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x37:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 >= iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LSTE) Test Less-than or Equal to
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x38:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 <= iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-13 01:41:23 +00:00
|
|
|
|
|
|
|
// BITWISE INSTRUCTIONS
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SHR): Arithmetic binary shift right
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x3F:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
2004-05-01 16:15:55 +00:00
|
|
|
// Preserve most significant bit
|
|
|
|
data = (0x01 << ((sizeof param1 * CHAR_BIT) - 1)) & param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < (int)iparam2; i++) {
|
|
|
|
param1 >>= 1;
|
|
|
|
param1 |= data;
|
|
|
|
}
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SHL) Binary shift left
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x40:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 <<= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (AND) Binary AND
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x41:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 &= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (OR) Binary OR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x42:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 |= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (XOR) Binary XOR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x43:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 ^= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// BOOLEAN LOGIC INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LAND): Logical AND
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x44:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = (param1 && param2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LOR): Logical OR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x45:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = (param1 || param2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LXOR): Logical XOR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x46:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = ((param1) ? !(param2) : !!(param2));
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// GAME INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (DLGP): Play Character Dialogue
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x53:
|
|
|
|
{
|
|
|
|
int n_voices;
|
|
|
|
int a_index;
|
|
|
|
int voice_rn;
|
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
n_voices = scriptS.readByte();
|
|
|
|
param1 = (SDataWord_T) scriptS.readUint16LE();
|
2004-05-01 16:15:55 +00:00
|
|
|
// ignored ?
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readByte();
|
|
|
|
scriptS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 15:47:42 +00:00
|
|
|
a_index = _vm->_actor->getActorIndex(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (a_index < 0) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_WARN_PREFIX "%X: DLGP Actor id not found.", thread->i_offset);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n_voices; i++) {
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (a_index < 0)
|
|
|
|
continue;
|
2004-08-12 23:57:45 +00:00
|
|
|
if (!isVoiceLUTPresent()) {
|
2004-04-12 21:40:49 +00:00
|
|
|
voice_rn = -1;
|
|
|
|
} else {
|
2004-08-12 23:57:45 +00:00
|
|
|
voice_rn = currentScript()->voice->voices[data];
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-10-21 06:49:11 +00:00
|
|
|
_vm->_actor->speak(a_index, currentScript()->diag->str[data], voice_rn, &thread->sem);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (DLGS): Initialize dialogue interface
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x54:
|
2004-08-26 23:28:10 +00:00
|
|
|
warning("dialog_begin opcode: stub");
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (DLGX): Run dialogue interface
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x55:
|
2004-08-26 23:28:10 +00:00
|
|
|
warning("dialog_end opcode: stub");
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (DLGO): Add a dialogue option to interface
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x56:
|
|
|
|
{
|
|
|
|
printf("DLGO | ");
|
2004-10-03 17:11:23 +00:00
|
|
|
param1 = scriptS.readByte();
|
|
|
|
param2 = scriptS.readByte();
|
|
|
|
printf("%02X %02X ", param1, param2);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-03 17:11:23 +00:00
|
|
|
if (param2 > 0) {
|
|
|
|
SDataWord_T param3 = scriptS.readUint16LE();
|
|
|
|
printf("%04X", param3);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x57: // animate
|
|
|
|
scriptS.readUint16LE();
|
|
|
|
scriptS.readUint16LE();
|
|
|
|
iparam1 = (long)scriptS.readByte();
|
|
|
|
thread->i_offset += iparam1;
|
|
|
|
break;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// End instruction list
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_ERROR_PREFIX "%X: Invalid opcode encountered: " "(%X).\n", thread->i_offset, in_char);
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->executing = 0;
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Set instruction offset only if a previous instruction didn't branch
|
2004-04-12 21:40:49 +00:00
|
|
|
if (saved_offset == thread->i_offset) {
|
2004-08-22 18:28:42 +00:00
|
|
|
thread->i_offset = scriptS.pos();
|
2004-09-19 14:49:00 +00:00
|
|
|
} else {
|
|
|
|
scriptS.seek(thread->i_offset);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
if (unhandled) {
|
2004-08-10 18:31:33 +00:00
|
|
|
_vm->_console->print(S_ERROR_PREFIX "%X: Unhandled opcode.\n", thread->i_offset);
|
2004-04-12 21:40:49 +00:00
|
|
|
thread->executing = 0;
|
|
|
|
}
|
|
|
|
if (thread->executing && debug_print) {
|
2004-08-12 23:57:45 +00:00
|
|
|
SDebugPrintInstr(thread);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|
|
|
|
|