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
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)
equal = 0;
greater = 1;
else if(A == B)
less = 0;
equal = 1;
greater = 0;
else
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