Reading files from Verilog models Introduction This guide describes how you can read files in a Verilog model using a set of system functions which are based on the C stdio package. With these system functions you can perform file input directly in Verilog models without having to learn C or the PLI. You must know how to load data before you can use it to train a machine learning model. When starting out, it is a good idea to stick with small in-memory datasets using standard file formats like comma separated value (.csv). In this tutorial you will discover how to load your data in Python from.
Active4 years, 1 month ago
I have converted an image file into hex file which has R,G,B and alpha values in multiple columns. For example :
3c 48 36 ff 1d 2b 19 ff 08 18 06 ff 08 17 05 ff14 1f 0d ff 1b 22 11 ff 1a 1f 0e ff 1a 1b 0b ff1d 1a 0b ff 20 1a 0b ff 23 1a 0c ff 23 1c 0d ff24 1d 0e ff 24 1d 0e ff 21 1c 0d ff 23 1c 0d ff1f 1a 0b ff 1e 19 0a ff 1c 19 0a ff 1e 1a 0b ff20 1a 0b ff 24 18 0b ff 23 18 0a ff 21 18 0a ff
Now I want a way to read and store this data as a collection of 32 bits( i.e. 4bytes). If I do not know how bytes are present in the file (since file can be of any size), how can I achieve this?
ssgrssgr
1 Answer
If you have a file in that given format, you can use the
$readmemh
system call to take those values and put them in a Verilog array:That should get you your image as a Verilog array, with each element being 32-bits long.
(Note that smaller files will leave a bunch of
'x
values in the array, so you can check for this to determine length if needed)UnnUnn
Not the answer you're looking for? Browse other questions tagged verilogmodelsim or ask your own question.
Active19 days ago
So I'm creating a large FIR filter in Verilog, it has 256 taps. So I need 256 coefficients. I want to try and make my code as modular as possible so I wonder if there's a way to create another external file to the FIR module which contains the values for the coefficients? Currently the only I know to assign values to an array in Verilog is like the following:
But when you have 256 values to assign this is a very long process manually organising the code, even with Find/Replace you can only do so much. What I want is the ability to assign values to arrays like you can in System Verilog:
I don't want to use System Verilog as it isn't as widely used. Can anyone help?
George WallerGeorge Waller2422 gold badges44 silver badges1212 bronze badges
1 Answer
If the values (coefficients) is saved in an external file (for example 'file.txt'), you can use system functions in simulation (
$fscanf
) to read the values from the file and wirte them on the datafile
(datafile
is an array).In the following code I assumed that you have 256 values is saved in the external file ('file.txt') and I tried to read the values 256 times from 'file.txt' and write them on the
datafile
:As toolic said, system functions are not synthesizable. It's for simulation. If you want to write a synthesizable code, I suggest the following way :
AmirAmir