Comparators in Verilog
To compare the magnitude of two binary numbers to obtain their status, as
- The first number may be greater than the second.
- The two numbers may be equal. Or,
- The first number may be less than the second.
To do this we will need a comparator. We can explain the comparison operation on two binary variables x and y (each being one bit) using the truth table presented in Table. Here, g, e, and l stand for greater, equal, and less, respectively.
we can obtain logic functions between inputs and outputs of the one-bit comparator as follows
logic functions lead to the circuit diagram of the one-bit comparator as
Verilog Description of One-Bit Comparator:
The Verilog description of an N-bit comparator to compare two N-bit numbers using dataflow and structural modelling will be complex. Therefore, behavioural modelling will be more appropriate for this case.
To do so, we need to introduce relational operators and conditional statements in Verilog.
๐๐ relational operators.
An N-bit comparator can be constructed by the if keyword. We provide such a Verilog description only for behavioural modelling
Verilog Description of Four-Bit Comparator Using if Keyword
output Gt, Lt, Eq; //The Outputs of comparison
reg Gt, Lt, Eq;
always @ (In1 or In2) //Check the state of the input lines
begin
Gt <= ( In1 > In2 )? 1'b1 : 1'b0;
Lt <= ( In1 < In2 )? 1'b1 : 1'b0;
Eq <= ( In1 == In2)? 1'b1 : 1'b0;
end
`timescale 1ns / 1ps
module magComp_tb;
//
Inputs
reg [7:0] In1;
reg [7:0] In2;
//
Outputs
wire Gt;
wire Lt;
wire Eq;
//
Instantiate the Unit Under Test (UUT)
magComp uut ( .In1(In1), .In2(In2), .Gt(Gt), .Lt(Lt), .Eq(Eq) );
initial begin
//
Initialize Inputs
In1 = 8'b0;
In2 = 8'b0;
//
Wait 100 ns for global reset to finish
#100;
//
Add stimulus here
In1 = 8'd8;
In2 = 8'd7;
#20;
In1 = 8'd100;
In2 = 8'd120;
#20;
In1 = 8'd250;
In2 = 8'd250;
#20;
In1 =
8'd0;
In2 = -8'd5;
#20;
In1 = -8'd5;
In2 = -8'd5;
#20;
end
endmodule
simulation result:
No comments:
Post a Comment