fixed timing and registers
[hwmod.git] / src / pc_communication.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use work.gen_pkg.all;
5
6 entity pc_communication is
7         port (
8                 sys_clk : in std_logic;
9                 sys_res_n : in std_logic;
10
11                 --button
12                 btn_a : in std_logic;
13
14                 --uart_tx
15                 tx_data : out std_logic_vector(7 downto 0);
16                 tx_new : out std_logic;
17                 tx_done : in std_logic;
18
19                 --uart_rx
20                 rx_data : in std_logic_vector(7 downto 0);
21                 rx_new : in std_logic;
22
23                 -- History
24                 d_zeile : out hzeile;
25                 d_spalte : out hspalte;
26                 d_get :  out std_logic;
27                 d_done : in std_logic;
28                 d_char : in hbyte
29         );
30 end entity pc_communication;
31
32 architecture beh of pc_communication is
33         signal push_history, push_history_next : std_logic;
34
35         signal spalte, spalte_next : integer range 1 to hspalte_max + 1;
36         signal zeile , zeile_next : integer range 1 to hzeile_max + 1;
37         signal spalte_up, spalte_up_next : std_logic;
38         signal get, get_next : std_logic;
39         signal new_i, new_i_next : std_logic;
40         signal tx_done_i, tx_done_i_next : std_logic;
41         signal tx_data_i, tx_data_i_next : std_logic_vector (7 downto 0);
42         signal d_done_i : std_logic;
43         signal s_done, s_done_next : std_logic;
44
45         signal char, char_next : hbyte;
46         signal char_en : std_logic;
47         type STATE_PC is (IDLE, FETCH, FORWARD, DONE);
48         signal state, state_next : STATE_PC ;
49
50 begin
51
52
53         d_zeile <= hzeile(std_logic_vector(to_unsigned(zeile,7)));
54         d_spalte <= hspalte(std_logic_vector(to_unsigned(spalte,7)));
55         d_get <= get;
56         char_next <= d_char;
57         tx_new <= new_i;
58         d_done_i <= d_done;
59         tx_done_i_next <= tx_done;
60         tx_data <= tx_data_i;
61
62         sync: process (sys_clk, sys_res_n)
63         begin
64                 if sys_res_n = '0' then
65                         state <= IDLE;
66                         push_history <= '0';
67                         spalte <= 1;
68                         zeile <= 1;
69                         get <= '0';
70                         new_i <= '0';
71                         tx_data_i <= "00000000";
72                         spalte_up <= '0';
73                         tx_done_i <= '0';
74                         s_done <= '0';
75                 elsif rising_edge(sys_clk) then
76                         push_history <= push_history_next;
77                         spalte <= spalte_next;
78                         zeile <= zeile_next;
79                         state <= state_next;
80                         get <= get_next;
81                         new_i <= new_i_next;
82                         tx_done_i <= tx_done_i_next;
83                         tx_data_i <= tx_data_i_next;
84                         spalte_up <= spalte_up_next;
85                         s_done <= s_done_next;
86                         if (char_en = '1') then
87                                 char <= char_next;
88                         end if;
89                 end if;
90         end process sync;
91
92         async_push_history : process (rx_new, rx_data, btn_a)
93         begin
94                 if rx_new = '1' then
95                         if rx_data = X"41" then
96                                 push_history_next <= '1';
97                         else
98                                 push_history_next <= '0';
99                         end if;
100                 elsif btn_a = '1' then
101                                 push_history_next <= '1';
102                 else
103                         push_history_next <= '0';
104                 end if;
105         end process async_push_history;
106
107         output_pc : process (state, zeile, spalte, char, tx_done_i, spalte_up)
108         begin
109                 get_next <= '0';
110                 new_i_next <= '0';
111
112                 spalte_up_next <= '0';
113                 s_done_next <= '0';
114                 spalte_next <= spalte;
115                 zeile_next <= zeile;
116                 tx_data_i_next <= tx_data_i;
117
118                 if spalte_up = '1' then
119                         if spalte = hspalte_max  then
120                                 if zeile = hzeile_max then
121                                         spalte_next <= 1;
122                                         zeile_next <= 1;
123                                         s_done_next <= '1';
124                                 else
125                                         spalte_next <= 1;
126                                         zeile_next <= zeile + 1;
127                                 end if;
128                         else
129                                 spalte_next <= spalte + 1; --overflow here!
130                                 zeile_next <= zeile;
131                         end if;
132                 end if;
133
134                 case state is
135                         when IDLE =>
136                                 null;
137                         when FETCH =>
138                                 get_next <= '1';
139                                 char_en <= '1';
140                         when FORWARD =>
141                                 char_en <= '0';
142                                 tx_data_i_next <= char;
143                                 new_i_next <= '1';
144                                 if (tx_done_i = '1') then
145                                         spalte_up_next <= '1';
146                                 end if;
147
148                         when DONE =>
149                                 null;
150                 end case;
151         end process output_pc;
152
153         next_state_pc : process (rx_new, btn_a, d_done, tx_done_i, s_done)
154         begin
155                 case state is
156                         when IDLE =>
157                                 if rx_new = '1' or btn_a = '1' then
158                                         state_next <= FETCH;
159                                 end if;
160                         when FETCH =>
161                                 if (d_done = '1') then
162                                         state_next <= FORWARD;
163                                 elsif (s_done = '1') then
164                                         state_next <= IDLE;
165                                 end if;
166                         when FORWARD =>
167                                 if (tx_done_i = '1') then
168                                         state_next <= FETCH;
169                                 end if;
170                         when DONE =>
171                                 state_next <= IDLE;
172                 end case;
173         end process next_state_pc;
174
175 end architecture beh;