Package lab06

Class CPU

java.lang.Object
lab06.CPU

public class CPU
extends java.lang.Object
Simulate a Pippin CPU. A Pippin CPU consists of:
  • a reference to a Memory object,
  • three internal integer registers: an instructionPointer, an accumulator, and a dataMemoryBase,
  • a single flag called "halted",
  • the logic to execute Pippin instructions in an ALU to manipulate the registers, flags, and memory
Author:
cs140
  • Constructor Summary

    Constructors
    Constructor Description
    CPU​(Memory memory)
    Constructor to initialize CPU
  • Method Summary

    Modifier and Type Method Description
    void execute()
    Execute a single Pippin Instruction
    int getAccumulator()
    Get the current value of the accumulator register.
    int getData​(int loc)
    Get the value in the data part of the memory at the specified location.
    Memory getMemory()
    Get the memory object this CPU is using.
    void run()
    Run the current program by executing instructions until halted
    void setAccumulator​(int accumulator)
    Set the value of the accumulator register.
    void setData​(int loc, int value)
    Set the value in the data part of the memory at the specified location.
    void setDataMemoryBase​(int dataMemoryBase)
    Set the value of the dataMemoryBase register.
    void setHalted​(boolean halted)
    Set the halted flag to the specified value.
    void setInstructionPointer​(int instructionPointer)
    Set the instructionPointer register.

    Methods inherited from class java.lang.Object

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

    • CPU

      public CPU​(Memory memory)
      Constructor to initialize CPU
      Parameters:
      memory - the memory to be used by the CPU
  • Method Details

    • run

      public void run()
      Run the current program by executing instructions until halted
    • execute

      public void execute()
      Execute a single Pippin Instruction
    • getAccumulator

      public int getAccumulator()
      Get the current value of the accumulator register.
      Returns:
      The current value of the accumulator register
    • setAccumulator

      public void setAccumulator​(int accumulator)
      Set the value of the accumulator register.
      Parameters:
      accumulator - new value
    • setDataMemoryBase

      public void setDataMemoryBase​(int dataMemoryBase)
      Set the value of the dataMemoryBase register.
      Parameters:
      dataMemoryBase - new value
    • getMemory

      public Memory getMemory()
      Get the memory object this CPU is using.
      Returns:
      A reference to the Memory object this CPU is using
    • setInstructionPointer

      public void setInstructionPointer​(int instructionPointer)
      Set the instructionPointer register.
      Parameters:
      instructionPointer - new value
    • getData

      public int getData​(int loc)
      Get the value in the data part of the memory at the specified location.
      Parameters:
      loc - index into the data part of the memory (offset from the dataMemoryBase)
      Returns:
      the value in memory at the specified location
    • setData

      public void setData​(int loc, int value)
      Set the value in the data part of the memory at the specified location.
      Parameters:
      loc - index into the data part of the memory (offset from the dataMemoryBase)
      value - new value to write into the specified location in data memory
    • setHalted

      public void setHalted​(boolean halted)
      Set the halted flag to the specified value.
      Parameters:
      halted - new value for the halted flag