(1)=====創建文件
(2)=====編寫testbench文件=======
主文件
`timescale 1ns / 1ps
//=====(2)計數器,循環移位,串並轉化=======
module Counter (Clk,En,Rst,Cnt_result //功能一端口(計數器)
); //功能二端口(循環移位,串並轉換)
//端口定義
input Clk;
input En,Rst;
output reg [2:0] Cnt_result;
//功能實現
//reg [2:0] Cnt_value;
initial begin
Cnt_result = 3'b000;
end
always @ (posedge Clk or Rst) //異步清零
//always @ (posedge Clk ) //同步清零
if (Rst)
Cnt_result <= 3'b000;
else if (En)
Cnt_result <= Cnt_result + 1'b1;
//assign Cnt_result = Cnt_value;
endmodule
仿真testbench
`timescale 1ns / 1ps
module Counter_test (
);
//端口定義
reg Clk;
reg En,Rst;
wire [2:0] Cnt_result;
//模塊例化
Counter Counter ( //注意頂層模塊例化
. Clk(Clk), //別出現illegal recursive design instantiation,非法遞歸例化
. En(En),
. Rst(Rst),
. Cnt_result(Cnt_result)
);
initial
begin
Clk = 1'b0;
En = 1'b1;
Rst = 1'b0;
end
always
begin
# 10 Clk <= ~Clk;
end
endmodule