pc-com: tx_done_* ist unnoetig bzw. verzoegert das ganze sogar
[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                 --button
11                 btn_a : in std_logic;
12                 --uart_tx
13                 tx_data : out std_logic_vector(7 downto 0);
14                 tx_new : out std_logic;
15                 tx_done : in std_logic;
16                 --uart_rx
17                 rx_data : in std_logic_vector(7 downto 0);
18                 rx_new : in std_logic;
19                 -- History
20                 pc_zeile : out hzeile;
21                 pc_spalte : out hspalte;
22                 pc_get :  out std_logic;
23                 pc_done : in std_logic;
24                 pc_char : in hbyte
25         );
26 end entity pc_communication;
27
28 architecture beh of pc_communication is
29         signal spalte, spalte_next : integer range 1 to HSPALTE_MAX + 2;
30         signal zeile , zeile_next : integer range 0 to HZEILE_MAX + 1;
31         signal get, get_next : std_logic;
32         signal new_i, new_i_next : std_logic;
33         signal tx_data_i, tx_data_i_next : std_logic_vector (7 downto 0);
34
35         type STATE_PC is (IDLE, FETCH, FORWARD, UART_DONE, CR, CR_WAIT,
36                 NL, NL_WAIT, PRINT_NO1, PRINT_NO1_WAIT, PRINT_NO2, PRINT_NO2_WAIT,
37                 PRINT_NO3, PRINT_NO3_WAIT, PRINT_NO4, PRINT_NO4_WAIT, PRINT_NO5,
38                 PRINT_NO5_WAIT, PRINT_NO6);
39         signal state, state_next : STATE_PC ;
40 begin
41         pc_zeile <= hzeile(std_logic_vector(to_unsigned(zeile,7)));
42         pc_spalte <= hspalte(std_logic_vector(to_unsigned(spalte,7)));
43         pc_get <= get;
44         tx_new <= new_i;
45         tx_data <= tx_data_i;
46
47         sync: process (sys_clk, sys_res_n)
48         begin
49                 if sys_res_n = '0' then
50                         state <= IDLE;
51                         spalte <= 1;
52                         zeile <= 0;
53                         get <= '0';
54                         new_i <= '0';
55                         tx_data_i <= x"00";
56                 elsif rising_edge(sys_clk) then
57                         spalte <= spalte_next;
58                         zeile <= zeile_next;
59                         state <= state_next;
60                         get <= get_next;
61                         new_i <= new_i_next;
62                         tx_data_i <= tx_data_i_next;
63                 end if;
64         end process sync;
65
66         process (state, zeile, spalte, tx_data_i, tx_done, pc_char, rx_new, btn_a,
67                         pc_done, rx_data)
68                 variable tmp : std_logic_vector(6 downto 0);
69         begin
70                 get_next <= '0';
71                 new_i_next <= '0';
72                 spalte_next <= spalte;
73                 zeile_next <= zeile;
74                 tx_data_i_next <= tx_data_i;
75
76                 state_next <= state;
77                 case state is
78                         when IDLE =>
79                                 zeile_next <= 0;
80                                 spalte_next <= 1;
81                                 if ((rx_new = '1' and rx_data = x"41") or btn_a = '0') and tx_done = '0' then
82                                         state_next <= PRINT_NO1;
83                                 end if;
84
85                         when PRINT_NO1 =>
86                                 tx_data_i_next <= x"28"; -- '('
87                                 new_i_next <= '1';
88                                 if tx_done = '1' then
89                                         state_next <= PRINT_NO1_WAIT;
90                                 end if;
91                         when PRINT_NO1_WAIT =>
92                                 if tx_done = '0' then
93                                         state_next <= PRINT_NO2;
94                                 end if;
95                         when PRINT_NO2 =>
96                                 tx_data_i_next <= zeile2char(std_logic_vector(to_unsigned(zeile,7)), 1);
97                                 new_i_next <= '1';
98                                 if tx_done = '1' then
99                                         state_next <= PRINT_NO2_WAIT;
100                                 end if;
101                         when PRINT_NO2_WAIT =>
102                                 if tx_done = '0' then
103                                         state_next <= PRINT_NO3;
104                                 end if;
105                         when PRINT_NO3 =>
106                                 tx_data_i_next <= zeile2char(std_logic_vector(to_unsigned(zeile,7)), 2);
107                                 new_i_next <= '1';
108                                 if tx_done = '1' then
109                                         state_next <= PRINT_NO3_WAIT;
110                                 end if;
111                         when PRINT_NO3_WAIT =>
112                                 if tx_done = '0' then
113                                         state_next <= PRINT_NO4;
114                                 end if;
115                         when PRINT_NO4 =>
116                                 tx_data_i_next <= x"29"; -- ')'
117                                 new_i_next <= '1';
118                                 if tx_done = '1' then
119                                         state_next <= PRINT_NO4_WAIT;
120                                 end if;
121                         when PRINT_NO4_WAIT =>
122                                 if tx_done = '0' then
123                                         state_next <= PRINT_NO5;
124                                 end if;
125                         when PRINT_NO5 =>
126                                 tx_data_i_next <= x"24"; -- '$'
127                                 new_i_next <= '1';
128                                 if tx_done = '1' then
129                                         state_next <= PRINT_NO5_WAIT;
130                                 end if;
131                         when PRINT_NO5_WAIT =>
132                                 if tx_done = '0' then
133                                         state_next <= PRINT_NO6;
134                                 end if;
135                         when PRINT_NO6 =>
136                                 tx_data_i_next <= x"20"; -- ' '
137                                 new_i_next <= '1';
138                                 if tx_done = '1' then
139                                         state_next <= FETCH;
140                                 end if;
141
142                         when FETCH =>
143                                 get_next <= '1';
144                                 if pc_done = '1' and tx_done = '0' then
145                                         state_next <= FORWARD;
146                                         if pc_char = x"00" then
147                                                 state_next <= UART_DONE;
148                                         end if;
149                                 end if;
150                         when FORWARD =>
151                                 tx_data_i_next <= pc_char;
152                                 new_i_next <= '1';
153                                 -- halte pc_get weiterhin high sodass pc_char garantiert
154                                 -- gleicht bleibt (blockiert history!)
155                                 get_next <= '1';
156                                 if tx_done = '1' then
157                                         state_next <= UART_DONE;
158                                 end if;
159                         when UART_DONE =>
160                                 if tx_done = '0' then
161                                         state_next <= FETCH;
162                                         spalte_next <= spalte + 1;
163                                         if spalte = HSPALTE_MAX + 1 then
164                                                 state_next <= NL;
165                                                 spalte_next <= 1;
166                                                 zeile_next <= zeile + 1;
167                                         end if;
168                                 end if;
169                         when NL =>
170                                 tx_data_i_next <= x"0a";
171                                 new_i_next <= '1';
172                                 if tx_done = '1' then
173                                         state_next <= NL_WAIT;
174                                 end if;
175                         when NL_WAIT =>
176                                 if tx_done = '0' then
177                                         state_next <= CR;
178                                 end if;
179                         when CR =>
180                                 tx_data_i_next <= x"0d";
181                                 new_i_next <= '1';
182                                 if tx_done = '1' then
183                                         state_next <= CR_WAIT;
184                                 end if;
185                         when CR_WAIT =>
186                                 if tx_done = '0' then
187                                         tmp := std_logic_vector(to_unsigned(zeile,7));
188                                         if tmp(0) = '0' then
189                                                 -- es handelt sich um eingabe im naechsten schritt
190                                                 -- => print zeilennummer
191                                                 state_next <= PRINT_NO1;
192                                         else
193                                                 state_next <= FETCH;
194                                         end if;
195                                 end if;
196                                 if zeile = HZEILE_MAX then
197                                         state_next <= IDLE;
198                                 end if;
199                 end case;
200         end process;
201 end architecture beh;