diff --git a/test/Makefile b/test/Makefile index 3b7c49b..f3c5138 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,8 +1,10 @@ all: @make -C example_1 @make -C stackoverflow + @make -C custom_syscall clean: make -C example_1 clean make -C stackoverflow clean + make -C custom_syscall diff --git a/test/custom_syscall/Makefile b/test/custom_syscall/Makefile new file mode 100644 index 0000000..07ba1b0 --- /dev/null +++ b/test/custom_syscall/Makefile @@ -0,0 +1,33 @@ +C_COMPILER=gcc + +UNITY_ROOT=../unity + +CFLAGS=-std=c99 +CFLAGS += -Wall +CFLAGS += -Werror +CFLAGS += -DUVM32_MEMORY_SIZE=16384 + +SUITE_NAME=tests + +TARGET_BASE1=test1 +TARGET1 = $(TARGET_BASE1) +SRC_FILES1=$(UNITY_ROOT)/src/unity.c test/${SUITE_NAME}.c test/test_runners/${SUITE_NAME}_Runner.c ../../uvm32/uvm32.c +INC_DIRS=-I$(UNITY_ROOT)/src -I../../uvm32/ -I../../common -Irom + +.PHONY: rom + +default: $(SRC_FILES1) rom + @$(C_COMPILER) $(CFLAGS) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) rom/rom-header.c -o $(TARGET1) + @- ./$(TARGET1) + +rom: + @(cd rom && make) + +test/test_runners/${SUITE_NAME}_Runner.c: test/${SUITE_NAME}.c + @mkdir -p test/test_runners + @ruby $(UNITY_ROOT)/auto/generate_test_runner.rb test/${SUITE_NAME}.c test/test_runners/${SUITE_NAME}_Runner.c + +clean: + rm -rf $(TARGET1) test/test_runners + (cd rom && make clean) + diff --git a/test/custom_syscall/rom/Makefile b/test/custom_syscall/rom/Makefile new file mode 100644 index 0000000..4698f22 --- /dev/null +++ b/test/custom_syscall/rom/Makefile @@ -0,0 +1,13 @@ +TOPDIR=../../../ +PROJECT:=$(shell basename ${PWD}) +SRCS=${PROJECT}.c ${TOPDIR}/apps/crt0.S +all: all_common + @# Convert ROM to C file and header + @xxd -i ${PROJECT}.bin > ${PROJECT}-header.c + @echo "extern unsigned char ${PROJECT}_bin[]; extern int ${PROJECT}_bin_len;" > ${PROJECT}-header.h + +test: test_common +clean: clean_common + rm -f ${PROJECT}-header.h ${PROJECT}-header.c + +include ${TOPDIR}/apps/makefile.common diff --git a/test/custom_syscall/rom/rom.c b/test/custom_syscall/rom/rom.c new file mode 100644 index 0000000..a5dae81 --- /dev/null +++ b/test/custom_syscall/rom/rom.c @@ -0,0 +1,10 @@ +#include "uvm32_target.h" + +void main(void) { + if (0xAABBCCDD == syscall(0xDEADBEEF, 0xABCD1234, 0xDECAFBAD)) { + print("ok"); + } else { + print("fail"); + } +} + diff --git a/test/custom_syscall/test/tests.c b/test/custom_syscall/test/tests.c new file mode 100644 index 0000000..901b79c --- /dev/null +++ b/test/custom_syscall/test/tests.c @@ -0,0 +1,62 @@ +#include +#include "unity.h" +#include "uvm32.h" +#include "../common/uvm32_common_custom.h" + +#include "rom-header.h" + +static uvm32_state_t vmst; +static uvm32_evt_t evt; + +void setUp(void) { + // runs before each test + uvm32_init(&vmst); + uvm32_load(&vmst, rom_bin, rom_bin_len); +} + +void tearDown(void) { +} + +void test_custom_syscall_normal(void) { + // run the vm + uvm32_run(&vmst, &evt, 100); + // check for custom syscall + TEST_ASSERT_EQUAL(evt.typ, UVM32_EVT_SYSCALL); + TEST_ASSERT_EQUAL(evt.data.syscall.code, 0xDEADBEEF); + TEST_ASSERT_EQUAL(0xABCD1234, uvm32_getval(&vmst, &evt, ARG0)); + TEST_ASSERT_EQUAL(0xDECAFBAD, uvm32_getval(&vmst, &evt, ARG1)); + uvm32_setval(&vmst, &evt, RET, 0xAABBCCDD); + + uvm32_run(&vmst, &evt, 100); + // check for print syscall + TEST_ASSERT_EQUAL(evt.typ, UVM32_EVT_SYSCALL); + TEST_ASSERT_EQUAL(evt.data.syscall.code, UVM32_SYSCALL_PRINT); + const char *str = uvm32_getcstr(&vmst, &evt, ARG0); + TEST_ASSERT_EQUAL(0, strcmp(str, "ok")); + // run vm to completion + uvm32_run(&vmst, &evt, 100); + TEST_ASSERT_EQUAL(evt.typ, UVM32_EVT_END); +} + +void test_custom_syscall_badval(void) { + // run the vm + uvm32_run(&vmst, &evt, 100); + // check for custom syscall + TEST_ASSERT_EQUAL(evt.typ, UVM32_EVT_SYSCALL); + TEST_ASSERT_EQUAL(evt.data.syscall.code, 0xDEADBEEF); + TEST_ASSERT_EQUAL(0xABCD1234, uvm32_getval(&vmst, &evt, ARG0)); + TEST_ASSERT_EQUAL(0xDECAFBAD, uvm32_getval(&vmst, &evt, ARG1)); + uvm32_setval(&vmst, &evt, RET, 0); // send value that is not being expected + + uvm32_run(&vmst, &evt, 100); + // check for print syscall + TEST_ASSERT_EQUAL(evt.typ, UVM32_EVT_SYSCALL); + TEST_ASSERT_EQUAL(evt.data.syscall.code, UVM32_SYSCALL_PRINT); + const char *str = uvm32_getcstr(&vmst, &evt, ARG0); + TEST_ASSERT_EQUAL(0, strcmp(str, "fail")); + // run vm to completion + uvm32_run(&vmst, &evt, 100); + TEST_ASSERT_EQUAL(evt.typ, UVM32_EVT_END); +} + +