2019-07-08 09:40:12 -05:00
|
|
|
// From Eddie Hung
|
|
|
|
// extracted from: https://github.com/eddiehung/vtr-with-yosys/blob/vtr7-with-yosys/vtr_flow/misc/yosys_models.v#L220
|
|
|
|
// revised by Andre DeHon
|
|
|
|
// further revised by David Shah
|
|
|
|
`ifndef DSP_A_MAXWIDTH
|
2019-07-16 17:55:13 -05:00
|
|
|
$error("Macro DSP_A_MAXWIDTH must be defined");
|
2019-07-08 09:40:12 -05:00
|
|
|
`endif
|
2019-07-16 17:55:13 -05:00
|
|
|
`ifndef DSP_B_MAXWIDTH
|
|
|
|
$error("Macro DSP_B_MAXWIDTH must be defined");
|
|
|
|
`endif
|
2019-07-08 09:40:12 -05:00
|
|
|
|
|
|
|
`ifndef DSP_NAME
|
2019-07-16 17:55:13 -05:00
|
|
|
$error("Macro DSP_NAME must be defined");
|
2019-07-08 09:40:12 -05:00
|
|
|
`endif
|
|
|
|
|
|
|
|
`define MAX(a,b) (a > b ? a : b)
|
|
|
|
`define MIN(a,b) (a < b ? a : b)
|
|
|
|
|
|
|
|
module \$mul (A, B, Y);
|
|
|
|
parameter A_SIGNED = 0;
|
|
|
|
parameter B_SIGNED = 0;
|
|
|
|
parameter A_WIDTH = 1;
|
|
|
|
parameter B_WIDTH = 1;
|
|
|
|
parameter Y_WIDTH = 1;
|
|
|
|
|
|
|
|
input [A_WIDTH-1:0] A;
|
|
|
|
input [B_WIDTH-1:0] B;
|
|
|
|
output [Y_WIDTH-1:0] Y;
|
|
|
|
|
|
|
|
generate
|
2019-07-19 22:20:33 -05:00
|
|
|
if (A_SIGNED != B_SIGNED || A_WIDTH <= 1 || B_WIDTH <= 1)
|
2019-07-18 15:11:26 -05:00
|
|
|
wire _TECHMAP_FAIL_ = 1;
|
2019-07-17 14:44:52 -05:00
|
|
|
// NB: A_SIGNED == B_SIGNED == 0 from here
|
|
|
|
else if (A_WIDTH >= B_WIDTH)
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-17 14:44:52 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
|
|
|
.B_SIGNED(B_SIGNED),
|
|
|
|
.A_WIDTH(A_WIDTH),
|
|
|
|
.B_WIDTH(B_WIDTH),
|
|
|
|
.Y_WIDTH(Y_WIDTH)
|
|
|
|
) _TECHMAP_REPLACE_ (
|
|
|
|
.A(A),
|
|
|
|
.B(B),
|
|
|
|
.Y(Y)
|
|
|
|
);
|
|
|
|
else
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-17 14:44:52 -05:00
|
|
|
.A_SIGNED(B_SIGNED),
|
|
|
|
.B_SIGNED(A_SIGNED),
|
|
|
|
.A_WIDTH(B_WIDTH),
|
|
|
|
.B_WIDTH(A_WIDTH),
|
|
|
|
.Y_WIDTH(Y_WIDTH)
|
|
|
|
) _TECHMAP_REPLACE_ (
|
|
|
|
.A(B),
|
|
|
|
.B(A),
|
|
|
|
.Y(Y)
|
|
|
|
);
|
2019-07-08 09:40:12 -05:00
|
|
|
endgenerate
|
|
|
|
endmodule
|
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
module \$__mul (A, B, Y);
|
2019-07-08 09:40:12 -05:00
|
|
|
parameter A_SIGNED = 0;
|
|
|
|
parameter B_SIGNED = 0;
|
|
|
|
parameter A_WIDTH = 1;
|
|
|
|
parameter B_WIDTH = 1;
|
|
|
|
parameter Y_WIDTH = 1;
|
|
|
|
|
|
|
|
input [A_WIDTH-1:0] A;
|
|
|
|
input [B_WIDTH-1:0] B;
|
|
|
|
output [Y_WIDTH-1:0] Y;
|
|
|
|
|
|
|
|
wire [1023:0] _TECHMAP_DO_ = "proc; clean";
|
|
|
|
|
2019-07-18 12:53:18 -05:00
|
|
|
`ifdef DSP_SIGNEDONLY
|
|
|
|
localparam sign_headroom = 1;
|
|
|
|
`else
|
|
|
|
localparam sign_headroom = 0;
|
|
|
|
`endif
|
|
|
|
|
2019-07-15 13:19:54 -05:00
|
|
|
genvar i;
|
2019-07-08 09:40:12 -05:00
|
|
|
generate
|
2019-07-19 13:39:24 -05:00
|
|
|
if (A_WIDTH <= 1 || B_WIDTH <= 1)
|
|
|
|
wire _TECHMAP_FAIL_ = 1;
|
|
|
|
`ifdef DSP_MINWIDTH
|
|
|
|
else if (A_WIDTH+B_WIDTH < `DSP_MINWIDTH || Y_WIDTH < `DSP_MINWIDTH)
|
|
|
|
wire _TECHMAP_FAIL_ = 1;
|
|
|
|
`endif
|
|
|
|
else if (A_WIDTH > `DSP_A_MAXWIDTH) begin
|
2019-07-18 15:11:26 -05:00
|
|
|
localparam n = (A_WIDTH+`DSP_A_MAXWIDTH-sign_headroom-1) / (`DSP_A_MAXWIDTH-sign_headroom);
|
2019-07-18 11:20:48 -05:00
|
|
|
localparam partial_Y_WIDTH = `MIN(Y_WIDTH, B_WIDTH+`DSP_A_MAXWIDTH);
|
2019-07-22 15:01:26 -05:00
|
|
|
localparam last_Y_WIDTH = `MIN(partial_Y_WIDTH, B_WIDTH+A_WIDTH-(n-1)*(`DSP_A_MAXWIDTH-sign_headroom));
|
2019-07-18 15:11:26 -05:00
|
|
|
if (A_SIGNED && B_SIGNED) begin
|
2019-07-22 15:01:26 -05:00
|
|
|
wire signed [partial_Y_WIDTH-1:0] partial [n-2:0];
|
|
|
|
wire signed [last_Y_WIDTH-1:0] last_partial;
|
2019-07-18 15:11:26 -05:00
|
|
|
wire signed [Y_WIDTH-1:0] partial_sum [n-1:0];
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
wire [partial_Y_WIDTH-1:0] partial [n-1:0];
|
2019-07-22 15:01:26 -05:00
|
|
|
wire [last_Y_WIDTH-1:0] last_partial;
|
2019-07-18 15:11:26 -05:00
|
|
|
wire [Y_WIDTH-1:0] partial_sum [n-1:0];
|
|
|
|
end
|
2019-07-08 09:40:12 -05:00
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-19 11:16:13 -05:00
|
|
|
.A_SIGNED(sign_headroom),
|
2019-07-18 15:11:26 -05:00
|
|
|
.B_SIGNED(B_SIGNED),
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_WIDTH(`DSP_A_MAXWIDTH),
|
|
|
|
.B_WIDTH(B_WIDTH),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y_WIDTH(partial_Y_WIDTH)
|
2019-07-08 09:40:12 -05:00
|
|
|
) mul_slice_first (
|
2019-07-18 15:11:26 -05:00
|
|
|
.A({{sign_headroom{1'b0}}, A[`DSP_A_MAXWIDTH-sign_headroom-1 : 0]}),
|
2019-07-08 09:40:12 -05:00
|
|
|
.B(B),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y(partial[0])
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
2019-07-18 11:20:48 -05:00
|
|
|
assign partial_sum[0] = partial[0];
|
2019-07-08 09:40:12 -05:00
|
|
|
|
|
|
|
for (i = 1; i < n-1; i=i+1) begin:slice
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-19 11:16:13 -05:00
|
|
|
.A_SIGNED(sign_headroom),
|
2019-07-18 15:11:26 -05:00
|
|
|
.B_SIGNED(B_SIGNED),
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_WIDTH(`DSP_A_MAXWIDTH),
|
|
|
|
.B_WIDTH(B_WIDTH),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y_WIDTH(partial_Y_WIDTH)
|
2019-07-08 09:40:12 -05:00
|
|
|
) mul_slice (
|
2019-07-18 11:20:48 -05:00
|
|
|
.A({{sign_headroom{1'b0}}, A[i*(`DSP_A_MAXWIDTH-sign_headroom) +: `DSP_A_MAXWIDTH-sign_headroom]}),
|
2019-07-08 09:40:12 -05:00
|
|
|
.B(B),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y(partial[i])
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
2019-07-18 11:20:48 -05:00
|
|
|
assign partial_sum[i] = (partial[i] << i*(`DSP_A_MAXWIDTH-sign_headroom)) + partial_sum[i-1];
|
2019-07-08 09:40:12 -05:00
|
|
|
end
|
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
|
|
|
.B_SIGNED(B_SIGNED),
|
2019-07-18 05:33:37 -05:00
|
|
|
.A_WIDTH(A_WIDTH-(n-1)*(`DSP_A_MAXWIDTH-sign_headroom)),
|
2019-07-08 09:40:12 -05:00
|
|
|
.B_WIDTH(B_WIDTH),
|
2019-07-22 15:01:26 -05:00
|
|
|
.Y_WIDTH(last_Y_WIDTH)
|
2019-07-08 09:40:12 -05:00
|
|
|
) mul_slice_last (
|
2019-07-18 15:11:26 -05:00
|
|
|
.A(A[A_WIDTH-1 : (n-1)*(`DSP_A_MAXWIDTH-sign_headroom)]),
|
2019-07-08 09:40:12 -05:00
|
|
|
.B(B),
|
2019-07-22 15:01:26 -05:00
|
|
|
.Y(last_partial)
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
2019-07-22 15:01:26 -05:00
|
|
|
assign partial_sum[n-1] = (last_partial << (n-1)*(`DSP_A_MAXWIDTH-sign_headroom)) + partial_sum[n-2];
|
2019-07-18 15:11:26 -05:00
|
|
|
assign Y = partial_sum[n-1];
|
2019-07-08 09:40:12 -05:00
|
|
|
end
|
|
|
|
else if (B_WIDTH > `DSP_B_MAXWIDTH) begin
|
2019-07-18 15:11:26 -05:00
|
|
|
localparam n = (B_WIDTH+`DSP_B_MAXWIDTH-sign_headroom-1) / (`DSP_B_MAXWIDTH-sign_headroom);
|
2019-07-18 11:20:48 -05:00
|
|
|
localparam partial_Y_WIDTH = `MIN(Y_WIDTH, A_WIDTH+`DSP_B_MAXWIDTH);
|
2019-07-22 15:01:26 -05:00
|
|
|
localparam last_Y_WIDTH = `MIN(partial_Y_WIDTH, A_WIDTH+B_WIDTH-(n-1)*(`DSP_B_MAXWIDTH-sign_headroom));
|
2019-07-18 15:11:26 -05:00
|
|
|
if (A_SIGNED && B_SIGNED) begin
|
2019-07-22 15:01:26 -05:00
|
|
|
wire signed [partial_Y_WIDTH-1:0] partial [n-2:0];
|
|
|
|
wire signed [last_Y_WIDTH-1:0] last_partial;
|
2019-07-18 15:11:26 -05:00
|
|
|
wire signed [Y_WIDTH-1:0] partial_sum [n-1:0];
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
wire [partial_Y_WIDTH-1:0] partial [n-1:0];
|
2019-07-22 15:01:26 -05:00
|
|
|
wire [last_Y_WIDTH-1:0] last_partial;
|
2019-07-18 15:11:26 -05:00
|
|
|
wire [Y_WIDTH-1:0] partial_sum [n-1:0];
|
|
|
|
end
|
2019-07-08 09:40:12 -05:00
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
2019-07-19 11:16:13 -05:00
|
|
|
.B_SIGNED(sign_headroom),
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_WIDTH(A_WIDTH),
|
|
|
|
.B_WIDTH(`DSP_B_MAXWIDTH),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y_WIDTH(partial_Y_WIDTH)
|
2019-07-08 09:40:12 -05:00
|
|
|
) mul_first (
|
|
|
|
.A(A),
|
2019-07-18 15:11:26 -05:00
|
|
|
.B({{sign_headroom{1'b0}}, B[`DSP_B_MAXWIDTH-sign_headroom-1 : 0]}),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y(partial[0])
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
2019-07-18 11:20:48 -05:00
|
|
|
assign partial_sum[0] = partial[0];
|
2019-07-08 09:40:12 -05:00
|
|
|
|
|
|
|
for (i = 1; i < n-1; i=i+1) begin:slice
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
2019-07-19 11:16:13 -05:00
|
|
|
.B_SIGNED(sign_headroom),
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_WIDTH(A_WIDTH),
|
|
|
|
.B_WIDTH(`DSP_B_MAXWIDTH),
|
2019-07-18 11:20:48 -05:00
|
|
|
.Y_WIDTH(partial_Y_WIDTH)
|
2019-07-08 09:40:12 -05:00
|
|
|
) mul (
|
|
|
|
.A(A),
|
2019-07-18 11:20:48 -05:00
|
|
|
.B({{sign_headroom{1'b0}}, B[i*(`DSP_B_MAXWIDTH-sign_headroom) +: `DSP_B_MAXWIDTH-sign_headroom]}),
|
|
|
|
.Y(partial[i])
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
2019-07-18 15:11:26 -05:00
|
|
|
assign partial_sum[i] = (partial[i] << i*(`DSP_B_MAXWIDTH-sign_headroom)) + partial_sum[i-1];
|
2019-07-08 09:40:12 -05:00
|
|
|
end
|
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
\$__mul #(
|
2019-07-08 09:40:12 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
|
|
|
.B_SIGNED(B_SIGNED),
|
|
|
|
.A_WIDTH(A_WIDTH),
|
2019-07-18 15:11:26 -05:00
|
|
|
.B_WIDTH(B_WIDTH-(n-1)*(`DSP_B_MAXWIDTH-sign_headroom)),
|
2019-07-23 17:05:20 -05:00
|
|
|
.Y_WIDTH(last_Y_WIDTH)
|
2019-07-08 09:40:12 -05:00
|
|
|
) mul_last (
|
|
|
|
.A(A),
|
2019-07-18 15:11:26 -05:00
|
|
|
.B(B[B_WIDTH-1 : (n-1)*(`DSP_B_MAXWIDTH-sign_headroom)]),
|
2019-07-22 15:01:26 -05:00
|
|
|
.Y(last_partial)
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
2019-07-22 15:01:26 -05:00
|
|
|
assign partial_sum[n-1] = (last_partial << (n-1)*(`DSP_B_MAXWIDTH-sign_headroom)) + partial_sum[n-2];
|
2019-07-18 17:21:23 -05:00
|
|
|
assign Y = partial_sum[n-1];
|
2019-07-08 09:40:12 -05:00
|
|
|
end
|
|
|
|
else begin
|
2019-07-18 17:37:35 -05:00
|
|
|
`DSP_NAME #(
|
|
|
|
.A_SIGNED(A_SIGNED),
|
|
|
|
.B_SIGNED(B_SIGNED),
|
2019-07-19 17:50:13 -05:00
|
|
|
.A_WIDTH(A_WIDTH),
|
|
|
|
.B_WIDTH(B_WIDTH),
|
|
|
|
.Y_WIDTH(`MIN(Y_WIDTH,A_WIDTH+B_WIDTH)),
|
2019-07-18 17:37:35 -05:00
|
|
|
) _TECHMAP_REPLACE_ (
|
2019-07-19 17:50:13 -05:00
|
|
|
.A(A),
|
|
|
|
.B(B),
|
2019-07-18 12:53:18 -05:00
|
|
|
.Y(Y)
|
2019-07-08 09:40:12 -05:00
|
|
|
);
|
|
|
|
end
|
|
|
|
endgenerate
|
|
|
|
endmodule
|
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
(* techmap_celltype = "$__mul" *)
|
2019-07-19 22:20:33 -05:00
|
|
|
module $__soft_mul (A, B, Y);
|
2019-07-19 13:39:24 -05:00
|
|
|
parameter A_SIGNED = 0;
|
|
|
|
parameter B_SIGNED = 0;
|
|
|
|
parameter A_WIDTH = 1;
|
|
|
|
parameter B_WIDTH = 1;
|
|
|
|
parameter Y_WIDTH = 1;
|
2019-07-08 09:40:12 -05:00
|
|
|
|
2019-07-19 13:39:24 -05:00
|
|
|
input [A_WIDTH-1:0] A;
|
|
|
|
input [B_WIDTH-1:0] B;
|
|
|
|
output [Y_WIDTH-1:0] Y;
|
|
|
|
|
2019-07-19 22:20:33 -05:00
|
|
|
// Indirection necessary since mapping
|
|
|
|
// back to $mul will cause recursion
|
2019-07-19 13:39:24 -05:00
|
|
|
generate
|
|
|
|
if (A_SIGNED && !B_SIGNED)
|
2019-07-19 22:20:33 -05:00
|
|
|
\$__soft__mul #(
|
2019-07-19 13:39:24 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
|
|
|
.B_SIGNED(1),
|
|
|
|
.A_WIDTH(A_WIDTH),
|
|
|
|
.B_WIDTH(B_WIDTH+1),
|
|
|
|
.Y_WIDTH(Y_WIDTH)
|
|
|
|
) _TECHMAP_REPLACE_ (
|
|
|
|
.A(A),
|
2019-07-19 22:20:33 -05:00
|
|
|
.B({1'b0,B}),
|
2019-07-19 13:39:24 -05:00
|
|
|
.Y(Y)
|
|
|
|
);
|
|
|
|
else if (!A_SIGNED && B_SIGNED)
|
2019-07-19 22:20:33 -05:00
|
|
|
\$__soft_mul #(
|
2019-07-19 13:39:24 -05:00
|
|
|
.A_SIGNED(1),
|
|
|
|
.B_SIGNED(B_SIGNED),
|
|
|
|
.A_WIDTH(A_WIDTH+1),
|
|
|
|
.B_WIDTH(B_WIDTH),
|
|
|
|
.Y_WIDTH(Y_WIDTH)
|
|
|
|
) _TECHMAP_REPLACE_ (
|
2019-07-19 22:20:33 -05:00
|
|
|
.A({1'b0,A}),
|
2019-07-19 13:39:24 -05:00
|
|
|
.B(B),
|
|
|
|
.Y(Y)
|
|
|
|
);
|
|
|
|
else
|
2019-07-19 22:20:33 -05:00
|
|
|
\$__soft_mul #(
|
2019-07-19 13:39:24 -05:00
|
|
|
.A_SIGNED(A_SIGNED),
|
|
|
|
.B_SIGNED(B_SIGNED),
|
|
|
|
.A_WIDTH(A_WIDTH),
|
|
|
|
.B_WIDTH(B_WIDTH),
|
|
|
|
.Y_WIDTH(Y_WIDTH)
|
|
|
|
) _TECHMAP_REPLACE_ (
|
|
|
|
.A(A),
|
|
|
|
.B(B),
|
|
|
|
.Y(Y)
|
|
|
|
);
|
|
|
|
endgenerate
|
|
|
|
endmodule
|