Description
This comprehensive guide covers everything about designing a 2-bit magnitude comparator in Verilog – from truth table analysis to complete working code with testbench. Includes live EDA Playground simulation link for hands-on practice.
Introduction
Digital magnitude comparators are essential building blocks in computer systems, used everywhere from ALUs to memory address decoding. This tutorial provides a complete walkthrough of designing, implementing, and verifying a 2-bit comparator in Verilog HDL. We’ll cover:
Understanding the 2-Bit Comparator Architecture
Functional Specifications
A 2-bit magnitude comparator takes two 2-bit binary numbers as inputs:
And produces three single-bit outputs:
Complete Truth Table Analysis
A1 | A0 | B1 | B0 | x (A>B) | y (A=B) | z (A<B) | Description |
0 | 0 | 0 | 0 | 0 | 1 | 0 | Equal case (0 == 0) |
0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 < 1 |
0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 < 2 |
0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 < 3 |
0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 > 0 |
0 | 1 | 0 | 1 | 0 | 1 | 0 | Equal case (1 == 1) |
0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 < 2 |
0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 < 3 |
1 | 0 | 0 | 0 | 1 | 0 | 0 | 2 > 0 |
1 | 0 | 0 | 1 | 1 | 0 | 0 | 2 > 1 |
1 | 0 | 1 | 0 | 0 | 1 | 0 | Equal case (2 == 2) |
1 | 0 | 1 | 1 | 0 | 0 | 1 | 2 < 3 |
1 | 1 | 0 | 0 | 1 | 0 | 0 | 3 > 0 |
1 | 1 | 0 | 1 | 1 | 0 | 0 | 3 > 1 |
1 | 1 | 1 | 0 | 1 | 0 | 0 | 3 > 2 |
1 | 1 | 1 | 1 | 0 | 1 | 0 | Equal case (3 == 3) |
Verilog Implementation Using Dataflow Modeling
Module Definition
module magnitude_comparator(
input [1:0] a, // First 2-bit number (A1A0)
input [1:0] b, // Second 2-bit number (B1B0)
output x, // A > B
output y, // A == B
output z // A < B
);
Download
// Using Verilog relational operators
assign x = (a > b) ? 1’b1 : 1’b0;
assign y = (a == b) ? 1’b1 : 1’b0;
assign z = (a < b) ? 1’b1 : 1’b0;
// Alternative implementation using gate-level logic:
// assign x = (a[1] & ~b[1]) |
// (a[0] & ~b[1] & ~b[0]) |
// (a[1] & a[0] & ~b[0]);
// assign y = (a[1]~^b[1]) & (a[0]~^b[0]); // XNOR for equality
// assign z = ~x & ~y;
endmodule
Comprehensive Testbench Design
Testbench Module
module tb_magnitude_comparator;
reg [1:0] a, b;
wire x, y, z;
// Instantiate the comparator
magnitude_comparator uut (.a(a), .b(b), .x(x), .y(y), .z(z));
// Initialize inputs and monitor changes
initial begin
$display(“Time\tA\tB\t>\t=\t<“);
$monitor(“%0t\t%b\t%b\t%b\t%b\t%b”,
$time, a, b, x, y, z);
// Test all 16 possible combinations
a = 2’b00; b = 2’b00; #10;
a = 2’b00; b = 2’b01; #10;
a = 2’b00; b = 2’b10; #10;
a = 2’b00; b = 2’b11; #10;
a = 2’b01; b = 2’b00; #10;
a = 2’b01; b = 2’b01; #10;
a = 2’b01; b = 2’b10; #10;
a = 2’b01; b = 2’b11; #10;
a = 2’b10; b = 2’b00; #10;
a = 2’b10; b = 2’b01; #10;
a = 2’b10; b = 2’b10; #10;
a = 2’b10; b = 2’b11; #10;
a = 2’b11; b = 2’b00; #10;
a = 2’b11; b = 2’b01; #10;
a = 2’b11; b = 2’b10; #10;
a = 2’b11; b = 2’b11; #10;
$finish;
end
endmodule
Simulation Results and Verification
Figure 1: Comparator simulation output log file
Figure 2: Comparator simulation output waveform
The simulation should show correct comparison results for all 16 input combinations, with exactly one of x, y, or z high for each input pair.
Practical Applications
FAQs
Q1: How can I extend this to 4-bit or larger comparators?
A: Either cascade multiple 2-bit comparators or modify the code to handle wider inputs directly:
module comp_4bit(input [3:0] a, input [3:0] b, output x, y, z);
assign x = (a > b);
assign y = (a == b);
assign z = (a < b);
endmodule
Q2: What’s the difference between dataflow and behavioral modeling for comparators?
A: Dataflow (shown here) uses continuous assignments, while behavioral would use procedural blocks (always). Dataflow is generally more concise for simple combinational logic.
Q3: How do I implement this on actual hardware?
A: You can synthesize this code for FPGAs (Xilinx/Altera) or ASICs. The synthesis tool will optimize the logic gates.
Q4: Can I make a pipelined version for better timing?
A: Yes, by adding pipeline registers, though for 2-bit comparison it’s typically unnecessary.
Conclusion
This tutorial provided a complete implementation of a 2-bit magnitude comparator in Verilog, covering:
Try it yourself on EDA Playground:
2-Bit Magnitude Comparator Implementation
For hands-on learning:
To dive deeper into digital design, check out our:
A. Manikandan is an RTL Engineer at Pantech India Solutions Pvt. Ltd. With a strong passion for digital design ,FPGAs and ASIC bus protocols. he specializes in FPGA and hardware development, sharing insights to bridge the gap between academia and industry.
You can adjust the second line to reflect any specific expertise or areas of interest you wish to highlight!
click here
click here...
click here
click here
click here
click here
click here
click here
click here
click here
click here
click here
click here