Package mars.simulator
Class Simulator
java.lang.Object
mars.simulator.Simulator
Used to simulate the execution of an assembled MIPS program.
- Author:
- Pete Sanderson, August 2005
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
Constant that represents unlimited run speed. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addGUIListener
(SimulatorListener listener) Add aSimulatorListener
whose callbacks will be executed on the GUI thread.void
addThreadListener
(SimulatorListener listener) Add aSimulatorListener
whose callbacks will be executed on the simulator thread.void
changeState
(Runnable stateChanger) Modify program state used by the simulator (e.g. memory and registers) whenever is allowed by the simulator.Get the identifier of the memory-mapped I/O device which flagged an external interrupt, if any.void
dispatchFinishEvent
(SimulatorFinishEvent.Reason reason, SimulatorException exception) Called when the simulator has finished execution of the current program.void
dispatchPauseEvent
(int stepCount, SimulatorPauseEvent.Reason reason) Called when the simulator has paused execution of the current program.void
dispatchStartEvent
(int stepCount) Called when the simulator has started execution of the current program.void
Called when the simulator has finished executing an instruction, but only if the run speed is not unlimited.void
Perform any changes to program state from calls tochangeState(Runnable)
which are waiting to execute.static Simulator
Returns the singleton instance of the MIPS simulator.double
Get the current run speed of the simulator in instructions per second.Obtain the associatedSystemIO
instance, which handles I/O-related syscall functionality.boolean
Determine whether the current run speed of the simulator is limited (that is, any value other thanUNLIMITED_SPEED
).boolean
void
pause()
Flag the simulator to stop due to pausing, unless it is already paused.void
processJump
(int targetAddress) Schedule a jump in execution to another point in the program.void
raiseExternalInterrupt
(int device) Flag an external interrupt as a result of a memory-mapped I/O device.void
removeGUIListener
(SimulatorListener listener) Remove aSimulatorListener
which was added viaaddGUIListener(SimulatorListener)
.void
removeThreadListener
(SimulatorListener listener) Remove aSimulatorListener
which was added viaaddThreadListener(SimulatorListener)
.void
reset()
void
setRunSpeed
(double runSpeed) Set the current run speed of the simulator in instructions per second.void
simulate
(int maxSteps, int[] breakpoints) Simulate execution of given MIPS program.void
storeProgramArguments
(List<String> arguments) Place any program arguments into MIPS memory and registers.void
Flag the simulator to stop due to termination, even if it is currently paused.
-
Field Details
-
UNLIMITED_SPEED
public static final double UNLIMITED_SPEEDConstant that represents unlimited run speed. Compare with return value of getRunSpeed() to determine if set to unlimited. At the unlimited setting, the GUI will not attempt to update register and memory contents as each instruction is executed. This is the only possible value for command-line use of Mars.- See Also:
-
-
Method Details
-
getInstance
Returns the singleton instance of the MIPS simulator.- Returns:
- The
Simulator
object in use.
-
isRunning
public boolean isRunning() -
getBackStepper
-
getSystemIO
Obtain the associatedSystemIO
instance, which handles I/O-related syscall functionality.- Returns:
- The system I/O handler.
-
reset
public void reset() -
getRunSpeed
public double getRunSpeed()Get the current run speed of the simulator in instructions per second.- Returns:
- The run speed, or
UNLIMITED_SPEED
if the run speed is unlimited. - See Also:
-
isLimitingRunSpeed
public boolean isLimitingRunSpeed()Determine whether the current run speed of the simulator is limited (that is, any value other thanUNLIMITED_SPEED
). When the run speed is limited, the simulator intentionally slows down execution to allow the GUI to track the program counter, memory accesses, register updates, etc.- Returns:
false
if the current run speed isUNLIMITED_SPEED
, ortrue
otherwise.- See Also:
-
setRunSpeed
public void setRunSpeed(double runSpeed) Set the current run speed of the simulator in instructions per second.- Parameters:
runSpeed
- The run speed, orUNLIMITED_SPEED
to disable run speed limiting.- Throws:
IllegalArgumentException
- Thrown ifrunSpeed
is NaN or ≤ 0.
-
processJump
public void processJump(int targetAddress) Schedule a jump in execution to another point in the program. If delayed branching is enabled, the actual jump will occur after the next instruction is executed.- Parameters:
targetAddress
- The address of the instruction to jump to.
-
checkExternalInterruptDevice
Get the identifier of the memory-mapped I/O device which flagged an external interrupt, if any. Once this method is called, the external interrupt flag is reset.- Returns:
- The device identifier, typically the address of the control register, or null if no interrupt was flagged.
-
raiseExternalInterrupt
public void raiseExternalInterrupt(int device) Flag an external interrupt as a result of a memory-mapped I/O device.This method may be called from any thread.
- Parameters:
device
- The device identifier, typically the address of the control register.
-
changeState
Modify program state used by the simulator (e.g. memory and registers) whenever is allowed by the simulator. If the simulator is not currently running, this change will occur immediately. On the other hand, if the simulator is running, this change will be queued and execute between instructions.If any thread other than the simulator needs to modify memory, registers, or some other program state, this method must be used for synchronization purposes!
- Parameters:
stateChanger
- The modification on program state, which will be performed asynchronously if needed.
-
flushStateChanges
public void flushStateChanges()Perform any changes to program state from calls tochangeState(Runnable)
which are waiting to execute.Note: This method must be called from the simulator thread.
-
addGUIListener
Add aSimulatorListener
whose callbacks will be executed on the GUI thread.- Parameters:
listener
- The listener to add.- See Also:
-
removeGUIListener
Remove aSimulatorListener
which was added viaaddGUIListener(SimulatorListener)
.- Parameters:
listener
- The listener to remove.
-
addThreadListener
Add aSimulatorListener
whose callbacks will be executed on the simulator thread.- Parameters:
listener
- The listener to add.- See Also:
-
removeThreadListener
Remove aSimulatorListener
which was added viaaddThreadListener(SimulatorListener)
.- Parameters:
listener
- The listener to remove.
-
storeProgramArguments
Place any program arguments into MIPS memory and registers. Arguments are stored starting at highest word of non-kernel memory and working back toward runtime stack (there is a 4096 byte gap in between). The argument count (argc) and pointers to the arguments are stored on the runtime stack. The stack pointer register $sp is adjusted accordingly and $a0 is set to the argument count (argc), and $a1 is set to the stack address holding the first argument pointer (argv).- Parameters:
arguments
- The list of program argument strings.- Throws:
SimulatorException
- Thrown if a memory exception occurs while writing program arguments.
-
simulate
Simulate execution of given MIPS program. It must have already been assembled.- Parameters:
maxSteps
- Maximum number of steps to perform before returning false (0 or less means no max).breakpoints
- Array of breakpoint program counter values. (Can be null.)- Throws:
SimulatorException
- Thrown if an unhandled exception occurs in the program and MARS is running in the command line.
-
pause
public void pause()Flag the simulator to stop due to pausing, unless it is already paused. Once it has stopped,SimulatorListener.simulatorPaused(SimulatorPauseEvent)
will be called for all registered listeners. -
terminate
public void terminate()Flag the simulator to stop due to termination, even if it is currently paused. Once it has stopped,SimulatorListener.simulatorFinished(SimulatorFinishEvent)
will be called for all registered listeners. -
dispatchStartEvent
public void dispatchStartEvent(int stepCount) Called when the simulator has started execution of the current program. InvokesSimulatorListener.simulatorStarted(SimulatorStartEvent)
for all listeners. -
dispatchPauseEvent
Called when the simulator has paused execution of the current program. InvokesSimulatorListener.simulatorPaused(SimulatorPauseEvent)
for all listeners. -
dispatchFinishEvent
Called when the simulator has finished execution of the current program. InvokesSimulatorListener.simulatorFinished(SimulatorFinishEvent)
for all listeners. -
dispatchStepEvent
public void dispatchStepEvent()Called when the simulator has finished executing an instruction, but only if the run speed is not unlimited. InvokesSimulatorListener.simulatorStepped()
for all listeners.Note: For very fast run speeds, GUI listeners may not receive all step events. This is an intentional feature to prevent overloading of the GUI event queue.
-