summaryrefslogtreecommitdiff
path: root/test/emul/run-emul.sh
blob: 496f13d19f03539f9a14e424f2b743aebff3360c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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}:${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"