/ src / hal / components / lut5.comp
lut5.comp
  1  component lut5 """Arbitrary 5-input logic function based on a look-up table""";
  2  pin in bit in_0;
  3  pin in bit in_1;
  4  pin in bit in_2;
  5  pin in bit in_3;
  6  pin in bit in_4;
  7  pin out bit out;
  8  param rw u32 function;
  9  function _ nofp;
 10  description """
 11  .B lut5
 12  constructs a logic function with up to 5 inputs using a
 13  \\fBl\\fRook-\\fBu\\fRp \\fBt\\fRable. The value for \\fBfunction\\fR can be
 14  determined by writing the truth table, and computing the sum of \\fBall\\fR
 15  the \\fBweights\\fR for which the output value would be \\fRTRUE\\fR.
 16  The weights are hexadecimal not decimal so hexadecimal math must be used to
 17  sum the weights. A wiki page has a calculator to assist in computing the proper
 18  value for function.
 19  .PP
 20  http://wiki.linuxcnc.org/cgi-bin/wiki.pl?Lut5
 21  .PP
 22  Note that LUT5 will generate any of the 4,294,967,296
 23  logical functions of 5 inputs so \\fBAND\\fR, \\fBOR\\fR, \\fBNAND\\fR,
 24  \\fBNOR\\fR, \\fBXOR\\fR and every other combinatorial function is possible.
 25  .PP
 26  .SS Example Functions
 27  A 5-input
 28  \\fIand\\fR function is TRUE only when all the inputs are true, so the correct
 29  value for \\fBfunction\\fR is \\fB0x80000000\\fR.
 30  .PP
 31  A 2-input \\fIor\\fR function would be the sum of \\fB0x2\\fR + \\fB0x4\\fR +
 32  \\fB0x8\\fR, so the correct value for \\fBfunction\\fR is \\fB0xe\\fR.
 33  .PP
 34  A 5-input \\fIor\\fR
 35  function is TRUE whenever any of the inputs are true, so the correct value for
 36  \\fBfunction\\fR is \\fB0xfffffffe\\fR. Because every weight except \\fB0x1\\fR
 37  is true the function is the sum of every line except the first one.
 38  .PP
 39  A 2-input \\fIxor\\fR function is
 40  TRUE whenever exactly one of the inputs is true, so the correct value for
 41  \\fBfunction\\fR is \\fB0x6\\fR.  Only \\fBin-0\\fR and \\fBin-1\\fR should be
 42  connected to signals, because if any other bit is \\fBTRUE\\fR then the output
 43  will be \\fBFALSE\\fR.
 44  .PP
 45  .ie '\*[.T]'html' \\{\\
 46  .HTML \\
 47  <STYLE> \\
 48  #weight TD { text-align: center; padding-left: .5ex; padding-right: .5ex } \\
 49  #weight TH { text-align: center; padding-left: .5ex; padding-right: .5ex } \\
 50  #weight TD.W { text-align: right; } \\
 51  </STYLE> \\
 52  <TABLE ID="weight" STYLE="border: 1px solid black; border-collapse: collapse"> \\
 53      <COL SPAN=5 STYLE="margin: .2ex"><COL SPAN=1 STYLE="border-left: 1px solid black"> \\
 54  <TR STYLE="border-bottom: 1px solid black"> \\
 55      <TH COLSPAN=6>Weights for each line of truth table \\
 56  <TR STYLE="border-bottom: 1px solid black"> \\
 57      <TH>Bit 4<TH>Bit 3<TH>Bit 2<TH>Bit 1<TH>Bit 0<TH> Weight \\
 58  <TR><TD>0<TD>0<TD>0<TD>0<TD>0<TD CLASS="w">0x1 \\
 59  <TR><TD>0<TD>0<TD>0<TD>0<TD>1<TD CLASS="w">0x2 \\
 60  <TR><TD>0<TD>0<TD>0<TD>1<TD>0<TD CLASS="w">0x4 \\
 61  <TR><TD>0<TD>0<TD>0<TD>1<TD>1<TD CLASS="w">0x8 \\
 62  <TR><TD>0<TD>0<TD>1<TD>0<TD>0<TD CLASS="w">0x10 \\
 63  <TR><TD>0<TD>0<TD>1<TD>0<TD>1<TD CLASS="w">0x20 \\
 64  <TR><TD>0<TD>0<TD>1<TD>1<TD>0<TD CLASS="w">0x40 \\
 65  <TR><TD>0<TD>0<TD>1<TD>1<TD>1<TD CLASS="w">0x80 \\
 66  <TR><TD>0<TD>1<TD>0<TD>0<TD>0<TD CLASS="w">0x100 \\
 67  <TR><TD>0<TD>1<TD>0<TD>0<TD>1<TD CLASS="w">0x200 \\
 68  <TR><TD>0<TD>1<TD>0<TD>1<TD>0<TD CLASS="w">0x400 \\
 69  <TR><TD>0<TD>1<TD>0<TD>1<TD>1<TD CLASS="w">0x800 \\
 70  <TR><TD>0<TD>1<TD>1<TD>0<TD>0<TD CLASS="w">0x1000 \\
 71  <TR><TD>0<TD>1<TD>1<TD>0<TD>1<TD CLASS="w">0x2000 \\
 72  <TR><TD>0<TD>1<TD>1<TD>1<TD>0<TD CLASS="w">0x4000 \\
 73  <TR><TD>0<TD>1<TD>1<TD>1<TD>1<TD CLASS="w">0x8000 \\
 74  <TR><TD>1<TD>0<TD>0<TD>0<TD>0<TD CLASS="w">0x10000 \\
 75  <TR><TD>1<TD>0<TD>0<TD>0<TD>1<TD CLASS="w">0x20000 \\
 76  <TR><TD>1<TD>0<TD>0<TD>1<TD>0<TD CLASS="w">0x40000 \\
 77  <TR><TD>1<TD>0<TD>0<TD>1<TD>1<TD CLASS="w">0x80000 \\
 78  <TR><TD>1<TD>0<TD>1<TD>0<TD>0<TD CLASS="w">0x100000 \\
 79  <TR><TD>1<TD>0<TD>1<TD>0<TD>1<TD CLASS="w">0x200000 \\
 80  <TR><TD>1<TD>0<TD>1<TD>1<TD>0<TD CLASS="w">0x400000 \\
 81  <TR><TD>1<TD>0<TD>1<TD>1<TD>1<TD CLASS="w">0x800000 \\
 82  <TR><TD>1<TD>1<TD>0<TD>0<TD>0<TD CLASS="w">0x1000000 \\
 83  <TR><TD>1<TD>1<TD>0<TD>0<TD>1<TD CLASS="w">0x2000000 \\
 84  <TR><TD>1<TD>1<TD>0<TD>1<TD>0<TD CLASS="w">0x4000000 \\
 85  <TR><TD>1<TD>1<TD>0<TD>1<TD>1<TD CLASS="w">0x8000000 \\
 86  <TR><TD>1<TD>1<TD>1<TD>0<TD>0<TD CLASS="w">0x10000000 \\
 87  <TR><TD>1<TD>1<TD>1<TD>0<TD>1<TD CLASS="w">0x20000000 \\
 88  <TR><TD>1<TD>1<TD>1<TD>1<TD>0<TD CLASS="w">0x40000000 \\
 89  <TR><TD>1<TD>1<TD>1<TD>1<TD>1<TD CLASS="w">0x80000000 \\
 90  </TABLE>
 91  \\}
 92  .el \\{\\
 93  .TS
 94  box tab(;);
 95  cb s s s s s
 96  cb cb cb cb cb | cb
 97  c  c  c  c  c  | r.
 98  Weights for each line of truth table
 99  _
100  Bit 4;Bit 3;Bit 2;Bit 1;Bit 0; Weight
101  _
102  0;0;0;0;0;0x1
103  0;0;0;0;1;0x2
104  0;0;0;1;0;0x4
105  0;0;0;1;1;0x8
106  0;0;1;0;0;0x10
107  0;0;1;0;1;0x20
108  0;0;1;1;0;0x40
109  0;0;1;1;1;0x80
110  0;1;0;0;0;0x100
111  0;1;0;0;1;0x200
112  0;1;0;1;0;0x400
113  0;1;0;1;1;0x800
114  0;1;1;0;0;0x1000
115  0;1;1;0;1;0x2000
116  0;1;1;1;0;0x4000
117  0;1;1;1;1;0x8000
118  1;0;0;0;0;0x10000
119  1;0;0;0;1;0x20000
120  1;0;0;1;0;0x40000
121  1;0;0;1;1;0x80000
122  1;0;1;0;0;0x100000
123  1;0;1;0;1;0x200000
124  1;0;1;1;0;0x400000
125  1;0;1;1;1;0x800000
126  1;1;0;0;0;0x1000000
127  1;1;0;0;1;0x2000000
128  1;1;0;1;0;0x4000000
129  1;1;0;1;1;0x8000000
130  1;1;1;0;0;0x10000000
131  1;1;1;0;1;0x20000000
132  1;1;1;1;0;0x40000000
133  1;1;1;1;1;0x80000000
134  .TE
135  \\}
136  """;
137  license "GPL";
138  ;;
139  
140  FUNCTION(_) {
141      int shift = 0;
142      if(in_0) shift += 1;
143      if(in_1) shift += 2;
144      if(in_2) shift += 4;
145      if(in_3) shift += 8;
146      if(in_4) shift += 16;
147  
148      out = (function & (1<<shift)) != 0;
149  }