Arbitrary logic functions

The $lut cell type implements a single-output LUT (lookup table). It implements an arbitrary logic function with its \LUT parameter to map input port \A to values of \Y output port values. In psuedocode: Y = \LUT[A]. \A has width set by parameter \WIDTH and \Y has a width of 1. Every logic function with a single bit output has a unique $lut representation.

The $sop cell type implements a sum-of-products expression, also known as disjunctive normal form (DNF). It implements an arbitrary logic function. Its structure mimics a programmable logic array (PLA). Output port \Y is the sum of products of the bits of the input port \A as defined by parameter \TABLE. \A is \WIDTH bits wide. The number of products in the sum is set by parameter \DEPTH, and each product has two bits for each input bit - for the presence of the unnegated and negated version of said input bit in the product. Therefore the \TABLE parameter holds 2 * \WIDTH * \DEPTH bits.

For example:

Let \WIDTH be 3. We would like to represent \Y =~\A[0] + \A[1]~\A[2]. There are 2 products to be summed, so \DEPTH shall be 2.

~A[2]-----+
 A[2]----+|
~A[1]---+||
 A[1]--+|||
~A[0]-+||||
 A[0]+|||||
     |||||| product formula
     010000 ~\A[0]
     001001 \A[1]~\A[2]

So the value of \TABLE will become 010000001001.

Any logic function with a single bit output can be represented with $sop but may have variously minimized or ordered summands represented in the \TABLE values.

yosys> help $lut
Properties:

is_evaluable

Simulation model (verilog)
Listing 220 simlib.v
1763module \$lut (A, Y);
1764
1765    parameter WIDTH = 0;
1766    parameter LUT = 0;
1767
1768    input [WIDTH-1:0] A;
1769    output Y;
1770
1771    \$bmux #(.WIDTH(1), .S_WIDTH(WIDTH)) mux(.A(LUT[(1<<WIDTH)-1:0]), .S(A), .Y(Y));
1772
1773endmodule
yosys> help $sop
Properties:

is_evaluable

Simulation model (verilog)
Listing 221 simlib.v
1779module \$sop (A, Y);
1780
1781    parameter WIDTH = 0;
1782    parameter DEPTH = 0;
1783    parameter TABLE = 0;
1784
1785    input [WIDTH-1:0] A;
1786    output reg Y;
1787
1788    integer i, j;
1789    reg match;
1790
1791    always @* begin
1792        Y = 0;
1793        for (i = 0; i < DEPTH; i=i+1) begin
1794            match = 1;
1795            for (j = 0; j < WIDTH; j=j+1) begin
1796                if (TABLE[2*WIDTH*i + 2*j + 0] && A[j]) match = 0;
1797                if (TABLE[2*WIDTH*i + 2*j + 1] && !A[j]) match = 0;
1798            end
1799            if (match) Y = 1;
1800        end
1801    end
1802
1803endmodule