_TOP_MENU

Nov 25, 2016

Verilog Code for the counting number of 1's and 0's


Here is the verilog code for counting number of 1's and number of 0's in parallel data. since data is continuously changing on every clock cycle,  counting has to be done using parallel hardware.

Below is the code with testbench and simulation result :-

Does anyone know what will be the hardware synthesis tool will make ?

Is it going to be encoder , or  multiple adders or full of mux ??

---------------------------- Verilog Code ------------------

module count1n0 (
input clk,
input rstn,
input [9:0] data,
output reg [3:0] num_of_one,
output reg [3:0] num_of_zero
);

 

 always @(*) begin
      num_of_one  = 4'b0;
      num_of_zero = 4'b0;

    if(!data[0])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one + 1 ;

    if(!data[1])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[2])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[3])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[4])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[5])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[6])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[7])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[8])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

    if(!data[9])
      num_of_zero = num_of_zero + 1;
    else
      num_of_one = num_of_one +1 ;

  end

endmodule
------------------------------------------------------

------------------- Verilog Testbench ----------------

module tb;

reg clk;
reg rstn;
reg [9:0] count;
wire [3:0] one;
wire [3:0] zero;

initial begin
clk =0;
rstn =0;
count =0;
end

initial begin
#100;
rstn = 1 ;
#1000;
$finish;
end

initial $monitor("data = %b, num of zero = %d, num_of_one = %d ", count , zero, one);

always @(negedge clk) begin
count = count + 1297;
end

always #5 clk = ~clk ;

count1n0 dut (
.clk(clk),
.rstn(rstn),
.data(count),
.num_of_one(one),
.num_of_zero(zero)
);

initial begin
 $recordfile("test.trn");
 $recordvars();
end

endmodule
----------------------------------------------------

-------------------Simulation Result - -------------
data = 0000000000, num of zero = 10, num_of_one =  0 
data = 0100010001, num of zero =  7, num_of_one =  3 
data = 1000100010, num of zero =  7, num_of_one =  3 
data = 1100110011, num of zero =  4, num_of_one =  6 
data = 0001000100, num of zero =  8, num_of_one =  2 
data = 0101010101, num of zero =  5, num_of_one =  5 
data = 1001100110, num of zero =  5, num_of_one =  5 
data = 1101110111, num of zero =  2, num_of_one =  8 
data = 0010001000, num of zero =  8, num_of_one =  2 
data = 0110011001, num of zero =  5, num_of_one =  5 
data = 1010101010, num of zero =  5, num_of_one =  5 
data = 1110111011, num of zero =  2, num_of_one =  8 
data = 0011001100, num of zero =  6, num_of_one =  4 
data = 0111011101, num of zero =  3, num_of_one =  7 
data = 1011101110, num of zero =  3, num_of_one =  7 
data = 1111111111, num of zero =  0, num_of_one = 10 
data = 0100010000, num of zero =  8, num_of_one =  2 
data = 1000100001, num of zero =  7, num_of_one =  3 
data = 1100110010, num of zero =  5, num_of_one =  5 
data = 0001000011, num of zero =  7, num_of_one =  3 
data = 0101010100, num of zero =  6, num_of_one =  4 
data = 1001100101, num of zero =  5, num_of_one =  5 
data = 1101110110, num of zero =  3, num_of_one =  7 
data = 0010000111, num of zero =  6, num_of_one =  4 
data = 0110011000, num of zero =  6, num_of_one =  4 
data = 1010101001, num of zero =  5, num_of_one =  5 
data = 1110111010, num of zero =  3, num_of_one =  7 
data = 0011001011, num of zero =  5, num_of_one =  5 
data = 0111011100, num of zero =  4, num_of_one =  6 
data = 1011101101, num of zero =  3, num_of_one =  7 
data = 1111111110, num of zero =  1, num_of_one =  9 
data = 0100001111, num of zero =  5, num_of_one =  5 
data = 1000100000, num of zero =  8, num_of_one =  2 
data = 1100110001, num of zero =  5, num_of_one =  5 
data = 0001000010, num of zero =  8, num_of_one =  2 
data = 0101010011, num of zero =  5, num_of_one =  5 
data = 1001100100, num of zero =  6, num_of_one =  4 
data = 1101110101, num of zero =  3, num_of_one =  7 
data = 0010000110, num of zero =  7, num_of_one =  3 
data = 0110010111, num of zero =  4, num_of_one =  6 
data = 1010101000, num of zero =  6, num_of_one =  4 
data = 1110111001, num of zero =  3, num_of_one =  7 
data = 0011001010, num of zero =  6, num_of_one =  4 
data = 0111011011, num of zero =  3, num_of_one =  7 
data = 1011101100, num of zero =  4, num_of_one =  6 
data = 1111111101, num of zero =  1, num_of_one =  9 
data = 0100001110, num of zero =  6, num_of_one =  4 
data = 1000011111, num of zero =  4, num_of_one =  6 
data = 1100110000, num of zero =  6, num_of_one =  4 
data = 0001000001, num of zero =  8, num_of_one =  2 
data = 0101010010, num of zero =  6, num_of_one =  4 
data = 1001100011, num of zero =  5, num_of_one =  5 
data = 1101110100, num of zero =  4, num_of_one =  6 
--------------------------------------------------------------

Thanks for visiting my blog. 


7 comments:

  1. Does anyone knows what will be the hardware for this ?

    ReplyDelete
  2. What will happen if we are reusing the same adder for both 1 and 0 counters?

    ReplyDelete
    Replies
    1. You can not use the same adder, as the process is concurrent and it has to count on every clock pulse.

      Delete
  3. Replies
    1. 1279 is random number, you can take choose any number which can generate maximum possible combinations. It's testbench part.

      Delete