A comparator checks which number is bigger or if they are equal. It helps decisions in digital circuits.
Comparator design in Verilog
module comparator #( parameter N = 4 )( input [N-1:0] a, b, output reg gt, lt, eq ); always @(*) begin if (a > b) begin gt = 1; lt = 0; eq = 0; end else if (a < b) begin gt = 0; lt = 1; eq = 0; end else begin gt = 0; lt = 0; eq = 1; end end endmodule
a and b are the numbers to compare.
gt, lt, and eq show if a is greater, less, or equal to b.
module comparator_4bit( input [3:0] a, b, output reg gt, lt, eq ); always @(*) begin if (a > b) begin gt = 1; lt = 0; eq = 0; end else if (a < b) begin gt = 0; lt = 1; eq = 0; end else begin gt = 0; lt = 0; eq = 1; end end endmodule
module comparator_1bit( input a, b, output reg gt, lt, eq ); always @(*) begin if (a > b) begin gt = 1; lt = 0; eq = 0; end else if (a < b) begin gt = 0; lt = 1; eq = 0; end else begin gt = 0; lt = 0; eq = 1; end end endmodule
This test program compares three pairs of 4-bit numbers and prints which is greater, less, or equal.
module test_comparator; reg [3:0] a, b; wire gt, lt, eq; comparator_4bit comp(.a(a), .b(b), .gt(gt), .lt(lt), .eq(eq)); initial begin a = 4'd9; b = 4'd5; #10; $display("a=%d b=%d gt=%b lt=%b eq=%b", a, b, gt, lt, eq); a = 4'd3; b = 4'd7; #10; $display("a=%d b=%d gt=%b lt=%b eq=%b", a, b, gt, lt, eq); a = 4'd4; b = 4'd4; #10; $display("a=%d b=%d gt=%b lt=%b eq=%b", a, b, gt, lt, eq); $finish; end endmodule module comparator_4bit( input [3:0] a, b, output reg gt, lt, eq ); always @(*) begin if (a > b) begin gt = 1; lt = 0; eq = 0; end else if (a < b) begin gt = 0; lt = 1; eq = 0; end else begin gt = 0; lt = 0; eq = 1; end end endmodule
Use always @(*) to update outputs whenever inputs change.
Make sure output signals are declared as reg because they change inside always block.
A comparator checks if one number is greater, less, or equal to another.
It uses simple if-else conditions inside an always block.
Outputs tell the result clearly with separate signals.
Practice
What is the main purpose of a comparator in Verilog?
Solution
Step 1: Understand comparator function
A comparator compares two values to determine their relationship.Step 2: Identify correct purpose
Only To check if one number is greater, less, or equal to another describes comparing numbers, which is the comparator's job.Final Answer:
To check if one number is greater, less, or equal to another -> Option BQuick Check:
Comparator = Compare numbers [OK]
- Confusing comparator with adder
- Thinking comparator stores data
- Mixing comparator with clock generator
Which of the following is the correct way to start an always block for a comparator that reacts to changes in inputs a and b?
Solution
Step 1: Understand sensitivity list syntax
In Verilog, sensitivity list uses 'or' to list signals that trigger the block.Step 2: Check options
always @(a or b) begin uses 'or' correctly. always @(a, b) begin uses comma which is not valid in Verilog sensitivity lists.Final Answer:
always @(a or b) begin -> Option DQuick Check:
Sensitivity list uses 'or' between signals [OK]
- Using commas instead of 'or' in sensitivity list
- Using 'and' instead of 'or'
- Using clock edge when not needed
Given the following Verilog code snippet, what will be the output signals gt, lt, and eq if a = 5 and b = 3?
module comp(input [3:0] a, b, output reg gt, lt, eq);
always @(a or b) begin
if (a > b) begin
gt = 1; lt = 0; eq = 0;
end else if (a < b) begin
gt = 0; lt = 1; eq = 0;
end else begin
gt = 0; lt = 0; eq = 1;
end
end
endmoduleSolution
Step 1: Compare values a=5 and b=3
Since 5 is greater than 3, the condition 'a > b' is true.Step 2: Determine output signals
For 'a > b', gt=1, lt=0, eq=0 as per code.Final Answer:
gt=1, lt=0, eq=0 -> Option CQuick Check:
5 > 3 means gt=1 [OK]
- Mixing up greater than and less than
- Setting multiple outputs to 1 incorrectly
- Ignoring else conditions
Identify the error in this comparator code snippet:
module comp(input [3:0] a, b, output reg gt, lt, eq);
always @(a, b) begin
if (a > b)
gt = 1; lt = 0; eq = 0;
else if (a < b)
gt = 0; lt = 1; eq = 0;
else
gt = 0; lt = 0; eq = 1;
end
endmoduleSolution
Step 1: Check if-else statement structure
Multiple statements after if need to be grouped with begin-end blocks.Step 2: Identify missing begin-end
Here, multiple assignments after if and else if lack begin-end, causing syntax errors.Final Answer:
Missing begin-end blocks after if and else if -> Option AQuick Check:
Multiple statements need begin-end [OK]
- Forgetting begin-end with multiple statements
- Confusing sensitivity list syntax
- Wrong data type for outputs
You want to design a 4-bit comparator that sets gt high if a > b, lt high if a < b, and eq high if equal. However, you also want to ignore the comparison if either a or b is zero (all bits zero), setting all outputs low in that case. Which Verilog code snippet correctly implements this?
Solution
Step 1: Understand the zero check condition
The requirement is to ignore comparison if either a or b is zero, so use 'a == 0 || b == 0'.Step 2: Verify output assignments
always @(a or b) begin if (a == 0 || b == 0) begin gt = 0; lt = 0; eq = 0; end else if (a > b) begin gt = 1; lt = 0; eq = 0; end else if (a < b) begin gt = 0; lt = 1; eq = 0; end else begin gt = 0; lt = 0; eq = 1; end end sets all outputs low when either input is zero, then compares normally otherwise.Final Answer:
Option A code snippet -> Option AQuick Check:
Use '||' to check either zero, outputs low [OK]
- Using '&&' instead of '||' for zero check
- Setting outputs high when inputs are zero
- Not handling zero inputs separately
Ready to prove it?
Pass the test and this pattern moves unknown
