2009-02-17 15:02:16 +00:00
/* 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 .
2014-02-18 02:34:24 +01:00
*
2009-02-17 15:02:16 +00:00
* 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 .
2014-02-18 02:34:24 +01:00
*
2009-02-17 15:02:16 +00:00
* 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 .
*
*/
2009-02-15 06:10:59 +00:00
2016-09-30 19:59:05 -05:00
# include "common/config-manager.h"
2009-02-20 20:11:12 +00:00
# include "common/debug.h"
2010-05-04 11:59:22 +00:00
# include "common/debug-channels.h"
2009-02-15 06:10:59 +00:00
2009-05-14 09:12:27 +00:00
# include "sci/sci.h"
2009-07-06 10:39:22 +00:00
# include "sci/console.h"
2010-02-13 17:44:58 +00:00
# include "sci/engine/features.h"
SCI: Improve audio volume & settings sync code
This patch includes enhancements to the ScummVM integration with
SCI engine, with particular focus on SCI32 support.
1. Fixes audio volumes syncing erroneously to ScummVM in games
that modify the audio volume without user action (e.g. SCI1.1
talkies that reduce music volume during speech playback). Now,
volumes will only be synchronised when the user interacts with
the game's audio settings. This mechanism works by looking for
a known volume control object in the stack, and only syncing
when the control object is present. (Ports and planes were
researched and found unreliable.)
2. Fixes audio syncing in SCI32 games that do not set game
volumes through kDoSoundMasterVolume/kDoAudioVolume, like GK1,
GK2, Phant1, and Torin.
3. Fixes speech/subtitles syncing in SCI32 games that do not use
global 90, like LSL6hires.
4. Fixes in-game volume controls in SCI32 games reflecting
outdated audio volumes when a change is made during the game
from the ScummVM launcher.
5. Fixes SCI32 games that would restore volumes from save games
or reset volumes on startup, which caused game volumes to be
out-of-sync with ScummVM when started.
6. ScummVM integration code for audio sync has been abstracted
into a new GuestAdditions class. This keeps the ScummVM-
specific code all in one place, with only small hooks into the
engine code. ScummVM integrated save/load code should probably
also go here in the future.
Fixes Trac#9700.
2017-01-26 13:18:41 -06:00
# include "sci/engine/guest_additions.h"
2009-02-27 02:23:40 +00:00
# include "sci/engine/state.h"
2009-02-24 05:51:55 +00:00
# include "sci/engine/kernel.h"
2010-11-19 08:18:24 +00:00
# include "sci/engine/object.h"
2010-01-29 11:05:06 +00:00
# include "sci/engine/script.h"
2010-06-10 09:18:57 +00:00
# include "sci/engine/seg_manager.h"
# include "sci/engine/selector.h" // for SELECTOR
2009-02-15 08:34:13 +00:00
# include "sci/engine/gc.h"
2010-07-21 14:18:26 +00:00
# include "sci/engine/workarounds.h"
2017-05-26 12:36:20 +02:00
# include "sci/engine/scriptdebug.h"
2009-02-15 06:10:59 +00:00
2009-02-21 10:23:36 +00:00
namespace Sci {
2009-10-02 11:04:36 +00:00
const reg_t NULL_REG = { 0 , 0 } ;
const reg_t SIGNAL_REG = { 0 , SIGNAL_OFFSET } ;
2010-07-01 22:44:36 +00:00
const reg_t TRUE_REG = { 0 , 1 } ;
2011-06-20 00:59:48 +02:00
// Enable the define below to have the VM abort on cases where a conditional
2011-02-22 18:52:44 +02:00
// statement is followed by an unconditional jump (which will most likely lead
2021-02-25 01:09:19 -08:00
// to an infinite loop). Aids in detecting script bugs such as #5172.
2011-02-22 18:52:44 +02:00
//#define ABORT_ON_INFINITE_LOOP
2009-02-15 06:10:59 +00:00
2009-02-20 23:09:29 +00:00
// validation functionality
2009-02-15 06:10:59 +00:00
2010-11-17 17:21:02 +00:00
static reg_t & validate_property ( EngineState * s , Object * obj , int index ) {
2009-10-02 11:04:36 +00:00
// A static dummy reg_t, which we return if obj or index turn out to be
// invalid. Note that we cannot just return NULL_REG, because client code
2010-02-07 17:56:57 +00:00
// may modify the value of the returned reg_t.
2010-06-24 20:10:51 +00:00
static reg_t dummyReg = NULL_REG ;
2009-10-02 11:04:36 +00:00
2010-06-24 11:29:26 +00:00
// If this occurs, it means there's probably something wrong with the garbage
// collector, so don't hide it with fake return values
2010-08-06 15:05:05 +00:00
if ( ! obj )
2010-06-24 18:48:50 +00:00
error ( " validate_property: Sending to disposed object " ) ;
2009-02-15 06:10:59 +00:00
2010-11-17 17:21:02 +00:00
if ( getSciVersion ( ) = = SCI_VERSION_3 )
index = obj - > locateVarSelector ( s - > _segMan , index ) ;
else
index > > = 1 ;
2010-06-24 20:01:31 +00:00
if ( index < 0 | | ( uint ) index > = obj - > getVarCount ( ) ) {
// This is same way sierra does it and there are some games, that contain such scripts like
// iceman script 998 (fred::canBeHere, executed right at the start)
2011-03-04 00:14:46 +02:00
debugC ( kDebugLevelVM , " [VM] Invalid property #%d (out of [0..%d]) requested from object %04x:%04x (%s) " ,
index , obj - > getVarCount ( ) , PRINT_REG ( obj - > getPos ( ) ) , s - > _segMan - > getObjectName ( obj - > getPos ( ) ) ) ;
2010-06-24 20:01:31 +00:00
return dummyReg ;
2009-02-15 06:10:59 +00:00
}
2010-05-26 16:30:10 +00:00
return obj - > getVariableRef ( index ) ;
2009-02-15 06:10:59 +00:00
}
2009-03-12 03:26:47 +00:00
static StackPtr validate_stack_addr ( EngineState * s , StackPtr sp ) {
2009-02-15 06:10:59 +00:00
if ( sp > = s - > stack_base & & sp < s - > stack_top )
return sp ;
2011-03-27 18:13:42 +03:00
else
2010-01-25 01:39:44 +00:00
error ( " [VM] Stack index %d out of valid range [%d..%d] " ,
2009-05-30 15:40:49 +00:00
( int ) ( sp - s - > stack_base ) , 0 , ( int ) ( s - > stack_top - s - > stack_base - 1 ) ) ;
2009-02-15 06:10:59 +00:00
}
2010-07-04 21:31:09 +00:00
static bool validate_variable ( reg_t * r , reg_t * stack_base , int type , int max , int index ) {
2009-02-15 06:10:59 +00:00
const char * names [ 4 ] = { " global " , " local " , " temp " , " param " } ;
if ( index < 0 | | index > = max ) {
2010-11-01 16:02:28 +00:00
Common : : String txt = Common : : String : : format (
2010-01-25 01:39:44 +00:00
" [VM] Attempt to use invalid %s variable %04x " ,
2009-10-12 12:03:06 +00:00
names [ type ] , index ) ;
2009-02-15 06:10:59 +00:00
if ( max = = 0 )
2009-10-02 12:44:12 +00:00
txt + = " (variable type invalid) " ;
else
2010-11-01 16:02:28 +00:00
txt + = Common : : String : : format ( " (out of range [%d..%d]) " , 0 , max - 1 ) ;
2009-06-03 09:45:16 +00:00
2009-02-15 06:10:59 +00:00
if ( type = = VAR_PARAM | | type = = VAR_TEMP ) {
int total_offset = r - stack_base ;
if ( total_offset < 0 | | total_offset > = VM_STACK_SIZE ) {
2010-06-17 23:50:28 +00:00
// Fatal, as the game is trying to do an OOB access
error ( " %s. [VM] Access would be outside even of the stack (%d); access denied " , txt . c_str ( ) , total_offset ) ;
2009-10-30 18:01:27 +00:00
return false ;
2009-02-15 06:10:59 +00:00
} else {
2011-01-01 12:48:12 +00:00
debugC ( kDebugLevelVM , " %s " , txt . c_str ( ) ) ;
debugC ( kDebugLevelVM , " [VM] Access within stack boundaries; access granted. " ) ;
2009-10-30 18:01:27 +00:00
return true ;
2009-02-15 06:10:59 +00:00
}
2009-06-03 09:45:16 +00:00
}
2009-10-30 18:01:27 +00:00
return false ;
2009-02-15 06:10:59 +00:00
}
2009-10-30 18:01:27 +00:00
return true ;
2009-02-15 06:10:59 +00:00
}
2011-03-26 12:25:48 +02:00
static reg_t read_var ( EngineState * s , int type , int index ) {
if ( validate_variable ( s - > variables [ type ] , s - > stack_base , type , s - > variablesMax [ type ] , index ) ) {
2017-01-05 10:29:14 -06:00
if ( s - > variables [ type ] [ index ] . getSegment ( ) = = kUninitializedSegment ) {
2010-07-12 08:38:30 +00:00
switch ( type ) {
case VAR_TEMP : {
// Uninitialized read on a temp
// We need to find correct replacements for each situation manually
2016-08-28 16:20:37 -05:00
SciCallOrigin originReply ;
2010-07-20 17:58:04 +00:00
SciWorkaroundSolution solution = trackOriginAndFindWorkaround ( index , uninitializedReadWorkarounds , & originReply ) ;
2010-10-07 14:40:11 +00:00
if ( solution . type = = WORKAROUND_NONE ) {
# ifdef RELEASE_BUILD
// If we are running an official ScummVM release -> fake 0 in unknown cases
2016-09-26 20:41:37 -05:00
warning ( " Uninitialized read for temp %d from %s " , index , originReply . toString ( ) . c_str ( ) ) ;
2010-10-07 14:57:59 +00:00
2011-03-28 22:45:01 +01:00
s - > variables [ type ] [ index ] = NULL_REG ;
2010-10-07 14:40:11 +00:00
break ;
# else
2016-09-26 20:41:37 -05:00
error ( " Uninitialized read for temp %d from %s " , index , originReply . toString ( ) . c_str ( ) ) ;
2010-10-07 14:40:11 +00:00
# endif
}
2010-07-20 17:58:04 +00:00
assert ( solution . type = = WORKAROUND_FAKE ) ;
2011-03-26 12:25:48 +02:00
s - > variables [ type ] [ index ] = make_reg ( 0 , solution . value ) ;
2010-07-12 08:38:30 +00:00
break ;
}
2016-11-19 12:58:12 -06:00
case VAR_PARAM : {
2010-07-12 08:38:30 +00:00
// Out-of-bounds read for a parameter that goes onto stack and hits an uninitialized temp
2018-08-22 23:46:12 +03:00
// We need to find correct replacements for each situation manually
SciCallOrigin originReply ;
SciWorkaroundSolution solution = trackOriginAndFindWorkaround ( index , uninitializedReadForParamWorkarounds , & originReply ) ;
if ( solution . type = = WORKAROUND_NONE ) {
warning ( " Uninitialized read for parameter %d from %s " , index , originReply . toString ( ) . c_str ( ) ) ;
return NULL_REG ;
} else {
return make_reg ( 0 , solution . value ) ;
}
2016-11-19 12:58:12 -06:00
}
2010-07-12 08:38:30 +00:00
default :
break ;
}
2010-06-24 11:54:33 +00:00
}
2011-03-26 12:25:48 +02:00
return s - > variables [ type ] [ index ] ;
2010-06-24 11:54:33 +00:00
} else
2011-03-26 12:25:48 +02:00
return s - > r_acc ;
2009-02-15 06:10:59 +00:00
}
2011-03-26 12:25:48 +02:00
static void write_var ( EngineState * s , int type , int index , reg_t value ) {
if ( validate_variable ( s - > variables [ type ] , s - > stack_base , type , s - > variablesMax [ type ] , index ) ) {
2009-10-21 08:28:39 +00:00
2009-10-21 10:00:08 +00:00
// WORKAROUND: This code is needed to work around a probable script bug, or a
// limitation of the original SCI engine, which can be observed in LSL5.
//
// In some games, ego walks via the "Grooper" object, in particular its "stopGroop"
// child. In LSL5, during the game, ego is swapped from Larry to Patti. When this
// happens in the original interpreter, the new actor is loaded in the same memory
// location as the old one, therefore the client variable in the stopGroop object
// points to the new actor. This is probably why the reference of the stopGroop
// object is never updated (which is why I mentioned that this is either a script
// bug or some kind of limitation).
//
// In our implementation, each new object is loaded in a different memory location,
// and we can't overwrite the old one. This means that in our implementation,
// whenever ego is changed, we need to update the "client" variable of the
// stopGroop object, which points to ego, to the new ego object. If this is not
// done, ego's movement will not be updated properly, so the result is
// unpredictable (for example in LSL5, Patti spins around instead of walking).
2016-09-26 19:28:51 -05:00
if ( index = = kGlobalVarEgo & & type = = VAR_GLOBAL & & getSciVersion ( ) > SCI_VERSION_0_EARLY ) {
2011-03-26 12:25:48 +02:00
reg_t stopGroopPos = s - > _segMan - > findObjectByName ( " stopGroop " ) ;
2009-10-21 08:28:39 +00:00
if ( ! stopGroopPos . isNull ( ) ) { // does the game have a stopGroop object?
2009-10-22 05:42:14 +00:00
// Find the "client" member variable of the stopGroop object, and update it
2009-10-21 08:28:39 +00:00
ObjVarRef varp ;
2011-03-26 12:25:48 +02:00
if ( lookupSelector ( s - > _segMan , stopGroopPos , SELECTOR ( client ) , & varp , NULL ) = = kSelectorVariable ) {
reg_t * clientVar = varp . getPointer ( s - > _segMan ) ;
2009-10-21 08:28:39 +00:00
* clientVar = value ;
}
}
}
2010-06-25 16:04:37 +00:00
// If we are writing an uninitialized value into a temp, we remove the uninitialized segment
// this happens at least in sq1/room 44 (slot-machine), because a send is missing parameters, then
// those parameters are taken from uninitialized stack and afterwards they are copied back into temps
// if we don't remove the segment, we would get false-positive uninitialized reads later
2017-01-05 10:29:14 -06:00
if ( type = = VAR_TEMP & & value . getSegment ( ) = = kUninitializedSegment )
2012-06-18 05:21:59 +03:00
value . setSegment ( 0 ) ;
2010-06-25 16:04:37 +00:00
2011-03-26 12:25:48 +02:00
s - > variables [ type ] [ index ] = value ;
2010-12-22 13:58:28 +00:00
SCI: Improve audio volume & settings sync code
This patch includes enhancements to the ScummVM integration with
SCI engine, with particular focus on SCI32 support.
1. Fixes audio volumes syncing erroneously to ScummVM in games
that modify the audio volume without user action (e.g. SCI1.1
talkies that reduce music volume during speech playback). Now,
volumes will only be synchronised when the user interacts with
the game's audio settings. This mechanism works by looking for
a known volume control object in the stack, and only syncing
when the control object is present. (Ports and planes were
researched and found unreliable.)
2. Fixes audio syncing in SCI32 games that do not set game
volumes through kDoSoundMasterVolume/kDoAudioVolume, like GK1,
GK2, Phant1, and Torin.
3. Fixes speech/subtitles syncing in SCI32 games that do not use
global 90, like LSL6hires.
4. Fixes in-game volume controls in SCI32 games reflecting
outdated audio volumes when a change is made during the game
from the ScummVM launcher.
5. Fixes SCI32 games that would restore volumes from save games
or reset volumes on startup, which caused game volumes to be
out-of-sync with ScummVM when started.
6. ScummVM integration code for audio sync has been abstracted
into a new GuestAdditions class. This keeps the ScummVM-
specific code all in one place, with only small hooks into the
engine code. ScummVM integrated save/load code should probably
also go here in the future.
Fixes Trac#9700.
2017-01-26 13:18:41 -06:00
g_sci - > _guestAdditions - > writeVarHook ( type , index , value ) ;
2009-10-21 08:28:39 +00:00
}
2009-02-15 06:10:59 +00:00
}
2009-02-20 23:09:29 +00:00
// Operating on the stack
// 16 bit:
2009-02-15 06:10:59 +00:00
# define PUSH(v) PUSH32(make_reg(0, v))
2009-02-20 23:09:29 +00:00
// 32 bit:
2010-06-06 23:00:33 +00:00
# define PUSH32(a) (*(validate_stack_addr(s, (s->xs->sp)++)) = (a))
# define POP32() (*(validate_stack_addr(s, --(s->xs->sp))))
2009-02-15 06:10:59 +00:00
2009-03-12 03:26:47 +00:00
ExecStack * execute_method ( EngineState * s , uint16 script , uint16 pubfunct , StackPtr sp , reg_t calling_obj , uint16 argc , StackPtr argp ) {
2009-10-04 18:38:18 +00:00
int seg = s - > _segMan - > getScriptSegment ( script ) ;
Script * scr = s - > _segMan - > getScriptIfLoaded ( seg ) ;
2009-02-15 06:10:59 +00:00
2010-05-19 08:50:24 +00:00
if ( ! scr | | scr - > isMarkedAsDeleted ( ) ) { // Script not present yet?
2010-06-27 21:18:19 +00:00
seg = s - > _segMan - > instantiateScript ( script ) ;
2010-05-19 08:50:24 +00:00
scr = s - > _segMan - > getScript ( seg ) ;
}
2009-02-15 06:10:59 +00:00
2016-10-14 13:20:04 -05:00
// Check if a breakpoint is set on this method
2020-01-09 18:03:32 -08:00
if ( g_sci - > checkExportBreakpoint ( script , pubfunct ) ) {
logExportCall ( script , pubfunct , s , argc , argp ) ;
}
2016-10-14 13:20:04 -05:00
2012-06-23 21:20:43 +03:00
uint32 exportAddr = scr - > validateExportFunc ( pubfunct , false ) ;
if ( ! exportAddr )
2009-02-15 06:10:59 +00:00
return NULL ;
2012-09-26 04:17:31 +02:00
2016-04-01 23:54:00 +02:00
assert ( argp [ 0 ] . toUint16 ( ) = = argc ) ; // The first argument is argc
2011-03-26 02:37:10 +02:00
ExecStack xstack ( calling_obj , calling_obj , sp , argc , argp ,
2016-04-02 00:04:00 +02:00
seg , make_reg32 ( seg , exportAddr ) , - 1 , - 1 , - 1 , pubfunct , - 1 ,
2011-03-26 02:37:10 +02:00
s - > _executionStack . size ( ) - 1 , EXEC_STACK_TYPE_CALL ) ;
s - > _executionStack . push_back ( xstack ) ;
return & ( s - > _executionStack . back ( ) ) ;
2009-02-15 06:10:59 +00:00
}
2011-03-25 14:09:09 +02:00
static void _exec_varselectors ( EngineState * s ) {
// Executes all varselector read/write ops on the TOS
while ( ! s - > _executionStack . empty ( ) & & s - > _executionStack . back ( ) . type = = EXEC_STACK_TYPE_VARSELECTOR ) {
ExecStack & xs = s - > _executionStack . back ( ) ;
reg_t * var = xs . getVarPointer ( s - > _segMan ) ;
if ( ! var ) {
error ( " Invalid varselector exec stack entry " ) ;
} else {
// varselector access?
if ( xs . argc ) { // write?
* var = xs . variables_argp [ 1 ] ;
2016-02-25 00:39:31 +01:00
# ifdef ENABLE_SCI32
updateInfoFlagViewVisible ( s - > _segMan - > getObject ( xs . addr . varp . obj ) , xs . addr . varp . varindex ) ;
# endif
2011-03-25 14:09:09 +02:00
} else // No, read
s - > r_acc = * var ;
}
s - > _executionStack . pop_back ( ) ;
}
}
2011-03-27 01:43:36 +02:00
2009-02-28 11:12:59 +00:00
ExecStack * send_selector ( EngineState * s , reg_t send_obj , reg_t work_obj , StackPtr sp , int framesize , StackPtr argp ) {
2011-03-25 13:25:38 +02:00
// send_obj and work_obj are equal for anything but 'super'
// Returns a pointer to the TOS exec_stack element
2009-05-18 18:15:45 +00:00
assert ( s ) ;
2009-02-15 06:10:59 +00:00
reg_t funcp ;
2011-03-25 13:25:38 +02:00
Selector selector ;
2009-02-15 06:10:59 +00:00
int argc ;
2011-03-25 13:25:38 +02:00
int origin = s - > _executionStack . size ( ) - 1 ; // Origin: Used for debugging
int activeBreakpointTypes = g_sci - > _debugState . _activeBreakpointTypes ;
2011-03-26 02:37:10 +02:00
ObjVarRef varp ;
2009-02-15 06:10:59 +00:00
2011-03-26 02:37:10 +02:00
Common : : List < ExecStack > : : iterator prevElementIterator = s - > _executionStack . end ( ) ;
2009-02-15 06:10:59 +00:00
while ( framesize > 0 ) {
2011-02-15 11:25:54 +02:00
selector = argp - > requireUint16 ( ) ;
argp + + ;
argc = argp - > requireUint16 ( ) ;
2009-02-15 06:10:59 +00:00
2011-02-15 11:25:54 +02:00
if ( argc > 0x800 ) // More arguments than the stack could possibly accomodate for
2009-07-03 21:59:07 +00:00
error ( " send_selector(): More than 0x800 arguments to function call " ) ;
2009-02-15 06:10:59 +00:00
SCI: Improve audio volume & settings sync code
This patch includes enhancements to the ScummVM integration with
SCI engine, with particular focus on SCI32 support.
1. Fixes audio volumes syncing erroneously to ScummVM in games
that modify the audio volume without user action (e.g. SCI1.1
talkies that reduce music volume during speech playback). Now,
volumes will only be synchronised when the user interacts with
the game's audio settings. This mechanism works by looking for
a known volume control object in the stack, and only syncing
when the control object is present. (Ports and planes were
researched and found unreliable.)
2. Fixes audio syncing in SCI32 games that do not set game
volumes through kDoSoundMasterVolume/kDoAudioVolume, like GK1,
GK2, Phant1, and Torin.
3. Fixes speech/subtitles syncing in SCI32 games that do not use
global 90, like LSL6hires.
4. Fixes in-game volume controls in SCI32 games reflecting
outdated audio volumes when a change is made during the game
from the ScummVM launcher.
5. Fixes SCI32 games that would restore volumes from save games
or reset volumes on startup, which caused game volumes to be
out-of-sync with ScummVM when started.
6. ScummVM integration code for audio sync has been abstracted
into a new GuestAdditions class. This keeps the ScummVM-
specific code all in one place, with only small hooks into the
engine code. ScummVM integrated save/load code should probably
also go here in the future.
Fixes Trac#9700.
2017-01-26 13:18:41 -06:00
# ifdef ENABLE_SCI32
g_sci - > _guestAdditions - > sendSelectorHook ( send_obj , selector , argp ) ;
# endif
2011-03-25 13:25:38 +02:00
SelectorType selectorType = lookupSelector ( s - > _segMan , send_obj , selector , & varp , & funcp ) ;
2011-03-26 02:37:10 +02:00
if ( selectorType = = kSelectorNone )
2017-02-21 15:07:33 -06:00
error ( " Send to invalid selector 0x%x (%s) of object at %04x:%04x " , 0xffff & selector , g_sci - > getKernel ( ) - > getSelectorName ( 0xffff & selector ) . c_str ( ) , PRINT_REG ( send_obj ) ) ;
2011-03-26 02:37:10 +02:00
ExecStackType stackType = EXEC_STACK_TYPE_VARSELECTOR ;
StackPtr curSP = NULL ;
2018-08-25 12:21:36 +03:00
reg_t curFP = make_reg32 ( 0 , 0 ) ;
2011-03-26 02:37:10 +02:00
if ( selectorType = = kSelectorMethod ) {
stackType = EXEC_STACK_TYPE_CALL ;
curSP = sp ;
2012-06-23 21:38:46 +03:00
curFP = make_reg32 ( funcp . getSegment ( ) , funcp . getOffset ( ) ) ;
2011-03-26 02:37:10 +02:00
sp = CALL_SP_CARRY ; // Destroy sp, as it will be carried over
}
2017-05-27 20:26:57 +02:00
if ( ( activeBreakpointTypes & ( BREAK_SELECTOREXEC | BREAK_SELECTORREAD | BREAK_SELECTORWRITE ) )
| | DebugMan . isDebugChannelEnabled ( kDebugLevelScripts ) )
2011-03-25 13:25:38 +02:00
debugSelectorCall ( send_obj , selector , argc , argp , varp , funcp , s - > _segMan , selectorType ) ;
2016-04-01 23:54:00 +02:00
assert ( argp [ 0 ] . toUint16 ( ) = = argc ) ; // The first argument is argc
2011-03-26 02:37:10 +02:00
ExecStack xstack ( work_obj , send_obj , curSP , argc , argp ,
2017-01-05 10:29:14 -06:00
kUninitializedSegment , curFP , selector , - 1 , - 1 , - 1 , - 1 ,
2011-03-26 02:37:10 +02:00
origin , stackType ) ;
if ( selectorType = = kSelectorVariable )
xstack . addr . varp = varp ;
2009-02-15 06:10:59 +00:00
2011-03-26 02:37:10 +02:00
// The new stack entries should be put on the stack in reverse order
// so that the first one is executed first
s - > _executionStack . insert ( prevElementIterator , xstack ) ;
// Decrement the stack end pointer so that it points to our recently
// added element, so that the next insert() places it before this one.
- - prevElementIterator ;
2011-03-25 13:25:38 +02:00
2009-02-15 06:10:59 +00:00
framesize - = ( 2 + argc ) ;
argp + = argc + 1 ;
2011-03-25 13:25:38 +02:00
} // while (framesize > 0)
2009-02-15 06:10:59 +00:00
2016-11-19 19:13:17 +01:00
// Perform all varselector actions at the top of the stack immediately.
// Note that there may be some behind method selector calls as well;
// those will get executed by op_ret later.
2009-02-15 06:10:59 +00:00
_exec_varselectors ( s ) ;
2010-05-26 09:33:33 +00:00
return s - > _executionStack . empty ( ) ? NULL : & ( s - > _executionStack . back ( ) ) ;
2009-02-15 06:10:59 +00:00
}
2016-04-02 00:04:00 +02:00
static void addKernelCallToExecStack ( EngineState * s , int kernelCallNr , int kernelSubCallNr , int argc , reg_t * argv ) {
2010-07-10 20:50:52 +00:00
// Add stack frame to indicate we're executing a callk.
// This is useful in debugger backtraces if this
// kernel function calls a script itself.
2017-01-20 15:43:30 -06:00
ExecStack xstack ( NULL_REG , NULL_REG , argv + argc , argc , argv - 1 , kUninitializedSegment , make_reg32 ( 0 , 0 ) ,
2016-04-02 00:04:00 +02:00
- 1 , kernelCallNr , kernelSubCallNr , - 1 , - 1 , s - > _executionStack . size ( ) - 1 , EXEC_STACK_TYPE_KERNEL ) ;
2011-03-26 02:37:10 +02:00
s - > _executionStack . push_back ( xstack ) ;
2010-07-10 20:50:52 +00:00
}
2010-07-09 21:33:12 +00:00
static void callKernelFunc ( EngineState * s , int kernelCallNr , int argc ) {
2010-06-30 10:09:07 +00:00
Kernel * kernel = g_sci - > getKernel ( ) ;
2010-02-07 17:57:51 +00:00
2010-07-09 21:33:12 +00:00
if ( kernelCallNr > = ( int ) kernel - > _kernelFuncs . size ( ) )
error ( " Invalid kernel function 0x%x requested " , kernelCallNr ) ;
2010-02-07 17:57:51 +00:00
2010-07-09 21:33:12 +00:00
const KernelFunction & kernelCall = kernel - > _kernelFuncs [ kernelCallNr ] ;
2010-07-09 12:06:41 +00:00
reg_t * argv = s - > xs - > sp + 1 ;
2010-02-07 17:57:51 +00:00
2010-06-30 10:09:07 +00:00
if ( kernelCall . signature
2010-07-09 12:06:41 +00:00
& & ! kernel - > signatureMatch ( kernelCall . signature , argc , argv ) ) {
2010-06-30 09:47:04 +00:00
// signature mismatch, check if a workaround is available
2016-08-28 16:20:37 -05:00
SciCallOrigin originReply ;
2010-07-20 17:58:04 +00:00
SciWorkaroundSolution solution = trackOriginAndFindWorkaround ( 0 , kernelCall . workarounds , & originReply ) ;
switch ( solution . type ) {
2015-03-20 16:06:19 +01:00
case WORKAROUND_NONE : {
Common : : String signatureDetailsStr ;
kernel - > signatureDebug ( signatureDetailsStr , kernelCall . signature , argc , argv ) ;
2016-09-26 20:41:37 -05:00
error ( " \n %s[VM] k%s[%x]: signature mismatch in %s " , signatureDetailsStr . c_str ( ) , kernelCall . name , kernelCallNr , originReply . toString ( ) . c_str ( ) ) ;
2010-07-20 17:58:04 +00:00
break ;
2015-03-20 16:06:19 +01:00
}
2010-07-20 17:58:04 +00:00
case WORKAROUND_IGNORE : // don't do kernel call, leave acc alone
2010-07-20 11:45:06 +00:00
return ;
2010-07-20 17:58:04 +00:00
case WORKAROUND_STILLCALL : // call kernel anyway
2010-07-20 11:45:06 +00:00
break ;
2010-07-20 17:58:04 +00:00
case WORKAROUND_FAKE : // don't do kernel call, fake acc
s - > r_acc = make_reg ( 0 , solution . value ) ;
2010-06-30 09:47:04 +00:00
return ;
2010-07-20 11:45:06 +00:00
default :
error ( " unknown workaround type " ) ;
}
2010-02-07 17:57:51 +00:00
}
2010-07-10 18:21:09 +00:00
// Call kernel function
if ( ! kernelCall . subFunctionCount ) {
2016-04-01 23:54:00 +02:00
argv [ - 1 ] = make_reg ( 0 , argc ) ; // The first argument is argc
2016-04-02 00:04:00 +02:00
addKernelCallToExecStack ( s , kernelCallNr , - 1 , argc , argv ) ;
2010-07-10 18:21:09 +00:00
s - > r_acc = kernelCall . function ( s , argc , argv ) ;
2010-07-23 20:59:27 +00:00
2017-05-27 20:26:57 +02:00
if ( g_sci - > checkKernelBreakpoint ( kernelCall . name ) )
2010-07-29 17:22:38 +00:00
logKernelCall ( & kernelCall , NULL , s , argc , argv , s - > r_acc ) ;
2010-07-10 18:21:09 +00:00
} else {
// Sub-functions available, check signature and call that one directly
if ( argc < 1 )
2011-02-17 17:33:36 +02:00
error ( " [VM] k%s[%x]: no subfunction ID parameter given " , kernelCall . name , kernelCallNr ) ;
if ( argv [ 0 ] . isPointer ( ) )
error ( " [VM] k%s[%x]: given subfunction ID is actually a pointer " , kernelCall . name , kernelCallNr ) ;
2016-07-27 21:00:37 -05:00
# ifdef ENABLE_SCI32
// The Windows version of kShowMovie has subops, but the subop number
// is put in the second parameter in SCI2.1+, even though every other
// kcall with subops puts the subop in the first parameter. To allow use
// of the normal subops system, we swap the arguments so the subop
// number is in the usual place.
if ( getSciVersion ( ) > SCI_VERSION_2 & &
g_sci - > getPlatform ( ) = = Common : : kPlatformWindows & &
strcmp ( kernelCall . name , " ShowMovie " ) = = 0 ) {
assert ( argc > 1 ) ;
SWAP ( argv [ 0 ] , argv [ 1 ] ) ;
}
# endif
2010-07-10 18:21:09 +00:00
const uint16 subId = argv [ 0 ] . toUint16 ( ) ;
// Skip over subfunction-id
argc - - ;
argv + + ;
if ( subId > = kernelCall . subFunctionCount )
2011-02-17 17:33:36 +02:00
error ( " [VM] k%s: subfunction ID %d requested, but not available " , kernelCall . name , subId ) ;
2010-07-10 18:21:09 +00:00
const KernelSubFunction & kernelSubCall = kernelCall . subFunctions [ subId ] ;
if ( kernelSubCall . signature & & ! kernel - > signatureMatch ( kernelSubCall . signature , argc , argv ) ) {
// Signature mismatch
2016-08-28 16:20:37 -05:00
SciCallOrigin originReply ;
2010-07-20 17:58:04 +00:00
SciWorkaroundSolution solution = trackOriginAndFindWorkaround ( 0 , kernelSubCall . workarounds , & originReply ) ;
switch ( solution . type ) {
case WORKAROUND_NONE : {
2015-03-20 16:06:19 +01:00
Common : : String signatureDetailsStr ;
kernel - > signatureDebug ( signatureDetailsStr , kernelSubCall . signature , argc , argv ) ;
2010-07-10 18:21:09 +00:00
int callNameLen = strlen ( kernelCall . name ) ;
if ( strncmp ( kernelCall . name , kernelSubCall . name , callNameLen ) = = 0 ) {
const char * subCallName = kernelSubCall . name + callNameLen ;
2016-09-26 20:41:37 -05:00
error ( " \n %s[VM] k%s(%s): signature mismatch in %s " ,
signatureDetailsStr . c_str ( ) , kernelCall . name , subCallName ,
originReply . toString ( ) . c_str ( ) ) ;
2010-07-09 21:33:12 +00:00
}
2016-09-26 20:41:37 -05:00
error ( " \n %s[VM] k%s: signature mismatch in %s " ,
signatureDetailsStr . c_str ( ) , kernelSubCall . name ,
originReply . toString ( ) . c_str ( ) ) ;
2010-07-20 17:58:04 +00:00
break ;
2010-07-09 12:06:41 +00:00
}
2010-07-20 17:58:04 +00:00
case WORKAROUND_IGNORE : // don't do kernel call, leave acc alone
2010-07-20 12:50:13 +00:00
return ;
2010-07-20 17:58:04 +00:00
case WORKAROUND_STILLCALL : // call kernel anyway
2010-07-20 12:50:13 +00:00
break ;
2010-07-20 17:58:04 +00:00
case WORKAROUND_FAKE : // don't do kernel call, fake acc
s - > r_acc = make_reg ( 0 , solution . value ) ;
2010-07-10 18:21:09 +00:00
return ;
2010-07-20 12:50:13 +00:00
default :
error ( " unknown workaround type " ) ;
}
2010-07-09 12:06:41 +00:00
}
2010-07-10 18:21:09 +00:00
if ( ! kernelSubCall . function )
2011-02-17 17:33:36 +02:00
error ( " [VM] k%s: subfunction ID %d requested, but not available " , kernelCall . name , subId ) ;
2016-04-01 23:54:00 +02:00
argv [ - 1 ] = make_reg ( 0 , argc ) ; // The first argument is argc
2016-04-02 00:04:00 +02:00
addKernelCallToExecStack ( s , kernelCallNr , subId , argc , argv ) ;
2010-07-10 18:21:09 +00:00
s - > r_acc = kernelSubCall . function ( s , argc , argv ) ;
2010-07-29 17:22:38 +00:00
2017-05-27 20:26:57 +02:00
if ( g_sci - > checkKernelBreakpoint ( kernelSubCall . name ) )
2010-07-29 17:22:38 +00:00
logKernelCall ( & kernelCall , & kernelSubCall , s , argc , argv , s - > r_acc ) ;
2010-07-10 18:21:09 +00:00
}
2010-06-18 14:48:39 +00:00
2010-07-10 18:21:09 +00:00
// Remove callk stack frame again, if there's still an execution stack
if ( s - > _executionStack . begin ( ) ! = s - > _executionStack . end ( ) )
s - > _executionStack . pop_back ( ) ;
2010-02-07 17:57:51 +00:00
}
2010-02-07 12:13:59 +00:00
int readPMachineInstruction ( const byte * src , byte & extOpcode , int16 opparams [ 4 ] ) {
uint offset = 0 ;
extOpcode = src [ offset + + ] ; // Get "extended" opcode (lower bit has special meaning)
2010-02-07 17:57:25 +00:00
const byte opcode = extOpcode > > 1 ; // get the actual opcode
2010-02-07 12:13:59 +00:00
2011-06-25 21:15:34 +02:00
memset ( opparams , 0 , 4 * sizeof ( int16 ) ) ;
2010-02-07 12:13:59 +00:00
2011-11-29 18:34:25 +01:00
for ( int i = 0 ; g_sci - > _opcode_formats [ opcode ] [ i ] ; + + i ) {
2010-11-02 09:49:47 +00:00
//debugN("Opcode: 0x%x, Opnumber: 0x%x, temp: %d\n", opcode, opcode, temp);
2010-08-25 09:02:43 +00:00
assert ( i < 3 ) ;
2011-11-29 18:34:25 +01:00
switch ( g_sci - > _opcode_formats [ opcode ] [ i ] ) {
2010-02-07 12:13:59 +00:00
case Script_Byte :
opparams [ i ] = src [ offset + + ] ;
break ;
case Script_SByte :
opparams [ i ] = ( int8 ) src [ offset + + ] ;
break ;
case Script_Word :
2010-05-18 04:17:58 +00:00
opparams [ i ] = READ_SCI11ENDIAN_UINT16 ( src + offset ) ;
2010-02-07 12:13:59 +00:00
offset + = 2 ;
break ;
case Script_SWord :
2010-05-18 04:17:58 +00:00
opparams [ i ] = ( int16 ) READ_SCI11ENDIAN_UINT16 ( src + offset ) ;
2010-02-07 12:13:59 +00:00
offset + = 2 ;
break ;
case Script_Variable :
case Script_Property :
case Script_Local :
case Script_Temp :
case Script_Global :
case Script_Param :
case Script_Offset :
if ( extOpcode & 1 ) {
opparams [ i ] = src [ offset + + ] ;
} else {
2010-05-18 04:17:58 +00:00
opparams [ i ] = READ_SCI11ENDIAN_UINT16 ( src + offset ) ;
2010-02-07 12:13:59 +00:00
offset + = 2 ;
}
break ;
case Script_SVariable :
case Script_SRelative :
if ( extOpcode & 1 ) {
opparams [ i ] = ( int8 ) src [ offset + + ] ;
} else {
2010-05-18 04:17:58 +00:00
opparams [ i ] = ( int16 ) READ_SCI11ENDIAN_UINT16 ( src + offset ) ;
2010-02-07 12:13:59 +00:00
offset + = 2 ;
}
break ;
case Script_None :
case Script_End :
break ;
case Script_Invalid :
default :
error ( " opcode %02x: Invalid " , extOpcode ) ;
}
}
2011-01-07 20:24:49 +00:00
// Special handling of the op_line opcode
if ( opcode = = op_pushSelf ) {
// Compensate for a bug in non-Sierra compilers, which seem to generate
// pushSelf instructions with the low bit set. This makes the following
// heuristic fail and leads to endless loops and crashes. Our
// interpretation of this seems correct, as other SCI tools, like for
// example SCI Viewer, have issues with these scripts (e.g. script 999
2021-02-25 01:09:19 -08:00
// in Circus Quest). Fixes bug #5113.
2011-01-07 20:24:49 +00:00
if ( ! ( extOpcode & 1 ) | | g_sci - > getGameId ( ) = = GID_FANMADE ) {
// op_pushSelf: no adjustment necessary
} else {
// Debug opcode op_file, skip null-terminated string (file name)
while ( src [ offset + + ] ) { }
}
}
2010-02-07 12:13:59 +00:00
return offset ;
}
2016-11-05 17:57:34 -05:00
uint32 findOffset ( const int16 relOffset , const Script * scr , const uint32 pcOffset ) {
uint32 offset ;
switch ( g_sci - > _features - > detectLofsType ( ) ) {
case SCI_VERSION_0_EARLY :
offset = ( uint16 ) pcOffset + relOffset ;
break ;
case SCI_VERSION_1_MIDDLE :
offset = relOffset ;
break ;
case SCI_VERSION_1_1 :
2017-05-20 20:59:16 -05:00
offset = relOffset + scr - > getHeapOffset ( ) ;
2016-11-05 17:57:34 -05:00
break ;
2017-04-17 21:07:23 -05:00
# ifdef ENABLE_SCI32
2016-11-05 17:57:34 -05:00
case SCI_VERSION_3 :
// In theory this can break if the variant with a one-byte argument is
// used. For now, assume it doesn't happen.
offset = scr - > relocateOffsetSci3 ( pcOffset - 2 ) ;
break ;
2017-04-17 21:07:23 -05:00
# endif
2016-11-05 17:57:34 -05:00
default :
error ( " Unknown lofs type " ) ;
}
return offset ;
}
2010-07-20 23:15:07 +00:00
void run_vm ( EngineState * s ) {
2009-05-19 00:02:10 +00:00
assert ( s ) ;
2009-02-15 06:10:59 +00:00
int temp ;
2009-02-20 23:09:29 +00:00
reg_t r_temp ; // Temporary register
2009-02-28 11:12:59 +00:00
StackPtr s_temp ; // Temporary stack pointer
2009-02-21 21:16:41 +00:00
int16 opparams [ 4 ] ; // opcode parameters
2009-02-20 23:09:29 +00:00
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ; // &rest adjusts the parameter count by this value
2009-02-20 23:09:29 +00:00
// Current execution data:
2010-06-06 23:00:33 +00:00
s - > xs = & ( s - > _executionStack . back ( ) ) ;
2009-02-28 11:12:59 +00:00
ExecStack * xs_new = NULL ;
2010-06-06 23:00:33 +00:00
Object * obj = s - > _segMan - > getObject ( s - > xs - > objp ) ;
2010-07-06 07:42:25 +00:00
Script * scr = 0 ;
2010-06-06 23:00:33 +00:00
Script * local_script = s - > _segMan - > getScriptIfLoaded ( s - > xs - > local_segment ) ;
2010-06-10 11:18:10 +00:00
int old_executionStackBase = s - > executionStackBase ;
2009-02-22 13:11:43 +00:00
// Used to detect the stack bottom, for "physical" returns
2009-02-15 06:10:59 +00:00
2010-07-06 07:42:25 +00:00
if ( ! local_script )
2009-07-03 21:59:07 +00:00
error ( " run_vm(): program counter gone astray (local_script pointer is null) " ) ;
2009-02-15 22:28:12 +00:00
2010-07-22 12:38:48 +00:00
s - > executionStackBase = s - > _executionStack . size ( ) - 1 ;
2009-02-15 06:10:59 +00:00
2010-06-10 11:43:20 +00:00
s - > variablesSegment [ VAR_TEMP ] = s - > variablesSegment [ VAR_PARAM ] = s - > _segMan - > findSegmentByType ( SEG_TYPE_STACK ) ;
s - > variablesBase [ VAR_TEMP ] = s - > variablesBase [ VAR_PARAM ] = s - > stack_base ;
2009-02-15 06:10:59 +00:00
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ; // Force initialization
2009-02-15 06:10:59 +00:00
2011-02-22 18:52:44 +02:00
# ifdef ABORT_ON_INFINITE_LOOP
byte prevOpcode = 0xFF ;
# endif
2009-02-15 06:10:59 +00:00
while ( 1 ) {
2009-02-20 23:09:29 +00:00
int var_type ; // See description below
2009-02-15 06:10:59 +00:00
int var_number ;
2012-06-18 05:21:59 +03:00
g_sci - > _debugState . old_pc_offset = s - > xs - > addr . pc . getOffset ( ) ;
2010-07-12 23:20:33 +00:00
g_sci - > _debugState . old_sp = s - > xs - > sp ;
2009-02-15 06:10:59 +00:00
2010-08-29 00:39:33 +00:00
if ( s - > abortScriptProcessing ! = kAbortNone )
2010-06-21 20:17:59 +00:00
return ; // Stop processing
2009-04-28 15:58:19 +00:00
if ( s - > _executionStackPosChanged ) {
2012-06-18 05:21:59 +03:00
scr = s - > _segMan - > getScriptIfLoaded ( s - > xs - > addr . pc . getSegment ( ) ) ;
2010-06-25 17:13:47 +00:00
if ( ! scr )
2012-06-18 05:21:59 +03:00
error ( " No script in segment %d " , s - > xs - > addr . pc . getSegment ( ) ) ;
2010-06-06 23:00:33 +00:00
s - > xs = & ( s - > _executionStack . back ( ) ) ;
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = false ;
2009-02-15 06:10:59 +00:00
2010-06-25 17:13:47 +00:00
obj = s - > _segMan - > getObject ( s - > xs - > objp ) ;
local_script = s - > _segMan - > getScriptIfLoaded ( s - > xs - > local_segment ) ;
if ( ! local_script ) {
2010-08-05 16:58:59 +00:00
error ( " Could not find local script from segment %x " , s - > xs - > local_segment ) ;
2009-02-15 06:10:59 +00:00
} else {
2011-11-05 02:53:08 +02:00
s - > variablesSegment [ VAR_LOCAL ] = local_script - > getLocalsSegment ( ) ;
s - > variablesBase [ VAR_LOCAL ] = s - > variables [ VAR_LOCAL ] = local_script - > getLocalsBegin ( ) ;
s - > variablesMax [ VAR_LOCAL ] = local_script - > getLocalsCount ( ) ;
2010-06-25 17:13:47 +00:00
s - > variablesMax [ VAR_TEMP ] = s - > xs - > sp - s - > xs - > fp ;
s - > variablesMax [ VAR_PARAM ] = s - > xs - > argc + 1 ;
2009-02-15 06:10:59 +00:00
}
2010-06-25 17:13:47 +00:00
s - > variables [ VAR_TEMP ] = s - > xs - > fp ;
s - > variables [ VAR_PARAM ] = s - > xs - > variables_argp ;
2009-02-15 06:10:59 +00:00
}
2010-08-29 00:39:33 +00:00
if ( s - > abortScriptProcessing ! = kAbortNone )
2010-06-21 20:17:59 +00:00
return ; // Stop processing
2009-02-15 06:10:59 +00:00
2016-11-05 17:56:15 -05:00
g_sci - > checkAddressBreakpoint ( s - > xs - > addr . pc ) ;
2009-02-20 23:09:29 +00:00
// Debug if this has been requested:
2009-07-18 12:51:12 +00:00
// TODO: re-implement sci_debug_flags
2010-07-12 23:20:33 +00:00
if ( g_sci - > _debugState . debugging /* sci_debug_flags*/ ) {
g_sci - > scriptDebug ( ) ;
g_sci - > _debugState . breakpointWasHit = false ;
2009-02-15 06:10:59 +00:00
}
2010-02-13 17:42:49 +00:00
Console * con = g_sci - > getSciDebugger ( ) ;
2010-07-17 18:38:42 +00:00
con - > onFrame ( ) ;
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
if ( s - > xs - > sp < s - > xs - > fp )
2010-06-26 16:21:28 +00:00
error ( " run_vm(): stack underflow, sp: %04x:%04x, fp: %04x:%04x " ,
2010-06-06 23:00:33 +00:00
PRINT_REG ( * s - > xs - > sp ) , PRINT_REG ( * s - > xs - > fp ) ) ;
2009-02-15 06:10:59 +00:00
2010-06-10 11:43:20 +00:00
s - > variablesMax [ VAR_TEMP ] = s - > xs - > sp - s - > xs - > fp ;
2009-02-15 06:10:59 +00:00
2012-06-18 05:21:59 +03:00
if ( s - > xs - > addr . pc . getOffset ( ) > = scr - > getBufSize ( ) )
2010-06-26 16:21:28 +00:00
error ( " run_vm(): program counter gone astray, addr: %d, code buffer size: %d " ,
2012-06-18 05:21:59 +03:00
s - > xs - > addr . pc . getOffset ( ) , scr - > getBufSize ( ) ) ;
2009-02-15 06:10:59 +00:00
2010-02-07 12:13:59 +00:00
// Get opcode
2010-02-07 17:57:25 +00:00
byte extOpcode ;
2021-10-17 01:37:00 +03:00
s - > xs - > addr . pc . incOffset ( readPMachineInstruction ( scr - > getBuf ( s - > xs - > addr . pc . getOffset ( ) ) , extOpcode , opparams ) ) ;
2010-02-07 17:57:25 +00:00
const byte opcode = extOpcode > > 1 ;
2011-02-22 18:52:44 +02:00
//debug("%s: %d, %d, %d, %d, acc = %04x:%04x, script %d, local script %d", opcodeNames[opcode], opparams[0], opparams[1], opparams[2], opparams[3], PRINT_REG(s->r_acc), scr->getScriptNumber(), local_script->getScriptNumber());
# ifdef ABORT_ON_INFINITE_LOOP
if ( prevOpcode ! = 0xFF ) {
if ( prevOpcode = = op_eq_ | | prevOpcode = = op_ne_ | |
prevOpcode = = op_gt_ | | prevOpcode = = op_ge_ | |
prevOpcode = = op_lt_ | | prevOpcode = = op_le_ | |
prevOpcode = = op_ugt_ | | prevOpcode = = op_uge_ | |
prevOpcode = = op_ult_ | | prevOpcode = = op_ule_ ) {
if ( opcode = = op_jmp )
error ( " Infinite loop detected in script %d " , scr - > getScriptNumber ( ) ) ;
}
}
prevOpcode = opcode ;
# endif
2009-02-15 06:10:59 +00:00
2010-02-07 17:57:25 +00:00
switch ( opcode ) {
2009-02-15 06:10:59 +00:00
2011-02-17 17:33:36 +02:00
case op_bnot : // 0x00 (00)
2010-07-04 14:55:28 +00:00
// Binary not
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , 0xffff ^ s - > r_acc . requireUint16 ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_add : // 0x01 (01)
2011-02-17 17:33:36 +02:00
s - > r_acc = POP32 ( ) + s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_sub : // 0x02 (02)
2011-02-17 17:33:36 +02:00
s - > r_acc = POP32 ( ) - s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_mul : // 0x03 (03)
s - > r_acc = POP32 ( ) * s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_div : // 0x04 (04)
// we check for division by 0 inside the custom reg_t division operator
s - > r_acc = POP32 ( ) / s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2010-07-18 18:57:28 +00:00
2011-02-21 05:18:21 +02:00
case op_mod : // 0x05 (05)
// we check for division by 0 inside the custom reg_t modulo operator
s - > r_acc = POP32 ( ) % s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2010-07-18 18:57:28 +00:00
2011-02-17 17:33:36 +02:00
case op_shr : // 0x06 (06)
2010-07-04 14:55:28 +00:00
// Shift right logical
2011-02-17 17:33:36 +02:00
s - > r_acc = POP32 ( ) > > s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_shl : // 0x07 (07)
2010-07-04 14:55:28 +00:00
// Shift left logical
2011-02-17 17:33:36 +02:00
s - > r_acc = POP32 ( ) < < s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_xor : // 0x08 (08)
s - > r_acc = POP32 ( ) ^ s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_and : // 0x09 (09)
s - > r_acc = POP32 ( ) & s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_or : // 0x0a (10)
s - > r_acc = POP32 ( ) | s - > r_acc ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_neg : // 0x0b (11)
s - > r_acc = make_reg ( 0 , - s - > r_acc . requireSint16 ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_not : // 0x0c (12)
2012-06-18 05:21:59 +03:00
s - > r_acc = make_reg ( 0 , ! ( s - > r_acc . getOffset ( ) | | s - > r_acc . getSegment ( ) ) ) ;
2009-02-22 13:11:43 +00:00
// Must allow pointers to be negated, as this is used for checking whether objects exist
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_eq_ : // 0x0d (13)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) = = s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ne_ : // 0x0e (14)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) ! = s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_gt_ : // 0x0f (15)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) > s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ge_ : // 0x10 (16)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) > = s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_lt_ : // 0x11 (17)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) < s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_le_ : // 0x12 (18)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) < = s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ugt_ : // 0x13 (19)
2010-07-04 14:55:28 +00:00
// > (unsigned)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) . gtU ( s - > r_acc ) ) ;
2010-01-30 04:01:15 +00:00
break ;
case op_uge_ : // 0x14 (20)
2010-07-04 14:55:28 +00:00
// >= (unsigned)
2010-01-30 04:01:15 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) . geU ( s - > r_acc ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ult_ : // 0x15 (21)
2010-07-04 14:55:28 +00:00
// < (unsigned)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) . ltU ( s - > r_acc ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ule_ : // 0x16 (22)
2010-07-04 14:55:28 +00:00
// <= (unsigned)
2009-02-15 06:10:59 +00:00
s - > r_prev = s - > r_acc ;
2011-02-17 17:33:36 +02:00
s - > r_acc = make_reg ( 0 , POP32 ( ) . leU ( s - > r_acc ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_bt : // 0x17 (23)
2010-07-04 14:55:28 +00:00
// Branch relative if true
2021-10-17 01:37:00 +03:00
if ( s - > r_acc . getOffset ( ) | | s - > r_acc . getSegment ( ) )
s - > xs - > addr . pc . incOffset ( opparams [ 0 ] ) ;
2011-11-28 22:35:10 +02:00
2021-10-17 01:37:00 +03:00
if ( s - > xs - > addr . pc . getOffset ( ) > = local_script - > getScriptSize ( ) )
error ( " [VM] op_bt: request to jump past the end of script %d (offset %d, script is %d bytes) " ,
local_script - > getScriptNumber ( ) , s - > xs - > addr . pc . getOffset ( ) , local_script - > getScriptSize ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_bnt : // 0x18 (24)
2010-07-04 14:55:28 +00:00
// Branch relative if not true
2021-10-17 01:37:00 +03:00
if ( ! ( s - > r_acc . getOffset ( ) | | s - > r_acc . getSegment ( ) ) )
s - > xs - > addr . pc . incOffset ( opparams [ 0 ] ) ;
2011-11-28 22:35:10 +02:00
2021-10-17 01:37:00 +03:00
if ( s - > xs - > addr . pc . getOffset ( ) > = local_script - > getScriptSize ( ) )
error ( " [VM] op_bnt: request to jump past the end of script %d (offset %d, script is %d bytes) " ,
local_script - > getScriptNumber ( ) , s - > xs - > addr . pc . getOffset ( ) , local_script - > getScriptSize ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_jmp : // 0x19 (25)
2021-10-17 01:37:00 +03:00
s - > xs - > addr . pc . incOffset ( opparams [ 0 ] ) ;
2011-11-28 22:35:10 +02:00
2021-10-17 01:37:00 +03:00
if ( s - > xs - > addr . pc . getOffset ( ) > = local_script - > getScriptSize ( ) )
error ( " [VM] op_jmp: request to jump past the end of script %d (offset %d, script is %d bytes) " ,
local_script - > getScriptNumber ( ) , s - > xs - > addr . pc . getOffset ( ) , local_script - > getScriptSize ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ldi : // 0x1a (26)
2010-07-04 14:55:28 +00:00
// Load data immediate
2009-02-15 06:10:59 +00:00
s - > r_acc = make_reg ( 0 , opparams [ 0 ] ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_push : // 0x1b (27)
2010-07-04 14:55:28 +00:00
// Push to stack
2009-02-15 06:10:59 +00:00
PUSH32 ( s - > r_acc ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_pushi : // 0x1c (28)
2010-07-04 14:55:28 +00:00
// Push immediate
2009-02-15 06:10:59 +00:00
PUSH ( opparams [ 0 ] ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_toss : // 0x1d (29)
2010-07-04 14:55:28 +00:00
// TOS (Top Of Stack) subtract
2010-06-06 23:00:33 +00:00
s - > xs - > sp - - ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_dup : // 0x1e (30)
2010-07-04 14:55:28 +00:00
// Duplicate TOD (Top Of Stack) element
2010-06-06 23:00:33 +00:00
r_temp = s - > xs - > sp [ - 1 ] ;
2009-02-15 06:10:59 +00:00
PUSH32 ( r_temp ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_link : // 0x1f (31)
2010-06-24 11:54:33 +00:00
// We shouldn't initialize temp variables at all
// We put special segment 0xFFFF in there, so that uninitialized reads can get detected
for ( int i = 0 ; i < opparams [ 0 ] ; i + + )
2017-01-05 10:29:14 -06:00
s - > xs - > sp [ i ] = make_reg ( kUninitializedSegment , 0 ) ;
2010-06-24 07:54:02 +00:00
2010-06-06 23:00:33 +00:00
s - > xs - > sp + = opparams [ 0 ] ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_call : { // 0x20 (32)
2010-07-04 14:55:28 +00:00
// Call a script subroutine
2009-02-22 13:11:43 +00:00
int argc = ( opparams [ 1 ] > > 1 ) // Given as offset, but we need count
2011-03-27 18:13:42 +03:00
+ 1 + s - > r_rest ;
2010-06-06 23:00:33 +00:00
StackPtr call_base = s - > xs - > sp - argc ;
2012-06-23 21:38:46 +03:00
uint32 localCallOffset = s - > xs - > addr . pc . getOffset ( ) + opparams [ 0 ] ;
2010-06-26 08:29:55 +00:00
2016-04-01 23:54:00 +02:00
int final_argc = ( call_base - > requireUint16 ( ) ) + s - > r_rest ;
call_base [ 0 ] = make_reg ( 0 , final_argc ) ; // The first argument is argc
2011-06-20 00:59:48 +02:00
ExecStack xstack ( s - > xs - > objp , s - > xs - > objp , s - > xs - > sp ,
2016-04-01 23:54:00 +02:00
final_argc , call_base ,
2012-06-23 21:38:46 +03:00
s - > xs - > local_segment , make_reg32 ( s - > xs - > addr . pc . getSegment ( ) , localCallOffset ) ,
2016-04-02 00:04:00 +02:00
NULL_SELECTOR , - 1 , - 1 , - 1 , localCallOffset , s - > _executionStack . size ( ) - 1 ,
2011-03-26 02:37:10 +02:00
EXEC_STACK_TYPE_CALL ) ;
s - > _executionStack . push_back ( xstack ) ;
xs_new = & ( s - > _executionStack . back ( ) ) ;
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ; // Used up the &rest adjustment
2010-06-06 23:00:33 +00:00
s - > xs - > sp = call_base ;
2009-02-15 06:10:59 +00:00
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-15 06:10:59 +00:00
break ;
}
2010-01-28 13:07:47 +00:00
case op_callk : { // 0x21 (33)
2011-02-20 00:18:27 +02:00
// Run the garbage collector, if needed
if ( s - > gcCountDown - - < = 0 ) {
s - > gcCountDown = s - > scriptGCInterval ;
run_gc ( s ) ;
}
2009-02-15 06:10:59 +00:00
2011-02-20 00:18:27 +02:00
// Call kernel function
2010-06-06 23:00:33 +00:00
s - > xs - > sp - = ( opparams [ 1 ] > > 1 ) + 1 ;
2009-08-25 08:38:14 +00:00
2009-09-17 16:50:53 +00:00
bool oldScriptHeader = ( getSciVersion ( ) = = SCI_VERSION_0_EARLY ) ;
2010-06-09 07:32:17 +00:00
if ( ! oldScriptHeader )
2011-03-27 18:13:42 +03:00
s - > xs - > sp - = s - > r_rest ;
2009-02-15 06:10:59 +00:00
2011-02-15 11:25:54 +02:00
int argc = s - > xs - > sp [ 0 ] . requireUint16 ( ) ;
- Changed the unimplemented debug SCI kernel functions (InspectObj, ShowSends, ShowObjs, ShowFree, StackUsage and Profiler) to be dummy functions - we have our own debugger, and don't use these functions for debugging
- Removed the function number parameter from all kernel functions, as it's no longer needed, and removed the FAKE_FUNCT_NR hack
- Removed kUnknown() and kStub()
- Dummy/unknown kernel functions are no longer invoked, and a warning is shown instead, with the paremeters passed to them
Note: there is an evil hack used for debugging scripts in invoke_selector(), which probably no longer works now
svn-id: r44461
2009-09-29 14:24:07 +00:00
2010-02-07 17:57:51 +00:00
if ( ! oldScriptHeader )
2011-03-27 18:13:42 +03:00
argc + = s - > r_rest ;
- Changed the unimplemented debug SCI kernel functions (InspectObj, ShowSends, ShowObjs, ShowFree, StackUsage and Profiler) to be dummy functions - we have our own debugger, and don't use these functions for debugging
- Removed the function number parameter from all kernel functions, as it's no longer needed, and removed the FAKE_FUNCT_NR hack
- Removed kUnknown() and kStub()
- Dummy/unknown kernel functions are no longer invoked, and a warning is shown instead, with the paremeters passed to them
Note: there is an evil hack used for debugging scripts in invoke_selector(), which probably no longer works now
svn-id: r44461
2009-09-29 14:24:07 +00:00
2010-02-07 17:57:51 +00:00
callKernelFunc ( s , opparams [ 0 ] , argc ) ;
2009-02-15 06:10:59 +00:00
2010-02-07 17:57:51 +00:00
if ( ! oldScriptHeader )
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ;
2009-02-15 06:10:59 +00:00
2010-02-07 17:57:51 +00:00
// Calculate xs again: The kernel function might
// have spawned a new VM
2009-02-15 06:10:59 +00:00
2010-02-07 17:57:51 +00:00
xs_new = & ( s - > _executionStack . back ( ) ) ;
s - > _executionStackPosChanged = true ;
2010-06-25 17:25:00 +00:00
// If a game is being loaded, stop processing
2010-08-29 00:39:33 +00:00
if ( s - > abortScriptProcessing ! = kAbortNone )
2010-06-25 17:25:00 +00:00
return ; // Stop processing
2009-02-15 06:10:59 +00:00
break ;
2009-09-17 13:20:58 +00:00
}
2009-02-15 06:10:59 +00:00
2010-01-28 13:07:47 +00:00
case op_callb : // 0x22 (34)
2010-07-04 14:55:28 +00:00
// Call base script
2011-03-27 18:13:42 +03:00
temp = ( ( opparams [ 1 ] > > 1 ) + s - > r_rest + 1 ) ;
2010-06-06 23:00:33 +00:00
s_temp = s - > xs - > sp ;
s - > xs - > sp - = temp ;
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 0 ] . incOffset ( s - > r_rest ) ;
2010-06-06 23:00:33 +00:00
xs_new = execute_method ( s , 0 , opparams [ 0 ] , s_temp , s - > xs - > objp ,
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 0 ] . getOffset ( ) , s - > xs - > sp ) ;
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ; // Used up the &rest adjustment
2009-02-22 13:11:43 +00:00
if ( xs_new ) // in case of error, keep old stack
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-15 06:10:59 +00:00
break ;
2009-02-15 22:28:12 +00:00
2010-01-28 13:07:47 +00:00
case op_calle : // 0x23 (35)
2010-07-04 14:55:28 +00:00
// Call external script
2011-03-27 18:13:42 +03:00
temp = ( ( opparams [ 2 ] > > 1 ) + s - > r_rest + 1 ) ;
2010-06-06 23:00:33 +00:00
s_temp = s - > xs - > sp ;
s - > xs - > sp - = temp ;
2009-02-15 06:10:59 +00:00
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 0 ] . incOffset ( s - > r_rest ) ;
2010-06-06 23:00:33 +00:00
xs_new = execute_method ( s , opparams [ 0 ] , opparams [ 1 ] , s_temp , s - > xs - > objp ,
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 0 ] . getOffset ( ) , s - > xs - > sp ) ;
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ; // Used up the &rest adjustment
2009-02-22 13:11:43 +00:00
if ( xs_new ) // in case of error, keep old stack
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_ret : // 0x24 (36)
2010-07-04 14:55:28 +00:00
// Return from an execution loop started by call, calle, callb, send, self or super
2009-02-15 06:10:59 +00:00
do {
2010-06-06 23:00:33 +00:00
StackPtr old_sp2 = s - > xs - > sp ;
StackPtr old_fp = s - > xs - > fp ;
2009-05-18 18:15:45 +00:00
ExecStack * old_xs = & ( s - > _executionStack . back ( ) ) ;
2009-02-15 06:10:59 +00:00
2010-06-10 11:18:10 +00:00
if ( ( int ) s - > _executionStack . size ( ) - 1 = = s - > executionStackBase ) { // Have we reached the base?
s - > executionStackBase = old_executionStackBase ; // Restore stack base
2009-02-15 06:10:59 +00:00
2009-05-18 18:15:45 +00:00
s - > _executionStack . pop_back ( ) ;
2009-02-15 06:10:59 +00:00
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-22 13:11:43 +00:00
return ; // "Hard" return
2009-02-15 06:10:59 +00:00
}
if ( old_xs - > type = = EXEC_STACK_TYPE_VARSELECTOR ) {
2009-02-22 13:11:43 +00:00
// varselector access?
2009-10-04 18:38:18 +00:00
reg_t * var = old_xs - > getVarPointer ( s - > _segMan ) ;
2016-11-19 19:13:17 +01:00
if ( old_xs - > argc ) { // write?
2009-09-21 21:39:00 +00:00
* var = old_xs - > variables_argp [ 1 ] ;
2016-11-19 19:13:17 +01:00
# ifdef ENABLE_SCI32
updateInfoFlagViewVisible ( s - > _segMan - > getObject ( old_xs - > addr . varp . obj ) , old_xs - > addr . varp . varindex ) ;
# endif
} else // No, read
2009-09-21 21:39:00 +00:00
s - > r_acc = * var ;
2009-02-15 06:10:59 +00:00
}
2009-02-22 13:11:43 +00:00
// Not reached the base, so let's do a soft return
2009-05-18 18:15:45 +00:00
s - > _executionStack . pop_back ( ) ;
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2010-06-06 23:00:33 +00:00
s - > xs = & ( s - > _executionStack . back ( ) ) ;
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
if ( s - > xs - > sp = = CALL_SP_CARRY // Used in sends to 'carry' the stack pointer
| | s - > xs - > type ! = EXEC_STACK_TYPE_CALL ) {
s - > xs - > sp = old_sp2 ;
s - > xs - > fp = old_fp ;
2009-02-15 06:10:59 +00:00
}
2010-06-06 23:00:33 +00:00
} while ( s - > xs - > type = = EXEC_STACK_TYPE_VARSELECTOR ) ;
2009-02-22 13:11:43 +00:00
// Iterate over all varselector accesses
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2010-06-06 23:00:33 +00:00
xs_new = s - > xs ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_send : // 0x25 (37)
2010-07-04 14:55:28 +00:00
// Send for one or more selectors
2010-06-06 23:00:33 +00:00
s_temp = s - > xs - > sp ;
2011-03-27 18:13:42 +03:00
s - > xs - > sp - = ( ( opparams [ 0 ] > > 1 ) + s - > r_rest ) ; // Adjust stack
2009-02-15 06:10:59 +00:00
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 1 ] . incOffset ( s - > r_rest ) ;
2009-07-11 06:19:29 +00:00
xs_new = send_selector ( s , s - > r_acc , s - > r_acc , s_temp ,
2011-03-27 18:13:42 +03:00
( int ) ( opparams [ 0 ] > > 1 ) + ( uint16 ) s - > r_rest , s - > xs - > sp ) ;
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
if ( xs_new & & xs_new ! = s - > xs )
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-15 06:10:59 +00:00
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ;
2009-02-15 06:10:59 +00:00
break ;
2017-01-05 10:33:54 -06:00
case op_info : // (38)
2016-02-05 23:52:18 +02:00
if ( getSciVersion ( ) < SCI_VERSION_3 )
2010-11-17 20:43:51 +00:00
error ( " Dummy opcode 0x%x called " , opcode ) ; // should never happen
2016-02-05 23:52:18 +02:00
if ( ! ( extOpcode & 1 ) )
s - > r_acc = obj - > getInfoSelector ( ) ;
else
PUSH32 ( obj - > getInfoSelector ( ) ) ;
break ;
2017-01-05 10:33:54 -06:00
case op_superP : // (39)
2016-02-05 23:52:18 +02:00
if ( getSciVersion ( ) < SCI_VERSION_3 )
error ( " Dummy opcode 0x%x called " , opcode ) ; // should never happen
if ( ! ( extOpcode & 1 ) )
s - > r_acc = obj - > getSuperClassSelector ( ) ;
else
PUSH32 ( obj - > getSuperClassSelector ( ) ) ;
2010-01-28 13:07:47 +00:00
break ;
case op_class : // 0x28 (40)
2010-07-04 14:55:28 +00:00
// Get class address
2009-10-04 18:38:18 +00:00
s - > r_acc = s - > _segMan - > getClassAddress ( ( unsigned ) opparams [ 0 ] , SCRIPT_GET_LOCK ,
2012-06-18 05:21:59 +03:00
s - > xs - > addr . pc . getSegment ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case 0x29 : // (41)
2010-02-07 17:57:25 +00:00
error ( " Dummy opcode 0x%x called " , opcode ) ; // should never happen
2010-01-28 13:07:47 +00:00
break ;
case op_self : // 0x2a (42)
2010-07-04 14:55:28 +00:00
// Send to self
2010-06-06 23:00:33 +00:00
s_temp = s - > xs - > sp ;
2011-03-27 18:13:42 +03:00
s - > xs - > sp - = ( ( opparams [ 0 ] > > 1 ) + s - > r_rest ) ; // Adjust stack
2009-02-15 06:10:59 +00:00
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 1 ] . incOffset ( s - > r_rest ) ;
2010-06-06 23:00:33 +00:00
xs_new = send_selector ( s , s - > xs - > objp , s - > xs - > objp ,
2011-03-27 18:13:42 +03:00
s_temp , ( int ) ( opparams [ 0 ] > > 1 ) + ( uint16 ) s - > r_rest ,
2010-06-06 23:00:33 +00:00
s - > xs - > sp ) ;
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
if ( xs_new & & xs_new ! = s - > xs )
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-15 06:10:59 +00:00
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_super : // 0x2b (43)
2010-07-04 14:55:28 +00:00
// Send to any class
2012-06-18 05:21:59 +03:00
r_temp = s - > _segMan - > getClassAddress ( opparams [ 0 ] , SCRIPT_GET_LOAD , s - > xs - > addr . pc . getSegment ( ) ) ;
2009-02-15 06:10:59 +00:00
2011-02-17 17:33:36 +02:00
if ( ! r_temp . isPointer ( ) )
2009-07-03 21:59:07 +00:00
error ( " [VM]: Invalid superclass in object " ) ;
2009-02-15 06:10:59 +00:00
else {
SCI32: Put superclass address in r_acc for SCI3 super calls
This fixes a problem in Lighthouse 2.0a where the mini-sub would
fail to start playing the animation of the shipwreck when clicking
on the throttle.
In SSCI, in SCI3 only, r_acc was (inadvertently?) set to the
superclass object ID whenever a super call was made. This happened
because OP_super would call to get the superclass object ID, the
calling conventions of the compiler put this return value into EAX,
and then the PMachine message processing code put whatever was in
EAX into r_acc before each message was processed.
In the game code, there are a sequence of steps that look like
this:
* First, throttle::doVerb is called when throttle is clicked on;
* Which calls getRobot::doit to tell the shipwreck robot to start
playing;
* Which calls wreckBot::init to reset the Robot for the animation;
* Which calls Hiliter::hotVerbs(0) to remove cursor hotspots;
* Which calls Hiliter::dispose to clean up since it is not used;
* Which causes Hiliter::verbList to get set to 0.
* Later, verbList is loaded into r_acc, and it is still 0;
* Then, Hiliter::dispose makes a super call to Obj::dispose;
* Then, Obj::dispose does nothing except call kDisposeClone,
which does not mutate r_acc, so r_acc is still 0 from verbList;
* Then we return back through 5 calls to throttle::doVerb;
* Then throttle::doVerb checks that r_acc is non-zero, and if so,
adds wreckBot to theDoits global, allowing the animation to
occur.
In ScummVM, without setting r_acc in the super call, the non-zero
check failed and the wreckBot didn't get put into theDoits, so the
entire sequence fell apart. In SSCI, the non-zero check happened
to succeed because the Obj::dispose super call put the Obj class
into the accumulator. So now we do that too, and now Lighthouse
2.0a works here.
Earlier versions of SSCI used EAX for other things in between the
OP_super call and the message processing, so would set r_acc from
different data, so this change does not apply to those versions.
2017-09-19 01:37:44 -05:00
// SCI3 sets r_acc to whatever was in EAX at the start of a
// send. In the case of a super call this is the object ID of
// the superclass, as determined by the interpreter, rather than
// by the game scripts
if ( getSciVersion ( ) = = SCI_VERSION_3 ) {
s - > r_acc = r_temp ;
}
2010-06-06 23:00:33 +00:00
s_temp = s - > xs - > sp ;
2011-03-27 18:13:42 +03:00
s - > xs - > sp - = ( ( opparams [ 1 ] > > 1 ) + s - > r_rest ) ; // Adjust stack
2009-02-15 06:10:59 +00:00
2012-06-18 05:21:59 +03:00
s - > xs - > sp [ 1 ] . incOffset ( s - > r_rest ) ;
2010-06-06 23:00:33 +00:00
xs_new = send_selector ( s , r_temp , s - > xs - > objp , s_temp ,
2011-03-27 18:13:42 +03:00
( int ) ( opparams [ 1 ] > > 1 ) + ( uint16 ) s - > r_rest ,
2010-06-06 23:00:33 +00:00
s - > xs - > sp ) ;
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
if ( xs_new & & xs_new ! = s - > xs )
2009-04-28 15:58:19 +00:00
s - > _executionStackPosChanged = true ;
2009-02-15 06:10:59 +00:00
2011-03-27 18:13:42 +03:00
s - > r_rest = 0 ;
2009-02-15 06:10:59 +00:00
}
break ;
2010-01-28 13:07:47 +00:00
case op_rest : // 0x2c (44)
2010-07-04 14:55:28 +00:00
// Pushes all or part of the parameter variable list on the stack
2017-02-03 23:05:27 -06:00
// Index 0 is argc, so normally this will be called as &rest 1 to
// forward all the arguments.
2009-02-22 13:11:43 +00:00
temp = ( uint16 ) opparams [ 0 ] ; // First argument
2011-03-27 18:13:42 +03:00
s - > r_rest = MAX < int16 > ( s - > xs - > argc - temp + 1 , 0 ) ; // +1 because temp counts the paramcount while argc doesn't
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
for ( ; temp < = s - > xs - > argc ; temp + + )
PUSH32 ( s - > xs - > variables_argp [ temp ] ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_lea : // 0x2d (45)
2010-07-04 14:55:28 +00:00
// Load Effective Address
2009-02-21 21:16:41 +00:00
temp = ( uint16 ) opparams [ 0 ] > > 1 ;
2009-02-22 13:11:43 +00:00
var_number = temp & 0x03 ; // Get variable type
2009-02-15 06:10:59 +00:00
2009-02-22 13:11:43 +00:00
// Get variable block offset
2012-06-18 05:21:59 +03:00
r_temp . setSegment ( s - > variablesSegment [ var_number ] ) ;
r_temp . setOffset ( s - > variables [ var_number ] - s - > variablesBase [ var_number ] ) ;
2009-02-15 06:10:59 +00:00
2009-02-22 13:11:43 +00:00
if ( temp & 0x08 ) // Add accumulator offset if requested
2012-06-18 05:21:59 +03:00
r_temp . incOffset ( s - > r_acc . requireSint16 ( ) ) ;
2009-02-15 06:10:59 +00:00
2012-06-18 05:21:59 +03:00
r_temp . incOffset ( opparams [ 1 ] ) ; // Add index
r_temp . setOffset ( r_temp . getOffset ( ) * 2 ) ; // variables are 16 bit
2009-02-22 13:11:43 +00:00
// That's the immediate address now
2009-02-15 06:10:59 +00:00
s - > r_acc = r_temp ;
break ;
2010-01-28 13:07:47 +00:00
case op_selfID : // 0x2e (46)
2010-07-04 14:55:28 +00:00
// Get 'self' identity
2010-06-06 23:00:33 +00:00
s - > r_acc = s - > xs - > objp ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case 0x2f : // (47)
2010-02-07 17:57:25 +00:00
error ( " Dummy opcode 0x%x called " , opcode ) ; // should never happen
2010-01-28 13:07:47 +00:00
break ;
case op_pprev : // 0x30 (48)
2010-07-04 14:55:28 +00:00
// Pushes the value of the prev register, set by the last comparison
// bytecode (eq?, lt?, etc.), on the stack
2009-02-15 06:10:59 +00:00
PUSH32 ( s - > r_prev ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_pToa : // 0x31 (49)
2010-07-04 14:55:28 +00:00
// Property To Accumulator
2017-05-26 12:52:55 +02:00
if ( g_sci - > _debugState . _activeBreakpointTypes & BREAK_SELECTORREAD ) {
2020-01-09 17:53:11 -08:00
debugPropertyAccess ( obj , s - > xs - > objp , opparams [ 0 ] , NULL_SELECTOR ,
2017-05-26 12:52:55 +02:00
validate_property ( s , obj , opparams [ 0 ] ) , NULL_REG ,
s - > _segMan , BREAK_SELECTORREAD ) ;
}
2010-11-17 17:21:02 +00:00
s - > r_acc = validate_property ( s , obj , opparams [ 0 ] ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_aTop : // 0x32 (50)
2017-05-26 12:52:55 +02:00
{
2010-07-04 14:55:28 +00:00
// Accumulator To Property
2017-05-26 12:52:55 +02:00
reg_t & opProperty = validate_property ( s , obj , opparams [ 0 ] ) ;
if ( g_sci - > _debugState . _activeBreakpointTypes & BREAK_SELECTORWRITE ) {
2020-01-09 17:53:11 -08:00
debugPropertyAccess ( obj , s - > xs - > objp , opparams [ 0 ] , NULL_SELECTOR ,
2017-05-26 12:52:55 +02:00
opProperty , s - > r_acc ,
s - > _segMan , BREAK_SELECTORWRITE ) ;
}
opProperty = s - > r_acc ;
2016-02-24 00:12:29 +01:00
# ifdef ENABLE_SCI32
SCI32: Fix mustSetViewVisible for SCI3
In SCI2/2.1, variable indexes are used along with a range encoded
in the interpreter executable to determine whether an object
variable is a view-related variable. Operands to aTop, sTop, ipToa,
dpToa, ipTos, and dpTos are byte offsets into an object, which
are divided by two to get the varindex to check against the
interpreter range.
In SCI3, objects in game scripts contain groups of 32 selectors,
and each group has a flag that says whether or not the selectors
in that group are view-related. Operands to aTop, sTop, ipToa,
dpToa, ipTos, and dpTos are selectors.
2017-04-01 22:01:22 -05:00
updateInfoFlagViewVisible ( obj , opparams [ 0 ] , true ) ;
2016-02-24 00:12:29 +01:00
# endif
2009-02-15 06:10:59 +00:00
break ;
2017-05-26 12:52:55 +02:00
}
2009-02-15 06:10:59 +00:00
2010-01-28 13:07:47 +00:00
case op_pTos : // 0x33 (51)
2017-05-26 12:52:55 +02:00
{
2010-07-04 14:55:28 +00:00
// Property To Stack
2017-05-26 12:52:55 +02:00
reg_t value = validate_property ( s , obj , opparams [ 0 ] ) ;
if ( g_sci - > _debugState . _activeBreakpointTypes & BREAK_SELECTORREAD ) {
2020-01-09 17:53:11 -08:00
debugPropertyAccess ( obj , s - > xs - > objp , opparams [ 0 ] , NULL_SELECTOR ,
2017-05-26 12:52:55 +02:00
value , NULL_REG ,
s - > _segMan , BREAK_SELECTORREAD ) ;
}
PUSH32 ( value ) ;
2009-02-15 06:10:59 +00:00
break ;
2017-05-26 12:52:55 +02:00
}
2009-02-15 06:10:59 +00:00
2010-01-28 13:07:47 +00:00
case op_sTop : // 0x34 (52)
2017-05-26 12:52:55 +02:00
{
2010-07-04 14:55:28 +00:00
// Stack To Property
2017-05-26 12:52:55 +02:00
reg_t newValue = POP32 ( ) ;
reg_t & opProperty = validate_property ( s , obj , opparams [ 0 ] ) ;
if ( g_sci - > _debugState . _activeBreakpointTypes & BREAK_SELECTORWRITE ) {
2020-01-09 17:53:11 -08:00
debugPropertyAccess ( obj , s - > xs - > objp , opparams [ 0 ] , NULL_SELECTOR ,
2017-05-26 12:52:55 +02:00
opProperty , newValue ,
s - > _segMan , BREAK_SELECTORWRITE ) ;
}
opProperty = newValue ;
2016-02-24 00:12:29 +01:00
# ifdef ENABLE_SCI32
SCI32: Fix mustSetViewVisible for SCI3
In SCI2/2.1, variable indexes are used along with a range encoded
in the interpreter executable to determine whether an object
variable is a view-related variable. Operands to aTop, sTop, ipToa,
dpToa, ipTos, and dpTos are byte offsets into an object, which
are divided by two to get the varindex to check against the
interpreter range.
In SCI3, objects in game scripts contain groups of 32 selectors,
and each group has a flag that says whether or not the selectors
in that group are view-related. Operands to aTop, sTop, ipToa,
dpToa, ipTos, and dpTos are selectors.
2017-04-01 22:01:22 -05:00
updateInfoFlagViewVisible ( obj , opparams [ 0 ] , true ) ;
2016-02-24 00:12:29 +01:00
# endif
2009-02-15 06:10:59 +00:00
break ;
2017-05-26 12:52:55 +02:00
}
2009-02-15 06:10:59 +00:00
2011-02-17 17:33:36 +02:00
case op_ipToa : // 0x35 (53)
case op_dpToa : // 0x36 (54)
case op_ipTos : // 0x37 (55)
case op_dpTos : // 0x38 (56)
{
// Increment/decrement a property and copy to accumulator,
// or push to stack
2010-11-17 17:21:02 +00:00
reg_t & opProperty = validate_property ( s , obj , opparams [ 0 ] ) ;
2017-05-26 12:52:55 +02:00
reg_t oldValue = opProperty ;
if ( g_sci - > _debugState . _activeBreakpointTypes & BREAK_SELECTORREAD ) {
2020-01-09 17:53:11 -08:00
debugPropertyAccess ( obj , s - > xs - > objp , opparams [ 0 ] , NULL_SELECTOR ,
2017-05-26 12:52:55 +02:00
oldValue , NULL_REG ,
s - > _segMan , BREAK_SELECTORREAD ) ;
}
2011-02-17 17:33:36 +02:00
if ( opcode & 1 )
opProperty + = 1 ;
2010-07-18 18:01:52 +00:00
else
2011-02-17 17:33:36 +02:00
opProperty - = 1 ;
2017-05-26 12:52:55 +02:00
if ( g_sci - > _debugState . _activeBreakpointTypes & BREAK_SELECTORWRITE ) {
2020-01-09 17:53:11 -08:00
debugPropertyAccess ( obj , s - > xs - > objp , opparams [ 0 ] , NULL_SELECTOR ,
2017-05-26 12:52:55 +02:00
oldValue , opProperty ,
s - > _segMan , BREAK_SELECTORWRITE ) ;
}
2016-02-24 00:12:29 +01:00
# ifdef ENABLE_SCI32
SCI32: Fix mustSetViewVisible for SCI3
In SCI2/2.1, variable indexes are used along with a range encoded
in the interpreter executable to determine whether an object
variable is a view-related variable. Operands to aTop, sTop, ipToa,
dpToa, ipTos, and dpTos are byte offsets into an object, which
are divided by two to get the varindex to check against the
interpreter range.
In SCI3, objects in game scripts contain groups of 32 selectors,
and each group has a flag that says whether or not the selectors
in that group are view-related. Operands to aTop, sTop, ipToa,
dpToa, ipTos, and dpTos are selectors.
2017-04-01 22:01:22 -05:00
updateInfoFlagViewVisible ( obj , opparams [ 0 ] , true ) ;
2016-02-24 00:12:29 +01:00
# endif
2011-02-17 17:33:36 +02:00
if ( opcode = = op_ipToa | | opcode = = op_dpToa )
s - > r_acc = opProperty ;
2010-07-18 18:01:52 +00:00
else
2011-02-17 17:33:36 +02:00
PUSH32 ( opProperty ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-07-18 18:01:52 +00:00
}
2009-02-15 06:10:59 +00:00
2010-01-28 13:07:47 +00:00
case op_lofsa : // 0x39 (57)
2016-11-05 17:57:34 -05:00
case op_lofss : { // 0x3a (58)
2011-02-17 17:33:36 +02:00
// Load offset to accumulator or push to stack
2009-07-07 07:51:26 +00:00
2016-11-05 17:57:34 -05:00
r_temp . setSegment ( s - > xs - > addr . pc . getSegment ( ) ) ;
r_temp . setOffset ( findOffset ( opparams [ 0 ] , local_script , s - > xs - > addr . pc . getOffset ( ) ) ) ;
2012-06-18 05:21:59 +03:00
if ( r_temp . getOffset ( ) > = scr - > getBufSize ( ) )
2011-02-17 17:33:36 +02:00
error ( " VM: lofsa/lofss operation overflowed: %04x:%04x beyond end "
2016-11-05 17:57:34 -05:00
" of script (at %04x) " , PRINT_REG ( r_temp ) , scr - > getBufSize ( ) ) ;
2011-02-17 17:33:36 +02:00
if ( opcode = = op_lofsa )
s - > r_acc = r_temp ;
else
PUSH32 ( r_temp ) ;
2009-02-15 06:10:59 +00:00
break ;
2016-11-05 17:57:34 -05:00
}
2009-02-15 06:10:59 +00:00
2010-01-28 13:07:47 +00:00
case op_push0 : // 0x3b (59)
2009-02-15 06:10:59 +00:00
PUSH ( 0 ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_push1 : // 0x3c (60)
2009-02-15 06:10:59 +00:00
PUSH ( 1 ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_push2 : // 0x3d (61)
2009-02-15 06:10:59 +00:00
PUSH ( 2 ) ;
break ;
2010-01-28 13:07:47 +00:00
case op_pushSelf : // 0x3e (62)
2010-11-09 11:57:54 +00:00
// Compensate for a bug in non-Sierra compilers, which seem to generate
// pushSelf instructions with the low bit set. This makes the following
// heuristic fail and leads to endless loops and crashes. Our
// interpretation of this seems correct, as other SCI tools, like for
// example SCI Viewer, have issues with these scripts (e.g. script 999
2021-02-25 01:09:19 -08:00
// in Circus Quest). Fixes bug #5113.
2010-11-09 10:07:34 +00:00
if ( ! ( extOpcode & 1 ) | | g_sci - > getGameId ( ) = = GID_FANMADE ) {
2010-06-06 23:00:33 +00:00
PUSH32 ( s - > xs - > objp ) ;
2010-01-28 19:22:58 +00:00
} else {
2011-01-07 20:24:49 +00:00
// Debug opcode op_file
2010-01-28 19:22:58 +00:00
}
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 19:22:58 +00:00
case op_line : // 0x3f (63)
// Debug opcode (line number)
2012-07-26 11:06:19 +03:00
//debug("Script %d, line %d", scr->getScriptNumber(), opparams[0]);
2010-01-28 13:07:47 +00:00
break ;
case op_lag : // 0x40 (64)
case op_lal : // 0x41 (65)
case op_lat : // 0x42 (66)
case op_lap : // 0x43 (67)
2010-07-04 14:55:28 +00:00
// Load global, local, temp or param variable into the accumulator
2011-02-17 17:33:36 +02:00
case op_lagi : // 0x48 (72)
case op_lali : // 0x49 (73)
case op_lati : // 0x4a (74)
case op_lapi : // 0x4b (75)
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_lagi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
s - > r_acc = read_var ( s , var_type , var_number ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_lsg : // 0x44 (68)
case op_lsl : // 0x45 (69)
case op_lst : // 0x46 (70)
case op_lsp : // 0x47 (71)
2010-07-04 14:55:28 +00:00
// Load global, local, temp or param variable into the stack
2010-01-28 13:07:47 +00:00
case op_lsgi : // 0x4c (76)
case op_lsli : // 0x4d (77)
case op_lsti : // 0x4e (78)
2011-02-17 17:33:36 +02:00
case op_lspi : // 0x4f (79)
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_lsgi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
PUSH32 ( read_var ( s , var_type , var_number ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_sag : // 0x50 (80)
case op_sal : // 0x51 (81)
case op_sat : // 0x52 (82)
case op_sap : // 0x53 (83)
2010-07-04 14:55:28 +00:00
// Save the accumulator into the global, local, temp or param variable
2010-01-28 13:07:47 +00:00
case op_sagi : // 0x58 (88)
case op_sali : // 0x59 (89)
case op_sati : // 0x5a (90)
case op_sapi : // 0x5b (91)
2010-07-04 14:55:28 +00:00
// Save the accumulator into the global, local, temp or param variable,
// using the accumulator as an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_sagi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
if ( opcode > = op_sagi ) // load the actual value to store in the accumulator
s - > r_acc = POP32 ( ) ;
2011-03-26 12:25:48 +02:00
write_var ( s , var_type , var_number , s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2011-02-17 17:33:36 +02:00
case op_ssg : // 0x54 (84)
case op_ssl : // 0x55 (85)
case op_sst : // 0x56 (86)
case op_ssp : // 0x57 (87)
// Save the stack into the global, local, temp or param variable
2010-01-28 13:07:47 +00:00
case op_ssgi : // 0x5c (92)
case op_ssli : // 0x5d (93)
case op_ssti : // 0x5e (94)
case op_sspi : // 0x5f (95)
2011-02-17 17:33:36 +02:00
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_ssgi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
write_var ( s , var_type , var_number , POP32 ( ) ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_plusag : // 0x60 (96)
case op_plusal : // 0x61 (97)
case op_plusat : // 0x62 (98)
case op_plusap : // 0x63 (99)
2010-07-04 14:55:28 +00:00
// Increment the global, local, temp or param variable and save it
// to the accumulator
2011-02-17 17:33:36 +02:00
case op_plusagi : // 0x68 (104)
case op_plusali : // 0x69 (105)
case op_plusati : // 0x6a (106)
case op_plusapi : // 0x6b (107)
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_plusagi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
s - > r_acc = read_var ( s , var_type , var_number ) + 1 ;
write_var ( s , var_type , var_number , s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_plussg : // 0x64 (100)
case op_plussl : // 0x65 (101)
case op_plusst : // 0x66 (102)
case op_plussp : // 0x67 (103)
2010-07-04 14:55:28 +00:00
// Increment the global, local, temp or param variable and save it
// to the stack
2010-01-28 13:07:47 +00:00
case op_plussgi : // 0x6c (108)
case op_plussli : // 0x6d (109)
case op_plussti : // 0x6e (110)
case op_plusspi : // 0x6f (111)
2011-02-17 17:33:36 +02:00
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_plussgi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
r_temp = read_var ( s , var_type , var_number ) + 1 ;
2009-02-15 06:10:59 +00:00
PUSH32 ( r_temp ) ;
2011-03-26 12:25:48 +02:00
write_var ( s , var_type , var_number , r_temp ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_minusag : // 0x70 (112)
case op_minusal : // 0x71 (113)
case op_minusat : // 0x72 (114)
case op_minusap : // 0x73 (115)
2010-07-04 14:55:28 +00:00
// Decrement the global, local, temp or param variable and save it
// to the accumulator
2011-02-17 17:33:36 +02:00
case op_minusagi : // 0x78 (120)
case op_minusali : // 0x79 (121)
case op_minusati : // 0x7a (122)
case op_minusapi : // 0x7b (123)
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_minusagi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
s - > r_acc = read_var ( s , var_type , var_number ) - 1 ;
write_var ( s , var_type , var_number , s - > r_acc ) ;
2009-02-15 06:10:59 +00:00
break ;
2010-01-28 13:07:47 +00:00
case op_minussg : // 0x74 (116)
case op_minussl : // 0x75 (117)
case op_minusst : // 0x76 (118)
case op_minussp : // 0x77 (119)
2010-07-04 14:55:28 +00:00
// Decrement the global, local, temp or param variable and save it
// to the stack
2010-01-28 13:07:47 +00:00
case op_minussgi : // 0x7c (124)
case op_minussli : // 0x7d (125)
case op_minussti : // 0x7e (126)
case op_minusspi : // 0x7f (127)
2011-02-17 17:33:36 +02:00
// Same as the 4 ones above, except that the accumulator is used as
// an additional index
2010-02-07 17:57:25 +00:00
var_type = opcode & 0x3 ; // Gets the variable type: g, l, t or p
2011-02-17 17:33:36 +02:00
var_number = opparams [ 0 ] + ( opcode > = op_minussgi ? s - > r_acc . requireSint16 ( ) : 0 ) ;
2011-03-26 12:25:48 +02:00
r_temp = read_var ( s , var_type , var_number ) - 1 ;
2009-02-15 06:10:59 +00:00
PUSH32 ( r_temp ) ;
2011-03-26 12:25:48 +02:00
write_var ( s , var_type , var_number , r_temp ) ;
2009-02-15 06:10:59 +00:00
break ;
default :
2010-02-07 17:57:25 +00:00
error ( " run_vm(): illegal opcode %x " , opcode ) ;
2009-02-15 06:10:59 +00:00
2010-02-07 17:57:25 +00:00
} // switch (opcode)
2009-02-15 06:10:59 +00:00
2009-04-28 15:58:19 +00:00
if ( s - > _executionStackPosChanged ) // Force initialization
2010-06-06 23:00:33 +00:00
s - > xs = xs_new ;
2009-02-15 06:10:59 +00:00
2010-06-06 23:00:33 +00:00
if ( s - > xs ! = & ( s - > _executionStack . back ( ) ) ) {
2010-06-17 23:50:28 +00:00
error ( " xs is stale (%p vs %p); last command was %02x " ,
2010-06-06 23:00:33 +00:00
( void * ) s - > xs , ( void * ) & ( s - > _executionStack . back ( ) ) ,
2010-02-07 17:57:25 +00:00
opcode ) ;
2009-02-15 06:10:59 +00:00
}
2010-06-10 11:18:10 +00:00
+ + s - > scriptStepCounter ;
2009-02-15 06:10:59 +00:00
}
}
2010-05-26 16:30:10 +00:00
reg_t * ObjVarRef : : getPointer ( SegManager * segMan ) const {
2009-09-12 00:10:07 +00:00
Object * o = segMan - > getObject ( obj ) ;
2010-05-26 16:30:10 +00:00
return o ? & o - > getVariableRef ( varindex ) : 0 ;
2009-06-06 11:38:20 +00:00
}
2010-05-26 16:30:10 +00:00
reg_t * ExecStack : : getVarPointer ( SegManager * segMan ) const {
2009-06-06 11:38:20 +00:00
assert ( type = = EXEC_STACK_TYPE_VARSELECTOR ) ;
2009-09-06 12:57:42 +00:00
return addr . varp . getPointer ( segMan ) ;
2009-06-06 11:38:20 +00:00
}
2009-05-28 22:42:18 +00:00
2009-02-21 10:23:36 +00:00
} // End of namespace Sci