FPU: Implement fmr and related instructions
[microwatt.git] / decode2.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.decode_types.all;
7 use work.common.all;
8 use work.helpers.all;
9 use work.insn_helpers.all;
10
11 entity decode2 is
12 generic (
13 EX1_BYPASS : boolean := true;
14 HAS_FPU : boolean := true;
15 -- Non-zero to enable log data collection
16 LOG_LENGTH : natural := 0
17 );
18 port (
19 clk : in std_ulogic;
20 rst : in std_ulogic;
21
22 complete_in : in std_ulogic;
23 busy_in : in std_ulogic;
24 stall_out : out std_ulogic;
25
26 stopped_out : out std_ulogic;
27
28 flush_in: in std_ulogic;
29
30 d_in : in Decode1ToDecode2Type;
31
32 e_out : out Decode2ToExecute1Type;
33
34 r_in : in RegisterFileToDecode2Type;
35 r_out : out Decode2ToRegisterFileType;
36
37 c_in : in CrFileToDecode2Type;
38 c_out : out Decode2ToCrFileType;
39
40 log_out : out std_ulogic_vector(9 downto 0)
41 );
42 end entity decode2;
43
44 architecture behaviour of decode2 is
45 type reg_type is record
46 e : Decode2ToExecute1Type;
47 end record;
48
49 signal r, rin : reg_type;
50
51 signal deferred : std_ulogic;
52
53 type decode_input_reg_t is record
54 reg_valid : std_ulogic;
55 reg : gspr_index_t;
56 data : std_ulogic_vector(63 downto 0);
57 end record;
58
59 type decode_output_reg_t is record
60 reg_valid : std_ulogic;
61 reg : gspr_index_t;
62 end record;
63
64 function decode_input_reg_a (t : input_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
65 reg_data : std_ulogic_vector(63 downto 0);
66 ispr : gspr_index_t;
67 instr_addr : std_ulogic_vector(63 downto 0))
68 return decode_input_reg_t is
69 begin
70 if t = RA or (t = RA_OR_ZERO and insn_ra(insn_in) /= "00000") then
71 return ('1', gpr_to_gspr(insn_ra(insn_in)), reg_data);
72 elsif t = SPR then
73 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
74 -- If it's all 0, we don't treat it as a dependency as slow SPRs
75 -- operations are single issue.
76 --
77 assert is_fast_spr(ispr) = '1' or ispr = "0000000"
78 report "Decode A says SPR but ISPR is invalid:" &
79 to_hstring(ispr) severity failure;
80 return (is_fast_spr(ispr), ispr, reg_data);
81 elsif t = CIA then
82 return ('0', (others => '0'), instr_addr);
83 elsif HAS_FPU and t = FRA then
84 return ('1', fpr_to_gspr(insn_fra(insn_in)), reg_data);
85 else
86 return ('0', (others => '0'), (others => '0'));
87 end if;
88 end;
89
90 function decode_input_reg_b (t : input_reg_b_t; insn_in : std_ulogic_vector(31 downto 0);
91 reg_data : std_ulogic_vector(63 downto 0);
92 ispr : gspr_index_t) return decode_input_reg_t is
93 variable ret : decode_input_reg_t;
94 begin
95 case t is
96 when RB =>
97 ret := ('1', gpr_to_gspr(insn_rb(insn_in)), reg_data);
98 when FRB =>
99 if HAS_FPU then
100 ret := ('1', fpr_to_gspr(insn_frb(insn_in)), reg_data);
101 else
102 ret := ('0', (others => '0'), (others => '0'));
103 end if;
104 when CONST_UI =>
105 ret := ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_ui(insn_in)), 64)));
106 when CONST_SI =>
107 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)), 64)));
108 when CONST_SI_HI =>
109 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)) & x"0000", 64)));
110 when CONST_UI_HI =>
111 ret := ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_si(insn_in)) & x"0000", 64)));
112 when CONST_LI =>
113 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_li(insn_in)) & "00", 64)));
114 when CONST_BD =>
115 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_bd(insn_in)) & "00", 64)));
116 when CONST_DS =>
117 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_ds(insn_in)) & "00", 64)));
118 when CONST_DXHI4 =>
119 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_dx(insn_in)) & x"0004", 64)));
120 when CONST_M1 =>
121 ret := ('0', (others => '0'), x"FFFFFFFFFFFFFFFF");
122 when CONST_SH =>
123 ret := ('0', (others => '0'), x"00000000000000" & "00" & insn_in(1) & insn_in(15 downto 11));
124 when CONST_SH32 =>
125 ret := ('0', (others => '0'), x"00000000000000" & "000" & insn_in(15 downto 11));
126 when SPR =>
127 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
128 -- If it's all 0, we don't treat it as a dependency as slow SPRs
129 -- operations are single issue.
130 assert is_fast_spr(ispr) = '1' or ispr = "0000000"
131 report "Decode B says SPR but ISPR is invalid:" &
132 to_hstring(ispr) severity failure;
133 ret := (is_fast_spr(ispr), ispr, reg_data);
134 when NONE =>
135 ret := ('0', (others => '0'), (others => '0'));
136 end case;
137
138 return ret;
139 end;
140
141 function decode_input_reg_c (t : input_reg_c_t; insn_in : std_ulogic_vector(31 downto 0);
142 reg_data : std_ulogic_vector(63 downto 0)) return decode_input_reg_t is
143 begin
144 case t is
145 when RS =>
146 return ('1', gpr_to_gspr(insn_rs(insn_in)), reg_data);
147 when RCR =>
148 return ('1', gpr_to_gspr(insn_rcreg(insn_in)), reg_data);
149 when FRS =>
150 if HAS_FPU then
151 return ('1', fpr_to_gspr(insn_frt(insn_in)), reg_data);
152 else
153 return ('0', (others => '0'), (others => '0'));
154 end if;
155 when NONE =>
156 return ('0', (others => '0'), (others => '0'));
157 end case;
158 end;
159
160 function decode_output_reg (t : output_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
161 ispr : gspr_index_t) return decode_output_reg_t is
162 begin
163 case t is
164 when RT =>
165 return ('1', gpr_to_gspr(insn_rt(insn_in)));
166 when RA =>
167 return ('1', gpr_to_gspr(insn_ra(insn_in)));
168 when FRT =>
169 if HAS_FPU then
170 return ('1', fpr_to_gspr(insn_frt(insn_in)));
171 else
172 return ('0', "0000000");
173 end if;
174 when SPR =>
175 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
176 -- If it's all 0, we don't treat it as a dependency as slow SPRs
177 -- operations are single issue.
178 assert is_fast_spr(ispr) = '1' or ispr = "0000000"
179 report "Decode B says SPR but ISPR is invalid:" &
180 to_hstring(ispr) severity failure;
181 return (is_fast_spr(ispr), ispr);
182 when NONE =>
183 return ('0', "0000000");
184 end case;
185 end;
186
187 function decode_rc (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
188 begin
189 case t is
190 when RC =>
191 return insn_rc(insn_in);
192 when ONE =>
193 return '1';
194 when NONE =>
195 return '0';
196 end case;
197 end;
198
199 -- For now, use "rc" in the decode table to decide whether oe exists.
200 -- This is not entirely correct architecturally: For mulhd and
201 -- mulhdu, the OE field is reserved. It remains to be seen what an
202 -- actual POWER9 does if we set it on those instructions, for now we
203 -- test that further down when assigning to the multiplier oe input.
204 --
205 function decode_oe (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
206 begin
207 case t is
208 when RC =>
209 return insn_oe(insn_in);
210 when OTHERS =>
211 return '0';
212 end case;
213 end;
214
215 -- issue control signals
216 signal control_valid_in : std_ulogic;
217 signal control_valid_out : std_ulogic;
218 signal control_sgl_pipe : std_logic;
219
220 signal gpr_write_valid : std_ulogic;
221 signal gpr_write : gspr_index_t;
222 signal gpr_bypassable : std_ulogic;
223
224 signal update_gpr_write_valid : std_ulogic;
225 signal update_gpr_write_reg : gspr_index_t;
226
227 signal gpr_a_read_valid : std_ulogic;
228 signal gpr_a_read :gspr_index_t;
229 signal gpr_a_bypass : std_ulogic;
230
231 signal gpr_b_read_valid : std_ulogic;
232 signal gpr_b_read : gspr_index_t;
233 signal gpr_b_bypass : std_ulogic;
234
235 signal gpr_c_read_valid : std_ulogic;
236 signal gpr_c_read : gspr_index_t;
237 signal gpr_c_bypass : std_ulogic;
238
239 signal cr_write_valid : std_ulogic;
240 signal cr_bypass : std_ulogic;
241 signal cr_bypass_avail : std_ulogic;
242
243 begin
244 control_0: entity work.control
245 generic map (
246 PIPELINE_DEPTH => 1
247 )
248 port map (
249 clk => clk,
250 rst => rst,
251
252 complete_in => complete_in,
253 valid_in => control_valid_in,
254 busy_in => busy_in,
255 deferred => deferred,
256 flush_in => flush_in,
257 sgl_pipe_in => control_sgl_pipe,
258 stop_mark_in => d_in.stop_mark,
259
260 gpr_write_valid_in => gpr_write_valid,
261 gpr_write_in => gpr_write,
262 gpr_bypassable => gpr_bypassable,
263
264 update_gpr_write_valid => update_gpr_write_valid,
265 update_gpr_write_reg => update_gpr_write_reg,
266
267 gpr_a_read_valid_in => gpr_a_read_valid,
268 gpr_a_read_in => gpr_a_read,
269
270 gpr_b_read_valid_in => gpr_b_read_valid,
271 gpr_b_read_in => gpr_b_read,
272
273 gpr_c_read_valid_in => gpr_c_read_valid,
274 gpr_c_read_in => gpr_c_read,
275
276 cr_read_in => d_in.decode.input_cr,
277 cr_write_in => cr_write_valid,
278 cr_bypass => cr_bypass,
279 cr_bypassable => cr_bypass_avail,
280
281 valid_out => control_valid_out,
282 stall_out => stall_out,
283 stopped_out => stopped_out,
284
285 gpr_bypass_a => gpr_a_bypass,
286 gpr_bypass_b => gpr_b_bypass,
287 gpr_bypass_c => gpr_c_bypass
288 );
289
290 deferred <= r.e.valid and busy_in;
291
292 decode2_0: process(clk)
293 begin
294 if rising_edge(clk) then
295 if rst = '1' or flush_in = '1' or deferred = '0' then
296 if rin.e.valid = '1' then
297 report "execute " & to_hstring(rin.e.nia);
298 end if;
299 r <= rin;
300 end if;
301 end if;
302 end process;
303
304 r_out.read1_reg <= d_in.ispr1 when d_in.decode.input_reg_a = SPR
305 else fpr_to_gspr(insn_fra(d_in.insn)) when d_in.decode.input_reg_a = FRA and HAS_FPU
306 else gpr_to_gspr(insn_ra(d_in.insn));
307 r_out.read2_reg <= d_in.ispr2 when d_in.decode.input_reg_b = SPR
308 else fpr_to_gspr(insn_frb(d_in.insn)) when d_in.decode.input_reg_b = FRB and HAS_FPU
309 else gpr_to_gspr(insn_rb(d_in.insn));
310 r_out.read3_reg <= gpr_to_gspr(insn_rcreg(d_in.insn)) when d_in.decode.input_reg_c = RCR
311 else fpr_to_gspr(insn_frt(d_in.insn)) when d_in.decode.input_reg_c = FRS and HAS_FPU
312 else gpr_to_gspr(insn_rs(d_in.insn));
313
314 c_out.read <= d_in.decode.input_cr;
315
316 decode2_1: process(all)
317 variable v : reg_type;
318 variable mul_a : std_ulogic_vector(63 downto 0);
319 variable mul_b : std_ulogic_vector(63 downto 0);
320 variable decoded_reg_a : decode_input_reg_t;
321 variable decoded_reg_b : decode_input_reg_t;
322 variable decoded_reg_c : decode_input_reg_t;
323 variable decoded_reg_o : decode_output_reg_t;
324 variable length : std_ulogic_vector(3 downto 0);
325 begin
326 v := r;
327
328 v.e := Decode2ToExecute1Init;
329
330 mul_a := (others => '0');
331 mul_b := (others => '0');
332
333 --v.e.input_cr := d_in.decode.input_cr;
334 v.e.output_cr := d_in.decode.output_cr;
335
336 decoded_reg_a := decode_input_reg_a (d_in.decode.input_reg_a, d_in.insn, r_in.read1_data, d_in.ispr1,
337 d_in.nia);
338 decoded_reg_b := decode_input_reg_b (d_in.decode.input_reg_b, d_in.insn, r_in.read2_data, d_in.ispr2);
339 decoded_reg_c := decode_input_reg_c (d_in.decode.input_reg_c, d_in.insn, r_in.read3_data);
340 decoded_reg_o := decode_output_reg (d_in.decode.output_reg_a, d_in.insn, d_in.ispr1);
341
342 r_out.read1_enable <= decoded_reg_a.reg_valid and d_in.valid;
343 r_out.read2_enable <= decoded_reg_b.reg_valid and d_in.valid;
344 r_out.read3_enable <= decoded_reg_c.reg_valid and d_in.valid;
345
346 case d_in.decode.length is
347 when is1B =>
348 length := "0001";
349 when is2B =>
350 length := "0010";
351 when is4B =>
352 length := "0100";
353 when is8B =>
354 length := "1000";
355 when NONE =>
356 length := "0000";
357 end case;
358
359 -- execute unit
360 v.e.nia := d_in.nia;
361 v.e.unit := d_in.decode.unit;
362 v.e.insn_type := d_in.decode.insn_type;
363 v.e.read_reg1 := decoded_reg_a.reg;
364 v.e.read_data1 := decoded_reg_a.data;
365 v.e.bypass_data1 := gpr_a_bypass;
366 v.e.read_reg2 := decoded_reg_b.reg;
367 v.e.read_data2 := decoded_reg_b.data;
368 v.e.bypass_data2 := gpr_b_bypass;
369 v.e.read_data3 := decoded_reg_c.data;
370 v.e.bypass_data3 := gpr_c_bypass;
371 v.e.write_reg := decoded_reg_o.reg;
372 v.e.rc := decode_rc(d_in.decode.rc, d_in.insn);
373 if not (d_in.decode.insn_type = OP_MUL_H32 or d_in.decode.insn_type = OP_MUL_H64) then
374 v.e.oe := decode_oe(d_in.decode.rc, d_in.insn);
375 end if;
376 v.e.cr := c_in.read_cr_data;
377 v.e.bypass_cr := cr_bypass;
378 v.e.xerc := c_in.read_xerc_data;
379 v.e.invert_a := d_in.decode.invert_a;
380 v.e.invert_out := d_in.decode.invert_out;
381 v.e.input_carry := d_in.decode.input_carry;
382 v.e.output_carry := d_in.decode.output_carry;
383 v.e.is_32bit := d_in.decode.is_32bit;
384 v.e.is_signed := d_in.decode.is_signed;
385 if d_in.decode.lr = '1' then
386 v.e.lr := insn_lk(d_in.insn);
387 end if;
388 v.e.insn := d_in.insn;
389 v.e.data_len := length;
390 v.e.byte_reverse := d_in.decode.byte_reverse;
391 v.e.sign_extend := d_in.decode.sign_extend;
392 v.e.update := d_in.decode.update;
393 v.e.reserve := d_in.decode.reserve;
394 v.e.br_pred := d_in.br_pred;
395
396 -- issue control
397 control_valid_in <= d_in.valid;
398 control_sgl_pipe <= d_in.decode.sgl_pipe;
399
400 gpr_write_valid <= decoded_reg_o.reg_valid;
401 gpr_write <= decoded_reg_o.reg;
402 gpr_bypassable <= '0';
403 if EX1_BYPASS and d_in.decode.unit = ALU then
404 gpr_bypassable <= '1';
405 end if;
406 update_gpr_write_valid <= d_in.decode.update;
407 update_gpr_write_reg <= decoded_reg_a.reg;
408 if v.e.lr = '1' then
409 -- there are no instructions that have both update=1 and lr=1
410 update_gpr_write_valid <= '1';
411 update_gpr_write_reg <= fast_spr_num(SPR_LR);
412 end if;
413
414 gpr_a_read_valid <= decoded_reg_a.reg_valid;
415 gpr_a_read <= decoded_reg_a.reg;
416
417 gpr_b_read_valid <= decoded_reg_b.reg_valid;
418 gpr_b_read <= decoded_reg_b.reg;
419
420 gpr_c_read_valid <= decoded_reg_c.reg_valid;
421 gpr_c_read <= decoded_reg_c.reg;
422
423 cr_write_valid <= d_in.decode.output_cr or decode_rc(d_in.decode.rc, d_in.insn);
424 cr_bypass_avail <= '0';
425 if EX1_BYPASS and d_in.decode.unit = ALU then
426 cr_bypass_avail <= d_in.decode.output_cr;
427 end if;
428
429 v.e.valid := control_valid_out;
430
431 if rst = '1' or flush_in = '1' then
432 v.e := Decode2ToExecute1Init;
433 end if;
434
435 -- Update registers
436 rin <= v;
437
438 -- Update outputs
439 e_out <= r.e;
440 end process;
441
442 d2_log: if LOG_LENGTH > 0 generate
443 signal log_data : std_ulogic_vector(9 downto 0);
444 begin
445 dec2_log : process(clk)
446 begin
447 if rising_edge(clk) then
448 log_data <= r.e.nia(5 downto 2) &
449 r.e.valid &
450 stopped_out &
451 stall_out &
452 r.e.bypass_data3 &
453 r.e.bypass_data2 &
454 r.e.bypass_data1;
455 end if;
456 end process;
457 log_out <= log_data;
458 end generate;
459
460 end architecture behaviour;