From 98cef5e9a772602d42acfcf233838c760424db9a Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Thu, 13 Feb 2025 18:00:17 +1100 Subject: initial commit --- comp1521/smips/input.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 comp1521/smips/input.c (limited to 'comp1521/smips/input.c') diff --git a/comp1521/smips/input.c b/comp1521/smips/input.c new file mode 100644 index 0000000..0932978 --- /dev/null +++ b/comp1521/smips/input.c @@ -0,0 +1,52 @@ +#include "input.h" + +// Converts decimal values of a uint32_t argument to hex. +uint32_t hex_to_decimal(const uint32_t decimal) { + uint32_t ret = 0; + for (size_t i = 0; i < sizeof(uint32_t); ++i) { // Iterate through 4 bytes. + uint32_t tmp = (decimal & (0xFFu << 8 * i)) >> 8 * i; + if (isalpha(tmp)) { + tmp -= 'a'; + tmp += 10; + } else if (isdigit(tmp)) { + tmp -= '0'; + } + // There should be no other cases if the input is hex. + ret |= (tmp << 4 * i); + } + return ret; +} + +// Returns the next instruction in hex. Returns 0 if EOF reached. +uint32_t get_next_instruction(FILE *const iptr) { + uint32_t ret = 0; + int i = 0; + for (int c = fgetc(iptr); c != '\n'; c = fgetc(iptr), ++i) { + if (c == EOF) { + return 0u; + } + uint32_t tmp = hex_to_decimal((uint32_t)c); + ret |= (tmp << (28 - i * 4)); + } + // We have to take into account the potential for the hex value to not be + // "full", ie we may read abc instead of 00000abc. + ret >>= (32 - i * 4); + return ret; +} + +// Sets the iptr to the previous instruction, which sits on the newline so that +// later calls to get_next_instruction work as expected. +// Also, if unable to find a previous instruction, sets the iptr to EOF. +void goto_previous_instruction(FILE *const iptr) { + int c = 0; + fseek(iptr, -2L, SEEK_CUR); + while (ftell(iptr)) { + c = fgetc(iptr); + if (c == '\n') { + return; + } + fseek(iptr, -2L, SEEK_CUR); + } + // Start of file reached, out of bounds, so set EOF. + fseek(iptr, 0L, SEEK_END); +} -- cgit v1.2.3