【问题标题】:Easy way to assign values to an array in Verilog?在 Verilog 中为数组赋值的简单方法?
【发布时间】:2015-05-02 09:28:33
【问题描述】:

所以我在 Verilog 中创建了一个大型 FIR 滤波器,它有 256 个抽头。所以我需要256个系数。我想尝试使我的代码尽可能模块化,所以我想知道是否有办法为 FIR 模块创建另一个包含系数值的外部文件?目前我唯一知道在 Verilog 中为数组赋值的方法如下:

reg [15:0] datafile [8];

initial
begin
    datafile[0] = 32768;
    datafile[1] = 37045;
    datafile[2] = 41248;
    datafile[3] = 45307;
    datafile[4] = 49151;
    datafile[5] = 52715;
    datafile[6] = 55938;
    datafile[7] = 58764;
end

但是,当您要分配 256 个值时,手动组织代码是一个非常漫长的过程,即使使用查找/替换,您也只能做这么多。我想要的是能够像在 System Verilog 中那样为数组赋值:

reg [15:0] datafile [8] = '{8468,56472,56874,358,2564,8498,4513,9821};

我不想使用 System Verilog,因为它没有被广泛使用。有人可以帮忙吗?

【问题讨论】:

  • 你需要这个只是为了模拟还是你需要常量数组是可合成的?
  • 我希望可以合成。

标签: arrays verilog


【解决方案1】:

如果值(系数)保存在外部文件中(例如“file.txt”),您可以使用仿真中的系统函数 ($fscanf) 从文件中读取值并将它们写入@ 987654322@(datafile是一个数组)。

在下面的代码中,我假设您有 256 个值保存在外部文件 ('file.txt') 中,我尝试从 'file.txt' 读取值 256 次并将它们写入datafile

module b(clk,reset);
    input clk;
    input reset;
    
    integer fileH; // file handler
    reg [15:0] datafile [0:255];
    reg [7:0] counter;
    
    initial begin
        fileH = $fopen ("file.txt", "r");
    end
    
    always @(posedge clk or posedge reset) begin
        if (reset)
            counter <= 0;
        else begin
            $fscanf (fileH, "%d\n", datafile[counter]);
            counter <= counter + 1;
        end
    end 

endmodule

系统功能不可综合。是用来模拟的。如果你想写一个可综合的代码,我建议如下方式:

assign {datafile[0], datafile[1], ...} = {16'b0, 16'b0, ...};

【讨论】:

  • 谢谢。我假设这在编译时执行?
  • @George waller,我用一个可综合的代码完成了我的回答。
  • @Amir,感谢您的回复。这有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
相关资源
最近更新 更多