Saturday 4 February 2012

Verilog Codes for different COUNTERS


Verilog code for a 4-bit unsigned up counter with asynchronous clear.
        module counter (clk, clr, q);
        input        clk, clr;
        output [3:0] q;
        reg    [3:0] tmp;
        always @(posedge clk or posedge clr)
        begin
           if (clr)
              tmp <= 4’b0000;
           else
              tmp <= tmp + 1’b1;
        end
           assign q = tmp;
        endmodule
        

 Verilog code for a 4-bit unsigned down counter with synchronous set.
        module counter (clk, s, q);
        input        clk, s;
        output [3:0] q;
        reg    [3:0] tmp;
        always @(posedge clk)
        begin
           if (s)
              tmp <= 4’b1111;
           else
              tmp <= tmp - 1’b1;
        end
           assign q = tmp;
        endmodule
        
 Verilog code for a 4-bit unsigned up counter with an asynchronous load from the primary input.
        module counter (clk, load, d, q);
        input        clk, load;
        input  [3:0] d;
        output [3:0] q;
        reg    [3:0] tmp;
        always @(posedge clk or posedge load)
        begin
           if (load)
              tmp <= d;
           else
              tmp <= tmp + 1’b1;
        end
           assign q = tmp;
        endmodule 
        
 Verilog code for a 4-bit unsigned up counter with a synchronous load with a constant.
 module counter (clk, sload, q);
 input        clk, sload;
 output [3:0] q;
 reg    [3:0] tmp;
 always @(posedge clk)
 begin
    if (sload) 
              tmp <= 4’b1010;
    else 
       tmp <= tmp + 1’b1;
 end
    assign q = tmp;
        endmodule
        
 Verilog code for a 4-bit unsigned up counter with an asynchronous clear and a clock enable.
 module counter (clk, clr, ce, q);
 input        clk, clr, ce;
 output [3:0] q;
 reg    [3:0] tmp;
 always @(posedge clk or posedge clr)
 begin
    if (clr)
       tmp <= 4’b0000;
    else if (ce)
       tmp <= tmp + 1’b1;
 end
    assign q = tmp;
        endmodule
        
 Verilog code for a 4-bit unsigned up/down counter with an asynchronous clear.
 module counter (clk, clr, up_down, q);
 input        clk, clr, up_down;
 output [3:0] q;
 reg    [3:0] tmp;
 always @(posedge clk or posedge clr)
 begin
    if (clr)
       tmp <= 4’b0000;
    else if (up_down) 
       tmp <= tmp + 1’b1;
    else
       tmp <= tmp - 1’b1;
 end
    assign q = tmp;
        endmodule
        
e Verilog code for a 4-bit signed up counter with an asynchronous reset.
        module counter (clk, clr, q);
        input               clk, clr;
        output signed [3:0] q;
        reg    signed [3:0] tmp;
        always @ (posedge clk or posedge clr)
        begin
           if (clr)
              tmp <= 4’b0000;
           else
              tmp <= tmp + 1’b1;
        end
           assign q = tmp;
        endmodule
        
 Verilog code for a 4-bit signed up counter with an asynchronous reset and a modulo maximum.
        module counter (clk, clr, q);
        parameter MAX_SQRT = 4, MAX = (MAX_SQRT*MAX_SQRT);
        input                 clk, clr;
        output [MAX_SQRT-1:0] q;
        reg    [MAX_SQRT-1:0] cnt;
        always @ (posedge clk or posedge clr)
        begin
           if (clr)
              cnt <= 0;
           else
              cnt <= (cnt + 1) %MAX;
        end
           assign q = cnt;
        endmodule
        

10 comments:

  1. hello sir,
    i want code of 3 bit up/down synchronous counter in verilog..
    will u pls help me
    if not give me some idea how to write its code in verilog..because i had design the ckt but i don't know how to write code for this ckt.

    ReplyDelete
    Replies
    1. 4 bit code also works for 3 bit , there is no specifically 3bits , so you can go with 4 bit code

      Delete
  2. I second Himanshu's request. Along with a gated D-latch example.

    ReplyDelete
  3. hello sir,
    I am doing processor modelling project. Will you please give me the codes of memory unit in verilog where there is no user input.
    Thank you

    ReplyDelete
  4. structural way of mod 3 async counter: (synthesized)
    `timescale 1ns / 1ps
    module divbythree(clock,reset,q0,q1);
    input clock,reset;
    output q0,q1;
    wire q0_bar;
    wire reset1,reset2,reset3;

    tff tf1(1'b1,clock,reset3,q0);
    not n2(q0_bar,q0);
    tff tf2(1'b1,q0_bar,reset2,q1);
    nand n1(reset1,q0,q1);
    assign reset2=reset1 & reset;
    assign reset3=reset2 & reset;

    endmodule

    module tff(t,clock,reset,q);
    input t,clock,reset;
    output reg q;

    always @ (posedge clock)
    begin
    if(!reset)
    q<=1'b0;
    else if(t==1)
    q<=~q;
    else
    q<=q;
    end

    endmodule

    ReplyDelete
  5. Can u provide program for mod n counter

    ReplyDelete
  6. need 8 bit up down counter plz

    ReplyDelete
  7. can u please help me with my md5 code

    ReplyDelete
  8. Can u plz provide the verilog code for 4 bit counter with reset and enable inputs

    ReplyDelete
  9. Can u please forward me real time clock verilog code

    ReplyDelete