pc_com: p_busy ist unnoetig und noch ein paar kleinere fehler ausgebessert
[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, CALC_VAL);
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         output_pc : process (state, zeile, spalte, tx_data_i, tx_done_i, pc_char)
72         begin
73                 get_next <= '0';
74                 new_i_next <= '0';
75
76                 spalte_next <= spalte;
77                 zeile_next <= zeile;
78                 tx_data_i_next <= tx_data_i;
79
80                 case state is
81                         when IDLE =>
82                                 null;
83                         when FETCH =>
84                                 get_next <= '1';
85                         when FORWARD =>
86                                 tx_data_i_next <= pc_char;
87                                 new_i_next <= '1';
88                                 -- halte pc_get weiterhin high sodass pc_char garantiert gleich bleibt
89                                 get_next <= '1';
90                         when WAIT_UART =>
91                                 new_i_next <= '1';
92                                 get_next <= '1';
93                         when UART_DONE => null;
94                                 -- get_next <= '0';
95                                 -- new_i_next <= '0';
96                         when CALC_VAL =>
97                                 spalte_next <= spalte + 1;
98                                 if spalte = HSPALTE_MAX + 1 then
99                                         tx_data_i_next <= x"0a";
100                                         new_i_next <= '1';
101
102                                         spalte_next <= 1;
103                                         zeile_next <= zeile + 1;
104                                         if zeile = HZEILE_MAX-1 then
105                                                 zeile_next <= 0;
106                                         end if;
107                                 end if;
108                 end case;
109         end process output_pc;
110
111         next_state_pc : process (btn_a, pc_done, rx_new, rx_data, spalte, state,
112                 tx_data_i ,tx_done_i, zeile, pc_char)
113         begin
114                 state_next <= state;
115                 case state is
116                         when IDLE =>
117 --                              if (rx_new = '1' and rx_data = x"0a") or btn_a = '0' then
118                                 if (rx_new = '1') or btn_a = '0' then
119                                         state_next <= FETCH;
120                                 end if;
121                         when FETCH =>
122                                 if pc_done = '1' and tx_done_i = '0' then
123                                         if pc_char = x"00" then
124                                                 state_next <= UART_DONE;
125                                         else
126                                                 state_next <= FORWARD;
127                                         end if;
128                                 end if;
129                         when FORWARD =>
130                                 state_next <= WAIT_UART;
131                         when WAIT_UART =>
132                                 if (tx_done_i = '1') then
133                                         state_next <= UART_DONE;
134                                 end if;
135                         when UART_DONE =>
136                                 state_next <= CALC_VAL;
137                         when CALC_VAL =>
138                                 if spalte = HSPALTE_MAX + 1 and zeile = HZEILE_MAX - 1 then
139                                         state_next <= IDLE;
140                                 else
141                                         state_next <= FETCH;
142                                 end if;
143                 end case;
144         end process next_state_pc;
145 end architecture beh;