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;
13 architecture behav of writeback_stage is
15 signal data_ram_read, data_ram_read_ext : word_t;
16 signal data_addr : word_t;
18 signal wb_reg, wb_reg_nxt : writeback_rec;
20 signal ext_uart,ext_timer,ext_gpmp,ext_7seg : extmod_rec;
21 signal ext_uart_out, ext_timer_out, ext_gpmp_out : gp_register_t;
23 signal sel_nxt, dmem_we, ext_anysel : std_logic;
25 signal calc_mem_res : gp_register_t;
29 ext_timer_out <= (others => '0'); --TODO: delete when timer is connected
30 ext_gpmp_out <= (others => '0'); --TODO: delete when gpm is connected
39 data_addr(DATA_ADDR_WIDTH+1 downto 2),
40 data_addr(DATA_ADDR_WIDTH+1 downto 2),
43 wb_reg_nxt.data, --ram_data,
74 syn: process(clk, reset)
78 if (reset = RESET_VALUE) then
79 wb_reg.address <= (others => '0');
80 wb_reg.dmem_en <= '0';
81 wb_reg.dmem_write_en <= '0';
85 wb_reg.byte_en <= (others => '0');
86 wb_reg.data <= (others =>'0');
87 elsif rising_edge(clk) then
93 -- type writeback_rec is record
94 -- address : in word_t; --ureg
95 -- dmem_en : in std_logic; --ureg (jump addr in mem or in address)
96 -- dmem_write_en : in std_logic; --ureg
97 -- hword_hl : in std_logic --ureg
102 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)
103 variable byte_en : byte_en_t;
104 variable address_val : std_logic_vector(1 downto 0);
106 wb_reg_nxt.address <= address;
107 wb_reg_nxt.dmem_en <= dmem_en;
108 wb_reg_nxt.dmem_write_en <= dmem_write_en;
109 wb_reg_nxt.hword <= hword;
110 wb_reg_nxt.byte_s <= byte_s;
112 calc_mem_res <= result; --(others => '0');
114 wb_reg_nxt.data <= ram_data;
115 byte_en := (others => '0');
116 address_val := address(BYTEADDR-1 downto 0);
117 if dmem_en = '1' then
119 -- case address(BYTEADDR-1 downto 0) is
122 byte_en(1 downto 0) := "11";
124 byte_en(3 downto 2) := "11";
125 wb_reg_nxt.data(31 downto 16) <= ram_data(15 downto 0);
128 elsif byte_s = '1' then
129 -- case address(BYTEADDR-1 downto 0) is
131 when "00" => byte_en(0) := '1';
134 wb_reg_nxt.data(15 downto 8) <= ram_data(7 downto 0);
137 wb_reg_nxt.data(23 downto 16) <= ram_data(7 downto 0);
140 wb_reg_nxt.data(31 downto 24) <= ram_data(7 downto 0);
144 byte_en := (others => '1');
147 wb_reg_nxt.byte_en <= byte_en;
149 -- if (wb_reg.dmem_en = '1' and wb_reg.dmem_write_en = '0') then -- ram read operation --alu_jmp = '0' and
150 -- calc_mem_res <= data_ram_read;
151 -- if (wb_reg.hword = '1') then
152 -- calc_mem_res <= (others => '0');
153 -- if (wb_reg.address(1) = '1') then
154 -- calc_mem_res(15 downto 0) <= data_ram_read(31 downto 16);
156 -- calc_mem_res(15 downto 0) <= data_ram_read(15 downto 0);
159 -- if (wb_reg.byte_s = '1') then
160 -- calc_mem_res <= (others => '0');
161 -- case wb_reg.address(1 downto 0) is
162 -- when "00" => calc_mem_res(7 downto 0) <= data_ram_read(7 downto 0);
163 -- when "01" => calc_mem_res(7 downto 0) <= data_ram_read(15 downto 8);
164 -- when "10" => calc_mem_res(7 downto 0) <= data_ram_read(23 downto 16);
165 -- when "11" => calc_mem_res(7 downto 0) <= data_ram_read(31 downto 24);
166 -- when others => null;
171 --jump <= (alu_jmp xor br_pred) and (write_en or wb_reg.dmem_en);
172 jump <= (alu_jmp xor br_pred);-- and (write_en or wb_reg.dmem_en);
174 if (alu_jmp = '1' and wb_reg.dmem_en = '1' and wb_reg.dmem_write_en = '0' and write_en = '0') then
175 jump_addr <= data_ram_read;
180 -- if alu_jmp = '0' and br_pred = '1' and write_en = '0' then
184 -- if ((alu_jmp and wb_reg.dmem_en) = '1') then
185 -- jump_addr <= data_ram_read;
190 -- result : in gp_register_t; --reg (alu result or jumpaddr)
191 -- result_addr : in gp_addr_t; --reg
192 -- address : in word_t; --ureg
193 -- alu_jmp : in std_logic; --reg
194 -- br_pred : in std_logic; --reg
195 -- write_en : in std_logic; --reg (register file)
196 -- dmem_en : in std_logic; --ureg (jump addr in mem or in result)
197 -- dmem_write_en : in std_logic; --ureg
198 -- hword : in std_logic --ureg
202 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)
203 variable reg_we_v : std_logic;
204 variable data_out : gp_register_t;
206 reg_we_v := (write_en or (wb_reg.dmem_en and not(wb_reg.dmem_write_en))) and not(alu_jmp);
207 reg_addr <= result_addr;
209 data_addr <= (others => '0');
212 if (wb_reg.address(DATA_ADDR_WIDTH+2) /= '1') then
213 data_out := data_ram_read;
215 reg_we_v := reg_we_v and ext_anysel;
216 data_out := data_ram_read_ext;
219 if wb_reg.byte_en(0) = '0' then
220 data_out(byte_t'range) := (others => '0');
222 if wb_reg.byte_en(1) = '0' then
223 data_out(2*byte_t'length-1 downto byte_t'length) := (others => '0');
225 if wb_reg.byte_en(2) = '0' then
226 data_out(3*byte_t'length-1 downto 2*byte_t'length) := (others => '0');
228 if wb_reg.byte_en(3) = '0' then
229 data_out(4*byte_t'length-1 downto 3*byte_t'length) := (others => '0');
233 -- if wb_reg.hword = '1' or wb_reg.byte_s = '1' then
234 -- if wb_reg.address(1)='1' then
235 -- data_out(hword_t'range) := data_out(data_out'high downto (data_out'length/2));
237 -- data_out(data_out'high downto (data_out'length/2)) := (others => '0');
238 -- if byte_s = '1' then
239 -- if wb_reg.address(0) = '1' then
240 -- data_out(byte_t'range) := data_out(hword_t'high downto (hword_t'length/2));
242 -- data_out(hword_t'high downto (hword_t'length/2)) := (others => '0');
247 data_out := to_stdlogicvector(to_bitvector(data_out) srl to_integer(unsigned(wb_reg.address(BYTEADDR-1 downto 0)))*byte_t'length);
249 if (wb_reg_nxt.address(DATA_ADDR_WIDTH+2) /= '1') then
250 data_addr(DATA_ADDR_WIDTH+1 downto 0) <= wb_reg_nxt.address(DATA_ADDR_WIDTH+1 downto 0);
251 dmem_we <= wb_reg_nxt.dmem_write_en;
254 regfile_val <= data_out;
256 if wb_reg.dmem_en = '0' then
257 regfile_val <= result;
265 addr_de_mult: process(wb_reg, wb_reg_nxt, ram_data, sel_nxt, ext_uart_out, ext_gpmp_out, ext_timer_out)
266 variable wr_en, enable : std_logic; -- these are all registered
267 variable byte_en : byte_en_t; -- if a module needs the nxt signals it has to manually select them
268 variable addr : ext_addr_t; -- for example the data memory, because it already has input registers
269 variable addrid : std_logic_vector(27 downto 0);--ext_addrid_t;
270 variable data : gp_register_t;
273 --if selecting enable is too slow, see alu_b
274 enable := wb_reg.dmem_en;
275 wr_en := wb_reg.dmem_write_en;
276 byte_en := wb_reg.byte_en;
277 addr := wb_reg.address(gp_register_t'high downto BYTEADDR);
278 addrid := wb_reg.address(gp_register_t'high downto EXTWORDS);
286 ext_uart.wr_en <= wr_en;
287 ext_7seg.wr_en <= wr_en;
288 ext_timer.wr_en <= wr_en;
289 ext_gpmp.wr_en <= wr_en;
291 ext_uart.byte_en <= byte_en;
292 ext_7seg.byte_en <= byte_en;
293 ext_timer.byte_en <= byte_en;
294 ext_gpmp.byte_en <= byte_en;
296 ext_uart.addr <= addr;
297 ext_7seg.addr <= addr;
298 ext_timer.addr <= addr;
299 ext_gpmp.addr <= addr;
301 ext_uart.data <= data;
302 ext_7seg.data <= data;
303 ext_timer.data <= data;
304 ext_gpmp.data <= data;
305 -- wenn ich hier statt dem 4rer die konstante nehme dann gibts an fehler wegen nicht lokaler variable -.-
307 when EXT_UART_ADDR =>
308 ext_uart.sel <= enable;
309 ext_anysel <= enable;
311 -- ext_uart.wr_en <= wb_reg_nxt.dmem_write_en;
312 -- ext_uart.data <= ram_data;
313 -- ext_uart.addr <= wb_reg_nxt.address(31 downto 2);
314 -- case wb_reg_nxt.address(1 downto 0) is
315 -- when "00" => ext_uart.byte_en <= "0001";
316 -- when "01" => ext_uart.byte_en <= "0010";
317 -- when "10" => ext_uart.byte_en <= "0100";
318 -- --when "11" => ext_uart.byte_en <= "1000";
319 -- when "11" => ext_uart.byte_en <= "1111";
320 -- when others => null;
323 when EXT_7SEG_ADDR =>
324 ext_7seg.sel <= enable;
325 ext_anysel <= enable;
327 -- ext_7seg.wr_en <= wb_regdmem_write_en;
328 -- ext_7seg.data <= ram_data;
329 -- ext_7seg.addr <= wb_reg_nxt.address(31 downto 2);
330 -- ext_7seg.byte_en(1 downto 0) <= wb_reg_nxt.address(1 downto 0);
333 -- case wb_reg_nxt.address(1 downto 0) is
334 -- when "00" => ext_7seg.byte_en <= "0001";
335 -- when "01" => ext_7seg.byte_en <= "0010";
336 -- when "10" => ext_7seg.byte_en <= "0100";
337 -- when "11" => ext_7seg.byte_en <= "1000";
338 -- when others => null;
341 when EXT_TIMER_ADDR =>
342 ext_timer.sel <= enable;
343 ext_anysel <= enable;
344 -- ext_timer.wr_en <= wb_reg_nxt.dmem_write_en;
345 -- ext_timer.data <= ram_data;
346 -- ext_timer.addr <= wb_reg_nxt.address(wb_reg_nxt.address'high downto BYTEADDR);
347 -- case wb_reg.address(1 downto 0) is
348 -- when "00" => ext_timer.byte_en <= "0001";
349 -- when "01" => ext_timer.byte_en <= "0010";
350 -- when "10" => ext_timer.byte_en <= "0100";
351 -- when "11" => ext_timer.byte_en <= "1000";
352 -- when others => null;
354 when EXT_GPMP_ADDR =>
355 ext_gpmp.sel <= enable;
356 ext_anysel <= enable;
357 -- ext_gpmp.wr_en <= wb_reg_nxt.dmem_write_en;
358 -- ext_gpmp.data <= ram_data;
359 -- ext_gpmp.addr <= wb_reg_nxt.address(wb_reg_nxt.address'high downto BYTEADDR);
360 -- case wb_reg.address(1 downto 0) is
361 -- when "00" => ext_gpmp.byte_en <= "0001";
362 -- when "01" => ext_gpmp.byte_en <= "0010";
363 -- when "10" => ext_gpmp.byte_en <= "0100";
364 -- when "11" => ext_gpmp.byte_en <= "1000";
365 -- when others => null;
367 -- hier kann man weiter extensions adden :) Konstanten sind im extension pkg definiert
368 when others => ext_anysel <= '0';
371 data_ram_read_ext <= ext_uart_out or ext_gpmp_out or ext_timer_out;