ulas: Added .section directive
authorLukas Krickl <lukas@krickl.dev>
Sun, 19 Jan 2025 06:17:23 +0000 (07:17 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 19 Jan 2025 06:17:23 +0000 (07:17 +0100)
This directive allows a user to define the memory region.
This is currently only used for mlb symbol files.

man/ulas.1
man/ulas.5
src/ulas.c
src/ulas.h
tests/t0.s

index b2ce502bee7b540109b6245923066a7ea9313251..4614691e4563d020a65e6859d4b4d63daf6f74d5 100644 (file)
@@ -1,7 +1,7 @@
 .\" Manpage for ulas.
 .\" Contact lukas@krickl.dev to correct errors or typos.
 
-.TH man 1 "29 October 2024" "0.0.1" "ulas manual"
+.TH man 1 "19 January 2025" "0.0.1" "ulas manual"
 
 .SH NAME
   ulas
@@ -77,4 +77,4 @@
   Lukas Krickl (lukas@krickl.dev)
 
 .SH COPYRIGHT
-  Copyright 2024 Lukas Krickl (lukas@krickl.dev)
+  Copyright 2025 Lukas Krickl (lukas@krickl.dev)
index 785395dc5d5c4599cb59e9d16d05306c22b5194f..ec22dfd0314f65244342092d4a250c7f81f9b26f 100644 (file)
@@ -1,7 +1,7 @@
 .\" Manpage for ulas.
 .\" Contact lukas@krickl.dev to correct errors or typos.
 
-.TH man 1 "29 October 2024" "0.0.1" "ulas language reference"
+.TH man 5 "19 January 2025" "0.0.1" "ulas language reference"
 
 .SH PREPROCESSOR 
 
@@ -128,7 +128,7 @@ The special value $ will evaluate to the current address.
   Re-defines the ascii value for one character to another value.
   Can be used for custom encodings.
 
-.SH .chr
+.SH chr
   \.chr 01233213
   Outputs a byte as a 2bpp sprite.
   Must have exactly 8 values ranging from 0-3
@@ -138,6 +138,13 @@ The special value $ will evaluate to the current address.
   Repeats the instruction n times advancing from 0 by step each iteration.
   The first parameter is a counter value that allows the instruction to access the 
   current iterator value.
+
+.SH section
+  \.section <name>
+  Defines a section name. 
+  This is currently only used for mlb symbol files, but might be added to other areas in the future.
+  If no section was defined the .section directive will use pre-defined values for mlb outputs.
+
 .SH SEE ALSO
 
   ulas(1) for a rference on how to use ulas.
@@ -146,4 +153,4 @@ The special value $ will evaluate to the current address.
   Lukas Krickl (lukas@krickl.dev)
 
 .SH COPYRIGHT
-  Copyright 2024 Lukas Krickl (lukas@krickl.dev)
+  Copyright 2025 Lukas Krickl (lukas@krickl.dev)
index 33d3192a4ec3fc3c71bdd4e3329cf258fcd4099c..2b0acbd74062f30e5254414483d3a50736aa1db8 100644 (file)
@@ -365,6 +365,11 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok,
 }
 
 int ulas_symbolout_mlbloc(FILE *dst, long addr) {
+  if (ulas.section[0] != '\0') {
+    fprintf(dst, "%s:", ulas.section);
+    return 0;
+  }
+
   if (addr == -1) {
     fprintf(dst, "Unknown:");
     return 0;
@@ -2430,6 +2435,18 @@ int ulas_asmdirrep(FILE *dst, FILE *src, const char **line, unsigned long n) {
   return rc;
 }
 
+int ulas_asmdirsection(FILE *dst, FILE *src, const char **line,
+                       unsigned long n) {
+  ulas_tok(&ulas.tok, line, n);
+  if (!ulas_isname(ulas.tok.buf, 
+        MIN(strlen(ulas.tok.buf), ulas.tok.maxlen))) {
+    ULASERR("Unexpected token '%s'\n", ulas.tok.buf);
+    return -1;
+  }
+  strncpy(ulas.section, ulas.tok.buf, ULAS_SECTIONMAX);
+  return 0;
+}
+
 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];
@@ -2466,14 +2483,23 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
   }
 
   if (ulas.tok.buf[0] == ULAS_TOK_ASMDIR_BEGIN) {
-    const char *dirstrs[] = {ULAS_ASMSTR_ORG,          ULAS_ASMSTR_SET,
-                             ULAS_ASMSTR_BYTE,         ULAS_ASMSTR_STR,
-                             ULAS_ASMSTR_FILL,         ULAS_ASMSTR_PAD,
-                             ULAS_ASMSTR_INCBIN,       ULAS_ASMSTR_DEF,
-                             ULAS_ASMSTR_CHKSM,        ULAS_ASMSTR_ADV,
-                             ULAS_ASMSTR_SET_ENUM_DEF, ULAS_ASMSTR_DEFINE_ENUM,
-                             ULAS_ASMSTR_SETCHRCODE,   ULAS_ASMSTR_CHR,
-                             ULAS_ASMSTR_REP,          NULL};
+    const char *dirstrs[] = {ULAS_ASMSTR_ORG,
+                             ULAS_ASMSTR_SET,
+                             ULAS_ASMSTR_BYTE,
+                             ULAS_ASMSTR_STR,
+                             ULAS_ASMSTR_FILL,
+                             ULAS_ASMSTR_PAD,
+                             ULAS_ASMSTR_INCBIN,
+                             ULAS_ASMSTR_DEF,
+                             ULAS_ASMSTR_CHKSM,
+                             ULAS_ASMSTR_ADV,
+                             ULAS_ASMSTR_SET_ENUM_DEF,
+                             ULAS_ASMSTR_DEFINE_ENUM,
+                             ULAS_ASMSTR_SETCHRCODE,
+                             ULAS_ASMSTR_CHR,
+                             ULAS_ASMSTR_REP,
+                             ULAS_ASMSTR_SECTION,
+                             NULL};
     enum ulas_asmdir dirs[] = {
         ULAS_ASMDIR_ORG,          ULAS_ASMDIR_SET,
         ULAS_ASMDIR_BYTE,         ULAS_ASMDIR_STR,
@@ -2482,7 +2508,7 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
         ULAS_ASMDIR_CHKSM,        ULAS_ASMDIR_ADV,
         ULAS_ASMDIR_SET_ENUM_DEF, ULAS_ASMDIR_DEFINE_ENUM,
         ULAS_ASMDIR_SETCHRCODE,   ULAS_ASMDIR_CHR,
-        ULAS_ASMDIR_REP};
+        ULAS_ASMDIR_REP,          ULAS_ASMDIR_SECTION};
 
     enum ulas_asmdir dir = ULAS_ASMDIR_NONE;
 
