简体   繁体   English

如何将变量值传递给SystemVerilog中的宏?

[英]How to pass a variable value to a macro in SystemVerilog?

I think the question sums it up pretty well as to what I want: passing the value of a variable to a macro in SystemVerilog. 我认为这个问题很好地总结了我想要的东西:将变量的值传递给SystemVerilog中的宏。

For example, what I want: Say, there are 4 signals by the name of abc_X_def and I want to initialize all of them to 0. So, without macros: 例如,我想要的:说,有4个信号的名称为abc_X_def,我想将它们全部初始化为0.所以,没有宏:

abc_0_def = 4'b0000;
abc_1_def = 4'b0000;
abc_2_def = 4'b0000;
abc_3_def = 4'b0000;

Now, the code that I have written is having a problem: 现在,我写的代码有一个问题:

`define set_value(bit) abc_``bit``_def = 4'b0000

for (int i = 0; i < 4; i++) begin
  `set_value(i);
end

The error is that it's trying to find the signal abc_i_def which is obviously wrong. 错误是它试图找到信号abc_i_def,这显然是错误的。 Just wondering if it's possible to pass the actual value of the variable 'i' to the macro. 只是想知道是否可以将变量'i'的实际值传递给宏。

The preprocessor directives are evaluated by the preprocessor and modify the code that is presented to the compiler. 预处理器指令由预处理器评估,并修改呈现给编译器的代码。

The for loop is a verilog construct that is evaluated by the compiler. for循环是由编译器评估的verilog构造。

So your preprocessor is not evaluating the for loop. 所以你的预处理器没有评估for循环。 It sees: 它看到:

`define `set_value(bit) abc_``bit``_def = 4'b0000

[some verilog]
   `set_value(i);
[some verilog]

So 'i' is just i. 所以'我'就是我。 It doesn't become a number until compilation. 在编译之前它不会成为数字。

Why don't you use local params with generate, so the local params are created in a loop at elaboration as the for loop is unrolled? 为什么不使用带有生成的本地参数,所以当for循环展开时,本地参数是在详细说明的循环中创建的?

This is one of the many places that macros are a problem. 这是宏是问题的众多地方之一。 Generate is a problem in other places (like when you want to control port lists). 生成是其他地方的问题(例如,当您想要控制端口列表时)。


I dug into this a bit more. 我再挖一下这个。 Parameters and local parameters inside a generate are created a localparams in the scope of the generate. 生成中的参数和局部参数是在generate的范围内创建的localparams。 See here: System Verilog parameters in generate block . 请参见此处: 生成块中的系统Verilog参数 I had to get back to work before I could test it. 在我测试它之前,我不得不重新开始工作。

I would just use code and populate an array. 我只是使用代码并填充数组。 This compiles in VCS: 这在VCS中编译:

module veritest  
    #(  
    parameter   MAX_X = 5,  
                MAX_Y = 3,  
                MAX_Z = 2  
    )  
    (); // empty port list  

logic [4:0] abc_def[1:MAX_X][1:MAX_Y][1:MAX_Z];  

always @*  
begin  
for (integer z=1; z<(MAX_X+1);z=z+1)  
   for (integer y=1; y<(MAX_Y+1);y=y+1)  
       for (integer x=1; x<(MAX_X+1);x=x+1)  
       begin  
            abc_def[x][y][z] = 4'b0000;  
       end  
end  
endmodule  

Since you said the naming conventions is out of your control, you should consider using another tool to generate the verilog for you. 既然您说命名约定不受您的控制,您应该考虑使用其他工具为您生成verilog。

You can have the code generated by your preferred programming then use an `include statement in your verilog file. 您可以使用首选编程生成的代码,然后在verilog文件中使用`include语句。 Or you can go with an embedded route. 或者你可以使用嵌入式路线。

Concept is the same, just a difference in embedded language and tool used for conversion. 概念是相同的,只是嵌入式语言和用于转换的工具的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM