org.zmpp.vm
Class AbstractInstruction

java.lang.Object
  extended by org.zmpp.vm.AbstractInstruction
All Implemented Interfaces:
Instruction
Direct Known Subclasses:
LongInstruction, PrintLiteralInstruction, Short0Instruction, Short1Instruction, VariableInstruction

public abstract class AbstractInstruction
extends Object
implements Instruction

This class represents can be considered as a mutable value object, which basically stores an instruction's information in order to restrict the Instruction class's responsibility to executing logic. This information will be incrementally added by the decoder, therefore there are setter methods to add information.

Version:
1.0
Author:
Wei-ju Wu

Nested Class Summary
static class AbstractInstruction.InstructionForm
          The available instruction forms.
static class AbstractInstruction.OperandCount
          The available operand count types.
 
Field Summary
static short FALSE
          The constant for false.
static short TRUE
          The constant for true.
 
Constructor Summary
AbstractInstruction(Machine machineState, int opcode)
          Constructor.
 
Method Summary
 void addOperand(Operand operand)
          Adds an operand to this object.
 boolean branchIfTrue()
          Returns true if this is a branch condition and the branch is executed if the test condition is true, false otherwise.
protected  void branchOnTest(boolean condition)
          Performs a branch, depending on the state of the condition flag.
 short getBranchOffset()
          Returns the branch offset.
abstract  AbstractInstruction.InstructionForm getInstructionForm()
          Returns the instruction's form.
 int getLength()
          Returns the instruction's length in bytes.
protected  Machine getMachine()
          Returns the reference to the machine state.
 int getNumOperands()
          Returns the number of operands.
 int getOpcode()
          Returns the instruction's opcode.
 Operand getOperand(int operandNum)
          Returns the operand at the specified position.
abstract  AbstractInstruction.OperandCount getOperandCount()
          Returns the instruction's operand count type.
 int getStoreVariable()
          Returns the instruction's store variable.
 int getUnsignedValue(int operandNum)
          Retrieves the value of the specified operand as an unsigned 16 bit integer.
 short getValue(int operandNum)
          Converts the specified value into a signed value, depending on the type of the operand.
 boolean isBranch()
          Returns true, if this instruction is a branch, false, otherwise.
protected  void nextInstruction()
          Advances the program counter to the next instruction.
protected  void returnFromRoutine(short returnValue)
          This function returns from the current routine, setting the return value into the specified return variable.
 void setBranchIfTrue(boolean flag)
          Sets the branch if condition true flag.
 void setBranchOffset(short offset)
          Sets the branch offset.
 void setLength(int length)
          Sets the instruction's length in bytes.
 void setOpcode(int opcode)
          Sets the instruction's opcode.
 void setStoreVariable(int var)
          Sets the store variable.
protected  void storeResult(short value)
          Stores the specified value in the result variable.
 boolean storesResult()
          Returns true, if this instruction stores a result, false, otherwise.
protected  void throwInvalidOpcode()
          Halt the virtual machine with an error message about this instruction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.zmpp.vm.Instruction
execute
 

Field Detail

FALSE

public static final short FALSE
The constant for false.

See Also:
Constant Field Values

TRUE

public static final short TRUE
The constant for true.

See Also:
Constant Field Values
Constructor Detail

AbstractInstruction

public AbstractInstruction(Machine machineState,
                           int opcode)
Constructor.

Parameters:
machineState - a reference to the machine state
opcode - the opcode
Method Detail

getMachine

protected Machine getMachine()
Returns the reference to the machine state.

Returns:
the machine state

getOpcode

public int getOpcode()
Returns the instruction's opcode.

Returns:
the opcode

getInstructionForm

public abstract AbstractInstruction.InstructionForm getInstructionForm()
Returns the instruction's form.

Returns:
the instruction form

getOperandCount

public abstract AbstractInstruction.OperandCount getOperandCount()
Returns the instruction's operand count type.

Returns:
the operand count type

getOperand

public Operand getOperand(int operandNum)
Returns the operand at the specified position.

Parameters:
operandNum - the operand number, starting with 0 as the first operand.
Returns:
the specified operand

getNumOperands

public int getNumOperands()
Returns the number of operands.

Returns:
the number of operands

getStoreVariable

public int getStoreVariable()
Returns the instruction's store variable.

Returns:
the store variable

getBranchOffset

public short getBranchOffset()
Returns the branch offset.

Returns:
the branch offset

getLength

public int getLength()
Returns the instruction's length in bytes.

Returns:
the instruction length

setOpcode

public void setOpcode(int opcode)
Sets the instruction's opcode.

Parameters:
opcode - the opcode

addOperand

public void addOperand(Operand operand)
Adds an operand to this object.

Parameters:
operand - the operand to add

setStoreVariable

public void setStoreVariable(int var)
Sets the store variable.

Parameters:
var - the store variable

setBranchOffset

public void setBranchOffset(short offset)
Sets the branch offset.

Parameters:
offset - the branch offset

setBranchIfTrue

public void setBranchIfTrue(boolean flag)
Sets the branch if condition true flag.

Parameters:
flag - the branch if condition true flag

setLength

public void setLength(int length)
Sets the instruction's length in bytes.

Parameters:
length - the length in bytes

storesResult

public boolean storesResult()
Returns true, if this instruction stores a result, false, otherwise.

Returns:
true if a result is stored, false otherwise

isBranch

public boolean isBranch()
Returns true, if this instruction is a branch, false, otherwise.

Returns:
true if branch, false otherwise

branchIfTrue

public boolean branchIfTrue()
Returns true if this is a branch condition and the branch is executed if the test condition is true, false otherwise.

Returns:
true if the branch is executed on a true test condition

getValue

public short getValue(int operandNum)
Converts the specified value into a signed value, depending on the type of the operand. If the operand is LARGE_CONSTANT or VARIABLE, the value is treated as a 16 bit signed integer, if it is SMALL_CONSTANT, it is treated as an 8 bit signed integer.

Parameters:
operandNum - the operand number
Returns:
a signed value

getUnsignedValue

public int getUnsignedValue(int operandNum)
Retrieves the value of the specified operand as an unsigned 16 bit integer.

Parameters:
operandNum - the operand number
Returns:
the value

storeResult

protected void storeResult(short value)
Stores the specified value in the result variable.

Parameters:
value - the value to store

throwInvalidOpcode

protected void throwInvalidOpcode()
Halt the virtual machine with an error message about this instruction.


nextInstruction

protected void nextInstruction()
Advances the program counter to the next instruction.


branchOnTest

protected void branchOnTest(boolean condition)
Performs a branch, depending on the state of the condition flag. If branchIfConditionTrue is true, the branch will be performed if condition is true, if branchIfCondition is false, the branch will be performed if condition is false.

Parameters:
condition - the test condition

returnFromRoutine

protected void returnFromRoutine(short returnValue)
This function returns from the current routine, setting the return value into the specified return variable.

Parameters:
returnValue - the return value


Copyright © 2005 . All Rights Reserved.