The main documentation page fot LUT5 could be clearer about what is going on. Internally the 5 inputs are interpreted as a binary number. Then that number is used to select one of 32 possible outputs from the table. (2^5 = 32) The outputs could have been individual HAL bits, but that would have been tedious, so instead the 32 possible output states are encoded into a binary number. (This is where the "Weights" in the docs come in) Rather than add up a bunch of hex numbers, it is easier to open the Ubuntu calculator in "Programmer mode" which can handle up to 64 bit binary numbers (just click the 0s and 1s below the display). Bit 31 is the bottom row of LUT5. Bit 0 is the top one.

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))

LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org