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.

VHDL file IO simulation

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
Hello,

I want to write a textio tesbench for this simple VHDL code:

Code:
library ieee ;
use ieee.std_logic_1164.all ;
use ieee.numeric_std.all ;
		
entity parity_generator is 
port						
( 		
  CLK : in	std_logic ;
  RST : in std_logic ;
  INPUT : in std_logic_vector ( 1 downto 0 ) ;
  OUTPUT : out std_logic 
) ;	 	 			            
end entity parity_generator ;

architecture synthesizable_parity_generator of parity_generator is	   
begin

process ( CLK , RST ) is 
begin
  if RST = '1' then
    OUTPUT <= 'Z' ;
  elsif rising_edge ( CLK ) then
    OUTPUT <= INPUT ( 0 ) xor INPUT ( 1 ) ;
  end if ;
end process ;

end architecture synthesizable_parity_generator ;

In my TB, I want to feed the above component with a file that looks like this:
Code:
00
01
10
11
I want the TB to read a new line from the above file with every clock edge and print the output in a different file.
Please help me write the TB.
 
Last edited:

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
What exactly are you having trouble with?
The syntax...
I want to learn how to use the textio mechanism. Doing so based on my own specific example will help me understand better...

Suppose my stimulus file is named: stimulus.txt and its content is:
00
01
10
11

1.The file is a string...but I need it to be STD_LOGIC_VECTOR. How do I make the conversion?
2.How do I make my test bench read a new line with every new clock ?
3.How do I write back the output to a new file?
4.How do I save the information?

I've found a lot of information regarding textio simulation but couldn't come up with a working TB to learn from.
If you can post a testbench example for the code above it'd be very helpful.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,381
Helped
14,245
Reputation
28,751
Reaction score
12,932
Trophy points
1,393
Location
Bochum, Germany
Activity points
280,019
Did you review the available textio procedures?

1. Conversion from string to bit_vector is perfomed by the READ() procedure.
2. Perform READLN() and READ() on rising edge.
 

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
Did you review the available textio procedures?
Yes. I did. It raised more questions then it answered.
This is why I'm asking someone more knowledgeable then me in the subject to post an example of a TB that does what I described.
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,114
We learned by doing, and asking specific questions.
I dont have any code to hand.
Why not just try and we can comment on what you did?
 

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
Why not just try and we can comment on what you did?
OK...

This is my basic TB.

Code:
library ieee ;
use ieee.std_logic_1164.all ;
use ieee.numeric_std.all ;
		
entity tb_parity_generator is                                                                            	                                    
end entity tb_parity_generator ;

architecture simulation_tb_parity_generator of parity_generator is

component parity_generator is 
port					
( 		
  CLK : in std_logic ;
  RST : in std_logic ;
  INPUT : in std_logic_vector ( 1 downto 0 ) ;
  OUTPUT : out std_logic 
) ;	 	 				                                       
end component parity_generator ;

signal stimulus_clk : std_logic := '0' ;
signal stimulus_rst : std_logic ;
signal stimulus_input : std_logic_vector ( 1 downto 0 ) ; -- from file 
signal stimulus_output : std_logic ; -- to file

begin

stimulus_rst <= '1' , '0' after 20 ns ;
stimulus_clk <= not stimulus_clk after 10 ns ;
	
simulation : parity_generator 
port map 
( 		
  CLK => stimulus_clk ,
  RST => stimulus_rst ,
  INPUT => stimulus_input ,
  OUTPUT => stimulus_output
) ;	 	 	
             	
end architecture simulation_tb_counter ;

Should I add both IEEE.STD_LOGIC_TEXTIO and STD.TEXTIO ?
What does each one do?
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,114
OK...

This is my basic TB.

Code:
library ieee ;
use ieee.std_logic_1164.all ;
use ieee.numeric_std.all ;
		
entity tb_parity_generator is                                                                            	                                    
end entity tb_parity_generator ;

architecture simulation_tb_parity_generator of parity_generator is

component parity_generator is 
port					
( 		
  CLK : in std_logic ;
  RST : in std_logic ;
  INPUT : in std_logic_vector ( 1 downto 0 ) ;
  OUTPUT : out std_logic 
) ;	 	 				                                       
end component parity_generator ;

signal stimulus_clk : std_logic := '0' ;
signal stimulus_rst : std_logic ;
signal stimulus_input : std_logic_vector ( 1 downto 0 ) ; -- from file 
signal stimulus_output : std_logic ; -- to file

begin

stimulus_rst <= '1' , '0' after 20 ns ;
stimulus_clk <= not stimulus_clk after 10 ns ;
	
simulation : parity_generator 
port map 
( 		
  CLK => stimulus_clk ,
  RST => stimulus_rst ,
  INPUT => stimulus_input ,
  OUTPUT => stimulus_output
) ;	 	 	
             	
end architecture simulation_tb_counter ;

