Created
December 6, 2021 05:56
-
-
Save paranlee/54c959c951c39325220759a6c864af1e to your computer and use it in GitHub Desktop.
counter verilog example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module counter # ( | |
| parameter width = 4 | |
| ) ( | |
| input clk, | |
| input rstn, | |
| output reg [width - 1:0] out | |
| ); | |
| always @ (posedge clk) begin | |
| if (!rstn) begin | |
| out <= out + 1; | |
| end else | |
| out <= 0; | |
| end | |
| endmodule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module counter_tb; | |
| localparam width = 4; | |
| reg clk; | |
| reg rstn; | |
| wire [width - 1:0] out; | |
| always #1 clk = ~clk; | |
| counter #( | |
| .width (width) | |
| ) COUNTER ( | |
| .clk(clk), | |
| .rstn(rstn), | |
| .out(out) | |
| ); | |
| initial begin | |
| $display("Initialize to 0."); | |
| clk = 0; | |
| rstn = 0; | |
| $monitor ("[%0t] rstn=0x%0h out=0x%0h", $time, rstn, out); | |
| #10 rstn <= 1; | |
| #30 rstn <= 0; | |
| #70 rstn <= 1; | |
| #95 rstn <= 0; | |
| $dumpfile("counter_tb.vcd"); | |
| $dumpvars(0, counter_tb); // all vars dump | |
| #100 $finish; | |
| end | |
| endmodule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment