You are on page 1of 4

Comparator

2-bit magnitude comparator using Behavioral modeling style.


module 2_Mag_Comp(
input [1:0]a,b,
output equal, greater, lower
);
reg greater, equal, lower;
initial greater = 0, equal = 0, lower = 0;
always @ (a or b)
begin
if (a < b)

begin
greater = 0; equal = 0; lower = 1;
end
else if (a == b)
begin
greater = 0; equal = 1; lower = 0;
end
else
begin
greater = 1; equal = 0; lower = 0;
end
end
endmodule

2-bit magnitude comparator using Dataflow modeling style.


module comparator_2bit ( a ,b ,equal ,greater ,lower );

output equal ;
output greater ;
output lower ;

input [1:0] a ;
input [1:0] b ;

assign equal = (a==b) ? 1 : 0;


assign greater = (a>b) ? 1 : 0;
assign lower = (a<b) ? 1 : 0;

endmodule
Gate Level

module mc2bit(a0,a1,b0,b1,f0,f1,f2); //Gate level model

input a0,a1,b0,b1;

output f0,f1,f2;

wire x,y,u,v,p,q,r,j,k,c,f,g;

not(x,a0);

not(y,a1);

not(u,b0);

not(v,b1);

and(p,x,y,b0);

and(q,x,b0);

and(r,b0,b1,y);

or(f0,p,q,r);

and(j,a1,b1);

and(k,y,v);

or(f1,j,k);

and(c,a1,u,v);

and(f,a0,u);

and(g,v,x,y);

or(f2,c,f,g);

endmodule
STRUCTURAL

module comparator2BitStruct(
input wire[1:0] a, b,
output wire eq
);

wire s0, s1;

comparator1Bit eq_bit0 (.x(a[0]), .y(b[0]), .eq(s0));


comparator1Bit eq_bit1 (.x(a[1]), .y(b[1]), .eq(s1));

assign eq = s0 & s1;


endmodule

4 BIT COMPARATOR

Behave

module comparator(
Data_in_A, //input A
Data_in_B, //input B
less, //high when A is less than B
equal, //high when A is equal to B
greater //high when A is greater than B
);

//what are the input ports.


input [3:0] Data_in_A;
input [3:0] Data_in_B;
//What are the output ports.
output less;
output equal;
output greater;
//Internal variables
reg less;
reg equal;
reg greater;

//When the inputs and A or B are changed execute this block


always @(Data_in_A or Data_in_B)
begin
if(Data_in_A > Data_in_B) begin //check if A is bigger than B.
less = 0;
equal = 0;
greater = 1; end
else if(Data_in_A == Data_in_B) begin //Check if A is equal to B
less = 0;
equal = 1;
greater = 0; end
else begin //Otherwise - check for A less than B.
less = 1;
equal = 0;
greater =0;
end
end
endmodule

Data Flow
module comparator_4_bit (a_gt_b, a_lt_b, a_eq_b, a,b);

input [3 : 0] a,b;
output a_gt_b, a_lt_b, a_eq_b;

assign a_gt_b = (a > b);


assign a_lt_b = (a < b);
assign a_eq_b = (a == b);

endmodule

ONE BIT

Gate Level

module comparator_1_bit(
input x,
input y,
output a, //x>y
output b, //x=y
output c //x<y
);
not(xn,x),(yn,y);
and(a,x,yn),(c,xn,y);
nor(b,a,c);
endmodule
Data
module comparator_1bit ( a ,b ,equal ,greater ,lower );

output equal ;
output greater ;
output lower ;

input a ;
input b ;

assign equal = a ~^ b;
assign lower = (~a) & b;
assign greater = a & (~b);

endmodule

You might also like