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
37 spartan3e: if FPGATYPE = "s3e" generate
44 data_addr(DATA_ADDR_WIDTH+1 downto 2),
47 wb_reg_nxt.data, --ram_data,
51 -- else generate gibt es erst mit vhdl 2008 ...
52 altera: if FPGATYPE /= "s3e" generate
60 data_addr(DATA_ADDR_WIDTH+1 downto 2),
61 data_addr(DATA_ADDR_WIDTH+1 downto 2),
64 wb_reg_nxt.data, --ram_data,
97 altera_7seg: if FPGATYPE /= "s3e" generate
113 interrupt : extension_interrupt
129 syn: process(clk, reset)
133 if (reset = RESET_VALUE) then
134 wb_reg.address <= (others => '0');
135 wb_reg.dmem_en <= '0';
136 wb_reg.dmem_write_en <= '0';
138 wb_reg.byte_s <= '0';
140 wb_reg.byte_en <= (others => '0');
141 wb_reg.data <= (others =>'0');
142 elsif rising_edge(clk) then
143 wb_reg <= wb_reg_nxt;
148 -- type writeback_rec is record
149 -- address : in word_t; --ureg
150 -- dmem_en : in std_logic; --ureg (jump addr in mem or in address)
151 -- dmem_write_en : in std_logic; --ureg
152 -- hword_hl : in std_logic --ureg
157 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)
158 variable byte_en : byte_en_t;
159 variable address_val : std_logic_vector(1 downto 0);
161 wb_reg_nxt.address <= address;
162 wb_reg_nxt.dmem_en <= dmem_en;
163 wb_reg_nxt.dmem_write_en <= dmem_write_en;
164 wb_reg_nxt.hword <= hword;
165 wb_reg_nxt.byte_s <= byte_s;
167 calc_mem_res <= result; --(others => '0');
169 wb_reg_nxt.data <= ram_data;
170 byte_en := (others => '0');
171 address_val := address(BYTEADDR-1 downto 0);
172 if dmem_en = '1' then
174 -- case address(BYTEADDR-1 downto 0) is
177 byte_en(1 downto 0) := "11";
179 byte_en(3 downto 2) := "11";
180 wb_reg_nxt.data(31 downto 16) <= ram_data(15 downto 0);
183 elsif byte_s = '1' then
184 -- case address(BYTEADDR-1 downto 0) is
186 when "00" => byte_en(0) := '1';
189 wb_reg_nxt.data(15 downto 8) <= ram_data(7 downto 0);
192 wb_reg_nxt.data(23 downto 16) <= ram_data(7 downto 0);
195 wb_reg_nxt.data(31 downto 24) <= ram_data(7 downto 0);
199 byte_en := (others => '1');
202 wb_reg_nxt.byte_en <= byte_en;
204 -- if (wb_reg.dmem_en = '1' and wb_reg.dmem_write_en = '0') then -- ram read operation --alu_jmp = '0' and
205 -- calc_mem_res <= data_ram_read;
206 -- if (wb_reg.hword = '1') then
207 -- calc_mem_res <= (others => '0');
208 -- if (wb_reg.address(1) = '1') then
209 -- calc_mem_res(15 downto 0) <= data_ram_read(31 downto 16);
211 -- calc_mem_res(15 downto 0) <= data_ram_read(15 downto 0);
214 -- if (wb_reg.byte_s = '1') then
215 -- calc_mem_res <= (others => '0');
216 -- case wb_reg.address(1 downto 0) is
217 -- when "00" => calc_mem_res(7 downto 0) <= data_ram_read(7 downto 0);
218 -- when "01" => calc_mem_res(7 downto 0) <= data_ram_read(15 downto 8);
219 -- when "10" => calc_mem_res(7 downto 0) <= data_ram_read(23 downto 16);
220 -- when "11" => calc_mem_res(7 downto 0) <= data_ram_read(31 downto 24);
221 -- when others => null;
226 --jump <= (alu_jmp xor br_pred) and (write_en or wb_reg.dmem_en);
227 jump <= (alu_jmp xor br_pred);-- and (write_en or wb_reg.dmem_en);
229 if (alu_jmp = '1' and wb_reg.dmem_en = '1' and wb_reg.dmem_write_en = '0' and write_en = '0') then
230 jump_addr <= data_ram_read;
235 -- if alu_jmp = '0' and br_pred = '1' and write_en = '0' then
239 -- if ((alu_jmp and wb_reg.dmem_en) = '1') then
240 -- jump_addr <= data_ram_read;
245 -- result : in gp_register_t; --reg (alu result or jumpaddr)
246 -- result_addr : in gp_addr_t; --reg
247 -- address : in word_t; --ureg
248 -- alu_jmp : in std_logic; --reg
249 -- br_pred : in std_logic; --reg
250 -- write_en : in std_logic; --reg (register file)
251 -- dmem_en : in std_logic; --ureg (jump addr in mem or in result)
252 -- dmem_write_en : in std_logic; --ureg
253 -- hword : in std_logic --ureg
257 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)
258 variable reg_we_v : std_logic;
259 variable data_out : gp_register_t;
261 reg_we_v := (write_en or (wb_reg.dmem_en and not(wb_reg.dmem_write_en))) and not(alu_jmp);
262 reg_addr <= result_addr;
264 data_addr <= (others => '0');
267 if (wb_reg.address(DATA_ADDR_WIDTH+2) /= '1') then
268 data_out := data_ram_read;
270 reg_we_v := reg_we_v and ext_anysel;
271 data_out := data_ram_read_ext;
274 if wb_reg.byte_en(0) = '0' then
275 data_out(byte_t'range) := (others => '0');
277 if wb_reg.byte_en(1) = '0' then
278 data_out(2*byte_t'length-1 downto byte_t'length) := (others => '0');
280 if wb_reg.byte_en(2) = '0' then
281 data_out(3*byte_t'length-1 downto 2*byte_t'length) := (others => '0');
283 if wb_reg.byte_en(3) = '0' then
284 data_out(4*byte_t'length-1 downto 3*byte_t'length) := (others => '0');
288 -- if wb_reg.hword = '1' or wb_reg.byte_s = '1' then
289 -- if wb_reg.address(1)='1' then
290 -- data_out(hword_t'range) := data_out(data_out'high downto (data_out'length/2));
292 -- data_out(data_out'high downto (data_out'length/2)) := (others => '0');
293 -- if byte_s = '1' then
294 -- if wb_reg.address(0) = '1' then
295 -- data_out(byte_t'range) := data_out(hword_t'high downto (hword_t'length/2));
297 -- data_out(hword_t'high downto (hword_t'length/2)) := (others => '0');
302 data_out := to_stdlogicvector(to_bitvector(data_out) srl to_integer(unsigned(wb_reg.address(BYTEADDR-1 downto 0)))*byte_t'length);
304 if (wb_reg_nxt.address(DATA_ADDR_WIDTH+2) /= '1') then
305 data_addr(DATA_ADDR_WIDTH+1 downto 0) <= wb_reg_nxt.address(DATA_ADDR_WIDTH+1 downto 0);
306 dmem_we <= wb_reg_nxt.dmem_write_en;
309 regfile_val <= data_out;
311 if wb_reg.dmem_en = '0' then
312 regfile_val <= result;
320 addr_de_mult: process(wb_reg, wb_reg_nxt, ram_data, sel_nxt, ext_uart_out, ext_gpmp_out, ext_timer_out)
321 variable wr_en, enable : std_logic; -- these are all registered
322 variable byte_en : byte_en_t; -- if a module needs the nxt signals it has to manually select them
323 variable addr : ext_addr_t; -- for example the data memory, because it already has input registers
324 variable addrid : std_logic_vector(27 downto 0);--ext_addrid_t;
325 variable data : gp_register_t;
328 --if selecting enable is too slow, see alu_b
329 enable := wb_reg.dmem_en;
330 wr_en := wb_reg.dmem_write_en;
331 byte_en := wb_reg.byte_en;
332 addr := wb_reg.address(gp_register_t'high downto BYTEADDR);
333 addrid := wb_reg.address(gp_register_t'high downto EXTWORDS);
343 ext_uart.wr_en <= wr_en;
344 ext_7seg.wr_en <= wr_en;
345 ext_timer.wr_en <= wr_en;
346 ext_gpmp.wr_en <= wr_en;
347 ext_int.wr_en <= wr_en;
348 ext_imp.wr_en <= wr_en;
350 ext_uart.byte_en <= byte_en;
351 ext_7seg.byte_en <= byte_en;
352 ext_timer.byte_en <= byte_en;
353 ext_gpmp.byte_en <= byte_en;
354 ext_int.byte_en <= byte_en;
355 ext_imp.byte_en <= byte_en;
357 ext_uart.addr <= addr;
358 ext_7seg.addr <= addr;
359 ext_timer.addr <= addr;
360 ext_gpmp.addr <= addr;
361 ext_int.addr <= addr;
362 ext_imp.addr <= addr;
364 ext_uart.data <= data;
365 ext_7seg.data <= data;
366 ext_timer.data <= data;
367 ext_gpmp.data <= data;
368 ext_int.data <= data;
369 ext_imp.data <= data;
371 -- wenn ich hier statt dem 4rer die konstante nehme dann gibts an fehler wegen nicht lokaler variable -.-
373 when EXT_UART_ADDR =>
374 ext_uart.sel <= enable;
375 ext_anysel <= enable;
377 -- ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
378 -- ext_uart.data <= ram_data;
379 -- ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
380 -- case wb_reg_nxt.address(1 downto 0) is
381 -- when "00" => ext_uart.byte_en <= "0001";
382 -- when "01" => ext_uart.byte_en <= "0010";
383 -- when "10" => ext_uart.byte_en <= "0100";
384 -- --when "11" => ext_uart.byte_en <= "1000";
385 -- when "11" => ext_uart.byte_en <= "1111";
386 -- when others => null;
389 ext_imp.sel <= enable;
390 ext_anysel <= enable;
392 -- ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
393 -- ext_uart.data <= ram_data;
394 -- ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
395 -- case wb_reg_nxt.address(1 downto 0) is
396 -- when "00" => ext_uart.byte_en <= "0001";
397 -- when "01" => ext_uart.byte_en <= "0010";
398 -- when "10" => ext_uart.byte_en <= "0100";
399 -- --when "11" => ext_uart.byte_en <= "1000";
400 -- when "11" => ext_uart.byte_en <= "1111";
401 -- when others => null;
405 ext_int.sel <= enable;
406 ext_anysel <= enable;
408 -- ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
409 -- ext_uart.data <= ram_data;
410 -- ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
411 -- case wb_reg_nxt.address(1 downto 0) is
412 -- when "00" => ext_uart.byte_en <= "0001";
413 -- when "01" => ext_uart.byte_en <= "0010";
414 -- when "10" => ext_uart.byte_en <= "0100";
415 -- --when "11" => ext_uart.byte_en <= "1000";
416 -- when "11" => ext_uart.byte_en <= "1111";
417 -- when others => null;
420 when EXT_7SEG_ADDR =>
421 ext_7seg.sel <= enable;
422 ext_anysel <= enable;
424 -- ext_7seg.wr_en <= wb_regdmem_write_en;
425 -- ext_7seg.data <= ram_data;
426 -- ext_7seg.addr <= wb_reg_nxt.address(31 downto 2);
427 -- ext_7seg.byte_en(1 downto 0) <= wb_reg_nxt.address(1 downto 0);
430 -- case wb_reg_nxt.address(1 downto 0) is
431 -- when "00" => ext_7seg.byte_en <= "0001";
432 -- when "01" => ext_7seg.byte_en <= "0010";
433 -- when "10" => ext_7seg.byte_en <= "0100";
434 -- when "11" => ext_7seg.byte_en <= "1000";
435 -- when others => null;
438 when EXT_TIMER_ADDR =>
439 ext_timer.sel <= enable;
440 ext_anysel <= enable;
441 -- ext_timer.wr_en <= wb_reg_nxt.dmem_write_en;
442 -- ext_timer.data <= ram_data;
443 -- ext_timer.addr <= wb_reg_nxt.address(wb_reg_nxt.address'high downto BYTEADDR);
444 -- case wb_reg.address(1 downto 0) is
445 -- when "00" => ext_timer.byte_en <= "0001";
446 -- when "01" => ext_timer.byte_en <= "0010";
447 -- when "10" => ext_timer.byte_en <= "0100";
448 -- when "11" => ext_timer.byte_en <= "1000";
449 -- when others => null;
451 when EXT_GPMP_ADDR =>
452 ext_gpmp.sel <= enable;
453 ext_anysel <= enable;
454 -- ext_gpmp.wr_en <= wb_reg_nxt.dmem_write_en;
455 -- ext_gpmp.data <= ram_data;
456 -- ext_gpmp.addr <= wb_reg_nxt.address(wb_reg_nxt.address'high downto BYTEADDR);
457 -- case wb_reg.address(1 downto 0) is
458 -- when "00" => ext_gpmp.byte_en <= "0001";
459 -- when "01" => ext_gpmp.byte_en <= "0010";
460 -- when "10" => ext_gpmp.byte_en <= "0100";
461 -- when "11" => ext_gpmp.byte_en <= "1000";
462 -- when others => null;
464 -- hier kann man weiter extensions adden :) Konstanten sind im extension pkg definiert
465 when others => ext_anysel <= '0';
468 data_ram_read_ext <= ext_uart_out or ext_gpmp_out or ext_timer_out;