verilog設計一個可預置初值的7進位制迴圈計數器
阿新 • • 發佈:2020-08-24
實現預置計數器,當為7進位制時,直接修改資料位寬為3bit即可。
//實現可預置初值的15進位制迴圈計數器 module test ( input clk, input rst, input [3:0]d_in, output reg [3:0]d_out ); reg [3:0]cnt; always @(posedge clk or negedge rst) begin if(!rst) begin cnt <= d_in; end else if (cnt == 'b1110) begincnt <= 'b0; end else begin cnt <= cnt + 1'b1; end end always @(posedge clk or negedge rst) begin if(!rst) begin d_out <= 'b0; end else begin d_out <= cnt; end end endmodule
`timescale 1ns/10psmodule test_tb; reg clk; reg rst; reg [3:0]d_in; wire [3:0]d_out; test u1( .clk(clk), .rst(rst), .d_in(d_in), .d_out(d_out) ); initial begin clk = 1'b1; rst = 1'b0; #8; rst = 1'b1; end always #5 clk = ~clk; initialbegin d_in = 4'b0010; #10; end endmodule