4 bit counter - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

208


37
 
1
// Code your testbench here
2
// or browse Examples
3
`default_nettype none
4
5
module tb_counter;
6
  reg clk;                 
7
  reg rstn;                    
8
  wire [3:0] out;             
9
10
  counter uut(clk,rstn,out);
11
12
  // Generate a clock that should be driven to design
13
  // This clock will flip its value every 5ns -> time period = 10ns -> freq = 100 MHz
14
  always #5 clk = ~clk;
15
16
  // This initial block forms the stimulus of the testbench
17
  initial 
18
    begin
19
    
20
      clk <= 0;
21
      rstn <= 0;
22
    #20   
23
      rstn <= 1;                   
24
    #80  
25
      rstn <= 0;
26
    #50   
27
      rstn <= 1;
28
    
29
    #200 
30
      $finish;
31
  end
32
  
33
  initial begin
34
    $dumpvars(0,uut);
35
    $dumpfile("dump.vcd");
36
  end
37
endmodule
xxxxxxxxxx
1
20
 
1
// Code your design here
2
`default_nettype none
3
module counter (  input clk,              
4
                  input rstn,              
5
                  output reg[3:0] out);    // Declare 4-bit output port to get the counter values
6
7
  // This always block will be triggered at the rising edge of clk (0->1)
8
  // Once inside this block, it checks if the reset is 0, if yes then change out to zero
9
  // If reset is 1, then design should be allowed to count up, so increment counter
10
  always @ (posedge clk) begin
11
    if (rstn <= 0)
12
      out <= 0;
13
    else 
14
      out <= out + 1;
15
  end
16
endmodule
17
18
//look up how to generate clock in FPGA
19
20
//https://www.edaplayground.com/x/rux
2473 views and 0 likes     
A short description will be helpful for you to remember your playground's details
 
100:0