无法通过牛客网的测试,代码和仿真波形如下:
module triffic_light ( input rst_n, //异位复位信号,低电平有效 input clk, //时钟信号 input pass_request, output wire[7:0]clock, output reg red, output reg yellow, output reg green ); reg [7:0] count,out,new; reg [1:0] state; reg temp_g,temp_r,temp_y,temp_g1,temp_r1,temp_y1; parameter s0=0,s1=1,s2=2; always @(posedge clk or negedge rst_n) begin if(!rst_n) begin count<=10; end else begin if (pass_request!=1) begin count<=10; end else begin count<= count==1? new : count-1; end end end always @ (*) begin if (!rst_n) begin state =s1; temp_g=0;temp_r=1;temp_y=0; end else if (count==1) begin case (state) s0 :begin state=s1;new=10;temp_g=0;temp_r=1;temp_y=0; end s1 :begin state=s2;new=60;temp_g=1;temp_r=0;temp_y=0; end s2 :begin state=s0;new=5;temp_g=0;temp_r=0;temp_y=1; end endcase end end always@(posedge clk or negedge rst_n) begin temp_r1<=temp_r; temp_g1<=temp_g; temp_y1<=temp_y; end always @(posedge clk or negedge rst_n) begin if(!rst_n) begin red=0; yellow=0; green=0; out=10; end else begin red<=temp_r1; green<=temp_g1; yellow<=temp_y1; out<=count; end end assign clock =out; endmodule
`timescale 1ns/1ns module textbench(); reg clk,rst_n; reg pass_request; wire red,yellow,green; wire [7:0]out; triffic_light u0 ( . rst_n(rst_n), //异位复位信号,低电平有效 .clk(clk), //时钟信号 .pass_request(pass_request), .clock(out), .red(red), .yellow(yellow), .green(green) ); always #5 clk=~clk; initial begin clk=0;rst_n = 0; pass_request=1; #10 rst_n=1; #150 pass_request=0; #10 pass_request=1; #2000; $finish; end endmodule
仿真结果如下: