From da803619df81542c91f118569b3d9f572970b8dd Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sat, 9 Dec 2023 22:34:11 +0100 Subject: [PATCH] Added .incbin directive --- src/ulas.c | 31 ++++++++++++++++++++++++++++--- tests/inc.bin | 1 + tests/t0.bin | Bin 139 -> 143 bytes tests/t0.s | 2 ++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/inc.bin diff --git a/src/ulas.c b/src/ulas.c index f07f012..43c253c 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -66,8 +66,8 @@ FILE *ulas_incpathfopen(const char *path, const char *mode) { char pathbuf[ULAS_PATHMAX]; memset(pathbuf, 0, ULAS_PATHMAX); int baselen = strlen(path); - - // check all include paths + + // check all include paths for (int i = 0; i < ulas.include_paths_len; i++) { pathbuf[0] = '\0'; char *ip = ulas.include_paths[i]; @@ -77,7 +77,7 @@ FILE *ulas_incpathfopen(const char *path, const char *mode) { } strcat(pathbuf, ip); - if (ip[len-1] != ULAS_PATHSEP[0]) { + if (ip[len - 1] != ULAS_PATHSEP[0]) { strcat(pathbuf, ULAS_PATHSEP); } strcat(pathbuf, path); @@ -2280,6 +2280,29 @@ int ulas_asmdirstr(FILE *dst, const char **line, unsigned long n, int *rc) { return written; } +int ulas_asmdirincbin(FILE *dst, const char **line, unsigned long n, int *rc) { + char *path = ulas_strexpr(line, n, rc); + char buf[256]; + memset(buf, 0, 256); + int written = 0; + + FILE *f = ulas_incpathfopen(path, "re"); + if (!f) { + *rc = -1; + return 0; + } + + int read = 0; + while ((read = fread(buf, 1, 256, f))) { + ulas_asmout(dst, buf, read); + written += read; + } + + fclose(f); + + 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]; @@ -2344,6 +2367,8 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { other_writes += ulas_asmdirstr(dst, &line, n, &rc); break; case ULAS_ASMDIR_INCBIN: + other_writes += ulas_asmdirincbin(dst, &line, n, &rc); + break; case ULAS_ASMDIR_PAD: // TODO: pad is the same as .fill n, $ - n case ULAS_ASMDIR_NONE: diff --git a/tests/inc.bin b/tests/inc.bin new file mode 100644 index 0000000..698181d --- /dev/null +++ b/tests/inc.bin @@ -0,0 +1 @@ +ª»ÌÝ \ No newline at end of file diff --git a/tests/t0.bin b/tests/t0.bin index 5f3faedb0e43472151f75f8091ef024132d022c2..384eb120297436f96d5a63451160bc7374e7dfe3 100644 GIT binary patch delta 11 ScmeBX>}Q}H(M4FCr}0yqEw diff --git a/tests/t0.s b/tests/t0.s index f3999dc..36daff4 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -93,3 +93,5 @@ l2: .def str s2 = "Hello" .str "test1", "test2" + +.incbin "tests/inc.bin" -- 2.30.2