Part of Curriculum: Verilog Fundamentals (Module 9: Magnitude Comparators)
View Full Roadmap →
Level 1 Easy ★ Flagship #10 15 pts ✓ Solved

4-Bit Magnitude Comparator — Relational Operators

Arithmetic Circuits · combinational, logic-gates, easy, comparators

Design a 4-bit unsigned magnitude comparator comparing inputs a[3:0] and b[3:0], generating gt (a>b), eq (a==b), and lt (a<b).

Write a synthesizable Verilog module named top_module matching the ports below exactly.
📐 Circuit Schematic Diagram
a[3:0]b[3:0]gteqlt4-BIT MAGNITUDE COMPDIGITAL HARDWARE BLOCK
⚡ Timing & Synthesizability Constraints

Timing Contract: Combinational delay across 4-bit subtraction/comparison logic.

RTL Constraints: Outputs `gt`, `eq`, and `lt` are mutually exclusive: exactly one output must be 1 for any valid input pair.

Objective: Understand hardware comparison, relational operators (`>`, `==`, `<`), and mutual exclusivity of relational outputs.

Prerequisites: Unsigned binary numbers, bitwise comparisons.
NameDirWidthDescription
ainput4First 4-bit unsigned operand
binput4Second 4-bit unsigned operand
gtoutput1High when a > b
eqoutput1High when a == b
ltoutput1High when a < b
Expected Waveform (Golden Timing Contract)
💡 Need Guidance? Progressive Hints (3 Levels)
Hint 1: Conceptual Direction & Hardware Primitives
Verilog provides relational operators `>`, `<`, and equality `==`.
Hint 2: Implementation Architecture & Approach
You can use continuous assignments: `assign gt = a > b;`.
Hint 3: Edge Cases, Timing & Verilog Syntax
Ensure equality is checked with `==` (not `=` which is assignment).
🔒 Reveal Verified Reference RTL Solution (Click to expand)
⚠️ Self-Discovery Warning: Try solving and simulating first! Viewing the verified reference implementation will bypass the real debugging experience.
Verified Synthesizable RTL
module top_module(
  input  [3:0] a,
  input  [3:0] b,
  output       gt,
  output       eq,
  output       lt
);
  assign gt = (a > b);
  assign eq = (a == b);
  assign lt = (a < b);
endmodule
Verilog-2001 / SV Saved
Font: |
// Click Run Tests (or press Ctrl+Enter) to compile your RTL and simulate against hidden testbenches.
Simulation Timing Diagram (VCD Waveform)
100%
No simulation waveform yet. Run tests to simulate and generate VCD traces.
No compilation run yet. Syntax warnings and elaboration messages will appear here.
No simulation output yet. Raw stdout from vvp will appear here.

RTL Problems / Level 1 · Arithmetic Circuits

What You Will Learn

By solving 4-Bit Magnitude Comparator — Relational Operators, you will acquire core industry skills:

  • Synthesizable hardware modeling in standard Verilog-2001 / SystemVerilog.
  • Correct handling of interface port contracts, edge-triggered clocks, and active-low/high resets.
  • Simulation waveform interpretation and debugging using timing diagrams (VCD traces).
  • Architectural avoidance of timing violations, glitching hazards, and synthesis warnings.

Problem Statement & Requirements

Design a 4-bit unsigned magnitude comparator comparing inputs a[3:0] and b[3:0], generating gt (a>b), eq (a==b), and lt (a<b).

Hardware design teams at leading semiconductor firms evaluate these exact port contracts, race-condition assertions, and corner cases during technical design reviews and ASIC engineer interviews.

Objective: Understand hardware comparison, relational operators (`>`, `==`, `<`), and mutual exclusivity of relational outputs.

Prerequisites: Unsigned binary numbers, bitwise comparisons.

Port List & Interface Timing

The hidden testbench instantiates top_module. Module port names and bit-widths must match the interface contract exactly:

NameDirWidthDescription
ainput4First 4-bit unsigned operand
binput4Second 4-bit unsigned operand
gtoutput1High when a > b
eqoutput1High when a == b
ltoutput1High when a < b

Timing Constraints & Synthesizable RTL Rules

⚡ Synthesizable Implementation Guidelines

Timing Contract: Combinational delay across 4-bit subtraction/comparison logic.

Synthesis Constraints: Outputs `gt`, `eq`, and `lt` are mutually exclusive: exactly one output must be 1 for any valid input pair.

Architectural Deep Dive & Implementation Walkthrough

A magnitude comparator evaluates the numerical order of two binary numbers. Hardware implementations perform virtual subtraction $a - b$: the sign of the difference and the zero detector indicate whether $a > b$, $a == b$, or $a < b$.

Common RTL Designer Pitfalls

⚠️ Common Mistakes to Avoid

['Treating operands as signed when unsigned comparison was intended (or vice-versa with `$signed`).', 'Allowing more than one output to be active simultaneously.']

Silicon & Hardware Engineering Notes

🛠️ ASIC vs FPGA Hardware Considerations

Equality (`==`) synthesizes into bitwise XNOR gates followed by a wide AND gate. Magnitude (`>` and `<`) uses borrow-lookahead carry chains similar to subtractors.

Starter RTL Code

module top_module(
  input  [3:0] a,
  input  [3:0] b,
  output       gt,
  output       eq,
  output       lt
);

  // Compare a and b: drive gt, eq, lt

endmodule

Verified Reference Solution

🔒 Reveal Verified Reference RTL Solution
module top_module(
  input  [3:0] a,
  input  [3:0] b,
  output       gt,
  output       eq,
  output       lt
);
  assign gt = (a > b);
  assign eq = (a == b);
  assign lt = (a < b);
endmodule
Advertisement

Related RTL Problems in Level 1

Frequently Asked Questions

What does 4-Bit Magnitude Comparator — Relational Operators require?

Design a 4-bit unsigned magnitude comparator comparing inputs a[3:0] and b[3:0], generating gt (a>b), eq (a==b), and lt (a<b). Implement it as Verilog module top_module matching the listed ports.

Is this design combinational or sequential?

Tags: combinational, logic-gates, easy, comparators. If a clock signal is present in the port table, implement it as sequential logic; otherwise, use continuous assignments or combinational blocks.

How is my solution graded?

A hidden SystemVerilog testbench simulates your module in the cloud judge. You pass when all directed corner cases match the golden model and the VCD timing waveform matches the contract.

How does this challenge help with semiconductor interviews?

Technical interviewers at NVIDIA, Qualcomm, Apple, and Intel routinely ask candidates to code, debug, and explain the timing waveforms of 4-Bit Magnitude Comparator — Relational Operators to assess real-world synthesizability and race-condition awareness.

Advertisement