Tuesday, May 12, 2020

Parsing file in systemverilog

We often need to read file generated by tools and use the data in systemverilog task/function to decide pass or fail for many verification tasks.

Systemverilog is not a language that is built for text parsing but it does provide some basic system functions to play with. Key is to read the data which is simple and easy to parse. one way is to pre process the data in external perl script which then be read by systemverilog.

Here is the sample file where the real numbers at the end of the file are to be extracted and result to be used in the task:

sample_checks_184ms 47166.67
sample_checks_370ms 41333.33

Below system tasks are used
$fopen()   Open file for reading
$feof()      Identify end of file
$fgets()    read the line from file
$fscanf() parse a line


Sample Systemverilog Task for parsing :
task parse_file_sv();
int samplefile;
string line_from_file, sub1;
real sub2;
string srcdir;
if($value$plusargs("SRCDIR=%s", srcdir))
`uvm_info(get_name(), $sformatf("+SRCDIR:%s",srcdir),UVM_MEDIUM)
else
`uvm_error(get_name(), $sformatf("+SRCDIR is not passed"));
samplefile=$fopen({srcdir,"/log.txt"},"r");
//check if path of logfile path is valid
if(!samplefile)
`uvm_error(get_name(),{srcdir,"/log.txt missing"})
while(!$feof(samplefile) && $fgets(line_from_file, samplefile)) begin
void'($sscanf(line_from_file, "%s %f\n", sub1, sub2));
`uvm_info(get_name(), $sformatf("sub1=%s,sub2=%0f",sub1,sub2),UVM_MEDIUM)
//operate on collected data or store in stack here
end
$fclose(samplefile);
endtask


A snippet of task result:
sub1=sample_checks_184ms,sub2=47166.67
sub1=sample_checks_370ms,sub2=41333.33