@@ -2546,6 +2572,9 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
     case ULAS_ASMDIR_REP:
       rc = ulas_asmdirrep(dst, src, &line, n);
       break;
+    case ULAS_ASMDIR_SECTION:
+      rc = ulas_asmdirsection(dst, src, &line, n);
+      break;
     case ULAS_ASMDIR_PAD:
       // TODO: pad is the same as .fill n, $ - n
     case ULAS_ASMDIR_NONE:
index d6c6f90d6943a9bf9bae7a113cffd13365c1de96..fc67478d2db8c51c32a98a15354bea973eb97a82 100644 (file)
@@ -38,6 +38,8 @@ extern unsigned long defslen;
 #define ULAS_OUTBUFMAX 64
 #define ULAS_MACROPARAMMAX 15
 
+#define ULAS_SECTIONMAX 32
+
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
 
@@ -75,6 +77,7 @@ extern unsigned long defslen;
 #define ULAS_ASMSTR_SETCHRCODE ".scc"
 #define ULAS_ASMSTR_CHR ".chr"
 #define ULAS_ASMSTR_REP ".rep"
+#define ULAS_ASMSTR_SECTION ".section"
 
 // configurable tokens
 #define ULAS_TOK_COMMENT ';'
@@ -315,6 +318,7 @@ struct ulas {
   // defaults to just x=x mapping
   // but cna be set with a directive
   char charcodemap[ULAS_CHARCODEMAPLEN];
+  char section[ULAS_SECTIONMAX];
 
   struct ulas_arch arch;
 };
@@ -457,6 +461,9 @@ enum ulas_asmdir {
   // .rep <n>, <step>, <line>
   // repeats a line n times
   ULAS_ASMDIR_REP,
+  // .section <name>
+  // set the current section
+  ULAS_ASMDIR_SECTION,
 };
 
 #define ULAS_INSTRTOKMAX 16
index 66b53bd2729c6e21bf7599d3b64a7d93ff45e629..814596bfb60f7160e0415f824d3914a8e3bc20b1 100644 (file)
@@ -1,3 +1,6 @@
+.section test ; with comment
+.section tests
+
 ; this is a sample assembly file 
 ; that can be read by tests.
 ; the expected result will just be a regular run of the program