CPUSim64 Software Interrupts

The CPUSim64 comes with many functions defined as part of its operating system. These functions are accessed via software interrupts. To access the defines that give symbolic names to the interrupt values you must include the appropriate system include file.

Input to interrupt functions is passed through known registers, specifically r1, r2, r3 for integers and f1 and f2 for floating point. Output is in either r0 or f0 if the function returns a value.

When invoking interrupts, there is no guarantee that the input registers will not be modified.

<system/io.def>

InterruptPneumonicInput RegistersOperation
200iPUT_NLr1: output portOutputs the newline character(s) to the output port.
201iPUT_INTr1: output port
r2: output value
r3: base
Converts the output value to a string using the specified base and outputs it to the specified port.
202iPUT_DECr1: output port
r2: output value
Converts the output value to a string using base 10 and outputs it to the specified port.
203iPUT_HEXr1: output port
r2: output value
r3: pad size
Converts the output value to a string using base 16 and outputs it to the specified port padded with zeros to the pad size.
204iPUT_FPr1: output port
r2: precision
f1: output value
Converts the output value to a string and outputs it to the specified port using the specified precision.
205iPUTSr1: output port
r2: output string address
Outputs a string to the specified port.
206iPUT_LINEr1: output port
r2: output string address
Outputs a string to the specified port and adds a newline.
207iGET_INTr1: input port
r2: base
Gets string from the specified port and converts it to an integer using base.
208iGET_DECr1: input portGets string from the specified port and converts it to an integer using base 10.
209iGET_HEXr1: input portGets string from the specified port and converts it to an integer using base 16.
210iGET_FPr1: input portGets string from the specified port and converts it to floating point.
211iGETSr1: input port
r2: buffer to hold string (from iALLOC)
Gets string from the specified port. r2 will change if buffer is realloced to a larger size. If r2 is 0 then an allocation from the heap will be made to store the string. r0 will be -1 on EOF or the length of the string read.
212iGET_LINEr1: input port
r2: buffer to hold string (from iALLOC)
Gets a line from the specified port. r2 will change if buffer is realloced to a larger size. If r2 is 0 then an allocation from the heap will be made to store the string. The newline is removed from the resultant string. r0 will be -1 on EOF or the length of the string read.
213iPRINTFArg1: port
Arg2: format string address
Arg3: first value
Arg4: second value
...
Stack-based interrupt that implements C-style printf(). Arguments are pushed in reverse order onto the stack. Return address and stack frame are expected to be pushed last which means this interrupt should only be used inside a called function like fprintf().
214iCOND_PRINTFArg1: condition code
Arg2: port
Arg3: format string address
Arg4: first value
Arg4: second value
...
Stack-based interrupt that implements C-style printf(). Arguments are pushed in reverse order onto the stack. Return address and stack frame are expected to be pushed last which means this interrupt should only be used inside a called function like fprintf(). Only prints if the condition is TRUE.
220iOPEN_FILE_READr1: port (3-255)
r2: filepath string
Opens the file for text reading. Returns FALSE in r0 on error. TRUE on success.
221iOPEN_FILE_WRITEr1: port (3-255)
r2: filepath string
Opens the file for text writing. Returns FALSE in r0 on error. TRUE on success.
222iOPEN_FILE_APPENDr1: port (3-255)
r2: filepath string
Opens the file for text append. Returns FALSE in r0 on error. TRUE on success.
223iOPEN_RAW_FILE_READr1: port (3-255)
r2: filepath string
Opens the file for raw read. Returns FALSE in r0 on error. TRUE on success.
224iOPEN_RAW_FILE_WRITEr1: port (3-255)
r2: filepath string
Opens the file for raw write. Returns FALSE in r0 on error. TRUE on success.
225iOPEN_RAW_FILE_APPENDr1: port (3-255)
r2: filepath string
Opens the file for raw append. Returns FALSE in r0 on error. TRUE on success.
226iCLOSE_FILEr1: portCloses the file on port.
227iFLUSHr1: portFlushes output to the file on port.
228iDELETE_FILEr1: filepath stringDeletes the file specified. Returns FALSE in r0 on error. TRUE on success.
229iMAKE_DIRr1: path stringCreates the specified directory. Returns FALSE in r0 on error. TRUE on success.
230iDELETE_DIRr1: path stringDeletes the specified directory. Must be empty. Returns FALSE in r0 on error. TRUE on success.
231iIS_DIRr1: filepath stringReturns TRUE in r0 if the specified filepath belongs to a directory.
232iIS_FILEr1: filepath stringReturns TRUE in r0 if the specified filepath belongs to a file.
233iFILE_EXISTSr1: filepath stringReturns TRUE in r0 if the specified filepath exists as a file or directory.
234iFILESr1: path stringReturns an array of strings containing the files/dirs residing in the path specified.
235iTEMP_DIRr1: prefix stringReturns a heap allocated string containing the name of a randomly generated tempory directory. Returns null on error.
236iTEMP_FILEr1: prefix string
r2: extension string
Returns an heap allocated string containing the name of a randomly generated tempory filepath. Returns null on error.

