# to store series of integers into an array using vhdl

Status
Not open for further replies.

#### chitra ranganath

##### Member level 2
i want to know how to store all the values read from the text file which contains integers, into an array

#### TrickyDicky

Ill assume each value is stored on a seperate line:

Code:
type some_array_t is array(integer range <>) of integer;
signal ar : some_array_t(0 to 99); --could be a varaible

--inside a process
file f : text open read_mode is "my_file.txt";
variable inline : line;

for i in ar'range loop
end loop;
Simple as that.

#### chitra ranganath

##### Member level 2
ERROR:HDLParsers:3326 - "F:/Xilinx/check/check1.vhd" Line 49. Formal parameter VALUE of procedure read must be associated with an actual value.
ERROR:HDLParsers:3326 - "F:/Xilinx/check/check1.vhd" Line 49. Formal parameter VALUE of procedure read must be associated with an actual value.
ERROR:HDLParsers:164 - "F:/Xilinx/check/check1.vhd" Line 50. parse error, unexpected LOOP, expecting SEMICOLON
i got these errors i actually tried this before bit dont know what to do

---------- Post added at 17:10 ---------- Previous post was at 16:47 ----------

im working on the text file which id put as attachment in last few threads

#### FvM

##### Super Moderator
Staff member
Does Xilinx XST support VHDL textio library for synthesized code? Most likely not.

P.S.: If it's for a simulation test bench, the sequence would be
Code:
for i in ar'range loop
end loop;

Last edited:

#### kalyanasv

##### Full Member level 4
I concur on that it will have to be test bench simulation. In case you want to load some values then you better use the memory macros and use a initialization file to load them with a particular value.

The below code is not too different just offers a few more modifiable parameters to play with: for an active low reset, 16bit precision, falling edge input of data.
Just ensure your file has enough data...You can also loop this infinitely you loop back after end of file

Code:

declare before begin
Code:
constant prec:integer:=16;
Code:
 process
file infile : TEXT;
variable fstatus : file_open_status;
variable buf_in : line;
variable temp: integer:=16;
begin
data_bus<=(others=>'0');
wait for 1 ns;
while reset = '1' loop
while (not endfile (infile)) loop
wait until clk'event and clk = '0';
data_bus<=CONV_STD_LOGIC_VECTOR(temp,prec);
end loop;
file_close (infile);
end loop;
end process;

#### chitra ranganath

##### Member level 2
sir problem is by using readline and read commands im able to read the values in file but everytime the parameter in read ie value is getting overwritten. here for me the parameter value is integer, which can display or store only one value at a time and read cannot output result in array. the content of my text file are all integers. so i think i should write a package of textio which can store the result in an array. i mean type array of integers.

or else i need a function which takes this integer as input n store it in an array whenever read updates it. pls help me in this view.

the reason y i want to store in array is i need to give these integer values as input to another program which converts it into binary value n use them for further use

#### FvM

##### Super Moderator
Staff member
Both methods mentioned in your post can work. As in the recent example posted by kalyanasav, read can directly take an integer argument, also an array element, if you prefer.

Referring to your previous post, you experienced VHDL syntax errors, not particularly related to textio operations, I think. But you didn't show the actual code, so nobody can help with the VHDL errors.

#### chitra ranganath

##### Member level 2
process
variable L1:line ;
variable x1:integer ; --- x1 can be of any format
file f_open1 : text open read_mode is "C:\Documents and Settings\Chitra\Desktop\abc1.txt";
begin
while (not endfile(f_open1)) loop

int<=x1 ;
wait until clk='1';
end loop;
end process;
this was the program i was using before.

as per my knowledgeREAD can output only bit, bit_vector,boolean, character, integer, real, string. n all these gets overwritten inside loop. in program above i want to store int into array arr(i). how can i do????

the program above can display only one value

#### FvM

##### Super Moderator
Staff member
Using suitable types, the below code should work. I remove the wait statement from your previous code, because there's no need for a timed statement in this place. It's assumed, that the input file size doesn't exceed the array size, otherwise you have to handle this case. But it should be sufficient to get a simulator exception, if the condition is ignored.
Code:
i:=0;
while (not endfile(f_open1)) loop
i:=i+1;
end loop;

#### kalyanasv

##### Full Member level 4
im able to read the values in file but everytime the parameter in read ie value is getting overwritten.
I assume your using a synchronous circuit. Every clk'event you need to use the data or do something with it or it will get overwritten.

"here for me the parameter value is integer, which can display or store only one value at a time and read cannot output result in array.
the content of my text file are all integers. so i think i should write a package of textio which can store the result in an array. i mean type array of integers."

-->Use FvM Code. I presume your just generating data like a C++ program.

"the reason y i want to store in array is i need to give these integer values as input to another program which converts it into binary value n use them for further use"

The code that I pasted above just that. CONV_STD_LOGIC_VECTOR converts from integer to binary.

#### chitra ranganath

##### Member level 2
no sir its not possible to read that way. array s r not supported that way i think i shd write a package which includes this array concept and add it to library textio.
one more thing wen i altered program this way
while (not endfile(f_open1)) loop
int1<=x1 ;
int2<=x2 ;
int3<=x3 ;
int4<=x4 ;
int5<=x5 ;
it read the values but all 1st row elements r not read properly. like till 4th value it was in order later for 5th value instead of 15 it read 8. i dont know wat is happening

#### FvM

##### Super Moderator
Staff member
its not possible to read that way. array s r not supported that way i think
What's exactly the problem or error in this case? If there's actually a problem to use array elements in read() function with your simulator tool (I don't know why), then you can read the data to an auxilary integer variable.
Code:
read(inline, temp);
ar(i):=temp;
it read the values but all 1st row elements r not read properly.
Like others, I was under the assumption, that the values are supplied in the file line by line. Please clarify.

#### chitra ranganath

##### Member level 2
how do i declare this temp as. integer ?? or wat??? sorry bit confused so asking

---------- Post added at 17:04 ---------- Previous post was at 16:58 ----------

i get this error
ERROR:HDLParsers:1209 - "F:/Xilinx/read2/read21.vhd" Line 53. i: Undefined symbol (last report in this block)
i`ve given for loop before read

---------- Post added at 17:07 ---------- Previous post was at 17:04 ----------

---------- Post added at 17:16 ---------- Previous post was at 17:07 ----------

wat is data_bus here i mean declaration

#### FvM

##### Super Moderator
Staff member
Yes, the variable i has to be defined as integer, it's not a loop variable in my example.

Status
Not open for further replies.