Saturday, July 27, 2013

Specifications : quad_odd_1s_finder

Write a Verilog Code for a module "odd_1s_finder" to find out odd number of 1s in a 8 bit signal "data" it gets every clock "clk". 


Whenever data has odd 1s, an event called "odd event", two outputs change: odd_1s_found is true, and a 32 bit counter odd_1s_counter is incremented by 1. 

Example:
If in a given clock cycle, data[7:0]=8'b0100001, you notice it has 2 1s. Since 2 is even number, therefore there are even number of 1s, so your odd_1s_found signal should be False or 0.
Now if data[7:0]=8'b0100011, odd_1s_found signal should be True or 1, given there are 3 1s in the data.

Write a top level module "quad_odd_1s_finder" it create 4 instances of "odd_1s_finder", it comprise of 1 another 34 bit counter to count total number "odd events" from all 4 instance at each clock edge. 
    
For example in a given clock if 2 out of 4 instances had odd events, increment the top-level counter by 2, and if 3 odd events, increment it by 3 and so on. 

Thursday, July 25, 2013

tb_top.sv to verify quad_odd_1s_finder DUT

//tb_top.sv to verify quad_odd_1s_finder

module tb_top;

wire [33:0] total_odd_count;
reg [7:0] data1,data2,data3,data4;
reg clk,rst;

quad_odd_1s_finder uut(.total_odd_count(total_odd_count),.data1(data1),.data2(data2),.data3(data3),.data4(data4),.clk(clk),.rst(rst));


initial clk=1;
always #10 clk =~clk;

initial
 begin

   data1= 8'b0000_0000; data2= 8'b0000_0000; data3= 8'b0000_0000; data4= 8'b0000_0000; rst =1;

   @(posedge clk) rst=1;
 
   @(posedge clk) begin rst = 0;data1 = 8'b0000_0000; data2 = 8'b0000_0000; data3 = 8'b0000_0000; data4 = 8'b0000_0000;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1011; data2 = 8'b1010_1010; data3 = 8'b1010_1010; data4 = 8'b1010_1010;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1011; data2 = 8'b1010_1010; data3 = 8'b1010_1010; data4 = 8'b1010_1010;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1011; data2 = 8'b1010_1010; data3 = 8'b1010_1010; data4 = 8'b1010_1010;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1010; data2 = 8'b1010_1010; data3 = 8'b1010_1011; data4 = 8'b1010_1011;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1011; data2 = 8'b1010_1011; data3 = 8'b1010_1011; data4 = 8'b1010_1010;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1011; data2 = 8'b1010_1011; data3 = 8'b1010_1011; data4 = 8'b1010_1011;  end

   @(posedge clk) begin rst = 0;data1 = 8'b1010_1010; data2 = 8'b1010_1010; data3 = 8'b1010_1010; data4 = 8'b1010_1010;  end

  #100 $stop;

 end

endmodule

Quad_odd_1s_finder DUT

DUT .v files

//Quad_odd_1s_finder
module quad_odd_1s_finder(output reg [33:0] total_odd_count,input [7:0] data1,data2,data3,data4, input clk,rst);

wire [31:0] odd_count1,odd_count2,odd_count3,odd_count4;
wire [2:0] temp_total_odd_count;
wire od_f1,od_f2,od_f3,od_f4;

odd_1s_finder u1(.odd_count(odd_count1),.data(data1),.clk(clk),.rst(rst),.od_f(od_f1));
odd_1s_finder u2(.odd_count(odd_count2),.data(data2),.clk(clk),.rst(rst),.od_f(od_f2));
odd_1s_finder u3(.odd_count(odd_count3),.data(data3),.clk(clk),.rst(rst),.od_f(od_f3));
odd_1s_finder u4(.odd_count(odd_count4),.data(data4),.clk(clk),.rst(rst),.od_f(od_f4));

assign temp_total_odd_count = (odd_count1 || odd_count2 || odd_count3 || odd_count4)? (od_f1+od_f2+od_f3+od_f4):0;

always@(odd_count1 or odd_count2 or odd_count3 or odd_count4 or rst)
 if(rst)
  total_odd_count = 0;
 else
  total_odd_count = total_odd_count+temp_total_odd_count;

endmodule



//odd_1s_finder dut file
module odd_1s_finder(output reg od_f,output reg [31:0] odd_count,input [7:0 ]data,input clk,rst);
parameter TRUE=1'b1,FALSE=1'b0;
reg r;

always@(posedge clk)
  begin
   if(rst)
    begin
     r=0;
     odd_count=0;
    end
   else
     r=^data;
  end

always@(posedge clk)
  begin
   if(r==1)
    begin
     od_f=TRUE;
     odd_count=odd_count+1;
    end
   else
    begin
     od_f=FALSE;
     odd_count=odd_count;
    end
end

endmodule