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