parser: / und mod in hardware
[hwmod.git] / src / beh_parser_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_parser_tb is
7 end entity beh_parser_tb;
8
9 architecture sim of beh_parser_tb is
10         -- system
11         signal sys_clk, sys_res_n : std_logic;
12         -- history
13         signal p_rw, p_rget, p_rdone, p_wtake, p_wdone, p_finished : std_logic;
14         signal p_read, p_write : hbyte;
15         signal p_spalte : hspalte;
16
17         -- alu
18         signal opcode : alu_ops;
19         signal op1, op2, op3, opM : csigned;
20         signal do_calc, calc_done, calc_error : std_logic;
21
22         --scanner
23         signal do_it : std_logic;
24         signal finished : std_logic;
25
26         signal stop : boolean := false;
27 begin
28         inst : entity work.parser(beh)
29         port map
30         (
31                 sys_clk => sys_clk,
32                 sys_res_n => sys_res_n,
33                 p_rw => p_rw,
34                 p_spalte => p_spalte,
35                 p_rget => p_rget,
36                 p_rdone => p_rdone,
37                 p_read => p_read,
38                 p_wtake => p_wtake,
39                 p_wdone => p_wdone,
40                 p_write => p_write,
41                 p_finished => p_finished,
42                 -- ALU
43                 opcode => opcode,
44                 op1 => op1,
45                 op2 => op2,
46                 op3 => op3,
47                 opM => opM,
48                 do_calc => do_calc,
49                 calc_done => calc_done,
50                 calc_error => calc_error,
51                 -- TODO: calc_error : in std_logic;
52                 -- Scanner
53                 do_it => do_it,
54                 finished => finished
55         );
56
57         instalu : entity work.alu(beh)
58         port map
59         (
60                 sys_clk => sys_clk,
61                 sys_res_n => sys_res_n,
62                 do_calc => do_calc,
63                 calc_done => calc_done,
64                 calc_error => calc_error,
65                 op1 => op1,
66                 op2 => op2,
67                 op3 => op3,
68                 opM => opM,
69                 opcode => opcode
70         );
71
72         process
73         begin
74                 sys_clk <= '0';
75                 wait for 15 ns;
76                 sys_clk <= '1';
77                 wait for 15 ns;
78                 if stop = true then
79                         wait;
80                 end if;
81         end process;
82
83         process
84                 -- textio stuff
85                 use std.textio.all;
86                 file f : text open read_mode is "../../src/parser.test";
87                 variable l : line;
88
89                 variable input : hstring;
90                 variable expectedresult : hstring;
91                 variable realresult : hstring;
92                 variable hstrtmp : hstring;
93
94                 variable checkall : boolean := true;
95                 variable run_tc : boolean := true;
96                 variable i, j, k, y : natural;
97         begin
98                 -- init & reset
99                 sys_res_n <= '0';
100                 p_rdone <= '0';
101                 p_wdone <= '0';
102                 p_read <= (others => '0');
103                 do_it <= '0';
104
105                 icwait(sys_clk, 5);
106                 sys_res_n <= '1';
107
108                 i := 1;
109                 f_loop : while not endfile(f) loop
110                         realresult := (71 => nul, others => ' ');
111
112                         f1_loop : while not endfile(f) loop
113                                 readline (f, l);
114                                 input := (others => nul);
115                                 if (l'length <= 72) then
116                                         input(1 to l'length) := l.all;
117                                         if (input(1) = '#') then
118                                                 next f1_loop;
119                                         else
120                                                 exit f1_loop;
121                                         end if;
122                                 else
123                                         report "fehler in parser.test: eingabe zu lange in testfall " & natural'image(i);
124                                         next f_loop;
125                                 end if;
126                         end loop f1_loop;
127
128                         f2_loop : while not endfile(f) loop
129                                 readline (f, l);
130                                 expectedresult := (others => nul);
131                                 if (l'length <= 72) then
132                                         expectedresult(1 to l'length) := l.all;
133                                         if (expectedresult(1) = '#') then
134                                                 next f2_loop;
135                                         else
136                                                 y := l'length;
137                                                 exit f2_loop;
138                                         end if;
139                                 else
140                                         report "fehler in parser.test: eingabe zu lange in testfall " & natural'image(i);
141                                         next f_loop;
142                                 end if;
143                         end loop f2_loop;
144
145                         -- ergebnis string richtig formatieren
146                         hstrtmp := expectedresult;
147                         expectedresult := (71 => nul, others => ' ');
148                         for x in 1 to 70 loop
149                                 if hstrtmp(x) /= nul then
150                                         expectedresult((70-y) + x) := hstrtmp(x);
151                                 end if;
152                         end loop;
153
154
155                         report "testcase(" & natural'image(i) & ").input: " & input;
156                         report "testcase(" & natural'image(i) & ").expectedresult: " & expectedresult;
157                         i := i + 1;
158
159                         icwait(sys_clk, 5);
160                         do_it <= '1';
161                         run_tc := true;
162                         j := 1; k := 70;
163
164                         while run_tc loop
165                                 wait on p_rget, p_wtake, p_finished, finished;
166                                 icwait(sys_clk, 2);
167
168                                 if p_rget = '1' then
169                                         p_read <= hbyte( to_unsigned(character'pos(input(j)),8) );
170                                         p_rdone <= '1';
171                                         j := j + 1;
172                                 end if;
173                                 if p_rget = '0' then
174                                         p_rdone <= '0';
175                                 end if;
176
177                                 if p_wtake = '1' then
178                                         realresult(k) := character'val(to_integer(unsigned(p_write)));
179                                         p_wdone <= '1';
180                                         k := k - 1;
181                                 end if;
182                                 if p_wtake = '0' then
183                                         p_wdone <= '0';
184                                 end if;
185
186                                 if p_finished = '1' or finished = '1' then
187                                         run_tc := false;
188                                 end if;
189                         end loop;
190                         
191                         do_it <= '0';
192                         report "realresult                : " & realresult;
193                         if realresult /= expectedresult then
194                                 checkall := false;
195                         end if;
196                         report "==================";
197                 end loop f_loop;
198
199                 if checkall then
200                         report "alle testfaelle des Parser waren erfolgreich!";
201                 else
202                         report "nicht alle testfaelle des Parsers waren erfolgreich!";
203                 end if;
204                 stop <= true;
205                 wait;
206         end process;
207 end architecture sim;