Add regression tests for soft packed unions

Check that soft packed unions can have members with different widths.
Check that the `soft` qualifier implies `packed` and that nested soft
packed unions use the same representation recursively.

Also check that member bits are right-justified and that assignments to
narrower members leave the MSBs beyond the member bits unchanged. Check
that soft packed unions reject default member values.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2026-05-16 08:39:20 -07:00
parent fb3be420b4
commit f8e9384689
5 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,81 @@
// Check that soft packed unions can have members with different widths.
module test;
bit failed = 1'b0;
typedef union soft packed {
logic [31:0] w;
logic [15:0] h;
logic [7:0] b;
} data_t;
typedef union soft {
logic [31:0] w;
logic [7:0] b;
} implicit_packed_t;
typedef union soft packed {
logic [15:0] w;
union soft {
logic [7:0] b;
logic [3:0] n;
} part;
} nested_t;
data_t data;
implicit_packed_t implicit_packed;
nested_t nested;
`define check(val, exp) do \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %0h, got %0h", `__LINE__, \
`"val`", exp, val); \
failed = 1'b1; \
end \
while(0)
initial begin
data.w = 32'h12345678;
`check($bits(data), 32);
`check($bits(data_t), 32);
`check(data.w, 32'h12345678);
`check(data.h, 16'h5678);
`check(data.b, 8'h78);
data.h = 16'habcd;
`check(data.w, 32'h1234abcd);
`check(data.h, 16'habcd);
`check(data.b, 8'hcd);
data.b = 8'hef;
`check(data.w, 32'h1234abef);
`check(data.h, 16'habef);
`check(data.b, 8'hef);
implicit_packed.w = 32'hf0e1d2c3;
`check($bits(implicit_packed_t), 32);
`check(implicit_packed.b, 8'hc3);
implicit_packed.b = 8'h34;
`check(implicit_packed.w, 32'hf0e1d234);
nested.w = 16'hbeef;
`check($bits(nested_t), 16);
`check($bits(nested.part), 8);
`check(nested.part.b, 8'hef);
`check(nested.part.n, 4'hf);
nested.part.n = 4'ha;
`check(nested.w, 16'hbeea);
`check(nested.part.b, 8'hea);
nested.part.b = 8'h55;
`check(nested.w, 16'hbe55);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,12 @@
// Check that soft packed unions can not have default member values.
module test;
typedef union soft {
logic [31:0] w = 32'h1;
logic [7:0] b;
} data_t;
data_t data;
endmodule

View File

@ -339,6 +339,8 @@ sv_package_lifetime vvp_tests/sv_package_lifetime.json
sv_package_lifetime_fail vvp_tests/sv_package_lifetime_fail.json
sv_parameter_type vvp_tests/sv_parameter_type.json
sv_queue_assign_op vvp_tests/sv_queue_assign_op.json
sv_soft_packed_union vvp_tests/sv_soft_packed_union.json
sv_soft_packed_union_fail1 vvp_tests/sv_soft_packed_union_fail1.json
sv_super_member_fail vvp_tests/sv_super_member_fail.json
sv_wildcard_import8 vvp_tests/sv_wildcard_import8.json
sdf_header vvp_tests/sdf_header.json

View File

@ -0,0 +1,5 @@
{
"type" : "normal",
"source" : "sv_soft_packed_union.v",
"iverilog-args" : [ "-g2023" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "sv_soft_packed_union_fail1.v",
"iverilog-args" : [ "-g2023" ]
}