0
0
Verilogprogramming~20 mins

Up-down counter with direction control in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Up-Down Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a 3-bit up-down counter with direction control
What is the value of count after 5 clock cycles if dir = 1 (count up) starting from 3'b010?
Verilog
module counter(input clk, input dir, output reg [2:0] count);
  initial count = 3'b010;
  always @(posedge clk) begin
    if (dir)
      count <= count + 1;
    else
      count <= count - 1;
  end
endmodule
A3'b101
B3'b111
C3'b100
D3'b000
Attempts:
2 left
💡 Hint
Count up 5 times starting from 2 (binary 010).
Predict Output
intermediate
2:00remaining
Output of a 4-bit up-down counter counting down
Given a 4-bit up-down counter starting at 4'b1001 and dir = 0 (count down), what is the value of count after 3 clock cycles?
Verilog
module counter(input clk, input dir, output reg [3:0] count);
  initial count = 4'b1001;
  always @(posedge clk) begin
    if (dir)
      count <= count + 1;
    else
      count <= count - 1;
  end
endmodule
A4'b0110
B4'b0111
C4'b1000
D4'b0101
Attempts:
2 left
💡 Hint
Count down 3 times starting from 9 (binary 1001).
🔧 Debug
advanced
2:00remaining
Identify the error in this up-down counter code
What error will this Verilog code produce when simulated?
Verilog
module counter(input clk, input dir, output reg [2:0] count);
  initial count = 3'b000;
  always @(posedge clk) begin
    if (dir == 1)
      count <= count + 1;
    else
      count <= count - 1;
  end
endmodule
ASynthesis error: blocking assignment used in sequential always block
BRuntime error: count overflows without warning
CNo error, code runs correctly
DSyntax error: missing semicolon
Attempts:
2 left
💡 Hint
Check the type of assignment used inside the always block triggered by clock.
🧠 Conceptual
advanced
2:00remaining
Behavior of up-down counter at overflow
What happens when a 3-bit up-down counter with direction control counts up from 3'b111 (decimal 7) and increments once more?
ASimulation error due to overflow
BCount stays at 3'b111 (saturates)
CCount becomes 3'b1000 (4 bits)
DCount wraps around to 3'b000
Attempts:
2 left
💡 Hint
Think about how binary counters behave with fixed bit width.
📝 Syntax
expert
2:00remaining
Identify the syntax error in this up-down counter code
Which option correctly fixes the syntax error in this Verilog code snippet?
Verilog
always @(posedge clk) begin
  if (dir == 1)
    count <= count + 1
  else
    count <= count - 1;
end
AChange 'begin' to 'begin:'
BReplace '==' with '=' in the if condition
CAdd semicolon after 'count <= count + 1;'
DRemove 'else' keyword
Attempts:
2 left
💡 Hint
Check statement termination inside always block.