uart_rx: ein prozessmodell. spart weitere 3 logic elements :P
[hwmod.git] / src / textmode_vga / textmode_vga_v_sm_beh.vhd
1 -------------------------------------------------------------------------\r
2 --\r
3 -- Filename: textmode_vga_v_sm_beh.vhd\r
4 -- =========\r
5 --\r
6 -- Short Description:\r
7 -- ==================\r
8 --   Behavioural implementation of the vertical VGA timing finite state machine\r
9 --\r
10 -------------------------------------------------------------------------\r
11 \r
12 library ieee;
13 use ieee.std_logic_1164.all;
14 use ieee.numeric_std.all;
15 use work.math_pkg.all;
16 use work.textmode_vga_pkg.all;
17
18 architecture beh of textmode_vga_v_sm is
19   type TEXTMODE_VGA_V_SM_STATE_TYPE is
20   (
21     VSYNC_FIRST, VSYNC, VSYNC_NEXT,
22     VBACK_FIRST, VBACK, VBACK_NEXT,
23     VDATA_FIRST, VDATA, VDATA_NEXTLINE, VDATA_NEXTCHAR,
24     VFRONT_FIRST, VFRONT, VFRONT_NEXT    
25   );
26   signal textmode_vga_v_sm_state, textmode_vga_v_sm_state_next : TEXTMODE_VGA_V_SM_STATE_TYPE;
27   signal vcnt, vcnt_next : integer range 0 to max(VSYNC_LINES, VBACK_LINES, VFRONT_LINES);
28   signal char_height_pixel_int, char_height_pixel_int_next : integer range 0 to CHAR_HEIGHT;
29   signal char_line_cnt_int, char_line_cnt_int_next : integer range 0 to LINE_COUNT;
30 begin
31   char_height_pixel <= std_logic_vector(to_unsigned(char_height_pixel_int, log2c(CHAR_HEIGHT)));
32   char_line_cnt <= std_logic_vector(to_unsigned(char_line_cnt_int, log2c(LINE_COUNT)));
33   
34   v_sm_next_state : process(textmode_vga_v_sm_state, is_eol, vcnt, char_height_pixel_int, char_line_cnt_int)
35   begin
36     textmode_vga_v_sm_state_next <= textmode_vga_v_sm_state;
37     
38     case textmode_vga_v_sm_state is
39       when VSYNC_FIRST =>
40         textmode_vga_v_sm_state_next <= VSYNC;
41       when VSYNC =>
42         if is_eol = '1' then
43           if vcnt = VSYNC_LINES - 1 then
44             textmode_vga_v_sm_state_next <= VBACK_FIRST;
45           else
46             textmode_vga_v_sm_state_next <= VSYNC_NEXT;
47           end if;
48         end if;
49       when VSYNC_NEXT =>
50         textmode_vga_v_sm_state_next <= VSYNC;
51       when VBACK_FIRST =>
52         textmode_vga_v_sm_state_next <= VBACK;
53       when VBACK =>
54         if is_eol = '1' then
55           if vcnt = VBACK_LINES - 1 then
56             textmode_vga_v_sm_state_next <= VDATA_FIRST;
57           else
58             textmode_vga_v_sm_state_next <= VBACK_NEXT;
59           end if;
60         end if;
61       when VBACK_NEXT =>
62         textmode_vga_v_sm_state_next <= VBACK;
63       when VDATA_FIRST =>
64         textmode_vga_v_sm_state_next <= VDATA;
65       when VDATA =>
66         if is_eol = '1' then
67           if char_height_pixel_int = CHAR_HEIGHT - 1 then
68             if char_line_cnt_int = LINE_COUNT - 1 then
69               textmode_vga_v_sm_state_next <= VFRONT_FIRST;
70             else
71               textmode_vga_v_sm_state_next <= VDATA_NEXTCHAR;
72             end if;
73           else
74             textmode_vga_v_sm_state_next <= VDATA_NEXTLINE;
75           end if;
76         end if;
77       when VDATA_NEXTLINE =>
78         textmode_vga_v_sm_state_next <= VDATA;
79       when VDATA_NEXTCHAR =>
80         textmode_vga_v_sm_state_next <= VDATA;
81       when VFRONT_FIRST =>
82         textmode_vga_v_sm_state_next <= VFRONT;
83       when VFRONT =>
84         if is_eol = '1' then
85           if vcnt = VFRONT_LINES - 1 then
86             textmode_vga_v_sm_state_next <= VSYNC_FIRST;
87           else
88             textmode_vga_v_sm_state_next <= VFRONT_NEXT;
89           end if;
90         end if;
91       when VFRONT_NEXT =>
92         textmode_vga_v_sm_state_next <= VFRONT;
93     end case;
94   end process v_sm_next_state;
95
96   v_sm_output : process(textmode_vga_v_sm_state, vcnt, char_height_pixel_int, char_line_cnt_int)
97   begin
98     vcnt_next <= vcnt;
99     is_data_line <= '0';
100     vsync_n <= '1';
101     char_height_pixel_int_next <= char_height_pixel_int;
102     char_line_cnt_int_next <= char_line_cnt_int;
103     
104     case textmode_vga_v_sm_state is
105       when VSYNC_FIRST =>
106         vsync_n <= '0';
107         vcnt_next <= 0;
108       when VSYNC =>
109         vsync_n <= '0';
110       when VSYNC_NEXT =>
111         vsync_n <= '0';
112         vcnt_next <= vcnt + 1;
113       when VBACK_FIRST =>
114         vcnt_next <= 0;        
115       when VBACK =>
116         null;
117       when VBACK_NEXT =>
118         vcnt_next <= vcnt + 1;
119       when VDATA_FIRST =>
120         is_data_line <= '1';
121         char_height_pixel_int_next <= 0;
122         char_line_cnt_int_next <= 0;
123       when VDATA =>
124         is_data_line <= '1';
125       when VDATA_NEXTLINE =>
126         is_data_line <= '1';
127         char_height_pixel_int_next <= char_height_pixel_int + 1;
128       when VDATA_NEXTCHAR =>
129         is_data_line <= '1';
130         char_height_pixel_int_next <= 0;
131         char_line_cnt_int_next <= char_line_cnt_int + 1;
132       when VFRONT_FIRST =>
133         vcnt_next <= 0;
134       when VFRONT =>
135         null;
136       when VFRONT_NEXT =>
137         vcnt_next <= vcnt + 1;
138     end case;
139   end process v_sm_output;
140   
141   sync : process(sys_clk, sys_res_n)
142   begin
143     if sys_res_n = '0' then
144       textmode_vga_v_sm_state <= VSYNC_FIRST;
145       vcnt <= 0;
146       char_height_pixel_int <= 0;      
147       char_line_cnt_int <= 0;
148     elsif rising_edge(sys_clk) then
149       textmode_vga_v_sm_state <= textmode_vga_v_sm_state_next;
150       vcnt <= vcnt_next;
151       char_height_pixel_int <= char_height_pixel_int_next;
152       char_line_cnt_int <= char_line_cnt_int_next;
153     end if;
154   end process sync;
155 end architecture beh;