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