2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
6 use work.decode_types.all;
9 use work.insn_helpers.all;
16 complete_in : in std_ulogic;
17 stall_out : out std_ulogic;
19 stopped_out : out std_ulogic;
21 flush_in: in std_ulogic;
23 d_in : in Decode1ToDecode2Type;
25 e_out : out Decode2ToExecute1Type;
26 m_out : out Decode2ToMultiplyType;
27 d_out : out Decode2ToDividerType;
28 l_out : out Decode2ToLoadstore1Type;
30 r_in : in RegisterFileToDecode2Type;
31 r_out : out Decode2ToRegisterFileType;
33 c_in : in CrFileToDecode2Type;
34 c_out : out Decode2ToCrFileType
38 architecture behaviour of decode2 is
39 type state_type is (IDLE, WAIT_FOR_PREV_TO_COMPLETE, WAIT_FOR_CURR_TO_COMPLETE);
41 type reg_internal_type is record
43 outstanding : integer;
46 type reg_type is record
47 e : Decode2ToExecute1Type;
48 m : Decode2ToMultiplyType;
49 d : Decode2ToDividerType;
50 l : Decode2ToLoadstore1Type;
53 signal r_int, rin_int : reg_internal_type;
54 signal r, rin : reg_type;
56 type decode_input_reg_t is record
57 reg_valid : std_ulogic;
58 reg : std_ulogic_vector(4 downto 0);
59 data : std_ulogic_vector(63 downto 0);
62 function decode_input_reg_a (t : input_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
63 reg_data : std_ulogic_vector(63 downto 0)) return decode_input_reg_t is
67 return ('1', insn_ra(insn_in), reg_data);
69 return ('1', insn_ra(insn_in), ra_or_zero(reg_data, insn_ra(insn_in)));
71 return ('1', insn_rs(insn_in), reg_data);
73 return ('0', (others => '0'), (others => '0'));
77 function decode_input_reg_b (t : input_reg_b_t; insn_in : std_ulogic_vector(31 downto 0);
78 reg_data : std_ulogic_vector(63 downto 0)) return decode_input_reg_t is
82 return ('1', insn_rb(insn_in), reg_data);
84 return ('1', insn_rs(insn_in), reg_data);
86 return ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_ui(insn_in)), 64)));
88 return ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)), 64)));
90 return ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)) & x"0000", 64)));
92 return ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_si(insn_in)) & x"0000", 64)));
94 return ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_li(insn_in)) & "00", 64)));
96 return ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_bd(insn_in)) & "00", 64)));
98 return ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_ds(insn_in)) & "00", 64)));
100 return ('0', (others => '0'), x"FFFFFFFFFFFFFFFF");
102 return ('0', (others => '0'), (others => '0'));
106 function decode_input_reg_c (t : input_reg_c_t; insn_in : std_ulogic_vector(31 downto 0);
107 reg_data : std_ulogic_vector(63 downto 0)) return decode_input_reg_t is
111 return ('1', insn_rs(insn_in), reg_data);
113 return ('0', (others => '0'), (others => '0'));
117 function decode_output_reg (t : output_reg_a_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic_vector is
121 return insn_rt(insn_in);
123 return insn_ra(insn_in);
129 function decode_rc (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
133 return insn_rc(insn_in);
142 decode2_0: process(clk)
144 if rising_edge(clk) then
145 assert r_int.outstanding <= 1 report "Outstanding bad " & integer'image(r_int.outstanding) severity failure;
147 if rin.e.valid = '1' or rin.l.valid = '1' or rin.m.valid = '1' or rin.d.valid = '1' then
148 report "execute " & to_hstring(rin.e.nia);
155 r_out.read1_reg <= insn_ra(d_in.insn) when (d_in.decode.input_reg_a = RA) else
156 insn_ra(d_in.insn) when d_in.decode.input_reg_a = RA_OR_ZERO else
157 insn_rs(d_in.insn) when d_in.decode.input_reg_a = RS else
160 r_out.read2_reg <= insn_rb(d_in.insn) when d_in.decode.input_reg_b = RB else
161 insn_rs(d_in.insn) when d_in.decode.input_reg_b = RS else
164 r_out.read3_reg <= insn_rs(d_in.insn) when d_in.decode.input_reg_c = RS else
167 c_out.read <= d_in.decode.input_cr;
169 decode2_1: process(all)
170 variable v : reg_type;
171 variable v_int : reg_internal_type;
172 variable mul_a : std_ulogic_vector(63 downto 0);
173 variable mul_b : std_ulogic_vector(63 downto 0);
174 variable decoded_reg_a : decode_input_reg_t;
175 variable decoded_reg_b : decode_input_reg_t;
176 variable decoded_reg_c : decode_input_reg_t;
177 variable signed_division: std_ulogic;
178 variable is_valid : std_ulogic;
183 v.e := Decode2ToExecute1Init;
184 v.l := Decode2ToLoadStore1Init;
185 v.m := Decode2ToMultiplyInit;
186 v.d := Decode2ToDividerInit;
188 mul_a := (others => '0');
189 mul_b := (others => '0');
191 --v.e.input_cr := d_in.decode.input_cr;
192 --v.m.input_cr := d_in.decode.input_cr;
193 --v.e.output_cr := d_in.decode.output_cr;
195 decoded_reg_a := decode_input_reg_a (d_in.decode.input_reg_a, d_in.insn, r_in.read1_data);
196 decoded_reg_b := decode_input_reg_b (d_in.decode.input_reg_b, d_in.insn, r_in.read2_data);
197 decoded_reg_c := decode_input_reg_c (d_in.decode.input_reg_c, d_in.insn, r_in.read3_data);
199 r_out.read1_enable <= decoded_reg_a.reg_valid;
200 r_out.read2_enable <= decoded_reg_b.reg_valid;
201 r_out.read3_enable <= decoded_reg_c.reg_valid;
205 v.e.insn_type := d_in.decode.insn_type;
206 v.e.read_reg1 := decoded_reg_a.reg;
207 v.e.read_data1 := decoded_reg_a.data;
208 v.e.read_reg2 := decoded_reg_b.reg;
209 v.e.read_data2 := decoded_reg_b.data;
210 v.e.write_reg := decode_output_reg(d_in.decode.output_reg_a, d_in.insn);
211 v.e.rc := decode_rc(d_in.decode.rc, d_in.insn);
212 v.e.cr := c_in.read_cr_data;
213 v.e.invert_a := d_in.decode.invert_a;
214 v.e.input_carry := d_in.decode.input_carry;
215 v.e.output_carry := d_in.decode.output_carry;
216 if d_in.decode.lr = '1' then
217 v.e.lr := insn_lk(d_in.insn);
219 v.e.insn := d_in.insn;
222 v.m.insn_type := d_in.decode.insn_type;
223 mul_a := decoded_reg_a.data;
224 mul_b := decoded_reg_b.data;
225 v.m.write_reg := decode_output_reg(d_in.decode.output_reg_a, d_in.insn);
226 v.m.rc := decode_rc(d_in.decode.rc, d_in.insn);
228 if d_in.decode.mul_32bit = '1' then
229 if d_in.decode.mul_signed = '1' then
230 v.m.data1 := (others => mul_a(31));
231 v.m.data1(31 downto 0) := mul_a(31 downto 0);
232 v.m.data2 := (others => mul_b(31));
233 v.m.data2(31 downto 0) := mul_b(31 downto 0);
235 v.m.data1 := '0' & x"00000000" & mul_a(31 downto 0);
236 v.m.data2 := '0' & x"00000000" & mul_b(31 downto 0);
239 if d_in.decode.mul_signed = '1' then
240 v.m.data1 := mul_a(63) & mul_a;
241 v.m.data2 := mul_b(63) & mul_b;
243 v.m.data1 := '0' & mul_a;
244 v.m.data2 := '0' & mul_b;
249 -- PPC divide and modulus instruction words have these bits in
250 -- the bottom 11 bits: o1dns 010t1 r
251 -- where o = OE for div instrs, signedness for mod instrs
252 -- d = 1 for div*, 0 for mod*
253 -- n = 1 for normal, 0 for extended (dividend << 32/64)
254 -- s = 1 for signed, 0 for unsigned (for div*)
255 -- t = 1 for 32-bit, 0 for 64-bit
256 -- r = RC bit (record condition code)
257 v.d.write_reg := decode_output_reg(d_in.decode.output_reg_a, d_in.insn);
258 v.d.is_modulus := not d_in.insn(8);
259 v.d.is_32bit := not d_in.insn(2);
260 if d_in.insn(8) = '1' then
261 signed_division := d_in.insn(6);
263 signed_division := d_in.insn(10);
265 v.d.is_signed := signed_division;
266 if d_in.insn(2) = '0' then
268 if d_in.insn(8) = '1' and d_in.insn(7) = '0' then
269 v.d.is_extended := '1';
271 v.d.dividend := decoded_reg_a.data;
272 v.d.divisor := decoded_reg_b.data;
275 if d_in.insn(8) = '1' and d_in.insn(7) = '0' then -- extended forms
276 v.d.dividend := decoded_reg_a.data(31 downto 0) & x"00000000";
277 elsif signed_division = '1' and decoded_reg_a.data(31) = '1' then
278 -- sign extend to 64 bits
279 v.d.dividend := x"ffffffff" & decoded_reg_a.data(31 downto 0);
281 v.d.dividend := x"00000000" & decoded_reg_a.data(31 downto 0);
283 if signed_division = '1' and decoded_reg_b.data(31) = '1' then
284 v.d.divisor := x"ffffffff" & decoded_reg_b.data(31 downto 0);
286 v.d.divisor := x"00000000" & decoded_reg_b.data(31 downto 0);
289 v.d.rc := decode_rc(d_in.decode.rc, d_in.insn);
292 v.l.update_reg := decoded_reg_a.reg;
293 v.l.addr1 := decoded_reg_a.data;
294 v.l.addr2 := decoded_reg_b.data;
295 v.l.data := decoded_reg_c.data;
296 v.l.write_reg := decode_output_reg(d_in.decode.output_reg_a, d_in.insn);
298 if d_in.decode.insn_type = OP_LOAD then
304 case d_in.decode.length is
306 v.l.length := "0001";
308 v.l.length := "0010";
310 v.l.length := "0100";
312 v.l.length := "1000";
314 v.l.length := "0000";
317 v.l.byte_reverse := d_in.decode.byte_reverse;
318 v.l.sign_extend := d_in.decode.sign_extend;
319 v.l.update := d_in.decode.update;
323 if complete_in = '1' then
324 v_int.outstanding := v_int.outstanding - 1;
327 -- state machine to handle instructions that must be single
328 -- through the pipeline.
330 is_valid := d_in.valid;
332 -- Handle debugger stop
334 if d_in.stop_mark = '1' and v_int.outstanding = 0 then
340 if (flush_in = '0') and (is_valid = '1') and (d_in.decode.sgl_pipe = '1') then
341 if v_int.outstanding /= 0 then
342 v_int.state := WAIT_FOR_PREV_TO_COMPLETE;
346 -- send insn out and wait on it to complete
347 v_int.state := WAIT_FOR_CURR_TO_COMPLETE;
351 when WAIT_FOR_PREV_TO_COMPLETE =>
352 if v_int.outstanding = 0 then
353 -- send insn out and wait on it to complete
354 v_int.state := WAIT_FOR_CURR_TO_COMPLETE;
360 when WAIT_FOR_CURR_TO_COMPLETE =>
361 if v_int.outstanding = 0 then
373 case d_in.decode.unit is
375 v.e.valid := is_valid;
377 v.l.valid := is_valid;
379 v.m.valid := is_valid;
381 v.d.valid := is_valid;
383 v.e.valid := is_valid;
384 v.e.insn_type := OP_ILLEGAL;
387 if flush_in = '1' then
394 -- track outstanding instructions
395 if v.e.valid = '1' or v.l.valid = '1' or v.m.valid = '1' or v.d.valid = '1' then
396 v_int.outstanding := v_int.outstanding + 1;
401 v_int.outstanding := 0;
402 v.e := Decode2ToExecute1Init;
403 v.l := Decode2ToLoadStore1Init;
404 v.m := Decode2ToMultiplyInit;
405 v.d := Decode2ToDividerInit;
418 end architecture behaviour;