I have tried to do it this way:
module encoder
#(
parameter WIDTH = 4
)
(
input wire [WIDTH-1: 0] in,
output reg [$clog2(WIDTH)-1: 0] out
);
genvar i;
generate
for (i = 0;i < WIDTH;i = i+1)
begin :gen_block
always @*
begin
if (in[i]==1'b1)
out = i;
end
end
endgenerate
endmodule
It works well in the simulation, but I'm not sure if this can be synthesized. The generate block will produce multiple copies of the always block, and I'm guessing there will be a race condition between the previous value of out and the updated value of out.
Am I correct about assuming that this code can't be synthesized? If so, What do I change to make it synthesize correctly?