""" Author: Bart Meyers Date: October 2012 usage: python Test.py option options: -a : test alu -s : test simple datapath (only r-type and sw/lw) -f : test full datapath (all instructions) Needs to be in the same folder as logisim-generic-2.7.1.jar This program will convert test cases (currently only ALU tests) to logisim dat files, which can be loaded into your logisim project. If this project includes the main circuit of ALU_GroupXX.circ, running this program with the project as last argument will behave as a test run: logisim will read in a test and oracle, and produce outputs for each test. Then, this program will parse this output and verify whether the test result is the same as the oracle. """ logisim = "logisim-generic-2.7.1.jar" # logisim filename def dec2twoscompl_hex(s, width): if s.startswith("-"): return "%x" % ((2**width)+int(s)) elif len(s) == width: # in case of a binary string return "%x" % int(s, 2) else: return "%x" % int(s) def twoscompl_bin2dec(s, width): return (int(s.replace(" ", ""), 2)-2**width) if s[0] == '1' else int(s.replace(" ", ""), 2) def findallfiles(path, pattern, subfolders=True): # find all files in path whose file matches a given pattern import re, os def match(pattern, f): return re.match(pattern, f) and not f.startswith(".") goodfiles = [] if subfolders: for r,d,files in os.walk(path): for f in files: if match(pattern, f): goodfiles.append(os.path.join(r,f)) else: files = os.listdir(path) r = path for f in files: if match(pattern, f): goodfiles.append(os.path.join(r,f)) return goodfiles def Datapathtestcompiler(textfile, testfile, width, nr_of_tests, fulldatapath=True): import re, os def pattern(patterns, can_have_label=False): fullpattern = r'^\s*' # pattern must start in the beginning of a line if can_have_label: fullpattern += "(?P