Skip to content

Instantly share code, notes, and snippets.

@Forty-Bot
Created May 18, 2020 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Forty-Bot/c470f33a232d582e94ef695cfb19c671 to your computer and use it in GitHub Desktop.
Save Forty-Bot/c470f33a232d582e94ef695cfb19c671 to your computer and use it in GitHub Desktop.
module add_pipe #(
parameter WIDTH = 64,
parameter SLICE_WIDTH = 16,
) (
input clk,
input carry_in,
input [0 +: WIDTH] x, y,
output [0 +: WIDTH] z,
output carry_out,
);
/* round up */
localparam SLICES = (WIDTH + SLICE_WIDTH - 1) / SLICE_WIDTH;
localparam EXTRA_WIDTH = WIDTH - (SLICE_WIDTH * SLICES);
reg [0 +: SLICE_WIDTH] sum[0 +: SLICES - 1][0 +: SLICES - 1];
reg [0 +: EXTRA_WIDTH] extra[0 +: SLICES - 1];
genvar i, j;
always @(posedge clk) begin
for (j = 0; j < SLICES - 1; j = j + 1) begin: add
sum[0][j] <= x[j * SLICE_WIDTH +: SLICE_WIDTH] + y[j * SLICE_WIDTH +: SLICE_WIDTH] + ((j == 0) ? carry_in : 0);
end
extra[0] <= x[WIDTH - 1 -: EXTRA_WIDTH] + y[WIDTH - 1 -: EXTRA_WIDTH] + ((SLICES == 1) ? carry_in : 0);
end
/* Propagate the carry between each slice */
always @(posedge clk) begin
for (i = 1; i < SLICES - 1; i = i + 1) begin: stage
for (j = 0; j < SLICES - 1; j = j + 1) begin: slice
sum[i][j] <= sum[i - 1][j] + ((i == j) ? sum[i - 1][j - 1][SLICE_WIDTH] : 0);
end
extra[i] <= extra[i - 1];
end
end
/* Assign output */
for (j = 0; j < SLICES - 1; j = j + 1) begin: out
assign z[j * SLICE_WIDTH +: SLICE_WIDTH] = sum[SLICES - 1][j];
end
assign { carry_out, z[WIDTH - 1 -: EXTRA_WIDTH] } = extra[SLICES - 1] + sum[SLICES - 1][SLICES - 1][SLICE_WIDTH];
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment