Added basic be/le flag to arch
authorLukas Krickl <lukas@krickl.dev>
Tue, 20 Feb 2024 12:39:21 +0000 (13:39 +0100)
committerLukas Krickl <lukas@krickl.dev>
Tue, 20 Feb 2024 12:39:21 +0000 (13:39 +0100)
src/archs.c
src/archs.h
src/ulas.c

index 371ae7bf5f30404c304061352f093c34e4b2eec4..5b28aeb0de4b24ba6feeb9170c30fe49dc3d53ff 100644 (file)
@@ -320,6 +320,7 @@ void ulas_arch_set(enum ulas_archs arch) {
     ulas.arch = (struct ulas_arch){NULL, 0, ULASINSTRS_SM83};
     ulas.arch.regs_names = ULAS_SM83_REGS;
     ulas.arch.regs_len = ULAS_SM83_REGS_LEN;
+    ulas.arch.endianess = ULAS_LE;
     break;
   default:
     ULASPANIC("Unknown architecture\n");
index 83b4e41c9f4242ec52c619eb256105dc0a95413b..d0f84ac7f43660a5b89fefbf5176a51bbc82bdf2 100644 (file)
@@ -1,6 +1,11 @@
 #ifndef ARCHS_H_
 #define ARCHS_H_
 
+enum ulas_endianess {
+  ULAS_BE,
+  ULAS_LE
+};
+
 /**
  * Insturction table:
  * It is simply a table of all possible instructions for
@@ -65,6 +70,7 @@ struct ulas_arch {
   unsigned long regs_len;
 
   const struct ulas_instr *instrs;
+  enum ulas_endianess endianess;
 };
 
 void ulas_arch_set(enum ulas_archs arch);
index 864380425d52098db6aa3fed198bd4481f200472..6852c8a1d601debae5a19a62b1d3182aff223528 100644 (file)
@@ -1803,7 +1803,8 @@ int ulas_asminstr(char *dst, unsigned long max, const char **line,
     }
 
     // expression results in order they appear
-    // TODO: this should probably become a union of sort to allow float expressions
+    // TODO: this should probably become a union of sort to allow float
+    // expressions
     int exprres[ULAS_INSTRDATMAX];
     int expridx = 0;
     memset(&exprres, 0, sizeof(int) * ULAS_INSTRDATMAX);
@@ -1864,14 +1865,18 @@ int ulas_asminstr(char *dst, unsigned long max, const char **line,
       assert(datread < ULAS_INSTRDATMAX);
       assert(expridx < ULAS_INSTRDATMAX);
 
-      // TODO: implement big endian here
       if (dat[datread] == ULAS_E8 || dat[datread] == ULAS_A8) {
         dst[written] = (char)exprres[expridx++];
       } else if (dat[datread] == ULAS_E16) {
-        // write 16-bit le values
         short val = (short)exprres[expridx++];
-        dst[written++] = (char)(val & 0xFF);
-        dst[written] = (char)(val >> 8);
+        if (ulas.arch.endianess == ULAS_BE) {
+          dst[written++] = (char)(val >> 8);
+          dst[written] = (char)(val & 0xFF);
+        } else {
+          // write 16-bit le values
+          dst[written++] = (char)(val & 0xFF);
+          dst[written] = (char)(val >> 8);
+        }
       } else {
         dst[written] = (char)dat[datread];
       }