Note that the "~" operator may not be used for negation, because in Python, ~True = -2(!) |
Number of inputs: default 5, can be set with '-n <number of inputs>' |
Number of inputs: default (and maximum) 5, can be set with '-n <number of inputs>' |
The lut5.9 component is quite useful but then I'm too lazy to write out truth tables and determine the function value manually
here's a script which takes an arbitrary boolean expression, and creates the truth table and function value for lut5: upload:lut5.py
Variables are named i0..i4
The expression may contain arbitrary logical or bitwise operators, like 'i0 and i1 or i2' or 'i0 & i1 & ((i2 << i3) | i4)' Note that the "~" operator may not be used for negation, because in Python, ~True = -2(!)
Number of inputs: default (and maximum) 5, can be set with '-n <number of inputs>'
Copy & paste output into .hal file as documentation - done.
Example: 1-out-of-2 mux:
$ python lut5.py -n3 '(i2 and i1) or (not i2 and i0)'
# expression = (i2 and i1) or (not i2 and i0) #in: i4 i3 i2 i1 i0 out weight # 0: 0 0 0 0 0 0 # 1: 0 0 0 0 1 1 0x2 # 2: 0 0 0 1 0 0 # 3: 0 0 0 1 1 1 0x8 # 4: 0 0 1 0 0 0 # 5: 0 0 1 0 1 0 # 6: 0 0 1 1 0 1 0x40 # 7: 0 0 1 1 1 1 0x80 # setp lut5.N.function 0xca
Ben Jackson came up with this:
Even simpler:
i0 = 0xaaaaaaaa i1 = 0xcccccccc i2 = 0xf0f0f0f0 i3 = 0xff00ff00 i4 = 0xffff0000
and as long as you stick to bitwise operations (~ for not, |, &, ^) you can simply evaluate your expression directly:
print '0x%08x' % ((i2 & i1) | (~i2 & i0))