scanner: rewrite fuer key-pressed only. TODO: testbench
[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); last : 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                 variable last : std_logic_vector(7 downto 0);
108         begin
109                 -- init & reset
110                 sys_res_n <= '0';
111                 new_data <= '0';
112                 data <= (others => '0');
113                 s_done <= '0';
114                 finished <= '0';
115
116                 icwait(sys_clk, 5);
117                 sys_res_n <= '1';
118
119                 i := 1;
120                 f_loop : while not endfile(f) loop
121                         data <= (others => '0');
122                         realresult := (others => nul);
123
124                         f1_loop : while not endfile(f) loop
125                                 readline (f, l);
126                                 input := (others => nul);
127                                 if (l'length <= 72) then
128                                         input(1 to l'length) := l.all;
129                                         if (input(1) = '#') then
130                                                 next f1_loop;
131                                         else
132                                                 exit f1_loop;
133                                         end if;
134                                 else
135                                         report "fehler in scanner.test: eingabe zu lange in testfall " & natural'image(i);
136                                         next f_loop;
137                                 end if;
138                         end loop f1_loop;
139
140                         f2_loop : while not endfile(f) loop
141                                 readline (f, l);
142                                 expectedresult := (others => nul);
143                                 if (l'length <= 72) then
144                                         expectedresult(1 to l'length) := l.all;
145                                         if (expectedresult(1) = '#') then
146                                                 next f2_loop;
147                                         else
148                                                 y := l'length;
149                                                 exit f2_loop;
150                                         end if;
151                                 else
152                                         report "fehler in scanner.test: eingabe zu lange in testfall " & natural'image(i);
153                                         next f_loop;
154                                 end if;
155                         end loop f2_loop;
156
157
158
159                         report "testcase(" & natural'image(i) & ").input: " & input;
160                         report "testcase(" & natural'image(i) & ").expectedresult: " & expectedresult;
161                         i := i + 1;
162
163                         icwait(sys_clk, 5);
164                         run_tc := true;
165                         j := 0; k := 1;
166
167                         mainl : while run_tc loop
168                                 last := data;
169                                 icwait(sys_clk, 1);
170                                 j := j + 1;
171
172                                 if j = 73 then
173                                         run_tc := false;
174                                         assert(false) report "wtf @ schleife";
175                                         next mainl;
176                                 end if;
177
178                                 new_data <= '1';
179
180                                 case input(j) is
181                                         when nul => data <= ascii2sc(x"1c"); -- $ (enter)
182                                         when '!' => data <= ascii2sc(x"0e"); -- ! (backspace)
183                                         when others => data <= ascii2sc(std_logic_vector(to_unsigned(character'pos(input(j)),8)));
184                                 end case;
185                                 icwait(sys_clk, 1);
186                                 new_data <= '0';
187
188                                 -- ack'en skippen, falls es ein "spezielles" zeichen ist (steht
189                                 -- in abhaengigkeit zum vorherigen zeichen)
190                                 if(not valid_char(data, last)) then
191                                         next mainl;
192                                 end if;
193
194                                 -- wuenschswert waere das hier:
195                                 -- > wait on s_backspace, s_take, do_it;
196                                 -- geht aber leider nicht, weil sich die signale vllt schon
197                                 -- geaendert haben
198                                 run_inner := true;
199                                 main_inner : while run_inner loop
200                                         icwait(sys_clk, 1);
201
202                                         run_inner := false;
203                                         if s_backspace = '1' then
204                                                 if k > 1 then
205                                                         realresult(k) := nul;
206                                                         k := k - 1;
207                                                         realresult(k) := nul;
208                                                 end if;
209                                                 icwait(sys_clk, 1);
210                                                 s_done <= '1';
211                                                 wait on s_take; -- = '0'
212                                                 icwait(sys_clk, 1);
213                                                 s_done <= '0';
214                                         elsif do_it = '1' then
215                                                 -- dauert normalweiser noch laenger (parser braucht
216                                                 -- relativ lange)
217                                                 icwait(sys_clk, 7);
218                                                 finished <= '1';
219                                                 wait on do_it; -- = '0'
220                                                 icwait(sys_clk, 1);
221                                                 finished <= '0';
222
223                                                 run_tc := false;
224                                         elsif s_take = '1' then
225                                                 realresult(k) := character'val(to_integer(unsigned(s_char)));
226                                                 k := k + 1;
227
228                                                 icwait(sys_clk, 1);
229                                                 s_done <= '1';
230                                                 wait on s_take; -- = '0'
231                                                 icwait(sys_clk, 1);
232                                                 s_done <= '0';
233                                         else
234                                                 -- assert(false) report "scanner_tb: kann passieren. wenn tb haengt, dann hier auskommentieren";
235                                                 run_inner := true;
236                                         end if;
237                                 end loop;
238                         end loop;
239
240                         report "realresult                : " & realresult;
241                         if realresult /= expectedresult then
242                                 checkall := false;
243                         end if;
244                         report "==================";
245                 end loop f_loop;
246
247                 if checkall then
248                         report "alle testfaelle des Scanners waren erfolgreich!";
249                 else
250                         report "nicht alle testfaelle des Scanners waren erfolgreich!";
251                 end if;
252                 stop <= true;
253                 wait;
254         end process;
255 end architecture sim;