display: vorbereitungen fuer eine art $PS1 :p
[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, S_INIT, S_PS1_0,
33                 S_PS1_1, S_PS1_2, S_PS1_3, S_PS1_4, S_PS1_5);
34         signal state_int, state_next : DISPLAY_STATE;
35         signal d_zeile_int, d_zeile_next : hzeile;
36         signal d_spalte_int, d_spalte_next : hspalte;
37         signal d_get_int, d_get_next : std_logic;
38         signal command_int, command_next : std_logic_vector(7 downto 0);
39         signal command_data_int, command_data_next : std_logic_vector(31 downto 0);
40         signal istate_next, istate_int : signed(3 downto 0);
41 begin
42         d_zeile <= d_zeile_int;
43         d_spalte <= d_spalte_int;
44         d_get <= d_get_int;
45         command <= command_int;
46         command_data <= command_data_int;
47
48         process(sys_clk, sys_res_n)
49         begin
50                 if sys_res_n = '0' then
51                         -- internal
52                         state_int <= S_INIT;
53                         istate_int <= (others => '0');
54                         -- out
55                         d_zeile_int <= (others => '0');
56                         d_spalte_int <= (others => '0');
57                         d_get_int <= '0';
58                         command_int <= COMMAND_NOP;
59                         command_data_int <= (others => '0');
60                 elsif rising_edge(sys_clk) then
61                         -- internal
62                         state_int <= state_next;
63                         istate_int <= istate_next;
64                         -- out
65                         d_zeile_int <= d_zeile_next;
66                         d_spalte_int <= d_spalte_next;
67                         d_get_int <= d_get_next;
68                         command_int <= command_next;
69                         command_data_int <= command_data_next;
70                 end if;
71         end process;
72
73         -- next state
74         process(state_int, d_new_result, d_new_eingabe, d_new_bs, d_done, free,
75                 d_spalte_int, d_char, istate_int)
76         begin
77                 state_next <= state_int;
78                 istate_next <= istate_int;
79
80                 case state_int is
81                         when S_INIT =>
82                                 state_next <= S_PS1_0;
83
84                         when S_PS1_0 =>
85                                 istate_next <= b"1001";
86                                 state_next <= S_WAIT;
87                         when S_PS1_1 =>
88                                 istate_next <= b"1010";
89                                 state_next <= S_WAIT;
90                         when S_PS1_2 =>
91                                 istate_next <= b"1011";
92                                 state_next <= S_WAIT;
93                         when S_PS1_3 =>
94                                 istate_next <= b"1100";
95                                 state_next <= S_WAIT;
96                         when S_PS1_4 =>
97                                 istate_next <= b"1101";
98                                 state_next <= S_WAIT;
99                         when S_PS1_5 =>
100                                 istate_next <= b"0111";
101                                 state_next <= S_WAIT;
102
103                         when SIDLE =>
104                                 istate_next <= b"0111"; -- default: immer wieder ins SIDLE;
105                                 if d_new_bs = '1' then
106                                         state_next <= S_NEW_BS;
107                                 elsif d_new_eingabe = '1' then
108                                         state_next <= S_NEW_INPUT;
109                                 end if;
110                                 if d_new_result = '1' then
111                                         state_next <= S_NEW_RESULT;
112                                 end if;
113
114                         when S_NEW_RESULT =>
115                                 state_next <= S_ZEILEUP;
116                         when S_NEW_INPUT =>
117                                 state_next <= S_COUNTUP;
118
119                         when S_NEW_BS =>
120                                 state_next <= S_BACK;
121                         when S_BACK =>
122                                 if free = '0' then
123                                         state_next <= S_WAIT;
124                                         case istate_int is
125                                                 when b"0111" => istate_next <= b"0001"; -- => danach S_BLANK und wieder hierher
126                                                 when others => istate_next <= b"0111"; -- => danach SIDLE
127                                         end case;
128                                 end if;
129                         when S_BLANK =>
130                                 if free = '0' then
131                                         state_next <= S_WAIT;
132                                         istate_next <= b"0010"; -- => danach S_BACK
133                                 end if;
134
135                         when S_ZEILEUP =>
136                                 case istate_int is
137                                         when b"0011" =>
138                                                 state_next <= S_WAIT;
139                                                 istate_next <= b"1000"; -- => danach S_PS1
140                                         when others => state_next <= S_CR1;
141                                 end case;
142
143                         when S_CR1 =>
144                                 if free = '0' then
145                                         state_next <= S_WAIT;
146                                         case istate_int is
147                                                 when b"0110" => istate_next <= b"0101"; -- => danach S_NL1, S_ZEILEUP, S_PS1, SIDLE
148                                                 when others => istate_next <= b"0000"; -- => danach S_NL1 und S_COUNTUP
149                                         end case;
150                                 end if;
151                         when S_NL1 =>
152                                 if free = '0' then
153                                         state_next <= S_WAIT;
154                                         case istate_int is
155                                                 when b"0101" => istate_next <= b"0011"; -- => danach S_ZEILEUP, PS1
156                                                 when others => istate_next <= b"0100"; -- => danach S_READ_RESULT
157                                         end case;
158                                 end if;
159                         when S_READ_RESULT =>
160                                 if unsigned(d_spalte_int) /= 70 then
161                                         state_next <= S_COUNTUP;
162                                         istate_next <= b"0100"; -- => wieder nach S_READ_RESULT
163                                 else
164                                         state_next <= S_WAIT;
165                                         istate_next <= b"0110"; -- => danach S_CR1 und d_spalte_next clearen und d_zeile_next inkrementieren
166                                 end if;
167
168                         when S_COUNTUP =>
169                                 state_next <= S_GETCH;
170                         when S_GETCH =>
171                                 if free = '1' and d_done = '1' and d_new_result = '0' and d_new_eingabe = '0' then
172                                         state_next <= S_PUTCH1;
173                                 end if;
174                         when S_PUTCH1 =>
175                                 state_next <= S_PUTCH2;
176                         when S_PUTCH2 =>
177                                 if free = '0' or (free = '1' and d_char = x"00") then
178                                         state_next <= S_WAIT;
179                                 end if;
180                         when S_WAIT =>
181                                 if free = '1' and d_done = '0' then
182                                         state_next <= S_NOP1;
183                                 end if;
184                         when S_NOP1 =>
185                                 if free = '1' then
186                                         case istate_int is
187                                                 when b"0000" => state_next <= S_NL1;
188                                                 when b"0001" => state_next <= S_BLANK;
189                                                 when b"0010" => state_next <= S_BACK;
190                                                 when b"0011" => state_next <= S_ZEILEUP;
191                                                 when b"0100" => state_next <= S_READ_RESULT;
192                                                 when b"0110" => state_next <= S_CR1;
193                                                 when b"0101" => state_next <= S_NL1;
194
195                                                 when b"1000" => state_next <= S_PS1_0;
196                                                 when b"1001" => state_next <= S_PS1_1;
197                                                 when b"1010" => state_next <= S_PS1_2;
198                                                 when b"1011" => state_next <= S_PS1_3;
199                                                 when b"1100" => state_next <= S_PS1_4;
200                                                 when b"1101" => state_next <= S_PS1_5;
201                                                 when others => state_next <= SIDLE;
202                                         end case;
203                                 end if;
204                 end case;
205         end process;
206
207         -- out
208         process(state_int, d_zeile_int, d_spalte_int, d_get_int, command_int,
209                 command_data_int, d_char)
210         begin
211                 d_zeile_next <= d_zeile_int;
212                 d_spalte_next <= d_spalte_int;
213                 d_get_next <= '0';
214                 command_next <= command_int;
215                 command_data_next <= command_data_int;
216
217                 case state_int is
218                         when S_INIT => null;
219
220                         -- TODO: coole farben
221                         when S_PS1_0 =>
222                                 command_next <= COMMAND_SET_CHAR;
223                                 command_data_next <= x"ffffff" & x"28"; -- '('
224                         when S_PS1_1 =>
225                                 command_next <= COMMAND_SET_CHAR;
226                                 -- d_zeile/2, zehnerstelle
227                                 command_data_next <= x"ffffff" & x"78"; -- 'x'
228                         when S_PS1_2 =>
229                                 command_next <= COMMAND_SET_CHAR;
230                                 -- d_zeile/2, einerstelle
231                                 command_data_next <= x"ffffff" & x"79"; -- 'y'
232                         when S_PS1_3 =>
233                                 command_next <= COMMAND_SET_CHAR;
234                                 command_data_next <= x"ffffff" & x"29"; -- ')'
235                         when S_PS1_4 =>
236                                 command_next <= COMMAND_SET_CHAR;
237                                 command_data_next <= x"ffffff" & x"24"; -- '$'
238                         when S_PS1_5 =>
239                                 command_next <= COMMAND_SET_CHAR;
240                                 command_data_next <= x"ffffff" & x"20"; -- ' '
241
242                         when SIDLE => null;
243                         when S_NEW_RESULT => null;
244                         when S_NEW_INPUT => null;
245
246                         when S_NEW_BS =>
247                                 -- underflow check schon im history modul
248                                 d_spalte_next <= std_logic_vector(unsigned(d_spalte_int) - 1);
249                         when S_BACK =>
250                                 -- einen schritt zurueck, +6 wegen $PS1
251                                 command_next <= COMMAND_SET_CURSOR_COLUMN;
252                                 command_data_next <= x"ffffff" & '0' & std_logic_vector(unsigned(d_spalte_int) + 6);
253                         when S_BLANK =>
254                                 command_next <= COMMAND_SET_CHAR;
255                                 command_data_next <= x"ffffff" & x"20"; -- white space
256
257                         when S_ZEILEUP =>
258                                 d_spalte_next <= (others => '0');
259                                 case d_zeile_int is
260                                         -- 49 * 2 + 1
261                                         when "1100010" => d_zeile_next <= (others => '0');
262                                         when others => d_zeile_next <= std_logic_vector(unsigned(d_zeile_int) + 1);
263                                 end case;
264
265                         when S_CR1 =>
266                                 command_next <= COMMAND_SET_CHAR;
267                                 command_data_next <= x"ffffff" & x"0d"; -- carrige return
268                         when S_NL1 =>
269                                 command_next <= COMMAND_SET_CHAR;
270                                 command_data_next <= x"ffffff" & x"0a"; -- newline
271                         when S_READ_RESULT => null;
272
273                         when S_COUNTUP =>
274                                 d_get_next <= '1';
275                                 d_spalte_next <= std_logic_vector(unsigned(d_spalte_int) + 1);
276                         when S_GETCH =>
277                                 d_get_next <= '1';
278                         when S_PUTCH1 =>
279                                 if d_char /= x"00" then
280                                         command_next <= COMMAND_SET_CHAR;
281                                         command_data_next <= x"ffffff" & std_logic_vector(d_char);
282                                 end if;
283                         when S_PUTCH2 => null;
284                         when S_WAIT | S_NOP1 =>
285                                 command_next <= COMMAND_NOP;
286                                 command_data_next <= x"00000000";
287                 end case;
288         end process;
289 end architecture beh;