------------------------------------------------------------------------------- -- $Source: /evtfs/home/tres/vhdl/play/RCS/char_file.vhd,v $ -- $Revision: 1.9 $ $Date: 2007/01/06 19:13:41 $ -- Here's an example using a character array to write and read a binary file. ------------------------------------------------------------------------------- -- 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 begin -- architecture sim what : process is type char_file_t is file of character; -- one byte each file my_file : char_file_t; constant file_name : string := "char_file.bin"; type char_array_t is array(natural range <>) of character; -- Many characters are difficult to enter and some are not -- printable, so I will reference by natural index subtype char_index is natural range character'pos(character'left) to character'pos(character'right); ------------------------------------------------------------------------------- -- Note that character'left is the identifier nul [no quotes] at position 0 -- and character'right is the character literal 'ÿ' at position 255 -- This is entered in emacs by typing C-\ to toggle input method then "y -- Use C-x 8 C-h to see all the character entry choices with prefixes "'*^_`~ ------------------------------------------------------------------------------- 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_t is variable result : char_array_t(0 to i_arg-1); variable index : char_index; begin for i in 0 to i_arg-1 loop index := i mod (1+char_index'right); result(i) := character'val(index); end loop; return result; end function spew; constant box_o_chars_c : char_array_t := spew(512); variable my_char_v : character; begin verify_range; file_open(my_file, file_name, write_mode); for i in box_o_chars_c'range loop write(my_file, box_o_chars_c(i)); end loop; -- i file_close(my_file); file_open(my_file, file_name, read_mode); for i in box_o_chars_c'range loop read(my_file, my_char_v); assert my_char_v = box_o_chars_c(i) report "read error" severity warning; end loop; -- i file_close(my_file); report("The binary file " & file_name & " has been written and tested"); report("See ./" & file_name & " in a hex viewer like emacs hexl-mode"); wait; end process what; end architecture sim; --74 Sat Mar 11 /evtfs/home/tres/vhdl/play> vsim -c char_file -do "run 1" --Reading /flip/usr1/modeltech/tcl/vsim/pref.tcl --# 6.1c --# vsim -do {run 1} -c char_file --# // ModelSim SE 6.1c Nov 17 2005 Linux 2.6.5-7.201-smp --# Loading /flip/usr1/modeltech/linux/../std.standard --# Loading work.char_file(sim) --# run 1 --# ** Note: The binary file char_file.bin has been written and tested --# Time: 0 ns Iteration: 0 Instance: /char_file --# ** Note: See ./char_file.bin in a hex viewer like 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 feff ................ --00000100: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................ --00000110: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................ --00000120: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !"#$%&'()*+,-./ --00000130: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>? --00000140: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f @ABCDEFGHIJKLMNO --00000150: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f PQRSTUVWXYZ[\]^_ --00000160: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f `abcdefghijklmno --00000170: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f pqrstuvwxyz{|}~. --00000180: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................ --00000190: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f ................ --000001a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf ................ --000001b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf ................ --000001c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf ................ --000001d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf ................ --000001e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef ................ --000001f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff ................