Part of Curriculum: Sequential RTL Design (Module 4: Binary Up/Down Counters)
View Full Roadmap →
Level 2 Medium ★ Flagship #17 25 pts ✓ Solved

Up/Down Counter — Bidirectional Counting

Sequential Logic · sequential, fsm, counters, medium, counters

Design a 4-bit synchronous up/down counter with active-low async reset and up_down control input (1 = count up, 0 = count down).

Write a synthesizable Verilog module named top_module matching the ports below exactly.
📐 Circuit Schematic Diagram
up_downq[3:0]>clkrst_nUP/DOWN COUNTERSEQUENTIAL COUNTER
⚡ Timing & Synthesizability Constraints

Timing Contract: State updates on `posedge clk`. Rolls over from 15 to 0 on up-count; rolls under from 0 to 15 on down-count.

RTL Constraints: Unsigned 4-bit register arithmetic (`+ 1'b1` and `- 1'b1`).

Objective: Understand bidirectional sequential state machines and dynamic datapath steering.

Prerequisites: Binary counters, conditional arithmetic.
NameDirWidthDescription
clkinput1Clock signal
areset_ninput1Active-low asynchronous reset
up_downinput1Direction: 1 = increment, 0 = decrement
qoutput44-bit counter value
Expected Waveform (Golden Timing Contract)
💡 Need Guidance? Progressive Hints (3 Levels)
Hint 1: Conceptual Direction & Hardware Primitives
Use an `if (up_down) q <= q + 1'b1; else q <= q - 1'b1;` branch.
Hint 2: Implementation Architecture & Approach
Unsigned subtraction in 4-bit Verilog naturally underflows `4'd0 - 1'b1` to `4'd15`.
Hint 3: Edge Cases, Timing & Verilog Syntax
Verify reset returns `q` to 0.
🔒 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        clk,
  input        areset_n,
  input        up_down,
  output reg [3:0] q
);
  always @(posedge clk or negedge areset_n) begin
    if (!areset_n)
      q <= 4'd0;
    else if (up_down)
      q <= q + 1'b1;
    else
      q <= q - 1'b1;
  end
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 2 · Sequential Logic

What You Will Learn

By solving Up/Down Counter — Bidirectional Counting, 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 synchronous up/down counter with active-low async reset and up_down control input (1 = count up, 0 = count down).

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 bidirectional sequential state machines and dynamic datapath steering.

Prerequisites: Binary counters, conditional arithmetic.

Port List & Interface Timing

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

NameDirWidthDescription
clkinput1Clock signal
areset_ninput1Active-low asynchronous reset
up_downinput1Direction: 1 = increment, 0 = decrement
qoutput44-bit counter value

Timing Constraints & Synthesizable RTL Rules

⚡ Synthesizable Implementation Guidelines

Timing Contract: State updates on `posedge clk`. Rolls over from 15 to 0 on up-count; rolls under from 0 to 15 on down-count.

Synthesis Constraints: Unsigned 4-bit register arithmetic (`+ 1'b1` and `- 1'b1`).

Architectural Deep Dive & Implementation Walkthrough

Bidirectional counters are essential in FIFO occupancy trackers, quadrature rotary encoders, and circular ring buffer pointers. The direction signal `up_down` steers an adder/subtractor unit.

Common RTL Designer Pitfalls

⚠️ Common Mistakes to Avoid

['Treating `q` as signed, causing underflow to yield `-1` instead of `15`.', 'Using separate `always` blocks for up and down, inferring conflicting multi-driver nets.']

Silicon & Hardware Engineering Notes

🛠️ ASIC vs FPGA Hardware Considerations

In FPGA DSP blocks and adder slices, addition and subtraction use the same adder hardware by inverting the B-operand and asserting carry-in ($A - B = A + \sim B + 1$).

Starter RTL Code

module top_module(
  input        clk,
  input        areset_n,
  input        up_down,
  output reg [3:0] q
);

  // 4-bit bidirectional counter

endmodule

Verified Reference Solution

🔒 Reveal Verified Reference RTL Solution
module top_module(
  input        clk,
  input        areset_n,
  input        up_down,
  output reg [3:0] q
);
  always @(posedge clk or negedge areset_n) begin
    if (!areset_n)
      q <= 4'd0;
    else if (up_down)
      q <= q + 1'b1;
    else
      q <= q - 1'b1;
  end
endmodule
Advertisement

Related RTL Problems in Level 2

Frequently Asked Questions

What does Up/Down Counter — Bidirectional Counting require?

Design a 4-bit synchronous up/down counter with active-low async reset and up_down control input (1 = count up, 0 = count down). Implement it as Verilog module top_module matching the listed ports.

Is this design combinational or sequential?

Tags: sequential, fsm, counters, medium, counters. 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 Up/Down Counter — Bidirectional Counting to assess real-world synthesizability and race-condition awareness.

Advertisement