How can i extract strings and values using sscanf

hi,
I have a string in below format and i would like to read the final value of the string from the system verilog. can someone help me how to do it using sscanf or is there any other way to do it easily?

// sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD [2:0] 0x1

i just want the value 0x1 to be read and assign it to some variable. how can i do that?

i tried this way:

bit [31:0] value;
string str1= “sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD”
int fd = $fopen (“rndcfg.cfg”, “r”);
if (fd == 0) begin
$display (“ERROR: opening file”);
$finish;
end
while (!$feof(fd)) begin
$fgets (line, fd); //Get entire line
if (match(line,str1)) begin
$fwrite(rv,“%s\n”,line);
$sscanf(line, “%s, %s, %s, %x”, first, hierarchy, nob, &value);
$display(“%s, %s, %s, %x”,first, hierarchy, nob, value);
end
end

in the above code match is my own function. (which is working totally fine)

is there a way i can get 0x1 to the variable value. – the above code doesnt seems to be working. when i am printing value… its not showing 0x1.

can someone help me here?

regards
Srikanth

In reply to VerificationLearn:

It would be very helpful if you post a complete, self-contained example which can be run. This ensures that others see exactly what the issue is.

Are you trying to read a string name of a register and then assign a value to that register? If so, you can not do that in SystemVerilog. You would have to compare the string to a known value and then assign the register appropriately.

If this is not what you want to accomplish, you will have to explain your issue more clearly.

In reply to cgales:

Hi cgales,
Sure, let me explain my requirement.

i have a file which would contain the below format:
-sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0 0x00000149
// sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD [2:0] 0x1
// sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD1 [5:3] 0x1
// sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD2 [7:6] 0x1
// sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD3 [31:8] 0x1

now my requirement is to read this file and get the value of either the REG0 which is 0x00000149 or if i want i should be able to get FIELD1 value which 0x1.

i would simply call from my function as GetValue(“path”)

Ex: GetValue(“sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0”); → this should return the entire value of the register which is 0x00000149
or
GetValue(“sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD1”); → this should return the value of the field which is 0x1.

Currently i am able to open the file and match the string in the file and get the entire line, but not able to get the value of the reg or field.

Hope i have explained it well.

let me know if need more details?

regards
Srikanth

In reply to VerificationLearn:

SystemVerilog does not allow access to identifiers via a string name. There are several options

  1. Convert your file to legal SystemVerilog syntax in the form of a list of assignments
initial begin
sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0 = 'h00000149;
sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD [2:0] = 'h1;
...
  1. Create a mapping between the string and the identifiers they represent
case({path, nob})
"sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0":    sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0 = value;
"sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD":
sys.hir00.mbo01.fir_hid.nn0.hero0.nd.gb.crappy.REG0.FIELD = value;

  1. If this is part of the UVM register model, it already provides a string to register name lookup.
  2. You can tool specific commands or the PLI to access identifiers using a string. This forum is not for discussing tool specific issues, and using the PLI is beyond the scope of what we can get into in this forum