import argparse parser = argparse.ArgumentParser(description='Simple TT file generator for CGP.') parser.add_argument('expression', metavar='exp', type=str, nargs='+', help='expression for TT') parser.add_argument('--inputWidth', type=int, help='Bit width of all inputs (default 1)', default=1) parser.add_argument('--outputWidth', type=int, help='Bit width of all outputs (default 1)', default=1) parser.add_argument('--file', type=str, help='File to save TT if not set STDOUT used') ARGS_VARS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] args = parser.parse_args() expr = " ".join(args.expression).lower() exprArgs = [] for s in expr: if s in ARGS_VARS and s not in exprArgs: exprArgs.append(s) # TT computation combinations = pow(2, args.inputWidth * len(exprArgs)) print(f"Generating TT for {combinations} combinations") line = f"{args.inputWidth * len(exprArgs)} {args.outputWidth} # generated by tt.py; inputs are {exprArgs} on {args.inputWidth} bits; output are {expr}" # print header if args.file is None: print() print(line) else: with open(args.file, 'w') as the_file: the_file.write(f"{line}\n") # init table with bits bits = [] exprArgsVals = [] for inp in range(len(exprArgs)): bits.append([]) exprArgsVals.append(0) for bit in range(args.inputWidth): bits[-1].append(0) # compute combinations for i in range(combinations): line = "" overFlow = True for inp in range(len(exprArgs)): exprArgsVals[inp] = 0 for bit in range(args.inputWidth): line += f"{bits[inp][bit]} " exprArgsVals[inp] |= bits[inp][bit] << bit if overFlow: if bits[inp][bit] == 1: bits[inp][bit] = 0 overFlow = True else: bits[inp][bit] = 1 overFlow = False # build expr combinationExpr = expr for inp in range(len(exprArgs)): combinationExpr = combinationExpr.replace(exprArgs[inp], str(exprArgsVals[inp])) #compute expr output out = int(eval(combinationExpr)) line += "| " for bit in range(args.outputWidth): line += f"{out & 0b1} " out = out >> 1 if args.file is None: print(line) else: with open(args.file, 'a') as the_file: the_file.write(f"{line}\n")