使用生成和 For 循环在 verilog 中级联结构模型
Cascading of structural Model in verilog using generate and For Loop
我陷入了一个非常困难的问题。我是 Verilog 的新手。我无法使用 for 循环多次级联电路并生成语句。
我正在尝试生成如下所示的电路:
我写的实现代码如下:
module rightShift(
input [3 : 0] right_data_input,
output [3 : 0] right_data_output
);
assign right_data_output = right_data_input >> 1;
endmodule
module adder(
input [7 : 0] adder_in1,
input [7 : 0] adder_in2,
output [7 : 0] adder_out
);
assign adder_out = adder_in1 + adder_in2;
endmodule
module Mult_1(
input [3 : 0] input1,
input [7 : 0] input2,
output wire [3 : 0] output1,
output wire [7 : 0] output2
);
rightShift rs_obj(.right_data_input(input1), .right_data_output(output1));
adder add_obj(.adder_in1(output1), .adder_in2(input2), .adder_out(output2));
endmodule
module multiply(
input wire [3 : 0] in_a,
output wire [7 : 0] OUT
);
genvar i;
wire [3 : 0] x1 = 8'b0;
GENERATE
FOR(i = 0;
i < 4;
i = i + 1)
BEGIN
Mult_1 mult_obj1(
.input1(in_a), // Need Help IN the FOR LOOP
.input2(x1),
.output2(OUT)
);
END
endgenerate
endmodule
如果能帮我修正For循环来执行附属电路就太好了
好的,使用生成循环它可能看起来像下面的假模块。
假设你必须实例化一个你想要级联的单一输入和输出的模块。
module buffer(input [3:0] in, output [3:0] out);
您的顶级模块具有相似的输入和输出。为了连接多个模块实例,您需要声明连接线。我建议您为这个方案创建一个数组。
module top(input [3:0] in, output [3:0] out);
wire [3:0] connector [5]; // an array of 5 elements.
现在第一个模块实例化为 'in' 作为其输入。我的解决方案如下:
assign connector[0] = in;
genvar i;
for (i = 1; i <= 4; i++) begin: cascade
buffer buf(.in(connector[i-1]), .out(connector[i]));
end
通过上述方案,您实例化了 4 个缓冲区。等价于以下。
buffer buf(.in(connector[0]), .out(connector[1])); // [0] is the same as 'in'
buffer buf(.in(connector[1]), .out(connector[2]));
buffer buf(.in(connector[2]), .out(connector[3]));
buffer buf(.in(connector[3]), .out(connector[4])); // i need connector[5] for this. [4] will go to out
最后,您需要分配顶层模块的输出:
assign out = connector[4];
endmodule: top
这是一种可能的情况。您可以想出一个不同的并根据您的模型需求进行调整。
我陷入了一个非常困难的问题。我是 Verilog 的新手。我无法使用 for 循环多次级联电路并生成语句。 我正在尝试生成如下所示的电路:
我写的实现代码如下:
module rightShift(
input [3 : 0] right_data_input,
output [3 : 0] right_data_output
);
assign right_data_output = right_data_input >> 1;
endmodule
module adder(
input [7 : 0] adder_in1,
input [7 : 0] adder_in2,
output [7 : 0] adder_out
);
assign adder_out = adder_in1 + adder_in2;
endmodule
module Mult_1(
input [3 : 0] input1,
input [7 : 0] input2,
output wire [3 : 0] output1,
output wire [7 : 0] output2
);
rightShift rs_obj(.right_data_input(input1), .right_data_output(output1));
adder add_obj(.adder_in1(output1), .adder_in2(input2), .adder_out(output2));
endmodule
module multiply(
input wire [3 : 0] in_a,
output wire [7 : 0] OUT
);
genvar i;
wire [3 : 0] x1 = 8'b0;
GENERATE
FOR(i = 0;
i < 4;
i = i + 1)
BEGIN
Mult_1 mult_obj1(
.input1(in_a), // Need Help IN the FOR LOOP
.input2(x1),
.output2(OUT)
);
END
endgenerate
endmodule
如果能帮我修正For循环来执行附属电路就太好了
好的,使用生成循环它可能看起来像下面的假模块。 假设你必须实例化一个你想要级联的单一输入和输出的模块。
module buffer(input [3:0] in, output [3:0] out);
您的顶级模块具有相似的输入和输出。为了连接多个模块实例,您需要声明连接线。我建议您为这个方案创建一个数组。
module top(input [3:0] in, output [3:0] out);
wire [3:0] connector [5]; // an array of 5 elements.
现在第一个模块实例化为 'in' 作为其输入。我的解决方案如下:
assign connector[0] = in;
genvar i;
for (i = 1; i <= 4; i++) begin: cascade
buffer buf(.in(connector[i-1]), .out(connector[i]));
end
通过上述方案,您实例化了 4 个缓冲区。等价于以下。
buffer buf(.in(connector[0]), .out(connector[1])); // [0] is the same as 'in'
buffer buf(.in(connector[1]), .out(connector[2]));
buffer buf(.in(connector[2]), .out(connector[3]));
buffer buf(.in(connector[3]), .out(connector[4])); // i need connector[5] for this. [4] will go to out
最后,您需要分配顶层模块的输出:
assign out = connector[4];
endmodule: top
这是一种可能的情况。您可以想出一个不同的并根据您的模型需求进行调整。