WIP: disas functionality
authorLukas Krickl <lukas@krickl.dev>
Sat, 24 Feb 2024 06:50:29 +0000 (07:50 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 24 Feb 2024 06:50:29 +0000 (07:50 +0100)
src/uldas.c

index 15cddab0f7781fc73ac83e52553fe08c61d10e51..d0e978a0f9f225e5bb013e9393e0ef358c7b7a39 100644 (file)
@@ -7,12 +7,54 @@
 int ulas_dasm_next(FILE *src, FILE *dst) {
   unsigned long srctell = ftell(src);
   // read n bytes (as many as in instruction)
-  char outbuf[ULAS_OUTBUFMAX];
-  memset(outbuf, 0, ULAS_OUTBUFMAX);
+  char buf[ULAS_OUTBUFMAX];
+  memset(buf, 0, ULAS_OUTBUFMAX);
+  unsigned long read = 0;
 
-  return 0;
+  // find the correct instructions
+  // needs to match every byte!
+  // first read max outbuf
+  read = fread(buf, 1, ULAS_OUTBUFMAX, src);
+  if (read == 0) {
+    return 0;
+  }
+
+  // now find the instruction that matches all
+  // read bytes
+  // -> then reset src's read buffer to the srctell + actual instruction's
+  // length if nothing matches simply output a .db for the first byte and return
+  for (int i = 0; ulas.arch.instrs[i].name; i++) {
+    const struct ulas_instr *instr = &ulas.arch.instrs[i];
+
+    // test all instruction's contents
+    for (int j = 0; instr->data[j]; j++) {
+      unsigned short dat = instr->data[j];
+      if (dat == ULAS_DATZERO) {
+        dat = 0;
+      }
+    
+      // do we even have enough data?
+      if (j >= read) {
+        break;
+      }
+
+      switch (dat) {
+      case ULAS_E8:
+      case ULAS_A8:
+        break;
+      case ULAS_E16:
+      case ULAS_A16:
+        break;
+      default:
+        break;
+      }
+    }
+  }
+
+  return 1;
 }
 
+// TODO: implement label generation
 int ulas_dasm(FILE *src, FILE *dst) {
   // pass 1: run and collect labels
   // pass 2: run and output to file