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:
  1. An operation - the kind of operation that the CPU should perform
  2. A mode - How the instruction argument should be interpreted
  3. 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 int encodeOpModeArg​(int opcode, int mode, int arg)
    Generate binary encoded instruction from the parameters.
    void execute​(CPU cpu)
    execute this instruction.
    static Instruction factory​(int binInstr)
    Generate an InstructionXXX object from the parameters.
    static Instruction factory​(int opcode, int mode, int argument)
    Generate an InstructionXXX object from the parameters.
    static Instruction factory​(java.lang.String opName, java.lang.String modeName, int argument)
    Generate an InstructionXXX object from the parameters.
    int fetchOperand​(CPU cpu)
    Fetch the value of the operand, based on the mode, for this instruction
    static int findMode​(java.lang.String name)
    Find the index of the parameter in the modeNames array.
    static int findOpcode​(java.lang.String name)
    Find the index of the parameter in the opNames array.
    int getArgument()
    get the argument value from this instruction.
    static int getEncodedArg​(int binInstr)
    Extract and return the argument from the binary encoded instruction.
    static int getEncodedMode​(int binInstr)
    Extract and return the mode from the binary encoded instruction.
    static int getEncodedOpcode​(int binInstr)
    Extract and return the opcode from the binary encoded instruction.
    int getMode()
    get the mode value from this instruction.
    java.lang.String getModeName()
    get the three character mode mnemonic for this instruction.
    int getOpcode()
    get the opcode for this instruction
    java.lang.String getOpName()
    get the three character operation name (mnemonic) for this instruction.
    boolean isModeIMMorDIR()
    Determine if this mode is either IMM or DIR.
    boolean isModeValid()
    Determine if the mode for this instruction is valid for the current operation.
    boolean isValid()
    Determine if everything in this instruction is valid.
    void store​(Memory mem, int loc)
    Store this instruction in the specified memory at the specified location.
    java.lang.String toString()  

    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 operation
      mode - index into the modeNames array to specify the mode for this instruction
      argument - 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 name
      modeName - mode name
      argument - argument value
      Returns:
      a reference to an InstructionXXX object
    • factory

      public static Instruction factory​(int binInstr)
      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

      public static Instruction factory​(int opcode, int mode, 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:
      opcode - index into the opNames array
      mode - index into the modeNames array
      argument - argument value
      Returns:
      a reference to an InstructionXXX object
    • store

      public void store​(Memory mem, int loc)
      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 instruction
      loc - 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

      public int fetchOperand​(CPU cpu)
      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

      public void execute​(CPU cpu)
      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 array
      mode - index into the modeNames array
      arg - 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:
      toString in class java.lang.Object