4 bit comparator
This is the code for a 4 bit equality comparator.
module eq4bit ( a, b, led2); input wire [3:0] a, b; output wire led2; wire p1,p2,p3,p4; comp1bit compare1 (a[0],b[0],p1); comp1bit compare2 (a[1],b[1],p2); comp1bit compare3 (a[2],b[2],p3); comp1bit compare4 (a[3],b[3],p4); assign led2 = p1 & p2 & p3 & p4; endmodule module comp1bit ( input a, b, output p ); assign p = (a & b) | ( ~a & ~b); endmodule |
Essentially, it gives output 1 when all 4 bits of a are equal to the 4 bits of b.
The video shows the result when it is implemmented on Spartixed board.
Following ucf file is used.
NET "led2" LOC = P131; NET "a[3]" LOC = P33; NET "a[2]" LOC = P32; NET "a[1]" LOC = P30; NET "a[0]" LOC = P29; NET "b[3]" LOC = P27; NET "b[2]" LOC = P26; NET "b[1]" LOC = P24; NET "b[0]" LOC = P23; |
In place of the module instantiation, we could also use a much simpler implementation as this one.
module eq4bit ( a, b, led2); input wire [3:0] a, b; output wire led2; assign led2 = (a == b) ? 1'b1 : 1'b0; endmodule |
Suggested Exercises
1. Modify the code so that it gives output when a is less than b - use the 1 bit istantiation method ( is slightly difficult) and well as the shorter version .
2. Modify the code so it compares if a is greater than b.