[英]Verilog HDL behavioral coding calling modules for ALU
这是我第一次在verilog hdl中进行编程,因此我很难弄清我的代码有什么问题。 我需要在行为代码中设计一个简单的ALU。
到目前为止,我已经创建了一个减法器和加法器模块。(我需要添加更多模块,但是在添加其他模块之前,我想让它们在ALU模块中正常工作)。
我在同一项目的单独.v文件中具有以下模块(非常确定这是行为的吗?):
module adder3bit( sum, co, a, b);
parameter n = 2;
output reg [n:0] sum;
output reg co;
input [n:0] a;
input [n:0] b;
always @(a, b)
{co, sum} = a + b;
endmodule
module subtract3bit(diff, bo, a, b);
parameter n = 2;
output reg [n:0] diff;
output reg bo;
input [n:0] a;
input [n:0] b;
always @(a, b)
{bo, diff} = a - b;
endmodule
我测试了这些,发现它们正在工作。
现在,我试图在主ALU模块中调用它们:
module alu( out, overflow, a, b,sel);
input [2:0] a, b;
input sel;
output [2:0] out;
output overflow;
always @(a,b,sel)
begin
if(sel=='b0)
adder3bit A1(out,overflow,a,b);
if(sel=='b1)
subtract3bit S1( out, overflow, a, b);
end
endmodule
我的语法可能是错误的,但是显示错误。 我只是不很熟悉verilog。 我觉得我对学习C的初衷。非常感谢您的帮助。
我知道它正确地调用了模块,但是我认为这与if语句有关。
谢谢,我希望学习新的东西!
主要问题是您要调用模块。 模块不是您不能调用的功能或任务。 您可以并且应该对模块执行的操作是在另一个模块(在本例中为ALU模块)中实例化它。 无法在过程块内部实例化模块(总是在您的代码中)。 加法器和减法器都将在其输入的每次更改时产生新的结果,因此您只需要适当地驱动该模块的输入并读取其输出即可。
我建议您以更易读的方式声明模块的端口:
module adder3bit #(
parameter N = 2
) (
output reg [N:0] sum,
output reg co,
input [N:0] a,
input [N:0] b
);
always @(a, b)
{co, sum} = a + b;
endmodule
在ALU中,您可以像这样实例化adder3bit:
module alu (
input [2:0] a,
input [2:0] b,
input sel,
output [2:0] out,
output overflow
)
localparam SIZE = 3;
wire [SIZE - 1 : 0] diff;
wire [SIZE - 1 : 0] sum;
wire co;
wire bo;
adder3bit #(
.N(SIZE)
) adder (
.a(a),
.b(b),
.sum(sum),
.co(co)
);
subtract3bit #(
.N(SIZE)
) subtractor (
.a(a),
.b(b),
.diff(diff),
.bo(bo)
);
always @(*)
begin
if(sel=='b0)
{out,overflow) = {sum, co};
if(sel=='b1)
{out,overflow) = {diff, bo};
end
endmodule
还有一件事,您的模块具有定义其输入和输出大小的参数,但其名称表明该模块固定为三个,这可能会造成混淆。
您无法在verilog中的always块中实例化模块。 相反,您可以将模块adder3bit和substract3bit更改为任务,并且可以使用现在编写的代码。 解决方案将是这样
task adder3bit;
parameter n = 2;
input [n:0] a;
input [n:0] b;
output reg [n:0] sum;
output reg co;
begin
always @(*) {co, sum} = a + b;
endtask
task subtract3bit;
parameter n = 2;
input [n:0] a;
input [n:0] b;
output reg [n:0] diff;
output reg bo;
begin
always @(*) {bo, diff} = a - b;
endtask
module alu( out, overflow, a, b,sel);
input [2:0] a, b;
input sel;
output [2:0] out;
output overflow;
always @(a,b,sel)
begin
if(sel=='b0)
adder3bit (a,b,out,overflow);
if(sel=='b1)
subtract3bit ( a,b,out,overflow);
end
endmodule
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.