|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.princeton.toy.lang.TVirtualMachine
public class TVirtualMachine
TVirtualMachine is an object that encapsulates the concept of the Toy Machine. This includes a program counter, set of registers, memory array, and a set of I/O buffers.
Nested Class Summary | |
---|---|
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. |
Field Summary | |
---|---|
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 Summary | |
---|---|
TVirtualMachine()
Constructs a completely blank TVirtualMachine. |
Method Summary | |
---|---|
void |
addChangeListener(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. |
String |
getCoreDump()
Returns a core dump of the toy machine. |
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 sector. |
void |
getMem(TWord[] array)
Copies all of the virtual machine's memory onto the given TWord array. |
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. |
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(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 sector. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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
Constructor Detail |
---|
public TVirtualMachine()
Method Detail |
---|
public void finalize() throws Throwable
finalize
in class Object
Throwable
public void addChangeListener(ChangeListener listener)
listener
- The listener to add to this virtualMachine. A null value will cause a
NullPointerException.public void removeChangeListener(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)
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 sector. 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 String getStderr()
public String getMemDump()
public String getCoreDump()
getCoreDump(getDistinguishUninitialized())
.public String getCoreDump(boolean distinguishUninitialized)
distinguishUninitialized
- If true, uninitialized memory sectors and registers will
show ???? instead of 0000.
public void reset()
protected void fireStateChanged()
public void interrupt()
isRunning()
,
run(TVirtualMachine.ExecutionController)
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()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |