Class Operation

java.lang.Object
io.matita08.logic.Operation

public class Operation extends Object
Represents a CPU operation/instruction in the simulator with dynamic loading capabilities. This class serves as a wrapper for CPU operations that can be executed by the simulation engine.

The Operation class uses reflection to dynamically load operation definitions from an external enum class specified by Constants.getOperationEnumName(). This design allows for flexible operation sets without modifying the core simulation logic.

Each operation contains:

  • Opcode: Numeric identifier for the operation
  • Action: Consumer function that implements the operation logic
  • Cycles: Number of CPU cycles required for execution
  • Name: Human-readable operation name

The class maintains static references to special operations like Halt and Unknown, and provides utility methods for operation lookup and CPU interaction.

Since:
1.0
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The executable action for this operation.
    static Operation[]
    Array containing all available operations loaded from the external enum.
    final int
    The number of CPU cycles required to execute this operation.
    static Operation
    Special operation representing a halt/stop instruction.
    final String
    The human-readable name of this operation.
    final int
    The numeric opcode identifier for this operation.
    static Operation
    Default operation used when an invalid opcode is encountered.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Operation
    get(int opcode)
    Retrieves an operation by its opcode identifier.
    static int
    Gets the address size used by the CPU architecture.
    static Value
    Gets the current Program Counter value and increments it for the next instruction.
    static void
    readMC(Value address)
    Initiates a memory read operation from the specified address.
    static void
    readPointer(int cycle)
    Initiates a pointer read operation for the specified cycle.
    static void
    setMC(Value address, Value value)
    Sets a value in central memory at the specified address.
    static void
    setRemainingCycles(int remaining)
    Sets the number of remaining execution cycles for the current operation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • Halt

      public static Operation Halt
      Special operation representing a halt/stop instruction. This operation typically terminates program execution.
    • Unknown

      public static Operation Unknown
      Default operation used when an invalid opcode is encountered. This operation handles error cases gracefully.
    • all

      public static Operation[] all
      Array containing all available operations loaded from the external enum. Operations are indexed by their position in the source enum.
    • action

      public final Consumer<Integer> action
      The executable action for this operation. Takes an integer parameter (typically an operand) and performs the operation.
    • opcode

      public final int opcode
      The numeric opcode identifier for this operation. Used to match instructions to their corresponding operations.
    • cycles

      public final int cycles
      The number of CPU cycles required to execute this operation. Used for accurate timing simulation.
    • name

      public final String name
      The human-readable name of this operation. Used for debugging and display purposes.
  • Method Details

    • get

      public static Operation get(int opcode)
      Retrieves an operation by its opcode identifier. If no matching operation is found, returns the Unknown operation and prints diagnostic information about available operations.
      Parameters:
      opcode - the opcode to search for
      Returns:
      the matching Operation, or Unknown if not found
    • setRemainingCycles

      public static void setRemainingCycles(int remaining)
      Sets the number of remaining execution cycles for the current operation. When cycles reach 1, automatically sets the next phase to Fetch for the next instruction.
      Parameters:
      remaining - the number of cycles remaining for the current operation
    • getAddressSize

      public static int getAddressSize()
      Gets the address size used by the CPU architecture. This is a convenience method that delegates to Constants.getAddressSize().
      Returns:
      the address size in bytes/words
    • readPointer

      public static void readPointer(int cycle)
      Initiates a pointer read operation for the specified cycle. This method delegates to the execution engine to handle pointer dereferencing.
      Parameters:
      cycle - the execution cycle when the read should occur
    • setMC

      public static void setMC(Value address, Value value)
      Sets a value in central memory at the specified address. This method updates both the MAR (Memory Address Register) and MDR (Memory Data Register) before writing to central memory.
      Parameters:
      address - the memory address where the value should be stored
      value - the value to store in memory
    • readMC

      public static void readMC(Value address)
      Initiates a memory read operation from the specified address. This method sets up the Memory Address Register for reading and delegates to the execution engine.
      Parameters:
      address - the memory address to read from
    • getAndIncPc

      public static Value getAndIncPc()
      Gets the current Program Counter value and increments it for the next instruction. This method is commonly used during instruction fetching to advance program execution.
      Returns:
      the current PC value before incrementing