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
-
Method Summary
Modifier and Type Method Description voidexecute()Execute a single Pippin InstructionintgetAccumulator()Get the current value of the accumulator register.intgetData(int loc)Get the value in the data part of the memory at the specified location.MemorygetMemory()Get the memory object this CPU is using.voidrun()Run the current program by executing instructions until haltedvoidsetAccumulator(int accumulator)Set the value of the accumulator register.voidsetData(int loc, int value)Set the value in the data part of the memory at the specified location.voidsetDataMemoryBase(int dataMemoryBase)Set the value of the dataMemoryBase register.voidsetHalted(boolean halted)Set the halted flag to the specified value.voidsetInstructionPointer(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
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
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
-