922911cb73134904fd0fd207c6c7b47715fcd5d9
[hwmod.git] / src / gen_pkg.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use work.math_pkg.all;
5
6 package gen_pkg is
7         subtype alu_ops is std_logic_vector(2 downto 0);
8         constant ALU_NOP : alu_ops := "000";
9         constant ALU_SUB : alu_ops := "001";
10         constant ALU_ADD : alu_ops := "010";
11         constant ALU_MUL : alu_ops := "011";
12         constant ALU_DIV : alu_ops := "100";
13         constant ALU_DONE : alu_ops := "101";
14
15         constant CBITS : integer := 32;
16         subtype csigned is signed((CBITS-1) downto 0);
17         subtype divinteger is unsigned(4 downto 0);
18         -- integer ist 32bit (31bit + sign)
19         subtype cinteger is integer;
20
21
22         constant HSPALTE_MAX : integer := 71;
23         subtype hspalte is std_logic_vector(6 downto 0);
24
25         constant HZEILE_MAX : integer := 100;
26         subtype hzeile is std_logic_vector(6 downto 0);
27
28         -- 50 zeilen * 71 zeichen * 2 (berechnung + ergebnis) = 7100 bytes
29         constant H_RAM_SIZE : integer := HZEILE_MAX * HSPALTE_MAX;
30         constant H_RAM_WIDTH : integer := log2c(H_RAM_SIZE);
31
32         subtype hbyte is std_logic_vector(7 downto 0);
33         subtype hstring is string(1 to HSPALTE_MAX+1);
34         subtype hstr_int is integer range 0 to HSPALTE_MAX+1;
35
36         procedure icwait(signal clk_i : IN std_logic; cycles: natural);
37
38         function ascii2sc (x : hbyte) return hbyte;
39         function valid_char (x : hbyte) return boolean;
40         function zeile2char(i : std_logic_vector; stelle : natural) return hbyte;
41
42         -- http://www.marjorie.de/ps2/scancode-set2.htm
43         constant SC_KP_0 : hbyte := x"70";
44         constant SC_KP_1 : hbyte := x"69";
45         constant SC_KP_2 : hbyte := x"72";
46         constant SC_KP_3 : hbyte := x"7a";
47         constant SC_KP_4 : hbyte := x"6b";
48         constant SC_KP_5 : hbyte := x"73";
49         constant SC_KP_6 : hbyte := x"74";
50         constant SC_KP_7 : hbyte := x"6c";
51         constant SC_KP_8 : hbyte := x"75";
52         constant SC_KP_9 : hbyte := x"7d";
53
54         constant SC_0 : hbyte := x"45";
55         constant SC_1 : hbyte := x"16";
56         constant SC_2 : hbyte := x"1e";
57         constant SC_3 : hbyte := x"26";
58         constant SC_4 : hbyte := x"25";
59         constant SC_5 : hbyte := x"2e";
60         constant SC_6 : hbyte := x"36";
61         constant SC_7 : hbyte := x"3d";
62         constant SC_8 : hbyte := x"3e";
63         constant SC_9 : hbyte := x"46";
64
65         constant SC_KP_PLUS : hbyte := x"79";
66         constant SC_KP_MINUS : hbyte := x"7b";
67         constant SC_KP_MUL : hbyte := x"7c";
68         constant SC_KP_DIV : hbyte := x"4a"; -- inkl. 0xe0!
69
70         -- fuer deutsches layout, alle anderen zeichen sind unguenstig belegt
71         constant SC_PLUS : hbyte := x"5b";
72
73         constant SC_ENTER : hbyte := x"5a";
74         constant SC_BKSP : hbyte := x"66";
75         constant SC_SPACE : hbyte := x"29";
76
77         -- components...
78         component alu is
79                 port (
80                         sys_clk : in std_logic;
81                         sys_res_n : in std_logic;
82                         opcode : in alu_ops;
83                         op1 : in csigned;
84                         op2 : in csigned;
85                         op3 : out csigned;
86                         opM : out csigned;
87                         do_calc : in std_logic;
88                         calc_done : out std_logic;
89                         calc_error : out std_logic
90                 );
91         end component alu;
92
93         component parser is
94                 port (
95                         sys_clk : in std_logic;
96                         sys_res_n : in std_logic;
97                         -- History
98                         p_rget : out std_logic;
99                         p_rdone : in std_logic;
100                         p_read : in hbyte;
101                         p_wtake : out std_logic;
102                         p_wdone : in std_logic;
103                         p_write : out hbyte;
104                         p_finished : out std_logic;
105                         -- Scanner
106                         do_it : in std_logic;
107                         finished : out std_logic
108                 );
109         end component parser;
110
111         component scanner is
112                 port
113                 (
114                         sys_clk : in std_logic;
115                         sys_res_n : in std_logic;
116                         -- PS/2
117                         new_data : in std_logic;
118                         data : in std_logic_vector(7 downto 0);
119                         -- History
120                         s_char : out hbyte;
121                         s_take : out std_logic;
122                         s_done : in std_logic;
123                         s_backspace : out std_logic;
124                         -- Parser
125                         do_it : out std_logic;
126                         finished : in std_logic
127                 );
128         end component scanner;
129
130         component history is
131                 port (
132                         sys_clk : in std_logic;
133                         sys_res_n : in std_logic;
134                         -- PC-komm
135                         pc_get :  in std_logic;
136                         pc_spalte : in hspalte;
137                         pc_zeile : in hzeile;
138                         pc_char : out hbyte;
139                         pc_done : out std_logic;
140                         -- Scanner
141                         s_char : in hbyte;
142                         s_take : in std_logic;
143                         s_done : out std_logic;
144                         s_backspace : in std_logic;
145                         -- Display
146                         d_new_eingabe : out std_logic;
147                         d_new_result : out std_logic;
148                         d_new_bs : out std_logic;
149                         d_zeile : in hzeile;
150                         d_spalte : in hspalte;
151                         d_get : in std_logic;
152                         d_done : out std_logic;
153                         d_char : out hbyte;
154                         -- Parser
155                         p_rget : in std_logic;
156                         p_rdone : out std_logic;
157                         p_read : out hbyte;
158                         p_wtake : in std_logic;
159                         p_wdone : out std_logic;
160                         p_write : in hbyte;
161                         p_finished : in std_logic
162                 );
163         end component history;
164
165         component display is
166                 port (
167                         sys_clk : in std_logic;
168                         sys_res_n : in std_logic;
169                         -- History
170                         d_new_eingabe : in std_logic;
171                         d_new_result : in std_logic;
172                         d_new_bs : in std_logic;
173                         d_zeile : out hzeile;
174                         d_spalte : out hspalte;
175                         d_get : out std_logic;
176                         d_done : in std_logic;
177                         d_char : in hbyte;
178                         -- VGA
179                         command : out std_logic_vector(7 downto 0);
180                         command_data : out std_logic_vector(31 downto 0);
181                         free : in std_logic
182                 );
183         end component display;
184
185         component pc_communication is
186                 port (
187                         sys_clk : in std_logic;
188                         sys_res_n : in std_logic;
189
190                         --button
191                         btn_a : in std_logic;
192
193                         --uart_tx
194                         tx_data : out std_logic_vector(7 downto 0);
195                         tx_new : out std_logic;
196                         tx_done : in std_logic;
197
198                         --uart_rx
199                         rx_data : in std_logic_vector(7 downto 0);
200                         rx_new : in std_logic;
201
202                         -- History
203                         pc_zeile : out hzeile;
204                         pc_spalte : out hspalte;
205                         pc_get :  out std_logic;
206                         pc_done : in std_logic;
207                         pc_char : in hbyte
208                 );
209         end component pc_communication;
210
211         component uart_rx is
212                 generic (
213                         CLK_FREQ : integer := 33000000;
214                         BAUDRATE : integer := 115200
215                 );
216                 port(
217                         sys_clk : in std_logic;
218                         sys_res_n : in std_logic;
219                         rxd : in std_logic;
220                         rx_data : out std_logic_vector(7 downto 0);
221                         rx_new : out std_logic
222                 );
223         end component uart_rx;
224
225         component uart_tx is
226                 generic (
227                         CLK_FREQ : integer := 33000000;
228                         BAUDRATE : integer := 115200
229                 );
230                 port(
231                         sys_clk : in std_logic;
232                         sys_res_n : in std_logic;
233                         txd : out std_logic;
234                         tx_data : in std_logic_vector(7 downto 0);
235                         tx_new : in std_logic;
236                         tx_done : out std_logic
237                 );
238         end component uart_tx;
239
240         component vpll IS
241                 port (
242                         inclk0 : in std_logic := '0';
243                         c0 : out std_logic 
244                 );
245         end component vpll;
246         
247         component clk_vga_s3e is
248                 port (
249                         clk50 : in std_logic;
250                         clk25 : out std_logic
251                 );
252         end component clk_vga_s3e;
253
254         component sp_ram is
255                 generic (
256                         ADDR_WIDTH : integer range 1 to integer'high
257                 );
258                 port (
259                         sys_clk : in std_logic;
260                         address : in std_logic_vector(ADDR_WIDTH - 1 downto 0);
261                         data_out : out hbyte;
262                         wr : in std_logic;
263                         data_in : in hbyte
264                 );
265         end component sp_ram;
266 end package gen_pkg;
267
268 package body gen_pkg is
269         procedure icwait(signal clk_i : IN std_logic; cycles: Natural) is
270         begin
271                 for i in 1 to cycles loop
272                         wait until clk_i= '0' and clk_i'event;
273                 end loop;
274         end;
275
276         function ascii2sc (x : hbyte) return hbyte is
277                 variable y : hbyte;
278         begin
279                 case x is
280                         when x"30" => y := SC_KP_0;
281                         when x"31" => y := SC_KP_1;
282                         when x"32" => y := SC_KP_2;
283                         when x"33" => y := SC_KP_3;
284                         when x"34" => y := SC_KP_4;
285                         when x"35" => y := SC_KP_5;
286                         when x"36" => y := SC_KP_6;
287                         when x"37" => y := SC_KP_7;
288                         when x"38" => y := SC_KP_8;
289                         when x"39" => y := SC_KP_9;
290                         when x"2b" => y := SC_KP_PLUS;
291                         when x"2d" => y := SC_KP_MINUS;
292                         when x"2a" => y := SC_KP_MUL;
293                         when x"2f" => y := SC_KP_DIV;
294                         when x"20" => y := SC_SPACE;
295                         when x"1c" => y := SC_ENTER;
296                         when x"0e" => y := SC_BKSP;
297                         when others => y := x"41";
298                 end case;
299                 return y;
300         end function;
301
302         function valid_char (x : hbyte) return boolean is
303                 variable y : boolean;
304         begin
305                 case x is
306                         when SC_KP_0 | SC_KP_1 | SC_KP_2 | SC_KP_3 |
307                                 SC_KP_4 | SC_KP_5 | SC_KP_6 | SC_KP_7 |
308                                 SC_KP_8 | SC_KP_9 | SC_KP_PLUS |
309                                 SC_KP_MINUS | SC_KP_MUL |
310                                 SC_KP_DIV | SC_SPACE |
311                                 SC_BKSP | SC_ENTER =>
312                                         y := true;
313                         when others => y := false;
314                 end case;
315                 return y;
316         end function;
317
318         function zeile2char(i : std_logic_vector; stelle : natural) return hbyte is
319                 subtype zeilnum is string(1 to 2);
320                 type zeilnum_arr is array (natural range 0 to 49) of zeilnum;
321                 constant zn : zeilnum_arr := (
322                         0 => "00", 1 => "01", 2 => "02", 3 => "03", 4 => "04",
323                         5 => "05", 6 => "06", 7 => "07", 8 => "08", 9 => "09",
324                         10 => "10", 11 => "11", 12 => "12", 13 => "13", 14 => "14",
325                         15 => "15", 16 => "16", 17 => "17", 18 => "18", 19 => "19",
326                         20 => "20", 21 => "21", 22 => "22", 23 => "23", 24 => "24",
327                         25 => "25", 26 => "26", 27 => "27", 28 => "28", 29 => "29",
328                         30 => "30", 31 => "31", 32 => "32", 33 => "33", 34 => "34",
329                         35 => "35", 36 => "36", 37 => "37", 38 => "38", 39 => "39",
330                         40 => "40", 41 => "41", 42 => "42", 43 => "43", 44 => "44",
331                         45 => "45", 46 => "46", 47 => "47", 48 => "48", 49 => "49",
332                         others => "xy");
333                 variable t : signed(hzeile'length downto 0);
334         begin
335                 t := signed('0' & i);
336                 t := t / 2;
337                 return hbyte(to_unsigned(character'pos(zn(to_integer(t))(stelle)),8));
338         end;
339 end package body gen_pkg;