2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
6 use work.decode_types.all;
9 use work.crhelpers.all;
10 use work.insn_helpers.all;
11 use work.ppc_fx_insns.all;
15 EX1_BYPASS : boolean := true;
16 HAS_FPU : boolean := true;
17 -- Non-zero to enable log data collection
18 LOG_LENGTH : natural := 0
25 flush_out : out std_ulogic;
26 busy_out : out std_ulogic;
28 e_in : in Decode2ToExecute1Type;
29 l_in : in Loadstore1ToExecute1Type;
30 fp_in : in FPUToExecute1Type;
32 ext_irq_in : std_ulogic;
35 l_out : out Execute1ToLoadstore1Type;
36 f_out : out Execute1ToFetch1Type;
37 fp_out : out Execute1ToFPUType;
39 e_out : out Execute1ToWritebackType;
40 bypass_data : out bypass_data_t;
41 bypass_cr_data : out cr_bypass_data_t;
43 dbg_msr_out : out std_ulogic_vector(63 downto 0);
45 icache_inval : out std_ulogic;
46 terminate_out : out std_ulogic;
48 log_out : out std_ulogic_vector(14 downto 0);
49 log_rd_addr : out std_ulogic_vector(31 downto 0);
50 log_rd_data : in std_ulogic_vector(63 downto 0);
51 log_wr_addr : in std_ulogic_vector(31 downto 0)
55 architecture behaviour of execute1 is
56 type reg_type is record
57 e : Execute1ToWritebackType;
58 cur_instr : Decode2ToExecute1Type;
60 terminate: std_ulogic;
61 fp_exception_next : std_ulogic;
62 trace_next : std_ulogic;
63 prev_op : insn_type_t;
64 next_lr : std_ulogic_vector(63 downto 0);
65 br_taken : std_ulogic;
66 mul_in_progress : std_ulogic;
67 mul_finish : std_ulogic;
68 div_in_progress : std_ulogic;
69 cntz_in_progress : std_ulogic;
70 last_nia : std_ulogic_vector(63 downto 0);
71 redirect : std_ulogic;
73 taken_br : std_ulogic;
76 vector : integer range 0 to 16#fff#;
77 br_offset : std_ulogic_vector(63 downto 0);
78 redir_mode : std_ulogic_vector(3 downto 0);
79 log_addr_spr : std_ulogic_vector(31 downto 0);
81 constant reg_type_init : reg_type :=
82 (e => Execute1ToWritebackInit,
83 cur_instr => Decode2ToExecute1Init,
84 busy => '0', terminate => '0',
85 fp_exception_next => '0', trace_next => '0', prev_op => OP_ILLEGAL, br_taken => '0',
86 mul_in_progress => '0', mul_finish => '0', div_in_progress => '0', cntz_in_progress => '0',
87 next_lr => (others => '0'), last_nia => (others => '0'),
88 redirect => '0', abs_br => '0', taken_br => '0', br_last => '0', do_intr => '0', vector => 0,
89 br_offset => (others => '0'), redir_mode => "0000",
90 others => (others => '0'));
92 signal r, rin : reg_type;
94 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
95 signal cr_in : std_ulogic_vector(31 downto 0);
96 signal xerc_in : xer_common_t;
98 signal valid_in : std_ulogic;
99 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
100 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
101 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
102 signal rot_sign_ext: std_ulogic;
103 signal rotator_result: std_ulogic_vector(63 downto 0);
104 signal rotator_carry: std_ulogic;
105 signal logical_result: std_ulogic_vector(63 downto 0);
106 signal countzero_result: std_ulogic_vector(63 downto 0);
107 signal alu_result: std_ulogic_vector(63 downto 0);
108 signal adder_result: std_ulogic_vector(63 downto 0);
109 signal misc_result: std_ulogic_vector(63 downto 0);
110 signal muldiv_result: std_ulogic_vector(63 downto 0);
111 signal spr_result: std_ulogic_vector(63 downto 0);
112 signal result_mux_sel: std_ulogic_vector(2 downto 0);
113 signal sub_mux_sel: std_ulogic_vector(2 downto 0);
114 signal next_nia : std_ulogic_vector(63 downto 0);
115 signal current: Decode2ToExecute1Type;
117 signal carry_32 : std_ulogic;
118 signal carry_64 : std_ulogic;
119 signal overflow_32 : std_ulogic;
120 signal overflow_64 : std_ulogic;
122 signal trapval : std_ulogic_vector(4 downto 0);
124 signal write_cr_mask : std_ulogic_vector(7 downto 0);
125 signal write_cr_data : std_ulogic_vector(31 downto 0);
128 signal x_to_multiply: MultiplyInputType;
129 signal multiply_to_x: MultiplyOutputType;
132 signal x_to_divider: Execute1ToDividerType;
133 signal divider_to_x: DividerToExecute1Type;
135 -- random number generator signals
136 signal random_raw : std_ulogic_vector(63 downto 0);
137 signal random_cond : std_ulogic_vector(63 downto 0);
138 signal random_err : std_ulogic;
140 -- signals for logging
141 signal exception_log : std_ulogic;
142 signal irq_valid_log : std_ulogic;
144 type privilege_level is (USER, SUPER);
145 type op_privilege_array is array(insn_type_t) of privilege_level;
146 constant op_privilege: op_privilege_array := (
155 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
158 if op_privilege(op) = SUPER then
160 elsif op = OP_MFSPR or op = OP_MTSPR then
161 return insn(20) = '1';
167 procedure set_carry(e: inout Execute1ToWritebackType;
168 carry32 : in std_ulogic;
169 carry : in std_ulogic) is
171 e.xerc.ca32 := carry32;
175 procedure set_ov(e: inout Execute1ToWritebackType;
177 ov32 : in std_ulogic) is
186 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
187 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
189 return (ca xor msb_r) and not (msb_a xor msb_b);
192 function decode_input_carry(ic : carry_in_t;
193 xerc : xer_common_t) return std_ulogic is
207 function msr_copy(msr: std_ulogic_vector(63 downto 0))
208 return std_ulogic_vector is
209 variable msr_out: std_ulogic_vector(63 downto 0);
212 -- Defined MSR bits are classified as either full func-
213 -- tion or partial function. Full function MSR bits are
214 -- saved in SRR1 or HSRR1 when an interrupt other
215 -- than a System Call Vectored interrupt occurs and
216 -- restored by rfscv, rfid, or hrfid, while partial func-
217 -- tion MSR bits are not saved or restored.
218 -- Full function MSR bits lie in the range 0:32, 37:41, and
219 -- 48:63, and partial function MSR bits lie in the range
220 -- 33:36 and 42:47. (Note this is IBM bit numbering).
221 msr_out := (others => '0');
222 msr_out(63 downto 31) := msr(63 downto 31);
223 msr_out(26 downto 22) := msr(26 downto 22);
224 msr_out(15 downto 0) := msr(15 downto 0);
228 -- Tell vivado to keep the hierarchy for the random module so that the
229 -- net names in the xdc file match.
230 attribute keep_hierarchy : string;
231 attribute keep_hierarchy of random_0 : label is "yes";
235 rotator_0: entity work.rotator
239 shift => b_in(6 downto 0),
241 is_32bit => e_in.is_32bit,
242 right_shift => right_shift,
243 arith => e_in.is_signed,
244 clear_left => rot_clear_left,
245 clear_right => rot_clear_right,
246 sign_ext_rs => rot_sign_ext,
247 result => rotator_result,
248 carry_out => rotator_carry
251 logical_0: entity work.logical
255 op => e_in.insn_type,
256 invert_in => e_in.invert_a,
257 invert_out => e_in.invert_out,
258 result => logical_result,
259 datalen => e_in.data_len
262 countzero_0: entity work.zero_counter
266 count_right => e_in.insn(10),
267 is_32bit => e_in.is_32bit,
268 result => countzero_result
271 multiply_0: entity work.multiply
274 m_in => x_to_multiply,
275 m_out => multiply_to_x
278 divider_0: entity work.divider
282 d_in => x_to_divider,
283 d_out => divider_to_x
286 random_0: entity work.random
294 dbg_msr_out <= ctrl.msr;
295 log_rd_addr <= r.log_addr_spr;
297 a_in <= e_in.read_data1;
298 b_in <= e_in.read_data2;
299 c_in <= e_in.read_data3;
302 -- XER forwarding. To avoid having to track XER hazards, we use
303 -- the previously latched value. Since the XER common bits
304 -- (SO, OV[32] and CA[32]) are only modified by instructions that are
305 -- handled here, we can just forward the result being sent to
307 xerc_in <= r.e.xerc when r.e.write_xerc_enable = '1' or r.busy = '1' else e_in.xerc;
309 busy_out <= l_in.busy or r.busy or fp_in.busy;
310 valid_in <= e_in.valid and not busy_out;
312 terminate_out <= r.terminate;
314 current <= e_in when r.busy = '0' else r.cur_instr;
317 with current.result_sel select alu_result <=
318 adder_result when "000",
319 logical_result when "001",
320 rotator_result when "010",
321 muldiv_result when "011",
322 countzero_result when "100",
323 spr_result when "101",
325 misc_result when others;
327 execute1_0: process(clk)
329 if rising_edge(clk) then
332 ctrl.tb <= (others => '0');
333 ctrl.dec <= (others => '0');
334 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
335 ctrl.irq_state <= WRITE_SRR0;
339 if valid_in = '1' then
340 report "execute " & to_hstring(e_in.nia) & " op=" & insn_type_t'image(e_in.insn_type) &
341 " wr=" & to_hstring(rin.e.write_reg) & " we=" & std_ulogic'image(rin.e.write_enable) &
342 " tag=" & integer'image(rin.e.instr_tag.tag) & std_ulogic'image(rin.e.instr_tag.valid);
348 -- Data path for integer instructions
349 execute1_dp: process(all)
350 variable a_inv : std_ulogic_vector(63 downto 0);
351 variable b_or_m1 : std_ulogic_vector(63 downto 0);
352 variable sum_with_carry : std_ulogic_vector(64 downto 0);
353 variable sign1, sign2 : std_ulogic;
354 variable abs1, abs2 : signed(63 downto 0);
355 variable addend : std_ulogic_vector(127 downto 0);
356 variable addg6s : std_ulogic_vector(63 downto 0);
357 variable crbit : integer range 0 to 31;
358 variable isel_result : std_ulogic_vector(63 downto 0);
359 variable darn : std_ulogic_vector(63 downto 0);
360 variable setb_result : std_ulogic_vector(63 downto 0);
361 variable mfcr_result : std_ulogic_vector(63 downto 0);
362 variable lo, hi : integer;
363 variable l : std_ulogic;
364 variable zerohi, zerolo : std_ulogic;
365 variable msb_a, msb_b : std_ulogic;
366 variable a_lt : std_ulogic;
367 variable a_lt_lo : std_ulogic;
368 variable a_lt_hi : std_ulogic;
369 variable newcrf : std_ulogic_vector(3 downto 0);
370 variable bf, bfa : std_ulogic_vector(2 downto 0);
371 variable crnum : crnum_t;
372 variable scrnum : crnum_t;
373 variable cr_operands : std_ulogic_vector(1 downto 0);
374 variable crresult : std_ulogic;
375 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
376 variable btnum : integer range 0 to 3;
377 variable banum, bbnum : integer range 0 to 31;
378 variable j : integer;
381 if e_in.invert_a = '0' then
386 if e_in.addm1 = '0' then
389 b_or_m1 := (others => '1');
391 sum_with_carry := ppc_adde(a_inv, b_or_m1,
392 decode_input_carry(e_in.input_carry, xerc_in));
393 adder_result <= sum_with_carry(63 downto 0);
394 carry_32 <= sum_with_carry(32) xor a_inv(32) xor b_in(32);
395 carry_64 <= sum_with_carry(64);
396 overflow_32 <= calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31));
397 overflow_64 <= calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63));
399 -- signals to multiply and divide units
402 if e_in.is_signed = '1' then
403 if e_in.is_32bit = '1' then
411 -- take absolute values
413 abs1 := signed(a_in);
415 abs1 := - signed(a_in);
418 abs2 := signed(b_in);
420 abs2 := - signed(b_in);
423 -- Interface to multiply and divide units
424 x_to_divider.is_signed <= e_in.is_signed;
425 x_to_divider.is_32bit <= e_in.is_32bit;
426 x_to_divider.is_extended <= '0';
427 x_to_divider.is_modulus <= '0';
428 if e_in.insn_type = OP_MOD then
429 x_to_divider.is_modulus <= '1';
432 addend := (others => '0');
433 if e_in.insn(26) = '0' then
434 -- integer multiply-add, major op 4 (if it is a multiply)
435 addend(63 downto 0) := c_in;
436 if e_in.is_signed = '1' then
437 addend(127 downto 64) := (others => c_in(63));
440 if (sign1 xor sign2) = '1' then
441 addend := not addend;
444 x_to_multiply.is_32bit <= e_in.is_32bit;
445 x_to_multiply.not_result <= sign1 xor sign2;
446 x_to_multiply.addend <= addend;
447 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
448 if e_in.is_32bit = '0' then
450 x_to_multiply.data1 <= std_ulogic_vector(abs1);
451 x_to_multiply.data2 <= std_ulogic_vector(abs2);
452 if e_in.insn_type = OP_DIVE then
453 x_to_divider.is_extended <= '1';
455 x_to_divider.dividend <= std_ulogic_vector(abs1);
456 x_to_divider.divisor <= std_ulogic_vector(abs2);
459 x_to_multiply.data1 <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
460 x_to_multiply.data2 <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
461 x_to_divider.is_extended <= '0';
462 if e_in.insn_type = OP_DIVE then -- extended forms
463 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
465 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
467 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
470 case current.sub_select(1 downto 0) is
472 muldiv_result <= multiply_to_x.result(63 downto 0);
474 muldiv_result <= multiply_to_x.result(127 downto 64);
476 muldiv_result <= multiply_to_x.result(63 downto 32) &
477 multiply_to_x.result(63 downto 32);
479 muldiv_result <= divider_to_x.write_reg_data;
482 -- Compute misc_result
483 case current.sub_select is
485 misc_result <= (others => '0');
488 addg6s := (others => '0');
489 for i in 0 to 14 loop
492 if (a_in(hi) xor b_in(hi) xor sum_with_carry(hi)) = '0' then
493 addg6s(lo + 3 downto lo) := "0110";
496 if sum_with_carry(64) = '0' then
497 addg6s(63 downto 60) := "0110";
499 misc_result <= addg6s;
502 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
503 if cr_in(31-crbit) = '1' then
508 misc_result <= isel_result;
511 darn := (others => '1');
512 if random_err = '0' then
513 case e_in.insn(17 downto 16) is
515 darn := x"00000000" & random_cond(31 downto 0);
525 misc_result <= ctrl.msr;
527 if e_in.insn(20) = '0' then
529 mfcr_result := x"00000000" & cr_in;
532 crnum := fxm_to_num(insn_fxm(e_in.insn));
533 mfcr_result := (others => '0');
538 mfcr_result(hi downto lo) := cr_in(hi downto lo);
542 misc_result <= mfcr_result;
545 bfa := insn_bfa(e_in.insn);
546 crbit := to_integer(unsigned(bfa)) * 4;
547 setb_result := (others => '0');
548 if cr_in(31 - crbit) = '1' then
549 setb_result := (others => '1');
550 elsif cr_in(30 - crbit) = '1' then
551 setb_result(0) := '1';
553 misc_result <= setb_result;
555 misc_result <= (others => '0');
558 -- compute comparison results
559 -- Note, we have done RB - RA, not RA - RB
560 if e_in.insn_type = OP_CMP then
561 l := insn_l(e_in.insn);
563 l := not e_in.is_32bit;
565 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
566 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
567 if zerolo = '1' and (l = '0' or zerohi = '1') then
573 if unsigned(a_in(30 downto 0)) < unsigned(b_in(30 downto 0)) then
576 if unsigned(a_in(62 downto 31)) < unsigned(b_in(62 downto 31)) then
583 a_lt := a_lt_hi or (zerohi and (a_in(31) xnor b_in(31)) and a_lt_lo);
590 if msb_a /= msb_b then
591 -- Comparison is clear from MSB difference.
592 -- for signed, 0 is greater; for unsigned, 1 is greater
593 trapval <= msb_a & msb_b & '0' & msb_b & msb_a;
595 -- MSBs are equal, so signed and unsigned comparisons give the
597 trapval <= a_lt & not a_lt & '0' & a_lt & not a_lt;
602 bf := insn_bf(e_in.insn);
603 crnum := to_integer(unsigned(bf));
604 newcrf := (others => '0');
605 case current.sub_select is
607 -- CMP and CMPL instructions
608 if e_in.is_signed = '1' then
609 newcrf := trapval(4 downto 2) & xerc_in.so;
611 newcrf := trapval(1 downto 0) & trapval(2) & xerc_in.so;
614 newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
616 newcrf := ppc_cmpeqb(a_in, b_in);
618 if current.insn(1) = '1' then
619 -- CR logical instructions
620 j := (7 - crnum) * 4;
621 newcrf := cr_in(j + 3 downto j);
622 bt := insn_bt(e_in.insn);
623 ba := insn_ba(e_in.insn);
624 bb := insn_bb(e_in.insn);
625 btnum := 3 - to_integer(unsigned(bt(1 downto 0)));
626 banum := 31 - to_integer(unsigned(ba));
627 bbnum := 31 - to_integer(unsigned(bb));
628 -- Bits 6-9 of the instruction word give the truth table
629 -- of the requested logical operation
630 cr_operands := cr_in(banum) & cr_in(bbnum);
631 crresult := e_in.insn(6 + to_integer(unsigned(cr_operands)));
634 newcrf(i) := crresult;
639 bfa := insn_bfa(e_in.insn);
640 scrnum := to_integer(unsigned(bfa));
641 j := (7 - scrnum) * 4;
642 newcrf := cr_in(j + 3 downto j);
646 newcrf := xerc_in.ov & xerc_in.ca & xerc_in.ov32 & xerc_in.ca32;
649 if current.insn_type = OP_MTCRF then
650 if e_in.insn(20) = '0' then
652 write_cr_mask <= insn_fxm(e_in.insn);
654 -- mtocrf: We require one hot priority encoding here
655 crnum := fxm_to_num(insn_fxm(e_in.insn));
656 write_cr_mask <= num_to_fxm(crnum);
658 write_cr_data <= c_in(31 downto 0);
660 write_cr_mask <= num_to_fxm(crnum);
661 write_cr_data <= newcrf & newcrf & newcrf & newcrf &
662 newcrf & newcrf & newcrf & newcrf;
667 execute1_1: process(all)
668 variable v : reg_type;
669 variable lo, hi : integer;
670 variable sh, mb, me : std_ulogic_vector(5 downto 0);
671 variable bo, bi : std_ulogic_vector(4 downto 0);
672 variable overflow : std_ulogic;
673 variable lv : Execute1ToLoadstore1Type;
674 variable irq_valid : std_ulogic;
675 variable exception : std_ulogic;
676 variable exception_nextpc : std_ulogic;
677 variable illegal : std_ulogic;
678 variable is_branch : std_ulogic;
679 variable is_direct_branch : std_ulogic;
680 variable taken_branch : std_ulogic;
681 variable abs_branch : std_ulogic;
682 variable spr_val : std_ulogic_vector(63 downto 0);
683 variable do_trace : std_ulogic;
684 variable hold_wr_data : std_ulogic;
685 variable f : Execute1ToFetch1Type;
686 variable fv : Execute1ToFPUType;
689 is_direct_branch := '0';
695 v.e := Execute1ToWritebackInit;
700 v.br_offset := (others => '0');
701 v.redir_mode := ctrl.msr(MSR_IR) & not ctrl.msr(MSR_PR) &
702 not ctrl.msr(MSR_LE) & not ctrl.msr(MSR_SF);
707 lv := Execute1ToLoadstore1Init;
708 fv := Execute1ToFPUInit;
710 x_to_multiply.valid <= '0';
711 x_to_divider.valid <= '0';
712 v.mul_in_progress := '0';
713 v.div_in_progress := '0';
714 v.cntz_in_progress := '0';
717 spr_result <= (others => '0');
718 spr_val := (others => '0');
721 -- FIXME: run at 512MHz not core freq
722 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
723 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
726 if ctrl.msr(MSR_EE) = '1' then
727 if ctrl.dec(63) = '1' then
729 report "IRQ valid: DEC";
731 elsif ext_irq_in = '1' then
733 report "IRQ valid: External";
742 -- Next insn adder used in a couple of places
743 next_nia <= std_ulogic_vector(unsigned(e_in.nia) + 4);
745 -- rotator control signals
746 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
747 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
748 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
749 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
751 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
752 ctrl_tmp.irq_state <= WRITE_SRR0;
755 exception_nextpc := '0';
756 v.e.exc_write_enable := '0';
757 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
758 if valid_in = '1' then
759 v.e.exc_write_data := e_in.nia;
760 v.last_nia := e_in.nia;
762 v.e.exc_write_data := r.last_nia;
765 v.e.mode_32bit := not ctrl.msr(MSR_SF);
766 v.e.instr_tag := current.instr_tag;
768 do_trace := valid_in and ctrl.msr(MSR_SE);
769 if valid_in = '1' then
770 v.prev_op := e_in.insn_type;
773 -- Determine if there is any exception to be taken
774 -- before/instead of executing this instruction
775 if valid_in = '1' and e_in.second = '0' then
776 if HAS_FPU and r.fp_exception_next = '1' then
777 -- This is used for FP-type program interrupts that
778 -- become pending due to MSR[FE0,FE1] changing from 00 to non-zero.
781 ctrl_tmp.srr1(63 - 43) <= '1';
782 ctrl_tmp.srr1(63 - 47) <= '1';
783 elsif r.trace_next = '1' then
784 -- Generate a trace interrupt rather than executing the next instruction
785 -- or taking any asynchronous interrupt
788 ctrl_tmp.srr1(63 - 33) <= '1';
789 if r.prev_op = OP_LOAD or r.prev_op = OP_ICBI or r.prev_op = OP_ICBT or
790 r.prev_op = OP_DCBT or r.prev_op = OP_DCBST or r.prev_op = OP_DCBF then
791 ctrl_tmp.srr1(63 - 35) <= '1';
792 elsif r.prev_op = OP_STORE or r.prev_op = OP_DCBZ or r.prev_op = OP_DCBTST then
793 ctrl_tmp.srr1(63 - 36) <= '1';
796 elsif irq_valid = '1' then
797 -- Don't deliver the interrupt until we have a valid instruction
798 -- coming in, so we have a valid NIA to put in SRR0.
801 elsif ctrl.msr(MSR_PR) = '1' and instr_is_privileged(e_in.insn_type, e_in.insn) then
802 -- generate a program interrupt
805 -- set bit 45 to indicate privileged instruction type interrupt
806 ctrl_tmp.srr1(63 - 45) <= '1';
807 report "privileged instruction";
809 elsif not HAS_FPU and e_in.fac = FPU then
810 -- make lfd/stfd/lfs/stfs etc. illegal in no-FPU implementations
813 elsif HAS_FPU and ctrl.msr(MSR_FP) = '0' and e_in.fac = FPU then
814 -- generate a floating-point unavailable interrupt
817 report "FP unavailable interrupt";
821 if valid_in = '1' and exception = '0' and illegal = '0' and e_in.unit = ALU then
823 v.next_lr := next_nia;
826 case_0: case e_in.insn_type is
829 -- we need two cycles to write srr0 and 1
830 -- will need more when we have to write HEIR
833 -- check bit 1 of the instruction is 1 so we know this is sc;
834 -- 0 would mean scv, so generate an illegal instruction interrupt
835 -- we need two cycles to write srr0 and 1
836 if e_in.insn(1) = '1' then
838 exception_nextpc := '1';
845 -- check bits 1-10 of the instruction to make sure it's attn
846 -- if not then it is illegal
847 if e_in.insn(10 downto 1) = "0100000000" then
853 when OP_NOP | OP_DCBF | OP_DCBST | OP_DCBT | OP_DCBTST | OP_ICBT =>
856 if e_in.output_carry = '1' then
857 if e_in.input_carry /= OV then
858 set_carry(v.e, carry_32, carry_64);
860 v.e.xerc.ov := carry_64;
861 v.e.xerc.ov32 := carry_32;
864 if e_in.oe = '1' then
865 set_ov(v.e, overflow_64, overflow_32);
869 -- trap instructions (tw, twi, td, tdi)
871 -- set bit 46 to say trap occurred
872 ctrl_tmp.srr1(63 - 46) <= '1';
873 if or (trapval and insn_to(e_in.insn)) = '1' then
874 -- generate trap-type program interrupt
881 when OP_AND | OP_OR | OP_XOR | OP_POPCNT | OP_PRTY | OP_CMPB | OP_EXTS |
887 is_direct_branch := '1';
888 abs_branch := e_in.br_abs;
889 if ctrl.msr(MSR_BE) = '1' then
892 when OP_BC | OP_BCREG =>
894 -- for OP_BCREG, read_data2 is target register (CTR, LR or TAR)
895 -- If this instruction updates both CTR and LR, then it is
896 -- doubled; the first instruction decrements CTR and determines
897 -- whether the branch is taken, and the second does the
898 -- redirect and the LR update.
899 bo := insn_bo(e_in.insn);
900 bi := insn_bi(e_in.insn);
901 if e_in.second = '0' then
902 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
904 taken_branch := r.br_taken;
906 v.br_taken := taken_branch;
907 abs_branch := e_in.br_abs;
908 if e_in.repeat = '0' or e_in.second = '1' then
910 if e_in.insn_type = OP_BC then
911 is_direct_branch := '1';
913 if ctrl.msr(MSR_BE) = '1' then
919 v.redir_mode := (a_in(MSR_IR) or a_in(MSR_PR)) & not a_in(MSR_PR) &
920 not a_in(MSR_LE) & not a_in(MSR_SF);
921 -- Can't use msr_copy here because the partial function MSR
922 -- bits should be left unchanged, not zeroed.
923 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
924 ctrl_tmp.msr(26 downto 22) <= a_in(26 downto 22);
925 ctrl_tmp.msr(15 downto 0) <= a_in(15 downto 0);
926 if a_in(MSR_PR) = '1' then
927 ctrl_tmp.msr(MSR_EE) <= '1';
928 ctrl_tmp.msr(MSR_IR) <= '1';
929 ctrl_tmp.msr(MSR_DR) <= '1';
931 -- mark this as a branch so CFAR gets updated
936 v.fp_exception_next := fp_in.exception and
937 (a_in(MSR_FE0) or a_in(MSR_FE1));
943 v.cntz_in_progress := '1';
951 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
952 "=" & to_hstring(a_in);
953 if is_fast_spr(e_in.read_reg1) = '1' then
955 if decode_spr_num(e_in.insn) = SPR_XER then
956 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
957 spr_val(63 downto 32) := (others => '0');
958 spr_val(63-32) := xerc_in.so;
959 spr_val(63-33) := xerc_in.ov;
960 spr_val(63-34) := xerc_in.ca;
961 spr_val(63-35 downto 63-43) := "000000000";
962 spr_val(63-44) := xerc_in.ov32;
963 spr_val(63-45) := xerc_in.ca32;
967 case decode_spr_num(e_in.insn) is
971 spr_val(63 downto 32) := (others => '0');
972 spr_val(31 downto 0) := ctrl.tb(63 downto 32);
976 spr_val := ctrl.cfar;
978 spr_val(63 downto 32) := (others => '0');
979 spr_val(31 downto 0) := PVR_MICROWATT;
980 when 724 => -- LOG_ADDR SPR
981 spr_val := log_wr_addr & r.log_addr_spr;
982 when 725 => -- LOG_DATA SPR
983 spr_val := log_rd_data;
984 v.log_addr_spr := std_ulogic_vector(unsigned(r.log_addr_spr) + 1);
986 -- mfspr from unimplemented SPRs should be a nop in
987 -- supervisor mode and a program interrupt for user mode
988 if is_fast_spr(e_in.read_reg1) = '0' and ctrl.msr(MSR_PR) = '1' then
993 spr_result <= spr_val;
998 if e_in.insn(16) = '1' then
999 -- just update EE and RI
1000 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
1001 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
1003 -- Architecture says to leave out bits 3 (HV), 51 (ME)
1004 -- and 63 (LE) (IBM bit numbering)
1005 if e_in.is_32bit = '0' then
1006 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
1007 ctrl_tmp.msr(59 downto 32) <= c_in(59 downto 32);
1009 ctrl_tmp.msr(31 downto 13) <= c_in(31 downto 13);
1010 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
1011 if c_in(MSR_PR) = '1' then
1012 ctrl_tmp.msr(MSR_EE) <= '1';
1013 ctrl_tmp.msr(MSR_IR) <= '1';
1014 ctrl_tmp.msr(MSR_DR) <= '1';
1017 v.fp_exception_next := fp_in.exception and
1018 (c_in(MSR_FE0) or c_in(MSR_FE1));
1022 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
1023 "=" & to_hstring(c_in);
1024 if is_fast_spr(e_in.write_reg) then
1025 if decode_spr_num(e_in.insn) = SPR_XER then
1026 v.e.xerc.so := c_in(63-32);
1027 v.e.xerc.ov := c_in(63-33);
1028 v.e.xerc.ca := c_in(63-34);
1029 v.e.xerc.ov32 := c_in(63-44);
1030 v.e.xerc.ca32 := c_in(63-45);
1034 case decode_spr_num(e_in.insn) is
1036 ctrl_tmp.dec <= c_in;
1037 when 724 => -- LOG_ADDR SPR
1038 v.log_addr_spr := c_in(31 downto 0);
1040 -- mtspr to unimplemented SPRs should be a nop in
1041 -- supervisor mode and a program interrupt for user mode
1042 if ctrl.msr(MSR_PR) = '1' then
1047 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
1048 if e_in.output_carry = '1' then
1049 set_carry(v.e, rotator_carry, rotator_carry);
1055 v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1058 icache_inval <= '1';
1060 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
1062 v.mul_in_progress := '1';
1064 x_to_multiply.valid <= '1';
1066 when OP_DIV | OP_DIVE | OP_MOD =>
1068 v.div_in_progress := '1';
1070 x_to_divider.valid <= '1';
1077 -- Mispredicted branches cause a redirect
1078 if is_branch = '1' then
1079 if taken_branch = '1' then
1080 ctrl_tmp.cfar <= e_in.nia;
1082 if taken_branch = '1' then
1083 v.br_offset := b_in;
1084 v.abs_br := abs_branch;
1086 v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1088 if taken_branch /= e_in.br_pred then
1091 v.br_last := is_direct_branch;
1092 v.taken_br := taken_branch;
1095 elsif valid_in = '1' and exception = '0' and illegal = '0' then
1096 -- instruction for other units, i.e. LDST
1097 if e_in.unit = LDST then
1099 elsif e_in.unit = NONE then
1101 elsif HAS_FPU and e_in.unit = FPU then
1104 -- Handling an ITLB miss doesn't count as having executed an instruction
1105 if e_in.insn_type = OP_FETCH_FAILED then
1110 -- The following cases all occur when r.busy = 1 and therefore
1111 -- valid_in = 0. Hence they don't happen in the same cycle as any of
1112 -- the cases above which depend on valid_in = 1.
1114 if ctrl.irq_state = WRITE_SRR1 then
1115 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
1116 v.e.exc_write_data := ctrl.srr1;
1117 v.e.exc_write_enable := '1';
1118 ctrl_tmp.msr(MSR_SF) <= '1';
1119 ctrl_tmp.msr(MSR_EE) <= '0';
1120 ctrl_tmp.msr(MSR_PR) <= '0';
1121 ctrl_tmp.msr(MSR_SE) <= '0';
1122 ctrl_tmp.msr(MSR_BE) <= '0';
1123 ctrl_tmp.msr(MSR_FP) <= '0';
1124 ctrl_tmp.msr(MSR_FE0) <= '0';
1125 ctrl_tmp.msr(MSR_FE1) <= '0';
1126 ctrl_tmp.msr(MSR_IR) <= '0';
1127 ctrl_tmp.msr(MSR_DR) <= '0';
1128 ctrl_tmp.msr(MSR_RI) <= '0';
1129 ctrl_tmp.msr(MSR_LE) <= '1';
1130 v.trace_next := '0';
1131 v.fp_exception_next := '0';
1132 report "Writing SRR1: " & to_hstring(ctrl.srr1);
1134 elsif r.cntz_in_progress = '1' then
1135 -- cnt[lt]z always takes two cycles
1137 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
1138 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
1139 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
1140 if r.mul_in_progress = '1' then
1143 overflow := divider_to_x.overflow;
1145 if r.mul_in_progress = '1' and current.oe = '1' then
1146 -- have to wait until next cycle for overflow indication
1147 v.mul_finish := '1';
1150 -- We must test oe because the RC update code in writeback
1151 -- will use the xerc value to set CR0:SO so we must not clobber
1152 -- xerc if OE wasn't set.
1153 if current.oe = '1' then
1154 v.e.xerc.ov := overflow;
1155 v.e.xerc.ov32 := overflow;
1156 if overflow = '1' then
1164 v.mul_in_progress := r.mul_in_progress;
1165 v.div_in_progress := r.div_in_progress;
1167 elsif r.mul_finish = '1' then
1168 hold_wr_data := '1';
1169 v.e.xerc.ov := multiply_to_x.overflow;
1170 v.e.xerc.ov32 := multiply_to_x.overflow;
1171 if multiply_to_x.overflow = '1' then
1177 -- Generate FP-type program interrupt. fp_in.interrupt will only
1178 -- be set during the execution of a FP instruction.
1179 -- The case where MSR[FE0,FE1] goes from zero to non-zero is
1180 -- handled above by mtmsrd and rfid setting v.fp_exception_next.
1181 if HAS_FPU and fp_in.interrupt = '1' then
1182 v.vector := 16#700#;
1183 ctrl_tmp.srr1(63 - 43) <= '1';
1187 if illegal = '1' or (HAS_FPU and fp_in.illegal = '1') then
1189 v.vector := 16#700#;
1190 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
1191 -- set bit 44 to indicate we have an illegal
1192 ctrl_tmp.srr1(63 - 44) <= '1';
1195 if exception = '1' then
1196 v.e.exc_write_enable := '1';
1197 if exception_nextpc = '1' then
1198 v.e.exc_write_data := next_nia;
1202 -- generate DSI or DSegI for load/store exceptions
1203 -- or ISI or ISegI for instruction fetch exceptions
1204 if l_in.exception = '1' then
1205 if l_in.alignment = '1' then
1206 v.vector := 16#600#;
1207 elsif l_in.instr_fault = '0' then
1208 if l_in.segment_fault = '0' then
1209 v.vector := 16#300#;
1211 v.vector := 16#380#;
1214 if l_in.segment_fault = '0' then
1215 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
1216 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
1217 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
1218 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
1219 v.vector := 16#400#;
1221 v.vector := 16#480#;
1224 v.e.exc_write_enable := '1';
1225 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1226 report "ldst exception writing srr0=" & to_hstring(r.last_nia);
1229 if exception = '1' or l_in.exception = '1' then
1230 ctrl_tmp.irq_state <= WRITE_SRR1;
1235 if do_trace = '1' then
1236 v.trace_next := '1';
1239 if hold_wr_data = '0' then
1240 v.e.write_data := alu_result;
1242 v.e.write_data := r.e.write_data;
1244 v.e.write_reg := current.write_reg;
1245 v.e.write_enable := current.write_reg_enable and v.e.valid and not exception;
1246 v.e.rc := current.rc and v.e.valid and not exception;
1247 v.e.write_cr_data := write_cr_data;
1248 v.e.write_cr_mask := write_cr_mask;
1249 v.e.write_cr_enable := current.output_cr and v.e.valid and not exception;
1250 v.e.write_xerc_enable := current.output_xer and v.e.valid and not exception;
1252 bypass_data.tag.valid <= current.instr_tag.valid and current.write_reg_enable and v.e.valid;
1253 bypass_data.tag.tag <= current.instr_tag.tag;
1254 bypass_data.data <= v.e.write_data;
1256 bypass_cr_data.tag.valid <= current.instr_tag.valid and current.output_cr and v.e.valid;
1257 bypass_cr_data.tag.tag <= current.instr_tag.tag;
1258 for i in 0 to 7 loop
1259 if v.e.write_cr_mask(i) = '1' then
1260 bypass_cr_data.data(i*4 + 3 downto i*4) <= v.e.write_cr_data(i*4 + 3 downto i*4);
1262 bypass_cr_data.data(i*4 + 3 downto i*4) <= cr_in(i*4 + 3 downto i*4);
1266 -- Defer completion for one cycle when redirecting.
1267 -- This also ensures r.busy = 1 when ctrl.irq_state = WRITE_SRR1
1268 if v.redirect = '1' then
1272 if r.redirect = '1' then
1276 -- Outputs to fetch1
1277 f.redirect := r.redirect;
1278 f.br_nia := r.last_nia;
1279 f.br_last := r.br_last and not r.do_intr;
1280 f.br_taken := r.taken_br;
1281 if r.do_intr = '1' then
1282 f.redirect_nia := std_ulogic_vector(to_unsigned(r.vector, 64));
1285 -- XXX need an interrupt LE bit here, e.g. from LPCR
1286 f.big_endian := '0';
1287 f.mode_32bit := '0';
1289 if r.abs_br = '1' then
1290 f.redirect_nia := r.br_offset;
1292 f.redirect_nia := std_ulogic_vector(unsigned(r.last_nia) + unsigned(r.br_offset));
1294 -- send MSR[IR], ~MSR[PR], ~MSR[LE] and ~MSR[SF] up to fetch1
1295 f.virt_mode := r.redir_mode(3);
1296 f.priv_mode := r.redir_mode(2);
1297 f.big_endian := r.redir_mode(1);
1298 f.mode_32bit := r.redir_mode(0);
1301 -- Outputs to loadstore1 (async)
1302 lv.op := e_in.insn_type;
1304 lv.instr_tag := e_in.instr_tag;
1308 lv.write_reg := e_in.write_reg;
1309 lv.length := e_in.data_len;
1310 lv.byte_reverse := e_in.byte_reverse xnor ctrl.msr(MSR_LE);
1311 lv.sign_extend := e_in.sign_extend;
1312 lv.update := e_in.update;
1314 lv.reserve := e_in.reserve;
1316 lv.insn := e_in.insn;
1317 -- decode l*cix and st*cix instructions here
1318 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1319 e_in.insn(5 downto 1) = "10101" then
1322 lv.virt_mode := ctrl.msr(MSR_DR);
1323 lv.priv_mode := not ctrl.msr(MSR_PR);
1324 lv.mode_32bit := not ctrl.msr(MSR_SF);
1325 lv.is_32bit := e_in.is_32bit;
1326 lv.repeat := e_in.repeat;
1327 lv.second := e_in.second;
1330 fv.op := e_in.insn_type;
1332 fv.insn := e_in.insn;
1333 fv.itag := e_in.instr_tag;
1334 fv.single := e_in.is_32bit;
1335 fv.fe_mode := ctrl.msr(MSR_FE0) & ctrl.msr(MSR_FE1);
1339 fv.frt := e_in.write_reg;
1341 fv.out_cr := e_in.output_cr;
1351 flush_out <= f_out.redirect;
1353 exception_log <= exception;
1354 irq_valid_log <= irq_valid;
1357 e1_log: if LOG_LENGTH > 0 generate
1358 signal log_data : std_ulogic_vector(14 downto 0);
1360 ex1_log : process(clk)
1362 if rising_edge(clk) then
1363 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1364 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1367 std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
1376 log_out <= log_data;
1378 end architecture behaviour;