Is it worth it reading SystemVerilog LRM

In reply to Neddy:

Hi this is a description on how to read from binary files. It has some examples on how to use EOF. I hope it helps.

Below the example for reading from a binary file with systemverilog.

As shown in IEEE SV Standard documentation, the "nchar_code" will return the number of bytes/chars read. In case EOF have been already reached on last read this number will be zero.
Please, notice that "nchar_code" can be zero but EOF has not been reached, this happens if you have spaces or returns at the end of the data file.

You can control the number of bytes to be read with the $fread function call ("nchar_code"). This is done with the type definition of the "data_write_temp". If the "data_write_temp" variable is 16bits long then it will read 16bits each time the $fread is called. In case, "data_write_temp" is 32bits as in the example, the $fread will read nchar_code=4bytes(32bits). You can also define an array and the $fread function will try to fill that array.

Lets define a multidimensional array mem.

In the example word contents, wzyx,

-w shows the start of the word -z corresponds to words of the [0:2] dimension (3 blocks). -y corresponds to words of the [0:4] dimension (5 rows). -x corresponds to words of the [5:8] dimension (4 columns).

The file will be structure as below (notice @z shows the z dimension blocks):

@0 w005 w006 w007 w008 w015 w016 w017 w018 w025 w026 w027 w028 w035 w036 w037 w038 w045 w046 w047 w048 @1 w105 w106 w107 w108 w115 w116 w117 w118 w125 w126 w127 w128 w135 w136 w137 w138 w145 w146 w147 w148 @2 w205 w206 w207 w208 w215 w216 w217 w218 w225 w226 w227 w228 w235 w236 w237 w238 w245 w246 w247 w248

In the previous structure, the numbers shows the index of each dimension.
e.g. w048 means, the word w (32bits) value on index z =0, index y= 4 and index x= 8.

Now, you have many ways to read this.
You can read all in a **single shot** using the type "mem" declared above, or you can do a while loop until EOF reading pieces of 32bits using a "data_write_temp" variable of 32bits. The loop is interesting if you want to do something some checks for every word piece and you are not interested having a memory value.

In case multidimensional array / single shot read is chosen, then you can either use $fread or use an specific function $readmemh defined in SV standard.


is equivalent to

The $readmemh spare you the need to open/close the file.

If you use $fread for one shot read

logic[31:0] mem [0:2][0:4][5:8]; register_init_id = $fopen("mem.data","rb")