89dff252be4bc9cd13891311d76123ad7465a19a
[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_busy : in std_logic; --signals if the history module actually grants our request.
28                 pc_done : in std_logic;
29                 pc_char : in hbyte
30         );
31 end entity pc_communication;
32
33 architecture beh of pc_communication is
34         signal spalte, spalte_next : integer range 1 to hspalte_max + 1;
35         signal zeile , zeile_next : integer range 1 to hzeile_max + 1;
36         signal get, get_next : std_logic;
37         signal new_i, new_i_next : std_logic;
38         signal tx_done_i, tx_done_i_next : std_logic;
39         signal tx_data_i, tx_data_i_next : std_logic_vector (7 downto 0);
40
41         type STATE_PC is (IDLE, WAIT_HIST, FETCH, FORWARD, WAIT_UART, UART_DONE);
42         signal state, state_next : STATE_PC ;
43
44 begin
45
46
47         pc_zeile <= hzeile(std_logic_vector(to_unsigned(zeile,7)));
48         pc_spalte <= hspalte(std_logic_vector(to_unsigned(spalte,7)));
49         pc_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                 elsif rising_edge(sys_clk) then
65                         spalte <= spalte_next;
66                         zeile <= zeile_next;
67                         state <= state_next;
68                         get <= get_next;
69                         new_i <= new_i_next;
70                         tx_done_i <= tx_done_i_next;
71                         tx_data_i <= tx_data_i_next;
72                 end if;
73         end process sync;
74
75         output_pc : process (state, zeile, spalte, tx_data_i, tx_done_i, pc_char)
76         begin
77                 get_next <= '0';
78                 new_i_next <= '0';
79
80                 spalte_next <= spalte;
81                 zeile_next <= zeile;
82                 tx_data_i_next <= tx_data_i;
83
84                 case state is
85                         when IDLE =>
86                                 null;
87                         when FETCH =>
88                                 get_next <= '1';
89                         when WAIT_HIST =>
90                         tx_data_i_next <= pc_char;
91                         when FORWARD =>
92                                 new_i_next <= '1';
93                         when WAIT_UART =>
94                                 null;
95                         when UART_DONE =>
96                                 if tx_data_i = x"00" or spalte = hspalte_max then
97                                         zeile_next <= zeile + 1;
98                                         spalte_next <= 1;
99                                         if zeile = hzeile_max then
100                                                 zeile_next <= 1;
101                                         end if;
102                                 else
103                                         spalte_next <= spalte + 1;
104                                 end if;
105                 end case;
106         end process output_pc;
107
108         next_state_pc : process (btn_a, pc_busy, pc_done, rx_new, rx_data, spalte, 
109                 state, tx_data_i ,tx_done_i, zeile)
110         begin
111                 state_next <= state;
112                 case state is
113                         when IDLE =>
114                                 if (rx_new = '1' and rx_data = x"0a" ) or btn_a = '0' then
115                                         state_next <= FETCH;
116                                 end if;
117                         when FETCH =>
118                                 if pc_busy = '1' then
119                                         state_next <= WAIT_HIST;
120                                 else
121                                         state_next <= FETCH;
122                                 end if;
123                         when WAIT_HIST =>
124                                 if (pc_done = '1') then
125                                         state_next <= FORWARD;
126                                 end if;
127                         when FORWARD =>
128                                 state_next <= WAIT_UART;
129                         when WAIT_UART =>
130                                 if (tx_done_i = '1') then
131                                         state_next <= UART_DONE;
132                                 end if;
133                         when UART_DONE =>
134                                 if (tx_data_i = x"00" or spalte = hspalte_max) and
135                                         zeile = hzeile_max then
136                                         state_next <= IDLE;
137                                 else
138                                         state_next <= FETCH;
139                                 end if;
140                 end case;
141         end process next_state_pc;
142
143 end architecture beh;