From: Lukas Krickl Date: Sun, 10 Dec 2023 17:33:30 +0000 (+0100) Subject: Added .chksm directive that inserts rom header checksum into the rom X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=9d9291229cd532c2f23256335731fe2bda506535;p=ulas%2F.git Added .chksm directive that inserts rom header checksum into the rom --- diff --git a/include/ulas.h b/include/ulas.h index 34c6838..e220cf6 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -44,6 +44,7 @@ #define ULAS_ASMSTR_PAD ".pad" #define ULAS_ASMSTR_INCBIN ".incbin" #define ULAS_ASMSTR_DEF ".def" +#define ULAS_ASMSTR_CHKSM ".chksm" // configurable tokens #define ULAS_TOK_COMMENT ';' @@ -77,7 +78,7 @@ // this is a bit of a hack to get the int expression to evaluate anyway // because expressions only work in the final pass // Beware that this can cause unforseen writes to the file and should really -// only be uesd to evalulate an expression that needs to be evaled during +// only be uesd to evalulate an expression that needs to be evaled during // all passes and nothing else! #define ULAS_EVALEXPRS(...) \ { \ @@ -251,6 +252,8 @@ struct ulas { // internal counter // used whenever a new unique number might be needed int icntr; + + char chksm; }; extern struct ulas ulas; @@ -365,6 +368,8 @@ enum ulas_asmdir { ULAS_ASMDIR_INCBIN, // .def name = value ULAS_ASMDIR_DEF, + // inserts checksum into rom + ULAS_ASMDIR_CHKSM, }; // amount of registers diff --git a/src/ulas.c b/src/ulas.c index 2815dc6..23921f4 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -51,6 +51,7 @@ void ulas_nextpass(void) { ulas.line = 0; ulas.icntr = 0; ulas.address = 0; + ulas.chksm = 0; ulas.filename = ulas.initial_filename; } @@ -2198,6 +2199,12 @@ void ulas_asmout(FILE *dst, const char *outbuf, unsigned long n) { if (ulas.pass == ULAS_PASS_FINAL) { fwrite(outbuf, 1, n, dst); } + + if (ulas.address < 0x14C) { + for (int i =0; i < n; i++) { + ulas.chksm = ulas.chksm - outbuf[i] - 1; + } + } } int ulas_asmdirbyte(FILE *dst, const char **line, unsigned long n, int *rc) { @@ -2411,13 +2418,15 @@ 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, 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, NULL}; enum ulas_asmdir dirs[] = { - ULAS_ASMDIR_ORG, ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE, ULAS_ASMDIR_STR, - ULAS_ASMDIR_FILL, ULAS_ASMDIR_PAD, ULAS_ASMDIR_INCBIN, ULAS_ASMDIR_DEF}; + ULAS_ASMDIR_ORG, ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE, + ULAS_ASMDIR_STR, ULAS_ASMDIR_FILL, ULAS_ASMDIR_PAD, + ULAS_ASMDIR_INCBIN, ULAS_ASMDIR_DEF, ULAS_ASMDIR_CHKSM}; enum ulas_asmdir dir = ULAS_ASMDIR_NONE; @@ -2459,6 +2468,10 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { case ULAS_ASMDIR_INCBIN: other_writes += ulas_asmdirincbin(dst, &line, n, &rc); break; + case ULAS_ASMDIR_CHKSM: + ulas_asmout(dst, &ulas.chksm, 1); + other_writes += 1; + break; case ULAS_ASMDIR_PAD: // TODO: pad is the same as .fill n, $ - n case ULAS_ASMDIR_NONE: diff --git a/tests/t0.bin b/tests/t0.bin index 42b91e3..c4ede5e 100644 Binary files a/tests/t0.bin and b/tests/t0.bin differ diff --git a/tests/t0.s b/tests/t0.s index 60f3504..5e75e99 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -100,3 +100,4 @@ l2: jp j1 j1: jp j1 +.chksm