From: Lukas Krickl Date: Sun, 26 Nov 2023 15:14:38 +0000 (+0100) Subject: Added asm instruction tests X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=1e11162f5f516c5eb5d56bb1b82dd1810400ee58;p=ulas%2F.git Added asm instruction tests --- diff --git a/include/ulas.h b/include/ulas.h index 8472fdd..2a7d5a2 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -469,6 +469,11 @@ void ulas_exprbuffree(struct ulas_exprbuf *eb); * Assembly step */ +// assembles an instruction, writes bytes into dst +// returns bytes written or -1 on error +int ulas_asminstr(char *dst, unsigned long max, const char **line, + unsigned long n); + // returns 0 if no more data can be read // > 0 if data was read // -1 on error diff --git a/src/test.c b/src/test.c index 6264afa..1fbdfc6 100644 --- a/src/test.c +++ b/src/test.c @@ -277,9 +277,27 @@ void test_intexpr(void) { TESTEND("intexpr"); } +#define ASSERT_ASMINSTR(expect_len, line, ...) \ + { \ + const char *l = line; \ + int n = strlen(l); \ + char dst[64]; \ + unsigned char expect_dst[] = {__VA_ARGS__}; \ + int res = ulas_asminstr(dst, 64, &l, n); \ + assert(res == expect_len); \ + for (int i = 0; i < res; i++) { \ + assert(expect_dst[i] == (unsigned char)dst[i]); \ + } \ + } + void test_asminstr(void) { TESTBEGIN("asminstr"); + ASSERT_ASMINSTR(1, "nop", 0x00); + ASSERT_ASMINSTR(1, "halt", 0x76); + ASSERT_ASMINSTR(1, "ld b, c", 0x41); + ASSERT_ASMINSTR(2, "ldh a, [1 + 2]", 0xF0, 0x03); + TESTEND("asminstr"); }