7b57d4565c89592569fb72f709353497718d054d
[calu.git] / cpu / src / writeback_stage_b.vhd
1 library IEEE;
2 use IEEE.std_logic_1164.all;
3 use IEEE.numeric_std.all;
4
5 use work.common_pkg.all;
6 use work.core_pkg.all;
7
8 use work.mem_pkg.all;
9 use work.extension_pkg.all;
10 use work.extension_uart_pkg.all;
11 use work.extension_7seg_pkg.all;
12 use work.extension_imp_pkg.all;
13
14 architecture behav of writeback_stage is
15
16 signal data_ram_read, data_ram_read_ext : word_t;
17 signal data_addr : word_t;
18
19 signal wb_reg, wb_reg_nxt : writeback_rec;
20
21 signal ext_uart,ext_timer,ext_gpmp,ext_7seg,ext_int,ext_imp :  extmod_rec;
22 signal ext_uart_out, ext_timer_out, ext_gpmp_out, ext_int_out,ext_imp_out : gp_register_t;
23
24 --signal int_req : interrupt_t;
25 signal uart_int : std_logic;
26
27
28 signal sel_nxt, dmem_we, ext_anysel : std_logic;
29
30 signal calc_mem_res : gp_register_t;
31
32 begin
33
34         ext_timer_out <= (others => '0'); --TODO: delete when timer is connected
35         ext_gpmp_out <= (others => '0'); --TODO: delete when gpm is connected
36
37         data_ram : r_w_ram_be
38                 generic map (
39                         DATA_ADDR_WIDTH
40                 )
41                 
42                 port map (
43                         clk,
44                         data_addr(DATA_ADDR_WIDTH+1 downto 2),
45                         data_addr(DATA_ADDR_WIDTH+1 downto 2),
46                         wb_reg_nxt.byte_en,
47                         dmem_we,
48                         wb_reg_nxt.data, --ram_data,
49                         data_ram_read
50                 );
51
52 uart : extension_uart 
53         generic map(
54                 RESET_VALUE
55                 )
56         port map(
57                         clk ,
58                         reset,
59                         ext_uart,
60                         ext_uart_out,
61                         uart_int,
62                         bus_rx,
63                         bus_tx
64                 );
65         
66 imp : extension_imp 
67         generic map(
68                 RESET_VALUE
69                 )
70         port map(
71                         clk ,
72                         reset,
73                         ext_imp,
74                         ext_imp_out,
75                         im_addr,
76                         im_data,
77                         new_im_data_out
78                 );
79         
80 sseg : extension_7seg
81         generic map(
82                 RESET_VALUE
83                 )
84         port map(
85                 clk,
86                 reset,
87                 ext_7seg,
88                 sseg0,
89                 sseg1,
90                 sseg2,
91                 sseg3
92                 );
93
94 interrupt : extension_interrupt
95         generic map(
96                 RESET_VALUE
97                 )
98         port map(
99                 clk,
100                 reset,
101                 ext_int,
102                 ext_int_out,
103
104                 uart_int,
105
106                 int_req
107
108                 );
109         
110 syn: process(clk, reset)
111
112 begin
113
114         if (reset = RESET_VALUE) then
115                 wb_reg.address <= (others => '0');
116                 wb_reg.dmem_en <= '0';
117                 wb_reg.dmem_write_en <= '0';
118                 wb_reg.hword <= '0';
119                 wb_reg.byte_s <= '0';
120                 
121                 wb_reg.byte_en <= (others => '0');
122                 wb_reg.data <= (others =>'0');
123         elsif rising_edge(clk) then
124                 wb_reg <= wb_reg_nxt;
125         end if;
126         
127 end process; 
128
129 --      type writeback_rec is record
130 --              address : in word_t;            --ureg 
131 --              dmem_en : in std_logic;         --ureg (jump addr in mem or in address)
132 --              dmem_write_en : in std_logic;   --ureg
133 --              hword_hl : in std_logic         --ureg
134 --      end record;
135
136
137
138 shift_input: process(data_ram_read, address, dmem_en, dmem_write_en, hword, wb_reg, result, byte_s, alu_jmp, br_pred, write_en, ram_data)
139 variable byte_en : byte_en_t;
140 variable address_val : std_logic_vector(1 downto 0);
141 begin
142         wb_reg_nxt.address <= address;
143         wb_reg_nxt.dmem_en <= dmem_en;
144         wb_reg_nxt.dmem_write_en <= dmem_write_en;
145         wb_reg_nxt.hword <= hword;
146         wb_reg_nxt.byte_s <= byte_s;
147
148         calc_mem_res <= result; --(others => '0');
149         
150         wb_reg_nxt.data <= ram_data;
151         byte_en := (others => '0');
152         address_val := address(BYTEADDR-1 downto 0);
153         if dmem_en = '1' then
154                 if hword = '1' then
155 --                      case address(BYTEADDR-1 downto 0) is
156                         case address_val is
157                         when "00" => 
158                                 byte_en(1 downto 0) := "11";
159                         when "10" => 
160                                 byte_en(3 downto 2) := "11";
161                                 wb_reg_nxt.data(31 downto 16) <= ram_data(15 downto 0);
162                         when others => null;
163                         end case;
164                 elsif byte_s = '1' then
165 --                      case address(BYTEADDR-1 downto 0) is
166                         case address_val is
167                         when "00" => byte_en(0) := '1';
168                         when "01" => 
169                                 byte_en(1) := '1';
170                                 wb_reg_nxt.data(15 downto 8) <= ram_data(7 downto 0);
171                         when "10" => 
172                                 byte_en(2) := '1';
173                                 wb_reg_nxt.data(23 downto 16) <= ram_data(7 downto 0);
174                         when "11" => 
175                                 byte_en(3) := '1';
176                                 wb_reg_nxt.data(31 downto 24) <= ram_data(7 downto 0);
177                         when others => null;
178                         end case;
179                 else
180                         byte_en := (others => '1');
181                 end if;
182         end if;
183         wb_reg_nxt.byte_en <= byte_en;
184         
185         -- if (wb_reg.dmem_en = '1' and wb_reg.dmem_write_en = '0') then        -- ram read operation --alu_jmp = '0' and 
186                 -- calc_mem_res <= data_ram_read;
187                 -- if (wb_reg.hword = '1') then
188                         -- calc_mem_res <= (others => '0');
189                         -- if (wb_reg.address(1) = '1') then
190                                 -- calc_mem_res(15 downto 0) <= data_ram_read(31 downto 16);
191                         -- else
192                                 -- calc_mem_res(15 downto 0) <= data_ram_read(15 downto 0);
193                         -- end if;
194                 -- end if;
195                 -- if (wb_reg.byte_s = '1') then
196                         -- calc_mem_res <= (others => '0');
197                         -- case wb_reg.address(1 downto 0) is
198                                 -- when "00" => calc_mem_res(7 downto 0) <= data_ram_read(7 downto 0);
199                                 -- when "01" => calc_mem_res(7 downto 0) <= data_ram_read(15 downto 8);
200                                 -- when "10" => calc_mem_res(7 downto 0) <= data_ram_read(23 downto 16);
201                                 -- when "11" => calc_mem_res(7 downto 0) <= data_ram_read(31 downto 24);
202                                 -- when others => null;
203                         -- end case;
204                 -- end if;      
205         -- end if;
206
207         --jump <= (alu_jmp xor br_pred) and (write_en or wb_reg.dmem_en);
208         jump <= (alu_jmp xor br_pred);-- and (write_en or wb_reg.dmem_en);
209
210         if (alu_jmp = '1' and wb_reg.dmem_en = '1' and wb_reg.dmem_write_en = '0' and write_en = '0') then
211                 jump_addr <= data_ram_read;
212         else
213                 jump_addr <= result;    
214         end if;
215
216 --      if alu_jmp = '0' and br_pred = '1' and write_en = '0' then
217 --              jump <= '1';
218 --      end if;
219
220 --      if ((alu_jmp and wb_reg.dmem_en) = '1') then
221 --              jump_addr <= data_ram_read;
222 --      end if; 
223
224 end process;
225
226 --                      result : in gp_register_t;      --reg  (alu result or jumpaddr)
227 --                      result_addr : in gp_addr_t;     --reg
228 --                      address : in word_t;            --ureg 
229 --                      alu_jmp : in std_logic;         --reg
230 --                      br_pred : in std_logic;         --reg
231 --                      write_en : in std_logic;        --reg  (register file)
232 --                      dmem_en : in std_logic;         --ureg (jump addr in mem or in result)
233 --                      dmem_write_en : in std_logic;   --ureg
234 --                      hword : in std_logic            --ureg
235
236
237
238 out_logic: process(write_en, result_addr, wb_reg, alu_jmp, wb_reg_nxt, data_ram_read_ext, calc_mem_res, data_ram_read, ext_anysel, result, hword, byte_s)
239 variable reg_we_v : std_logic;
240 variable data_out : gp_register_t;
241 begin
242     reg_we_v := (write_en or (wb_reg.dmem_en and not(wb_reg.dmem_write_en))) and not(alu_jmp);
243         reg_addr <= result_addr;
244
245         data_addr <= (others => '0');
246         dmem_we <= '0';
247         
248         if (wb_reg.address(DATA_ADDR_WIDTH+2) /= '1') then
249                 data_out := data_ram_read;
250         else
251                 reg_we_v := reg_we_v and ext_anysel;
252                 data_out := data_ram_read_ext;
253         end if;
254         
255         if wb_reg.byte_en(0) = '0' then
256                 data_out(byte_t'range) := (others => '0');
257         end if;
258         if wb_reg.byte_en(1) = '0' then
259                 data_out(2*byte_t'length-1 downto byte_t'length) := (others => '0');
260         end if;
261         if wb_reg.byte_en(2) = '0' then
262                 data_out(3*byte_t'length-1 downto 2*byte_t'length) := (others => '0');
263         end if;
264         if wb_reg.byte_en(3) = '0' then
265                 data_out(4*byte_t'length-1 downto 3*byte_t'length) := (others => '0');
266         end if;
267         
268         
269 --      if wb_reg.hword = '1' or wb_reg.byte_s = '1' then
270 --              if wb_reg.address(1)='1' then
271 --                      data_out(hword_t'range) := data_out(data_out'high downto (data_out'length/2));
272 --              end if;
273 --              data_out(data_out'high downto (data_out'length/2)) := (others => '0');
274 --              if byte_s = '1' then
275 --                      if wb_reg.address(0) = '1' then
276 --                              data_out(byte_t'range) := data_out(hword_t'high downto (hword_t'length/2));
277 --                      end if;
278 --                      data_out(hword_t'high downto (hword_t'length/2)) := (others => '0');
279 --              end if;
280 --      end if;
281         
282         
283         data_out := to_stdlogicvector(to_bitvector(data_out) srl to_integer(unsigned(wb_reg.address(BYTEADDR-1 downto 0)))*byte_t'length); 
284         
285         if (wb_reg_nxt.address(DATA_ADDR_WIDTH+2) /= '1') then
286                 data_addr(DATA_ADDR_WIDTH+1 downto 0) <= wb_reg_nxt.address(DATA_ADDR_WIDTH+1 downto 0);
287                 dmem_we <= wb_reg_nxt.dmem_write_en;
288         end if;
289         
290         regfile_val <= data_out;
291         
292         if wb_reg.dmem_en = '0' then
293                 regfile_val <= result;
294         end if;
295         
296         reg_we <= reg_we_v;
297         
298 end process;
299
300
301 addr_de_mult: process(wb_reg, wb_reg_nxt, ram_data, sel_nxt, ext_uart_out, ext_gpmp_out, ext_timer_out)
302 variable wr_en, enable  : std_logic; -- these are all registered
303 variable byte_en : byte_en_t; -- if a module needs the nxt signals it has to manually select them
304 variable addr : ext_addr_t;   -- for example the data memory, because it already has input registers
305 variable addrid : std_logic_vector(27 downto 0);--ext_addrid_t;
306 variable data : gp_register_t;
307 begin
308
309  --if selecting enable is too slow, see alu_b
310   enable := wb_reg.dmem_en;
311   wr_en  := wb_reg.dmem_write_en;
312   byte_en := wb_reg.byte_en;
313   addr := wb_reg.address(gp_register_t'high downto BYTEADDR);
314   addrid := wb_reg.address(gp_register_t'high downto EXTWORDS);
315   data := wb_reg.data;
316
317   ext_uart.sel <='0';
318   ext_7seg.sel <='0';
319   ext_timer.sel <='0';
320   ext_gpmp.sel <='0';
321   ext_int.sel <= '0';
322   ext_imp.sel <= '0';
323   
324   ext_uart.wr_en <= wr_en;
325   ext_7seg.wr_en <= wr_en;
326   ext_timer.wr_en <= wr_en;
327   ext_gpmp.wr_en <= wr_en;
328   ext_int.wr_en <= wr_en;  
329   ext_imp.wr_en <= wr_en;  
330
331   ext_uart.byte_en <= byte_en;
332   ext_7seg.byte_en <= byte_en;
333   ext_timer.byte_en <= byte_en;
334   ext_gpmp.byte_en <= byte_en;
335   ext_int.byte_en <= byte_en;  
336   ext_imp.byte_en <= byte_en; 
337
338   ext_uart.addr <= addr;
339   ext_7seg.addr <= addr;
340   ext_timer.addr <= addr;
341   ext_gpmp.addr <= addr;
342   ext_int.addr <= addr;
343   ext_imp.addr <= addr;
344
345   ext_uart.data <= data;
346   ext_7seg.data <= data;
347   ext_timer.data <= data;
348   ext_gpmp.data <= data;
349   ext_int.data <= data;
350   ext_imp.data <= data;
351
352    -- wenn ich hier statt dem 4rer die konstante nehme dann gibts an fehler wegen nicht lokaler variable -.-
353  case addrid is
354     when EXT_UART_ADDR => 
355         ext_uart.sel <= enable;
356         ext_anysel <= enable;
357
358 --              ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
359 --              ext_uart.data <= ram_data;
360 --              ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
361 --              case wb_reg_nxt.address(1 downto 0) is
362 --                              when "00" => ext_uart.byte_en <= "0001";
363 --                              when "01" => ext_uart.byte_en <= "0010";
364 --                              when "10" => ext_uart.byte_en <= "0100";
365 --                              --when "11" => ext_uart.byte_en <= "1000";
366 --                              when "11" => ext_uart.byte_en <= "1111";
367 --                              when others => null;
368 --                      end case;
369     when EXT_IMP_ADDR => 
370         ext_imp.sel <= enable;
371         ext_anysel <= enable;
372
373 --              ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
374 --              ext_uart.data <= ram_data;
375 --              ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
376 --              case wb_reg_nxt.address(1 downto 0) is
377 --                              when "00" => ext_uart.byte_en <= "0001";
378 --                              when "01" => ext_uart.byte_en <= "0010";
379 --                              when "10" => ext_uart.byte_en <= "0100";
380 --                              --when "11" => ext_uart.byte_en <= "1000";
381 --                              when "11" => ext_uart.byte_en <= "1111";
382 --                              when others => null;
383 --                      end case;
384
385     when EXT_INT_ADDR => 
386         ext_int.sel <= enable;
387         ext_anysel <= enable;
388
389 --              ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
390 --              ext_uart.data <= ram_data;
391 --              ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
392 --              case wb_reg_nxt.address(1 downto 0) is
393 --                              when "00" => ext_uart.byte_en <= "0001";
394 --                              when "01" => ext_uart.byte_en <= "0010";
395 --                              when "10" => ext_uart.byte_en <= "0100";
396 --                              --when "11" => ext_uart.byte_en <= "1000";
397 --                              when "11" => ext_uart.byte_en <= "1111";
398 --                              when others => null;
399 --                      end case;
400
401         when EXT_7SEG_ADDR => 
402                 ext_7seg.sel <= enable;
403                 ext_anysel <= enable;
404
405                 -- ext_7seg.wr_en <= wb_regdmem_write_en;
406                 -- ext_7seg.data <= ram_data;
407                 -- ext_7seg.addr <= wb_reg_nxt.address(31 downto 2);
408                 -- ext_7seg.byte_en(1 downto 0) <= wb_reg_nxt.address(1 downto 0);
409
410                 
411 --              case wb_reg_nxt.address(1 downto 0) is
412 --                      when "00" => ext_7seg.byte_en <= "0001";
413 --                      when "01" => ext_7seg.byte_en <= "0010";
414 --                      when "10" => ext_7seg.byte_en <= "0100";
415 --                      when "11" => ext_7seg.byte_en <= "1000";
416 --                      when others => null;
417 --              end case;
418                         
419         when EXT_TIMER_ADDR => 
420                 ext_timer.sel <= enable;
421                 ext_anysel <= enable;
422                 -- ext_timer.wr_en <= wb_reg_nxt.dmem_write_en;
423                 -- ext_timer.data <= ram_data;
424                 -- ext_timer.addr <= wb_reg_nxt.address(wb_reg_nxt.address'high downto BYTEADDR);
425                 -- case wb_reg.address(1 downto 0) is
426                                 -- when "00" => ext_timer.byte_en <= "0001";
427                                 -- when "01" => ext_timer.byte_en <= "0010";
428                                 -- when "10" => ext_timer.byte_en <= "0100";
429                                 -- when "11" => ext_timer.byte_en <= "1000";
430                                 -- when others => null;
431                         -- end case;
432         when EXT_GPMP_ADDR => 
433                 ext_gpmp.sel <= enable;
434                 ext_anysel <= enable;
435                 -- ext_gpmp.wr_en <= wb_reg_nxt.dmem_write_en;
436                 -- ext_gpmp.data <= ram_data;
437                 -- ext_gpmp.addr <= wb_reg_nxt.address(wb_reg_nxt.address'high downto BYTEADDR);
438                 -- case wb_reg.address(1 downto 0) is
439                                 -- when "00" => ext_gpmp.byte_en <= "0001";
440                                 -- when "01" => ext_gpmp.byte_en <= "0010";
441                                 -- when "10" => ext_gpmp.byte_en <= "0100";
442                                 -- when "11" => ext_gpmp.byte_en <= "1000";
443                                 -- when others => null;
444                         -- end case;
445         -- hier kann man weiter extensions adden :) Konstanten sind im extension pkg definiert 
446         when others => ext_anysel <= '0';
447         end case;
448         
449         data_ram_read_ext <= ext_uart_out or ext_gpmp_out or ext_timer_out;
450 end process;
451
452 end behav;
453