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