代码内容如下:
module counter_led_3(
Clk,
Reset_n,
Ctrl_n,
Led
);
input Clk;
input Reset_n;
input [7:0]Ctrl_n;
output reg Led;
reg [26:0]counter;
parameter MCNT= 100000000;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
counter<=0;
else if(counter == MCNT-1)
counter <=0;
else
counter <= counter +1'b1;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
Led <=0;
else if(counter ==MCNT/8-1)
Led <=Ctrl_n[0];
else if(counter ==MCNT*2/8-1)
Led <=Ctrl_n[1];
else if(counter ==MCNT*3/8-1)
Led <=Ctrl_n[2];
else if(counter ==MCNT*4/8-1)
Led <=Ctrl_n[3];
else if(counter ==MCNT*5/8-1)
Led <=Ctrl_n[4];
else if(counter ==MCNT*6/8-1)
Led <=Ctrl_n[5];
else if(counter ==MCNT*7/8-1)
Led <=Ctrl_n[6];
else if(counter ==MCNT*8/8-1)
Led <=Ctrl_n[7];
else
Led <=Led;
endmodule
仿真代码如下:
`timescale 1ns/1ns
module counter_led_3_tb();
reg Clk_0;
reg Reset_n_0;
reg [7:0]Ctrl_n;
wire Led_0;
counter_led_3 counter_led_0_inst0(
.Clk(Clk_0),
.Reset_n(Reset_n_0),
.Ctrl_n(Ctrl_n),
.Led(Led_0)
);
initial Clk_0=1;
always #10 Clk_0 =!Clk_0;
defparam counter_led_3.MCNT=100000;
initial begin
Reset_n_0 <= 0;
Ctrl_n =0;
#201;
Reset_n_0 <=1;
#2000;
Ctrl_n=8'b1000_0110;
#200000000;
$stop;
end
endmodule