Added .fill instruction
authorLukas Krickl <lukas@krickl.dev>
Mon, 4 Dec 2023 19:45:32 +0000 (20:45 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 4 Dec 2023 19:45:32 +0000 (20:45 +0100)
src/ulas.c
tests/t0.bin
tests/t0.s

index 738ec606da6d92bd5444adf144a9ea1592bab569..f5cc43d5ef106423d63b6e695ef16d8d41e81a18 100644 (file)
@@ -2138,6 +2138,43 @@ int ulas_asmdirdef(const char **line, unsigned long n) {
   return ulas_asmdirset(line, n, t);
 }
 
+int ulas_asmdirfill(FILE *dst, const char **line, unsigned long n, int *rc) {
+  // fill <what>, <how many>
+  int written = 0;
+
+  int ival = ulas_intexpr(line, n, rc);
+  char val = (char)ival;
+  if (*rc == -1) {
+    return 0;
+  }
+
+  ulas_tok(&ulas.tok, line, n);
+  struct ulas_tok t =
+      ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
+
+  if (*rc == -1 || t.type != ',') {
+    ULASERR("Expected ,\n");
+    return 0;
+  }
+
+  int count = ulas_intexpr(line, n, rc);
+  if (count < 0) {
+    ULASERR("Count must be positive\n");
+    return 0;
+  }
+
+  if (*rc == -1) {
+    return 0;
+  }
+
+  for (int i = 0; i < count; i++) {
+    ulas_asmout(dst, &val, 1);
+    written++;
+  }
+
+  return written;
+}
+
 int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
   // this buffer is written both to dst and to verbose output
   char outbuf[ULAS_OUTBUFMAX];
@@ -2195,10 +2232,12 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
     case ULAS_ASMDIR_BYTE:
       other_writes += ulas_asmdirbyte(dst, &line, n, &rc);
       break;
-    case ULAS_ASMDIR_STR:
     case ULAS_ASMDIR_FILL:
+      other_writes += ulas_asmdirfill(dst, &line, n, &rc);
+      break;
     case ULAS_ASMDIR_PAD:
     case ULAS_ASMDIR_INCBIN:
+    case ULAS_ASMDIR_STR:
     case ULAS_ASMDIR_NONE:
       ULASPANIC("asmdir not implemented\n");
       break;
index 9afc30490e7a3f48857b954fa7cc084c14431fe0..44cd58b8f7d60f8e8a46237e6ed3dd3521264b29 100644 (file)
Binary files a/tests/t0.bin and b/tests/t0.bin differ
index 00713f36c86c9e969c595d7a202a8642c90388a5..3d195af5efc7084e697988a723bc3df3f34eae21 100644 (file)
@@ -87,3 +87,4 @@ l2:
   ld a, s1
 
 .db 1, 2, 3
+.fill 1, 3