Occasionally writing some FPGA-targeted Verilog code at my job, I often need to drive output signals high during one clock cycle exactly. Sometimes I use the following “trick“ to achieve this (let me show a simple example):
module SignalEvery10thClock(
input clock,
output reg signal);
reg [3:0] count = 0;
always @(posedge clock) begin
// The trick.
if (signal)
signal <= 0;
if (count != 10)
count <= count + 1'b1;
else begin
count <= 0;
signal <= 1;
end
end
endmodule
The code like the above seems to synthesize and work just fine (at least, in Synplify and Verilator). However, I don't have a clear understanding why it even compiles successfully: the assignment to signal
seems ambiguous in the case when signal == 1
and count == 10
.
What this ambiguity synthesizes into?
Can the synthesizer deduce that the case when both signal == 1
and count == 10
are true isn't actually possible?
Is writing code like this considered good or bad practice?
What is the proper way to reset the output signal once next clock arrives?