2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
9 sys_clk : in std_logic;
10 sys_res_n : in std_logic;
13 p_spalte : out hspalte;
14 p_rget : out std_logic;
15 p_rdone : in std_logic;
17 p_wtake : out std_logic;
18 p_wdone : in std_logic;
20 p_finished : out std_logic;
23 finished : out std_logic
27 architecture beh of parser is
28 type PARSER_STATE is (SIDLE, SREAD_NEWNUMBER, SREAD_SPACE1_2,
29 SREAD_SPACE1_3, SREAD_SPACE2, SREAD_SPACE3, SREAD_OP1, SREAD_OP2,
30 SREAD_SIGN1, SREAD_NEXTBYTE, SREAD_CALCNUMBER1, SREAD_CALCNUMBER2, SCALC_1,
31 SCALC_14, SCALC_15, SCALC_2, SWRITE_CHAR0, SWRITE_CHAR1, SWRITE_CHAR2,
32 SWRITE_SIGN1, SWRITE_SIGN2, SDONE, SERROR1, SERROR2);
33 signal state_int, state_next : PARSER_STATE;
34 signal z_int, z_next, strich_int, strich_next, wtmp_int, wtmp_next : csigned;
35 signal punkt_int, punkt_next : csigned;
36 signal rbyte_int, rbyte_next : hbyte;
37 signal p_write_int, p_write_next : hbyte;
38 signal p_rget_int, p_rget_next : std_logic;
39 signal p_wtake_int, p_wtake_next : std_logic;
40 signal p_finished_int, p_finished_next : std_logic;
41 signal aktop_int, aktop_next : alu_ops;
42 signal opp_int, opp_next : alu_ops;
43 signal opcode_int, opcode_next : alu_ops;
44 signal op1_int, op1_next : csigned;
45 signal op2_int, op2_next : csigned;
46 signal do_calc_int, do_calc_next : std_logic;
47 signal goto_calcn1, goto_op1, goto_space3, goto_sign : std_logic;
48 signal z_sign_next, z_sign_int : std_logic;
49 signal firstz_next, firstz_int : boolean;
50 signal err_next, err_int : hstr_int;
51 signal errc_next, errc_int : hstr_int;
52 signal errc_tmp_next, errc_tmp_int : hstr_int;
54 signal opcode : alu_ops;
59 signal do_calc : std_logic;
60 signal calc_done : std_logic;
61 signal calc_error : std_logic;
63 instalu : entity work.alu(beh)
67 sys_res_n => sys_res_n,
69 calc_done => calc_done,
70 calc_error => calc_error,
78 p_write <= p_write_int;
80 p_wtake <= p_wtake_int;
81 p_finished <= p_finished_int;
86 do_calc <= do_calc_int;
88 process(sys_clk, sys_res_n)
90 if sys_res_n = '0' then
92 z_int <= (others => '0');
94 strich_int <= (others => '0');
95 punkt_int <= (others => '0');
96 wtmp_int <= (others => '0');
97 rbyte_int <= (others => '0');
106 p_spalte <= (others => '0');
108 p_write_int <= (others => '0');
110 p_finished_int <= '0';
111 opcode_int <= ALU_NOP;
112 op1_int <= (others => '0');
113 op2_int <= (others => '0');
116 elsif rising_edge(sys_clk) then
118 state_int <= state_next;
120 z_sign_int <= z_sign_next;
121 strich_int <= strich_next;
122 punkt_int <= punkt_next;
123 wtmp_int <= wtmp_next;
124 rbyte_int <= rbyte_next;
125 aktop_int <= aktop_next;
128 errc_int <= errc_next;
129 errc_tmp_int <= errc_tmp_next;
130 firstz_int <= firstz_next;
132 p_rget_int <= p_rget_next;
133 p_write_int <= p_write_next;
134 p_wtake_int <= p_wtake_next;
135 p_finished_int <= p_finished_next;
136 opcode_int <= opcode_next;
139 do_calc_int <= do_calc_next;
144 process(state_int, do_it, p_rdone, p_wdone, p_read, aktop_int, strich_int,
145 punkt_int, calc_done, wtmp_int, opp_int, goto_calcn1, goto_op1,
146 goto_space3, goto_sign, z_sign_int, err_int, errc_int, calc_error,
149 state_next <= state_int;
154 state_next <= SREAD_NEWNUMBER;
156 when SREAD_NEWNUMBER =>
157 state_next <= SREAD_SPACE1_2;
158 when SREAD_SPACE1_2 =>
159 if p_rdone = '1' then
160 state_next <= SREAD_SPACE2;
162 when SREAD_SPACE1_3 =>
163 if p_rdone = '1' then
164 state_next <= SREAD_SPACE3;
166 when SREAD_SPACE2 | SREAD_SPACE3 =>
167 if goto_calcn1 = '1' then
169 when SREAD_SPACE2 => state_next <= SREAD_NEXTBYTE;
170 when SREAD_SPACE3 => state_next <= SREAD_OP1;
171 when others => assert(false) report "wtf @ state2";
174 if goto_sign = '1' then
176 when SREAD_SPACE2 => state_next <= SREAD_SIGN1;
177 when others => assert(false) report "wtf @ state3";
180 if p_rdone = '0' then
181 if goto_calcn1 = '0' then
183 when SREAD_SPACE2 => state_next <= SREAD_SPACE1_2;
184 when SREAD_SPACE3 => state_next <= SREAD_SPACE1_3;
185 when others => assert(false) report "wtf @ state1";
190 if p_rdone = '0' then
191 state_next <= SREAD_NEXTBYTE;
193 when SREAD_NEXTBYTE =>
194 if p_rdone = '1' then
195 state_next <= SREAD_CALCNUMBER1;
197 when SREAD_CALCNUMBER1 =>
198 if goto_op1 = '1' then
199 state_next <= SREAD_OP1;
200 elsif goto_space3 = '1' then
201 state_next <= SREAD_SPACE3;
202 elsif calc_done = '1' then
203 state_next <= SREAD_CALCNUMBER2;
206 state_next <= SREAD_OP2;
208 if p_rdone = '0' then
209 if aktop_int /= ALU_NOP then
210 state_next <= SCALC_1;
213 when SREAD_CALCNUMBER2 =>
214 if p_rdone = '0' and calc_done = '0' then
215 state_next <= SREAD_NEXTBYTE;
218 if calc_done = '1' then
220 -- spezialfall: eine zwischenberechnung wird fuer diese
221 -- kombination benoetigt
222 when ALU_MUL | ALU_DIV =>
224 when ALU_ADD | ALU_SUB | ALU_DONE => state_next <= SCALC_14;
225 when others => state_next <= SCALC_2;
227 when others => state_next <= SCALC_2;
231 if calc_done = '0' then
232 state_next <= SCALC_15;
235 if calc_done = '1' then
236 state_next <= SCALC_2;
239 if calc_done = '0' then
240 if aktop_int = ALU_DONE then
241 state_next <= SWRITE_CHAR2;
243 state_next <= SREAD_NEWNUMBER;
247 if calc_done = '1' then
248 state_next <= SWRITE_CHAR1;
251 if p_wdone = '1' then
252 if strich_int < 10 then
253 if z_sign_int = '1' then
254 state_next <= SWRITE_SIGN1;
259 state_next <= SWRITE_CHAR2;
263 if p_wdone = '1' then
264 if errc_int <= 2 then
267 state_next <= SERROR2;
271 if p_wdone = '0' then
272 state_next <= SERROR1;
275 if p_wdone = '0' and calc_done = '0' then
276 state_next <= SWRITE_CHAR0;
279 if p_wdone = '0' then
280 state_next <= SWRITE_SIGN2;
283 if p_wdone = '1' then
288 if p_wdone = '0' and do_it = '0' then
295 when SERROR1 | SERROR2 | SDONE => null;
298 state_next <= SERROR1;
304 process(state_int, p_read, p_write_int, z_int, rbyte_int, p_rget_int,
305 strich_int, aktop_int, opp_int, opcode_int, op1_int, op2_int, op3, opM,
306 do_calc_int, wtmp_int, punkt_int, z_sign_int, err_int, errc_int,
307 errc_tmp_int, firstz_int, calc_done, calc_error)
308 function hbyte2csigned (x : hbyte) return csigned is
309 variable y : csigned;
312 when x"30" => y := x"00000000";
313 when x"31" => y := x"00000001";
314 when x"32" => y := x"00000002";
315 when x"33" => y := x"00000003";
316 when x"34" => y := x"00000004";
317 when x"35" => y := x"00000005";
318 when x"36" => y := x"00000006";
319 when x"37" => y := x"00000007";
320 when x"38" => y := x"00000008";
321 when x"39" => y := x"00000009";
322 when others => assert(false) report "hbyte2csigned: shouldn't happen";
325 end function hbyte2csigned;
327 function csigned2hbyte (x : csigned) return hbyte is
331 when x"00000000" => y := x"30";
332 when x"00000001" => y := x"31";
333 when x"00000002" => y := x"32";
334 when x"00000003" => y := x"33";
335 when x"00000004" => y := x"34";
336 when x"00000005" => y := x"35";
337 when x"00000006" => y := x"36";
338 when x"00000007" => y := x"37";
339 when x"00000008" => y := x"38";
340 when x"00000009" => y := x"39";
341 when others => assert(false) report "csigned2hbyte: shouldn't happen";
344 end function csigned2hbyte;
347 variable multmp : signed(((2*CBITS)-1) downto 0);
348 variable tmp : csigned;
350 type errstrings is array (natural range 1 to 3) of hstring;
351 constant error_str : errstrings := (
352 1 => " Division durch Null" & nul,
353 2 => " Syntax" & nul,
354 3 => " Over- bzw. Underflow" & nul
359 z_sign_next <= z_sign_int;
360 strich_next <= strich_int;
361 punkt_next <= punkt_int;
362 wtmp_next <= wtmp_int;
363 rbyte_next <= rbyte_int;
364 aktop_next <= aktop_int;
367 errc_next <= errc_int;
368 errc_tmp_next <= errc_tmp_int;
369 firstz_next <= firstz_int;
372 p_write_next <= p_write_int;
374 p_finished_next <= '0';
375 opcode_next <= opcode_int;
386 strich_next <= (others => '0');
387 punkt_next <= (0 => '1', others => '0');
389 when SREAD_NEWNUMBER =>
390 z_next <= (others => '0');
393 rbyte_next <= (others => '0');
394 p_write_next <= (others => '0');
395 aktop_next <= ALU_NOP;
396 when SREAD_NEXTBYTE | SREAD_SPACE1_2 | SREAD_SPACE1_3 =>
398 when SREAD_SPACE2 | SREAD_SPACE3 =>
404 when SREAD_SPACE2 => goto_sign <= '1';
405 when SREAD_SPACE3 => goto_calcn1 <= '1'; p_rget_next <= '1';
406 when others => assert(false) report "SREAD_SPACE2/3: shouldn't happen";
413 when SREAD_CALCNUMBER1 =>
415 -- '+', '-', '*', '/'
416 when x"2B" | x"2D" | x"2A" | x"2F" | x"00" =>
431 opcode_next <= ALU_MUL;
432 op2_next <= to_signed(10,CBITS);
433 firstz_next <= false;
438 when SREAD_CALCNUMBER2 =>
439 z_next <= op3 + hbyte2csigned(p_read);
444 when x"2B" => aktop_next <= ALU_ADD; -- '+'
445 when x"2D" => aktop_next <= ALU_SUB; -- '-'
446 when x"2A" => aktop_next <= ALU_MUL; -- '*'
447 when x"2F" => aktop_next <= ALU_DIV; -- '/'
448 when x"00" => aktop_next <= ALU_DONE; -- '\0'
450 when others => err_next <= 2;
455 if z_sign_int = '1' then
456 tmp := (not z_int) + 1;
464 when ALU_NOP | ALU_ADD | ALU_SUB =>
466 when ALU_SUB => op1_next <= (not tmp) + 1;
467 when others => op1_next <= tmp;
470 when ALU_ADD | ALU_SUB | ALU_DONE =>
471 opcode_next <= ALU_ADD;
472 op2_next <= strich_int;
473 when ALU_MUL | ALU_DIV =>
474 opcode_next <= ALU_MUL;
475 op2_next <= punkt_int;
476 when others => assert(false) report "SCALC_1/1: shouldn't happen!";
479 when ALU_MUL | ALU_DIV =>
481 when ALU_ADD | ALU_SUB | ALU_DONE | ALU_MUL | ALU_DIV =>
482 op1_next <= punkt_int;
483 opcode_next <= opp_int;
485 when others => assert(false) report "SCALC_1/2: shouldn't happen!";
487 when others => assert(false) report "SCALC_1/3: shouldn't happen!";
492 -- ueberpruefung kann man sich sparen, da diese ohnehin in
493 -- nextstate gemacht wird.
498 -- ueberpruefung kann man sich sparen, da diese ohnehin in
499 -- nextstate gemacht wird.
500 opcode_next <= ALU_ADD;
501 op2_next <= strich_int;
502 punkt_next <= (0 => '1', others => '0');
507 when ALU_NOP | ALU_ADD | ALU_SUB | ALU_MUL | ALU_DIV =>
509 when ALU_ADD | ALU_SUB | ALU_DONE =>
510 if aktop_int = ALU_DONE and op3 < 0 then
511 strich_next <= (not op3) + 1;
512 wtmp_next <= (not op3) + 1;
518 when ALU_MUL | ALU_DIV =>
520 when others => assert (false) report "SCALC_2/1: shouldn't happen!";
522 when others => assert (false) report "SCALC_2/2: shouldn't happen!";
524 -- aktuelle rechenoperation fuer naechste 'runde' uebernehmen
525 opp_next <= aktop_int;
528 op1_next <= strich_int;
529 opcode_next <= ALU_DIV;
530 op2_next <= to_signed(10,CBITS);
536 p_write_next <= csigned2hbyte(tmp);
539 strich_next <= wtmp_int;
544 if z_sign_int = '1' then
546 p_write_next <= x"2D";
548 assert(false) report "SWRITE_SIGN: shouldn't happen!";
553 p_write_next <= hbyte(to_unsigned (character'pos(error_str(err_int)(errc_int)),8));
554 errc_tmp_next <= errc_int - 1;
556 errc_next <= errc_tmp_int;
561 p_finished_next <= '1';
566 when SERROR1 | SERROR2 | SDONE => null;
568 if calc_error = '1' then
577 end architecture beh;