Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help with VHDL reading from Hex file

Status
Not open for further replies.

LIFT

Newbie level 4
Newbie level 4
Joined
Jan 21, 2014
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
81
Hi Guys,I previously posted a thread about reading from a file in VHDL modelsim.Now I am stuck at another part of my Project.My project is to implement a Data Encryption Standard(DES)using VHDL,I am supposed to convert a Image into Hex,Binary,etc...,Then encrypt the data and convert it back into an image.

I have managed to convert a image into Hex,But cant seem to get Modelsim to read the hex file
I would like to request your help if it is possible to read from the following input file.
Pardon my english and thanks for taking your time to read this!


Input File
___________________________________________________
00000000 FF D8 FF E0 00 10 4A 46 49 46 00 01 01 00 00 01 ÿØÿà.JFIF...
00000010 00 01 00 00 FF DB 00 84 00 09 06 07 14 13 12 14 ...ÿÛ.„..
00000020 14 12 14 16 15 15 15 15 18 16 17 17 17 17 18 17 
00000030 17 18 18 14 18 18 17 17 18 18 17 18 1E 28 20 19 ( 
00000040 1A 25 1F 18 17 21 31 22 26 29 2B 2E 2E 2E 17 1F %!1"&)+...
00000050 33 38 33 2C 37 28 2D 2E 2B 01 0A 0A 0A 0E 0D 0E 383,7(-.+
00000060 1B 10 10 1B 34 24 20 24 2C 35 2C 32 2F 30 2C 2C 4$ $,5,2/0,,
00000070 34 2C 34 2C 2F 2C 2F 2C 2D 2F 2F 2F 34 2C 2C 2C 4,4,/,/,-///4,,,
00000080 2C 34 2F 2F 34 2C 2C 2C 2C 2C 2C 34 2C 2C 2C 2C ,4//4,,,,,,4,,,,
00000090 2C 2C 2C 2C 2C 2C 2C 2F 2C 2F FF C0 00 11 08 00 ,,,,,,,/,/ÿÀ..
000000A0 DB 00 E6 03 01 22 00 02 11 01 03 11 01 FF C4 00 Û.æ".ÿÄ.
000000B0 1B 00 00 01 05 01 01 00 00 00 00 00 00 00 00 00 ...........
000000C0 00 00 00 01 02 03 04 05 06 07 FF C4 00 39 10 00 ...ÿÄ.9.
000000D0 01 03 03 03 02 04 05 02 06 02 01 05 01 00 00 01 ..
 

You're much better off reading text files. VHDL is not really geared up for reading binary data. If you can convert the image to a text file, your life will be much simpler.
 
  • Like
Reactions: LIFT

    LIFT

    Points: 2
    Helpful Answer Positive Rating
Code:
[syntax=vhdl]
procedure FileReadBinByte 
(
   signal   reset       :  in    std_logic;
   signal   clk         :  in    std_logic;
   signal   fread       :  in    std_logic;
   signal   EOF         :  out   std_logic;
   signal   data        :  out   std_logic_vector;
   constant file_name   :  in    string := "input_data.bin"
) is

  variable fstatus: FILE_OPEN_STATUS; --(fstatus,file, 93
  type CharFileType is file of character; --int_byte;--natural;
  file data_in: CharFileType;
  variable c : character;
begin
   EOF <= '0';
   file_open(data_in, file_name, read_mode);
   while (not endfile(data_in)) loop
      if (reset = '1') then
         data  <= SetAllZeroes(data'length);
         wait until reset = '0';
      end if;
      wait until rising_edge(clk);

      if (fread = '1') then
         read(data_in,c);
         data <= std_logic_vector(to_unsigned(character'pos(c),data'length));
      end if;
   end loop;
   wait until rising_edge(clk);
   EOF   <= '1';
   file_close(data_in);
end procedure FileReadBinByte;

[/syntax]
 
  • Like
Reactions: LIFT

    LIFT

    Points: 2
    Helpful Answer Positive Rating
Yeah was thinking about the same thing,I would have to find a way to convert it as the software doesnt support converting or saving the file as text file.
Thanks for your reply!
 

Code:
[syntax=vhdl]
procedure FileReadBinByte 
(
   signal   reset       :  in    std_logic;
   signal   clk         :  in    std_logic;
   signal   fread       :  in    std_logic;
   signal   EOF         :  out   std_logic;
   signal   data        :  out   std_logic_vector;
   constant file_name   :  in    string := "input_data.bin"
) is

  variable fstatus: FILE_OPEN_STATUS; --(fstatus,file, 93
  type CharFileType is file of character; --int_byte;--natural;
  file data_in: CharFileType;
  variable c : character;
begin
   EOF <= '0';
   file_open(data_in, file_name, read_mode);
   while (not endfile(data_in)) loop
      if (reset = '1') then
         data  <= SetAllZeroes(data'length);
         wait until reset = '0';
      end if;
      wait until rising_edge(clk);

      if (fread = '1') then
         read(data_in,c);
         data <= std_logic_vector(to_unsigned(character'pos(c),data'length));
      end if;
   end loop;
   wait until rising_edge(clk);
   EOF   <= '1';
   file_close(data_in);
end procedure FileReadBinByte;

[/syntax]

This method works fine with modelsim (I have a whole library doing just this to read and write bitmap files)
But from what I understand, this will not work with other simulators. There is no definition of how reading binary files should work in VHDL. For example ISE simulator will refuse to open a binary file like this unless it has some header that there is no documentation for!!
 
  • Like
Reactions: LIFT

    LIFT

    Points: 2
    Helpful Answer Positive Rating
Hi Guys,I managed to put it as a .txt file,I also found out that the
text on the right colum is actually hex string.Is it possible to get
Modelsim to read this input file?

Input file
____________________________

00000000 FF D8 FF E0 00 10 4A 46 49 46 00 01 01 00 00 01 ÿØÿà.JFIF...
00000010 00 01 00 00 FF DB 00 84 00 09 06 07 14 13 12 14 ...ÿÛ.„..
00000020 14 12 14 16 15 15 15 15 18 16 17 17 17 17 18 17
00000030 17 18 18 14 18 18 17 17 18 18 17 18 1E 28 20 19 (
00000040 1A 25 1F 18 17 21 31 22 26 29 2B 2E 2E 2E 17 1F %!1"&)+...
00000050 33 38 33 2C 37 28 2D 2E 2B 01 0A 0A 0A 0E 0D 0E 383,7(-.+
 

Hi tricky ,yes it managed to read from the file.I took a portion of aruipksni's code and put it into mine.I only took 3 lines of the right column(weird characters)and after encoding,this it the result.

Result
________________
1335 ns 2757606E72465E51
2135 ns 8B5E51AA4C7E3215
2935 ns 57DBBC7510B7E38C



Sorry guys I would also like to ask if it is possible to read from a hex input file that has spaces in between the hexadecimal,I tried to read it and Modelsim read it as all zeros(000000000....)

Example
_____________________
00000000 FF D8 FF E0 00 10 4A 46 49 46 00 01 01 00 00 01
00000010 00 01 00 00 FF DB 00 84 00 09 06 07 14 13 12 14
00000020 14 12 14 16 15 15 15 15 18 16 17 17 17 17 18 17 
00000030 17 18 18 14 18 18 17 17 18 18 17 18 1E 28 20 19

Thanks for helping me with all these!Actually I just started VHDL few days ago and my instructor gave me this project,I hope to get good grades on it as it is my final project before graduating,Thank you for your time and patience!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top