在学习defparam和元件例化中,如果所写的模块中有parameter变量,我们可以在顶层模块中使用defparam将所调用模块的值进行改变,并且在顶层模块中,输入输出都是wire型的,因为在顶层模块中输入输出信号就是将各个模块连接起来,起到一个连接线的作用。
所实现的是不同小灯亮灭时间不同,周期为0.1s,1s,0.2s,代码如下:
`timescale 1ns / 1ps
module LR(
clk,
reset,
out
);
input clk;
input reset;
output out;
reg out;
reg [32:0] cnt;
parameter ENDCNT = 32'd2_499_999;
always @( posedge clk) begin
if (!reset) begin
out <= 0;
cnt <= 0;
end
else begin
if(cnt <= ENDCNT) begin
cnt <= cnt + 1;
out <= out;
end
else begin
cnt <= 0;
out <= !out;
end
end
end
endmodule
`timescale 1ns / 1ps
module LR8(
clk,
reset,
out
);
input clk;
input reset;
output[2:0] out;
wire [2:0] out;
LR LR_0(
.clk(clk),
.reset(reset),
.out(out[0])
);
LR LR_1(
.clk(clk),
.reset(reset),
.out(out[1])
);
defparam LR_1.ENDCNT = 32'd4_999_999;
LR LR_2(
.clk(clk),
.reset(reset),
.out(out[2])
);
defparam LR_2.ENDCNT = 32'd24_999_999;
endmodule
仿真图如下: