一、代码实现
设计定义:利用模块的调用,通过三八译码器来实现跑马灯。
`timescale 1ns / 1ns
module led_run2(
input clk ,
input rstn ,
output[7:0] led
);
reg[24:0] cnt ;
reg[2:0] sel ;
always @(posedge clk or negedge rstn) begin
if(!rstn)
cnt <= 0;
else if(cnt == 25_000_000-1)
cnt <= 0;
else
cnt <= cnt + 1'd1;
end
always @(posedge clk or negedge rstn) begin
if(!rstn)
sel <= 0;
else if(cnt == 25_000_000-1)
sel <= sel + 1'd1;
end
decoder_3_8 inst0(
.S0 (sel[0]) ,
.S1 (sel[1]) ,
.S2 (sel[2]) ,
.out (led) //output reg[7:0] led
);
endmodule
二、仿真
`timescale 1ns / 1ns
module led_run_tb();
reg clk ;
reg rstn ;
wire[7:0] led ;
led_run2 inst0(
.clk (clk ) ,
.rstn (rstn ) ,
.led (led )
);
initial clk = 1;
always #10 clk = ~clk;
initial begin
rstn = 0;
#201;
rstn = 1;
#2000_000_000;//#4000_000_000超过数额范围
#2000_000_000
#500_000_000
$stop;
end
endmodule