Fixed 16 bit values
authorLukas Krickl <lukas@krickl.dev>
Mon, 26 Feb 2024 05:56:24 +0000 (06:56 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 26 Feb 2024 05:56:24 +0000 (06:56 +0100)
src/archs.c
src/archs.h
src/uldas.c

index a8d4aa4a82ffbe1485886e2ec53c7c14499c1eba..75c77d677d35ce0352c03a2c88ac9dab795ff795 100644 (file)
@@ -326,10 +326,25 @@ const char *ULAS_SM83_REGS[] = {
 void ulas_arch_set(enum ulas_archs arch) {
   switch (arch) {
   case ULAS_ARCH_SM83:
-    ulas.arch = (struct ulas_arch){ULAS_SM83_REGS, ULAS_SM83_REGS_LEN,
+    ulas.arch = (struct ulas_arch){arch, ULAS_SM83_REGS, ULAS_SM83_REGS_LEN,
                                    ULASINSTRS_SM83, ULAS_LE};
     break;
   default:
     ULASPANIC("Unknown architecture\n");
   }
 }
+
+unsigned int ulas_arch_opcode_len(const char *buf, unsigned long read) {
+  if (read == 0) {
+    return 0;
+  }
+  switch (ulas.arch.type) {
+    case ULAS_ARCH_SM83:
+      if ((unsigned char)buf[0] == 0xCB) {
+        return 2;
+      }
+      return 1;
+    default:
+      return 0;
+  }
+}
index 26e885a7071fbf3ac0fd6e8b39b5c319d14abed4..822c0aeb52253d7583bc9267dd20153c37df2813 100644 (file)
@@ -69,6 +69,7 @@ enum ulas_asmspetok {
 enum ulas_archs { ULAS_ARCH_SM83 };
 
 struct ulas_arch {
+  enum ulas_archs type;
   const char **regs_names;
   unsigned long regs_len;
 
@@ -78,4 +79,8 @@ struct ulas_arch {
 
 void ulas_arch_set(enum ulas_archs arch);
 
+// returns how many bytes of an instruction are occupied 
+// by the opcode based on its data 
+unsigned int ulas_arch_opcode_len(const char *buf, unsigned long read);
+
 #endif
index fdb512fa084133d5a606bb0ab61816e75c252790..cecb44b9896cefdf4d9e1674c4ddfe4eaaa03034 100644 (file)
@@ -17,7 +17,7 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr,
   ulas_dasm_print_addr(dst);
 
   fprintf(dst, "  %s ", instr->name);
-  int bi = 0;
+  unsigned int bi = ulas_arch_opcode_len(buf, read);
   for (int i = 0; instr->tokens[i]; i++) {
     int dat = instr->tokens[i];
     switch (dat) {
@@ -25,19 +25,18 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr,
     case ULAS_A8:
       fprintf(dst, "0x%x", buf[bi++]);
       break;
+    case ULAS_A16:
     case ULAS_E16: {
       unsigned short val = 0;
       if (ulas.arch.endianess == ULAS_BE) {
-
+        val = (short)buf[bi + 1] | (short)(buf[bi] << 8);
       } else {
-        val = (char)buf[bi+1] | (char)(buf[bi] << 8);
+        val = (short)buf[bi] | (short)(buf[bi + 1] << 8);
       }
-      bi+=2;
+      bi += 2;
       fprintf(dst, "0x%x", val);
       break;
     }
-    case ULAS_A16:
-      break;
     default: {
       const char *reg = ulas_asmregstr(dat);
       if (reg) {