题解 | #信号发生器#
信号发生器
https://www.nowcoder.com/practice/39f6766689cc448e928a0921d1d1f858
//一个用cnt计数 一个是用flag判断 需要 `timescale 1ns/1ns module signal_generator( input clk, input rst_n, input [1:0] wave_choise, output reg [4:0]wave ); reg [4:0]cnt; always@(posedge clk or negedge rst_n) begin if(!rst_n) cnt<=0; else begin if(wave_choise==0) begin if (cnt>=5'd19) cnt<=0; else cnt<=cnt+1; end else cnt<=0; end end reg flag; always@(posedge clk or negedge rst_n) begin if(!rst_n) flag<=1; else begin if(wave_choise==2) begin if (wave==5'd19) flag<=1; else if(wave==1)//等于0的时候 下一个时钟信号就溢出到31去了 flag<=0; else flag<=flag; end else begin if (wave==0) flag<=0; else flag<=1; end end end always@(posedge clk or negedge rst_n) begin if(!rst_n) wave<=0; else begin case(wave_choise) 2'b00:begin wave<=cnt<9?0:cnt<19?5'd20:0; end 2'b01:begin wave<=wave>=20?0:wave+1; end 2'b10:begin wave<=flag==1?wave-1:wave+1; end default wave<=0; endcase end end endmodule