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