ulas: mlb files now translate the relative address of sections origin/mlb-sections
authorLukas Krickl <lukas@krickl.dev>
Sun, 19 Jan 2025 09:47:06 +0000 (10:47 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 19 Jan 2025 09:47:06 +0000 (10:47 +0100)
automatically

This is done by simply saving the .org address at time of invoking
.section.

TODO.md
man/ulas.5
src/ulas.c
tests/t0.s

diff --git a/TODO.md b/TODO.md
index ba7a90fe5cf205e7e3d542d56738a5bb78a23785..0d6c2cd5b119b3b93f0f3406f0f3c35f69f6eae2 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -6,4 +6,3 @@
 - Add `.error` and `.warning` directives 
 - Add failing tests for most failure cases
 - Assert `.section` in full test
-- Make `.section` work in mlb files. Maybe use the last .org location to get the relative location of addresses.
index 964b8810efa11ba7c72bc5ea0731288320242600..18e772a201df5eb5c1ec749d8572955617c3473f 100644 (file)
@@ -145,6 +145,9 @@ The special value $ will evaluate to the current address.
   For mlb files .section will use the address (set via .org) present at the time of 
   invoking .section as an offset address.
 
+  For mlb files ulas offers a few pre-defined section names that get translated to their 
+  respective mlb names (wram == GbWorkRam; prgrom == GbPrgRom).
+
 .SH SEE ALSO
 
   ulas(1) for a rference on how to use ulas.
index b38d4aa66a8eb2d6c5783d76fdca99b5c38257dc..6c3925396edf24198c65a0d6af22e18b207a2809 100644 (file)
@@ -366,9 +366,25 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok,
   return rc;
 }
 
+/**
+ * pre-defined mlb mappings
+ */
+#define ULAS_MLB_SECTION_PRGROM "prgrom"
+#define ULAS_MLB_SECTION_WORKRAM "wram"
+
 int ulas_symbolout_mlbloc(FILE *dst, long addr) {
   if (ulas.section[0] != '\0') {
-    fprintf(dst, "%s:", ulas.section);
+    const char *section = ulas.section;
+
+    // statically map some ulas section names to mlb sections
+    if (strncmp(ULAS_MLB_SECTION_PRGROM, ulas.section, ULAS_SECTIONMAX) == 0) {
+      section = "GbPrgRom";
+    } else if (strncmp(ULAS_MLB_SECTION_WORKRAM, ulas.section,
+                       ULAS_SECTIONMAX) == 0) {
+      section = "GbWorkRam";
+    }
+
+    fprintf(dst, "%s:", section);
     return (int)ulas.section_address;
   }
 
@@ -377,9 +393,9 @@ int ulas_symbolout_mlbloc(FILE *dst, long addr) {
     return 0;
   }
 
-  // TODO: maybe allow the user to define this by using
-  // .section and just trust the label location in the source
-  // is correct
+  // fallback
+  // if no previous section was declared we fall back to the 
+  // old default output 
   switch (ulas.arch.type) {
   case ULAS_ARCH_SM83:
     if (addr >= 0x0000 && addr <= 0x7FFF) {
@@ -2440,8 +2456,7 @@ int ulas_asmdirrep(FILE *dst, FILE *src, const char **line, unsigned long n) {
 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))) {
+  if (!ulas_isname(ulas.tok.buf, MIN(strlen(ulas.tok.buf), ulas.tok.maxlen))) {
     ULASERR("Unexpected token '%s'\n", ulas.tok.buf);
     return -1;
   }
index 814596bfb60f7160e0415f824d3914a8e3bc20b1..1a020d214f2b4a491072173d045f1ca1142be6bb 100644 (file)
@@ -1,5 +1,4 @@
 .section test ; with comment
-.section tests
 
 ; this is a sample assembly file 
 ; that can be read by tests.
@@ -8,6 +7,8 @@
 ; generate expected result with:
 ; make buildtests
 .org 0x100 ; comment
+.section tests
+
   nop
   ; full line is a comments
   halt ; comment