From 2008a47114dcdba7dfbb12b974e770e19bd82805 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 19 Jan 2025 10:47:06 +0100 Subject: [PATCH] ulas: mlb files now translate the relative address of sections automatically This is done by simply saving the .org address at time of invoking .section. --- TODO.md | 1 - man/ulas.5 | 3 +++ src/ulas.c | 27 +++++++++++++++++++++------ tests/t0.s | 3 ++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index ba7a90f..0d6c2cd 100644 --- 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. diff --git a/man/ulas.5 b/man/ulas.5 index 964b881..18e772a 100644 --- a/man/ulas.5 +++ b/man/ulas.5 @@ -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. diff --git a/src/ulas.c b/src/ulas.c index b38d4aa..6c39253 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -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; } diff --git a/tests/t0.s b/tests/t0.s index 814596b..1a020d2 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -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 -- 2.30.2