std.textio.read strange behaviour?!

P

pontus.stenstrom

I'm reading a text file containing values into a vhdl variable which I
ranged to [0..255].
If the values in the file are outside this range I expected the
simulator to complain,
but it happily reads in "any" integer into my byte, and displays it as
you can try below.
What am I missing? Shouldn't the assignment done in the read procedure
fail?
(tested with both modelsim and aldec - same behaviour)

Name this to a file called t.vhd (including the first comment line):

-- 1 23 456 7890

entity t is
end entity t;

use std.textio.all;

architecture t of t is
begin
t : process is
file my_file : text;
variable ok : file_open_status;
variable my_line : line;
variable to_stdout : line;
variable comment : string(1 to 2);
variable good : boolean := true;
subtype u8 is integer range 0 to 255;
variable byte, b2 : u8;
begin
file_open(ok, my_file, "t.vhd");
while (not endfile(my_file)) loop
readline(my_file, my_line);
exit when my_line'length = 0; -- only parse first non empty
lines
read(my_line, comment); -- throw comment
while good loop
std.textio.read(my_line, byte, good);
if good then
-- look at the output, even values larger than 255 are
printed!
write(to_stdout, integer'image(byte)); writeline(output,
to_stdout);
b2:= byte; -- this assignment works!?
-- b2:= byte+0; -- this fails as it should
end if;
end loop;
end loop;
file_close(my_file);
wait;
end process t;
end architecture t;

Regards -- Pontus
 
M

Mike Treseler

I'm reading a text file containing values into a vhdl variable which I
ranged to [0..255].
If the values in the file are outside this range I expected the
simulator to complain,
but it happily reads in "any" integer into my byte, and displays it as
you can try below.
What am I missing? Shouldn't the assignment done in the read procedure
fail?

I don't know.
Where is the declaration for subtype u8?
Here's a related example:
http://mysite.verizon.net/miketreseler/char_file.vhd

-- Mike Treseler
 
P

pontus.stenstrom

I don't know.
Where is the declaration for subtype u8?
Here's a related example:http://mysite.verizon.net/miketreseler/char_file.vhd

-- Mike Treseler

Right above the declaration of the variable "byte",
subtype u8 is integer range 0 to 255;
Your example is fine, but reads binary data, not ascii text.

What strikes me as strange is; first I can get an out of range value
in to a "ranged" variable, then I can copy this value over to another
variable
of the same type - and still retain the bad value. Apparently a
function (such as "+")
operating on this value gets its result range checked, but not the
procedure (read)
that originated the value in the first place.

I will test some more (tomorrow) with to_unsigned(byte, 9) etc. to see
what pops up...
Also b2 := byte-300; would be intresting.

Regards -- Pontus
 
P

pontus.stenstrom

I did som more tests.
The [range 0 to 255] variable read in using std.textio.read
can contain any integer e.g. 2147483647 (I didn't try them all...)
and is further available to use in expressions, providing the out of
bound value.
Scary!

I tried to create a procedure my self returning a
value outside the [0..255] byte range.
This is captured in simulation and failed on, as expected.

Regards -- Pontus

Try the following code (including the first comment line):

-- 2147483647

entity e is
end entity e;

use std.textio.all;

architecture a of e is
begin
p : process is

file my_file : text;
variable my_line : line;
variable comment : string(1 to 2);

variable byte : integer range 0 to 255;

procedure test_range_check(i : out integer) is
begin
i := 400;
end procedure test_range_check;

begin
file_open(my_file, "t.vhd"); -- opens <this> file
readline(my_file, my_line);
read(my_line, comment);
read(my_line, byte); -- this succeeds, ?!?
report integer'image(byte); -- and reports 2147483647 ?!?
file_close(my_file);

test_range_check(byte); -- this fails, as it should

wait;
end process p;
end architecture a;
 
M

Mike Treseler

I did som more tests.
The [range 0 to 255] variable read in using std.textio.read
can contain any integer e.g. 2147483647 (I didn't try them all...)
and is further available to use in expressions, providing the out of
bound value.
Scary!

If I add this right after begin:

byte := 2147483647;

I get this:

** Error: txt.vhd(24): (vcom-1144)
Value 2147483647 is out of std.standard.natural range 0 to 255.

Looks like a bug in textio.
Report it here:
http://www.eda-stds.org/vasg/bugrep.htm#bugrepform

I prefer to use vhdl constant arrays rather
than textio because text can't be compiled.

-- Mike Treseler
 
P

pontus.stenstrom

If I add this right after begin:

byte := 2147483647;

I get this:

** Error: txt.vhd(24): (vcom-1144)
Value 2147483647 is out of std.standard.natural range 0 to 255.

That comforts me, I like compile time errors!
Looks like a bug in textio.
Report it here:http://www.eda-stds.org/vasg/bugrep.htm#bugrepform

OK, I'll do that.
I prefer to use vhdl constant arrays rather
than textio because text can't be compiled.

Yes, but in this case someone else is giving me a textfile
which should be loaded into a memory, tomorrow they will
change the file ...

Thanks -- Pontus
 
M

Mike Treseler

Yes, but in this case someone else is giving me a textfile
which should be loaded into a memory, tomorrow they will
change the file ...

I would write a python or perl script to
convert the file to a vhdl package containing
a constant array.

Only have to write it once,
and it's easier than dealing with textio.

-- Mike Treseler
 
J

James Unterburger

This form is for bugs in the language defintion, which
is not the case here. The bug is in the implementation.

Mike said:
I did som more tests.
The [range 0 to 255] variable read in using std.textio.read
can contain any integer e.g. 2147483647 (I didn't try them all...)
and is further available to use in expressions, providing the out of
bound value.
Scary!


If I add this right after begin:

byte := 2147483647;

I get this:

** Error: txt.vhd(24): (vcom-1144)
Value 2147483647 is out of std.standard.natural range 0 to 255.

Looks like a bug in textio.
Report it here:
http://www.eda-stds.org/vasg/bugrep.htm#bugrepform

I prefer to use vhdl constant arrays rather
than textio because text can't be compiled.

-- Mike Treseler
 
M

Mike Treseler

James said:
This form is for bugs in the language defintion, which
is not the case here. The bug is in the implementation.

OK. I'll bite. How do make

std.textio.read(my_line, byte);

throw an error in this case?

-- Mike Treseler
 
J

James Unterburger

Your newsreader must be flaky, I commented right here in the
main thread (not this sub-thread), dated 06/24/2008 01:28 PM.
 
J

James Unterburger

It should throw an error when the value read into "byte"
is not in the range 0 to 255.

Now, "should", and "will" are two different things.
If your simulator does not throw an error, then it
has a bug. But there is nothing wrong with the LRM,
see my comment dated 06/24/2008 01:28 PM.
 
K

KJ

Your newsreader must be flaky, I commented right here in the
main thread (not this sub-thread), dated 06/24/2008 01:28 PM.

Your comments did not make it into Google groups either.

KJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top