ca885dd5da144295b812c08b926f015af20323c0
[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                 pc_zeile : out hzeile;
25                 pc_spalte : out hspalte;
26                 pc_get :  out std_logic;
27                 pc_done : in std_logic;
28                 pc_char : in hbyte
29         );
30 end entity pc_communication;
31
32 architecture beh of pc_communication is
33         signal spalte, spalte_next : integer range 1 to HSPALTE_MAX + 2;
34         signal zeile , zeile_next : integer range 0 to HZEILE_MAX + 1;
35         signal get, get_next : std_logic;
36         signal new_i, new_i_next : std_logic;
37         signal tx_done_i, tx_done_i_next : std_logic;
38         signal tx_data_i, tx_data_i_next : std_logic_vector (7 downto 0);
39
40         type STATE_PC is (IDLE, FETCH, FORWARD, WAIT_UART, UART_DONE, CR, CR_WAIT, NL, NL_WAIT);
41         signal state, state_next : STATE_PC ;
42 begin
43         pc_zeile <= hzeile(std_logic_vector(to_unsigned(zeile,7)));
44         pc_spalte <= hspalte(std_logic_vector(to_unsigned(spalte,7)));
45         pc_get <= get;
46         tx_new <= new_i;
47         tx_done_i_next <= tx_done;
48         tx_data <= tx_data_i;
49
50         sync: process (sys_clk, sys_res_n)
51         begin
52                 if sys_res_n = '0' then
53                         state <= IDLE;
54                         spalte <= 1;
55                         zeile <= 0;
56                         get <= '0';
57                         new_i <= '0';
58                         tx_data_i <= x"00";
59                         tx_done_i <= '0';
60                 elsif rising_edge(sys_clk) then
61                         spalte <= spalte_next;
62                         zeile <= zeile_next;
63                         state <= state_next;
64                         get <= get_next;
65                         new_i <= new_i_next;
66                         tx_done_i <= tx_done_i_next;
67                         tx_data_i <= tx_data_i_next;
68                 end if;
69         end process sync;
70
71         process (state, zeile, spalte, tx_data_i, tx_done_i, pc_char, rx_new, btn_a,
72                 pc_done)
73         begin
74                 get_next <= '0';
75                 new_i_next <= '0';
76                 spalte_next <= spalte;
77                 zeile_next <= zeile;
78                 tx_data_i_next <= tx_data_i;
79
80                 state_next <= state;
81                 case state is
82                         when IDLE =>
83 --                              if (rx_new = '1' and rx_data = x"0a") or btn_a = '0' then
84                                 if (rx_new = '1') or btn_a = '0' then
85                                         state_next <= FETCH;
86                                 end if;
87                         when FETCH =>
88                                 get_next <= '1';
89                                 if pc_done = '1' and tx_done_i = '0' then
90                                         if pc_char = x"00" then
91                                                 state_next <= UART_DONE;
92                                         else
93                                                 state_next <= FORWARD;
94                                         end if;
95                                 end if;
96                         when FORWARD =>
97                                 tx_data_i_next <= pc_char;
98                                 new_i_next <= '1';
99                                 -- halte pc_get weiterhin high sodass pc_char garantiert gleich bleibt
100                                 get_next <= '1';
101                                 state_next <= WAIT_UART;
102                         when WAIT_UART =>
103                                 new_i_next <= '1';
104                                 get_next <= '1';
105                                 if tx_done_i = '1' then
106                                         state_next <= UART_DONE;
107                                 end if;
108                         when UART_DONE => null;
109                                 state_next <= FETCH;
110                                 spalte_next <= spalte + 1;
111                                 if spalte = HSPALTE_MAX + 1 then
112                                         state_next <= NL;
113                                         spalte_next <= 1;
114                                         zeile_next <= zeile + 1;
115                                 end if;
116                         when NL =>
117                                 tx_data_i_next <= x"0a";
118                                 new_i_next <= '1';
119                                 if tx_done_i = '1' then
120                                         state_next <= NL_WAIT;
121                                 end if;
122                         when NL_WAIT =>
123                                 state_next <= CR;
124                         when CR =>
125                                 tx_data_i_next <= x"0d";
126                                 new_i_next <= '1';
127                                 if tx_done_i = '1' then
128                                         state_next <= CR_WAIT;
129                                 end if;
130                         when CR_WAIT =>
131                                 state_next <= FETCH;
132                                 if zeile = HZEILE_MAX then
133                                         state_next <= IDLE;
134                                         zeile_next <= 0;
135                                         spalte_next <= 1;
136                                 end if;
137                 end case;
138         end process;
139 end architecture beh;