2 use IEEE.std_logic_1164.all;
3 use IEEE.numeric_std.all;
5 use work.common_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;
14 architecture behav of writeback_stage is
16 signal data_ram_read, data_ram_read_ext : word_t;
17 signal data_addr : word_t;
19 signal wb_reg, wb_reg_nxt : writeback_rec;
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;
24 --signal int_req : interrupt_t;
25 signal uart_int : std_logic;
28 signal sel_nxt, dmem_we, ext_anysel : std_logic;
30 signal calc_mem_res : gp_register_t;
34 ext_timer_out <= (others => '0'); --TODO: delete when timer is connected
35 ext_gpmp_out <= (others => '0'); --TODO: delete when gpm is connected
44 data_addr(DATA_ADDR_WIDTH+1 downto 2),
45 data_addr(DATA_ADDR_WIDTH+1 downto 2),
48 wb_reg_nxt.data, --ram_data,
94 interrupt : extension_interrupt
110 syn: process(clk, reset)
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';
119 wb_reg.byte_s <= '0';
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;
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
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);
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;
148 calc_mem_res <= result; --(others => '0');
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
155 -- case address(BYTEADDR-1 downto 0) is
158 byte_en(1 downto 0) := "11";
160 byte_en(3 downto 2) := "11";
161 wb_reg_nxt.data(31 downto 16) <= ram_data(15 downto 0);
164 elsif byte_s = '1' then
165 -- case address(BYTEADDR-1 downto 0) is
167 when "00" => byte_en(0) := '1';
170 wb_reg_nxt.data(15 downto 8) <= ram_data(7 downto 0);
173 wb_reg_nxt.data(23 downto 16) <= ram_data(7 downto 0);
176 wb_reg_nxt.data(31 downto 24) <= ram_data(7 downto 0);
180 byte_en := (others => '1');
183 wb_reg_nxt.byte_en <= byte_en;
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);
192 -- calc_mem_res(15 downto 0) <= data_ram_read(15 downto 0);
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;
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);
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;
216 -- if alu_jmp = '0' and br_pred = '1' and write_en = '0' then
220 -- if ((alu_jmp and wb_reg.dmem_en) = '1') then
221 -- jump_addr <= data_ram_read;
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
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;
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;
245 data_addr <= (others => '0');
248 if (wb_reg.address(DATA_ADDR_WIDTH+2) /= '1') then
249 data_out := data_ram_read;
251 reg_we_v := reg_we_v and ext_anysel;
252 data_out := data_ram_read_ext;
255 if wb_reg.byte_en(0) = '0' then
256 data_out(byte_t'range) := (others => '0');
258 if wb_reg.byte_en(1) = '0' then
259 data_out(2*byte_t'length-1 downto byte_t'length) := (others => '0');
261 if wb_reg.byte_en(2) = '0' then
262 data_out(3*byte_t'length-1 downto 2*byte_t'length) := (others => '0');
264 if wb_reg.byte_en(3) = '0' then
265 data_out(4*byte_t'length-1 downto 3*byte_t'length) := (others => '0');
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));
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));
278 -- data_out(hword_t'high downto (hword_t'length/2)) := (others => '0');
283 data_out := to_stdlogicvector(to_bitvector(data_out) srl to_integer(unsigned(wb_reg.address(BYTEADDR-1 downto 0)))*byte_t'length);
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;
290 regfile_val <= data_out;
292 if wb_reg.dmem_en = '0' then
293 regfile_val <= result;
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;
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);
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;
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;
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;
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;
352 -- wenn ich hier statt dem 4rer die konstante nehme dann gibts an fehler wegen nicht lokaler variable -.-
354 when EXT_UART_ADDR =>
355 ext_uart.sel <= enable;
356 ext_anysel <= enable;
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;
370 ext_imp.sel <= enable;
371 ext_anysel <= enable;
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;
386 ext_int.sel <= enable;
387 ext_anysel <= enable;
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;
401 when EXT_7SEG_ADDR =>
402 ext_7seg.sel <= enable;
403 ext_anysel <= enable;
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);
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;
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;
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;
445 -- hier kann man weiter extensions adden :) Konstanten sind im extension pkg definiert
446 when others => ext_anysel <= '0';
449 data_ram_read_ext <= ext_uart_out or ext_gpmp_out or ext_timer_out;