Binary file IO in Modelsim

T

Tony Benham

I'm tring to read/write bytes from a binary file in a modelsim simulation.
I've checked back over many posts and the FAQ which say it is possible by
opening a file of type CHARACTER, and using READ(f,c) to read the data into
a CHARACTER. However I couldn't find one actual example of method detailed
any further. When I try to implement it myself I get "No feasible entries
for subprogram read"
I don't want to use ascii as I'm working with large files (3MB) so can
anyone point in the direction of a code fragment that actually show how to
implement this method ?
Regards
Tony Benham
 
M

Mike Treseler

Tony Benham said:
I'm tring to read/write bytes from a binary file in a modelsim simulation.

Here's an example to get you started.
Byte ordering and file offsets may vary.
Good luck.

-- Mike Treseler


-------------------------------------------------------------------------------
-- Binary file demo using 32 bit integers
-- Fri Jul 9 15:30:53 2004 Mike Treseler
-- Invoke with vsim -c bin_file -do "run 1";
-------------------------------------------------------------------------------
entity bin_file is
end entity bin_file;

architecture sim of bin_file is
type int_file is file of integer; -- 4 bytes each order=4,3,2,1,8,7,6,5...
file my_file : int_file;
type int_array is array(natural range <>) of integer;

function spew(i_arg : integer) return int_array is
variable result : int_array(0 to i_arg-1);
begin
result(0) := 16#0abc_0123#;
for i in 1 to i_arg-1 loop
result(i) := result(i-1) + 1;
end loop;
return result;
end;

constant box_o_ints : int_array := spew(100);
begin -- architecture sim

what : process is
variable my_int_v : integer;
begin
file_open(my_file, "my_file.bin", write_mode);
for i in box_o_ints'range loop
write(my_file, box_o_ints(i));

end loop; -- i
file_close(my_file);
file_open(my_file, "my_file.bin", read_mode);
for i in box_o_ints'range loop

-- report integer'image(box_o_ints(i));
read(my_file, my_int_v);
assert my_int_v = box_o_ints(i)
report "read error" severity warning;
end loop; -- i
file_close(my_file);
report("view my_file.bin in emacs hexl-mode");
wait;
end process what;

