Verilog code for a comparator

In this project, a simple 2-bit comparator is designed and implemented in Verilog HDL. Truth table, K-Map and minimized equations for the comparator are presented. The Verilog code of the comparator is simulated by ModelSim and the simulation waveform is presented.


Verilog code for a comparator


The specification of the 2-bit comparator is as follows:

  • Input: 2-bit A and B for comparison
  • Output:
    • A_greater_B: high if A > B else low
    • A_equal_B: high if A = B else low
    • A_less_B: high if A<B else low
The truth table for the comparator:


A1
A0
B1
B0
A_greater_B
A_equal_B
A_less_B
0
0
0
0
0
1
0
0
0
0
1
0
0
1
0
0
1
0
0
0
1
0
0
1
1
0
0
1
0
1
0
0
1
0
0
0
1
0
1
0
1
0
0
1
1
0
0
0
1
0
1
1
1
0
0
1
1
0
0
0
1
0
0
1
0
0
1
1
0
0
1
0
1
0
0
1
0
1
0
1
1
0
0
1
1
1
0
0
1
0
0
1
1
0
1
1
0
0
1
1
1
0
1
0
0
1
1
1
1
0
1
0

K-Map tables and the corresponding equations for the comparator:



A_greater_B
A1A0
00

01

11

10
B1B0
00
0
1
1
1
01
0
0
1
1
11
0
0
0
0
10
0
0
1
0

A_greater_B = B0 B1 A0 + B1 A1 + A1 A0 B0
= A0 B0 (B1 + A1) + + B1 A1



A_equal_B
A1A0
00


01


11


10
B1B0
00
1
0
0
0
01
0
1
0
0
11
0
0
1
0
10
0
0
0
1

A_equal_B = (B1 A1+A1 B1) (B0 A0+A0 B0)



A_less_B
A1A0
00


01


11


10
B1B0
00
0
0
0
0
01
1
0
0
0
11
1
1
0
1
10
1
1
0
0

A_less_B = A0 A1 B0 + A1 B1 + B1 B0 A0

After obtaining the minimized equations of the outputs of the comparator, it is easy to write Verilog code for the comparator.

Below is the Verilog code for the comparator:

 // FPGA projects using Verilog/ VHDL  
 // fpga4student.com : FPGA projects, Verilog projects, VHDL projects
 // Verilog code for 2-bit comparator   
 module comparator(input [1:0] A,B, output A_less_B, A_equal_B, A_greater_B);  
 wire tmp1,tmp2,tmp3,tmp4,tmp5, tmp6, tmp7, tmp8;  
 // A = B output   
 xnor u1(tmp1,A[1],B[1]);  
 xnor u2(tmp2,A[0],B[0]);  
 and u3(A_equal_B,tmp1,tmp2);  
 // A less than B output   
 assign tmp3 = (~A[0])& (~A[1])& B[0];  
 assign tmp4 = (~A[1])& B[1];  
 assign tmp5 = (~A[0])& B[1]& B[0];  
 assign A_less_B = tmp3 | tmp4 | tmp5;  
 // A greater than B output   
 assign tmp6 = (~B[0])& (~B[1])& A[0];  
 assign tmp7 = (~B[1])& A[1];  
 assign tmp8 = (~B[0])& A[1]& A[0];  
 assign A_greater_B = tmp6 | tmp7 | tmp8;  
 endmodule   
 `timescale     10 ps/ 10 ps  
 // FPGA projects using Verilog/ VHDL  
 // fpga4student.com  
 // Verilog testbench code for 2-bit comparator   
 module tb_comparator;  
 reg [1:0] A, B;  
 wire A_less_B, A_equal_B, A_greater_B;  
 integer i;  
 // device under test  
 comparator dut(A,B,A_less_B, A_equal_B, A_greater_B);  
 initial begin  
      for (i=0;i<4;i=i+1)  
      begin   
           A = i;  
           B = i + 1;  
           #20;  
      end   
      for (i=0;i<4;i=i+1)  
      begin   
           A = i;  
           B = i;  
           #20;  
      end   
      for (i=0;i<4;i=i+1)  
      begin   
           A = i+1;  
           B = i;  
           #20;  
      end   
 end   
 endmodule   

Finally, simulate the Verilog code and testbench in ModelSim and verify the operation of the comparator through simulation waveform. 

Verilog code for 2-bit comparator

By observing the waveform, Verilog code for the comparator are working correctly. 


Recommended Verilog projects:
2. Verilog code for FIFO memory
3. Verilog code for 16-bit single-cycle MIPS processor
4. Programmable Digital Delay Timer in Verilog HDL
5. Verilog code for basic logic components in digital circuits
6. Verilog code for 32-bit Unsigned Divider
7. Verilog code for Fixed-Point Matrix Multiplication
8. Plate License Recognition in Verilog HDL
9. Verilog code for Carry-Look-Ahead Multiplier
10. Verilog code for a Microcontroller
11. Verilog code for 4x4 Multiplier
12. Verilog code for Car Parking System
13. Image processing on FPGA using Verilog HDL
14. How to load a text file into FPGA using Verilog HDL
15. Verilog code for Traffic Light Controller
16. Verilog code for Alarm Clock on FPGA
17. Verilog code for comparator design
18. Verilog code for D Flip Flop
19. Verilog code for Full Adder
20. Verilog code for counter with testbench
21. Verilog code for 16-bit RISC Processor
22. Verilog code for button debouncing on FPGA
23. How to write Verilog Testbench for bidirectional/ inout ports
28. Verilog code for Decoder
29. Verilog code for Multiplexers
30.  N-bit Adder Design in Verilog
31. Verilog vs VHDL: Explain by Examples
32. Verilog code for Clock divider on FPGA
33. How to generate a clock enable signal in Verilog
34. Verilog code for PWM Generator
35. Verilog coding vs Software Programming
FPGA Verilog VHDL courses

1 comment:

  1. you should have given the circuit also. it makes easy to understand code.

    ReplyDelete

Trending FPGA Projects