added waiting states to pccom
[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 spalte, spalte_next : integer range 1 to hspalte_max + 1;
34         signal zeile , zeile_next : integer range 1 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, WAIT_HIST, FETCH, FORWARD, WAIT_UART, UART_DONE);
41         signal state, state_next : STATE_PC ;
42
43 begin
44
45
46         d_zeile <= hzeile(std_logic_vector(to_unsigned(zeile,7)));
47         d_spalte <= hspalte(std_logic_vector(to_unsigned(spalte,7)));
48         d_get <= get;
49         tx_new <= new_i;
50         tx_done_i_next <= tx_done;
51         tx_data <= tx_data_i;
52
53         sync: process (sys_clk, sys_res_n)
54         begin
55                 if sys_res_n = '0' then
56                         state <= IDLE;
57                         spalte <= 1;
58                         zeile <= 1;
59                         get <= '0';
60                         new_i <= '0';
61                         tx_data_i <= "00000000";
62                         tx_done_i <= '0';
63                 elsif rising_edge(sys_clk) then
64                         spalte <= spalte_next;
65                         zeile <= zeile_next;
66                         state <= state_next;
67                         get <= get_next;
68                         new_i <= new_i_next;
69                         tx_done_i <= tx_done_i_next;
70                         tx_data_i <= tx_data_i_next;
71                 end if;
72         end process sync;
73
74         output_pc : process (state, zeile, spalte, tx_data_i, tx_done_i, d_char)
75         begin
76                 get_next <= '0';
77                 new_i_next <= '0';
78
79                 spalte_next <= spalte;
80                 zeile_next <= zeile;
81                 tx_data_i_next <= tx_data_i;
82
83                 case state is
84                         when IDLE =>
85                                 null;
86                         when FETCH =>
87                                 get_next <= '1';
88                         when WAIT_HIST =>
89                                 tx_data_i_next <= d_char;
90                         when FORWARD =>
91                                 new_i_next <= '1';
92                         when WAIT_UART =>
93                                 null;
94                         when UART_DONE =>
95                                 if tx_data_i = x"00" or spalte = hspalte_max then
96                                         zeile_next <= zeile + 1;
97                                         spalte_next <= 1;
98                                         if zeile = hzeile_max then
99                                                 zeile_next <= 1;
100                                         end if;
101                                 else
102                                         spalte_next <= spalte + 1;
103                                 end if;
104                 end case;
105         end process output_pc;
106
107         next_state_pc : process (btn_a, d_done, rx_new, rx_data, spalte, state, tx_data_i ,tx_done_i, zeile)
108         begin
109                 state_next <= state;
110                 case state is
111                         when IDLE =>
112                                 if (rx_new = '1' and rx_data = x"0a" ) or btn_a = '1' then
113                                         state_next <= FETCH;
114                                 end if;
115                         when FETCH =>
116                                 state_next <= WAIT_HIST;
117                         when WAIT_HIST =>
118                                 if (d_done = '1') then
119                                         state_next <= FORWARD;
120                                 end if;
121                         when FORWARD =>
122                                 state_next <= WAIT_UART;
123                         when WAIT_UART =>
124                                 if (tx_done_i = '1') then
125                                         state_next <= UART_DONE;
126                                 end if;
127                         when UART_DONE =>
128                                 if (tx_data_i = x"00" or spalte = hspalte_max) and
129                                         zeile = hzeile_max then
130                                         state_next <= IDLE;
131                                 else
132                                         state_next <= FETCH;
133                                 end if;
134                 end case;
135         end process next_state_pc;
136
137 end architecture beh;