end architecture sim;
--58 steptoe /evtfs/home/tres/vhdl/play > vsim -c bin_file -do "run 1";
--# Loading /steptoe/usr1/modeltech/linux/../std.standard
--# Loading work.bin_file(sim)
--# run 1
--# ** Note: view my_file.bin in emacs hexl-mode
--# Time: 0 ns Iteration: 0 Instance: /bin_file
--VSIM 2> exit
--58 steptoe Fri Jul 09 /evtfs/home/tres/vhdl/play > emacs my_file.bin
-------------------------------------------------------------------------------
-- file: my_file.bin (hexl)
-------------------------------------------------------------------------------
--00000000: 2301 bc0a 2401 bc0a 2501 bc0a 2601 bc0a #...$...%...&...
--00000010: 2701 bc0a 2801 bc0a 2901 bc0a 2a01 bc0a '...(...)...*...
--00000020: 2b01 bc0a 2c01 bc0a 2d01 bc0a 2e01 bc0a +...,...-.......
--00000030: 2f01 bc0a 3001 bc0a 3101 bc0a 3201 bc0a /...0...1...2...
--00000040: 3301 bc0a 3401 bc0a 3501 bc0a 3601 bc0a 3...4...5...6...
--00000050: 3701 bc0a 3801 bc0a 3901 bc0a 3a01 bc0a 7...8...9...:...
--00000060: 3b01 bc0a 3c01 bc0a 3d01 bc0a 3e01 bc0a ;...<...=...>...
--00000070: 3f01 bc0a 4001 bc0a 4101 bc0a 4201 bc0a [email protected]...
--00000080: 4301 bc0a 4401 bc0a 4501 bc0a 4601 bc0a C...D...E...F...
--00000090: 4701 bc0a 4801 bc0a 4901 bc0a 4a01 bc0a G...H...I...J...
--000000a0: 4b01 bc0a 4c01 bc0a 4d01 bc0a 4e01 bc0a K...L...M...N...
--000000b0: 4f01 bc0a 5001 bc0a 5101 bc0a 5201 bc0a O...P...Q...R...
--000000c0: 5301 bc0a 5401 bc0a 5501 bc0a 5601 bc0a S...T...U...V...
--000000d0: 5701 bc0a 5801 bc0a 5901 bc0a 5a01 bc0a W...X...Y...Z...
--000000e0: 5b01 bc0a 5c01 bc0a 5d01 bc0a 5e01 bc0a [...\...]...^...
--000000f0: 5f01 bc0a 6001 bc0a 6101 bc0a 6201 bc0a _...`...a...b...
--00000100: 6301 bc0a 6401 bc0a 6501 bc0a 6601 bc0a c...d...e...f...
--00000110: 6701 bc0a 6801 bc0a 6901 bc0a 6a01 bc0a g...h...i...j...
--00000120: 6b01 bc0a 6c01 bc0a 6d01 bc0a 6e01 bc0a k...l...m...n...
--00000130: 6f01 bc0a 7001 bc0a 7101 bc0a 7201 bc0a o...p...q...r...
--00000140: 7301 bc0a 7401 bc0a 7501 bc0a 7601 bc0a s...t...u...v...
--00000150: 7701 bc0a 7801 bc0a 7901 bc0a 7a01 bc0a w...x...y...z...
--00000160: 7b01 bc0a 7c01 bc0a 7d01 bc0a 7e01 bc0a {...|...}...~...
--00000170: 7f01 bc0a 8001 bc0a 8101 bc0a 8201 bc0a ................
--00000180: 8301 bc0a 8401 bc0a 8501 bc0a 8601 bc0a ................


--00000000: 2301 bc0a 2201 bc0a 2101 bc0a 2001 bc0a #..."...!... ...
--00000010: 1f01 bc0a 1e01 bc0a 1d01 bc0a 1c01 bc0a ................
--00000020: 1b01 bc0a 1a01 bc0a 1901 bc0a 1801 bc0a ................
--00000030: 1701 bc0a 1601 bc0a 1501 bc0a 1401 bc0a ................
--00000040: 1301 bc0a 1201 bc0a 1101 bc0a 1001 bc0a ................
--00000050: 0f01 bc0a 0e01 bc0a 0d01 bc0a 0c01 bc0a ................
--00000060: 0b01 bc0a 0a01 bc0a 0901 bc0a 0801 bc0a ................
--00000070: 0701 bc0a 0601 bc0a 0501 bc0a 0401 bc0a ................
--00000080: 0301 bc0a 0201 bc0a 0101 bc0a 0001 bc0a ................
--00000090: ff00 bc0a fe00 bc0a fd00 bc0a fc00 bc0a ................
--000000a0: fb00 bc0a fa00 bc0a f900 bc0a f800 bc0a ................
--000000b0: f700 bc0a f600 bc0a f500 bc0a f400 bc0a ................
--000000c0: f300 bc0a f200 bc0a f100 bc0a f000 bc0a ................
--000000d0: ef00 bc0a ee00 bc0a ed00 bc0a ec00 bc0a ................
--000000e0: eb00 bc0a ea00 bc0a e900 bc0a e800 bc0a ................
--000000f0: e700 bc0a e600 bc0a e500 bc0a e400 bc0a ................
--00000100: e300 bc0a e200 bc0a e100 bc0a e000 bc0a ................
--00000110: df00 bc0a de00 bc0a dd00 bc0a dc00 bc0a ................
--00000120: db00 bc0a da00 bc0a d900 bc0a d800 bc0a ................
--00000130: d700 bc0a d600 bc0a d500 bc0a d400 bc0a ................
--00000140: d300 bc0a d200 bc0a d100 bc0a d000 bc0a ................
--00000150: cf00 bc0a ce00 bc0a cd00 bc0a cc00 bc0a ................
--00000160: cb00 bc0a ca00 bc0a c900 bc0a c800 bc0a ................
--00000170: c700 bc0a c600 bc0a c500 bc0a c400 bc0a ................
--00000180: c300 bc0a c200 bc0a c100 bc0a c000 bc0a ...

-- Mike Treseler
 
M

Mike Treseler

Here's an example to get you started.

I used integers in the previous example because
it was a little easier to code. But it made
me curious about the character array case,
so here it is.
-- Mike Treseler

-------------------------------------------------------------------------------
-- Binary file demo using 8 bit characters
-- Mon Jul 12 10:44:29 2004 Mike Treseler
-- Invoke with vsim -c char_file -do "run 1";
-------------------------------------------------------------------------------
entity char_file is
end entity char_file;

architecture sim of char_file is
type char_file is file of character; -- one byte each
file my_file : char_file;
constant file_name : string := "char_file.bin";
type char_array is array(natural range <>) of character;
subtype char_range is natural range character'pos(character'left) to
character'pos(character'right);
subtype char_index is natural range
character'pos(character'left) to
character'pos(character'right);

-- purpose: double check the character range
procedure verify_range is
begin -- procedure verify_range
assert char_index'left = 0
report "guess left again" severity warning;
assert char_index'right = 255
report "guess right again" severity warning;
end procedure verify_range;

function spew(i_arg : integer) return char_array is
variable result : char_array(0 to i_arg-1);
variable index : char_index;
begin
for i in 0 to i_arg-1 loop
index := i mod char_index'right;
result(i) := character'val(index);
end loop;
return result;
end function spew;

constant box_o_chars : char_array := spew(512);
begin -- architecture sim
what : process is
variable my_char_v : character;
begin
verify_range;
file_open(my_file, file_name, write_mode);
for i in box_o_chars'range loop
write(my_file, box_o_chars(i));
end loop; -- i
file_close(my_file);
file_open(my_file, file_name, read_mode);
for i in box_o_chars'range loop
read(my_file, my_char_v);
assert my_char_v = box_o_chars(i)
report "read error" severity warning;
end loop; -- i
file_close(my_file);
report("view " & file_name & " in emacs hexl-mode");
wait;
end process what;

end architecture sim;

--59 steptoe Mon Jul 12 /evtfs/home/tres/vhdl/play > !!
--vsim -c char_file -do "run 1;exit"
--# Loading /steptoe/usr1/modeltech/linux/../std.standard
--# Loading work.char_file(sim)
--# run 1;exit
--# ** Note: view char_file.bin in emacs hexl-mode
--# Time: 0 ns Iteration: 0 Instance: /char_file
--59 steptoe Fri Jul 09 /evtfs/home/tres/vhdl/play > emacs char_file.bin
-------------------------------------------------------------------------------
-- file: char_file.bin (hexl)
-------------------------------------------------------------------------------
--00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................
--00000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................
--00000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !"#$%&'()*+,-./
--00000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>?
--00000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f @ABCDEFGHIJKLMNO
--00000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f PQRSTUVWXYZ[\]^_
--00000060: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f `abcdefghijklmno
--00000070: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f pqrstuvwxyz{|}~.
--00000080: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................
--00000090: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f ................
--000000a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf ................
--000000b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf ................
--000000c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf ................
--000000d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf ................
--000000e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef ................
--000000f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd fe00 ................
--00000100: 0102 0304 0506 0708 090a 0b0c 0d0e 0f10 ................
--00000110: 1112 1314 1516 1718 191a 1b1c 1d1e 1f20 ...............
--00000120: 2122 2324 2526 2728 292a 2b2c 2d2e 2f30 !"#$%&'()*+,-./0
--00000130: 3132 3334 3536 3738 393a 3b3c 3d3e 3f40 123456789:;<=>?@
--00000140: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP
--00000150: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\]^_`
--00000160: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
--00000170: 7172 7374 7576 7778 797a 7b7c 7d7e 7f80 qrstuvwxyz{|}~..
--00000180: 8182 8384 8586 8788 898a 8b8c 8d8e 8f90 ................
--00000190: 9192 9394 9596 9798 999a 9b9c 9d9e 9fa0 ................
--000001a0: a1a2 a3a4 a5a6 a7a8 a9aa abac adae afb0 ................
--000001b0: b1b2 b3b4 b5b6 b7b8 b9ba bbbc bdbe bfc0 ................
--000001c0: c1c2 c3c4 c5c6 c7c8 c9ca cbcc cdce cfd0 ................
--000001d0: d1d2 d3d4 d5d6 d7d8 d9da dbdc ddde dfe0 ................
--000001e0: e1e2 e3e4 e5e6 e7e8 e9ea ebec edee eff0 ................
--000001f0: f1f2 f3f4 f5f6 f7f8 f9fa fbfc fdfe 0001 ................
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top