aboutsummaryrefslogtreecommitdiff
path: root/comp1521/smips/input.c
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-13 18:00:17 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-13 18:00:17 +1100
commit98cef5e9a772602d42acfcf233838c760424db9a (patch)
tree5277fa1d7cc0a69a0f166fcbf10fd320f345f049 /comp1521/smips/input.c
initial commit
Diffstat (limited to 'comp1521/smips/input.c')
-rw-r--r--comp1521/smips/input.c52
1 files changed, 52 insertions, 0 deletions
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);
+}