Merge pull request #113 from mikey/exec-sim-remove
[microwatt.git] / writeback.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.common.all;
7 use work.crhelpers.all;
8
9 entity writeback is
10 port (
11 clk : in std_ulogic;
12
13 e_in : in Execute1ToWritebackType;
14 l_in : in DcacheToWritebackType;
15 m_in : in MultiplyToWritebackType;
16 d_in : in DividerToWritebackType;
17
18 w_out : out WritebackToRegisterFileType;
19 c_out : out WritebackToCrFileType;
20
21 complete_out : out std_ulogic
22 );
23 end entity writeback;
24
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;
31
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;
47 begin
48 writeback_0: process(clk)
49 begin
50 if rising_edge(clk) then
51 if partial_write = '1' then
52 data_latched <= data_permuted;
53 end if;
54 end if;
55 end process;
56
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);
62 variable j : integer;
63 variable k : unsigned(3 downto 0);
64 begin
65 x := "" & e_in.valid;
66 y := "" & l_in.valid;
67 z := "" & m_in.valid;
68 w := "" & d_in.valid;
69 assert (to_integer(unsigned(x)) + to_integer(unsigned(y)) + to_integer(unsigned(z)) + to_integer(unsigned(w))) <= 1 severity failure;
70
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;
76
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;
82
83 w_out <= WritebackToRegisterFileInit;
84 c_out <= WritebackToCrFileInit;
85
86 complete_out <= '0';
87 if e_in.valid = '1' or l_in.valid = '1' or m_in.valid = '1' or d_in.valid = '1' then
88 complete_out <= '1';
89 end if;
90
91 rc <= '0';
92 brev_lenm1 <= "000";
93 byte_offset <= "000";
94 data_len <= x"8";
95 partial_write <= '0';
96 sign_extend <= '0';
97 second_word <= '0';
98 data_in <= e_in.write_data;
99
100 if e_in.write_enable = '1' then
101 w_out.write_reg <= e_in.write_reg;
102 data_in <= e_in.write_data;
103 w_out.write_enable <= '1';
104 data_len <= unsigned(e_in.write_len);
105 sign_extend <= e_in.sign_extend;
106 rc <= e_in.rc;
107 end if;
108
109 if e_in.write_cr_enable = '1' then
110 c_out.write_cr_enable <= '1';
111 c_out.write_cr_mask <= e_in.write_cr_mask;
112 c_out.write_cr_data <= e_in.write_cr_data;
113 end if;
114
115 if l_in.write_enable = '1' then
116 w_out.write_reg <= l_in.write_reg;
117 data_in <= l_in.write_data;
118 data_len <= unsigned(l_in.write_len);
119 byte_offset <= unsigned(l_in.write_shift);
120 sign_extend <= l_in.sign_extend;
121 if l_in.byte_reverse = '1' then
122 brev_lenm1 <= unsigned(l_in.write_len(2 downto 0)) - 1;
123 end if;
124 w_out.write_enable <= '1';
125 second_word <= l_in.second_word;
126 if l_in.valid = '0' and (data_len + byte_offset > 8) then
127 partial_write <= '1';
128 end if;
129 end if;
130
131 if m_in.write_reg_enable = '1' then
132 w_out.write_enable <= '1';
133 w_out.write_reg <= m_in.write_reg_nr;
134 data_in <= m_in.write_reg_data;
135 rc <= m_in.rc;
136 end if;
137
138 if d_in.write_reg_enable = '1' then
139 w_out.write_enable <= '1';
140 w_out.write_reg <= d_in.write_reg_nr;
141 data_in <= d_in.write_reg_data;
142 rc <= d_in.rc;
143 end if;
144
145 -- shift and byte-reverse data bytes
146 for i in 0 to 7 loop
147 k := ('0' & (to_unsigned(i, 3) xor brev_lenm1)) + ('0' & byte_offset);
148 perm(i) <= k(2 downto 0);
149 use_second(i) <= k(3);
150 end loop;
151 for i in 0 to 7 loop
152 j := to_integer(perm(i)) * 8;
153 data_permuted(i * 8 + 7 downto i * 8) <= data_in(j + 7 downto j);
154 end loop;
155
156 -- If the data can arrive split over two cycles, this will be correct
157 -- provided we don't have both sign extension and byte reversal.
158 negative <= (data_len(2) and data_permuted(31)) or (data_len(1) and data_permuted(15)) or
159 (data_len(0) and data_permuted(7));
160
161 -- trim and sign-extend
162 for i in 0 to 7 loop
163 if i < to_integer(data_len) then
164 if second_word = '1' then
165 trim_ctl(i) <= '1' & not use_second(i);
166 else
167 trim_ctl(i) <= not use_second(i) & '0';
168 end if;
169 else
170 trim_ctl(i) <= '0' & (negative and sign_extend);
171 end if;
172 end loop;
173 for i in 0 to 7 loop
174 case trim_ctl(i) is
175 when "11" =>
176 data_trimmed(i * 8 + 7 downto i * 8) <= data_latched(i * 8 + 7 downto i * 8);
177 when "10" =>
178 data_trimmed(i * 8 + 7 downto i * 8) <= data_permuted(i * 8 + 7 downto i * 8);
179 when "01" =>
180 data_trimmed(i * 8 + 7 downto i * 8) <= x"FF";
181 when others =>
182 data_trimmed(i * 8 + 7 downto i * 8) <= x"00";
183 end case;
184 end loop;
185
186 -- deliver to regfile
187 w_out.write_data <= data_trimmed;
188
189 -- test value against 0 and set CR0 if requested
190 if rc = '1' then
191 c_out.write_cr_enable <= '1';
192 c_out.write_cr_mask <= num_to_fxm(0);
193 if data_trimmed(63) = '1' then
194 c_out.write_cr_data <= x"80000000";
195 elsif or (data_trimmed(62 downto 0)) = '1' then
196 c_out.write_cr_data <= x"40000000";
197 else
198 c_out.write_cr_data <= x"20000000";
199 end if;
200 end if;
201 end process;
202 end;