计算系统Verilog中模块的实例

发布于 2025-02-01 00:23:02 字数 846 浏览 3 评论 0原文

我正在尝试使用SystemVerilog设计简单的CPU,并且我会使用SystemVerilog设计和模型来模拟设计。我正在尝试设置一些代码来计算特定模块的实例数。我目前正在使用静态变量来实现这一目标,但是它不起作用。我的代码如下。

module testbench;

logic a0, b0, y0;
logic a1, b1, y1;
logic a2, b2, y2;

AND and0(.a(a0), .b(b0), .y(y0));
AND and1(.a(a1), .b(b1), .y(y1));
AND or0(.a(a2), .b(b2), .y(y2));

initial
begin
    $display("AND Gates : ", and0.instance_count);
    $display("OR Gates : ", or0.instance_count);
end

endmodule



module AND(input logic a, b, output logic y);

static int instance_count = 0;

initial
    instance_count++;

and(y, a, b);

endmodule



module OR(input logic a, b, output logic y);

static int instance_count = 0;

initial
    instance_count++;

or(y, a, b);

endmodule

这给出的输出是不正确的门和门的1个实例。我该如何更改代码来解决此问题?除此之外,如果可能的话,可以通过modelsim确定的解决方案可以通过modelsim确定,而我不需要我将任何代码添加到我的模型中,这是理想的解决方案。

I am attempting to design a simple CPU and I ma using SystemVerilog to design and ModelSim to simulate the design. I am trying to set up some code to count the number of instances of a specific module. I am currently using static variables to try to achieve this however it is not working. My code is as follows.

module testbench;

logic a0, b0, y0;
logic a1, b1, y1;
logic a2, b2, y2;

AND and0(.a(a0), .b(b0), .y(y0));
AND and1(.a(a1), .b(b1), .y(y1));
AND or0(.a(a2), .b(b2), .y(y2));

initial
begin
    $display("AND Gates : ", and0.instance_count);
    $display("OR Gates : ", or0.instance_count);
end

endmodule



module AND(input logic a, b, output logic y);

static int instance_count = 0;

initial
    instance_count++;

and(y, a, b);

endmodule



module OR(input logic a, b, output logic y);

static int instance_count = 0;

initial
    instance_count++;

or(y, a, b);

endmodule

The output this gives is 1 instance for both the AND gates and OR gates, which is incorrect. How could I change my code to fix this issue? In addition to this, a solution to this problem which can be determined through modelsim and not require me to add any code to my model would be an ideal solution, if that is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2025-02-08 00:23:02

模块级别上的所有声明已经static,每个模块实例都有单独的变量实例分配。

您可以使用关联阵列创建一个全局软件包。

package inst;
int count[string] = '{default:0};
endpackage

module AND(input logic a, b, output logic y);

initial inst::count["AND"]++;

and(y, a, b);

endmodule

SystemVerilog没有内置的内省,但是是将其构建到您的流程的方式您真的想深入了解VPI细节。

但是,包括Modelsim在内的许多工具具有类似的TCL命令

find instances -r /testbench/* -file instances.txt

,很容易编写单个实例的计数脚本。

All declarations at the module level are already static, and each module instance has a separate allocation of variable instances.

You could create a global package with an associative array.

package inst;
int count[string] = '{default:0};
endpackage

module AND(input logic a, b, output logic y);

initial inst::count["AND"]++;

and(y, a, b);

endmodule

SystemVerilog has no built-in introspection, but there are ways of building it in to your flow if you really want to dive into the VPI details.

But many tools including ModelSim have Tcl commands like

find instances -r /testbench/* -file instances.txt

that would be easy to write a counting script of individual instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文