9a65e590e4071023efebb1f6e80ccb5f25b78aab
[hwmod.git] / src / history.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 history is
7         port (
8                 sys_clk : in std_logic;
9                 sys_res_n : in std_logic;
10                 -- PC-komm
11                 -- TODO: pins
12                 -- Scanner
13                 s_char : in hbyte;
14                 s_take : in std_logic;
15                 s_done : out std_logic;
16                 s_backspace : in std_logic;
17                 -- Display
18                 d_new_eingabe : out std_logic;
19                 d_new_result : out std_logic;
20                 d_new_bs : out std_logic;
21                 d_zeile : in hzeile;
22                 d_spalte : in hspalte;
23                 d_get : in std_logic;
24                 d_done : out std_logic;
25                 d_char : out hbyte;
26                 -- Parser
27                 p_rget : in std_logic;
28                 p_rdone : out std_logic;
29                 p_read : out hbyte;
30                 p_wtake : in std_logic;
31                 p_wdone : out std_logic;
32                 p_write : in hbyte;
33                 p_finished : in std_logic
34         );
35 end entity history;
36
37 architecture beh of history is
38         type HISTORY_STATE is (SIDLE, S_S_INIT, S_S_WRITE, S_S_BS, S_S_DONE, S_S_FIN,
39                 S_D_INIT, S_D_READ, S_S_FIN_POSUP, S_P_READ, S_P_READ_DONE, S_P_WRITE,
40                 S_P_WRITE_DONE, S_P_DONE, S_INIT, S_S_CLEAR_NEXT0, S_S_CLEAR_NEXT1);
41         signal state_int, state_next : HISTORY_STATE;
42         signal was_bs_int, was_bs_next : std_logic;
43         signal pos_int, pos_next : std_logic_vector(H_RAM_WIDTH - 1 downto 0);
44         signal s_done_int, s_done_next : std_logic;
45         signal s_cnt_int, s_cnt_next : hspalte;
46         signal d_new_eingabe_int, d_new_eingabe_next : std_logic;
47         signal d_new_result_int, d_new_result_next : std_logic;
48         signal d_new_bs_int, d_new_bs_next: std_logic;
49         signal d_done_int, d_done_next : std_logic;
50         signal d_char_int, d_char_next : hbyte;
51         signal p_rdone_int, p_rdone_next : std_logic;
52         signal p_wdone_int, p_wdone_next : std_logic;
53         signal p_read_int, p_read_next : hbyte;
54         signal p_sp_read_int, p_sp_read_next : hspalte;
55         signal p_sp_write_int, p_sp_write_next : hspalte;
56
57         -- ram
58         signal address_next, address_int : std_logic_vector(H_RAM_WIDTH - 1 downto 0);
59         signal data_out, data_in_next, data_in_int : hbyte;
60         signal wr_next, wr_int : std_logic;
61 begin
62         s_done <= s_done_int;
63         d_new_eingabe <= d_new_eingabe_int;
64         d_new_result <= d_new_result_int;
65         d_new_bs <= d_new_bs_int;
66         d_done <= d_done_int;
67         d_char <= d_char_int;
68         p_rdone <= p_rdone_int;
69         p_wdone <= p_wdone_int;
70         p_read <= p_read_int;
71
72         process(sys_clk, sys_res_n)
73         begin
74                 if sys_res_n = '0' then
75                         -- internal
76                         state_int <= S_INIT;
77                         was_bs_int <= '0';
78                         pos_int <= (others => '0');
79                         -- out
80                         s_done_int <= '0';
81                         s_cnt_int <= (0 => '1', others => '0');
82                         d_new_result_int <= '0';
83                         d_new_eingabe_int <= '0';
84                         d_new_bs_int <= '0';
85                         d_done_int <= '0';
86                         d_char_int <= (others => '0');
87                         p_rdone_int <= '0';
88                         p_wdone_int <= '0';
89                         p_read_int <= (others => '0');
90                         p_sp_read_int <= (others => '0');
91                         p_sp_write_int <= std_logic_vector(to_unsigned(71,p_sp_write_int'length));
92
93                         address_int <= (0 => '1', others => '0');
94                         data_in_int <= x"00";
95                         wr_int <= '0';
96                 elsif rising_edge(sys_clk) then
97                         -- internal
98                         state_int <= state_next;
99                         was_bs_int <= was_bs_next;
100                         pos_int <= pos_next;
101                         -- out
102                         s_done_int <= s_done_next;
103                         s_cnt_int <= s_cnt_next;
104                         d_new_result_int <= d_new_result_next;
105                         d_new_eingabe_int <= d_new_eingabe_next;
106                         d_new_bs_int <= d_new_bs_next;
107                         d_done_int <= d_done_next;
108                         d_char_int <= d_char_next;
109                         p_rdone_int <= p_rdone_next;
110                         p_wdone_int <= p_wdone_next;
111                         p_read_int <= p_read_next;
112                         p_sp_read_int <= p_sp_read_next;
113                         p_sp_write_int <= p_sp_write_next;
114
115                         address_int <= address_next;
116                         data_in_int <= data_in_next;
117                         wr_int <= wr_next;
118                 end if;
119         end process;
120
121         -- next state
122         process(state_int, d_get, p_finished, s_take, s_backspace, was_bs_int,
123                 p_rget, p_wtake, pos_int, s_cnt_int)
124         begin
125                 state_next <= state_int;
126
127                 case state_int is
128                         when S_INIT =>
129                                 -- ganzen speicher clearen: fuer ausgabe am vga nicht umbedingt
130                                 -- noetig, aber spaetestens fuers dumpen per rs232
131                                 if pos_int = std_logic_vector(to_unsigned(H_RAM_SIZE,H_RAM_WIDTH)) then
132                                         state_next <= SIDLE;
133                                 end if;
134                         when SIDLE =>
135                                 -- S_S_FIN: tmp..
136                                 if s_take = '1' then
137                                         state_next <= S_S_INIT;
138                                 elsif p_rget = '1' then
139                                         state_next <= S_P_READ;
140                                 elsif p_wtake = '1' then
141                                         state_next <= S_P_WRITE;
142                                 elsif p_finished = '1' then
143                                         state_next <= S_S_FIN;
144                                 elsif d_get = '1' then
145                                         state_next <= S_D_INIT;
146                                 end if;
147                         when S_S_INIT =>
148                                 if s_backspace = '1' then
149                                         state_next <= S_S_BS;
150                                 else
151                                         state_next <= S_S_WRITE;
152                                 end if;
153                         when S_S_WRITE =>
154                                 state_next <= S_S_DONE;
155                         when S_S_BS =>
156                                 state_next <= S_S_DONE;
157                         when S_S_FIN =>
158                                 if p_finished = '0' then
159                                         state_next <= S_S_FIN_POSUP;
160                                 end if;
161                         when S_S_FIN_POSUP =>
162                                 state_next <= S_S_CLEAR_NEXT0;
163                         when S_S_CLEAR_NEXT0 =>
164                                 if s_cnt_int = hspalte(to_unsigned(71,hspalte'length)) then
165                                         state_next <= S_S_CLEAR_NEXT1;
166                                 end if;
167                         when S_S_CLEAR_NEXT1 =>
168                                 if s_cnt_int = hspalte(to_unsigned(71,hspalte'length)) then
169                                         state_next <= SIDLE;
170                                 end if;
171                         when S_S_DONE =>
172                                 if s_take = '0' then
173                                         state_next <= SIDLE;
174                                 end if;
175
176                         when S_D_INIT =>
177                                 state_next <= S_D_READ;
178                         when S_D_READ =>
179                                 if d_get = '0' then
180                                         state_next <= SIDLE;
181                                 end if;
182
183                         when S_P_READ =>
184                                 state_next <= S_P_READ_DONE;
185                         when S_P_READ_DONE =>
186                                 if p_rget = '0' then
187                                         state_next <= S_P_DONE;
188                                 end if;
189                         when S_P_WRITE =>
190                                 state_next <= S_P_WRITE_DONE;
191                         when S_P_WRITE_DONE =>
192                                 if p_wtake = '0' then
193                                         state_next <= S_P_DONE;
194                                 end if;
195                         when S_P_DONE =>
196                                 state_next <= SIDLE;
197                 end case;
198         end process;
199
200         -- out
201         process(state_int, s_cnt_int, d_spalte, d_zeile, data_out, s_char, address_int,
202                         data_in_int, d_new_result_int, d_new_eingabe_int, d_new_bs_int,
203                         was_bs_int, s_take, pos_int, p_rdone_int, p_wdone_int, p_read_int,
204                         p_write, p_sp_read_int, p_sp_write_int)
205                 variable addr_tmp : std_logic_vector(H_RAM_WIDTH - 1 downto 0);
206                 variable spalte_tmp : hspalte;
207                 variable mul_tmp : std_logic_vector((H_RAM_WIDTH*2) -1 downto 0);
208         begin
209                 s_done_next <= '0';
210                 s_cnt_next <= s_cnt_int;
211                 was_bs_next <= was_bs_int;
212                 pos_next <= pos_int;
213                 d_new_result_next <= d_new_result_int;
214                 d_new_eingabe_next <= d_new_eingabe_int;
215                 d_new_bs_next <= '0';
216                 d_done_next <= '0';
217                 d_char_next <= (others => '0');
218                 wr_next <= '0';
219                 address_next <= address_int;
220                 data_in_next <= data_in_int;
221                 p_rdone_next <= p_rdone_int;
222                 p_wdone_next <= p_wdone_int;
223                 p_read_next <= p_read_int;
224                 p_sp_read_next <= p_sp_read_int;
225                 p_sp_write_next <= p_sp_write_int;
226
227                 case state_int is
228                         when S_INIT =>
229                                 wr_next <= '1';
230                                 address_next <= pos_int;
231                                 data_in_next <= (others => '0');
232                                 if pos_int = std_logic_vector(to_unsigned(H_RAM_SIZE,H_RAM_WIDTH)) then
233                                         pos_next <= (others => '0');
234                                 else
235                                         pos_next <= std_logic_vector(unsigned(pos_int) + to_unsigned(1,H_RAM_WIDTH));
236                                 end if;
237                         when SIDLE =>
238                                 -- TODO: tmp fix
239                                 d_new_result_next <= '0';
240                         when S_S_INIT =>
241                                 null;
242                         when S_S_WRITE =>
243                                 -- nur bei < 71 weiter machen
244                                 -- TODO: '/=' billiger als '<' ?
245                                 if unsigned(s_cnt_int) /= 71 then
246                                         wr_next <= '1';
247                                         address_next <= std_logic_vector(unsigned(pos_int) + unsigned(s_cnt_int));
248                                         data_in_next <= s_char;
249                                         s_cnt_next <= std_logic_vector(unsigned(s_cnt_int) + 1);
250                                 else
251                                         -- was_bs hier missbrauchen, um ein d_new_eingabe zu verhindern
252                                         was_bs_next <= '1';
253                                 end if;
254                         when S_S_BS =>
255                                 -- ab 1 darf nicht mehr dekrementiert werden
256                                 addr_tmp := (others => '0');
257                                 if unsigned(s_cnt_int) /= 1 then
258                                         addr_tmp(hspalte'length - 1 downto 0) := std_logic_vector(unsigned(s_cnt_int) - 1);
259                                         d_new_bs_next <= '1';
260                                 else
261                                         addr_tmp(hspalte'length - 1 downto 0) := s_cnt_int;
262                                 end if;
263                                 s_cnt_next <= addr_tmp(hspalte'length - 1 downto 0);
264                 
265                                 wr_next <= '1';
266                                 address_next <= std_logic_vector(unsigned(pos_int) + unsigned(addr_tmp));
267                                 data_in_next <= (others => '0');
268                                 was_bs_next <= '1';
269                         when S_S_FIN =>
270                                 s_cnt_next <= (0 => '1', others => '0');
271                                 d_new_result_next <= '1';
272                                 -- resetten der parser counter
273                                 p_sp_read_next <= (others => '0');
274                                 p_sp_write_next <= std_logic_vector(to_unsigned(71,p_sp_write_next'length));
275                         when S_S_FIN_POSUP =>
276                                 -- overflowcheck nach 50 berechnungen => wieder von vorne anfangen
277                                 if pos_int = std_logic_vector(to_unsigned(H_RAM_SIZE-142,H_RAM_WIDTH)) then
278                                         pos_next <= (others => '0');
279                                 else
280                                         pos_next <= std_logic_vector(unsigned(pos_int) + to_unsigned(142,H_RAM_WIDTH));
281                                 end if;
282                         when S_S_CLEAR_NEXT0 =>
283                                 -- die naechsten 142 bytes im speicher resetten
284                                 wr_next <= '1';
285                                 address_next <= std_logic_vector(unsigned(pos_int) + unsigned(s_cnt_int));
286                                 data_in_next <= (others => '0');
287                                 if s_cnt_int = hspalte(to_unsigned(71,hspalte'length)) then
288                                         s_cnt_next <= (0 => '1', others => '0');
289                                 else
290                                         s_cnt_next <= std_logic_vector(unsigned(s_cnt_int) + 1);
291                                 end if;
292                         when S_S_CLEAR_NEXT1 =>
293                                 -- die naechsten 142 bytes im speicher resetten
294                                 wr_next <= '1';
295                                 address_next <= std_logic_vector(unsigned(pos_int) + to_unsigned(71,H_RAM_WIDTH) + unsigned(s_cnt_int));
296                                 data_in_next <= (others => '0');
297                                 if s_cnt_int = hspalte(to_unsigned(71,hspalte'length)) then
298                                         s_cnt_next <= (0 => '1', others => '0');
299                                 else
300                                         s_cnt_next <= std_logic_vector(unsigned(s_cnt_int) + 1);
301                                 end if;
302                         when S_S_DONE =>
303                                 s_done_next <= '1';
304                                 if was_bs_int = '0' then
305                                         d_new_eingabe_next <= '1';
306                                 end if;
307                                 if s_take = '0' then
308                                         was_bs_next <= '0';
309                                 end if;
310
311                         when S_D_INIT =>
312                                 addr_tmp := (others => '0');
313                                 addr_tmp(hzeile'length - 1 downto 0) := d_zeile;
314                                 mul_tmp := std_logic_vector(unsigned(addr_tmp) * to_unsigned(71,H_RAM_WIDTH));
315                                 addr_tmp := mul_tmp((addr_tmp'length - 1) downto 0);
316                                 addr_tmp := std_logic_vector(unsigned(addr_tmp) + unsigned(d_spalte));
317                                 address_next <= addr_tmp;
318                                 d_new_eingabe_next <= '0';
319                                 d_new_result_next <= '0';
320                         when S_D_READ =>
321                                 d_char_next <= data_out;
322                                 d_done_next <= '1';
323
324                         when S_P_READ =>
325                                 wr_next <= '0';
326                                 spalte_tmp := std_logic_vector(unsigned(p_sp_read_int) + 1);
327                                 p_sp_read_next <= spalte_tmp;
328                                 address_next <= std_logic_vector(unsigned(pos_int) + unsigned(spalte_tmp));
329                         when S_P_READ_DONE =>
330                                 p_rdone_next <= '1';
331                                 p_read_next <= data_out;
332
333                         when S_P_WRITE =>
334                                 wr_next <= '1';
335                                 data_in_next <= p_write;
336                                 spalte_tmp := std_logic_vector(unsigned(p_sp_write_int) - 1);
337                                 p_sp_write_next <= spalte_tmp;
338                                 address_next <= std_logic_vector(unsigned(pos_int) + to_unsigned(71,H_RAM_WIDTH) + unsigned(spalte_tmp));
339                         when S_P_WRITE_DONE =>
340                                 p_wdone_next <= '1';
341                         when S_P_DONE =>
342                                 p_rdone_next <= '0';
343                                 p_wdone_next <= '0';
344                 end case;
345         end process;
346
347         sp_ram_inst : entity work.sp_ram(beh)
348         generic map (
349                 ADDR_WIDTH => H_RAM_WIDTH
350         )
351         port map (
352                 sys_clk => sys_clk,
353                 address => address_int,
354                 data_out => data_out,
355                 wr => wr_int,
356                 data_in => data_in_int
357         );
358 end architecture beh;