Given this Verilog pulse generator module, what is the width of the output pulse pulse_out in clock cycles?
module pulse_gen(input clk, input trigger, output reg pulse_out); reg [3:0] count; always @(posedge clk) begin if (trigger) count <= 4'd5; else if (count != 0) count <= count - 1; pulse_out <= (count != 0); end endmodule
Count starts at 5 and decrements each clock cycle while pulse_out is high.
The counter is set to 5 when triggered and counts down to 0. The pulse_out stays high while count is not zero, so it lasts 5 clock cycles.
In a pulse generator module, why do we use always @(posedge clk) instead of always @(clk)?
Think about how synchronous digital circuits work with clock edges.
Using posedge clk triggers the block only on the rising edge, which is standard for synchronous designs to avoid glitches and ensure predictable timing.
Consider this Verilog code snippet for a pulse generator:
module pulse_gen(input clk, input trigger, output reg pulse_out);
reg [2:0] count;
always @(posedge clk) begin
if (trigger) count <= 3'd4;
else if (count > 0) count <= count - 1;
pulse_out <= (count > 0);
end
endmoduleWhat is the length of the pulse generated after trigger is asserted?
Trace the count values and when pulse_out is high.
After the trigger clock (where count <= 4 but pulse_out uses prior count > 0 which is false), pulse_out goes high for the next four cycles: count=4 (high, dec to 3), 3 (high, to 2), 2 (high, to 1), 1 (high, to 0).
Identify which code snippet will cause a syntax error when synthesizing a pulse generator in Verilog.
Look carefully for missing punctuation in the code.
Option B is missing a semicolon after count <= 4'd5, causing a syntax error.
You want to design a Verilog module that outputs a single pulse of a width defined by a parameter PULSE_WIDTH when a trigger input goes high. Which code snippet correctly implements this behavior?
Check how the count is initialized and how pulse_out is assigned to get the exact pulse width.
Option A correctly sets count to PULSE_WIDTH and decrements while count > 0, with pulse_out high while count > 0, producing a pulse of exactly PULSE_WIDTH clock cycles.
