EmbDev.net

Forum: FPGA, VHDL & Verilog handling two dimensional array using vhdl


von Sanghamitra D. (Company: education) (sanghamitra6)


Rate this post
useful
not useful
i have a two dimensional array in a text file. i want to read it from 
the text file and write it in another text file as a two dimensional 
array. i have tried using the code given below. but it writes only one 
column. how to write all the columns.
1
library std;
2
use std.textio.all; --include package textio.vhd
3
4
5
--entity declaration
6
entity filehandle is
7
end filehandle;
8
9
10
--architecture definition
11
architecture Behavioral of filehandle is
12
--period of clock,bit for indicating end of file.
13
signal clock,endoffile : bit := '0';
14
--data read from the file.
15
signal dataread : real;
16
--data to be saved into the output file.
17
signal datatosave : real;
18
--line number of the file read or written.
19
signal linenumber : integer:=1; 
20
21
22
begin
23
24
25
26
27
clock <= not (clock) after 1 ns; --clock with time period 2 ns
28
29
30
31
32
--read process
33
reading :
34
process
35
file infile : text is in "bit8.txt"; --declare input file
36
variable inline : line; --line number declaration
37
variable dataread1 : real;
38
begin
39
wait until clock = '1' and clock'event;
40
if (not endfile(infile)) then --checking the "END OF FILE" is not reached.
41
readline(infile, inline); --reading a line from the file.
42
--reading the data from the line and putting it in a real type variable.
43
read(inline, dataread1);
44
dataread <=dataread1; --put the value available in variable in a signal.
45
else
46
endoffile <='1'; --set signal to tell end of file read file is reached.
47
end if;
48
49
50
end process reading;
51
52
53
--write process
54
writing :
55
process
56
file outfile : text is out "bit8_inv.txt"; --declare output file
57
variable outline : line; --line number declaration 
58
begin
59
wait until clock = '0' and clock'event;
60
if(endoffile='0') then --if the file end is not reached.
61
--write(linenumber,value(real type),justified(side),field(width),digits(natural) );
62
write(outline, dataread, right, 16, 12);
63
-- write line to external file.
64
writeline(outfile, outline);
65
linenumber <= linenumber + 1;
66
else
67
null;
68
end if;
69
70
71
end process writing;
72
end behavioral;

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Sanghamitra D. wrote:
> i have a two dimensional array in a text file. i want to read it from
> the text file and write it in another text file as a two dimensional
> array.
So, why not simpy copy one file to the other? This is, what you are 
doing here:
1
read(inline, dataread1);
2
dataread <=dataread1; --put the value available in variable in a signal.
3
:
4
:
5
write(outline, dataread, right, 16, 12);
So, at best those two files may be the same afterwards...

> how to write all the columns.
A text file itself is only linear memory. There no second dimension in a 
text file. There may be some kind of restrictions (line length or 
carriage returns) to mark a second or more dimensions.

> i have a two dimensional array in a text file.
How does that input text file look like? What ware the two dimensions in 
it? What should happen to those two dimensions? How should the output 
file look like?

von Achim S. (Guest)


Rate this post
useful
not useful
I think your main problem can be understood from this comment:

Sanghamitra D. wrote:
> --reading the data from the line and putting it in a real type variable.

Probably every line of your file contains several real numbers as 
ASCII-text. You read linewise, and so every every read access should 
give you several real numbers. But you interprete and store each line as 
a single real number.

All the remaining numbers in that line (i.e. all the remaining columns) 
get lost. You cannot store a sequence of real numbers in a single real 
type variable.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.