将 2 位模块(乘法器)变成更多位
Turn 2 bit module (Multiplier) into more bits
我有以下用于 2 位乘法器的代码:
module Multiplier (a0, a1, b0, b1, c[3:0]);
output [3:0]c;
input a0, a1, b0, b1;
wire a0b1, a1b0, ha0c, a1b1;
and (c[0], a0, b0);
and (a0b1, a0, b1);
and (a1b0, a1, b0);
HalfAdder ha0 (a1b0, a0b1, c[1], ha0c);
and (a1b1, a1, b1);
HalfAdder ha1 (ha0c, a1b1, c[2], c[3]);
endmodule
我希望能够将其扩展到 2 位以上(32 位)。不过,我的代码结构对此提出了挑战。首先,我必须为模块设置 68 个参数。此外,我还必须手动创建 64 根电线(电线 a0b1、a1b0、ha0c、a1b1 的副本)。最后我需要手动写出一堆逻辑门和 HalfAdder 模块来连接所有的逻辑。因此,我想知道是否有一种方法可以重构我的代码,以便能够实例化 n(传递的参数)大小的二进制乘法器。
您需要参数化并使用生成块。 (而且使用同步电路比使用异步电路要好得多)。
这里是一个不完整的例子,你可以补上必要的逻辑:
module Multiplier (a, b, c, clk);
parameter WIDTH = 64;
output [2*WIDTH:0]c;
input [WIDTH-1:0]a;
input [WIDTH-1:0]b;
input clk;
genvar i;
generate for (i = 0; i < WIDTH; i <= i + 1)
begin : shifts
// shift a 1-bit for each i and 'logical and' it with b
reg [WIDTH + i :0]carry;
wire [WIDTH + i -1:0]shifted = {a,i{0}} & b[i];
// sum the result of shift and 'logical and'
always @ (posedge clk)
begin
carry <= shifted + shifts[i-1].carry ;
end
end
assign c = shifts[WIDTH].carry;
endgenerate
endmodule
我有以下用于 2 位乘法器的代码:
module Multiplier (a0, a1, b0, b1, c[3:0]);
output [3:0]c;
input a0, a1, b0, b1;
wire a0b1, a1b0, ha0c, a1b1;
and (c[0], a0, b0);
and (a0b1, a0, b1);
and (a1b0, a1, b0);
HalfAdder ha0 (a1b0, a0b1, c[1], ha0c);
and (a1b1, a1, b1);
HalfAdder ha1 (ha0c, a1b1, c[2], c[3]);
endmodule
我希望能够将其扩展到 2 位以上(32 位)。不过,我的代码结构对此提出了挑战。首先,我必须为模块设置 68 个参数。此外,我还必须手动创建 64 根电线(电线 a0b1、a1b0、ha0c、a1b1 的副本)。最后我需要手动写出一堆逻辑门和 HalfAdder 模块来连接所有的逻辑。因此,我想知道是否有一种方法可以重构我的代码,以便能够实例化 n(传递的参数)大小的二进制乘法器。
您需要参数化并使用生成块。 (而且使用同步电路比使用异步电路要好得多)。
这里是一个不完整的例子,你可以补上必要的逻辑:
module Multiplier (a, b, c, clk);
parameter WIDTH = 64;
output [2*WIDTH:0]c;
input [WIDTH-1:0]a;
input [WIDTH-1:0]b;
input clk;
genvar i;
generate for (i = 0; i < WIDTH; i <= i + 1)
begin : shifts
// shift a 1-bit for each i and 'logical and' it with b
reg [WIDTH + i :0]carry;
wire [WIDTH + i -1:0]shifted = {a,i{0}} & b[i];
// sum the result of shift and 'logical and'
always @ (posedge clk)
begin
carry <= shifted + shifts[i-1].carry ;
end
end
assign c = shifts[WIDTH].carry;
endgenerate
endmodule