+/EE460N

computer architecture

git clone https://git.julia.blue/+/EE460N

Joseph RyanAdd tests6adc40e

master
2.0 KiB66 linesraw
1#!/usr/bin/python3
2import os
3from subprocess import run, DEVNULL
4import sys
5
6fields = [
7    "count", "n", "z", "p", "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7"
8]
9
10
11def parse_dump(dump):
12    lines = list(filter(None, dump.split('\n')))[2:]
13    state = {}
14    state["count"] = lines.pop(0).split()[-1]
15    state["pc"] = lines.pop(0).split()[-1]
16    cond = lines.pop(0).split()
17    state["n"], state["z"], state["p"] = cond[3], cond[6], cond[9]
18    lines.pop(0)
19    for i in range(8):
20        state["r" + str(i)] = lines.pop(0).split()[-1]
21    state["mem"] = {}
22    while lines:
23        lines.pop(0)
24        lines.pop(0)
25        content = lines.pop(0).lower().split()
26        state["mem"][content[0]] = content[-1]
27    return state
28
29
30def run_test(filename):
31    expected = []
32    print("Testing {}...".format(filename))
33    with open(filename) as f:
34        end = False
35        for line in f:
36            line = line.strip().lower()
37            if end:
38                left, right = line.split()
39                expected.append((left, right))
40            elif line == "assert":
41                end = True
42    outfile = filename.split('.')[0] + ".out"
43    run(["./../../Lab1/assembler", filename, outfile], check=True)
44    run(["./sim", outfile],
45        input=("go\nrdump\n" + ''.join("mdump {} {}\n".format(i[0], i[0])
46                                       for i in expected if i[0] not in fields)
47               + "quit\n").encode(),
48        stdout=DEVNULL,
49        check=True)
50    with open("dumpsim") as f:
51        results = parse_dump(f.read())
52    for i in expected:
53        actual = results[i[0]] if i[0] in fields else results["mem"][i[0]]
54        if int(i[1], 0) & 0xFFFF != int(actual, 0):
55            print("\033[91mAssertion {} = {} failed; Got {}\033[0m".format(
56                i[0], i[1], actual))
57
58
59run("clang -std=c99 -pedantic -Wall ../lc3bsim2.c -o sim".split(), check=True)
60if len(sys.argv) > 1:
61    for i in sys.argv[1:]:
62        run_test(i)
63else:
64    for filename in os.listdir():
65        if filename.endswith(".asm"):
66            run_test(filename)