<system/math.def>

InterruptPneumonicInput RegistersOperation
100iPInoneReturns the floating point constant π.
101iEnoneReturns the floating point constant base of the natural logarithm.
102iABS_FPf1: valueReturns the absolute value of the floating point value.
103iABSr1: valueReturns the absolute value of the integer value.
104iCEILf1: valueReturns the smallest whole number greater than or equal to value.
105iFLOORf1: valueReturns the largest whole number less than or equal to value.
106iROUNDf1: valueReturns the nearest whole number to value. Ties round away from 0.
107iSQRTf1: valueReturns the square root of value.
108iEXPf1: valueReturns evalue.
109iLOGf1: valueReturns the natural logarithm of value.
110iPOWf1: base
f2: exponent
Returns baseexponent
111iREMAINDERf1: num
f2: denom
Returns the fractional portion of num / denom using IEEE 754 remainder rules.
112iMAX_FPf1: value
f2: value
Returns the larger of the two floating point values.
113iMIN_FPf1: value
f2: value
Returns the smaller of the two floating point values.
114iMAXr1: value
r2: value
Returns the larger of the two integer values.
115iMINr1: value
r2: value
Returns the larger of the two integer values.
116iRANDOMnoneReturns a pseudorandom floating point greater than or equal to 0.0 and less than 1.0.
117iRANDr1: lower
r2: upper
Returns a uniformly distributed random integer on the interval [lower,upper]
118iTO_DEGREESf1: valueReturns value converted from radians to degrees.
119iTO_RADIANSf1: valueReturns value converted from degrees to radians.
120iSINf1: value in radiansReturns sin(value).
121iCOSf1: value in radiansReturns cos(value).
122iTANf1: value in radiansReturns tan(value).
123iASINf1: valueReturns arcsin(value) in radians.
124iACOSf1: valueReturns arccos(value) in radians.
126iATANf1: valueReturns arctan(value) in radians.
125iATAN2f1: num
f2: denom
Returns arctan(num/denom) in radians even if denom is 0.

<system/string.def>

