-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_luts_helper.py
90 lines (63 loc) · 2.55 KB
/
generate_luts_helper.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
import sys
import os
max_lut_in = 8
max_lut_out = 8
indent = " "
lut_input_type = "uint32_t"
lut_output_type = "uint8_t"
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} [include directory] [output source]")
exit(1)
output_h_path = os.path.join(sys.argv[1], "luts.h")
output_c_path = sys.argv[2]
with open(output_h_path, 'w') as f:
f.write(f"/** Generated by {sys.argv[0]} **/\n\n")
# struct defs
for lut_out_size in range(1, max_lut_out + 1):
f.write(f"struct out{lut_out_size}_t {{\n")
for i in range(lut_out_size):
f.write(f"{indent}{lut_output_type} o{i};\n")
f.write("};\n\n")
for lut_in_size in range(1, max_lut_in + 1):
for lut_out_size in range(1, max_lut_out + 1):
# return type
f.write(f"struct out{lut_out_size}_t ")
# fn name
f.write(f"lut_{lut_in_size}_to_{lut_out_size}(")
# parameters
params = [None] * lut_in_size
for i in range(lut_in_size):
params[i] = f"{lut_input_type} i{i}"
f.write(", ".join(["uint8_t* truth_table"] + params))
f.write(f");\n\n")
f.write("\n");
with open(output_c_path, 'w') as f:
f.write(f"/** Generated by {sys.argv[0]} **/\n\n")
f.write(f"#include <stdint.h>\n")
f.write(f"#include \"luts.h\"\n\n")
for lut_in_size in range(1, max_lut_in + 1):
for lut_out_size in range(1, max_lut_out + 1):
# return type
f.write(f"struct out{lut_out_size}_t ")
# fn name
f.write(f"lut_{lut_in_size}_to_{lut_out_size}(")
# parameters
params = [None] * lut_in_size
for i in range(lut_in_size):
params[i] = f"{lut_input_type} i{i}"
f.write(", ".join(["uint8_t* truth_table"] + params))
# function body
f.write(f") {{\n")
f.write(f"{indent}struct out{lut_out_size}_t ret;\n")
addr = " | ".join(["i0"] + [f"i{x} << {x}" for x in range(1, lut_in_size)])
f.write(f"{indent}uint32_t addr = {addr};\n")
f.write(f"{indent}addr = ((addr >> 6) * 4096) + (addr % 64);\n")
f.write(f"{indent}uint8_t out = truth_table[addr];\n")
f.write(f"{indent}ret.o0 = out & 0b1;\n")
for i in range(1, lut_out_size):
zeros = "0" * i
f.write(f"{indent}ret.o{i} = (out & 0b1{zeros}) > 0;\n")
f.write(f"{indent}return ret;\n")
f.write("}\n\n")
f.write("\n");