J
Jonathan Bromley
Guest
OK, so SystemVerilog is hyper-cool and object-oriented and stuff.
So here's me trying to read in some text and decide what kind of
value that text represents - so, for example, I'd like to be able
to read the text
100
and decide that it's an integer,
-34.5e4
and decide it's a real,
supercalifragilisticexpialidocious
and decide it's a string. And maybe other types, later.
Now I've been at this game a while, so I'm quite capable of
writing number-reading software for myself. But it seems to me
that those nice folk at the sim vendors already did that for me, and
gave me $sscanf. So I try to use that. I've got my string in a
string variable "str", and I would like to see if I have an integer.
(More types to do later, but that's a start.) Here's my effort:
int count, val;
string str, junk;
...
str = <whatever>;
count = $sscanf(str, "%d%s", val, junk);
At this point I expect count==0 if my string was nothing like
an integer. I expect count==1 if there's an integer and nothing
else (except possibly whitespace) in the string. And I expect
count==2 if there's an integer followed by something I don't want.
So let's try it with the latest offerings from three vendors.
One of 'em doesn't support strings yet. I'll try it again later
with a plain-old Verilog-style string (yuk!), but meanwhile...
The other two, given a simple integer like "256", both give
count==1 and fill up my "val" variable as expected. Good.
So, let's try some error cases - a foolish thing to do, I know,
but hey, let's live a little. For a string containing a single
uppercase A,
- vendor X gives me count==1, val==65
- vendor Y gives me count==0 (CORRECT!! Hurrah!!)
(Even more bizarre: for the string "AA", vendor X gives
val==16705. Go figure.)
For the string "4'b11" (without its enclosing quotes),
- vendor X gives me count==2, val==4, junk=="'b11" (Yippee!)
- vendor Y gives me count==1, val==11 (eh?????)
Finally some good news: for the string "123AAA", both
X and Y give count==2, val==123, junk=="AAA".
Can anyone comment? For me, it's back to the hand-
coded number reader. I hate to say this, because I know
how hard all the vendors have worked to bring SystemVerilog
implementations to market quickly, but surely we
deserve better - $sscanf is not rocket science.
Thanks for letting me get that off my chest
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
So here's me trying to read in some text and decide what kind of
value that text represents - so, for example, I'd like to be able
to read the text
100
and decide that it's an integer,
-34.5e4
and decide it's a real,
supercalifragilisticexpialidocious
and decide it's a string. And maybe other types, later.
Now I've been at this game a while, so I'm quite capable of
writing number-reading software for myself. But it seems to me
that those nice folk at the sim vendors already did that for me, and
gave me $sscanf. So I try to use that. I've got my string in a
string variable "str", and I would like to see if I have an integer.
(More types to do later, but that's a start.) Here's my effort:
int count, val;
string str, junk;
...
str = <whatever>;
count = $sscanf(str, "%d%s", val, junk);
At this point I expect count==0 if my string was nothing like
an integer. I expect count==1 if there's an integer and nothing
else (except possibly whitespace) in the string. And I expect
count==2 if there's an integer followed by something I don't want.
So let's try it with the latest offerings from three vendors.
One of 'em doesn't support strings yet. I'll try it again later
with a plain-old Verilog-style string (yuk!), but meanwhile...
The other two, given a simple integer like "256", both give
count==1 and fill up my "val" variable as expected. Good.
So, let's try some error cases - a foolish thing to do, I know,
but hey, let's live a little. For a string containing a single
uppercase A,
- vendor X gives me count==1, val==65
- vendor Y gives me count==0 (CORRECT!! Hurrah!!)
(Even more bizarre: for the string "AA", vendor X gives
val==16705. Go figure.)
For the string "4'b11" (without its enclosing quotes),
- vendor X gives me count==2, val==4, junk=="'b11" (Yippee!)
- vendor Y gives me count==1, val==11 (eh?????)
Finally some good news: for the string "123AAA", both
X and Y give count==2, val==123, junk=="AAA".
Can anyone comment? For me, it's back to the hand-
coded number reader. I hate to say this, because I know
how hard all the vendors have worked to bring SystemVerilog
implementations to market quickly, but surely we
deserve better - $sscanf is not rocket science.
Thanks for letting me get that off my chest
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.