I am getting confuse when coding Verilog which involves the mixture of always block, generate block and instantiation. Appreciate if somebody could help me on this.
block_A is a module which will reset x when rst signal is asserted. I tested block_A and see that x are being reset to 0 when rst signal is asserted. This is fairy straightforward.
module block_A #(parameter N=4) (
input clk, rst,
input signed [N-1:0] w [3:0];
output logic signed [N+2:0] x [3:0]
);
genvar i;
generate
for (i=0; i<=3; i=i+1) begin
always_ff @ (posedge clk, posedge rst) begin
if (rst) x[i] <= 0;
else x[i] <= x_[i];
end
end
endgenerate
However, when I try to instantiate multiple block_A into the top module using generate block, I observe a very strange scenario. x[6] and x[7] are not being reset to 0. Am I doing it in the wrong way? Should I create another always_ff block and put this generate block inside it? Meaning, we need to propagate the sensitivity list? It doesn't look sensible to put the generate block under the existing always_ff block as it is generated in loop fashion.
module top #(parameter N=4) (
input clk, rst,
input signed [N-1:0] w [15:0],
output logic signed [N+5:0] y [15:0]
};
logic signed [N+2:0] x [15:0];
genvar i;
generate
for (i=0; i<=15; i=i+1) begin: pipeline_y
always_ff @ (posedge clk, posedge rst) begin
if (rst) y[i] <= 0;
else y[i] <= y_[i];
end
end
endgenerate
genvar j;
generate
for (j=0; j<=15; j=j+4) begin: my_block
block_A #(.N(N)) my_block_A(.w(w[j+3:j]),.x(x[j+3:j]),.rst(rst),.clk(clk));
end
endgenerate
clk. Also 'x' itself can glitch due to a race between clk an rst and cause issues outside the code snipped you provided. - Serge