What is Comparator?
In Electronics, a comparator is used to compares two voltages (V) / Currents(I) and outputs a digital signal indicating which is larger than other. it consist of two analog input terminals and and one binary digital output . The output is ideally as follows
A comparator mainly has a specialized high gain Differential amplifier. Comparators are commonly used in devices that measure and digitize two analog signals, for example analog to digital signals (ADCs), as well as oscillators.
Verilog Code for 4-bit Comparator
There are many different types of comparators. But, Here we have designed and implemented a simple 4bit-comparator which has two 4 bit inputs and three output bits these outputs says, whether one of the input is less ,greater or equal to the second input.
The verilog code is written in behavioral model. Even though it checks for 4 bit inputs, the code can be extended for other input sizes with very small changes.
verilog code for 4bit-Comparator:
//Fpga Based Verilog Code
module 4bit-comparator(A, B,less, equal, greater );
input [3:0] A;
input [3:0] B;
output less;
output equal;
output greater;
reg less;
reg equal;
reg greater;
always @(A or B)
begin
if(A > B)
The verilog code is written in behavioral model. Even though it checks for 4 bit inputs, the code can be extended for other input sizes with very small changes.
verilog code for 4bit-Comparator:
//Fpga Based Verilog Code
module 4bit-comparator(A, B,less, equal, greater );
input [3:0] A;
input [3:0] B;
output less;
output equal;
output greater;
reg less;
reg equal;
reg greater;
always @(A or B)
begin
if(A > B)
begin // if A is greater than B input.
less = 0;
equal = 0;
greater = 1;
equal = 0;
greater = 1;
end
else if(A == B)
else if(A == B)
begin // if A is equal to B
less = 0;
equal = 1;
greater = 0;
less = 0;
equal = 1;
greater = 0;
end
else
else
begin // else check for A less than B.
less = 1;
equal = 0;
greater =0;
end
end
endmodule
Testbench for Comparator:
module test_tm;
// Inputs
reg [3:0] A;
reg [3:0] B;
// Outputs
wire less;
wire equal;
wire greater;
comparator uut (
.A(A),
.B(B),
.less(less),
.equal(equal),
.greater(greater)
);
initial begin
//Apply inputs
A = 10;
B = 12;
#100;
A = 15;
B = 11;
#100;
A = 10;
B = 10;
#100;
end
endmodule
less = 1;
equal = 0;
greater =0;
end
end
endmodule
Testbench for Comparator:
module test_tm;
// Inputs
reg [3:0] A;
reg [3:0] B;
// Outputs
wire less;
wire equal;
wire greater;
comparator uut (
.A(A),
.B(B),
.less(less),
.equal(equal),
.greater(greater)
);
initial begin
//Apply inputs
A = 10;
B = 12;
#100;
A = 15;
B = 11;
#100;
A = 10;
B = 10;
#100;
end
endmodule
No comments:
Post a Comment