edu.princeton.toy.lang
public class TVirtualMachine extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
TVirtualMachine.ExecutionController
ExecutionController is an interface that classes which wish to recieve notifications of a
ToyVirtualMachine's termination should implement.
|
protected class |
TVirtualMachine.Runner
Runner is a simple implementation of Runnable for the Thread stated by the run() method
of the TVM.
|
Modifier and Type | Field and Description |
---|---|
static int |
MEM_COUNT
The size of the memory array.
|
static TWord |
PC_START
The address on which the Virtual Machine begins execution.
|
static short |
PC_START_VALUE
The address on which the Virtual Machine begins execution.
|
static int |
REGISTER_COUNT
The number of registers.
|
Constructor and Description |
---|
TVirtualMachine()
Constructs a completely blank TVirtualMachine.
|
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(javax.swing.event.ChangeListener listener)
Adds a listener to monitor changes in the state of this machine.
|
void |
finalize()
Override the finalize() method to also interrupt the Runner.
|
protected void |
fireStateChanged()
Fires a state changed event to all the listeners.
|
TWordBuffer |
getConsumedStdin()
Returns the TWordBuffer containing all of the words which have been read already.
|
TWordBuffer |
getConsumedStdin(TWordBuffer buffer)
Copies all of the words which have been read already to the provided TWordBuffer.
|
java.lang.String |
getCoreDump()
Returns a core dump of the toy machine.
|
java.lang.String |
getCoreDump(boolean distinguishUninitialized)
Returns a core dump of the toy machine.
|
TWord |
getCurrentInstruction()
Returns the current instruction.
|
boolean |
getDistinguishUninitialized()
A convenience method for determining wheter or not uninitialied values should be
distinguished from a simple zero.
|
TExceptionHandler |
getExceptionHandler()
Returns the exception handler.
|
TWord |
getMem(int index)
Returns the value at the requested memory memory.
|
void |
getMem(TWord[] array)
Copies all of the virtual machine's memory onto the given TWord array.
|
java.lang.String |
getMemDump()
Returns a memory dump of the toy machine, which could be parsed by Visual X-TOY as another
program.
|
TWord |
getProgramCtr()
Returns the program counter.
|
TWord |
getRegister(int index)
Get the value of a particular register.
|
java.lang.String |
getStderr()
Returns the standard error stream.
|
TWordBuffer |
getStdout()
Returns the TWordBuffer containing all of the words which have been outputted by the virtual
machine.
|
TWordBuffer |
getStdout(TWordBuffer buffer)
Copies all of the words which have been outputted by the virtual machine to the provided
TWordBuffer.
|
TWordBuffer |
getUnconsumedStdin()
Returns the TWordBuffer containing all of the words which have not been read yet.
|
TWordBuffer |
getUnconsumedStdin(TWordBuffer buffer)
Copies all of the words which have not been read yet to the provided TWordBuffer.
|
boolean |
hasEncounteredError()
Returns true iff an error has been encountered.
|
void |
initMem(TWord[] words)
Initializes all of memory with an array of size exactly MEM_COUNT containing no null
values.
|
void |
interrupt()
Will cause the runner (started by the run method) to stop.
|
boolean |
isDone()
Returns wheter or not the GUI should continue stepping.
|
boolean |
isRunning()
Returns wheter or not the runner (started by the run method) is running.
|
boolean |
needsInput()
Returns wheter or not the GUI should prompt the user for input.
|
void |
removeChangeListener(javax.swing.event.ChangeListener listener)
Removes a listener from this machine.
|
void |
reset()
Wipes out the memory and registers, resets the programCtr to PC_START, and flushes the
streams.
|
boolean |
run(TVirtualMachine.ExecutionController controller)
Executes the command which programCtr points to and increments programCtr if necessary n
times, dispatching ChangeEvents to all the listeners afterwards.
|
void |
setExceptionHandler(TExceptionHandler exceptionHandler)
Sets the exception handler.
|
void |
setMem(int index,
TWord word)
Sets the value of the specified memory memory.
|
void |
setProgramCtr(TWord programCtr)
Change the programCtr.
|
void |
setRegister(int index,
TWord word)
Sets the value of a particular register.
|
void |
setStdin(TWordBuffer unconsumedStdin)
Clears the consumedStdin and sets unconsumedStdin to contain the values provided.
|
void |
setStdin(TWordBuffer consumedStdin,
TWordBuffer unconsumedStdin)
Sets the consumedStdin and unconsumedStdin to contain the values provided.
|
int |
step()
Executes the command which programCtr points to and increments programCtr if necessary one
time, dispatching ChangeEvents to all the listeners afterwards.
|
public static final int MEM_COUNT
public static final int REGISTER_COUNT
public static final TWord PC_START
public static final short PC_START_VALUE
public TVirtualMachine()
public void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void addChangeListener(javax.swing.event.ChangeListener listener)
listener
- The listener to add to this virtualMachine. A null value will cause a
NullPointerException.public void removeChangeListener(javax.swing.event.ChangeListener listener)
listener
- The listener to remove from this virtualMachine. A null value will cause a
NullPointerException.public boolean hasEncounteredError()
public boolean isDone()
public boolean needsInput()
public boolean getDistinguishUninitialized()
public TExceptionHandler getExceptionHandler()
public void setExceptionHandler(TExceptionHandler exceptionHandler)
exceptionHandler
- The exception handler. A null value will result in a NullPointerException.public TWord getProgramCtr()
public void setProgramCtr(TWord programCtr)
programCtr
- The new value of the programCtr. A null value will result in a
NullPointerException.public TWord getRegister(int index)
index
- The index of the requested register. An invalid value will result in an
ArrayIndexOutOfBoundsException.public void setRegister(int index, TWord word)
index
- The index of the register to be changed. An invalid value will result in an
ArrayIndexOutOfBoundsException.word
- The new value of the register. A null value will result in a
NullPointerException.public TWord getCurrentInstruction()
getMem(int)
,
getProgramCtr()
public void getMem(TWord[] array)
array
- The array on which to copy the virtual machine's memory. If the array is null,
a NullPointerException will be thrown, if the array is too small, an
ArrayIndexOutOfBoundsException will be thrown.public TWord getMem(int index)
index
- The address of the requested value. An invalid value will result in
an ArrayIndexOutOfBoundsException.public void setMem(int index, TWord word)
index
- The address of the value to be changed. An invalid value will result in
an ArrayIndexOutOfBoundsException.word
- The new value of the memory memory. A null value will result in a
NullPointerException.public void initMem(TWord[] words)
words
- The new contents of the memory array. A null value will result in a
NullPointerException, and an incorrect length will result in an IllegalArgumentException.public TWordBuffer getConsumedStdin()
public TWordBuffer getConsumedStdin(TWordBuffer buffer)
buffer
- The buffer to copy the data to. The buffer will be cleared before the copy.public TWordBuffer getUnconsumedStdin()
public TWordBuffer getUnconsumedStdin(TWordBuffer buffer)
buffer
- The buffer to copy the data to. The buffer will be cleared before the copy.public void setStdin(TWordBuffer unconsumedStdin)
unconsumedStdin
- The new values for the unconsumed stdin. A null implies that no
changes should be made to the current value.public void setStdin(TWordBuffer consumedStdin, TWordBuffer unconsumedStdin)
consumedStdin
- The new values for the consumed stdin. A null implies that no
changes should be made to the current value.unconsumedStdin
- The new values for the unconsumed stdin. A null implies that no
changes should be made to the current value.public TWordBuffer getStdout()
public TWordBuffer getStdout(TWordBuffer buffer)
buffer
- The buffer to copy the data to. The buffer will be cleared before the copy.public java.lang.String getStderr()
public java.lang.String getMemDump()
public java.lang.String getCoreDump()
getCoreDump(getDistinguishUninitialized())
.public java.lang.String getCoreDump(boolean distinguishUninitialized)
distinguishUninitialized
- If true, uninitialized memory memorys and registers will
show ???? instead of 0000.public void reset()
protected void fireStateChanged()
public void interrupt()
public boolean isRunning()
interrupt()
,
run(TVirtualMachine.ExecutionController)
public int step()
public boolean run(TVirtualMachine.ExecutionController controller)
controller
- The implementation of ExecutionController which will recieve notification
of the completion of a batch of steps and control the size of each batch. A null value will
result in a NullPointerException.interrupt()
,
isRunning()