From 9aa02bebf295ce9436451e0ce85db7717a6c9f81 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 4 Aug 2019 00:13:59 +1200 Subject: Add initial emulator implementation This emulator provides a rough way for binaries designed for this CPU to be executed in a virtual/emulated CPU for testing purposes. This patch also adds a small test setup for automated assembly, execution and checking of register postconditions for programs. --- test/Makefile | 1 + test/emul/002-nop.asm | 7 ++++ test/emul/003-small-loop.asm | 10 ++++++ test/emul/run-emul.sh | 68 +++++++++++++++++++++++++++++++++++ test/full-pipeline/005-small-loop.asm | 3 +- 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/emul/002-nop.asm create mode 100644 test/emul/003-small-loop.asm create mode 100755 test/emul/run-emul.sh (limited to 'test') diff --git a/test/Makefile b/test/Makefile index c2407c7..cb0f3ee 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,2 +1,3 @@ test: ./full-pipeline/run-full-pipeline.sh + ./emul/run-emul.sh diff --git a/test/emul/002-nop.asm b/test/emul/002-nop.asm new file mode 100644 index 0000000..31538ba --- /dev/null +++ b/test/emul/002-nop.asm @@ -0,0 +1,7 @@ +; POST $1 = 0x0 +; POST $2 = 0x0 +; POST $3 = 0x0 +; POST $4 = 0x0 +; POST $5 = 0x0 +; POST $6 = 0x0 +nop diff --git a/test/emul/003-small-loop.asm b/test/emul/003-small-loop.asm new file mode 100644 index 0000000..ae2f81f --- /dev/null +++ b/test/emul/003-small-loop.asm @@ -0,0 +1,10 @@ +; POST $1 = 0x2 +; POST $2 = 0x0 +; POST $3 = 0x28 +ldi $1, 2 +ldi $2, 20 +ldi $3, 0 +loop: + add $3, $3, $1 + subi $2, $2, 1 + bnz loop diff --git a/test/emul/run-emul.sh b/test/emul/run-emul.sh new file mode 100755 index 0000000..3b5ae69 --- /dev/null +++ b/test/emul/run-emul.sh @@ -0,0 +1,68 @@ +#!/bin/bash -e + +# +# Script for running all of the automated tests that involve the assembler +# and emulator. +# These tests are assembly files with postconditions for registers specified +# in special syntax within comments. They contain code to run, and the post- +# conditions are checked in order to determine the test result. +# + +fail() { + echo -e '[\e[1;31mFAIL\e[0m] '"$1:" "$2" +} + +pass() { + echo -e '[\e[1;32mPASS\e[0m] '"$1" +} + +clean() { + echo "Removing work dir $WORK" + rm -r "$WORK" +} + +WORK=$(mktemp -d) +pushd $(dirname "$0") >/dev/null +export ASM="$PWD/../../assembler" +export EMUL="$PWD/../../emulator" +has_failure=0 + +for asmfile in *.asm ; do + binfile="$WORK/$(sed -e 's/\.asm$/.bin/' <<< "$asmfile")" + outfile="$WORK/$(sed -e 's/\.asm$/.out/' <<< "$asmfile")" + # Assemble test code + if ! "$ASM" "$asmfile" "$binfile" ; then + fail "$asmfile" "test assembly failed" + continue + fi + + "$EMUL" "$binfile" > "$outfile" + + # Each postcondition line must hold true, and forms a separate test to + # help track down failures + (echo '; POST $0 = 0' ; + echo '; POST $H = 0xFFFF' ; + grep '^;\s\+POST\s\+' "$asmfile" ) | while read line ; do + reg=$(awk -F= '{print $1}' <<< "$line" | awk '{print $(NF)}') + val=$(awk -F= '{print $2}' <<< "$line"| awk '{print $1}') + subtest="${asmfile}:POST-${reg}" + # Scrape output of emulator for register value + actual=$(grep "$reg" "$outfile" | awk '{print $2}') + if [[ "$actual" -eq "$val" ]]; then + pass "$subtest" + else + fail "$subtest" "postcondition (expect $val, got $actual)" + has_failure=1 + fi + done + +done +popd >/dev/null + +if [[ "$failure" != "0" && "$NO_CLEAN" == "1" ]] ; then + echo "Warning: Leaving work dir $WORK in place. Please remove this yourself" +else + clean +fi + +exit "$has_failure" diff --git a/test/full-pipeline/005-small-loop.asm b/test/full-pipeline/005-small-loop.asm index 3f2dc5f..5c47e51 100644 --- a/test/full-pipeline/005-small-loop.asm +++ b/test/full-pipeline/005-small-loop.asm @@ -1,6 +1,7 @@ ldi $1, 2 -ldi $2, 100 +ldi $2, 20 ldi $3, 0 loop: add $3, $3, $1 + subi $2, $2, 1 bnz loop -- cgit v1.1