Class Execution
This class manages the CPU's instruction execution cycle, implementing the fetch-decode-execute paradigm. It coordinates instruction processing, memory access, and program counter management.
Key responsibilities:
- Single-step and multi-step execution control
- Instruction fetch from memory
- Instruction decoding and operation lookup
- Execution phase coordination
- Memory access operations
- Program counter advancement
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic booleanFlag indicating whether a step operation has been performed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddecode()DECODE Phase - Decodes the instruction and prepares for execution.static voidexecute()EXECUTE Phase - Executes the decoded instruction.static voidfetch()FETCH Phase - Retrieves the next instruction from memory.static Valuenext()Gets the next instruction address and increments the program counter.static voidreadPointer(int cycle) Reads address pointer data during multi-cycle instruction execution.static voidSets MAR and performs a memory read operation.static booleanstep()Executes a single CPU instruction step.static voidstep(int countMax) Executes multiple CPU instruction steps up to the specified maximum.static voidstep(ActionEvent ignored) GUI event handler for single-step execution.
-
Field Details
-
stepped
public static boolean steppedFlag indicating whether a step operation has been performed. Used for GUI update coordination and execution tracking.
-
-
Constructor Details
-
Execution
public Execution()
-
-
Method Details
-
step
public static void step(int countMax) Executes multiple CPU instruction steps up to the specified maximum.Continues executing CPU steps until either the maximum count is reached or the CPU enters a halted state (unable to step further).
- Parameters:
countMax- the maximum number of steps to execute
-
step
public static boolean step()Executes a single CPU instruction step.Advances the CPU through one phase of the instruction cycle (Fetch, Decode, or Execute). Handles halt conditions and coordinates GUI updates.
- Returns:
- true if the step was successful, false if CPU is halted
-
fetch
public static void fetch()FETCH Phase - Retrieves the next instruction from memory.Performs the instruction fetch cycle:
- Clears ALU operation display
- Sets MAR to current PC value and increments PC
- Loads instruction from memory into IR
- Prepares for decode phase
-
decode
public static void decode()DECODE Phase - Decodes the instruction and prepares for execution.Performs the instruction decode cycle:
- Looks up the operation based on IR contents
- Sets up cycle counts for the instruction
- Prepares for execute phase
-
execute
public static void execute()EXECUTE Phase - Executes the decoded instruction.Performs the instruction execution cycle:
- Calls the instruction's execution logic for current cycle
- Handles multi-cycle instruction coordination
- Returns to fetch phase when execution completes
-
step
GUI event handler for single-step execution.ActionListener implementation for GUI step buttons. Executes a single CPU step in a background thread.
- Parameters:
ignored- the ActionEvent (not used)
-
next
Gets the next instruction address and increments the program counter.This atomic operation is crucial for proper instruction sequencing. Returns the current PC value and then increments it for the next instruction fetch.
- Returns:
- the current PC value before increment
-
setMarR
Sets MAR and performs a memory read operation.Helper method that combines MAR setting with memory data retrieval:
- Sets the Memory Address Register to the specified value
- Reads the data from that memory location into MDR
- Parameters:
v- the memory address to read from
-
readPointer
public static void readPointer(int cycle) Reads address pointer data during multi-cycle instruction execution.This method handles the address reading phase of instructions that require memory address operands (like LOAD, STO, JPZ). It reads address components from consecutive memory locations and assembles them into the pointer register.
- Parameters:
cycle- the current cycle number within the address reading phase- Throws:
AssertionError- if cycle number is out of valid bounds
-