N bit comparator
N bit comparator:
A magnitude digital Comparator is a combinational circuit that compares two digital or binary numbers in order to find out whether one binary number is equal, less than, orgreater than the other binary number.
`timescale 1ns / 1ps
module comparator_nbit(a,b,Lesser,Greater,Equal);parameter n=32;
input [n-1:0]a,b;
output Lesser,Greater,Equal;
reg Lesser=0,Greater=0,Equal=0;
always @ (a,b)
begin
if(a>b)
begin
Lesser=0;Equal=0;Greater=1;
end
else if (a<b)
begin
Lesser=1;Equal=0;Greater=0;
end
else
begin
Lesser=0;Equal=1;Greater=0;
end
end
endmodule
Test bench code:
`timescale 1ns / 1ps
module comparator_nbit_tb;
// Inputs
reg [31:0] a;
reg [31:0] b;
// Outputs
wire Lesser;
wire Greater;
wire Equal;
// Instantiate the Unit Under Test (UUT)
comparator_nbit uut (
.a(a),
.b(b),
.Lesser(Lesser),
.Greater(Greater),
.Equal(Equal)
);
initial begin
$monitor(" A=%d B=%d Lesser=%b Greater=%b Equal=%b ",a,b,Lesser,Greater,Equal);
end
initial begin
a = 2;b = 2;
#100 a=22;b=444;
#100 a=444;b=555;
#100 a=777;b=111;
#100 a=8888;b=8888;
#500 $finish;
end
endmodule
#verilog #rtl N bit comparator using Verilog
Comments
Post a Comment