2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
8 sys_clk : in std_logic;
9 sys_res_n : in std_logic;
11 p_rget : out std_logic;
12 p_rdone : in std_logic;
14 p_wtake : out std_logic;
15 p_wdone : in std_logic;
17 p_finished : out std_logic;
20 finished : out std_logic
24 architecture beh of parser is
25 type PARSER_STATE is (SIDLE, SREAD_NEWNUMBER, SREAD_SPACE_GET,
26 SREAD_SPACE_GET_SIGN, SREAD_SPACE_PROC, SREAD_SPACE_PROC_SIGN, SREAD_OP1, SREAD_OP2,
27 SREAD_SIGN, SREAD_NEXTBYTE, SREAD_CALCNUMBER1, SREAD_CALCNUMBER2, SCALC_1,
28 SCALC_14, SCALC_15, SCALC_2, SWRITE_CHAR0, SWRITE_CHAR1, SWRITE_CHAR2,
29 SWRITE_SIGN1, SWRITE_SIGN2, SDONE, SERROR1, SERROR2);
30 signal state_int, state_next : PARSER_STATE;
31 signal z_int, z_next, strich_int, strich_next, wtmp_int, wtmp_next : csigned;
32 signal punkt_int, punkt_next : csigned;
33 signal rbyte_int, rbyte_next : hbyte;
34 signal p_write_int, p_write_next : hbyte;
35 signal p_rget_int, p_rget_next : std_logic;
36 signal p_wtake_int, p_wtake_next : std_logic;
37 signal p_finished_int, p_finished_next : std_logic;
38 signal finished_int, finished_next : std_logic;
39 signal aktop_int, aktop_next : alu_ops;
40 signal opp_int, opp_next : alu_ops;
41 signal opcode_int, opcode_next : alu_ops;
42 signal op1_int, op1_next : csigned;
43 signal op2_int, op2_next : csigned;
44 signal do_calc_int, do_calc_next : std_logic;
45 signal z_sign_next, z_sign_int : std_logic;
46 signal firstz_next, firstz_int : boolean;
47 signal err_next, err_int : hstr_int;
48 signal errc_next, errc_int : hstr_int;
49 signal errc_tmp_next, errc_tmp_int : hstr_int;
51 signal opcode : alu_ops;
56 signal do_calc : std_logic;
57 signal calc_done : std_logic;
58 signal calc_error : std_logic;
60 instalu : entity work.alu(beh)
64 sys_res_n => sys_res_n,
66 calc_done => calc_done,
67 calc_error => calc_error,
75 p_write <= p_write_int;
77 p_wtake <= p_wtake_int;
78 p_finished <= p_finished_int;
79 finished <= finished_int;
84 do_calc <= do_calc_int;
86 process(sys_clk, sys_res_n)
88 if sys_res_n = '0' then
90 z_int <= (others => '0');
92 strich_int <= (others => '0');
93 punkt_int <= (others => '0');
94 wtmp_int <= (others => '0');
95 rbyte_int <= (others => '0');
104 p_write_int <= (others => '0');
106 p_finished_int <= '0';
108 opcode_int <= ALU_NOP;
109 op1_int <= (others => '0');
110 op2_int <= (others => '0');
112 elsif rising_edge(sys_clk) then
114 state_int <= state_next;
116 z_sign_int <= z_sign_next;
117 strich_int <= strich_next;
118 punkt_int <= punkt_next;
119 wtmp_int <= wtmp_next;
120 rbyte_int <= rbyte_next;
121 aktop_int <= aktop_next;
124 errc_int <= errc_next;
125 errc_tmp_int <= errc_tmp_next;
126 firstz_int <= firstz_next;
128 p_rget_int <= p_rget_next;
129 p_write_int <= p_write_next;
130 p_wtake_int <= p_wtake_next;
131 p_finished_int <= p_finished_next;
132 finished_int <= finished_next;
133 opcode_int <= opcode_next;
136 do_calc_int <= do_calc_next;
141 process(do_it, p_rdone, p_wdone, p_read, aktop_int, strich_int, punkt_int,
142 calc_done, wtmp_int, opp_int, z_sign_int, err_int, errc_int,
143 calc_error, op2_int, state_int, p_write_int, z_int, rbyte_int,
144 p_rget_int, opcode_int, op1_int, op3, opM, do_calc_int,
145 errc_tmp_int, firstz_int)
146 function hbyte2csigned (x : hbyte) return csigned is
147 variable y : csigned;
150 when x"30" => y := x"00000000";
151 when x"31" => y := x"00000001";
152 when x"32" => y := x"00000002";
153 when x"33" => y := x"00000003";
154 when x"34" => y := x"00000004";
155 when x"35" => y := x"00000005";
156 when x"36" => y := x"00000006";
157 when x"37" => y := x"00000007";
158 when x"38" => y := x"00000008";
159 when x"39" => y := x"00000009";
160 when others => assert(false) report "hbyte2csigned: shouldn't happen";
163 end function hbyte2csigned;
165 function csigned2hbyte (x : csigned) return hbyte is
169 when x"00000000" => y := x"30";
170 when x"00000001" => y := x"31";
171 when x"00000002" => y := x"32";
172 when x"00000003" => y := x"33";
173 when x"00000004" => y := x"34";
174 when x"00000005" => y := x"35";
175 when x"00000006" => y := x"36";
176 when x"00000007" => y := x"37";
177 when x"00000008" => y := x"38";
178 when x"00000009" => y := x"39";
179 when others => assert(false) report "csigned2hbyte: shouldn't happen";
182 end function csigned2hbyte;
184 variable multmp : signed(((2*CBITS)-1) downto 0);
185 variable tmp : csigned;
187 type errstrings is array (natural range 1 to 3) of hstring;
188 constant error_str : errstrings := (
189 1 => " Fehler: Division durch Null " & nul,
190 2 => " Fehler: Syntax " & nul,
191 3 => " Fehler: Over- bzw. Underflow " & nul
194 state_next <= state_int;
197 z_sign_next <= z_sign_int;
198 strich_next <= strich_int;
199 punkt_next <= punkt_int;
200 wtmp_next <= wtmp_int;
201 rbyte_next <= rbyte_int;
202 aktop_next <= aktop_int;
205 errc_next <= errc_int;
206 errc_tmp_next <= errc_tmp_int;
207 firstz_next <= firstz_int;
210 p_write_next <= p_write_int;
212 p_finished_next <= '0';
213 finished_next <= '0';
214 opcode_next <= opcode_int;
221 strich_next <= (others => '0');
222 punkt_next <= (0 => '1', others => '0');
225 state_next <= SREAD_NEWNUMBER;
228 when SREAD_NEWNUMBER =>
229 z_next <= (others => '0');
232 rbyte_next <= (others => '0');
233 p_write_next <= (others => '0');
234 aktop_next <= ALU_NOP;
235 state_next <= SREAD_SPACE_GET;
237 when SREAD_SPACE_GET =>
239 if p_rdone = '1' then
240 state_next <= SREAD_SPACE_PROC;
242 when SREAD_SPACE_GET_SIGN =>
244 if p_rdone = '1' then
245 state_next <= SREAD_SPACE_PROC_SIGN;
247 when SREAD_SPACE_PROC | SREAD_SPACE_PROC_SIGN =>
252 when SREAD_SPACE_PROC =>
254 when SREAD_SPACE_PROC => state_next <= SREAD_SIGN;
255 when others => assert(false) report "wtf @ state3";
258 when SREAD_SPACE_PROC_SIGN =>
261 when SREAD_SPACE_PROC => state_next <= SREAD_NEXTBYTE;
262 when SREAD_SPACE_PROC_SIGN => state_next <= SREAD_OP1;
263 when others => assert(false) report "wtf @ state2";
266 when others => assert(false) report "SREAD_SPACE_PROC/3: shouldn't happen";
271 when SREAD_SPACE_PROC => state_next <= SREAD_NEXTBYTE;
272 when SREAD_SPACE_PROC_SIGN => state_next <= SREAD_OP1;
273 when others => assert(false) report "wtf @ state2";
277 if p_rdone = '0' then
279 when SREAD_SPACE_PROC => state_next <= SREAD_SPACE_GET;
280 when SREAD_SPACE_PROC_SIGN => state_next <= SREAD_SPACE_GET_SIGN;
281 when others => assert(false) report "wtf @ state1";
286 if p_rdone = '0' then
287 state_next <= SREAD_NEXTBYTE;
289 when SREAD_NEXTBYTE =>
291 if p_rdone = '1' then
292 state_next <= SREAD_CALCNUMBER1;
294 when SREAD_CALCNUMBER1 =>
296 -- '+', '-', '*', '/'
297 when x"2B" | x"2D" | x"2A" | x"2F" | x"00" =>
301 state_next <= SREAD_OP1;
307 state_next <= SREAD_SPACE_PROC_SIGN;
312 opcode_next <= ALU_MUL;
313 op2_next <= to_signed(10,CBITS);
314 firstz_next <= false;
317 if calc_done = '1' then
318 state_next <= SREAD_CALCNUMBER2;
320 when SREAD_CALCNUMBER2 =>
321 z_next <= op3 + hbyte2csigned(p_read);
322 if p_rdone = '0' and calc_done = '0' then
323 state_next <= SREAD_NEXTBYTE;
328 when x"2B" => aktop_next <= ALU_ADD; -- '+'
329 when x"2D" => aktop_next <= ALU_SUB; -- '-'
330 when x"2A" => aktop_next <= ALU_MUL; -- '*'
331 when x"2F" => aktop_next <= ALU_DIV; -- '/'
332 when x"00" => aktop_next <= ALU_DONE; -- '\0'
334 when others => err_next <= 2;
336 state_next <= SREAD_OP2;
338 if p_rdone = '0' then
339 if aktop_int /= ALU_NOP then
340 state_next <= SCALC_1;
345 if z_sign_int = '1' then
346 tmp := (not z_int) + 1;
354 when ALU_NOP | ALU_ADD | ALU_SUB =>
357 -- xst (xilinx) workaround
358 if x"80000000" = tmp then
359 -- vgl. testfall 37 und 38
363 op1_next <= (not tmp) + 1;
365 when others => op1_next <= tmp;
368 when ALU_ADD | ALU_SUB | ALU_DONE =>
369 opcode_next <= ALU_ADD;
370 op2_next <= strich_int;
371 when ALU_MUL | ALU_DIV =>
372 opcode_next <= ALU_MUL;
373 op2_next <= punkt_int;
374 when others => assert(false) report "SCALC_1/1: shouldn't happen!";
377 when ALU_MUL | ALU_DIV =>
379 when ALU_ADD | ALU_SUB | ALU_DONE | ALU_MUL | ALU_DIV =>
380 op1_next <= punkt_int;
381 opcode_next <= opp_int;
383 when others => assert(false) report "SCALC_1/2: shouldn't happen!";
385 when others => assert(false) report "SCALC_1/3: shouldn't happen!";
389 if calc_done = '1' then
391 -- spezialfall: eine zwischenberechnung wird fuer diese
392 -- kombination benoetigt
393 when ALU_MUL | ALU_DIV =>
395 when ALU_ADD | ALU_SUB | ALU_DONE => state_next <= SCALC_14;
396 when others => state_next <= SCALC_2;
398 when others => state_next <= SCALC_2;
402 -- ueberpruefung kann man sich sparen, da diese ohnehin in
403 -- nextstate gemacht wird.
407 if calc_done = '0' then
408 state_next <= SCALC_15;
411 -- ueberpruefung kann man sich sparen, da diese ohnehin in
412 -- nextstate gemacht wird.
413 opcode_next <= ALU_ADD;
414 op2_next <= strich_int;
415 punkt_next <= (0 => '1', others => '0');
418 if calc_done = '1' then
419 state_next <= SCALC_2;
423 when ALU_NOP | ALU_ADD | ALU_SUB | ALU_MUL | ALU_DIV =>
425 when ALU_ADD | ALU_SUB | ALU_DONE =>
426 if aktop_int = ALU_DONE and op3 < 0 then
427 strich_next <= (not op3) + 1;
428 wtmp_next <= (not op3) + 1;
434 when ALU_MUL | ALU_DIV =>
436 when others => assert (false) report "SCALC_2/1: shouldn't happen!";
438 when ALU_DONE => null;
439 when others => assert (false) report "SCALC_2/2: shouldn't happen!";
441 -- aktuelle rechenoperation fuer naechste 'runde' uebernehmen
442 opp_next <= aktop_int;
444 if calc_done = '0' then
445 if aktop_int = ALU_DONE then
446 state_next <= SWRITE_CHAR2;
448 state_next <= SREAD_NEWNUMBER;
453 -- fuer testfall 39 und 40
454 if strich_int = to_signed(-214748364,CBITS) then
455 op1_next <= to_signed(214748364,CBITS);
456 strich_next <= to_signed(214748364,CBITS);
458 op1_next <= strich_int;
460 opcode_next <= ALU_DIV;
461 op2_next <= to_signed(10,CBITS);
464 if calc_done = '1' then
465 state_next <= SWRITE_CHAR1;
471 p_write_next <= csigned2hbyte(tmp);
474 if p_wdone = '1' then
475 -- ueberpruefung auf -2147483648 fuer testfall 39 und 40
476 -- x"80000000": xst (xilinx) workaround
477 if strich_int < 10 and strich_int /= x"80000000" then
478 if z_sign_int = '1' then
479 state_next <= SWRITE_SIGN1;
484 state_next <= SWRITE_CHAR2;
488 strich_next <= wtmp_int;
489 if p_wdone = '0' and calc_done = '0' then
490 state_next <= SWRITE_CHAR0;
494 if p_wdone = '0' then
495 state_next <= SWRITE_SIGN2;
498 if z_sign_int = '1' then
500 p_write_next <= x"2D";
502 assert(false) report "SWRITE_SIGN: shouldn't happen!";
505 if p_wdone = '1' then
510 if p_wdone = '1' then
511 if errc_int <= 2 then
514 state_next <= SERROR2;
518 if p_wdone = '0' then
519 state_next <= SERROR1;
525 p_finished_next <= '1';
526 finished_next <= '1';
528 if p_wdone = '0' and do_it = '0' then
537 p_write_next <= hbyte(to_unsigned (character'pos(error_str(err_int)(errc_int)),8));
538 errc_tmp_next <= errc_int - 1;
540 errc_next <= errc_tmp_int;
543 if calc_error = '1' then
552 state_next <= SERROR1;
556 end architecture beh;