scanner: beh- und post-tb angepasst
[hwmod.git] / src / beh_scanner_tb.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use work.gen_pkg.all;
5
6 entity beh_scanner_tb is
7 end entity beh_scanner_tb;
8
9 architecture sim of beh_scanner_tb is
10         -- system
11         signal sys_clk, sys_res_n : std_logic;
12         -- ps/2
13         signal new_data : std_logic;
14         signal data : std_logic_vector(7 downto 0);
15         -- history
16         signal s_char : hbyte;
17         signal s_take, s_done, s_backspace : std_logic;
18         -- parser
19         signal do_it : std_logic;
20         signal finished : std_logic;
21
22         signal stop : boolean := false;
23 begin
24         inst : entity work.scanner(beh)
25         port map (
26                 sys_clk => sys_clk,
27                 sys_res_n => sys_res_n,
28                 -- ps/2
29                 new_data => new_data,
30                 data => data,
31                 -- history
32                 s_char => s_char,
33                 s_take => s_take,
34                 s_done => s_done,
35                 s_backspace => s_backspace,
36                 -- Parser
37                 do_it => do_it,
38                 finished => finished
39         );
40
41         process
42         begin
43                 sys_clk <= '0';
44                 wait for 15 ns;
45                 sys_clk <= '1';
46                 wait for 15 ns;
47                 if stop = true then
48                         wait;
49                 end if;
50         end process;
51
52         process
53                 function ascii2sc (x : hbyte) return hbyte is
54                         variable y : hbyte;
55                 begin
56                         case x is
57                                 when x"30" => y := SC_KP_0;
58                                 when x"31" => y := SC_KP_1;
59                                 when x"32" => y := SC_KP_2;
60                                 when x"33" => y := SC_KP_3;
61                                 when x"34" => y := SC_KP_4;
62                                 when x"35" => y := SC_KP_5;
63                                 when x"36" => y := SC_KP_6;
64                                 when x"37" => y := SC_KP_7;
65                                 when x"38" => y := SC_KP_8;
66                                 when x"39" => y := SC_KP_9;
67                                 when x"2b" => y := SC_KP_PLUS;
68                                 when x"2d" => y := SC_KP_MINUS;
69                                 when x"2a" => y := SC_KP_MUL;
70                                 when x"2f" => y := SC_KP_DIV;
71                                 when x"20" => y := SC_SPACE;
72                                 when x"1c" => y := SC_ENTER;
73                                 when x"0e" => y := SC_BKSP;
74                                 when others => y := x"41";
75                         end case;
76                         return y;
77                 end function;
78
79                 function valid_char (x : std_logic_vector(7 downto 0)) return boolean is
80                                 variable y : boolean;
81                 begin
82                         case x is
83                                 when SC_KP_0 | SC_KP_1 | SC_KP_2 | SC_KP_3 |
84                                         SC_KP_4 | SC_KP_5 | SC_KP_6 | SC_KP_7 |
85                                         SC_KP_8 | SC_KP_9 | SC_KP_PLUS |
86                                         SC_KP_MINUS | SC_KP_MUL |
87                                         SC_KP_DIV | SC_SPACE |
88                                         SC_BKSP | SC_ENTER =>
89                                                 y := true;
90                                 when others => y := false;
91                         end case;
92                         return y;
93                 end function;
94
95                 -- textio stuff
96                 use std.textio.all;
97                 file f : text open read_mode is "../../src/scanner.test";
98                 variable l : line;
99
100                 variable input : hstring;
101                 variable expectedresult : hstring;
102                 variable realresult : hstring;
103
104                 variable checkall : boolean := true;
105                 variable run_tc, run_inner : boolean := true;
106                 variable i, j, k, y : natural;
107         begin
108                 -- init & reset
109                 sys_res_n <= '0';
110                 new_data <= '0';
111                 data <= (others => '0');
112                 s_done <= '0';
113                 finished <= '0';
114
115                 icwait(sys_clk, 5);
116                 sys_res_n <= '1';
117
118                 i := 1;
119                 f_loop : while not endfile(f) loop
120                         data <= (others => '0');
121                         realresult := (others => nul);
122
123                         f1_loop : while not endfile(f) loop
124                                 readline (f, l);
125                                 input := (others => nul);
126                                 if (l'length <= 72) then
127                                         input(1 to l'length) := l.all;
128                                         if (input(1) = '#') then
129                                                 next f1_loop;
130                                         else
131                                                 exit f1_loop;
132                                         end if;
133                                 else
134                                         report "fehler in scanner.test: eingabe zu lange in testfall " & natural'image(i);
135                                         next f_loop;
136                                 end if;
137                         end loop f1_loop;
138
139                         f2_loop : while not endfile(f) loop
140                                 readline (f, l);
141                                 expectedresult := (others => nul);
142                                 if (l'length <= 72) then
143                                         expectedresult(1 to l'length) := l.all;
144                                         if (expectedresult(1) = '#') then
145                                                 next f2_loop;
146                                         else
147                                                 y := l'length;
148                                                 exit f2_loop;
149                                         end if;
150                                 else
151                                         report "fehler in scanner.test: eingabe zu lange in testfall " & natural'image(i);
152                                         next f_loop;
153                                 end if;
154                         end loop f2_loop;
155
156
157
158                         report "testcase(" & natural'image(i) & ").input: " & input;
159                         report "testcase(" & natural'image(i) & ").expectedresult: " & expectedresult;
160                         i := i + 1;
161
162                         icwait(sys_clk, 5);
163                         run_tc := true;
164                         j := 0; k := 1;
165
166                         mainl : while run_tc loop
167                                 icwait(sys_clk, 1);
168                                 j := j + 1;
169
170                                 if j = 73 then
171                                         run_tc := false;
172                                         assert(false) report "wtf @ schleife";
173                                         next mainl;
174                                 end if;
175
176                                 new_data <= '1';
177
178                                 case input(j) is
179                                         when nul => data <= ascii2sc(x"1c"); -- $ (enter)
180                                         when '!' => data <= ascii2sc(x"0e"); -- ! (backspace)
181                                         when '/' =>
182                                                 data <= x"e0";
183                                                 icwait(sys_clk, 1);
184                                                 new_data <= '0';
185                                                 icwait(sys_clk, 1);
186                                                 new_data <= '1';
187                                                 data <= SC_KP_DIV;
188                                         when others => data <= ascii2sc(std_logic_vector(to_unsigned(character'pos(input(j)),8)));
189                                 end case;
190                                 icwait(sys_clk, 1);
191                                 new_data <= '0';
192
193                                 -- ack'en skippen, falls es ein "spezielles" zeichen ist
194                                 if(not valid_char(data)) then
195                                         next mainl;
196                                 end if;
197
198                                 -- wuenschswert waere das hier:
199                                 -- > wait on s_backspace, s_take, do_it;
200                                 -- geht aber leider nicht, weil sich die signale vllt schon
201                                 -- geaendert haben
202                                 run_inner := true;
203                                 main_inner : while run_inner loop
204                                         icwait(sys_clk, 1);
205
206                                         run_inner := false;
207                                         if s_backspace = '1' then
208                                                 if k > 1 then
209                                                         realresult(k) := nul;
210                                                         k := k - 1;
211                                                         realresult(k) := nul;
212                                                 end if;
213                                                 icwait(sys_clk, 1);
214                                                 s_done <= '1';
215                                                 wait on s_take; -- = '0'
216                                                 icwait(sys_clk, 1);
217                                                 s_done <= '0';
218                                         elsif do_it = '1' then
219                                                 -- dauert normalweiser noch laenger (parser braucht
220                                                 -- relativ lange)
221                                                 icwait(sys_clk, 7);
222                                                 finished <= '1';
223                                                 wait on do_it; -- = '0'
224                                                 icwait(sys_clk, 1);
225                                                 finished <= '0';
226
227                                                 run_tc := false;
228                                         elsif s_take = '1' then
229                                                 realresult(k) := character'val(to_integer(unsigned(s_char)));
230                                                 k := k + 1;
231
232                                                 icwait(sys_clk, 1);
233                                                 s_done <= '1';
234                                                 wait on s_take; -- = '0'
235                                                 icwait(sys_clk, 1);
236                                                 s_done <= '0';
237                                         else
238                                                 -- assert(false) report "scanner_tb: kann passieren. wenn tb haengt, dann hier auskommentieren";
239                                                 run_inner := true;
240                                         end if;
241                                 end loop;
242                         end loop;
243
244                         report "realresult                : " & realresult;
245                         if realresult /= expectedresult then
246                                 checkall := false;
247                         end if;
248                         report "==================";
249                 end loop f_loop;
250
251                 if checkall then
252                         report "alle testfaelle des Scanners waren erfolgreich!";
253                 else
254                         report "nicht alle testfaelle des Scanners waren erfolgreich!";
255                 end if;
256                 stop <= true;
257                 wait;
258         end process;
259 end architecture sim;