Package mars.util
Class SystemIO
java.lang.Object
mars.util.SystemIO
Provides standard i/o services needed to simulate the MIPS syscall
routines. These methods will detect whether the simulator is being
run from the command line or through the GUI, then do I/O to
System.in and System.out in the former situation, and interact with
the GUI in the latter.
- Version:
- August 2003-2005
- Author:
- Pete Sanderson and Ken Vollmar
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic String
String used for description of file errorstatic int
Buffer size for syscalls for file I/Ostatic int
Maximum number of files that can be open -
Method Summary
Modifier and TypeMethodDescriptionstatic void
closeFile(int fd)
Close the file with specified file descriptorstatic String
Retrieve file operation or error messagestatic int
Open a file for either reading or writing.static void
printString(String string)
Implements syscall having 4 in $v0, to print a string.static int
readChar(int serviceNumber)
Implements syscall having 12 in $v0, to read a char value.static double
readDouble(int serviceNumber)
Implements syscall to read a double value.static float
readFloat(int serviceNumber)
Implements syscall to read a float value.static int
readFromFile(int fd, byte[] myBuffer, int lengthRequested)
Read bytes from file.static int
readInteger(int serviceNumber)
Implements syscall to read an integer value.static String
readString(int serviceNumber, int maxLength)
Implements syscall to read a string.static void
Reset all files -- clears out the file descriptor table.static int
writeToFile(int fd, byte[] myBuffer, int lengthRequested)
Write bytes to file.
-
Field Details
-
SYSCALL_BUFSIZE
public static final int SYSCALL_BUFSIZEBuffer size for syscalls for file I/O- See Also:
- Constant Field Values
-
SYSCALL_MAXFILES
public static final int SYSCALL_MAXFILESMaximum number of files that can be open- See Also:
- Constant Field Values
-
fileErrorString
String used for description of file error
-
-
Method Details
-
readInteger
public static int readInteger(int serviceNumber)Implements syscall to read an integer value. Client is responsible for catching NumberFormatException.- Parameters:
serviceNumber
- the number assigned to Read Int syscall (default 5)- Returns:
- int value corresponding to user input
-
readFloat
public static float readFloat(int serviceNumber)Implements syscall to read a float value. Client is responsible for catching NumberFormatException.- Parameters:
serviceNumber
- the number assigned to Read Float syscall (default 6)- Returns:
- float value corresponding to user input Feb 14 2005 Ken Vollmar
-
readDouble
public static double readDouble(int serviceNumber)Implements syscall to read a double value. Client is responsible for catching NumberFormatException.- Parameters:
serviceNumber
- the number assigned to Read Double syscall (default 7)- Returns:
- double value corresponding to user input
-
printString
Implements syscall having 4 in $v0, to print a string. -
readString
Implements syscall to read a string.- Parameters:
serviceNumber
- the number assigned to Read String syscall (default 8)maxLength
- the maximum string length- Returns:
- the entered string, truncated to maximum length if necessary
-
readChar
public static int readChar(int serviceNumber)Implements syscall having 12 in $v0, to read a char value.- Parameters:
serviceNumber
- the number assigned to Read Char syscall (default 12)- Returns:
- int value with lowest byte corresponding to user input
-
writeToFile
public static int writeToFile(int fd, byte[] myBuffer, int lengthRequested)Write bytes to file.- Parameters:
fd
- file descriptormyBuffer
- byte array containing characters to writelengthRequested
- number of bytes to write- Returns:
- number of bytes written, or -1 on error
-
readFromFile
public static int readFromFile(int fd, byte[] myBuffer, int lengthRequested)Read bytes from file.- Parameters:
fd
- file descriptormyBuffer
- byte array to contain bytes readlengthRequested
- number of bytes to read- Returns:
- number of bytes read, 0 on EOF, or -1 on error
-
openFile
Open a file for either reading or writing. Note that read/write flag is NOT IMPLEMENTED. Also note that file permission modes are also NOT IMPLEMENTED.- Parameters:
filename
- string containing filenameflag
- 0 for read, 1 for write- Returns:
- file descriptor in the range 0 to SYSCALL_MAXFILES-1, or -1 if error
-
closeFile
public static void closeFile(int fd)Close the file with specified file descriptor- Parameters:
fd
- the file descriptor of an open file
-
resetFiles
public static void resetFiles()Reset all files -- clears out the file descriptor table. -
getFileErrorMessage
Retrieve file operation or error message- Returns:
- string containing message
-