scanner: argh... es kommen ja scancodes rein und keine asciicodes!
[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, SREAD, SMOD, 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' and finished = '0' and s_done = '0' then
67                                         state_next <= SREAD;
68                                 end if;
69                         when SREAD =>
70                                 case data is
71                                         when x"e0" => state_next <= SMOD;
72                                         when SC_BKSP => state_next <= SDEL;
73                                         when SC_ENTER => state_next <= SENTER;
74                                         when SC_KP_0 | SC_KP_1 | SC_KP_2 | SC_KP_3 |
75                                                 SC_KP_4 | SC_KP_5 | SC_KP_6 | SC_KP_7 |
76                                                 SC_KP_8 | SC_KP_9 | SC_KP_PLUS |
77                                                 SC_KP_MINUS | SC_KP_MUL | SC_SPACE =>
78                                                         state_next <= STAKE;
79                                         when others => state_next <= SIDLE;
80                                 end case;
81                         when SMOD =>
82                                 if new_data = '1' then
83                                         if data = SC_KP_ENTER then
84                                                 state_next <= SENTER;
85                                         elsif data = SC_KP_DIV then
86                                                 state_next <= STAKE;
87                                         else
88                                                 state_next <= SIDLE;
89                                         end if;
90                                 end if;
91                         when STAKE | SDEL=>
92                                 if s_done = '1' then
93                                         state_next <= SIDLE;
94                                 end if;
95                         when SENTER =>
96                                 if finished = '1' then
97                                         state_next <= SIDLE;
98                                 end if;
99                 end case;
100         end process;
101
102         -- out
103         process(state_int, data)
104                 function sc2ascii (x : hbyte) return hbyte is
105                         variable y : hbyte;
106                 begin
107                         case x is
108                                 when SC_KP_0 => y := x"30";
109                                 when SC_KP_1 => y := x"31";
110                                 when SC_KP_2 => y := x"32";
111                                 when SC_KP_3 => y := x"33";
112                                 when SC_KP_4 => y := x"34";
113                                 when SC_KP_5 => y := x"35";
114                                 when SC_KP_6 => y := x"36";
115                                 when SC_KP_7 => y := x"37";
116                                 when SC_KP_8 => y := x"38";
117                                 when SC_KP_9 => y := x"39";
118                                 when SC_KP_PLUS => y := x"2b";
119                                 when SC_KP_MINUS => y := x"2d";
120                                 when SC_KP_MUL => y := x"2a";
121                                 when SC_KP_DIV => y := x"2f";
122                                 when SC_SPACE => y := x"20";
123                                 when others => y := x"41";
124                         end case;
125                         return y;
126                 end function;
127         begin
128                 s_char_next <= (others => '0');
129                 s_take_next <= '0';
130                 s_backspace_next <= '0';
131                 do_it_next <= '0';
132
133                 case state_int is
134                         when SIDLE =>
135                                 null;
136                         when SREAD =>
137                                 null;
138                         when SMOD =>
139                                 null;
140                         when STAKE =>
141                                 s_take_next <= '1';
142                                 s_char_next <= sc2ascii(hbyte(data));
143                         when SDEL =>
144                                 s_take_next <= '1';
145                                 s_backspace_next <= '1';
146                         when SENTER =>
147                                 do_it_next <= '1';
148                 end case;
149         end process;
150 end architecture beh;