Should I add both IEEE.STD_LOGIC_TEXTIO and STD.TEXTIO ?
What does each one do?

std.textio has functions (read/write etc) defined for standard types. It also declares the text file type
ieee.std_logic_textio has functions for std_logic and std_logic_vector types (it is also not a vhdl standard package)

If you use vhdl 2008, all the textio functions are added to the appropriate packages by default (std_logic_1164, numeric_std, fixed_pkg etc) so std_logic_textio is nothing more than a placeholder
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
So with VHDL 2008 I don't have to add any textio libraries?
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,114
you'll still need std.textio if you want to access the text file type, but all the textio is included in the standard libraries.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
suppose my input file is named: "input_stimulus.txt" located in:
C:\some_directory\input_stimulus.txt

What files extensions are legal?
Do I have to declare the file? If yes - how?
Do I have to add it to my project? If yes - how?
What are the difference between a TEXT file and other files?
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,114
All file extensions are legal. The file extension is meaningless - its just a name. The file must be a text file though.
Yes, you have to declare the file. If you read any textio tutorial or VHDL text book it will tell you:

file my_file : text open read_mode is "c:\some_directory\input_stimulus.txt"; --you can also use linux style formatting inside modelsim

Do you have to add it to your project? no. but remember the path is relative to the directory you're running the simulation in

What are differences between text files and other files?

The text type is declared in textIO as:

type text is file of string;

you can declare new file types if you want. A lot of examples do this:

type int_file_t is file of integer;

so any calls to read(f, x) and write(f, x) require x to be an integer. But how non-text files are formatted is not defined in the VHDL spec and can differ between vendors, so its highly recommended you stick to text files.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
All file extensions are legal. The file extension is meaningless - its just a name. The file must be a text file though.
Of topic question,
What if I'm trying to manipulate the contents of a JPEG file in a simulation environment using my HDL?
I have to change its type to text before I do anything? If yes, how is it done?
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,381
Helped
14,245
Reputation
28,751
Reaction score
12,932
Trophy points
1,393
Location
Bochum, Germany
Activity points
280,019
I don't see how processing of unformatted binary files should be specified in VHDL. It's however possible with Verilog system tasks.

For VHDL modelling, I would convert binary files to decimal formatted and vice versa with an external tool.
 

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
I don't see how processing of unformatted binary files should be specified in VHDL.
I didn't say it should be part of the VHDL spec. It was a general question...
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,114
There are ways you can read binary files in VHDL. In modelsim you can use:

file data_f_t is file of character

and that gives you byte access.

But as a JPEG, that is compressed image data. For raw image data you will need a bitmap.

But I would highly recommend you convert your jpeg to text somehow. it makes file reading easier and more predictable, as well as portable.
 
  • Like
Reactions: shaiko and FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating

    shaiko

    Points: 2
    Helpful Answer Positive Rating

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,381
Helped
14,245
Reputation
28,751
Reaction score
12,932
Trophy points
1,393
Location
Bochum, Germany
Activity points
280,019
There are ways you can read binary files in VHDL. In modelsim you can use:

file data_f_t is file of character

and that gives you byte access.
I see that the type definition complies with general VHDL syntax rules. But I didn't expect that Modelsim would support it. Good to know.
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,114
I see that the type definition complies with general VHDL syntax rules. But I didn't expect that Modelsim would support it.

Modelsim supports it just fine.
read(f, t) and write(f, t) are supported natively for all file types.

modelsim will return the characters in the order of the file, then from character you can convert to integer easily:


Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function char_to_slv( c : character ) return std_logic_vector is
begin
    return std_logic_vector(to_unsigned(character'pos(c), 8));
end function char_to_slv;
 
procedure read_bytes( file     f :     data_file_t;
                      variable s : out std_logic_vector ) is
    variable c_buf   : character;
begin
    for i in 0 to (s'length/8) -1 loop
        read(f, c_buf);
 
        s( (8*i) +7 downto i*8 ) := char_to_slv(c_buf);
    end loop;
end procedure read_bytes;



I have successfully been reading/writing bitmap files for years using this method.

There was a test done years ago on another forum testing different simulators for binary file IO. IIRC modelsim worked fine. ISIM worked but decided to put some proprietary header ontop of the written binary data and NCSIM just didnt like it.
 
  • Like
Reactions: shaiko and FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating

    shaiko

    Points: 2
    Helpful Answer Positive Rating

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,300
Back to my TB:

My file (input_stimulus.txt) is written in a text editor and contains the following:
00
01
10
11
The numbers are characters - how do I convert them to std_logic_vectors ?
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,381
Helped
14,245
Reputation
28,751
Reaction score
12,932
Trophy points
1,393
Location
Bochum, Germany
Activity points
280,019
I thought 24h hours would be time enough to try a few lines of textio code.
e.g.
Code:
readln(f,l);
read(l,my_slv2);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top