On Tue, 2013-04-30 at 10:12 -0700, Jon Taylor wrote:
> OK, first of all sorry about not providing a full testcase, you might
> have spotted what I found to be the problem (with reading the strings,
> anyway):
>
>
> variable temp_string:string(1 to 256);
>
>
> I guess it was trying to read 256 characters from the file into the
> string no matter what, presumably hitting EOF since the file wasn't
> that large. Wierd, but I guess that is how std.textio works.
Not weird, but definitely different if you're trained in C.
> I'm giving up on reading the integers directly for now - I can write a
> separate C program that will munge the original binary data into a set
> of ASCII strings.
Oh. Reading binary directly is pretty trivial and seems to work : the
following should get you started.
You have to realise that it isn't very portable between systems and even
different simulators. The byte order is not specified; nor are other
details of the file format. (Xilinx ISIM adds a 9-byte undocumented
header and inexplicably uses big-endian order for example; but Modelsim
and GHDL appear to be relatively sane.)
What IS guaranteed is that a file can be read back by the simulator that
wrote it. Anything above that (reading .BMP files for example) can be
determined by experiment.
That being said, GHDL seems to support it just fine.
The trick is : a file isn't just a file; it's a file OF something (in
strongly typed manner). So it could be a file of integer : then it must
be a multiple of 4 bytes. (else, see below) Or it could be a file of
(fixed size) records or any type, predefined or declared by you.
What you can't do is mix bytes, integers, reals, records in the same
file. (If you need to, you'd have to fake it with a file of bytes, and
reconstruct higher levels yourself)
Std.textio declares "text" as "file of character", and I'm guessing
that's all you have used so far, and that's pushing you into a string
based representation of your integers.
No need : here is the same testcase hacked to read the same file as
Integers! To show it's reading the same data, it extracts every 4th byte
and converts it to character form...
----------------------------------------------------------------------
entity Jon is
end Jon;
use std.textio.all;
architecture Taylor of Jon is
begin
process is
variable buf:line;
variable i : integer;
variable fstatus : file_open_status;
type intfile is file of integer;
file readfile : intfile;
constant temp_string : string := "hello.txt"; -- the same file!
begin
file_open(fstatus, readfile, temp_string, read_mode);
report "open " & file_open_status'image(fstatus) severity note;
if fstatus = OPEN_OK then
while not endfile(readfile) loop
read(readfile, i);
report "i= "& integer'image(i) severity note;
report " i LSB = " & integer'image(i mod 256) & " character "
& character'val(i mod 256);
end loop;
end if;
report "done" severity note;
wait;
end process;
end Taylor;
----------------------------------------------------------------------
brian@Gannet:~/Projects/ghdl/play/bugs$ ghdl -a read_binary.vhd
brian@Gannet:~/Projects/ghdl/play/bugs$ ghdl -e jon
brian@Gannet:~/Projects/ghdl/play/bugs$ ./jon
read_binary.vhd:21:4:@0ms:(report note): open open_ok
read_binary.vhd:25:10:@0ms:(report note): i= 540150321
read_binary.vhd:26:10:@0ms:(report note): i LSB = 49 character 1
read_binary.vhd:25:10:@0ms:(report note): i= 1818585098
read_binary.vhd:26:10:@0ms:(report note): i LSB = 10 character
read_binary.vhd:25:10:@0ms:(report note): i= 856321900
read_binary.vhd:26:10:@0ms:(report note): i LSB = 108 character l
./jon:error: read_scalar failed
./jon:error: simulation failed
----------------------------------------------------------------------
Notice one of the characters is a line feed!
To understand the reason for the last failure, reflect that the test
file is 13 bytes long...
This should get you started.
- Brian
_______________________________________________
Ghdl-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/ghdl-discuss