-
Introduction
An 8-bit magnitude comparator compares the two 8-bit values and produce a 1-bit flag as result, which indicates that the first value is either greater than or less than or equal to the second value. The block diagram of a comparator is shown in Figure 1. One key point to note is that Verilog treats reg data type as unsigned integer number of specified width. Therefore, the comparison 0 and -5 leads to (0 < -5). This is because 2’s complement representation of -5 is 251 and 0 < 251.
Figure 1. Block Diagram of Magnitude Comparator -
Truth Table
The truth table followed by the magnitude comparator is shown in Figure 2. A and B are the two 8-bit input signals to the comparator which compares the magnitude of these the two signals. The three 1-bit output signals are A < B, A = B and A > B.
Figure 2. Truth Table of 8-bit Magnitude Comparator -
Verilog Module
Figure 3 shows the Verilog module of the 8-bit magnitude comparator. The module has two 8-bit input port In1 and In2. There are three 1-bit output ports Eq, Gt, and Lt.
Figure 3. Verilog module of 8-bit Magnitude Comparator -
Verilog Code for 8-bit Magnitude Comparator (magComp.v)
- module magComp ( In1,
- In2,
- Gt,
- Lt,
- Eq
- );
- input [7:0] In1,
- In2; //The two 8-bit Inputs In1 and In2
- 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
- endmodule
Figure 4. Verilog Code for 8-bit Magnitude Comparator -
Verilog Test-Bench of 8-bit Magnitude Comparator (magComp_tb.v)
- `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
Figure 5. Verilog Test-bench for 8-bit Magnitude Comparator -
Timing Diagram
Figure 6. Timing waveform of 8-bit Magnitude Comparator
8 bit Magnitude Comparator
Subscribe to:
Posts (Atom)