Saturday 30 April 2016

Weight measurement of a 12 bit binary codeword using Verilog


The weight of a codeword implicates the number nonzero elements in a binary block.Weight measurement structure shown in above figure, counts number of 1's in a 12-bit binary codeword.
Verilog code for weight measurement of a 12 bit binary codeword is given below


//verilog code for weight measurement of a 12 bit binary input

//Full Adder
module fad(a,b,c,s,cout);
input a,b,c;
output s,cout;
assign s=a^b^c;
assign cout=(a&b)|(b&c)|(c&a);
endmodule

//Two-bit Adder
module twoad1(a,b,s,c);
input [1:0]a,b;
output [1:0]s,c;
assign s[0]=a[0]^b[0];
assign c[0]=a[0]&b[0];
assign s[1]=c[0]^a[1]^b[1];
assign c[1]=(a[1]&b[1])|(b[1]&c[0])|(c[0]&a[1]);
endmodule

//Three-bit Adder
module threead1(a,b,s,c);
input [2:0]a,b;
output [2:0]s,c;
assign s[0]=a[0]^b[0];
assign c[0]=a[0]&b[0];
assign s[1]=c[0]^a[1]^b[1];
assign c[1]=(a[1]&b[1])|(b[1]&c[0])|(c[0]&a[1]);
assign s[2]=c[1]^a[2]^b[2];
assign c[2]=(a[2]&b[2])|(b[2]&c[1])|(c[1]&a[2]);
endmodule

//Weight Measurement
module wt1(s,x);
input [11:0]s;
output [3:0]x;
wire s0,s1,s2,s3,c0,c1,c2,c3;
wire [1:0]d,e,f,g,s4,s5,c4,c5;
wire [2:0]h,i,s6,c6;

fad a1(s[11],s[10],s[9],s3,c3);
fad a2(s[8],s[7],s[6],s2,c2);
fad a3(s[5],s[4],s[3],s1,c1);
fad a4(s[2],s[1],s[0],s0,c0);

assign d={c3,s3};
assign e={c2,s2};
assign f={c1,s1};
assign g={c0,s0};

twoad1 a5(d,e,s4[1:0],c4[1:0]);
twoad1 a6(f,g,s5[1:0],c5[1:0]);

assign h={c4[1],s4[1:0]};
assign i={c5[1],s5[1:0]};

threead1 a7(h,i,s6[2:0],c6[2:0]);

assign x={c6[2],s6};
endmodule

//Test bench
module wt1_tb();
reg [11:0]s;
wire [3:0]x;
wt1 dut(s,x);
initial begin
s=12'b1000_0000_0001;
#50 $stop;
end
endmodule

//This code counts the number of 1's in a 12 bit binary codeword.
For example input message is s = 1010_0010_0001;
then output will be x=0100 (4).

No comments:

Post a Comment