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