small bugfixes that resolve signal instabilities where needed
[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); --not really required
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 push_history, push_history_next : std_logic;
34
35         signal spalte, spalte_next : hspalte;
36         signal zeile , zeile_next : hzeile;
37         signal spalte_up, spalte_up_next : std_logic;
38         signal get, get_next : std_logic;
39         signal new_i, new_i_next : std_logic;
40
41         signal char, char_next : hbyte;
42         signal char_en : std_logic;
43         type STATE_PC is (IDLE, FETCH, FORWARD, DONE);
44         signal state, state_next : STATE_PC ;
45
46 begin
47
48
49         d_zeile <= zeile;
50         d_spalte <= spalte;
51         d_get <= get;
52         char_next <= d_char;
53         tx_new <= new_i;
54
55         sync: process (sys_clk, sys_res_n)
56         begin
57                 if sys_res_n = '0' then
58                         state <= IDLE;
59                         push_history <= '0';
60                         spalte <= "0000000";
61                         spalte_next <= "0000000";
62                         zeile <= "0000000";
63                         zeile_next <= "0000000";
64                         get <= '0';
65                         new_i <= '0';
66                         tx_data <= "00000000";
67                 elsif rising_edge(sys_clk) then
68                         push_history <= push_history_next;
69                         spalte <= spalte_next;
70                         zeile <= zeile_next;
71                         state <= state_next;
72                         get <= get_next;
73                         new_i <= new_i_next;
74                         if (char_en = '1') then
75                                 char <= char_next;
76                         end if;
77                 end if;
78         end process sync;
79
80         process (spalte_up, spalte, zeile)
81         variable spalte_tmp, zeile_tmp : integer;
82         variable spalte2_tmp, zeile2_tmp : std_logic_vector(7 downto 0);
83         begin
84                 if (spalte_up = '1') then
85                         if (spalte > X"45") then
86                                 spalte_next <= "0000000";
87                                 zeile_tmp := to_integer(unsigned(zeile)) + 1;
88                                 zeile2_tmp := std_logic_vector(to_unsigned(zeile_tmp,8));
89                                 zeile_next <= hzeile(zeile2_tmp(6 downto 0));
90                         else
91                                 spalte_tmp := to_integer(unsigned(spalte)) + 1;
92                                 spalte2_tmp := std_logic_vector(to_unsigned(spalte_tmp,8));
93                                 spalte_next <= hspalte(spalte2_tmp(6 downto 0));
94                                 zeile_next <= zeile;
95                         end if;
96                         spalte_up <= '0';
97                 end if;
98         end process;
99
100         async_push_history : process (rx_new, rx_data, btn_a)
101         begin
102                 if rx_new = '1' then
103                         if rx_data = X"41" then
104                                 push_history_next <= '1';
105                         else
106                                 push_history_next <= '0';
107                         end if;
108                 elsif btn_a = '1' then
109                                 push_history_next <= '1';
110                 else
111                         push_history_next <= '0';
112                 end if;
113         end process async_push_history;
114
115         output_pc : process (state, zeile, spalte, char)
116         begin
117                 get_next <= '0';
118                 new_i_next <= '0';
119                 spalte_next <= "0000000";
120                 zeile_next <= "0000000";
121                 case state is
122                         when IDLE =>
123                                 null;
124                         when FETCH =>
125                                 get_next <= '1';
126                                 char_en <= '1';
127                         when FORWARD =>
128                                 char_en <= '0';
129                                 tx_data <= char;
130                                 new_i_next <= '1';
131                         when DONE =>
132                                 null;
133                 end case;
134         end process output_pc;
135
136         next_state_pc : process (rx_new, btn_a, d_done, tx_done)
137         begin
138                 spalte_up <= '0';
139                 case state is
140                         when IDLE =>
141                                 if rx_new = '1' or btn_a = '1' then
142                                         state_next <= FETCH;
143                                         
144                                 end if;
145                         when FETCH =>
146                                 if (d_done = '1') then
147                                         state_next <= FORWARD;
148                                 end if;
149                         when FORWARD =>
150                                 if (tx_done = '1') then
151                                         state_next <= FETCH;
152                                         spalte_up <= '1';
153                                 end if;
154                         when DONE =>
155                                 state_next <= IDLE;
156                 end case;
157         end process next_state_pc;
158
159 end architecture beh;