'$readmemh doesn't read the values from data file

I'm using readmemh like:

reg [11:0] rom [0:121];

initial $readmemh("x.data", rom);

My x.data file looks like:

line 1: 1 
line 2: 10
. 
. 
. 
line 118: 1110101 
line 119: 1110110 
line 120: 1110111

When I try to use rom[20] during the simulation, I see XXX value. Also, I have checked all rom data values during the simulation, and I saw XXX,XXX,..,XXX

I'm not sure that I used $readmemh in proper way.



Solution 1:[1]

When I use your code to read the file, I get warning messages with 2 different simulators. In both cases, the simulator fails to read the file and load the data into the rom variable. If I add the following code:

integer i;
initial for (i=0; i<=121; i=i+1) $displayb(rom[i]);

I see x for all locations in rom. You declared rom as a reg type, and reg types default to x.

The problem is that Line 1:, Line 2:, etc., are incorrect syntax. You need to remove them from the x.data file. Your file should look something like this:

1      
10     
1110101
1110110
1110111

However, I still get simulator warnings because the simulator interprets 1110101 as a hexadecimal value. Since this value is too large for the 12-bit variable, the simulator does not load it into rom, leaving the value as the default x.

My guess is that your data is really binary format instead of hex. In that case, use $readmemb instead of $readmemh:

initial $readmemb("x.data", rom);

When I make that change, I no longer see x.

Refer to IEEE Std 1800-2017, section 21.4 Loading memory array data from a file for detailed syntax.

Solution 2:[2]

Try using the full path to your file. Likely your simulator is not in the same directory as your file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ralph