The Designer's Guide Community Forum
https://designers-guide.org/forum/YaBB.pl
Design Languages >> Verilog-AMS >> Reading strings from a file in VerilogA
https://designers-guide.org/forum/YaBB.pl?num=1376423154

Message started by luxmee on Aug 13th, 2013, 12:45pm

Title: Reading strings from a file in VerilogA
Post by luxmee on Aug 13th, 2013, 12:45pm

I have a text file which looks like this:
x=1,y=2,z=3 'This is a comment
x=3,y=4,z=5
x=6,y=7,z=8 'Last line

I'm trying to read the file in VerilogA(values of x,y & z) and I want to ignore the comments.

I tried using the fscanf function
file = $fopen("myfile.txt","r");
$fscanf(file,"x=%d,y=%d,z=%d%s",int1,int2,int3,str1);

But this doesn't work, it works as far the syntax goes but when I run my testbench, it gives me a fatal error saying "Format string in $fscanf doesn't match input file/string".

I also tried a combination of fgets and then sscanf but that doesn't work either.

Does anyone know how to do this?

Title: Re: Reading strings from a file in VerilogA
Post by Marq Kole on Aug 15th, 2013, 7:38am

I've been able to get $fscanf to work but it is very picky - especially strings are a nuisance. Apparently a string is any character string delimited by whitespace, so if your comments include whitespace $fscanf won't work. On the other hand, the amount of whitespace does not matter - one space in the format string can match any number of spaces/tabs in the actual file, as long as it relates to a single line.

What I've found is that $fscanf reads complete lines and then tries to match the line to the format string.

Please find below a Verilog-A module definition and a text input file that at least works with the analog simulators of a few different vendors. It is a file-driven sine-wave source. All lines in the input file have to be present and have to be in the given order.


Code:
`include "disciplines.vams"
`include "constants.vams"

module vfile (out, gnd);

 parameter string fname = "vfile.ctrl";

 inout out, gnd;
 electrical out, gnd;

 real amp, offset, freq, phase;
 integer fp, n_items;

 analog begin
   @(initial_step) begin

     fp = $fopen(fname, "r");

`ifdef __TIBURONDA__
     n_items = n_items + $fscanf(fp, "Amp = %g\n", amp);
     n_items = n_items + $fscanf(fp, "Offset = %g\n", offset);
     n_items = n_items + $fscanf(fp, "Freq = %g\n", freq);
     n_items = n_items + $fscanf(fp, "Phase = %g\n", phase);
`else
     $fscanf(fp, "Amp = %g", amp);
     if (amp) n_items = n_items + 1;
     $fscanf(fp, "Offset = %g", offset);
     if (offset) n_items = n_items + 1;
     $fscanf(fp, "Freq = %g", freq);
     if (freq) n_items = n_items + 1;
     $fscanf(fp, "Phase = %g", phase);
     if (phase) n_items = n_items + 1;
`endif

     $display("Read %d specifications", n_items);
     $display("  Amp    = %g", amp);
     $display("  Offset = %g", offset);
     $display("  Freq   = %g", freq);
     $display("  Phase  = %g", phase);

     $fclose(fp);

   end // initial_step

   V(out, gnd) <+ offset + amp * sin(`M_TWO_PI * freq * $abstime + `M_PI * phase / 180);
 end

endmodule


And the file that is read through these $fscanf call has these contents:


Code:
Amp = 0.5
Offset = 0.5
Freq = 1.2e+06
Phase = 45


Hope this helps.

Cheers,
Marq

The Designer's Guide Community Forum » Powered by YaBB 2.2.2!
YaBB © 2000-2008. All Rights Reserved.