Skip to content

Instantly share code, notes, and snippets.

@Forty-Bot
Created July 18, 2020 18:06
Show Gist options
  • Save Forty-Bot/9871be740791dae59f812e1d44723291 to your computer and use it in GitHub Desktop.
Save Forty-Bot/9871be740791dae59f812e1d44723291 to your computer and use it in GitHub Desktop.
`default_nettype none
/*
* Prefix-Or operation.
* b = { ..., a[2] | a[1] | a[0], a[1] | a[0], a[0] }
*/
module prefix_or #(
parameter WIDTH = 0
) (
input wire clk,
input wire [WIDTH - 1:0] a,
output wire [WIDTH - 1:0] b,
);
wire [WIDTH:0] c;
assign c[0] = 1'b0;
genvar i;
for (i = 0; i < WIDTH; i = i + 1) begin
/*
* Equivalent naive implementation is (starting at i = 1)
* assign b[i] = a[i] | b[i - 1];
* There are of course fancy ways to arrange your luts to
* reduce the tree efficiently. But why bother when we have
* carry chains? :)
*/
SB_CARRY carry (
.I0(1'b1),
.I1(a[i]),
.CI(c[i]),
.CO(c[i + 1])
);
assign b[i] = a[i] | c[i];
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment