@@ -10,59 +10,76 @@ use ieee.numeric_std.all;
1010
1111library vunit_lib;
1212context vunit_lib.vunit_context;
13- use vunit_lib.array_pkg.all ;
14-
1513
1614library osvvm;
17- use osvvm.RandomPkg.all ;
15+ use osvvm.RandomPkg.RandomPType ;
1816
1917entity tb_sobel_x is
2018 generic (
2119 runner_cfg : string ;
22- tb_path : string );
20+ tb_path : string
21+ );
2322end entity ;
2423
2524architecture tb of tb_sobel_x is
26- signal clk : std_logic := '0' ;
27- signal input_tvalid : std_logic := '0' ;
28- signal input_tlast : std_logic := '0' ;
29- signal input_tdata : unsigned (14 - 1 downto 0 ) := (others => '0' );
25+
26+ signal clk : std_logic := '0' ;
27+ signal input_tvalid : std_logic := '0' ;
28+ signal input_tlast : std_logic := '0' ;
29+ signal input_tdata : unsigned (13 downto 0 ) := (others => '0' );
3030 signal output_tvalid : std_logic ;
31- signal output_tlast : std_logic ;
32- signal output_tdata : signed (input_tdata'length downto 0 );
31+ signal output_tlast : std_logic ;
32+ signal output_tdata : signed (input_tdata'length downto 0 );
3333
34- shared variable image : array_t;
35- shared variable reference_image : array_t;
34+ shared variable image , ref_image : integer_array_t;
3635 signal start, data_check_done, stimuli_done : boolean := false ;
36+
3737begin
3838
3939 main : process
40- procedure sobel_x(variable image : inout array_t;
41- variable result : inout array_t) is
40+ impure function sobel_x (
41+ constant image : integer_array_t
42+ ) return integer_array_t is
43+ variable result: integer_array_t := new_2d(
44+ width => width (image ),
45+ height => height(image ),
46+ bit_width => bit_width(image )+ 1 ,
47+ is_signed => true
48+ );
4249 begin
43- result.init_2d(width => image .width ,
44- height => image .height,
45- bit_width => image .bit_width+ 1 ,
46- is_signed => true );
47-
48- for y in 0 to image .height- 1 loop
49- for x in 0 to image .width - 1 loop
50- result.set(x => x, y => y,
51- value => (image .get (minimum(x+ 1 , image .width - 1 ),y) -
52- image .get (maximum(x- 1 , 0 ), y)));
50+ for y in 0 to height(image )- 1 loop
51+ for x in 0 to width (image )- 1 loop
52+ set(
53+ result,
54+ x => x,
55+ y => y,
56+ value => (
57+ get (image , minimum(x+ 1 , width (image )- 1 ),y)
58+ - get (image , maximum(x- 1 , 0 ), y)
59+ )
60+ );
5361 end loop ;
5462 end loop ;
55-
56- end procedure ;
63+ return result;
64+ end ;
5765
5866 variable rnd : RandomPType;
5967
60- procedure randomize(variable arr : inout array_t) is
68+ impure function randomize (
69+ constant width , height, bit_width: natural
70+ ) return integer_array_t is
71+ variable image : integer_array_t := new_2d(
72+ width => width ,
73+ height => height,
74+ bit_width => bit_width,
75+ is_signed => false
76+ );
6177 begin
62- for idx in 0 to arr. length - 1 loop
63- arr. set(idx, value => rnd.RandInt(arr. lower_limit, arr. upper_limit));
78+ for idx in 0 to length ( image ) - 1 loop
79+ set(image , idx, value => rnd.RandInt(lower_limit( image ), upper_limit( image ) ));
6480 end loop ;
65- end procedure ;
81+ return image ;
82+ end ;
6683
6784 procedure run_test is
6885 begin
@@ -71,18 +88,17 @@ begin
7188 wait until rising_edge (clk);
7289 start <= false ;
7390
74- wait until (stimuli_done and
75- data_check_done and
76- rising_edge (clk));
91+ wait until (
92+ stimuli_done and
93+ data_check_done and
94+ rising_edge (clk)
95+ );
7796 end procedure ;
7897
7998 procedure test_random_image(width , height : natural ) is
8099 begin
81- image .init_2d(width => width , height => height,
82- bit_width => input_tdata'length ,
83- is_signed => false );
84- randomize(image );
85- sobel_x(image , result => reference_image);
100+ image := randomize(width , height, input_tdata'length );
101+ ref_image := sobel_x(image );
86102 run_test;
87103 end procedure ;
88104
96112 test_random_image(16 , 1 );
97113 test_random_image(1 , 1 );
98114 elsif run(" test_input_file_against_output_file" ) then
99- image . load_csv(tb_path & " input.csv" );
100- reference_image. load_csv(tb_path & " output.csv" );
115+ image := load_csv(tb_path & " input.csv" );
116+ ref_image := load_csv(tb_path & " output.csv" );
101117 run_test;
102118 end if ;
103119 end loop ;
@@ -110,20 +126,22 @@ begin
110126 wait until start and rising_edge (clk);
111127 stimuli_done <= false ;
112128
113- report (" Sending image of size " &
114- to_string(image .width ) & "x" &
115- to_string(image .height));
129+ report (
130+ " Sending image of size " &
131+ to_string(width (image )) & "x" &
132+ to_string(height(image ))
133+ );
116134
117- for y in 0 to image . height- 1 loop
118- for x in 0 to image . width - 1 loop
135+ for y in 0 to height( image ) - 1 loop
136+ for x in 0 to width ( image ) - 1 loop
119137 wait until rising_edge (clk);
120138 input_tvalid <= '1' ;
121- if x = image . width - 1 then
139+ if x = width ( image ) - 1 then
122140 input_tlast <= '1' ;
123141 else
124142 input_tlast <= '0' ;
125143 end if ;
126- input_tdata <= to_unsigned (image . get (x, y), input_tdata'length );
144+ input_tdata <= to_unsigned (get (image , x, y), input_tdata'length );
127145 end loop ;
128146 end loop ;
129147
@@ -137,32 +155,36 @@ begin
137155 begin
138156 wait until start and rising_edge (clk);
139157 data_check_done <= false ;
140- for y in 0 to reference_image. height- 1 loop
141- for x in 0 to reference_image. width - 1 loop
158+ for y in 0 to height(ref_image) - 1 loop
159+ for x in 0 to width (ref_image) - 1 loop
142160 wait until output_tvalid = '1' and rising_edge (clk);
143- check_equal(output_tlast, x = reference_image. width - 1 );
144- check_equal(output_tdata, reference_image. get (x, y),
161+ check_equal(output_tlast, x = width (ref_image) - 1 );
162+ check_equal(output_tdata, get (ref_image, x, y),
145163 " x=" & to_string(x) & " y=" & to_string(y));
146164 end loop ;
147165 end loop ;
148- report (" Done checking image of size " &
149- to_string(reference_image.width ) & "x" &
150- to_string(reference_image.height));
166+ report (
167+ " Done checking image of size " &
168+ to_string(width (ref_image)) & "x" &
169+ to_string(height(ref_image))
170+ );
151171 data_check_done <= true ;
152172 end process ;
153173
154174 clk <= not clk after 1 ns ;
155175
156176 dut : entity work.sobel_x
157177 generic map (
158- data_width => input_tdata'length )
178+ data_width => input_tdata'length
179+ )
159180 port map (
160181 clk => clk,
161182 input_tvalid => input_tvalid,
162183 input_tlast => input_tlast,
163184 input_tdata => input_tdata,
164185 output_tvalid => output_tvalid,
165186 output_tlast => output_tlast,
166- output_tdata => output_tdata);
187+ output_tdata => output_tdata
188+ );
167189
168190end architecture ;
0 commit comments