InterruptPneumonicInput RegistersOperation
301iFMT_DECr1: valueFormats the integer value in decimal as a heap allocated string.
302iFMT_HEXr1: value
r2: pad size
Formats the integer value in hexdecimal padded with zeros to the pad size as a heap allocated string.
303iFMT_FLOATf1: value
r1: precision
Formats the floating point value to the specified precision as a heap allocated string.
304iPARSE_INTr1: string addressParses a string as decimal, hexadecimal (0x or # prefix) or octal (0 prefix) integer.
305iPARSE_DECr1: string addressParses a string as a decimal integer.
306iPARSE_HEXr1: string addressParses a string as a hexadecimal integer.
307iPARSE_FLOATr1: string addressParses a string as a floating point value.
308iSPRINTFArg1: format string address
Arg2: first value
Arg3: second value
...
Stack-based interrupt that implements C-style sprintf(). Arguments are pushed in reverse order onto the stack. Return address and stack frame are expected to be pushed last which means this interrupt should only be used inside a called function like sprintf(). Returns a heap allocated string in r0.
309iFORMATArg1: format string address
Arg1: first value
Arg2: second value
...
Stack-based interrupt that implements Python-style format(). Arguments are pushed in reverse order onto the stack. Return address and stack frame are expected to be pushed last which means this interrupt should only be used inside a called function like format(). Returns a heap allocated string in r0.
310iTO_LOWERr1: characterReturns the lowercase version of the character.
311iTO_UPPERr1: characterReturns the uppercase version of the character.
312iTO_LOWER_STRr1: string addressReturns lowercase version of string as a heap allocated string.
313iTO_UPPER_STRr1: string addressReturns uppercase version of string as a heap allocated string.
314iSTRCOPYr1: stringAllocates a new string and copies the argument. Returns the new string pointer in r0.
315iSTRCMPr1: string 1
r2: string 2
Compares string 1 to string 2 and returns 0 if equal, <0 if string 1 is less than string 2, and >0 if string 1 is greater than string 2.
316iSTRICMPr1: string 1
r2: string 2
Ignoring case, compares string 1 to string 2 and returns 0 if equal, <0 if string 1 is less than string 2, and >0 if string 1 is greater than string 2.
317iSUBSTRINGr1: string
r2: start index
r3: length (-1 to end of string)
Returns a substring of a string as a heap allocated string.
318iPREFIXr1: String
r2: length
Returns length character prefix of the string as a heap allocated string.
319iSUFFIXr1: String
r2: length
Returns length character suffix of the string as a heap allocated string.
320iCHAR_SEARCHr1: string
r2: search char
r3: start pos
Returns the index of the location of the character in the string or -1 if not found.
321iLAST_CHAR_SEARCHr1: string
r2: search char
r3: start pos
Returns the index of the location of the character in the string searching from the end or -1 if not found.
322iSUBSTRING_SEARCHr1: string to search
r2: string to find
r3: start pos
Searches a string for the specified substring and returns the index into the string if found or -1 if not found.
323iLAST_SUBSTRING_SEARCHr1: string to search
r2: string to find
r3: start pos
Searches a string from the end for the specified substring and returns the index into the string if found or -1 if not found.
324iGET_CODEPOINTSr1: string 1Returns a heap allocated array of the codepoints in the string.
325iFROM_CODEPOINTSr1: array of codepointsReturns a heap allocated string based on the passed array of codepoints.
326iCOUNT_GLPYHSr1: stringReturns the number of glyphs (graphemes) in the string.
327iHASHCODEr1: stringReturns the hashcode for the passed string.
328iTRIMr1: stringReturns a heap allocatd string with the leading and trailing whitespace removed.
350iMATCHESr1: string to search
r2: regex to find
Searches a string for the specified regular expression and returns TRUE if found or FALSE if not found.
351iREPLACE_FIRSTr1: string to search
r2: regex to find
r3: replacement string
Searches a string for the specified regular expression and returns the heap allocated string that has the first occurance replaced by the replacement string.
352iREPLACE_ALLr1: string to search
r2: regex to find
r3: replacement string
Searches a string for the specified regular expression and returns the heap allocated string that has all the occurances replaced by the replacement string.
353iSPLITr1: string to split
r2: split regex
r3: limit
Splits the string using the regex returning an array of strings capped with maximum elements specified by limit. If limit is -1 the splitting is not capped. Returns a heap allocated array of heap allocated strings.
354iJOINr1: list of strings
r2: delimiter string
Joins a length prefixed list of strings using the delimiter string.
355iSTRCATr1: string 1
r2: string 2
r3: buffer or 0
Concatenates the first string and second string. Result is put into buffer (might be reallocated) in r3.

<system/system.def>

InterruptPneumonicInput RegistersOperation
1iINT_MINnoneReturns largest negative integer.
2iINT_MAXnoneReturns largest negative positive integer.
3iFLOAT_MINnoneReturns most negative floating point value.
4iFLOAT_LOWESTnoneReturns smallest floating point value greater than 0.
5iFLOAT_MAXnoneReturns largest poitive floating point value.
6iNEGATIVE_INFINITYnoneReturns negative infinity.
7iPOSITIVE_INFINITYnoneReturns positive infinity.
8iNANnoneReturns Not A Number.
10iCYCLESnoneReturns user CPU time in cycles.
11iCLOCKnoneReturns current execution time in ns.
12iSAVEnoneSaves r1-r28 on the stack.
13iSAVE_FPnoneSaves f1-f31 on the stack.
14iRESTOREnoneRestores r1-r28 from the stack.
15iRESTORE_FPnoneRestores f1-f31 from the stack.
16iPrintCPUStatenonePrints a diagram of the current CPU state and stack.
20iALLOCr1: allocation sizeReturns a heap allocated block address.
21iREALLOCr1: allocation address
r2: new allocation size
Returns a new heap allocated block with data copied from the passed allocation address. The original allocation address is freed if it is in the heap.
22iFREEr1: allocation addressReturns the heap allocated block address to the free list.
23iMEMMOVEr1: dest address
r2: src address
r3: length
Moves contents of memory from src to dest. Can handle overlapping memory blocks.
24iMEMCLEARr1: dest address
r2: length
Clears contents of memory from dest for r2 words.
25iALLOC_COUNTnoneReturns a count of heap allocated blocks.
26iFREE_COUNTnoneReturns a count of heap free blocks.
27iALLOC_SIZEnoneReturns total size of heap allocated blocks.
28iFREE_SIZEnoneReturns total size of heap free blocks.
29iWALK_HEAPr1: portOutputs the allocated blocks in the heap.
30iARGCnoneReturns the number of command line arguments.
31iARGSr1: argument indexReturns address of of the command line argument specified by index or null. Index 0 returns the program name.
32iEXITr1: exit codeExits process with specified code.
33iSYSTEMr1: string addressExecutes the system command contained in the string. Returns the exit code of the command.
34iGET_PIDnoneReturns user process ID.
35iNEXT_PIDnoneReturns the next available process ID.
36iGET_NUM_CORESnoneReturns user number of CPU cores.
37iFORKnoneUnix-style process fork. Zero is returned to the child process and positive PID of the child is returned to the parent. If the fork fails, -1 is returned.
38iWAITWaits for all child processes to complete.
39iWAIT_PIDr1: PIDWaits for the child process with PID to complete.
40iTHREADr1: function, r2: dataStarts a thread running the function passed in r1 and the data in r2. Returns the PID of the thread in r1.
41iJOIN_THREADr1: PIDWaits for the thread with PID to complete.
42iSLEEPr1: delay in msPauses execution for the specified milliseconds.
43iWAKE_THREADr1: PIDWakes a thread from sleep.
44iALLOC_SHAREDr1: allocation sizeReturns a shared block of memory.