fi.hu.cs.titokone
Class Control

java.lang.Object
  extended byfi.hu.cs.titokone.Control
All Implemented Interfaces:
TTK91Core

public class Control
extends java.lang.Object
implements TTK91Core

Control class offers the extenal interface to titokone. Using the methods of this class one can compile and emulate the execution process of a ttk91-program from a text file or straight from a string object, which contain symbolic ttk91 machine code, A complete debugging and trackability of compilation, loading and execution cycles are provided with CompileInfo, LoadInfo and RunInfo objects, which can be used to get information of what had just happened. This doesn't take a position on how to show the output. That's left for the GUI, actually a piece of code between Control and the GUI, which prepares the output provided here to be shown in GUI. In this software that piece of code is the GUIBrain class.


Field Summary
private  Application application
           
private  Compiler compiler
           
private  java.io.File currentStdOutFile
           
static int DEF_HOME_POS
           
static int DEF_STDIN_POS
          This field set directs handling of an array containing file definitions set in the application.
static int DEF_STDOUT_POS
           
static int DEFAULT_MEMORY_SIZE
          This is the memory size that will be used by default, unless a higher class (GUIBrain) chooses to change the size later.
private  java.io.File defaultStdInFile
           
private  java.io.File defaultStdOutFile
           
private  FileHandler fileHandler
          This has control to all the files this program has opened.
private  LoadInfo pendingLoadInfo
           
private  Processor processor
           
private  java.io.File sourceFile
           
 
Constructor Summary
Control(java.io.File defaultStdInFile, java.io.File defaultStdOutFile)
          This constructor sets up the Control instance.
 
Method Summary
 void changeMemorySize(int powerOfTwo)
          Changes the size of memory measured in words.
 TTK91Application compile(TTK91CompileSource source)
          Compiles a symbolic TTK91-assembly language to binary executable application.
 CompileInfo compileLine()
          This compiles one next line of the t91 program that has been opened recently and hasn't yet been compiled to binary code.
 void eraseMemory()
          Erases the memory ie.
 java.io.File[] getApplicationDefinitions()
          This helper method gets the definitions in the currently loaded application.
 java.lang.String getBinary(TTK91Application app)
          Returns a string that contains the binary presentation of the application.
 TTK91Cpu getCpu()
          Returns a reference to the Processor object.
 TTK91Memory getMemory()
          Returns a reference to the RandomAccessMemory object which is attached to the current Processor object.
 LoadInfo getPendingLoadInfo()
          Sometimes load() throws an exception when the situation is not really requiring an error message.
private  void insertStdinToApplication(java.io.File applicationStdin)
          This method does the actual inserting STDIN datat to an application.
 void keyboardInput(int inputValue)
          GUIBrain calls this when it has recieved the TTK91NoKbdData exception.
 LoadInfo load()
          This method loads an application that has been either compiled or read from binary earlier.
 TTK91Application loadBinary(java.lang.String binary)
          Returns a TTK91Application object of the given string, which should contain proper binary code.
 java.util.ResourceBundle loadLanguageFile(java.io.File languageFile)
          This is called when user the has changed language to another one by choosing it from a dialog after selecting Set language from the GUI menu, and has chosen a special file to load.
 java.lang.String loadSettingsFileContents(java.io.File settingsFile)
          This is called when GUIBrain wants to load the settings.
 java.lang.String loadSettingsStreamContents(java.io.InputStream settingsStream)
          This is called when GUIBrain wants to load the settings from a stream.
 java.lang.String modifySource(java.lang.String[] modifiedSource)
          This method makes it possible to re-open the same source file, only modified.
 void openBinary(java.io.File openedFile)
          This is called when a binary file is opened from GUI.
 java.lang.String openSource(java.io.File openedFile)
          This is called when a source file is opened from GUI.
 void run(TTK91Application app, int steps)
          Runs a given app for steps at a time and updates its state.
 RunInfo runLine()
          This runs one next line of the program that is currently loaded into the TTK91's memory.
 void saveBinary()
          This method saves the opened source file to a binary of the same filename as the previous source file was loaded from, with the extension changed (probably from .k91) to .b91.
 void saveBinary(java.io.File specialFile)
          This method saves the current application to a given file.
 void saveSettings(java.lang.String currentSettings, java.io.File settingsFile)
          This method stores the settings string to the given file.
 void setDefaultStdIn(java.io.File stdinFile)
          This is called when the user has changed the file from which the stdin operations will be read.
 void setDefaultStdOut(java.io.File stdoutFile)
          This is called when the user has changed the file into which the stdout operations will be written.
