scanner: rewrite fuer key-pressed only. TODO: testbench
[hwmod.git] / src / scanner.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 scanner is
7         port
8         (
9                 sys_clk : in std_logic;
10                 sys_res_n : in std_logic;
11                 -- PS/2
12                 new_data : in std_logic;
13                 data : in std_logic_vector(7 downto 0);
14                 -- History
15                 s_char : out hbyte;
16                 s_take : out std_logic;
17                 s_done : in std_logic;
18                 s_backspace : out std_logic;
19                 -- Parser
20                 do_it : out std_logic;
21                 finished : in std_logic
22         );
23 end entity scanner;
24
25 architecture beh of scanner is
26         type SCANNER_STATE is (SIDLE, SIGNORE_NEXT, SREAD_NEXT, STAKE, SDEL, SENTER);
27         signal state_int, state_next : SCANNER_STATE;
28         signal s_char_int, s_char_next : hbyte;
29         signal s_take_int, s_take_next : std_logic;
30         signal s_backspace_int, s_backspace_next : std_logic;
31         signal do_it_int, do_it_next : std_logic;
32 begin
33         s_char <= s_char_int;
34         s_take <= s_take_int;
35         s_backspace <= s_backspace_int;
36         do_it <= do_it_int;
37
38         process(sys_clk, sys_res_n)
39         begin
40                 if sys_res_n = '0' then
41                         -- internal
42                         state_int <= SIDLE;
43                         -- out
44                         s_char_int <= (others => '0');
45                         s_take_int <= '0';
46                         s_backspace_int <= '0';
47                         do_it_int <= '0';
48                 elsif rising_edge(sys_clk) then
49                         -- internal
50                         state_int <= state_next;
51                         -- out
52                         s_char_int <= s_char_next;
53                         s_take_int <= s_take_next;
54                         s_backspace_int <= s_backspace_next;
55                         do_it_int <= do_it_next;
56                 end if;
57         end process;
58
59         -- next state
60         process(state_int, new_data, data, finished, s_done)
61         begin
62                 state_next <= state_int;
63
64                 case state_int is
65                         when SIDLE =>
66                                 if new_data = '1' then
67                                         case data is
68                                                 when x"f0" =>
69                                                         state_next <= SIGNORE_NEXT;
70                                                 when x"e0" =>
71                                                         state_next <= SREAD_NEXT;
72                                                 when SC_BKSP =>
73                                                         state_next <= SDEL;
74                                                 when SC_ENTER =>
75                                                         state_next <= SENTER;
76                                                 when SC_KP_0 | SC_KP_1 | SC_KP_2 | SC_KP_3 |
77                                                         SC_KP_4 | SC_KP_5 | SC_KP_6 | SC_KP_7 |
78                                                         SC_KP_8 | SC_KP_9 | SC_KP_PLUS |
79                                                         SC_KP_MINUS | SC_KP_MUL | SC_SPACE =>
80                                                                 state_next <= STAKE;
81                                                 when others => state_next <= SIDLE;
82                                         end case;
83                                 end if;
84                         when SIGNORE_NEXT =>
85                                 if new_data = '1' then
86                                         state_next <= SIDLE;
87                                 end if;
88                         when SREAD_NEXT =>
89                                 if new_data = '1' then
90                                         case data is
91                                                 when x"f0" =>
92                                                         state_next <= SIGNORE_NEXT;
93                                                 when SC_ENTER =>
94                                                         state_next <= SENTER;
95                                                 when SC_KP_DIV =>
96                                                         state_next <= STAKE;
97                                                 when others => state_next <= SIDLE;
98                                         end case;
99                                 end if;
100                         when STAKE | SDEL=>
101                                 if s_done = '1' then
102                                         state_next <= SIDLE;
103                                 end if;
104                         when SENTER =>
105                                 if finished = '1' then
106                                         state_next <= SIDLE;
107                                 end if;
108                 end case;
109         end process;
110
111         -- out
112         process(state_int, data, s_char_int, new_data)
113                 function sc2ascii (x : hbyte) return hbyte is
114                         variable y : hbyte;
115                 begin
116                         case x is
117                                 when SC_KP_0 => y := x"30";
118                                 when SC_KP_1 => y := x"31";
119                                 when SC_KP_2 => y := x"32";
120                                 when SC_KP_3 => y := x"33";
121                                 when SC_KP_4 => y := x"34";
122                                 when SC_KP_5 => y := x"35";
123                                 when SC_KP_6 => y := x"36";
124                                 when SC_KP_7 => y := x"37";
125                                 when SC_KP_8 => y := x"38";
126                                 when SC_KP_9 => y := x"39";
127                                 when SC_KP_PLUS => y := x"2b";
128                                 when SC_KP_MINUS => y := x"2d";
129                                 when SC_KP_MUL => y := x"2a";
130                                 when SC_KP_DIV => y := x"2f";
131                                 when SC_SPACE => y := x"20";
132                                 when others => y := x"41";
133                         end case;
134                         return y;
135                 end function;
136         begin
137                 s_char_next <= s_char_int;
138                 s_take_next <= '0';
139                 s_backspace_next <= '0';
140                 do_it_next <= '0';
141
142                 case state_int is
143                         when SIDLE => null;
144                         when SIGNORE_NEXT => null;
145                         when SREAD_NEXT => null;
146                         when STAKE =>
147                                 s_take_next <= '1';
148                                 s_char_next <= sc2ascii(hbyte(data));
149                         when SDEL =>
150                                 s_take_next <= '1';
151                                 s_backspace_next <= '1';
152                         when SENTER =>
153                                 do_it_next <= '1';
154                 end case;
155         end process;
156 end architecture beh;