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