2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
7 use work.crhelpers.all;
13 e_in : in Execute1ToWritebackType;
14 l_in : in Loadstore2ToWritebackType;
15 m_in : in MultiplyToWritebackType;
16 d_in : in DividerToWritebackType;
18 w_out : out WritebackToRegisterFileType;
19 c_out : out WritebackToCrFileType;
21 complete_out : out std_ulogic
25 architecture behaviour of writeback is
26 subtype byte_index_t is unsigned(2 downto 0);
27 type permutation_t is array(0 to 7) of byte_index_t;
28 subtype byte_trim_t is std_ulogic_vector(1 downto 0);
29 type trim_ctl_t is array(0 to 7) of byte_trim_t;
30 type byte_sel_t is array(0 to 7) of std_ulogic;
32 signal data_len : unsigned(3 downto 0);
33 signal data_in : std_ulogic_vector(63 downto 0);
34 signal data_permuted : std_ulogic_vector(63 downto 0);
35 signal data_trimmed : std_ulogic_vector(63 downto 0);
36 signal data_latched : std_ulogic_vector(63 downto 0);
37 signal perm : permutation_t;
38 signal use_second : byte_sel_t;
39 signal byte_offset : unsigned(2 downto 0);
40 signal brev_lenm1 : unsigned(2 downto 0);
41 signal trim_ctl : trim_ctl_t;
42 signal rc : std_ulogic;
43 signal partial_write : std_ulogic;
44 signal sign_extend : std_ulogic;
45 signal negative : std_ulogic;
46 signal second_word : std_ulogic;
48 writeback_0: process(clk)
50 if rising_edge(clk) then
51 if partial_write = '1' then
52 data_latched <= data_permuted;
57 writeback_1: process(all)
58 variable x : std_ulogic_vector(0 downto 0);
59 variable y : std_ulogic_vector(0 downto 0);
60 variable z : std_ulogic_vector(0 downto 0);
61 variable w : std_ulogic_vector(0 downto 0);
63 variable k : unsigned(3 downto 0);
69 assert (to_integer(unsigned(x)) + to_integer(unsigned(y)) + to_integer(unsigned(z)) + to_integer(unsigned(w))) <= 1 severity failure;
71 x := "" & e_in.write_enable;
72 y := "" & l_in.write_enable;
73 z := "" & m_in.write_reg_enable;
74 w := "" & d_in.write_reg_enable;
75 assert (to_integer(unsigned(x)) + to_integer(unsigned(y)) + to_integer(unsigned(z)) + to_integer(unsigned(w))) <= 1 severity failure;
77 w := "" & e_in.write_cr_enable;
78 x := "" & (e_in.write_enable and e_in.rc);
79 y := "" & (m_in.valid and m_in.rc);
80 z := "" & (d_in.valid and d_in.rc);
81 assert (to_integer(unsigned(w)) + to_integer(unsigned(x)) + to_integer(unsigned(y)) + to_integer(unsigned(z))) <= 1 severity failure;
83 w_out <= WritebackToRegisterFileInit;
84 c_out <= WritebackToCrFileInit;
87 if e_in.valid = '1' or l_in.valid = '1' or m_in.valid = '1' or d_in.valid = '1' then
99 if e_in.write_enable = '1' then
100 w_out.write_reg <= e_in.write_reg;
101 data_in <= e_in.write_data;
102 w_out.write_enable <= '1';
103 data_len <= unsigned(e_in.write_len);
104 sign_extend <= e_in.sign_extend;
108 if e_in.write_cr_enable = '1' then
109 c_out.write_cr_enable <= '1';
110 c_out.write_cr_mask <= e_in.write_cr_mask;
111 c_out.write_cr_data <= e_in.write_cr_data;
114 if l_in.write_enable = '1' then
115 w_out.write_reg <= l_in.write_reg;
116 data_in <= l_in.write_data;
117 data_len <= unsigned(l_in.write_len);
118 byte_offset <= unsigned(l_in.write_shift);
119 sign_extend <= l_in.sign_extend;
120 if l_in.byte_reverse = '1' then
121 brev_lenm1 <= unsigned(l_in.write_len(2 downto 0)) - 1;
123 w_out.write_enable <= '1';
124 second_word <= l_in.second_word;
125 if l_in.valid = '0' and (data_len + byte_offset > 8) then
126 partial_write <= '1';
130 if m_in.write_reg_enable = '1' then
131 w_out.write_enable <= '1';
132 w_out.write_reg <= m_in.write_reg_nr;
133 data_in <= m_in.write_reg_data;
137 if d_in.write_reg_enable = '1' then
138 w_out.write_enable <= '1';
139 w_out.write_reg <= d_in.write_reg_nr;
140 data_in <= d_in.write_reg_data;
144 -- shift and byte-reverse data bytes
146 k := ('0' & (to_unsigned(i, 3) xor brev_lenm1)) + ('0' & byte_offset);
147 perm(i) <= k(2 downto 0);
148 use_second(i) <= k(3);
151 j := to_integer(perm(i)) * 8;
152 data_permuted(i * 8 + 7 downto i * 8) <= data_in(j + 7 downto j);
155 -- If the data can arrive split over two cycles, this will be correct
156 -- provided we don't have both sign extension and byte reversal.
157 negative <= (data_len(2) and data_permuted(31)) or (data_len(1) and data_permuted(15)) or
158 (data_len(0) and data_permuted(7));
160 -- trim and sign-extend
162 if i < to_integer(data_len) then
163 if second_word = '1' then
164 trim_ctl(i) <= '1' & not use_second(i);
166 trim_ctl(i) <= not use_second(i) & '0';
169 trim_ctl(i) <= '0' & (negative and sign_extend);
175 data_trimmed(i * 8 + 7 downto i * 8) <= data_latched(i * 8 + 7 downto i * 8);
177 data_trimmed(i * 8 + 7 downto i * 8) <= data_permuted(i * 8 + 7 downto i * 8);
179 data_trimmed(i * 8 + 7 downto i * 8) <= x"FF";
181 data_trimmed(i * 8 + 7 downto i * 8) <= x"00";
185 -- deliver to regfile
186 w_out.write_data <= data_trimmed;
188 -- test value against 0 and set CR0 if requested
190 c_out.write_cr_enable <= '1';
191 c_out.write_cr_mask <= num_to_fxm(0);
192 if data_trimmed(63) = '1' then
193 c_out.write_cr_data <= x"80000000";
194 elsif or (data_trimmed(62 downto 0)) = '1' then
195 c_out.write_cr_data <= x"40000000";
197 c_out.write_cr_data <= x"20000000";