-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkirsch_unsynth_pkg.vhd
231 lines (190 loc) · 6.84 KB
/
kirsch_unsynth_pkg.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library std;
use std.textio.all;
package file_pkg is
--------------------------------------------------------------
-- write integers and strings to file
procedure write_file ( filename : in string; msg : in string);
procedure write_file ( filename : in string; msg : in integer);
procedure append_file ( filename : in string; msg : in string);
procedure append_file ( filename : in string; msg : in integer);
end package;
package body file_pkg is
--------------------------------------------------------------
-- open a new file
procedure open_file ( filename : in string )
is
file wr_file : text open write_mode is filename;
variable textline : line;
begin
write( textline, filename);
writeline(wr_file, textline);
end;
--------------------------------------------------------------
-- write a string to a file; overwriting original file
procedure write_file ( filename : in string; msg : in string) is
file wr_file : text open write_mode is filename;
variable textline : line;
begin
write( textline, msg );
writeline(wr_file, textline);
end write_file;
--------------------------------------------------------------
-- write an integer to a file; overwriting original file
procedure write_file ( filename : in string; msg : in integer) is
file wr_file : text open write_mode is filename;
variable textline : line;
begin
write( textline, msg );
writeline(wr_file, textline);
end write_file;
--------------------------------------------------------------
-- append a string to a file
procedure append_file ( filename : in string; msg : in string) is
file wr_file : text open append_mode is filename;
variable textline : line;
begin
write( textline, msg );
writeline(wr_file, textline);
end append_file;
--------------------------------------------------------------
-- append an integer to a file
procedure append_file ( filename : in string; msg : in integer) is
file wr_file : text open append_mode is filename;
variable textline : line;
begin
write( textline, msg );
writeline(wr_file, textline);
end append_file;
end file_pkg;
------------------------------------------------------------------------
-- constants and types for kirsch edge detection
------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library std;
use std.textio.all;
use work.string_pkg.all;
use work.file_pkg.all;
use work.kirsch_synth_pkg.all;
package kirsch_unsynth_pkg is
--------------------------------------------------------------
-- conversions for directions
function to_string ( dir : direction_ty ) return string;
--------------------------------------------------------------
-- read/write images from/to files
impure function read_image( filename : string ) return image_ty;
procedure write_image( filename : in string; image : in image_ty);
-- type text_file is file of text;
procedure write_edge_dir
( file wr_file : text;
edge : in std_logic ;
dir : in direction_ty;
row_idx,
col_idx : in integer
);
end kirsch_unsynth_pkg;
package body kirsch_unsynth_pkg is
--------------------------------------------------------------
-- convert a direction into a string
function to_string ( dir : direction_ty ) return string is
begin
case dir is
when dir_ne => return "NE";
when dir_sw => return "SW";
when dir_n => return "N ";
when dir_s => return "S ";
when dir_w => return "W ";
when dir_e => return "E ";
when dir_nw => return "NW";
when dir_se => return "SE";
when others => return "XX";
end case;
end to_string;
--------------------------------------------------------------
-- append edge, direction, row, col to a file
procedure write_edge_dir
( file wr_file : text;
edge : in std_logic;
dir : in direction_ty;
row_idx,
col_idx : in integer)
is
variable textline : line;
begin
if edge = '1' then
write( textline, 1);
else
write( textline, 0);
end if;
write( textline, ' ');
write( textline, to_integer(unsigned(dir)));
write( textline, ' ');
write( textline, row_idx);
write( textline, ' ');
write( textline, col_idx);
writeline(wr_file, textline);
end write_edge_dir;
--------------------------------------------------------------
-- read an image from a file
impure function read_image ( filename : string ) return image_ty is
file rd_file : text open read_mode is filename;
variable textline : line;
variable rd_ok : boolean;
variable pixel : integer;
variable row_min,
row_max,
col_min,
col_max,
row_idx,
col_idx : natural;
variable image : image_ty;
begin
row_min:= image_ty'low(1);
row_max:= image_ty'high(1);
col_min:= image_ty'low(2);
col_max:= image_ty'high(2);
for row_idx in row_min to row_max loop
if endfile(rd_file) then
report ("ERROR: premature end of file at ("&
to_string(row_idx) &","& to_string(col_idx) &")");
image(row_idx, col_idx) := pixel_ty(to_unsigned(pixel,8));
else
readline(rd_file, textline);
for col_idx in col_min to col_max loop
read( textline, pixel, rd_ok);
if rd_ok then
image(row_idx, col_idx) := pixel_ty(to_unsigned(pixel,8));
else
report ("ERROR: premature end of file at ("&
to_string(row_idx) &","& to_string(col_idx) &")");
image(row_idx, col_idx) := pixel_ty(to_unsigned(pixel,8));
end if;
end loop;
end if;
end loop;
return image;
end read_image;
--------------------------------------------------------------
-- write an image to a file
procedure write_image ( filename : in string; image : in image_ty) is
file wr_file : text open write_mode is filename;
variable textline : line;
variable pixel : integer;
variable row_idx,
col_idx : integer;
begin
for row_idx in image_ty'low(1) to image_ty'high(1) loop
for col_idx in image_ty'low(2) to image_ty'high(2) loop
-- put the pixel in textline
write( textline, to_integer(image(row_idx, col_idx)));
-- put a space between the pixels
write( textline, ' ');
end loop;
writeline(wr_file, textline); -- write the line into file
end loop;
end write_image;
end kirsch_unsynth_pkg;