display: fuer istate wird nun auch ein enum verwendet
[hwmod.git] / src / display.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use work.gen_pkg.all;
5 use work.textmode_vga_component_pkg.all;
6 use work.textmode_vga_pkg.all;
7 use work.textmode_vga_platform_dependent_pkg.all;
8
9 entity display is
10         port (
11                 sys_clk : in std_logic;
12                 sys_res_n : in std_logic;
13                 -- History
14                 d_new_eingabe : in std_logic;
15                 d_new_result : in std_logic;
16                 d_new_bs : in std_logic;
17                 d_zeile : out hzeile;
18                 d_spalte : out hspalte;
19                 d_get : out std_logic;
20                 d_done : in std_logic;
21                 d_char : in hbyte;
22                 -- VGA
23                 command : out std_logic_vector(7 downto 0);
24                 command_data : out std_logic_vector(31 downto 0);
25                 free : in std_logic
26         );
27 end entity display;
28
29 architecture beh of display is
30         type DISPLAY_STATE is (SIDLE, S_NEW_BS, S_BACK, S_BLANK, S_NEW_RESULT,
31                 S_ZEILEUP, S_NEW_INPUT, S_COUNTUP, S_GETCH, S_CR1, S_NL1, S_PUTCH1,
32                 S_PUTCH2, S_WAIT, S_NOP1, S_READ_RESULT_0, S_READ_RESULT_1,
33                 S_READ_RESULT_2, S_INIT, S_PS1_0, S_PS1_1, S_PS1_2, S_PS1_3, S_PS1_4,
34                 S_PS1_5);
35         type DISPLAY_ISTATE is (IS_BACK_2_BLANK, IS_BLANK_2_BACK,
36                 IS_NL1_2_ZEILEUP, IS_RESULT2_2_CR1, IS_CR1_2_NL1, IS_NL1_2_RESULT0,
37                 IS_RESULT0_2_RESULT1, IS_RESULT1_2_RESULT2, IS_ZEILEUP_2_PS10,
38                 IS_PS10_2_PS11, IS_PS11_2_PS12, IS_PS12_2_PS13, IS_PS13_2_PS14,
39                 IS_PS14_2_PS15, IS_RANDOM_2_IDLE, IS_OTHER_2_NL1);
40
41         signal state_int, state_next : DISPLAY_STATE;
42         signal istate_next, istate_int : DISPLAY_ISTATE;
43         signal d_zeile_int, d_zeile_next : hzeile;
44         signal d_spalte_int, d_spalte_next : hspalte;
45         signal d_get_int, d_get_next : std_logic;
46         signal command_int, command_next : std_logic_vector(7 downto 0);
47         signal command_data_int, command_data_next : std_logic_vector(31 downto 0);
48 begin
49         d_zeile <= d_zeile_int;
50         d_spalte <= d_spalte_int;
51         d_get <= d_get_int;
52         command <= command_int;
53         command_data <= command_data_int;
54
55         process(sys_clk, sys_res_n)
56         begin
57                 if sys_res_n = '0' then
58                         -- internal
59                         state_int <= S_INIT;
60                         istate_int <= IS_RANDOM_2_IDLE;
61                         -- out
62                         d_zeile_int <= (others => '0');
63                         d_spalte_int <= (others => '0');
64                         d_get_int <= '0';
65                         command_int <= COMMAND_NOP;
66                         command_data_int <= (others => '0');
67                 elsif rising_edge(sys_clk) then
68                         -- internal
69                         state_int <= state_next;
70                         istate_int <= istate_next;
71                         -- out
72                         d_zeile_int <= d_zeile_next;
73                         d_spalte_int <= d_spalte_next;
74                         d_get_int <= d_get_next;
75                         command_int <= command_next;
76                         command_data_int <= command_data_next;
77                 end if;
78         end process;
79
80         -- next state
81         process(state_int, d_new_result, d_new_eingabe, d_new_bs, d_done, free,
82                 d_spalte_int, d_char, istate_int)
83         begin
84                 state_next <= state_int;
85                 istate_next <= istate_int;
86
87                 case state_int is
88                         when S_INIT =>
89                                 state_next <= S_PS1_0;
90
91                         when S_PS1_0 =>
92                                 istate_next <= IS_PS10_2_PS11;
93                                 state_next <= S_WAIT;
94                         when S_PS1_1 =>
95                                 istate_next <= IS_PS11_2_PS12;
96                                 state_next <= S_WAIT;
97                         when S_PS1_2 =>
98                                 istate_next <= IS_PS12_2_PS13;
99                                 state_next <= S_WAIT;
100                         when S_PS1_3 =>
101                                 istate_next <= IS_PS13_2_PS14;
102                                 state_next <= S_WAIT;
103                         when S_PS1_4 =>
104                                 istate_next <= IS_PS14_2_PS15;
105                                 state_next <= S_WAIT;
106                         when S_PS1_5 =>
107                                 istate_next <= IS_RANDOM_2_IDLE;
108                                 state_next <= S_WAIT;
109
110                         when SIDLE =>
111                                 istate_next <= IS_RANDOM_2_IDLE;
112                                 if d_new_bs = '1' then
113                                         state_next <= S_NEW_BS;
114                                 elsif d_new_eingabe = '1' then
115                                         state_next <= S_NEW_INPUT;
116                                 end if;
117                                 if d_new_result = '1' then
118                                         state_next <= S_NEW_RESULT;
119                                 end if;
120
121                         when S_NEW_RESULT =>
122                                 state_next <= S_ZEILEUP;
123                         when S_NEW_INPUT =>
124                                 state_next <= S_COUNTUP;
125
126                         when S_NEW_BS =>
127                                 state_next <= S_BACK;
128                         when S_BACK =>
129                                 if free = '0' then
130                                         state_next <= S_WAIT;
131                                         case istate_int is
132                                                 when IS_RANDOM_2_IDLE => istate_next <= IS_BACK_2_BLANK;
133                                                 when others => istate_next <= IS_RANDOM_2_IDLE;
134                                         end case;
135                                 end if;
136                         when S_BLANK =>
137                                 if free = '0' then
138                                         state_next <= S_WAIT;
139                                         istate_next <= IS_BLANK_2_BACK;
140                                 end if;
141
142                         when S_ZEILEUP =>
143                                 case istate_int is
144                                         when IS_NL1_2_ZEILEUP =>
145                                                 state_next <= S_WAIT;
146                                                 istate_next <= IS_ZEILEUP_2_PS10;
147                                         when others => state_next <= S_CR1;
148                                 end case;
149
150                         when S_CR1 =>
151                                 if free = '0' then
152                                         state_next <= S_WAIT;
153                                         case istate_int is
154                                                 when IS_RESULT2_2_CR1 => istate_next <= IS_OTHER_2_NL1;
155                                                 when others => istate_next <= IS_CR1_2_NL1;
156                                         end case;
157                                 end if;
158                         when S_NL1 =>
159                                 if free = '0' then
160                                         state_next <= S_WAIT;
161                                         case istate_int is
162                                                 when IS_OTHER_2_NL1 => istate_next <= IS_NL1_2_ZEILEUP;
163                                                 when others => istate_next <= IS_NL1_2_RESULT0;
164                                         end case;
165                                 end if;
166
167                         when S_READ_RESULT_0 =>
168                                 istate_next <= IS_RESULT0_2_RESULT1;
169                                 state_next <= S_WAIT;
170                         when S_READ_RESULT_1 =>
171                                 istate_next <= IS_RESULT1_2_RESULT2;
172                                 state_next <= S_WAIT;
173                         when S_READ_RESULT_2 =>
174                                 if unsigned(d_spalte_int) /= 70 then
175                                         state_next <= S_COUNTUP;
176                                         istate_next <= IS_RESULT1_2_RESULT2;
177                                 else
178                                         state_next <= S_WAIT;
179                                         istate_next <= IS_RESULT2_2_CR1;
180                                 end if;
181
182                         when S_COUNTUP =>
183                                 state_next <= S_GETCH;
184                         when S_GETCH =>
185                                 if free = '1' and d_done = '1' and d_new_result = '0' and d_new_eingabe = '0' then
186                                         state_next <= S_PUTCH1;
187                                 end if;
188                         when S_PUTCH1 =>
189                                 state_next <= S_PUTCH2;
190                         when S_PUTCH2 =>
191                                 if free = '0' then
192                                         state_next <= S_WAIT;
193                                 end if;
194                         when S_WAIT =>
195                                 if free = '1' and d_done = '0' then
196                                         state_next <= S_NOP1;
197                                 end if;
198                         when S_NOP1 =>
199                                 if free = '1' then
200                                         case istate_int is
201                                                 when IS_CR1_2_NL1 => state_next <= S_NL1;
202                                                 when IS_BACK_2_BLANK => state_next <= S_BLANK;
203                                                 when IS_BLANK_2_BACK => state_next <= S_BACK;
204                                                 when IS_NL1_2_ZEILEUP => state_next <= S_ZEILEUP;
205                                                 when IS_RESULT2_2_CR1 => state_next <= S_CR1;
206                                                 when IS_OTHER_2_NL1 => state_next <= S_NL1;
207
208                                                 when IS_NL1_2_RESULT0 => state_next <= S_READ_RESULT_0;
209                                                 when IS_RESULT0_2_RESULT1 => state_next <= S_READ_RESULT_1;
210                                                 when IS_RESULT1_2_RESULT2 => state_next <= S_READ_RESULT_2;
211
212                                                 when IS_ZEILEUP_2_PS10 => state_next <= S_PS1_0;
213                                                 when IS_PS10_2_PS11 => state_next <= S_PS1_1;
214                                                 when IS_PS11_2_PS12 => state_next <= S_PS1_2;
215                                                 when IS_PS12_2_PS13 => state_next <= S_PS1_3;
216                                                 when IS_PS13_2_PS14 => state_next <= S_PS1_4;
217                                                 when IS_PS14_2_PS15 => state_next <= S_PS1_5;
218                                                 when IS_RANDOM_2_IDLE => state_next <= SIDLE;
219                                         end case;
220                                 end if;
221                 end case;
222         end process;
223
224         -- out
225         process(state_int, d_zeile_int, d_spalte_int, d_get_int, command_int,
226                 command_data_int, d_char)
227                 function zeile2char(i : std_logic_vector; stelle : natural) return hbyte is
228                         subtype zeilnum is string(1 to 2);
229                         type zeilnum_arr is array (natural range 0 to 49) of zeilnum;
230                         constant zn : zeilnum_arr := (
231                                 0 => "00", 1 => "01", 2 => "02", 3 => "03", 4 => "04",
232                                 5 => "05", 6 => "06", 7 => "07", 8 => "08", 9 => "09",
233                                 10 => "10", 11 => "11", 12 => "12", 13 => "13", 14 => "14",
234                                 15 => "15", 16 => "16", 17 => "17", 18 => "18", 19 => "19",
235                                 20 => "20", 21 => "21", 22 => "22", 23 => "23", 24 => "24",
236                                 25 => "25", 26 => "26", 27 => "27", 28 => "28", 29 => "29",
237                                 30 => "30", 31 => "31", 32 => "32", 33 => "33", 34 => "34",
238                                 35 => "35", 36 => "36", 37 => "37", 38 => "38", 39 => "39",
239                                 40 => "40", 41 => "41", 42 => "42", 43 => "43", 44 => "44",
240                                 45 => "45", 46 => "46", 47 => "47", 48 => "48", 49 => "49",
241                                 others => "xy");
242                         variable t : signed(hzeile'length downto 0);
243                 begin
244                         t := signed('0' & i);
245                         t := t / 2;
246                         return hbyte(to_unsigned(character'pos(zn(to_integer(t))(stelle)),8));
247                 end;
248         begin
249                 d_zeile_next <= d_zeile_int;
250                 d_spalte_next <= d_spalte_int;
251                 d_get_next <= '0';
252                 command_next <= command_int;
253                 command_data_next <= command_data_int;
254
255                 case state_int is
256                         when S_INIT => null;
257
258                         when S_PS1_0 =>
259                                 command_next <= COMMAND_SET_CHAR;
260                                 command_data_next <= x"0000ff" & x"28"; -- '('
261                         when S_PS1_1 =>
262                                 command_next <= COMMAND_SET_CHAR;
263                                 command_data_next <= x"00ff00" & zeile2char(d_zeile_int,1); -- 'x'
264                         when S_PS1_2 =>
265                                 command_next <= COMMAND_SET_CHAR;
266                                 command_data_next <= x"00ff00" & zeile2char(d_zeile_int,2); -- 'y'
267                         when S_PS1_3 =>
268                                 command_next <= COMMAND_SET_CHAR;
269                                 command_data_next <= x"0000ff" & x"29"; -- ')'
270                         when S_PS1_4 =>
271                                 command_next <= COMMAND_SET_CHAR;
272                                 command_data_next <= x"00ffff" & x"24"; -- '$'
273                         when S_PS1_5 =>
274                                 command_next <= COMMAND_SET_CHAR;
275                                 command_data_next <= x"ffffff" & x"20"; -- ' '
276
277                         when SIDLE => null;
278                         when S_NEW_RESULT => null;
279                         when S_NEW_INPUT => null;
280
281                         when S_NEW_BS =>
282                                 -- underflow check schon im history modul
283                                 d_spalte_next <= std_logic_vector(unsigned(d_spalte_int) - 1);
284                         when S_BACK =>
285                                 -- einen schritt zurueck, +6 wegen $PS1
286                                 command_next <= COMMAND_SET_CURSOR_COLUMN;
287                                 command_data_next <= x"ffffff" & '0' & std_logic_vector(unsigned(d_spalte_int) + 6);
288                         when S_BLANK =>
289                                 command_next <= COMMAND_SET_CHAR;
290                                 command_data_next <= x"ffffff" & x"20"; -- white space
291
292                         when S_ZEILEUP =>
293                                 d_spalte_next <= (others => '0');
294                                 case d_zeile_int is
295                                         when
296                                         std_logic_vector(to_unsigned((49*2)+1,d_zeile_int'length)) => d_zeile_next <= (others => '0');
297                                         when others => d_zeile_next <= std_logic_vector(unsigned(d_zeile_int) + 1);
298                                 end case;
299
300                         when S_CR1 =>
301                                 command_next <= COMMAND_SET_CHAR;
302                                 command_data_next <= x"ffffff" & x"0d"; -- carrige return
303                         when S_NL1 =>
304                                 command_next <= COMMAND_SET_CHAR;
305                                 command_data_next <= x"ffffff" & x"0a"; -- newline
306
307                         when S_READ_RESULT_0 =>
308                                 command_next <= COMMAND_SET_CHAR;
309                                 command_data_next <= x"ff0000" & x"3e"; -- '>'
310                         when S_READ_RESULT_1 =>
311                                 command_next <= COMMAND_SET_CHAR;
312                                 command_data_next <= x"ffffff" & x"20"; -- ' '
313                         when S_READ_RESULT_2 => null;
314
315                         when S_COUNTUP =>
316                                 d_get_next <= '1';
317                                 d_spalte_next <= std_logic_vector(unsigned(d_spalte_int) + 1);
318                         when S_GETCH =>
319                                 d_get_next <= '1';
320                         when S_PUTCH1 =>
321                                 command_next <= COMMAND_SET_CHAR;
322                                 if d_char = x"00" then
323                                         command_data_next <= x"ffffff" & x"20";
324                                 else
325                                         command_data_next <= x"ffffff" & std_logic_vector(d_char);
326                                 end if;
327                         when S_PUTCH2 => null;
328                         when S_WAIT | S_NOP1 =>
329                                 command_next <= COMMAND_NOP;
330                                 command_data_next <= x"00000000";
331                 end case;
332         end process;
333 end architecture beh;