【发布时间】:2020-03-18 09:37:51
【问题描述】:
我对 Verilog 还很陌生,并且正在学习其中的技巧。我有一些代码生成一个 8 位向上计数器(模块 counter.v),然后由顶级模块(top_module.v)调用。有一个模拟测试夹具(test_fixture.v)调用顶层模块进行测试。
我试图使用参数 (parameter COUNTER_WIDTH) 定义计数器的宽度,但在这样做时遇到了困难。一位同事为我修复了代码,它现在确实可以工作,但我想了解一些事情,以便了解实际发生的情况。
这是计数器模块的代码:
module counter
#(parameter COUNTER_WIDTH = 8)
(
input wire CLK,
input wire RST,
input wire CE,
output reg[COUNTER_WIDTH-1:0] out = {COUNTER_WIDTH{1'b0}}
);
always @(posedge CLK) begin
if (RST == 1) begin
out <= {COUNTER_WIDTH{1'b0}};
end else begin
if (CE == 1) begin
out <= out + 1'b1;
end
end
end
endmodule
顶层模块:
module top_module
#(parameter COUNTER_WIDTH = 8)
(
input wire CLK,
input wire CE,
input wire RST,
output wire[COUNTER_WIDTH-1:0] out
);
counter #(
.COUNTER_WIDTH(COUNTER_WIDTH)
)
counter_inst(
.CLK(CLK),
.RST(RST),
.CE(CE),
.out(out)
);
endmodule
还有测试夹具:
module test_fixture();
parameter COUNTER_WIDTH = 8;
// inputs
reg CLK = 0;
reg RST = 0;
reg CE = 0;
// outputs
wire [COUNTER_WIDTH-1:0] Q;
// instance of top module to be tested
top_module #(
.COUNTER_WIDTH(COUNTER_WIDTH)
)
test_inst(
.CLK(CLK),
.RST(RST),
.CE(CE),
.out(Q)
);
endmodule
我认为我对计数器模块没问题,但对顶级模块/测试夹具中发生的事情有疑问:
- 看起来参数 COUNTER_WIDTH 是在每个模块中声明的(#(parameter COUNTER_WIDTH = 8)),然后“连接到”(如果这是正确的表达式)另一个模块中的参数声明(例如 #(. COUNTER_WIDTH(COUNTER_WIDTH) )
- 这种理解正确吗?如果是这样,为什么我们必须在模块中声明参数并将其连接到另一个模块中的参数?
提前感谢您的帮助!
【问题讨论】:
标签: parameter-passing verilog vivado