Vivado Sim Error: "root scope declaration is not allowed in verilog 95/2K mode"

Vivado Sim Error: "root scope declaration is not allowed in verilog 95/2K mode"

当我在 Xilinx Vivado 2016.4 中模拟我的顶层模块时,我收到了奇怪的错误:

ERROR: [VRFC 10-1342] root scope declaration is not allowed in verilog 95/2K mode [<...>/header.vh]

我正在使用内置的 Vivado 模拟器,并指定了 Verilog 2001。我的 header.vh 如下所示:

`ifndef _header_vh_
`define _header_vh_

    function integer clog2;
        input integer value;
        begin 
            value = value - 1;
            for (clog2 = 0; value > 0; clog2 = clog2 + 1)
                value = value >> 1;
        end 
    endfunction

`endif

此错误的出现是因为函数 clog2 的作用域实际上设置为 root(因为它未在模块中声明);此范围声明在 Verilog 2001 中是不允许的,但在更高版本(例如 SystemVerilog)中是允许的。切换到 SystemVerilog 可以解决问题(但不推荐),但为函数引入模块包装器就足够了。

`ifndef _header_vh_
`define _header_vh_

module header();
    function integer clog2;
        input integer value;
        begin 
            value = value - 1;
            for (clog2 = 0; value > 0; clog2 = clog2 + 1)
                value = value >> 1;
        end 
    endfunction
endmodule

`endif