private  void writeToCRT(int inputValue)
          RunLine() calls this, when the processor wants to write a value to CRT.
private  void writeToStdOut(int inputValue)
          RunLine() calls this, when the processor wants to write a value to StdOut.
private  void writeToStdoutFile(java.lang.String data)
          This method appends the given data to the current stdout file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MEMORY_SIZE

public static final int DEFAULT_MEMORY_SIZE
This is the memory size that will be used by default, unless a higher class (GUIBrain) chooses to change the size later. The size is expressed as "the power of two which will give the wanted memory size"; here 9 means that the actual memory will be 2^9 = 512 words long.

See Also:
Constant Field Values

DEF_STDIN_POS

public static final int DEF_STDIN_POS
This field set directs handling of an array containing file definitions set in the application. See getApplicationDefinitions().

See Also:
Constant Field Values

DEF_STDOUT_POS

public static final int DEF_STDOUT_POS
See Also:
Constant Field Values

DEF_HOME_POS

public static final int DEF_HOME_POS
See Also:
Constant Field Values

fileHandler

private FileHandler fileHandler
This has control to all the files this program has opened.


processor

private Processor processor

compiler

private Compiler compiler

application

private Application application

defaultStdInFile

private java.io.File defaultStdInFile

defaultStdOutFile

private java.io.File defaultStdOutFile

currentStdOutFile

private java.io.File currentStdOutFile

sourceFile

private java.io.File sourceFile

pendingLoadInfo

private LoadInfo pendingLoadInfo
Constructor Detail

Control

public Control(java.io.File defaultStdInFile,
               java.io.File defaultStdOutFile)
This constructor sets up the Control instance.

Method Detail

compile

public TTK91Application compile(TTK91CompileSource source)
                         throws TTK91Exception,
                                TTK91CompileException
Compiles a symbolic TTK91-assembly language to binary executable application. Defined by TTK91Core. It throws exceptions if the compilation (eg. Compiler) throws them.

Specified by:
compile in interface TTK91Core
Parameters:
source - The source code to be compiled.
Returns:
The binary executable code.
Throws:
TTK91CompileException - If the compiler throws one.
TTK91Exception - Never in this implementation. (Except for TTK91CompileExceptions, which inherit it.)

load

public LoadInfo load()
              throws TTK91AddressOutOfBounds,
                     TTK91NoStdInData
This method loads an application that has been either compiled or read from binary earlier. It calls Loader's setApplicationToLoad and then loadApplication methods. It also sets up the STDIN contents from the default file to the Application. If this fails, a ParseException is thrown just before the method should return. It can be ignored until it leads to a TTK91NoStdInData exception when a program tries to read from STDIN.

Throws:
TTK91OutOfMemory - If the memory cannot fit the application.
TTK91NoStdInData - If the current STDIN file contains invalid STDIN input or the file cannot be opened.
java.lang.IllegalStateException - If application is null.
TTK91AddressOutOfBounds

getPendingLoadInfo

public LoadInfo getPendingLoadInfo()
Sometimes load() throws an exception when the situation is not really requiring an error message. This causes the caller to not recieve their LoadInfo. To avoid causing unnecessary suffering, the LoadInfo can be fetched by calling this method. The LoadInfo will be null if the loading was really not successful or if there has been no load.


insertStdinToApplication

private void insertStdinToApplication(java.io.File applicationStdin)
                               throws java.text.ParseException,
                                      java.io.IOException
This method does the actual inserting STDIN datat to an application.

Parameters:
applicationStdin - The application's suggestion for a file to read the stdin data from. If null, the data is read from the set default stdin file.
Throws:
java.io.IOException - If the current STDIN file cannot be opened.
java.text.ParseException - If the current STDIN file contains invalid input.

getApplicationDefinitions

public java.io.File[] getApplicationDefinitions()
This helper method gets the definitions in the currently loaded application. It assumes the definitions are file paths.

Returns:
A File-array containing STDIN, STDOUT and HOME definitions or nulls if the values have not been defined in the application. The positions of the values in the array are determined by the constants DEF_STDIN_POS, DEF_STDOUT_POS and DEF_HOME_POS respectively.

run

public void run(TTK91Application app,
                int steps)
         throws TTK91Exception,
                TTK91RuntimeException
Runs a given app for steps at a time and updates its state. Problem: the CPU and Memory states might need to somehow be attached to this thing, unless there is some sort of a 'resume running current' method added. Defined by TTK91Core.

Specified by:
run in interface TTK91Core
Parameters:
app - Application to be run.
steps - Number of steps the application will be run.
Throws:
TTK91NoStdInData - If the STDIN data file is unreadable or syntactically incorrect even if the file is not really needed.
TTK91Exception - place holder for extension.
TTK91RuntimeException - when encountered an invalid operation by the TTK91Application.

getMemory

public TTK91Memory getMemory()
Returns a reference to the RandomAccessMemory object which is attached to the current Processor object. Defined by TTK91Core.

Specified by:
getMemory in interface TTK91Core
Returns:
The reference to the RandomAccessMemory object.

getCpu

public TTK91Cpu getCpu()
Returns a reference to the Processor object. Defined by TTK91Core.

Specified by:
getCpu in interface TTK91Core
Returns:
The reference to the Processor object.

getBinary

public java.lang.String getBinary(TTK91Application app)
Returns a string that contains the binary presentation of the application. Defined by TTK91Core.

Specified by:
getBinary in interface TTK91Core
Parameters:
app - The application that is to be transformed into binary.
Returns:
Returns the application in the form a string, which contains the binary code.

loadBinary

public TTK91Application loadBinary(java.lang.String binary)
                            throws java.text.ParseException
Returns a TTK91Application object of the given string, which should contain proper binary code. If it doesn't, however, a ParseException is thrown. Defined by TTK91Core.

Specified by:
loadBinary in interface TTK91Core
Parameters:
binary - The binary to be compiled.
Returns:
Returns a TTK91Application object of the binary string.
Throws:
java.text.ParseException - if binary is not of TTK91 binary format

changeMemorySize

public void changeMemorySize(int powerOfTwo)
Changes the size of memory measured in words.

Parameters:
powerOfTwo - Implies the total size of memory which is 2^powerOfTwo. Legitimate values are 9,...,16.
Throws:
java.lang.IllegalArgumentException - if powerOfTwo is not between 9 and 16.

eraseMemory

public void eraseMemory()
Erases the memory ie. fills it with zeros.


compileLine

public CompileInfo compileLine()
                        throws TTK91CompileException
This compiles one next line of the t91 program that has been opened recently and hasn't yet been compiled to binary code.

Returns:
Returns CompileInfo object of the last line compiled.
Throws:
TTK91CompileException

runLine

public RunInfo runLine()
                throws TTK91RuntimeException
This runs one next line of the program that is currently loaded into the TTK91's memory.

Returns:
Returns RunInfo object of the last line executed.
Throws:
TTK91RuntimeException

writeToStdoutFile

private void writeToStdoutFile(java.lang.String data)
                        throws TTK91FailedWrite
This method appends the given data to the current stdout file.

Parameters:
data - The data to append to the stdout file.
Throws:
TTK91FailedWrite - If there was an I/O error.

loadLanguageFile

public java.util.ResourceBundle loadLanguageFile(java.io.File languageFile)
                                          throws ResourceLoadFailedException
This is called when user the has changed language to another one by choosing it from a dialog after selecting Set language from the GUI menu, and has chosen a special file to load.

Parameters:
languageFile - The language file to load into a ResourceBundle.
Returns:
The ResourceBundle represented by the file.
Throws:
ResourceLoadFailedException - If opening the resourcebundle from the file failed.

setDefaultStdIn

public void setDefaultStdIn(java.io.File stdinFile)
                     throws java.io.IOException,
                            java.text.ParseException
This is called when the user has changed the file from which the stdin operations will be read.

