From bc3d54edbc5b6aec58686e66524c99cb49e0d64a Mon Sep 17 00:00:00 2001 From: Max Regan Date: Sat, 20 May 2017 16:55:58 -0700 Subject: [PATCH] gbasm: add test for "ld r8, r8" --- src/gbasm/tests/ld.c | 89 ++++++++++++++++++++++++++++++++++++++++++ src/gbasm/tests/ld.h | 9 +++++ src/gbasm/tests/test.c | 2 + 3 files changed, 100 insertions(+) create mode 100644 src/gbasm/tests/ld.c create mode 100644 src/gbasm/tests/ld.h diff --git a/src/gbasm/tests/ld.c b/src/gbasm/tests/ld.c new file mode 100644 index 0000000..c53d76d --- /dev/null +++ b/src/gbasm/tests/ld.c @@ -0,0 +1,89 @@ +#include "gbasm/tests/test.h" +#include "common/common.h" + +#include + +void init_r8(void); + +static char ld_r8_src[1024] = { 0 }; + +static uint8_t ld_r8_output[0x40]; + +struct gbasm_test r8 = { + .init = init_r8, + .name = "r8", + .asm_source = ld_r8_src, + .expected_output = ld_r8_output, + .expected_output_len = 0, +}; + +static const struct gbasm_test *tests[] = { + &r8, +}; + +struct gbasm_tests gbasm_ld_tests = { + .name = "ld", + .num_tests = ARRAY_SIZE(tests), + .tests = tests, +}; + +const char *operands[] = { + "B", + "C", + "D", + "E", + "H", + "L", + "(HL)", + "A", +}; + +void gen_r8_src(void) +{ + const char *src, *dst; + size_t len = 0; + + for (int i = 0; i < ARRAY_SIZE(operands); i++) { + src = operands[i]; + for (int j = 0; j < ARRAY_SIZE(operands); j++) { + dst = operands[j]; + + /* Special case, HALT instruction goes where + * "ld (HL), (HL)" would be + */ + if (strcmp(src, "(HL)") == 0 && strcmp(dst, "(HL)") == 0) { + continue; + } + + len += sprintf(ld_r8_src + len, "ld %s %s\n", src, dst); + } + } + + sprintf(ld_r8_src + len, "halt"); + + DEBUG_LOG("%s", ld_r8_src); +} + +void gen_r8_output(void) +{ + int num_insts = ARRAY_SIZE(operands) * ARRAY_SIZE(operands); + int halt_code = 0x76; + int i = 0; + + for (int opcode = 0x40; opcode < 0x80; opcode++) { + if (opcode == halt_code) { + continue; + } + + ld_r8_output[i++] = opcode; + } + + ld_r8_output[i] = halt_code; + r8.expected_output_len = num_insts; +} + +void init_r8(void) +{ + gen_r8_src(); + gen_r8_output(); +} diff --git a/src/gbasm/tests/ld.h b/src/gbasm/tests/ld.h new file mode 100644 index 0000000..c9aeef0 --- /dev/null +++ b/src/gbasm/tests/ld.h @@ -0,0 +1,9 @@ +#ifndef GBASM_TEST_LD_H +#define GBASM_TEST_LD_H + +#include "gbasm/tests/test.h" +#include "common/common.h" + +extern const struct gbasm_tests gbasm_ld_tests; + +#endif diff --git a/src/gbasm/tests/test.c b/src/gbasm/tests/test.c index 70adbac..cc1834f 100644 --- a/src/gbasm/tests/test.c +++ b/src/gbasm/tests/test.c @@ -3,6 +3,7 @@ #include "gbasm/tests/test.h" #include "gbasm/tests/inc.h" #include "gbasm/tests/fixed.h" +#include "gbasm/tests/ld.h" #include "common/common.h" #include /* memset */ @@ -11,6 +12,7 @@ const static struct gbasm_tests *tests[] = { &gbasm_fixed_tests, &gbasm_inc_tests, + &gbasm_ld_tests, }; static int cmp_mem(const uint8_t *test_data, const uint8_t *expected_data, size_t len)