uart_rx: ein prozessmodell. spart weitere 3 logic elements :P
[hwmod.git] / src / textmode_vga / console_sm_beh.vhd
1 -------------------------------------------------------------------------\r
2 --\r
3 -- Filename: console_sm_beh.vhd\r
4 -- =========\r
5 --\r
6 -- Short Description:\r
7 -- ==================\r
8 --   Console mode finite state machine behavioral implementation\r
9 --\r
10 -------------------------------------------------------------------------\r
11 \r
12 library ieee;\r
13 use ieee.std_logic_1164.all;\r
14 use ieee.numeric_std.all;\r
15 use work.textmode_vga_pkg.all;\r
16 use work.textmode_vga_platform_dependent_pkg.all;\r
17 \r
18 architecture beh of console_sm is\r
19   type STATE_TYPE is\r
20   (\r
21     STATE_IDLE, STATE_ACK_UNKNOWN, STATE_WAIT_REQ_RELEASE, STATE_NEW_LINE_SIMPLE,\r
22     STATE_NEW_LINE_SCROLL, STATE_CARRIAGE_RETURN, STATE_SET_CHAR,\r
23     STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SIMPLE,\r
24     STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SCROLL,\r
25     STATE_SET_BACKGROUND, STATE_SCROLL_NEXT, STATE_SCROLL_TOP,\r
26     STATE_SCROLL_CLEAR_LINE, STATE_SCROLL_CLEAR_LINE_LOOP,\r
27     STATE_SCROLL_CLEAR_LINE_FINISH, STATE_SET_CURSOR_STATE,\r
28     STATE_SET_CURSOR_COLOR, STATE_SET_CURSOR_COLUMN,\r
29     STATE_SET_CURSOR_LINE, STATE_SET_CURSOR_STATE_OFF,\r
30     STATE_SET_CURSOR_STATE_ON, STATE_SET_CURSOR_STATE_BLINK\r
31   );\r
32 \r
33   signal state, state_next : STATE_TYPE;\r
34   signal line_int : integer range 0 to LINE_COUNT - 1;\r
35   signal line_next : integer range 0 to LINE_COUNT;\r
36   signal column_int : integer range 0 to COLUMN_COUNT - 1;\r
37   signal column_save, column_save_next : integer range 0 to COLUMN_COUNT - 1;\r
38   signal column_next : integer range 0 to COLUMN_COUNT;\r
39   signal scroll_int : integer range 0 to LINE_COUNT - 1;\r
40   signal scroll_next : integer range 0 to LINE_COUNT;  \r
41   signal ack_next : std_logic;  \r
42   signal background_color_int, background_color_next : std_logic_vector(RED_BITS + GREEN_BITS + BLUE_BITS - 1 downto 0);\r
43   signal cursor_color_int, cursor_color_next : std_logic_vector(RED_BITS + GREEN_BITS + BLUE_BITS - 1 downto 0);\r
44   signal cursor_state_int, cursor_state_next : CURSOR_STATE_TYPE;\r
45 begin\r
46   column_address <= std_logic_vector(to_unsigned(column_int, log2c(COLUMN_COUNT)));\r
47   row_address <= std_logic_vector(to_unsigned(line_int, log2c(LINE_COUNT)));\r
48   scroll_address <= std_logic_vector(to_unsigned(scroll_int, log2c(LINE_COUNT)));\r
49   background_color <= background_color_int;\r
50   cursor_color <= cursor_color_int;\r
51   cursor_state <= cursor_state_int;\r
52 \r
53   process(state, line_int, column_int, scroll_int, command, command_data, command_req)\r
54   begin\r
55     state_next <= state;\r
56     \r
57     case state is\r
58       when STATE_IDLE =>\r
59         if command_req = '1' then\r
60           if command = COMMAND_SET_CHAR then\r
61             if command_data(CHAR_SIZE - 1 downto 0) = CHAR_NEW_LINE then\r
62               if line_int < LINE_COUNT - 1 then\r
63                 state_next <= STATE_NEW_LINE_SIMPLE;\r
64               else  \r
65                 state_next <= STATE_NEW_LINE_SCROLL;\r
66               end if;\r
67             elsif command_data(CHAR_SIZE - 1 downto 0) = CHAR_CARRIAGE_RETURN then\r
68               state_next <= STATE_CARRIAGE_RETURN;\r
69             else\r
70               if column_int < COLUMN_COUNT - 1 then\r
71                 state_next <= STATE_SET_CHAR;\r
72               else\r
73                 if line_int < LINE_COUNT - 1 then\r
74                   state_next <= STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SIMPLE;\r
75                 else\r
76                   state_next <= STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SCROLL;\r
77                 end if;\r
78               end if;\r
79             end if;      \r
80           elsif command = COMMAND_SET_BACKGROUND then\r
81             state_next <= STATE_SET_BACKGROUND;\r
82           elsif command = COMMAND_SET_CURSOR_STATE then\r
83             state_next <= STATE_SET_CURSOR_STATE;\r
84           elsif command = COMMAND_SET_CURSOR_COLOR then\r
85             state_next <= STATE_SET_CURSOR_COLOR;\r
86           elsif command = COMMAND_SET_CURSOR_COLUMN then\r
87             state_next <= STATE_SET_CURSOR_COLUMN;\r
88           elsif command = COMMAND_SET_CURSOR_LINE then\r
89             state_next <= STATE_SET_CURSOR_LINE;\r
90           else\r
91             state_next <= STATE_ACK_UNKNOWN;\r
92           end if;\r
93         end if;\r
94       when STATE_ACK_UNKNOWN =>\r
95         state_next <= STATE_WAIT_REQ_RELEASE;\r
96       when STATE_NEW_LINE_SIMPLE =>\r
97         state_next <= STATE_WAIT_REQ_RELEASE;\r
98       when STATE_NEW_LINE_SCROLL =>\r
99         if scroll_int < LINE_COUNT - 1 then\r
100           state_next <= STATE_SCROLL_NEXT;\r
101         else\r
102           state_next <= STATE_SCROLL_TOP;\r
103         end if;        \r
104       when STATE_CARRIAGE_RETURN =>\r
105         state_next <= STATE_WAIT_REQ_RELEASE;\r
106       when STATE_SET_CHAR =>\r
107         state_next <= STATE_WAIT_REQ_RELEASE;\r
108       when STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SIMPLE =>\r
109         state_next <= STATE_WAIT_REQ_RELEASE;\r
110       when STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SCROLL =>\r
111         if scroll_int < LINE_COUNT - 1 then\r
112           state_next <= STATE_SCROLL_NEXT;\r
113         else\r
114           state_next <= STATE_SCROLL_TOP;\r
115         end if;\r
116       when STATE_SCROLL_NEXT =>\r
117         state_next <= STATE_SCROLL_CLEAR_LINE;\r
118       when STATE_SCROLL_TOP =>\r
119         state_next <= STATE_SCROLL_CLEAR_LINE;\r
120       when STATE_SCROLL_CLEAR_LINE =>\r
121         state_next <= STATE_SCROLL_CLEAR_LINE_LOOP;\r
122       when STATE_SCROLL_CLEAR_LINE_LOOP =>\r
123         if column_int = COLUMN_COUNT - 2 then\r
124           state_next <= STATE_SCROLL_CLEAR_LINE_FINISH;\r
125         end if;\r
126       when STATE_SCROLL_CLEAR_LINE_FINISH =>\r
127         state_next <= STATE_WAIT_REQ_RELEASE;\r
128       when STATE_WAIT_REQ_RELEASE =>\r
129         if command_req = '0' then\r
130           state_next <= STATE_IDLE;\r
131         end if;\r
132       when STATE_SET_BACKGROUND =>\r
133         state_next <= STATE_WAIT_REQ_RELEASE;\r
134       when STATE_SET_CURSOR_STATE =>\r
135         if command_data(1 downto 0) = "00" then\r
136           state_next <= STATE_SET_CURSOR_STATE_OFF;\r
137         elsif command_data(1 downto 0) = "01" then\r
138           state_next <= STATE_SET_CURSOR_STATE_ON;\r
139         else\r
140           state_next <= STATE_SET_CURSOR_STATE_BLINK;\r
141         end if;\r
142       when STATE_SET_CURSOR_STATE_OFF =>\r
143         state_next <= STATE_WAIT_REQ_RELEASE;\r
144       when STATE_SET_CURSOR_STATE_ON =>\r
145         state_next <= STATE_WAIT_REQ_RELEASE;\r
146       when STATE_SET_CURSOR_STATE_BLINK =>\r
147         state_next <= STATE_WAIT_REQ_RELEASE;\r
148       when STATE_SET_CURSOR_COLOR =>\r
149         state_next <= STATE_WAIT_REQ_RELEASE;\r
150       when STATE_SET_CURSOR_COLUMN =>\r
151         state_next <= STATE_WAIT_REQ_RELEASE;\r
152       when STATE_SET_CURSOR_LINE =>\r
153         state_next <= STATE_WAIT_REQ_RELEASE;\r
154     end case;\r
155   end process;\r
156 \r
157   process(state, line_int, column_int, scroll_int, column_save, background_color_int, cursor_color_int, cursor_state_int, command_data)\r
158   begin\r
159     line_next <= line_int;\r
160     column_next <= column_int;\r
161     scroll_next <= scroll_int;\r
162     column_save_next <= column_save;\r
163     data <= std_logic_vector(to_unsigned(0, RED_BITS + GREEN_BITS + BLUE_BITS + CHAR_SIZE));\r
164     ack_next <= '0';\r
165     wr <= '0';\r
166     background_color_next <= background_color_int;\r
167     cursor_color_next <= cursor_color_int;\r
168     cursor_state_next <= cursor_state_int;\r
169         \r
170     case state is\r
171       when STATE_IDLE =>\r
172         null;\r
173       when STATE_ACK_UNKNOWN =>\r
174         ack_next <= '1';\r
175       when STATE_NEW_LINE_SIMPLE =>\r
176         line_next <= line_int + 1;\r
177         ack_next <= '1';\r
178       when STATE_NEW_LINE_SCROLL =>\r
179         null;\r
180       when STATE_CARRIAGE_RETURN =>\r
181         column_next <= 0;\r
182         ack_next <= '1';\r
183       when STATE_SET_CHAR =>\r
184         data <= command_data(CHAR_SIZE + 3 * COLOR_SIZE - 1 downto CHAR_SIZE + 3 * COLOR_SIZE - RED_BITS) &\r
185                 command_data(CHAR_SIZE + 2 * COLOR_SIZE - 1 downto CHAR_SIZE + 2 * COLOR_SIZE - GREEN_BITS) &\r
186                 command_data(CHAR_SIZE + COLOR_SIZE - 1 downto CHAR_SIZE + COLOR_SIZE - BLUE_BITS) &\r
187                 command_data(CHAR_SIZE - 1 downto 0);\r
188         wr <= '1';\r
189         column_next <= column_int + 1;\r
190         ack_next <= '1';\r
191       when STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SIMPLE =>\r
192         data <= command_data(CHAR_SIZE + 3 * COLOR_SIZE - 1 downto CHAR_SIZE + 3 * COLOR_SIZE - RED_BITS) &\r
193                 command_data(CHAR_SIZE + 2 * COLOR_SIZE - 1 downto CHAR_SIZE + 2 * COLOR_SIZE - GREEN_BITS) &\r
194                 command_data(CHAR_SIZE + COLOR_SIZE - 1 downto CHAR_SIZE + COLOR_SIZE - BLUE_BITS) &\r
195                 command_data(CHAR_SIZE - 1 downto 0);\r
196         wr <= '1';\r
197         line_next <= line_int + 1;\r
198         column_next <= 0;\r
199         ack_next <= '1';\r
200       when STATE_SET_CHAR_NEW_LINE_AND_CARRIAGE_RETURN_SCROLL =>\r
201         data <= command_data(CHAR_SIZE + 3 * COLOR_SIZE - 1 downto CHAR_SIZE + 3 * COLOR_SIZE - RED_BITS) &\r
202                 command_data(CHAR_SIZE + 2 * COLOR_SIZE - 1 downto CHAR_SIZE + 2 * COLOR_SIZE - GREEN_BITS) &\r
203                 command_data(CHAR_SIZE + COLOR_SIZE - 1 downto CHAR_SIZE + COLOR_SIZE - BLUE_BITS) &\r
204                 command_data(CHAR_SIZE - 1 downto 0);\r
205         wr <= '1';\r
206         column_next <= 0;\r
207       when STATE_SCROLL_NEXT =>\r
208         scroll_next <= scroll_int + 1;\r
209       when STATE_SCROLL_TOP =>\r
210         scroll_next <= 0;\r
211       when STATE_SCROLL_CLEAR_LINE =>\r
212         column_save_next <= column_int;\r
213         column_next <= 0;\r
214       when STATE_SCROLL_CLEAR_LINE_LOOP =>\r
215         data <= COLOR_BLACK(3 * COLOR_SIZE - 1 downto 3 * COLOR_SIZE - RED_BITS) &\r
216                 COLOR_BLACK(2 * COLOR_SIZE - 1 downto 2 * COLOR_SIZE - GREEN_BITS) &\r
217                 COLOR_BLACK(COLOR_SIZE - 1 downto COLOR_SIZE - BLUE_BITS) &\r
218                 CHAR_NULL;\r
219         wr <= '1';\r
220         column_next <= column_int + 1;\r
221       when STATE_SCROLL_CLEAR_LINE_FINISH =>\r
222         data <= COLOR_BLACK(3 * COLOR_SIZE - 1 downto 3 * COLOR_SIZE - RED_BITS) &\r
223                 COLOR_BLACK(2 * COLOR_SIZE - 1 downto 2 * COLOR_SIZE - GREEN_BITS) &\r
224                 COLOR_BLACK(COLOR_SIZE - 1 downto COLOR_SIZE - BLUE_BITS) &\r
225                 CHAR_NULL;\r
226         wr <= '1';\r
227         column_next <= column_save;\r
228         ack_next <= '1';\r
229       when STATE_WAIT_REQ_RELEASE =>\r
230         ack_next <= '1';\r
231       when STATE_SET_BACKGROUND =>\r
232         background_color_next <=\r
233           command_data(3 * COLOR_SIZE - 1 downto 3 * COLOR_SIZE - RED_BITS) &\r
234           command_data(2 * COLOR_SIZE - 1 downto 2 * COLOR_SIZE - GREEN_BITS) &\r
235           command_data(COLOR_SIZE - 1 downto COLOR_SIZE - BLUE_BITS);\r
236         ack_next <= '1';\r
237       when STATE_SET_CURSOR_STATE =>\r
238         null;\r
239       when STATE_SET_CURSOR_STATE_OFF =>\r
240         cursor_state_next <= CURSOR_OFF;\r
241         ack_next <= '1';\r
242       when STATE_SET_CURSOR_STATE_ON =>\r
243         cursor_state_next <= CURSOR_ON;\r
244         ack_next <= '1';\r
245       when STATE_SET_CURSOR_STATE_BLINK =>\r
246         cursor_state_next <= CURSOR_BLINK;\r
247         ack_next <= '1';\r
248       when STATE_SET_CURSOR_COLOR =>\r
249         cursor_color_next <=\r
250           command_data(3 * COLOR_SIZE - 1 downto 3 * COLOR_SIZE - RED_BITS) &\r
251           command_data(2 * COLOR_SIZE - 1 downto 2 * COLOR_SIZE - GREEN_BITS) &\r
252           command_data(COLOR_SIZE - 1 downto COLOR_SIZE - BLUE_BITS);\r
253         ack_next <= '1';\r
254       when STATE_SET_CURSOR_COLUMN =>\r
255         column_next <= to_integer(unsigned(command_data(log2c(COLUMN_COUNT) - 1 downto 0)));\r
256         ack_next <= '1';\r
257       when STATE_SET_CURSOR_LINE =>\r
258         line_next <= to_integer(unsigned(command_data(log2c(LINE_COUNT) - 1 downto 0)));\r
259         ack_next <= '1';\r
260     end case;\r
261   end process;\r
262 \r
263   process(vga_clk, vga_res_n)\r
264   begin\r
265     if vga_res_n = '0' then\r
266       state <= STATE_IDLE;\r
267       line_int <= 0;\r
268       column_int <= 0;\r
269       scroll_int <= 0;\r
270       column_save <= 0;\r
271       ack <= '0';\r
272       background_color_int <= COLOR_BLACK(3 * COLOR_SIZE - 1 downto 3 * COLOR_SIZE - RED_BITS) &\r
273                               COLOR_BLACK(2 * COLOR_SIZE - 1 downto 2 * COLOR_SIZE - GREEN_BITS) &\r
274                               COLOR_BLACK(COLOR_SIZE - 1 downto COLOR_SIZE - BLUE_BITS);\r
275       cursor_color_int <= COLOR_WHITE(3 * COLOR_SIZE - 1 downto 3 * COLOR_SIZE - RED_BITS) &\r
276                           COLOR_WHITE(2 * COLOR_SIZE - 1 downto 2 * COLOR_SIZE - GREEN_BITS) &\r
277                           COLOR_WHITE(COLOR_SIZE - 1 downto COLOR_SIZE - BLUE_BITS);\r
278       cursor_state_int <= CURSOR_BLINK;\r
279     elsif rising_edge(vga_clk) then\r
280       state <= state_next;\r
281       line_int <= line_next;\r
282       column_int <= column_next;\r
283       scroll_int <= scroll_next;\r
284       column_save <= column_save_next;\r
285       ack <= ack_next;\r
286       background_color_int <= background_color_next;\r
287       cursor_color_int <= cursor_color_next;\r
288       cursor_state_int <= cursor_state_next;\r
289     end if;\r
290   end process;\r
291 end architecture beh;\r