Challenge - 5 Problems
Verilog Synthesis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Verilog simulation snippet?
Consider the following Verilog code snippet. What value will the register
count hold after 5 clock cycles?module counter(input clk, input rst, output reg [3:0] count);
always @(posedge clk or posedge rst) begin
if (rst)
count <= 0;
else
count <= count + 1;
end
endmoduleVerilog
module testbench(); reg clk = 0; reg rst = 1; wire [3:0] count; counter uut(clk, rst, count); always #5 clk = ~clk; initial begin #10 rst = 0; #50 $display("Count = %d", count); $finish; end endmodule
Attempts:
2 left
💡 Hint
Remember that the counter increments on the rising edge of clk after reset is deasserted.
✗ Incorrect
The reset is active for the first 10 time units, so the counter starts counting from 0 after that. The clock period is 10 time units, so after 5 clock cycles (50 time units), the count will be 5 because the first increment happens at the first rising edge after reset is released.
🧠 Conceptual
intermediateWhich coding style is best for synthesizable combinational logic?
You want to write combinational logic in Verilog that is synthesizable and avoids unintended latches. Which of the following coding styles is best practice?
Attempts:
2 left
💡 Hint
Combinational logic should react to any input change and avoid memory elements.
✗ Incorrect
Using
always @(*) with blocking assignments and assigning all outputs in every possible branch ensures combinational logic without latches. Using clocked blocks or initial blocks is for sequential or simulation-only code.🔧 Debug
advancedIdentify the synthesis issue in this Verilog code
This Verilog code is intended to implement a 2-to-1 multiplexer. However, synthesis tools infer unintended latches. What is the cause?
Verilog
module mux(input sel, input a, input b, output reg y); always @(*) begin if (sel == 1'b1) y = a; // Missing else branch end endmodule
Attempts:
2 left
💡 Hint
Think about what happens when sel is 0.
✗ Incorrect
Because y is not assigned a value when sel is 0, the synthesis tool infers a latch to hold the previous value. To avoid this, assign y in all branches.
📝 Syntax
advancedWhich Verilog code snippet correctly implements a synchronous reset flip-flop?
Select the code snippet that correctly implements a positive-edge triggered flip-flop with synchronous reset.
Attempts:
2 left
💡 Hint
Synchronous reset means reset is checked only on clock edge.
✗ Incorrect
Option A uses only posedge clk in sensitivity list and checks reset inside the block, making reset synchronous. Option A uses asynchronous reset. Option A uses negedge clk which is uncommon. Option A has inverted reset logic.
🚀 Application
expertHow many flip-flops are inferred by this Verilog code?
Given the following Verilog code, how many flip-flops will the synthesis tool infer?
Verilog
module regfile(input clk, input rst, input [1:0] addr, input d, output reg q); reg [3:0] mem; always @(posedge clk or posedge rst) begin if (rst) mem <= 4'b0000; else mem[addr] <= d; end always @(*) begin q = mem[addr]; end endmodule
Attempts:
2 left
💡 Hint
Each bit of mem is a register element updated on clock edge.
✗ Incorrect
The 4-bit register mem is updated on clock edge, so 4 flip-flops are inferred. The output q is combinationally assigned from mem.
