0

In Verilog, I can find two system functions to read data from text file. One is $readmemb/$readmemh, other is $fscanf. I am confused between what is difference of the two. Can I simply always use $readmemb and forget about $fscanf?

My understanding is $readmemb is used to initialize memory but that way it can initialize any variable. For example, I have text file with stream of 0s and 1s and I want to read it and store it and then feed them serially into shift register 1 bit every clock.

reg [63:0] seq_input;
$readmemb("pattern.in", seq_input);

I think think this will put streams of zero and one into seq_input at one go and then I can use delay to feed bits from this to DUT. Why would I use $fscanf and what would be the difference?

toolic
  • 57,801
  • 17
  • 75
  • 117

1 Answers1

2

The difference between $readmemb/h and $fscanf is how the file is parsed. $fscanf presents the typical C-style interface to a file (mostly) and let's you parse it however you want with format strings. $readmemb/h takes in a file with a very well-defined structure and does all of the parsing for you.

So, you would probably just use $readmemb in your case and forget handling the parsing yourself, but you could use $fscanf for the job if you really wanted to (with more work in general, you have to open the file first at minimum). Remember though that any of these system call needs to be made from a procedural block, like so:

reg [63:0] seq_input;
...
initial begin
  $readmemb("my_file.b", seq_input);
end

Where your file looks like this in a text editor:

101001000110011...

If you want more on the $readmemb/h format, Ive answered that question here: How to initialize contents of inferred Block RAM (BRAM) in Verilog

Community
  • 1
  • 1
Unn
  • 4,775
  • 18
  • 30