Parameters:
stdinFile - The stdin file.
Throws:
java.io.IOException - If an I/O error occurs.
java.text.ParseException - *After* everything else is done, if the contents of the file are invalid. This exception can be ignored until it leads into TTK91NoStdInData when an application tries to read from STDIN.

setDefaultStdOut

public void setDefaultStdOut(java.io.File stdoutFile)
                      throws java.io.IOException
This is called when the user has changed the file into which the stdout operations will be written.

Parameters:
stdoutFile - The stdout file.
Throws:
java.io.IOException

openSource

public java.lang.String openSource(java.io.File openedFile)
                            throws java.io.IOException
This is called when a source file is opened from GUI. This method passes the file to FileHandler and prepares to compile the contents.

Parameters:
openedFile - The file.
Returns:
The source string.
Throws:
java.io.IOException

modifySource

public java.lang.String modifySource(java.lang.String[] modifiedSource)
                              throws java.io.IOException
This method makes it possible to re-open the same source file, only modified. The opened file stays the same, the new string is passed to the compiler and the modifications are saved in the source file.

Parameters:
modifiedSource - The modified source as a string array, one line per cell. The format is optimized for GUI/GUIBrain use. The source must not be null.
Returns:
The source string.
Throws:
java.io.IOException - Just before the last return if the saving of the modified source failed.
java.lang.IllegalStateException - If there is no source file set. The method openSource() must be called before this method.

saveBinary

public void saveBinary()
                throws java.io.IOException
This method saves the opened source file to a binary of the same filename as the previous source file was loaded from, with the extension changed (probably from .k91) to .b91. If you wish to select the filename yourself, use saveBinary(File).

Throws:
java.lang.IllegalStateException - If the method is called before a source file has been opened or if there is no application to save.
java.io.IOException

saveBinary

public void saveBinary(java.io.File specialFile)
                throws java.io.IOException
This method saves the current application to a given file. It throws any IOExceptions upwards. Often saveBinary() with no parameters is sufficient; it guesses the file to save to from the source filename.

Parameters:
specialFile - A filename to save the binary to.
Throws:
java.lang.IllegalStateException - If there is no application to save.
java.io.IOException

openBinary

public void openBinary(java.io.File openedFile)
                throws java.io.IOException,
                       java.text.ParseException
This is called when a binary file is opened from GUI. This method passes the file to FileHandler, has the contents interpreted by Binary and prepares to load the result Application.

Parameters:
openedFile - The file.
Throws:
java.io.IOException
java.text.ParseException

loadSettingsFileContents

public java.lang.String loadSettingsFileContents(java.io.File settingsFile)
                                          throws java.io.IOException
This is called when GUIBrain wants to load the settings. It uses FileHandler for reading them.

Returns:
A string representation of the Settings object stored in the file.
Throws:
java.io.IOException

loadSettingsStreamContents

public java.lang.String loadSettingsStreamContents(java.io.InputStream settingsStream)
                                            throws java.io.IOException
This is called when GUIBrain wants to load the settings from a stream. It uses FileHandler for reading them.

Returns:
A string representation of the Settings object stored in the input stream, or null if the stream was null.
Throws:
java.io.IOException

saveSettings

public void saveSettings(java.lang.String currentSettings,
                         java.io.File settingsFile)
                  throws java.io.IOException
This method stores the settings string to the given file. It passes the parameters to FileHandler.

Parameters:
currentSettings - A string representation of the current settings.
settingsFile - The file to store the settings to.
Throws:
java.io.IOException - if FileHandler throws one to indicate an I/O error.

keyboardInput

public void keyboardInput(int inputValue)
GUIBrain calls this when it has recieved the TTK91NoKbdData exception. The input is passed on to the processor. Note that primarily input is searched for in the Application instance.

Parameters:
inputValue - The input to pass on.

writeToCRT

private void writeToCRT(int inputValue)
RunLine() calls this, when the processor wants to write a value to CRT.

Parameters:
inputValue - The input to CRT.

writeToStdOut

private void writeToStdOut(int inputValue)
RunLine() calls this, when the processor wants to write a value to StdOut.

Parameters:
inputValue - The inpuit to StdOut.