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);
97 signal valid_in : std_ulogic;
98 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
99 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
100 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
101 signal rot_sign_ext: std_ulogic;
102 signal rotator_result: std_ulogic_vector(63 downto 0);
103 signal rotator_carry: std_ulogic;
104 signal logical_result: std_ulogic_vector(63 downto 0);
105 signal countzero_result: std_ulogic_vector(63 downto 0);
106 signal alu_result: std_ulogic_vector(63 downto 0);
107 signal adder_result: std_ulogic_vector(63 downto 0);
108 signal misc_result: std_ulogic_vector(63 downto 0);
109 signal muldiv_result: std_ulogic_vector(63 downto 0);
110 signal spr_result: std_ulogic_vector(63 downto 0);
111 signal result_mux_sel: std_ulogic_vector(2 downto 0);
112 signal sub_mux_sel: std_ulogic_vector(2 downto 0);
113 signal next_nia : std_ulogic_vector(63 downto 0);
114 signal current: Decode2ToExecute1Type;
117 signal x_to_multiply: MultiplyInputType;
118 signal multiply_to_x: MultiplyOutputType;
121 signal x_to_divider: Execute1ToDividerType;
122 signal divider_to_x: DividerToExecute1Type;
124 -- random number generator signals
125 signal random_raw : std_ulogic_vector(63 downto 0);
126 signal random_cond : std_ulogic_vector(63 downto 0);
127 signal random_err : std_ulogic;
129 -- signals for logging
130 signal exception_log : std_ulogic;
131 signal irq_valid_log : std_ulogic;
133 type privilege_level is (USER, SUPER);
134 type op_privilege_array is array(insn_type_t) of privilege_level;
135 constant op_privilege: op_privilege_array := (
144 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
147 if op_privilege(op) = SUPER then
149 elsif op = OP_MFSPR or op = OP_MTSPR then
150 return insn(20) = '1';
156 procedure set_carry(e: inout Execute1ToWritebackType;
157 carry32 : in std_ulogic;
158 carry : in std_ulogic) is
160 e.xerc.ca32 := carry32;
162 e.write_xerc_enable := '1';
165 procedure set_ov(e: inout Execute1ToWritebackType;
167 ov32 : in std_ulogic) is
174 e.write_xerc_enable := '1';
177 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
178 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
180 return (ca xor msb_r) and not (msb_a xor msb_b);
183 function decode_input_carry(ic : carry_in_t;
184 xerc : xer_common_t) return std_ulogic is
198 function msr_copy(msr: std_ulogic_vector(63 downto 0))
199 return std_ulogic_vector is
200 variable msr_out: std_ulogic_vector(63 downto 0);
203 -- Defined MSR bits are classified as either full func-
204 -- tion or partial function. Full function MSR bits are
205 -- saved in SRR1 or HSRR1 when an interrupt other
206 -- than a System Call Vectored interrupt occurs and
207 -- restored by rfscv, rfid, or hrfid, while partial func-
208 -- tion MSR bits are not saved or restored.
209 -- Full function MSR bits lie in the range 0:32, 37:41, and
210 -- 48:63, and partial function MSR bits lie in the range
211 -- 33:36 and 42:47. (Note this is IBM bit numbering).
212 msr_out := (others => '0');
213 msr_out(63 downto 31) := msr(63 downto 31);
214 msr_out(26 downto 22) := msr(26 downto 22);
215 msr_out(15 downto 0) := msr(15 downto 0);
219 -- Tell vivado to keep the hierarchy for the random module so that the
220 -- net names in the xdc file match.
221 attribute keep_hierarchy : string;
222 attribute keep_hierarchy of random_0 : label is "yes";
226 rotator_0: entity work.rotator
230 shift => b_in(6 downto 0),
232 is_32bit => e_in.is_32bit,
233 right_shift => right_shift,
234 arith => e_in.is_signed,
235 clear_left => rot_clear_left,
236 clear_right => rot_clear_right,
237 sign_ext_rs => rot_sign_ext,
238 result => rotator_result,
239 carry_out => rotator_carry
242 logical_0: entity work.logical
246 op => e_in.insn_type,
247 invert_in => e_in.invert_a,
248 invert_out => e_in.invert_out,
249 result => logical_result,
250 datalen => e_in.data_len
253 countzero_0: entity work.zero_counter
257 count_right => e_in.insn(10),
258 is_32bit => e_in.is_32bit,
259 result => countzero_result
262 multiply_0: entity work.multiply
265 m_in => x_to_multiply,
266 m_out => multiply_to_x
269 divider_0: entity work.divider
273 d_in => x_to_divider,
274 d_out => divider_to_x
277 random_0: entity work.random
285 dbg_msr_out <= ctrl.msr;
286 log_rd_addr <= r.log_addr_spr;
288 a_in <= e_in.read_data1;
289 b_in <= e_in.read_data2;
290 c_in <= e_in.read_data3;
292 busy_out <= l_in.busy or r.busy or fp_in.busy;
293 valid_in <= e_in.valid and not busy_out;
295 terminate_out <= r.terminate;
297 current <= e_in when r.busy = '0' else r.cur_instr;
300 with current.result_sel select alu_result <=
301 adder_result when "000",
302 logical_result when "001",
303 rotator_result when "010",
304 muldiv_result when "011",
305 countzero_result when "100",
306 spr_result when "101",
308 misc_result when others;
310 execute1_0: process(clk)
312 if rising_edge(clk) then
315 ctrl.tb <= (others => '0');
316 ctrl.dec <= (others => '0');
317 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
318 ctrl.irq_state <= WRITE_SRR0;
322 if valid_in = '1' then
323 report "execute " & to_hstring(e_in.nia) & " op=" & insn_type_t'image(e_in.insn_type) &
324 " wr=" & to_hstring(rin.e.write_reg) & " we=" & std_ulogic'image(rin.e.write_enable) &
325 " tag=" & integer'image(rin.e.instr_tag.tag) & std_ulogic'image(rin.e.instr_tag.valid);
331 execute1_1: process(all)
332 variable v : reg_type;
333 variable a_inv : std_ulogic_vector(63 downto 0);
334 variable b_or_m1 : std_ulogic_vector(63 downto 0);
335 variable addg6s : std_ulogic_vector(63 downto 0);
336 variable isel_result : std_ulogic_vector(63 downto 0);
337 variable darn : std_ulogic_vector(63 downto 0);
338 variable mfcr_result : std_ulogic_vector(63 downto 0);
339 variable setb_result : std_ulogic_vector(63 downto 0);
340 variable newcrf : std_ulogic_vector(3 downto 0);
341 variable sum_with_carry : std_ulogic_vector(64 downto 0);
342 variable crnum : crnum_t;
343 variable crbit : integer range 0 to 31;
344 variable scrnum : crnum_t;
345 variable lo, hi : integer;
346 variable sh, mb, me : std_ulogic_vector(5 downto 0);
347 variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
348 variable bo, bi : std_ulogic_vector(4 downto 0);
349 variable bf, bfa : std_ulogic_vector(2 downto 0);
350 variable cr_op : std_ulogic_vector(9 downto 0);
351 variable cr_operands : std_ulogic_vector(1 downto 0);
352 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
353 variable btnum, banum, bbnum : integer range 0 to 31;
354 variable crresult : std_ulogic;
355 variable l : std_ulogic;
356 variable carry_32, carry_64 : std_ulogic;
357 variable sign1, sign2 : std_ulogic;
358 variable abs1, abs2 : signed(63 downto 0);
359 variable overflow : std_ulogic;
360 variable zerohi, zerolo : std_ulogic;
361 variable msb_a, msb_b : std_ulogic;
362 variable a_lt : std_ulogic;
363 variable a_lt_lo : std_ulogic;
364 variable a_lt_hi : std_ulogic;
365 variable lv : Execute1ToLoadstore1Type;
366 variable irq_valid : std_ulogic;
367 variable exception : std_ulogic;
368 variable exception_nextpc : std_ulogic;
369 variable trapval : std_ulogic_vector(4 downto 0);
370 variable illegal : std_ulogic;
371 variable is_branch : std_ulogic;
372 variable is_direct_branch : std_ulogic;
373 variable taken_branch : std_ulogic;
374 variable abs_branch : std_ulogic;
375 variable spr_val : std_ulogic_vector(63 downto 0);
376 variable addend : std_ulogic_vector(127 downto 0);
377 variable do_trace : std_ulogic;
378 variable hold_wr_data : std_ulogic;
379 variable f : Execute1ToFetch1Type;
380 variable fv : Execute1ToFPUType;
382 sum_with_carry := (others => '0');
383 newcrf := (others => '0');
385 is_direct_branch := '0';
391 v.e := Execute1ToWritebackInit;
396 v.br_offset := (others => '0');
397 v.redir_mode := ctrl.msr(MSR_IR) & not ctrl.msr(MSR_PR) &
398 not ctrl.msr(MSR_LE) & not ctrl.msr(MSR_SF);
402 lv := Execute1ToLoadstore1Init;
403 fv := Execute1ToFPUInit;
405 -- XER forwarding. To avoid having to track XER hazards, we use
406 -- the previously latched value. Since the XER common bits
407 -- (SO, OV[32] and CA[32]) are only modified by instructions that are
408 -- handled here, we can just forward the result being sent to
410 if r.e.write_xerc_enable = '1' or r.busy = '1' then
411 v.e.xerc := r.e.xerc;
413 v.e.xerc := e_in.xerc;
418 v.mul_in_progress := '0';
419 v.div_in_progress := '0';
420 v.cntz_in_progress := '0';
423 spr_result <= (others => '0');
424 spr_val := (others => '0');
427 if e_in.invert_a = '0' then
432 if e_in.addm1 = '0' then
435 b_or_m1 := (others => '1');
437 sum_with_carry := ppc_adde(a_inv, b_or_m1,
438 decode_input_carry(e_in.input_carry, v.e.xerc));
439 adder_result <= sum_with_carry(63 downto 0);
440 carry_32 := sum_with_carry(32) xor a_inv(32) xor b_in(32);
441 carry_64 := sum_with_carry(64);
443 -- signals to multiply and divide units
446 if e_in.is_signed = '1' then
447 if e_in.is_32bit = '1' then
455 -- take absolute values
457 abs1 := signed(a_in);
459 abs1 := - signed(a_in);
462 abs2 := signed(b_in);
464 abs2 := - signed(b_in);
467 -- Interface to multiply and divide units
468 x_to_multiply <= MultiplyInputInit;
469 x_to_multiply.is_32bit <= e_in.is_32bit;
471 x_to_divider <= Execute1ToDividerInit;
472 x_to_divider.is_signed <= e_in.is_signed;
473 x_to_divider.is_32bit <= e_in.is_32bit;
474 if e_in.insn_type = OP_MOD then
475 x_to_divider.is_modulus <= '1';
478 addend := (others => '0');
479 if e_in.insn(26) = '0' then
480 -- integer multiply-add, major op 4 (if it is a multiply)
481 addend(63 downto 0) := c_in;
482 if e_in.is_signed = '1' then
483 addend(127 downto 64) := (others => c_in(63));
486 if (sign1 xor sign2) = '1' then
487 addend := not addend;
490 x_to_multiply.not_result <= sign1 xor sign2;
491 x_to_multiply.addend <= addend;
492 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
493 if e_in.is_32bit = '0' then
495 x_to_multiply.data1 <= std_ulogic_vector(abs1);
496 x_to_multiply.data2 <= std_ulogic_vector(abs2);
497 if e_in.insn_type = OP_DIVE then
498 x_to_divider.is_extended <= '1';
500 x_to_divider.dividend <= std_ulogic_vector(abs1);
501 x_to_divider.divisor <= std_ulogic_vector(abs2);
504 x_to_multiply.data1 <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
505 x_to_multiply.data2 <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
506 x_to_divider.is_extended <= '0';
507 if e_in.insn_type = OP_DIVE then -- extended forms
508 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
510 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
512 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
515 case current.sub_select(1 downto 0) is
517 muldiv_result <= multiply_to_x.result(63 downto 0);
519 muldiv_result <= multiply_to_x.result(127 downto 64);
521 muldiv_result <= multiply_to_x.result(63 downto 32) &
522 multiply_to_x.result(63 downto 32);
524 muldiv_result <= divider_to_x.write_reg_data;
527 -- Compute misc_result
528 case current.sub_select is
530 misc_result <= (others => '0');
533 addg6s := (others => '0');
534 for i in 0 to 14 loop
537 if (a_in(hi) xor b_in(hi) xor sum_with_carry(hi)) = '0' then
538 addg6s(lo + 3 downto lo) := "0110";
541 if sum_with_carry(64) = '0' then
542 addg6s(63 downto 60) := "0110";
544 misc_result <= addg6s;
547 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
548 if cr_in(31-crbit) = '1' then
553 misc_result <= isel_result;
556 darn := (others => '1');
557 if random_err = '0' then
558 case e_in.insn(17 downto 16) is
560 darn := x"00000000" & random_cond(31 downto 0);
570 misc_result <= ctrl.msr;
572 if e_in.insn(20) = '0' then
574 mfcr_result := x"00000000" & cr_in;
577 crnum := fxm_to_num(insn_fxm(e_in.insn));
578 mfcr_result := (others => '0');
583 mfcr_result(hi downto lo) := cr_in(hi downto lo);
587 misc_result <= mfcr_result;
590 bfa := insn_bfa(e_in.insn);
591 crbit := to_integer(unsigned(bfa)) * 4;
592 setb_result := (others => '0');
593 if cr_in(31 - crbit) = '1' then
594 setb_result := (others => '1');
595 elsif cr_in(30 - crbit) = '1' then
596 setb_result(0) := '1';
598 misc_result <= setb_result;
600 misc_result <= (others => '0');
603 -- compute comparison results
604 -- Note, we have done RB - RA, not RA - RB
605 if e_in.insn_type = OP_CMP then
606 l := insn_l(e_in.insn);
608 l := not e_in.is_32bit;
610 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
611 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
612 if zerolo = '1' and (l = '0' or zerohi = '1') then
618 if unsigned(a_in(30 downto 0)) < unsigned(b_in(30 downto 0)) then
621 if unsigned(a_in(62 downto 31)) < unsigned(b_in(62 downto 31)) then
628 a_lt := a_lt_hi or (zerohi and (a_in(31) xnor b_in(31)) and a_lt_lo);
635 if msb_a /= msb_b then
636 -- Comparison is clear from MSB difference.
637 -- for signed, 0 is greater; for unsigned, 1 is greater
638 trapval := msb_a & msb_b & '0' & msb_b & msb_a;
640 -- MSBs are equal, so signed and unsigned comparisons give the
642 trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
647 -- FIXME: run at 512MHz not core freq
648 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
649 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
652 if ctrl.msr(MSR_EE) = '1' then
653 if ctrl.dec(63) = '1' then
655 report "IRQ valid: DEC";
657 elsif ext_irq_in = '1' then
659 report "IRQ valid: External";
668 -- Next insn adder used in a couple of places
669 next_nia <= std_ulogic_vector(unsigned(e_in.nia) + 4);
671 -- rotator control signals
672 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
673 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
674 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
675 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
677 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
678 ctrl_tmp.irq_state <= WRITE_SRR0;
681 exception_nextpc := '0';
682 v.e.exc_write_enable := '0';
683 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
684 if valid_in = '1' then
685 v.e.exc_write_data := e_in.nia;
686 v.last_nia := e_in.nia;
688 v.e.exc_write_data := r.last_nia;
691 v.e.mode_32bit := not ctrl.msr(MSR_SF);
692 v.e.instr_tag := current.instr_tag;
694 do_trace := valid_in and ctrl.msr(MSR_SE);
695 if valid_in = '1' then
696 v.prev_op := e_in.insn_type;
699 -- Determine if there is any exception to be taken
700 -- before/instead of executing this instruction
701 if valid_in = '1' and e_in.second = '0' then
702 if HAS_FPU and r.fp_exception_next = '1' then
703 -- This is used for FP-type program interrupts that
704 -- become pending due to MSR[FE0,FE1] changing from 00 to non-zero.
707 ctrl_tmp.srr1(63 - 43) <= '1';
708 ctrl_tmp.srr1(63 - 47) <= '1';
709 elsif r.trace_next = '1' then
710 -- Generate a trace interrupt rather than executing the next instruction
711 -- or taking any asynchronous interrupt
714 ctrl_tmp.srr1(63 - 33) <= '1';
715 if r.prev_op = OP_LOAD or r.prev_op = OP_ICBI or r.prev_op = OP_ICBT or
716 r.prev_op = OP_DCBT or r.prev_op = OP_DCBST or r.prev_op = OP_DCBF then
717 ctrl_tmp.srr1(63 - 35) <= '1';
718 elsif r.prev_op = OP_STORE or r.prev_op = OP_DCBZ or r.prev_op = OP_DCBTST then
719 ctrl_tmp.srr1(63 - 36) <= '1';
722 elsif irq_valid = '1' then
723 -- Don't deliver the interrupt until we have a valid instruction
724 -- coming in, so we have a valid NIA to put in SRR0.
727 elsif ctrl.msr(MSR_PR) = '1' and instr_is_privileged(e_in.insn_type, e_in.insn) then
728 -- generate a program interrupt
731 -- set bit 45 to indicate privileged instruction type interrupt
732 ctrl_tmp.srr1(63 - 45) <= '1';
733 report "privileged instruction";
735 elsif not HAS_FPU and e_in.fac = FPU then
736 -- make lfd/stfd/lfs/stfs etc. illegal in no-FPU implementations
739 elsif HAS_FPU and ctrl.msr(MSR_FP) = '0' and e_in.fac = FPU then
740 -- generate a floating-point unavailable interrupt
743 report "FP unavailable interrupt";
747 if valid_in = '1' and exception = '0' and illegal = '0' and e_in.unit = ALU then
749 v.next_lr := next_nia;
752 case_0: case e_in.insn_type is
755 -- we need two cycles to write srr0 and 1
756 -- will need more when we have to write HEIR
759 -- check bit 1 of the instruction is 1 so we know this is sc;
760 -- 0 would mean scv, so generate an illegal instruction interrupt
761 -- we need two cycles to write srr0 and 1
762 if e_in.insn(1) = '1' then
764 exception_nextpc := '1';
771 -- check bits 1-10 of the instruction to make sure it's attn
772 -- if not then it is illegal
773 if e_in.insn(10 downto 1) = "0100000000" then
779 when OP_NOP | OP_DCBF | OP_DCBST | OP_DCBT | OP_DCBTST | OP_ICBT =>
782 if e_in.output_carry = '1' then
783 if e_in.input_carry /= OV then
784 set_carry(v.e, carry_32, carry_64);
786 v.e.xerc.ov := carry_64;
787 v.e.xerc.ov32 := carry_32;
788 v.e.write_xerc_enable := '1';
791 if e_in.oe = '1' then
793 calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63)),
794 calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31)));
797 -- CMP and CMPL instructions
798 if e_in.is_signed = '1' then
799 newcrf := trapval(4 downto 2) & v.e.xerc.so;
801 newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
803 bf := insn_bf(e_in.insn);
804 crnum := to_integer(unsigned(bf));
805 v.e.write_cr_mask := num_to_fxm(crnum);
809 v.e.write_cr_data(hi downto lo) := newcrf;
812 -- trap instructions (tw, twi, td, tdi)
814 -- set bit 46 to say trap occurred
815 ctrl_tmp.srr1(63 - 46) <= '1';
816 if or (trapval and insn_to(e_in.insn)) = '1' then
817 -- generate trap-type program interrupt
823 newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
824 bf := insn_bf(e_in.insn);
825 crnum := to_integer(unsigned(bf));
826 v.e.write_cr_mask := num_to_fxm(crnum);
827 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
828 newcrf & newcrf & newcrf & newcrf;
830 newcrf := ppc_cmpeqb(a_in, b_in);
831 bf := insn_bf(e_in.insn);
832 crnum := to_integer(unsigned(bf));
833 v.e.write_cr_mask := num_to_fxm(crnum);
834 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
835 newcrf & newcrf & newcrf & newcrf;
836 when OP_AND | OP_OR | OP_XOR | OP_POPCNT | OP_PRTY | OP_CMPB | OP_EXTS |
842 is_direct_branch := '1';
843 abs_branch := e_in.br_abs;
844 if ctrl.msr(MSR_BE) = '1' then
847 when OP_BC | OP_BCREG =>
849 -- for OP_BCREG, read_data2 is target register (CTR, LR or TAR)
850 -- If this instruction updates both CTR and LR, then it is
851 -- doubled; the first instruction decrements CTR and determines
852 -- whether the branch is taken, and the second does the
853 -- redirect and the LR update.
854 bo := insn_bo(e_in.insn);
855 bi := insn_bi(e_in.insn);
856 if e_in.second = '0' then
857 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
859 taken_branch := r.br_taken;
861 v.br_taken := taken_branch;
862 abs_branch := e_in.br_abs;
863 if e_in.repeat = '0' or e_in.second = '1' then
865 if e_in.insn_type = OP_BC then
866 is_direct_branch := '1';
868 if ctrl.msr(MSR_BE) = '1' then
874 v.redir_mode := (a_in(MSR_IR) or a_in(MSR_PR)) & not a_in(MSR_PR) &
875 not a_in(MSR_LE) & not a_in(MSR_SF);
876 -- Can't use msr_copy here because the partial function MSR
877 -- bits should be left unchanged, not zeroed.
878 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
879 ctrl_tmp.msr(26 downto 22) <= a_in(26 downto 22);
880 ctrl_tmp.msr(15 downto 0) <= a_in(15 downto 0);
881 if a_in(MSR_PR) = '1' then
882 ctrl_tmp.msr(MSR_EE) <= '1';
883 ctrl_tmp.msr(MSR_IR) <= '1';
884 ctrl_tmp.msr(MSR_DR) <= '1';
886 -- mark this as a branch so CFAR gets updated
891 v.fp_exception_next := fp_in.exception and
892 (a_in(MSR_FE0) or a_in(MSR_FE1));
898 v.cntz_in_progress := '1';
902 cr_op := insn_cr(e_in.insn);
903 if cr_op(0) = '0' then -- MCRF
904 bf := insn_bf(e_in.insn);
905 bfa := insn_bfa(e_in.insn);
906 crnum := to_integer(unsigned(bf));
907 scrnum := to_integer(unsigned(bfa));
908 v.e.write_cr_mask := num_to_fxm(crnum);
913 newcrf := cr_in(hi downto lo);
919 v.e.write_cr_data(hi downto lo) := newcrf;
922 bt := insn_bt(e_in.insn);
923 ba := insn_ba(e_in.insn);
924 bb := insn_bb(e_in.insn);
925 btnum := 31 - to_integer(unsigned(bt));
926 banum := 31 - to_integer(unsigned(ba));
927 bbnum := 31 - to_integer(unsigned(bb));
928 -- Bits 5-8 of cr_op give the truth table of the requested
930 cr_operands := cr_in(banum) & cr_in(bbnum);
931 crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
932 v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
933 for i in 0 to 31 loop
935 v.e.write_cr_data(i) := crresult;
937 v.e.write_cr_data(i) := cr_in(i);
942 newcrf := v.e.xerc.ov & v.e.xerc.ca & v.e.xerc.ov32 & v.e.xerc.ca32;
943 bf := insn_bf(e_in.insn);
944 crnum := to_integer(unsigned(bf));
945 v.e.write_cr_mask := num_to_fxm(crnum);
946 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
947 newcrf & newcrf & newcrf & newcrf;
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) := v.e.xerc.so;
959 spr_val(63-33) := v.e.xerc.ov;
960 spr_val(63-34) := v.e.xerc.ca;
961 spr_val(63-35 downto 63-43) := "000000000";
962 spr_val(63-44) := v.e.xerc.ov32;
963 spr_val(63-45) := v.e.xerc.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;
997 if e_in.insn(20) = '0' then
999 v.e.write_cr_mask := insn_fxm(e_in.insn);
1001 -- mtocrf: We require one hot priority encoding here
1002 crnum := fxm_to_num(insn_fxm(e_in.insn));
1003 v.e.write_cr_mask := num_to_fxm(crnum);
1005 v.e.write_cr_data := c_in(31 downto 0);
1007 if e_in.insn(16) = '1' then
1008 -- just update EE and RI
1009 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
1010 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
1012 -- Architecture says to leave out bits 3 (HV), 51 (ME)
1013 -- and 63 (LE) (IBM bit numbering)
1014 if e_in.is_32bit = '0' then
1015 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
1016 ctrl_tmp.msr(59 downto 32) <= c_in(59 downto 32);
1018 ctrl_tmp.msr(31 downto 13) <= c_in(31 downto 13);
1019 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
1020 if c_in(MSR_PR) = '1' then
1021 ctrl_tmp.msr(MSR_EE) <= '1';
1022 ctrl_tmp.msr(MSR_IR) <= '1';
1023 ctrl_tmp.msr(MSR_DR) <= '1';
1026 v.fp_exception_next := fp_in.exception and
1027 (c_in(MSR_FE0) or c_in(MSR_FE1));
1031 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
1032 "=" & to_hstring(c_in);
1033 if is_fast_spr(e_in.write_reg) then
1034 if decode_spr_num(e_in.insn) = SPR_XER then
1035 v.e.xerc.so := c_in(63-32);
1036 v.e.xerc.ov := c_in(63-33);
1037 v.e.xerc.ca := c_in(63-34);
1038 v.e.xerc.ov32 := c_in(63-44);
1039 v.e.xerc.ca32 := c_in(63-45);
1040 v.e.write_xerc_enable := '1';
1044 case decode_spr_num(e_in.insn) is
1046 ctrl_tmp.dec <= c_in;
1047 when 724 => -- LOG_ADDR SPR
1048 v.log_addr_spr := c_in(31 downto 0);
1050 -- mtspr to unimplemented SPRs should be a nop in
1051 -- supervisor mode and a program interrupt for user mode
1052 if ctrl.msr(MSR_PR) = '1' then
1057 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
1058 if e_in.output_carry = '1' then
1059 set_carry(v.e, rotator_carry, rotator_carry);
1065 v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1068 icache_inval <= '1';
1070 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
1072 v.mul_in_progress := '1';
1074 x_to_multiply.valid <= '1';
1076 when OP_DIV | OP_DIVE | OP_MOD =>
1078 v.div_in_progress := '1';
1080 x_to_divider.valid <= '1';
1087 -- Mispredicted branches cause a redirect
1088 if is_branch = '1' then
1089 if taken_branch = '1' then
1090 ctrl_tmp.cfar <= e_in.nia;
1092 if taken_branch = '1' then
1093 v.br_offset := b_in;
1094 v.abs_br := abs_branch;
1096 v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1098 if taken_branch /= e_in.br_pred then
1101 v.br_last := is_direct_branch;
1102 v.taken_br := taken_branch;
1105 elsif valid_in = '1' and exception = '0' and illegal = '0' then
1106 -- instruction for other units, i.e. LDST
1107 if e_in.unit = LDST then
1109 elsif e_in.unit = NONE then
1111 elsif HAS_FPU and e_in.unit = FPU then
1114 -- Handling an ITLB miss doesn't count as having executed an instruction
1115 if e_in.insn_type = OP_FETCH_FAILED then
1120 -- The following cases all occur when r.busy = 1 and therefore
1121 -- valid_in = 0. Hence they don't happen in the same cycle as any of
1122 -- the cases above which depend on valid_in = 1.
1124 if ctrl.irq_state = WRITE_SRR1 then
1125 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
1126 v.e.exc_write_data := ctrl.srr1;
1127 v.e.exc_write_enable := '1';
1128 ctrl_tmp.msr(MSR_SF) <= '1';
1129 ctrl_tmp.msr(MSR_EE) <= '0';
1130 ctrl_tmp.msr(MSR_PR) <= '0';
1131 ctrl_tmp.msr(MSR_SE) <= '0';
1132 ctrl_tmp.msr(MSR_BE) <= '0';
1133 ctrl_tmp.msr(MSR_FP) <= '0';
1134 ctrl_tmp.msr(MSR_FE0) <= '0';
1135 ctrl_tmp.msr(MSR_FE1) <= '0';
1136 ctrl_tmp.msr(MSR_IR) <= '0';
1137 ctrl_tmp.msr(MSR_DR) <= '0';
1138 ctrl_tmp.msr(MSR_RI) <= '0';
1139 ctrl_tmp.msr(MSR_LE) <= '1';
1140 v.trace_next := '0';
1141 v.fp_exception_next := '0';
1142 report "Writing SRR1: " & to_hstring(ctrl.srr1);
1144 elsif r.cntz_in_progress = '1' then
1145 -- cnt[lt]z always takes two cycles
1147 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
1148 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
1149 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
1150 if r.mul_in_progress = '1' then
1153 overflow := divider_to_x.overflow;
1155 if r.mul_in_progress = '1' and current.oe = '1' then
1156 -- have to wait until next cycle for overflow indication
1157 v.mul_finish := '1';
1160 v.e.write_xerc_enable := current.oe;
1161 -- We must test oe because the RC update code in writeback
1162 -- will use the xerc value to set CR0:SO so we must not clobber
1163 -- xerc if OE wasn't set.
1164 if current.oe = '1' then
1165 v.e.xerc.ov := overflow;
1166 v.e.xerc.ov32 := overflow;
1167 if overflow = '1' then
1175 v.mul_in_progress := r.mul_in_progress;
1176 v.div_in_progress := r.div_in_progress;
1178 elsif r.mul_finish = '1' then
1179 hold_wr_data := '1';
1180 v.e.write_xerc_enable := current.oe;
1181 v.e.xerc.ov := multiply_to_x.overflow;
1182 v.e.xerc.ov32 := multiply_to_x.overflow;
1183 if multiply_to_x.overflow = '1' then
1189 -- Generate FP-type program interrupt. fp_in.interrupt will only
1190 -- be set during the execution of a FP instruction.
1191 -- The case where MSR[FE0,FE1] goes from zero to non-zero is
1192 -- handled above by mtmsrd and rfid setting v.fp_exception_next.
1193 if HAS_FPU and fp_in.interrupt = '1' then
1194 v.vector := 16#700#;
1195 ctrl_tmp.srr1(63 - 43) <= '1';
1199 if illegal = '1' or (HAS_FPU and fp_in.illegal = '1') then
1201 v.vector := 16#700#;
1202 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
1203 -- set bit 44 to indicate we have an illegal
1204 ctrl_tmp.srr1(63 - 44) <= '1';
1207 if exception = '1' then
1208 v.e.exc_write_enable := '1';
1209 if exception_nextpc = '1' then
1210 v.e.exc_write_data := next_nia;
1214 -- generate DSI or DSegI for load/store exceptions
1215 -- or ISI or ISegI for instruction fetch exceptions
1216 if l_in.exception = '1' then
1217 if l_in.alignment = '1' then
1218 v.vector := 16#600#;
1219 elsif l_in.instr_fault = '0' then
1220 if l_in.segment_fault = '0' then
1221 v.vector := 16#300#;
1223 v.vector := 16#380#;
1226 if l_in.segment_fault = '0' then
1227 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
1228 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
1229 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
1230 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
1231 v.vector := 16#400#;
1233 v.vector := 16#480#;
1236 v.e.exc_write_enable := '1';
1237 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1238 report "ldst exception writing srr0=" & to_hstring(r.last_nia);
1241 if exception = '1' or l_in.exception = '1' then
1242 ctrl_tmp.irq_state <= WRITE_SRR1;
1247 if do_trace = '1' then
1248 v.trace_next := '1';
1251 if hold_wr_data = '0' then
1252 v.e.write_data := alu_result;
1254 v.e.write_data := r.e.write_data;
1256 v.e.write_reg := current.write_reg;
1257 v.e.write_enable := current.write_reg_enable and v.e.valid and not exception;
1258 v.e.write_cr_enable := current.output_cr and v.e.valid and not exception;
1259 v.e.rc := current.rc and v.e.valid and not exception;
1261 bypass_data.tag.valid <= current.instr_tag.valid and current.write_reg_enable and v.e.valid;
1262 bypass_data.tag.tag <= current.instr_tag.tag;
1263 bypass_data.data <= v.e.write_data;
1265 bypass_cr_data.tag.valid <= current.instr_tag.valid and current.output_cr and v.e.valid;
1266 bypass_cr_data.tag.tag <= current.instr_tag.tag;
1267 for i in 0 to 7 loop
1268 if v.e.write_cr_mask(i) = '1' then
1269 bypass_cr_data.data(i*4 + 3 downto i*4) <= v.e.write_cr_data(i*4 + 3 downto i*4);
1271 bypass_cr_data.data(i*4 + 3 downto i*4) <= cr_in(i*4 + 3 downto i*4);
1275 -- Defer completion for one cycle when redirecting.
1276 -- This also ensures r.busy = 1 when ctrl.irq_state = WRITE_SRR1
1277 if v.redirect = '1' then
1281 if r.redirect = '1' then
1285 -- Outputs to fetch1
1286 f.redirect := r.redirect;
1287 f.br_nia := r.last_nia;
1288 f.br_last := r.br_last and not r.do_intr;
1289 f.br_taken := r.taken_br;
1290 if r.do_intr = '1' then
1291 f.redirect_nia := std_ulogic_vector(to_unsigned(r.vector, 64));
1294 -- XXX need an interrupt LE bit here, e.g. from LPCR
1295 f.big_endian := '0';
1296 f.mode_32bit := '0';
1298 if r.abs_br = '1' then
1299 f.redirect_nia := r.br_offset;
1301 f.redirect_nia := std_ulogic_vector(unsigned(r.last_nia) + unsigned(r.br_offset));
1303 -- send MSR[IR], ~MSR[PR], ~MSR[LE] and ~MSR[SF] up to fetch1
1304 f.virt_mode := r.redir_mode(3);
1305 f.priv_mode := r.redir_mode(2);
1306 f.big_endian := r.redir_mode(1);
1307 f.mode_32bit := r.redir_mode(0);
1310 -- Outputs to loadstore1 (async)
1311 lv.op := e_in.insn_type;
1313 lv.instr_tag := e_in.instr_tag;
1317 lv.write_reg := e_in.write_reg;
1318 lv.length := e_in.data_len;
1319 lv.byte_reverse := e_in.byte_reverse xnor ctrl.msr(MSR_LE);
1320 lv.sign_extend := e_in.sign_extend;
1321 lv.update := e_in.update;
1322 lv.xerc := v.e.xerc;
1323 lv.reserve := e_in.reserve;
1325 lv.insn := e_in.insn;
1326 -- decode l*cix and st*cix instructions here
1327 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1328 e_in.insn(5 downto 1) = "10101" then
1331 lv.virt_mode := ctrl.msr(MSR_DR);
1332 lv.priv_mode := not ctrl.msr(MSR_PR);
1333 lv.mode_32bit := not ctrl.msr(MSR_SF);
1334 lv.is_32bit := e_in.is_32bit;
1335 lv.repeat := e_in.repeat;
1336 lv.second := e_in.second;
1339 fv.op := e_in.insn_type;
1341 fv.insn := e_in.insn;
1342 fv.itag := e_in.instr_tag;
1343 fv.single := e_in.is_32bit;
1344 fv.fe_mode := ctrl.msr(MSR_FE0) & ctrl.msr(MSR_FE1);
1348 fv.frt := e_in.write_reg;
1350 fv.out_cr := e_in.output_cr;
1360 flush_out <= f_out.redirect;
1362 exception_log <= exception;
1363 irq_valid_log <= irq_valid;
1366 e1_log: if LOG_LENGTH > 0 generate
1367 signal log_data : std_ulogic_vector(14 downto 0);
1369 ex1_log : process(clk)
1371 if rising_edge(clk) then
1372 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1373 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1376 std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
1385 log_out <= log_data;
1387 end architecture behaviour;