verilator/test_regress/t/t_gen_class.v

70 lines
1.9 KiB
Systemverilog
Raw Normal View History

2025-01-05 23:10:04 +01:00
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2025 Wilson Snyder
2025-01-05 23:10:04 +01:00
// SPDX-License-Identifier: CC0-1.0
// verilog_format: off
2025-01-05 23:10:04 +01:00
`define stop $stop
`define checkh(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0x exp=%0x (%s !== %s)\n", `__FILE__,`__LINE__, (gotv), (expv), `"gotv`", `"expv`"); `stop; end while(0);
`define checks(gotv,expv) do if ((gotv) != (expv)) begin $write("%%Error: %s:%0d: got='%s' exp='%s'\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
// verilog_format: on
2025-01-05 23:10:04 +01:00
module Child;
2026-03-08 23:26:40 +01:00
int ch_value;
2025-01-05 23:10:04 +01:00
endmodule
module Parent;
2026-03-08 23:26:40 +01:00
for (genvar i = 0; i < 10; i++) begin : gen_child
Child child ();
end
2025-01-05 23:10:04 +01:00
endmodule
module t;
2026-03-08 23:26:40 +01:00
Parent parent ();
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
virtual class ChildAgentBase;
pure virtual task preload(int value);
pure virtual function string name();
endclass
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
ChildAgentBase child_agents[10];
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
for (genvar i = 0; i < 10; i++) begin : gfor
class ChildAgent extends ChildAgentBase;
task automatic preload(int value);
parent.gen_child[i].child.ch_value = value;
endtask
function string name();
return $sformatf("%m");
endfunction
endclass
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
ChildAgent agent = new();
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
initial child_agents[i] = agent;
end
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
task automatic preload_children;
for (int i = 0; i < 10; i++) begin
child_agents[i].preload(i);
end
endtask
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
string s;
2025-01-05 23:10:04 +01:00
2026-03-08 23:26:40 +01:00
initial begin
#1; // Ensure all class instances are initialized
preload_children();
`checkh(parent.gen_child[3].child.ch_value, 3);
`checkh(parent.gen_child[8].child.ch_value, 8);
2025-01-05 23:10:04 +01:00
`ifdef VERILATOR
2026-03-08 23:26:40 +01:00
// Some legal examples "t.gfor[4].\ChildAgent::name", "t.gfor[4].ChildAgent.name"
`checks(child_agents[4].name(), "t.gfor[4].ChildAgent.name");
2025-01-05 23:10:04 +01:00
`endif
2026-03-08 23:26:40 +01:00
$write("*-* All Finished *-*\n");
$finish;
end
2025-01-05 23:10:04 +01:00
endmodule