Package lab06
Class Instruction
java.lang.Object
lab06.Instruction
public class Instruction
extends java.lang.Object
Model a single Pippin Instruction.
A Pippin Instruction consists of:
- An operation - the kind of operation that the CPU should perform
- A mode - How the instruction argument should be interpreted
- An argument - an integer value
- Author:
- cs140
-
Constructor Summary
Constructors Constructor Description Instruction(int opcode, int mode, int argument)Construct an instruction object using the opcode, mode, and argument parameters. -
Method Summary
Modifier and Type Method Description static intencodeOpModeArg(int opcode, int mode, int arg)Generate binary encoded instruction from the parameters.voidexecute(CPU cpu)execute this instruction.static Instructionfactory(int binInstr)Generate an InstructionXXX object from the parameters.static Instructionfactory(int opcode, int mode, int argument)Generate an InstructionXXX object from the parameters.static Instructionfactory(java.lang.String opName, java.lang.String modeName, int argument)Generate an InstructionXXX object from the parameters.intfetchOperand(CPU cpu)Fetch the value of the operand, based on the mode, for this instructionstatic intfindMode(java.lang.String name)Find the index of the parameter in the modeNames array.static intfindOpcode(java.lang.String name)Find the index of the parameter in the opNames array.intgetArgument()get the argument value from this instruction.static intgetEncodedArg(int binInstr)Extract and return the argument from the binary encoded instruction.static intgetEncodedMode(int binInstr)Extract and return the mode from the binary encoded instruction.static intgetEncodedOpcode(int binInstr)Extract and return the opcode from the binary encoded instruction.intgetMode()get the mode value from this instruction.java.lang.StringgetModeName()get the three character mode mnemonic for this instruction.intgetOpcode()get the opcode for this instructionjava.lang.StringgetOpName()get the three character operation name (mnemonic) for this instruction.booleanisModeIMMorDIR()Determine if this mode is either IMM or DIR.booleanisModeValid()Determine if the mode for this instruction is valid for the current operation.booleanisValid()Determine if everything in this instruction is valid.voidstore(Memory mem, int loc)Store this instruction in the specified memory at the specified location.java.lang.StringtoString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Constructor Details
-
Instruction
public Instruction(int opcode, int mode, int argument)Construct an instruction object using the opcode, mode, and argument parameters.- Parameters:
opcode- index into the opNames array for this operationmode- index into the modeNames array to specify the mode for this instructionargument- the value of the argument to be used.
-
-
Method Details
-
factory
public static Instruction factory(java.lang.String opName, java.lang.String modeName, int argument)Generate an InstructionXXX object from the parameters. XXX is one of the valid operation names. If the operation is not recognized, returns an InstructionNOP.- Parameters:
opName- operation namemodeName- mode nameargument- argument value- Returns:
- a reference to an InstructionXXX object
-
factory
Generate an InstructionXXX object from the parameters. XXX is one of the valid operation names. If the operation is not recognized, returns an InstructionNOP- Parameters:
binInstr- binary encoded instruction- Returns:
- a reference to an InstructionXXX object
-
factory
Generate an InstructionXXX object from the parameters. XXX is one of the valid operation names. If the operation is not recognized, returns an InstructionNOP.- Parameters:
opcode- index into the opNames arraymode- index into the modeNames arrayargument- argument value- Returns:
- a reference to an InstructionXXX object
-
store
Store this instruction in the specified memory at the specified location. The store method will write over whatever used to be in memory with the binary encoded instruction.- Parameters:
mem- The memory in which to store the instructionloc- the location (index or address) where the instruction will be stored.
-
getOpName
public java.lang.String getOpName()get the three character operation name (mnemonic) for this instruction.- Returns:
- the operation name
-
getOpcode
public int getOpcode()get the opcode for this instruction- Returns:
- opcode
-
getModeName
public java.lang.String getModeName()get the three character mode mnemonic for this instruction.- Returns:
- the mode name (mnemonic)
-
getMode
public int getMode()get the mode value from this instruction.- Returns:
- the mode value
-
getArgument
public int getArgument()get the argument value from this instruction.- Returns:
- the argument value
-
fetchOperand
Fetch the value of the operand, based on the mode, for this instruction- Parameters:
cpu- The Pippin CPU to use when resolving the operand- Returns:
- the resolved operand.
-
isValid
public boolean isValid()Determine if everything in this instruction is valid. Checks include:- Does the instruction have a valid op-code
- Does the instruction have a valid mode, and is that mode consistent with the opcode
- Returns:
- true if this instruction is valid, false otherwise.
-
isModeValid
public boolean isModeValid()Determine if the mode for this instruction is valid for the current operation. Actual implementation is in the InstructionXXX sub-classes.- Returns:
- true if the mode is valid, false otherwise
-
isModeIMMorDIR
public boolean isModeIMMorDIR()Determine if this mode is either IMM or DIR.- Returns:
- true if mode is either IMM or DIR, false otherwise
-
execute
execute this instruction. The actual implementation is in each InstructionXXX sub-class- Parameters:
cpu- A Pippin CPU to use when executing this instruction
-
findOpcode
public static int findOpcode(java.lang.String name)Find the index of the parameter in the opNames array.- Parameters:
name- the operation name- Returns:
- the index of the operation in the opNames array. Returns -1 if the operation is not in the opNames array.
-
findMode
public static int findMode(java.lang.String name)Find the index of the parameter in the modeNames array.- Parameters:
name- the mode name- Returns:
- the index of the mode in the modeNames array. Returns -1 if the mode is not in the modeNames array.
-
encodeOpModeArg
public static int encodeOpModeArg(int opcode, int mode, int arg)Generate binary encoded instruction from the parameters.- Parameters:
opcode- index into the opNames arraymode- index into the modeNames arrayarg- argument value- Returns:
- binar encoded instruction
-
getEncodedOpcode
public static int getEncodedOpcode(int binInstr)Extract and return the opcode from the binary encoded instruction.- Parameters:
binInstr- binary encoded instruction- Returns:
- index into the opNames array extracted from the parameter
-
getEncodedMode
public static int getEncodedMode(int binInstr)Extract and return the mode from the binary encoded instruction.- Parameters:
binInstr- binary encoded instruction- Returns:
- index into the modeNames array extracted from the parameter
-
getEncodedArg
public static int getEncodedArg(int binInstr)Extract and return the argument from the binary encoded instruction.- Parameters:
binInstr- binary encoded instruction- Returns:
- argument value extracted from the parameter
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-