From 5f9ab6ba065d804bf4037629947c1bc90ab3fbc5 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 23 Nov 2023 06:21:27 +0100 Subject: [PATCH] Added instructions that can contain expressions --- src/ulas.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/ulas.c b/src/ulas.c index 523d3f8..8160db6 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -1407,6 +1407,7 @@ const struct ulas_instr ULASINSTRS[] = { {"di", {0}, {0xF4, 0x00}}, {"ei", {0}, {0xFB, 0x00}}, {"ld", {ULAS_REG_B, ',', ULAS_REG_B, 0}, {0x40, 0}}, + {"ld", {ULAS_REG_B, ',', ULAS_E8, 0}, {0x06, ULAS_E8, 0}}, {NULL}}; // assembles an instruction, writes bytes into dst @@ -1439,28 +1440,35 @@ int ulas_asminstr(char *dst, unsigned long max, const char **line, // expression results in order they appear int exprres[ULAS_INSTRDATMAX]; + int expridx = 0; memset(&exprres, 0, sizeof(int) * ULAS_INSTRDATMAX); // then check for each single token... const short *tok = instrs->tokens; int i = 0; while (tok[i]) { - if (ulas_tok(&ulas.tok, line, n) == -1) { - goto skip; - } - + assert(i < ULAS_INSTRTOKMAX); const char *regstr = NULL; if ((regstr = ulas_asmregstr(tok[i]))) { + if (ulas_tok(&ulas.tok, line, n) == -1) { + goto skip; + } + if (strncmp(regstr, ulas.tok.buf, ulas.tok.maxlen) != 0) { goto skip; } } else if (tok[i] == ULAS_E8 || tok[i] == ULAS_E16) { + assert(expridx < ULAS_INSTRDATMAX); int rc = 0; - exprres[i] = ulas_intexpr(line, n, &rc); + exprres[expridx++] = ulas_intexpr(line, n, &rc); if (rc == -1) { return -1; } } else { + if (ulas_tok(&ulas.tok, line, n) == -1) { + goto skip; + } + char c[2] = {tok[i], '\0'}; if (strncmp(ulas.tok.buf, c, ulas.tok.maxlen) != 0) { goto skip; @@ -1471,10 +1479,22 @@ int ulas_asminstr(char *dst, unsigned long max, const char **line, } // we are good to go! + int datread = 0; + expridx = 0; const short *dat = instrs->data; - while (dat[written]) { - dst[written] = dat[written]; + while (dat[datread]) { + assert(datread < ULAS_INSTRDATMAX); + assert(expridx < ULAS_INSTRDATMAX); + + if (dat[datread] == ULAS_E8) { + dst[written] = (char)exprres[expridx++]; + } else if (dat[datread] == ULAS_E16) { + // TODO: write 16 bit values + } else { + dst[written] = dat[datread]; + } written++; + datread++; } // this is nop, special case it is indeed 0! -- 2.30.2