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