The CPUSim64 comes with many library functions to make programming in CPU64 assembly easier. These functions are defined in .asm files and included using the #include preprocessor directive. Functions use stack-based calling conventions for arguments and return a value in R0 or F0.
Libraries also contain macros that use register-based calling conventions. Macros are defined in .def files along with the definitions for interrupts used by that library. When invoking macros, there is no guarantee that the registers R0, R1, R2, F0, F1 or F2 will be preserved. Most macros return a value in R0 or F0.
These debug macros only compile into your code if the assembler is passed the --DEBUG option. This allows you to retain debugging instructions and still create "non-debug" code that doesn't have the debugging instructions present just by omitting the --DEBUG option.
| Macro | Description |
|---|---|
| DEBUG_MSG | DEBUG_MSG(fmt, ...)Formats a printf-style format string using the variable number of arguments supplied and prints it as a debug message. |
| COND_DEBUG_MSG | COND_DEBUG_MSG(cond, fmt, ...)Formats a prinf-style format string using the variable number of arguments supplied and prints it as a debug message if the argument cond is true, otherwise it prints nothing. |
| PRINTCPU | PRINTCPUPrints the current state of the CPU. |
| SET_EXIT_ON_ASSERT_FAILURE | SET_EXIT_ON_ASSERT_FAILURE(b)Sets the mode for asserts. If the argument b is true, assert macros will print message and then exit the program if they fail. Otherwise assert macros will print a message if they fail but not exit. Default is true. |
| ASSERT_TRUE | ASSERT_TRUE(isTrue, message)Asserts that a condition is true. Prints the message if the assertion fails. |
| ASSERT_FALSE | ASSERT_FALSE(isFalse, message)Asserts that a condition is false. Prints the message if the assertion fails. |
| ASSERT_EQ | ASSERT_EQ(expected, actual, message)Asserts that integer values expected and actual are equal. Prints the message if the assertion fails. |
| ASSERT_NE | ASSERT_NE(expected, actual, message)Asserts that integer values expected and actual are not equal. Prints the message if the assertion fails. |
| ASSERT_GT | ASSERT_GT(expected, actual, message)Asserts that integer value expected is greater than actual. Prints the message if the assertion fails. |
| ASSERT_LT | ASSERT_LT(expected, actual, message)Asserts that integer value expected is less than actual. Prints the message if the assertion fails. |
| ASSERT_GE | ASSERT_GE(expected, actual, message)Asserts that integer value expected is greater than or equal to actual. Prints the message if the assertion fails. |
| ASSERT_LE | ASSERT_LE(expected, actual, message)Asserts that integer value expected is less than or equal to actual. Prints the message if the assertion fails. |
| ASSERT_EQ_FP | ASSERT_EQ_FP(expected, actual, message)Asserts that floating point values expected and actual are equal. Prints the message if the assertion fails. |
| ASSERT_NE_FP | ASSERT_NE_FP(expected, actual, message)Asserts that floating point values expected and actual are not equal. Prints the message if the assertion fails. |
| ASSERT_GT_FP | ASSERT_GT_FP(expected, actual, message)Asserts that floating point value expected is greater than actual. Prints the message if the assertion fails. |
| ASSERT_LT_FP | ASSERT_LT_FP(expected, actual, message)Asserts that floating point value expected is less than actual. Prints the message if the assertion fails. |
| ASSERT_GE_FP | ASSERT_GE_FP(expected, actual, message)Asserts that floating point value expected is greater than or equal to actual. Prints the message if the assertion fails. |
| ASSERT_LE_FP | ASSERT_LE_FP(expected, actual, message)Asserts that floating point value expected is less than or equal to actual. Prints the message if the assertion fails. |
The floating point assertion macros require that both float values be registers. The integer assertion macros can use a register or a literal for the values.
These are the I/O macros defined for CPUSim64.
| Macro | Description |
|---|---|
| IN0 | IN0(register,port)Reads one Unicode codepoint from the port. |
| IN1 | IN1(register,port)Reads one byte from the port. |
| IN2 | IN2(register,port)Reads one 2 byte short word from the port. |
| IN4 | IN4(register,port)Reads one 4 byte word from the port. |
| IN8 | IN8(register,port)Reads one 8 byte long word from the port. |
| OUT0 | OUT0(register, port)Writes one Unicode codepoint to the port. |
| OUT1 | OUT1(register, port)Writes one byte to the port. |
| OUT2 | OUT2(register, port)Writes one 2 byte short word to the port. |
| OUT4 | OUT4(register, port)Writes one 4 byte word to the port. |
| OUT8 | OUT8(register, port)Writes one 8 byte long to the port. |
| PUT_NL | PUT_NL()Writes the newline character(s) to STDOUT. |
| PUTS | PUTS(str)Writes the UTF-8 string pointed to by str to STDOUT. |
| PUT_INT | PUT_INT(val, base)Writes the integer val to STDOUT using the specified base. |
| PUT_DEC | PUT_DEC(val)Writes the integer to STDOUT in base 10. |
| PUT_HEX | PUT_HEX(val)Writes the integer to STDOUT in base 16. |
| PUT_FP | PUT_FP(val, prec)Writes the float to STDOUT. |
These are the I/O functions defined for CPUSim64.
| Function | Description |
|---|---|
| fgetline | fgetline(port, buffer)Read an entire line of text input from the port. The buffer argument points to a heap allocated buffer. Returns 0 on EOF or error. Returns buffer or perhaps a reallocated heap block. Must free when done with buffer. |
| put_nl | put_nl()Thread safe output of the newline character(s) to STDOUT. |
| fput_nl | fput_nl(port)Non-thread safe output of the newline character(s) to the port. |
| putc | putc(cp)Thread safe output of the Unicode codepoint to STDOUT. |
| fputc | fputc(port, cp)Non-thread safe output of the Unicode codepoint to the port. |
| puts | puts(str)Thread safe output of the UTF-8 string pointed to by str to STDOUT. |
| fputs | fputs(port, str)Non-thread safe output of the UTF-8 string pointed to by str to the port. |
| putline | putline(str)Thread safe output of the UTF-8 string pointed to by str to STDOUT with a newline appended. |
| fputline | fputline(port, str)Non-thread safe output of the UTF-8 string pointed to by str to STDOUT with a newline appended. |
| put_int | put_int(value, base)Thread safe output of the integer value to STDOUT using the specified base. |
| fput_int | fput_int(port, value, base)Non-thread safe output of the integer value to the port using the specified base. |
| put_dec | put_dec(value)Thread safe output of the integer value to STDOUT using base 10. |
| fput_dec | fput_dec(port, value)Non-thread safe output of the integer value to the port using base 10. |
| put_hex | put_hex(value)Thread safe output of the integer value to STDOUT using base 16. |
| fput_hex | fput_hex(port, value)Non-thread safe output of the integer value to the port using base 16. |
| put_hex_size | put_hex_size(value, size)Thread safe output of the integer value to STDOUT using base 16. Outputs at least size digits by padding with 0. |
| fput_hex_size | fput_hex_size(port, value, size)Non-thread safe output of the integer value to the port using base 16. Outputs at least size digits by padding with 0. |
| put_fp | put_fp(value, prec)Thread safe output of the float value to STDOUT. Output is rounded to prec significant digits. |
| fput_fp | fput_fp(port, value, prec)Non-thread safe output of the float value to the port. Output is rounded to prec significant digits. |
| printf | printf(fmt, values...)Thread safe output of the format string pointed to by fmt using values to STDOUT. Format specifiers include: %s, %c, %d, %x and %f. |
| fprintf | fprintf(port, fmt, values...)Non-thread safe output of the format string pointed to by fmt using values to port. Format specifiers include: %s, %c, %d, %x and %f. |
| cond_printf | cond_printf(cond, fmt, values...)Thread safe output of the format string pointed to by fmt using values to STDOUT only if cond is true. Format specifiers include: %s, %c, %d, %x and %f. |
| cond_fprintf | cond_fprintf(cond, port, fmt, values...)Non-thread safe output of the format string pointed to by fmt using values to port only if cond is true. Format specifiers include: %s, %c, %d, %x and %f. |
| fatal | fatal(fmt, values...)Thread safe output of the format string pointed to by fmt using values to STDERR. Format specifiers include: %s, %c, %d, %x and %f. After output the program is terminated. |
| cond_fatal | printf(cond, fmt, values...)Thread safe output of the format string pointed to by fmt using values to STDERR only if cond is true. Format specifiers include: %s, %c, %d, %x and %f. If cond is true the program is terminated. |
| opentTextFile | openTextFile(filename, mode)Opens the text file specified by filename using mode (READ_MODE, WRITE_MODE or APPEND_MODE). A non-negative port number is returned if successful whereas -1 on error. Reading or writing to text files will convert between operating system specific newlines and the newline ('\n') character. |
| opentRawFile | openRawFile(filename, mode)Opens the raw file specified by filename using mode (READ_MODE, WRITE_MODE or APPEND_MODE). A non-negative port number is returned if successful whereas -1 on error. Reading or writing to raw files will not perform any newline conversions. |
| close_file | close_file(port)Closes the port specified and makes it available for reuse. |
| flush | flush(port)Flushes buffered writes to the port. |
| deleteFile | deleteFile(filespec)Deletes the file specified by the pointer to UTF-8 string filespec. |
| makeDirectory | makeDirectory(path)Creates the directory specified by the pointer to UTF-8 string path. |
| deleteDirectory | deleteDirectory(path)Deletes the directory (if empty) specified by the pointer to UTF-8 string path. |
| isDirectory | isDirectory(path)Returns true if the path specified by the pointer to UTF-8 string path is a directory. |
| deleteFiles | deleteFiles(strList)Deletes the files specified in the list of pointers to UTF-8 strings. |
| listFiles | listFiles(path)Returns a heap allocated list of UTF-8 strings for the files in directory specified by the pointer to UTF-8 string path. |
| isFile | isFile(path)Returns true if the path specified by the pointer to UTF-8 string path is a regular file. |
| fileExists | fileExists(path)Deletes the file specified by the pointer to UTF-8 string path. |
| tempDirectory | tempDirectory(prefix)Returns a heap allocated UTF-8 string with a valid temporary directory location using prefix at the beginning of the directory name. |
| tempFile | tempFile(prefix, suffix)Returns a heap allocated UTF-8 string with a valid temporary file location using prefix at the beginning of the file name and suffix at the end. |
| copy_text_file | copy_text_file(fromPort, toPort)Copies the text file from the open port fromPort to the open port toPort. |
| copy_raw_file | copy_raw_file(fromPort, toPort)Copies the raw file from the open port fromPort to the open port toPort. |
| printIntegerArrayAsList | printIntegerArrayAsList(addrArg)Prints the array of integers to STDOUT as a comma separated list. |
| printFloatArrayAsList | printFloatArrayAsList(addrArg)Prints the array of floats to STDOUT as a comma separated list. |
| printStringArrayAsList | printStringArrayAslist(addrArg)Prints the array of UTF-8 strings to STDOUT as a comma separated list. |
| Macro | Description |
|---|---|
| MIN | MIN(a, b)Returns in R0 the smaller of integers a or b. |
| MAX | MAX(a, b)Returns in R0 the larger of integers a or b. |
| FMIN | FMIN(a, b)Returns in F0 the smaller of float a or b. |
| FMAX | FMAX(a, b)Returns in F0 the larger of float a or b. |
These are the math functions defined for CPUSim64. Integer arguments can be general purpose registers or integer constants. They are indicated by arguments named i, j, k, etc. Floating point arguments must be floating point registers. Floating point arguments are indicated using names x, y, z, a, b, c, etc.
| Function | Description |
|---|---|
| pi | pi()Returns in F0 the mathematics constant π. |
| ln_base | ln_base()Returns in F0 the base of the natural logarithm e. |
| idiv | idiv(i, j)Returns in R0 the integer result of i/j. |
| mod | mod(i, j)Returns in R0 the remainder after division of i/j. |
| abs | abs(i)Returns in R0 the absolute value of the integer i. |
| fabs | fabs(x)Returns in F0 the absolute value of the float x. |
| ceil | ceil(x)Returns in F0 the smallest integer greater than float x. |
| floor | floor(x)Returns in F0 the largest integer less than float x. |
| round | round(x)Returns in F0 the nearest integer to float x. It rounds down if the decimal part is less than 0.5 and rounds up if it is 0.5 or greater. |
| sqrt | sqrt(x)Returns in F0 the square root of float x. |
| exp | exp(x)Returns in F0 the value e^x. |
| log | log(x)Returns in F0 the natural logarithm of x. |
| exp10 | exp10(x)Returns in F0 the value 10.^x. |
| log10 | log10(x)Returns in F0 the common logarithm of x. |
| exp2 | exp2(x)Returns in F0 the value 2.^x. |
| log2 | log2(x)Returns in F0 the binary logarithm of x. |
| pow | pow(x, y)Returns in F0 the value x^y. |
| ifastpow | ifastpow(i, j)Returns in R0 the value i^j using fast exponentiation algorithm. |
| fastpow | fastpow(x, j)Returns in F0 the value x^j using fast exponentiation algorithm. |
| random | random()Returns in F0 a random value in the range [0.0, 1.0). |
| rand | rand(i, j)Returns in R0 a random value in the range [i,j]. |
| min | min(a, b)Returns in R0 the smaller of integers a or b. |
| max | max(a, b)Returns in R0 the larger of integers a or b. |
| fmin | fmin(a, b)Returns in F0 the smaller of float a or b. |
| fmax | fmax(a, b)Returns in F0 the larger of float a or b. |
| sum | sum(array)Returns in R0 the sum of the integers at address array. First element of array is the array size. |
| fsum | fsum(array)Returns in F0 the sum of the floats at address array. First element of array is the array size. |
| mod | mod() |
These are the string macros defined for CPUSim64.
| Macro | Description |
|---|---|
| FMT_DEC | FMT_DEC(x)Formats the value x as a base-10 (decimal) string. |
| FMT_HEX | FMT_HEX(x)Formats the value x as a hexadecimal string. |
| FMT_FLOAT | FMT_FLOAT(x)Formats the value x as a floating-point string. |
| PARSE_INT | PARSE_INT(x)Parses string x and converts it to an integer using automatic base detection. |
| PARSE_DEC | PARSE_DEC(x)Parses decimal string x and converts it to an integer. |
| PARSE_HEX | PARSE_HEX(x)Parses hexadecimal string x and converts it to an integer. |
| PARSE_FLOAT | PARSE_FLOAT(x)Parses string x and converts it to a floating-point value. |
| TO_LOWER | TO_LOWER(x)Converts a single codepoint x to lowercase. |
| TO_UPPER | TO_UPPER(x)Converts a single codepoint x to uppercase. |
| TO_LOWER_STR | TO_LOWER_STR(x)Returns a lowercase version of string x as a heap allocated string. |
| TO_UPPER_STR | TO_UPPER_STR(x)Returns an uppercase version of string x as a heap allocated string. |
| STRLEN | STRLEN(s1)Returns the number of characters in string s1. |
| STRCOPY | STRCOPY(s1)Creates and returns a heap allocated copy of string s1. |
| STRCMP | STRCMP(s1, s2)Compares strings s1 and s2 lexicographically. Returns negative integer if s1 < s2, 0 if equal, and a positive integer if s1 > s2. |
| STRICMP | STRICMP(s1, s2)Compares strings s1 and s2 lexicographically in a case-insensitive manner. Returns negative integer if s1 < s2, 0 if equal, and a positive integer if s1 > s2. |
| SUBSTRING | SUBSTRING(s, start, length)Extracts a substring from s starting at start with the specified length. |
| PREFIX | PREFIX(s, length)Returns the first length characters of string s. |
| SUFFIX | SUFFIX(s, length)Returns the last length characters of string s. |
| CHAR_SEARCH | CHAR_SEARCH(s, char, startpos)Finds the first occurrence of codepoint char in s starting at startpos. |
| LAST_CHAR_SEARCH | LAST_CHAR_SEARCH(s, char, startpos)Finds the last occurrence of codepoint char in s starting at startpos and searching backward. |
| SUBSTRING_SEARCH | SUBSTRING_SEARCH(s, substr, startpos)Finds the first occurrence of substr in s starting at startpos. |
| LAST_SUBSTRING_SEARCH | LAST_SUBSTRING_SEARCH(s, substr, startpos)Finds the last occurrence of substr in s starting at startpos and searching backward. |
| STARTS_WITH | STARTS_WITH(str, prefix)Checks to see if the string begins with prefix. |
| ENDS_WITH | ENDS_WITH(str, suffix)Checks to see if the string ends with suffix. |
| GET_CODEPOINTS | GET_CODEPOINTS(str)Returns an array of Unicode codepoints from string str. |
| FROM_CODEPOINTS | FROM_CODEPOINTS(arr)Creates a heap allocated string from an array of Unicode codepoints. |
| COUNT_GLPYHS | COUNT_GLPYHS(str)Returns the number of user-visible glyphs in string str. |
| HASH_CODE | HASH_CODE(str)Computes and returns a hash value for string str. |
| TRIM | TRIM(str)Removes leading and trailing whitespace from string str. |
| MATCHES | MATCHES(str, pattern)Checks whether str matches the given regex pattern. |
| REPLACE_FIRST | REPLACE_FIRST(str, pattern, replacement)Replaces the first occurrence of regex pattern in str with replacement. |
| REPLACE_ALL | REPLACE_ALL(str, pattern, replacement)Replaces all occurrences of regex pattern in str with replacement. |
| SPLIT | SPLIT(str, delimiter)Splits str into an heap allocated array using delimiter. |
| JOIN | JOIN(arr, delimiter)Joins array arr into a heap allocated string using delimiter. |
| STRCAT | STRCAT(s1, s2)Concatenates strings s1 and s2 and returns the heap allocated result. |
These are the string functions defined for CPUSim64.
| Function | Description |
|---|---|
| printStringArray | printStringArray(a)Prints an array of UTF-8 strings with the index and value. |
| freeStrArray | freeStrArray(addArg)Frees the heap allocated array containing heap allocated strings. |
| sprintf | sprintf(fmt, values...)Returns a heap allocated string with the values applied to the format string fmt. Format specifiers include: %s, %c, %d, %x and %f. |
| format | format(fmt, values...)Returns a heap allocated string with the values applied to the format string fmt. Format specifiers include: {}, {0}, {1}, etc. |
These are the system macros defined for CPUSim64.
| Macro | Description |
|---|---|
| TO_BOOLEAN | TO_BOOLEAN(x)Returns TRUE or FALSE depending on the truthiness of x. Truthiness is false for zero and true for non-zero. |
| TO_NOT_BOOLEAN | TO_NOT_BOOLEAN(x)Returns TRUE or FALSE depending on the not-truthiness of x. Truthiness is false for zero and true for non-zero. |
| COMPARE | COMPARE(x, op, y)Compares x to y using operator op. Status register will be set according to the comparison. |
| COMPARE_RANGE | COMPARE_RANGE(low, op1, x, op2, high)Compares x to low and high using operators op1 and op2. Status register will be set according to the comparison. |
| ALLOC | ALLOC(size)Allocates a heap block of size long words. Returned address must eventually be freed. |
| REALLOC | REALLOC(addr, size)Reallocates heap block at addr to size long words. May return addr or a new address of a larger heap block. |
| FREE | FREE(addr)Releases the heap allocated memory at the address addr. |
| FREE_ELEMENT | FREE_ELEMENT(a, offset)Releases the heap allocated memory using the address in array element a[offset]. |
| ALLOC_SHARED | ALLOC_SHARED(size)Allocates size long words in the shared memory area. This memory can not be freed. |
| MEMMOVE | MEMMOVE(dest, src, count)Moves count long words from src to dest. Handles overlapping memory. |
| MEMCLEAR | MEMCLEAR(dest, count)Sets the memory to 0 starting at address dest for count long words. |
| SLEEP | SLEEP(ms)Puts the current thread to sleep for ms milliseconds. |
These are the system functions defined for CPUSim64.
| Function | Description |
|---|---|
| printIntegerArray | printIntegerArray(a)Prints an array of integers with the index and value. |
| printFloatArray | printFloatArray(a)Prints an array of floats with the index and value. |
| memmove | memmove(dest, src, count)Moves count long words from src to dest. Handles overlapping memory. |
| memclear | memclear(dest, count)Sets the memory to 0 starting at address dest for count long words. |
| sleep | sleep(ms)Puts the current thread to sleep for ms milliseconds. |
| exit | exit(code)Terminates the program returning the code value to the operating system. |
| system | system(cmd)Executes the shell command specified in the UTF-8 string cmd. |
| alloc | alloc(size)Allocates a heap block of size long words. Returned address must eventually be freed. |
| realloc | realloc(addr, size)Reallocates heap block at addr to size long words. May return addr or a new address of a larger heap block. |
| free | free(addr)Releases the heap allocated memory at the address addr. |
| freeElement | freeElement(addr, offset)Releases the heap allocated memory using the address in array element a[offset]. |
| args | args(index)Returns the command line argument specified by index. Name of the program is in index 0. Command line arguments start at index 1. |
| mutexLock | mutexLock(addr, timeout)Pauses the current thread until the mutex at addr can be acquired or until timeout is reached. Timeout is in milliseconds. Use -1 for no timeout. Returns TRUE if successful. |
| mutexLockElement | mutexLockElement(addr, offset, timeout)auses the current thread until the mutex at addr[offset] can be acquired or until timeout is reached. Timeout is in milliseconds. Use -1 for no timeout. Returns TRUE if successful. |
| mutexUnlock | mutexUnlock(addr)Unlocks the mutex at addr. Returns TRUE if successful. |
| mutexUnlockElement | mutexUnlockElement(addr, offset)Unlocks the mutex at addr[offset]. Returns TRUE if successful. |
These are the thread macros defined for CPUSim64.
| Macro | Description |
|---|---|
| DEFINE_SPINLOCK | DEFINE_SPINLOCK(name)Allocates a spinlock with the specified name in the global data area. |
| DEFINE_SHARED_SPINLOCK | DEFINE_SHARED_SPINLOCK(name)Allocates a spinlock in the shared memory area. Stores the address of the spinlock in the variable with the specified name in the global data area. |
| DEFINE_RECURSIVE_SPINLOCK | DEFINE_RECURSIVE_SPINLOCK(name)Allocates a recursive spinlock with the specified name in the global data area. |
| DEFINE_SHARED_RECURSIVE_SPINLOCK | DEFINE_SHARED_RECURSIVE_SPINLOCK(name)Allocates a recursive spinlock in the shared memory area. Stores the address of the recursive spinlock in the variable with the specified name in the global data area. |
| DEFINE_MUTEX | DEFINE_MUTEX(name)Allocates a mutex with the specified name in the global data area. |
| DEFINE_SHARED_MUTEX | DEFINE_SHARED_MUTEX(name)Allocates a mutex in the shared memory area. Stores the address of the mutex in the variable with the specified name in the global data area. |
| CREATE_THREAD | CREATE_THREAD(entryPoint, data)Creates a thread that runs the code at entryPoint and passed the address of a data block at data. Returns thread PID of the new thread in R0 or -1 on error. |
| JOIN_THREAD | JOIN_THREAD(threadPID)Pauses the execution of the current thread until the thread with PID threadPID terminates. |
| WAKE_THREAD | WAKE_THREAD(threadPID)Wakes the thread with PID threadPID. |
These are the thread functions defined for CPUSim64.
| Function | Description |
|---|---|
| initializeSpinLock | initializeSpinLock(addr)Initializes the spinlock at address addr. |
| acquireSpinLock | acquireSpinLock(addr)Pauses the current thread until it can acquire the spinlock at address addr. |
| releaseSpinLock | releaseSpinLock(addr)Release the spinlock at address addr. |
| initializeRecursiveSpinLock | initializeRecursiveSpinLock(addr)Initializes the recursive spinlock at address addr. |
| acquireRecursiveSpinLock | acquireRecursiveSpinLock(addr)Pauses the current thread until it can acquire the recursive spinlock at address addr. |
| releaseRecursiveSpinLock | releaseRecursiveSpinLock(addr)Release the recursive spinlock at address addr. |
| initializeMutex | initializeMutex(addr) | Initializes the mutex at address
| acquireMutex | acquireMutex(addr)Pauses the current thread until it can acquire the mutex at address addr. |
| releaseMutex | releaseMutex(addr)Release the mutex at address addr. |
| get_and_increment | get_and_increment(addr)Atomically increments the value at addr. Returns the new value. |
| get_and_decrement | get_and_decrement(addr)Atomically decrements the value at addr. Returns the new value. |