Skip to content

Commit

Permalink
Support oob indices in addressing embed logic
Browse files Browse the repository at this point in the history
  • Loading branch information
povik committed Oct 10, 2024
1 parent e7e79c5 commit 8bd04af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/addressing.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,25 @@ struct Addressing {

Signal extract(Signal val, int width)
{
require(expr, raw_signal.is_fully_def());
ast_invariant(expr, raw_signal.is_fully_def());
int offset = raw_signal.as_const().as_int(true) + base_offset;
require(expr, offset >= 0 && offset * stride + width <= val.size());
return val.extract(offset * stride, width);
}

Signal embed(Signal val, int output_len, int stride, RTLIL::State padding)
{
require(expr, raw_signal.is_fully_def());
ast_invariant(expr, raw_signal.is_fully_def());
int offset = raw_signal.as_const().as_int(true) + base_offset;
require(expr, offset >= 0 && offset * stride + val.size() <= output_len);
return {Signal(padding, output_len - offset * stride - val.size()), val, {Signal(padding, offset * stride)}};

Signal ret;
ret.append(Signal(padding, std::clamp(offset * stride, 0, output_len)));
int val_offset = std::clamp(-offset * stride, 0, val.size());
ret.append(val.extract(val_offset, std::clamp(output_len - offset * stride, 0, val.size() - val_offset)));
ret.append(Signal(padding, std::clamp(output_len - offset * stride - val.size(), 0, output_len)));
log_assert(ret.size() == output_len);

return ret;
}
};

Expand Down
10 changes: 10 additions & 0 deletions tests/various/regress.sv
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ always @(*) begin
m[4-:2] <= 0;
end
endmodule

module r4;
logic [1:0] [31:0] data;
logic [1:0] s;

always_comb begin
for (int i = -10; i < 10; i++)
data[s][i+:8] = 0;
end
endmodule

0 comments on commit 8bd04af

Please sign in to comment.