Differ between binary and hex in $sscanf

  • Thread starter Kenneth Brun Nielsen
  • Start date
K

Kenneth Brun Nielsen

Guest
Is it possible to specify whether a given numeric string should be
interpreted as, say binary or hex type, by $sscanf?

E.g.

readLine = a register containing a string read from file.

if ($sscanf(readLine, "I2CWRITE %s %h", addrName, hexValue) == 2)
begin
// lookup addrName and
// write the hexadecimal sequence to the given address
end
else if ($sscanf(readLine, "I2CWRITE %s %b", addrName, binValue) == 2)
begin
// lookup addrName and
// write the binary sequence to the given address
end
else
$display( "Unknown command");

If readLine is the string "I2CWRITE TESTENABLE 01", $sscanf will
interpret "01" as both binary and hex. Does any syntax exist to
specify what type a given numeric value are of? For instance, if I
could write:
"I2CWRITE TESTENABLE 01b" in order to specify that the numeric value
01 should be interpreted as binary.

Obviously I could invent my own syntax and include it in the $sscanf
format line a'la
if ($sscanf(readLine, "I2CWRITE %s HEX %h", addrName, hexValue) == 2)
//
else if ($sscanf(readLine, "I2CWRITE %s BIN %b", addrName, binValue)
== 2)
, but:
1) I prefer a syntax without whitespaces (so the numeric value has
either a suffix or prefix)
2) I'd rather follow a predefined syntax, if it is already possible in
the $sscanf format.

Best regards,
Kenneth
 
Kenneth,

Is it possible to specify whether a given numeric string should be
interpreted as, say binary or hex type, by $sscanf?
As far as I know there's no built-in way to parse the standard
Verilog number format (things like 4'b1001), which is a pity.
However, you could try something like this:

$sscanf(readLine, "'h%h", val)

and now the line of text is required to contain something
like 'h1001 (the 'h prefix becomes mandatory). I like the
idea that you can enforce the requirement to use something
that is also legal Verilog number syntax.

Dunno if this helps in your specific case.
--
Jonathan Bromley
 
Hi Jonathan,

Is it possible to specify whether a given numeric string should be
interpreted as, say binary or hex type, by $sscanf?

As far as I know there's no built-in way to parse the standard
Verilog number format (things like 4'b1001), which is a pity.
However, you could try something like this:

  $sscanf(readLine, "'h%h", val)
That might be a feasible solution in my case. I will try it soon.

Thanks for your valuable response.

Best regards,
Kenneth
 

Welcome to EDABoard.com

Sponsor

Back
Top