03360575324de4a77e21e22021e243e0c1d5e411
[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 repeat : std_ulogic;
48 end record;
49
50 signal r, rin : reg_type;
51
52 signal deferred : std_ulogic;
53
54 type decode_input_reg_t is record
55 reg_valid : std_ulogic;
56 reg : gspr_index_t;
57 data : std_ulogic_vector(63 downto 0);
58 end record;
59
60 type decode_output_reg_t is record
61 reg_valid : std_ulogic;
62 reg : gspr_index_t;
63 end record;
64
65 function decode_input_reg_a (t : input_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
66 reg_data : std_ulogic_vector(63 downto 0);
67 ispr : gspr_index_t;
68 instr_addr : std_ulogic_vector(63 downto 0))
69 return decode_input_reg_t is
70 begin
71 if t = RA or (t = RA_OR_ZERO and insn_ra(insn_in) /= "00000") then
72 return ('1', gpr_to_gspr(insn_ra(insn_in)), reg_data);
73 elsif t = SPR then
74 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
75 -- If it's all 0, we don't treat it as a dependency as slow SPRs
76 -- operations are single issue.
77 --
78 assert is_fast_spr(ispr) = '1' or ispr = "0000000"
79 report "Decode A says SPR but ISPR is invalid:" &
80 to_hstring(ispr) severity failure;
81 return (is_fast_spr(ispr), ispr, reg_data);
82 elsif t = CIA then
83 return ('0', (others => '0'), instr_addr);
84 elsif HAS_FPU and t = FRA then
85 return ('1', fpr_to_gspr(insn_fra(insn_in)), reg_data);
86 else
87 return ('0', (others => '0'), (others => '0'));
88 end if;
89 end;
90
91 function decode_input_reg_b (t : input_reg_b_t; insn_in : std_ulogic_vector(31 downto 0);
92 reg_data : std_ulogic_vector(63 downto 0);
93 ispr : gspr_index_t) return decode_input_reg_t is
94 variable ret : decode_input_reg_t;
95 begin
96 case t is
97 when RB =>
98 ret := ('1', gpr_to_gspr(insn_rb(insn_in)), reg_data);
99 when FRB =>
100 if HAS_FPU then
101 ret := ('1', fpr_to_gspr(insn_frb(insn_in)), reg_data);
102 else
103 ret := ('0', (others => '0'), (others => '0'));
104 end if;
105 when CONST_UI =>
106 ret := ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_ui(insn_in)), 64)));
107 when CONST_SI =>
108 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)), 64)));
109 when CONST_SI_HI =>
110 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)) & x"0000", 64)));
111 when CONST_UI_HI =>
112 ret := ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_si(insn_in)) & x"0000", 64)));
113 when CONST_LI =>
114 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_li(insn_in)) & "00", 64)));
115 when CONST_BD =>
116 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_bd(insn_in)) & "00", 64)));
117 when CONST_DS =>
118 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_ds(insn_in)) & "00", 64)));
119 when CONST_DQ =>
120 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_dq(insn_in)) & "0000", 64)));
121 when CONST_DXHI4 =>
122 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_dx(insn_in)) & x"0004", 64)));
123 when CONST_M1 =>
124 ret := ('0', (others => '0'), x"FFFFFFFFFFFFFFFF");
125 when CONST_SH =>
126 ret := ('0', (others => '0'), x"00000000000000" & "00" & insn_in(1) & insn_in(15 downto 11));
127 when CONST_SH32 =>
128 ret := ('0', (others => '0'), x"00000000000000" & "000" & insn_in(15 downto 11));
129 when SPR =>
130 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
131 -- If it's all 0, we don't treat it as a dependency as slow SPRs
132 -- operations are single issue.
133 assert is_fast_spr(ispr) = '1' or ispr = "0000000"
134 report "Decode B says SPR but ISPR is invalid:" &
135 to_hstring(ispr) severity failure;
136 ret := (is_fast_spr(ispr), ispr, reg_data);
137 when NONE =>
138 ret := ('0', (others => '0'), (others => '0'));
139 end case;
140
141 return ret;
142 end;
143
144 function decode_input_reg_c (t : input_reg_c_t; insn_in : std_ulogic_vector(31 downto 0);
145 reg_data : std_ulogic_vector(63 downto 0)) return decode_input_reg_t is
146 begin
147 case t is
148 when RS =>
149 return ('1', gpr_to_gspr(insn_rs(insn_in)), reg_data);
150 when RCR =>
151 return ('1', gpr_to_gspr(insn_rcreg(insn_in)), reg_data);
152 when FRS =>
153 if HAS_FPU then
154 return ('1', fpr_to_gspr(insn_frt(insn_in)), reg_data);
155 else
156 return ('0', (others => '0'), (others => '0'));
157 end if;
158 when FRC =>
159 if HAS_FPU then
160 return ('1', fpr_to_gspr(insn_frc(insn_in)), reg_data);
161 else
162 return ('0', (others => '0'), (others => '0'));
163 end if;
164 when NONE =>
165 return ('0', (others => '0'), (others => '0'));
166 end case;
167 end;
168
169 function decode_output_reg (t : output_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
170 ispr : gspr_index_t) return decode_output_reg_t is
171 begin
172 case t is
173 when RT =>
174 return ('1', gpr_to_gspr(insn_rt(insn_in)));
175 when RA =>
176 return ('1', gpr_to_gspr(insn_ra(insn_in)));
177 when FRT =>
178 if HAS_FPU then
179 return ('1', fpr_to_gspr(insn_frt(insn_in)));
180 else
181 return ('0', "0000000");
182 end if;
183 when SPR =>
184 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
185 -- If it's all 0, we don't treat it as a dependency as slow SPRs
186 -- operations are single issue.
187 assert is_fast_spr(ispr) = '1' or ispr = "0000000"
188 report "Decode B says SPR but ISPR is invalid:" &
189 to_hstring(ispr) severity failure;
190 return (is_fast_spr(ispr), ispr);
191 when NONE =>
192 return ('0', "0000000");
193 end case;
194 end;
195
196 function decode_rc (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
197 begin
198 case t is
199 when RC =>
200 return insn_rc(insn_in);
201 when ONE =>
202 return '1';
203 when NONE =>
204 return '0';
205 end case;
206 end;
207
208 -- For now, use "rc" in the decode table to decide whether oe exists.
209 -- This is not entirely correct architecturally: For mulhd and
210 -- mulhdu, the OE field is reserved. It remains to be seen what an
211 -- actual POWER9 does if we set it on those instructions, for now we
212 -- test that further down when assigning to the multiplier oe input.
213 --
214 function decode_oe (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
215 begin
216 case t is
217 when RC =>
218 return insn_oe(insn_in);
219 when OTHERS =>
220 return '0';
221 end case;
222 end;
223
224 -- control signals that are derived from insn_type
225 type mux_select_array_t is array(insn_type_t) of std_ulogic_vector(2 downto 0);
226
227 constant result_select : mux_select_array_t := (
228 OP_AND => "001", -- logical_result
229 OP_OR => "001",
230 OP_XOR => "001",
231 OP_POPCNT => "001",
232 OP_PRTY => "001",
233 OP_CMPB => "001",
234 OP_EXTS => "001",
235 OP_BPERM => "001",
236 OP_BCD => "001",
237 OP_MTSPR => "001",
238 OP_RLC => "010", -- rotator_result
239 OP_RLCL => "010",
240 OP_RLCR => "010",
241 OP_SHL => "010",
242 OP_SHR => "010",
243 OP_EXTSWSLI => "010",
244 OP_MUL_L64 => "011", -- muldiv_result
245 OP_MUL_H64 => "011",
246 OP_MUL_H32 => "011",
247 OP_DIV => "011",
248 OP_DIVE => "011",
249 OP_MOD => "011",
250 OP_CNTZ => "100", -- countzero_result
251 OP_MFSPR => "101", -- spr_result
252 OP_ADDG6S => "111", -- misc_result
253 OP_ISEL => "111",
254 OP_DARN => "111",
255 OP_MFMSR => "111",
256 OP_MFCR => "111",
257 OP_SETB => "111",
258 others => "000" -- default to adder_result
259 );
260
261 constant subresult_select : mux_select_array_t := (
262 OP_MUL_L64 => "000", -- muldiv_result
263 OP_MUL_H64 => "001",
264 OP_MUL_H32 => "010",
265 OP_DIV => "011",
266 OP_DIVE => "011",
267 OP_MOD => "011",
268 OP_ADDG6S => "001", -- misc_result
269 OP_ISEL => "010",
270 OP_DARN => "011",
271 OP_MFMSR => "100",
272 OP_MFCR => "101",
273 OP_SETB => "110",
274 others => "000"
275 );
276
277 -- issue control signals
278 signal control_valid_in : std_ulogic;
279 signal control_valid_out : std_ulogic;
280 signal control_stall_out : std_ulogic;
281 signal control_sgl_pipe : std_logic;
282
283 signal gpr_write_valid : std_ulogic;
284 signal gpr_write : gspr_index_t;
285 signal gpr_bypassable : std_ulogic;
286
287 signal update_gpr_write_valid : std_ulogic;
288 signal update_gpr_write_reg : gspr_index_t;
289
290 signal gpr_a_read_valid : std_ulogic;
291 signal gpr_a_read :gspr_index_t;
292 signal gpr_a_bypass : std_ulogic;
293
294 signal gpr_b_read_valid : std_ulogic;
295 signal gpr_b_read : gspr_index_t;
296 signal gpr_b_bypass : std_ulogic;
297
298 signal gpr_c_read_valid : std_ulogic;
299 signal gpr_c_read : gspr_index_t;
300 signal gpr_c_bypass : std_ulogic;
301
302 signal cr_write_valid : std_ulogic;
303 signal cr_bypass : std_ulogic;
304 signal cr_bypass_avail : std_ulogic;
305
306 begin
307 control_0: entity work.control
308 generic map (
309 PIPELINE_DEPTH => 1
310 )
311 port map (
312 clk => clk,
313 rst => rst,
314
315 complete_in => complete_in,
316 valid_in => control_valid_in,
317 repeated => r.repeat,
318 busy_in => busy_in,
319 deferred => deferred,
320 flush_in => flush_in,
321 sgl_pipe_in => control_sgl_pipe,
322 stop_mark_in => d_in.stop_mark,
323
324 gpr_write_valid_in => gpr_write_valid,
325 gpr_write_in => gpr_write,
326 gpr_bypassable => gpr_bypassable,
327
328 update_gpr_write_valid => update_gpr_write_valid,
329 update_gpr_write_reg => update_gpr_write_reg,
330
331 gpr_a_read_valid_in => gpr_a_read_valid,
332 gpr_a_read_in => gpr_a_read,
333
334 gpr_b_read_valid_in => gpr_b_read_valid,
335 gpr_b_read_in => gpr_b_read,
336
337 gpr_c_read_valid_in => gpr_c_read_valid,
338 gpr_c_read_in => gpr_c_read,
339
340 cr_read_in => d_in.decode.input_cr,
341 cr_write_in => cr_write_valid,
342 cr_bypass => cr_bypass,
343 cr_bypassable => cr_bypass_avail,
344
345 valid_out => control_valid_out,
346 stall_out => control_stall_out,
347 stopped_out => stopped_out,
348
349 gpr_bypass_a => gpr_a_bypass,
350 gpr_bypass_b => gpr_b_bypass,
351 gpr_bypass_c => gpr_c_bypass
352 );
353
354 deferred <= r.e.valid and busy_in;
355
356 decode2_0: process(clk)
357 begin
358 if rising_edge(clk) then
359 if rst = '1' or flush_in = '1' or deferred = '0' then
360 if rin.e.valid = '1' then
361 report "execute " & to_hstring(rin.e.nia);
362 end if;
363 r <= rin;
364 end if;
365 end if;
366 end process;
367
368 c_out.read <= d_in.decode.input_cr;
369
370 decode2_1: process(all)
371 variable v : reg_type;
372 variable mul_a : std_ulogic_vector(63 downto 0);
373 variable mul_b : std_ulogic_vector(63 downto 0);
374 variable decoded_reg_a : decode_input_reg_t;
375 variable decoded_reg_b : decode_input_reg_t;
376 variable decoded_reg_c : decode_input_reg_t;
377 variable decoded_reg_o : decode_output_reg_t;
378 variable length : std_ulogic_vector(3 downto 0);
379 begin
380 v := r;
381
382 v.e := Decode2ToExecute1Init;
383
384 mul_a := (others => '0');
385 mul_b := (others => '0');
386
387 --v.e.input_cr := d_in.decode.input_cr;
388 v.e.output_cr := d_in.decode.output_cr;
389
390 decoded_reg_a := decode_input_reg_a (d_in.decode.input_reg_a, d_in.insn, r_in.read1_data, d_in.ispr1,
391 d_in.nia);
392 decoded_reg_b := decode_input_reg_b (d_in.decode.input_reg_b, d_in.insn, r_in.read2_data, d_in.ispr2);
393 decoded_reg_c := decode_input_reg_c (d_in.decode.input_reg_c, d_in.insn, r_in.read3_data);
394 decoded_reg_o := decode_output_reg (d_in.decode.output_reg_a, d_in.insn, d_in.ispr1);
395
396 if d_in.decode.repeat /= NONE then
397 v.e.repeat := '1';
398 v.e.second := r.repeat;
399 case d_in.decode.repeat is
400 when DRSE =>
401 -- do RS|1,RS for LE; RS,RS|1 for BE
402 if r.repeat = d_in.big_endian then
403 decoded_reg_c.reg(0) := '1';
404 end if;
405 when DRTE =>
406 -- do RT|1,RT for LE; RT,RT|1 for BE
407 if r.repeat = d_in.big_endian then
408 decoded_reg_o.reg(0) := '1';
409 end if;
410 when DUPD =>
411 -- update-form loads, 2nd instruction writes RA
412 if r.repeat = '1' then
413 decoded_reg_o.reg := decoded_reg_a.reg;
414 end if;
415 when others =>
416 end case;
417 end if;
418
419 r_out.read1_enable <= decoded_reg_a.reg_valid and d_in.valid;
420 r_out.read1_reg <= decoded_reg_a.reg;
421 r_out.read2_enable <= decoded_reg_b.reg_valid and d_in.valid;
422 r_out.read2_reg <= decoded_reg_b.reg;
423 r_out.read3_enable <= decoded_reg_c.reg_valid and d_in.valid;
424 r_out.read3_reg <= decoded_reg_c.reg;
425
426 case d_in.decode.length is
427 when is1B =>
428 length := "0001";
429 when is2B =>
430 length := "0010";
431 when is4B =>
432 length := "0100";
433 when is8B =>
434 length := "1000";
435 when NONE =>
436 length := "0000";
437 end case;
438
439 -- execute unit
440 v.e.nia := d_in.nia;
441 v.e.unit := d_in.decode.unit;
442 v.e.fac := d_in.decode.facility;
443 v.e.insn_type := d_in.decode.insn_type;
444 v.e.read_reg1 := decoded_reg_a.reg;
445 v.e.read_data1 := decoded_reg_a.data;
446 v.e.bypass_data1 := gpr_a_bypass;
447 v.e.read_reg2 := decoded_reg_b.reg;
448 v.e.read_data2 := decoded_reg_b.data;
449 v.e.bypass_data2 := gpr_b_bypass;
450 v.e.read_data3 := decoded_reg_c.data;
451 v.e.bypass_data3 := gpr_c_bypass;
452 v.e.write_reg := decoded_reg_o.reg;
453 v.e.write_reg_enable := decoded_reg_o.reg_valid;
454 v.e.rc := decode_rc(d_in.decode.rc, d_in.insn);
455 if not (d_in.decode.insn_type = OP_MUL_H32 or d_in.decode.insn_type = OP_MUL_H64) then
456 v.e.oe := decode_oe(d_in.decode.rc, d_in.insn);
457 end if;
458 v.e.cr := c_in.read_cr_data;
459 v.e.bypass_cr := cr_bypass;
460 v.e.xerc := c_in.read_xerc_data;
461 v.e.invert_a := d_in.decode.invert_a;
462 v.e.addm1 := '0';
463 if d_in.decode.insn_type = OP_BC or d_in.decode.insn_type = OP_BCREG then
464 -- add -1 to CTR
465 v.e.addm1 := '1';
466 if d_in.insn(23) = '1' or
467 (d_in.decode.insn_type = OP_BCREG and d_in.insn(10) = '0') then
468 -- don't write decremented CTR if BO(2) = 1 or bcctr
469 v.e.write_reg_enable := '0';
470 end if;
471 end if;
472 v.e.invert_out := d_in.decode.invert_out;
473 v.e.input_carry := d_in.decode.input_carry;
474 v.e.output_carry := d_in.decode.output_carry;
475 v.e.is_32bit := d_in.decode.is_32bit;
476 v.e.is_signed := d_in.decode.is_signed;
477 if d_in.decode.lr = '1' then
478 v.e.lr := insn_lk(d_in.insn);
479 end if;
480 v.e.insn := d_in.insn;
481 v.e.data_len := length;
482 v.e.byte_reverse := d_in.decode.byte_reverse;
483 v.e.sign_extend := d_in.decode.sign_extend;
484 v.e.update := d_in.decode.update;
485 v.e.reserve := d_in.decode.reserve;
486 v.e.br_pred := d_in.br_pred;
487 v.e.result_sel := result_select(d_in.decode.insn_type);
488 v.e.sub_select := subresult_select(d_in.decode.insn_type);
489
490 -- issue control
491 control_valid_in <= d_in.valid;
492 control_sgl_pipe <= d_in.decode.sgl_pipe;
493
494 gpr_write_valid <= v.e.write_reg_enable;
495 gpr_write <= decoded_reg_o.reg;
496 gpr_bypassable <= '0';
497 if EX1_BYPASS and d_in.decode.unit = ALU then
498 gpr_bypassable <= '1';
499 end if;
500
501 update_gpr_write_valid <= v.e.lr;
502 update_gpr_write_reg <= fast_spr_num(SPR_LR);
503
504 gpr_a_read_valid <= decoded_reg_a.reg_valid;
505 gpr_a_read <= decoded_reg_a.reg;
506
507 gpr_b_read_valid <= decoded_reg_b.reg_valid;
508 gpr_b_read <= decoded_reg_b.reg;
509
510 gpr_c_read_valid <= decoded_reg_c.reg_valid;
511 gpr_c_read <= decoded_reg_c.reg;
512
513 cr_write_valid <= d_in.decode.output_cr or decode_rc(d_in.decode.rc, d_in.insn);
514 cr_bypass_avail <= '0';
515 if EX1_BYPASS and d_in.decode.unit = ALU then
516 cr_bypass_avail <= d_in.decode.output_cr;
517 end if;
518
519 v.e.valid := control_valid_out;
520 if control_valid_out = '1' then
521 v.repeat := v.e.repeat and not r.repeat;
522 end if;
523
524 stall_out <= control_stall_out or v.repeat;
525
526 if rst = '1' or flush_in = '1' then
527 v.e := Decode2ToExecute1Init;
528 v.repeat := '0';
529 end if;
530
531 -- Update registers
532 rin <= v;
533
534 -- Update outputs
535 e_out <= r.e;
536 end process;
537
538 d2_log: if LOG_LENGTH > 0 generate
539 signal log_data : std_ulogic_vector(9 downto 0);
540 begin
541 dec2_log : process(clk)
542 begin
543 if rising_edge(clk) then
544 log_data <= r.e.nia(5 downto 2) &
545 r.e.valid &
546 stopped_out &
547 stall_out &
548 r.e.bypass_data3 &
549 r.e.bypass_data2 &
550 r.e.bypass_data1;
551 end if;
552 end process;
553 log_out <= log_data;
554 end generate;
555
556 end architecture behaviour;