'readmemh'没有正确读取内存文件?

时间:2014-12-13 20:23:30

标签: verilog hdl

我在verilog中编写了以下testbench,它编写了一个文件然后再读取值。

  // Verilog Test Fixture Template

  `timescale 1 ns / 1 ps

  module Read_And_Write_File;

  /*Add signals used for verification of the values written in SRAM*/
    integer   handle, channels,index;
   reg [15:0] memory [22:0]; 
    reg [22:0] mem_idx;
    reg [15:0] val;

initial begin
  /*Write the memory file 'SRAM.dat'using the values that are supposed to be in the SRAM after the simulation*/
            handle = $fopen("SRAM.dat");
            channels = handle | 1;
         $display("Generating contents of file SRAM.dat");
         $fdisplay(channels, "@1");
            val = 16'h2121;
            for(index = 0; index < 60; index = index + 1) 
                begin
                    $fdisplay(channels, "%h", val);
                    val=val+16'h1;
            end

         $fclose(handle);

        /*Read the values in the file 'SRAM.dat' and compare the values with the values that were actually written in the SRAM*/
            $readmemh("SRAM.dat", memory);

         $display("\nContents of memory array");
            mem_idx=23'h1;
         for(index = 0; index < 60; index = index + 1)
                begin
              $display("The Value is:%h and index in hex %h",memory[mem_idx],mem_idx);
                  mem_idx=mem_idx+23'h1;
                end
end
  endmodule

写文件似乎没问题。但是当用'readmemh'阅读时,我得到如下错误:

  

错误:数据文件SRAM.dat中指定的单词太多

阅读23个值后,我也没有得到正确的值:

值为:2135,索引以十六进制000015为准 值为:2136,索引为十六进制000016
值为:xxxx和十六进制000017的索引
值为:xxxx和十六进制000018中的索引

有关如何回读正确值并修复错误的任何帮助表示赞赏。模拟在Isim进行。

1 个答案:

答案 0 :(得分:0)

没关系,我发现了我的错误。 reg [15:0] memory [22:0]应该是reg [15:0] memory [60:0],因为它指的是内存中的总字数。我虽然把它解释为代表地址所需的位。