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;
41 dbg_msr_out : out std_ulogic_vector(63 downto 0);
43 icache_inval : out std_ulogic;
44 terminate_out : out std_ulogic;
46 log_out : out std_ulogic_vector(14 downto 0);
47 log_rd_addr : out std_ulogic_vector(31 downto 0);
48 log_rd_data : in std_ulogic_vector(63 downto 0);
49 log_wr_addr : in std_ulogic_vector(31 downto 0)
53 architecture behaviour of execute1 is
54 type reg_type is record
55 e : Execute1ToWritebackType;
56 cur_instr : Decode2ToExecute1Type;
58 terminate: std_ulogic;
59 fp_exception_next : std_ulogic;
60 trace_next : std_ulogic;
61 prev_op : insn_type_t;
62 lr_update : std_ulogic;
63 next_lr : std_ulogic_vector(63 downto 0);
64 mul_in_progress : std_ulogic;
65 mul_finish : std_ulogic;
66 div_in_progress : std_ulogic;
67 cntz_in_progress : std_ulogic;
68 last_nia : std_ulogic_vector(63 downto 0);
69 redirect : std_ulogic;
71 taken_br : std_ulogic;
74 vector : integer range 0 to 16#fff#;
75 br_offset : std_ulogic_vector(63 downto 0);
76 redir_mode : std_ulogic_vector(3 downto 0);
77 log_addr_spr : std_ulogic_vector(31 downto 0);
79 constant reg_type_init : reg_type :=
80 (e => Execute1ToWritebackInit,
81 cur_instr => Decode2ToExecute1Init,
82 busy => '0', lr_update => '0', terminate => '0',
83 fp_exception_next => '0', trace_next => '0', prev_op => OP_ILLEGAL,
84 mul_in_progress => '0', mul_finish => '0', div_in_progress => '0', cntz_in_progress => '0',
85 next_lr => (others => '0'), last_nia => (others => '0'),
86 redirect => '0', abs_br => '0', taken_br => '0', br_last => '0', do_intr => '0', vector => 0,
87 br_offset => (others => '0'), redir_mode => "0000",
88 others => (others => '0'));
90 signal r, rin : reg_type;
92 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
93 signal cr_in : std_ulogic_vector(31 downto 0);
95 signal valid_in : std_ulogic;
96 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
97 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
98 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
99 signal rot_sign_ext: std_ulogic;
100 signal rotator_result: std_ulogic_vector(63 downto 0);
101 signal rotator_carry: std_ulogic;
102 signal logical_result: std_ulogic_vector(63 downto 0);
103 signal countzero_result: std_ulogic_vector(63 downto 0);
104 signal alu_result: std_ulogic_vector(63 downto 0);
105 signal adder_result: std_ulogic_vector(63 downto 0);
106 signal misc_result: std_ulogic_vector(63 downto 0);
107 signal muldiv_result: std_ulogic_vector(63 downto 0);
108 signal spr_result: std_ulogic_vector(63 downto 0);
109 signal result_mux_sel: std_ulogic_vector(2 downto 0);
110 signal sub_mux_sel: std_ulogic_vector(2 downto 0);
111 signal current: Decode2ToExecute1Type;
114 signal x_to_multiply: MultiplyInputType;
115 signal multiply_to_x: MultiplyOutputType;
118 signal x_to_divider: Execute1ToDividerType;
119 signal divider_to_x: DividerToExecute1Type;
121 -- random number generator signals
122 signal random_raw : std_ulogic_vector(63 downto 0);
123 signal random_cond : std_ulogic_vector(63 downto 0);
124 signal random_err : std_ulogic;
126 -- signals for logging
127 signal exception_log : std_ulogic;
128 signal irq_valid_log : std_ulogic;
130 type privilege_level is (USER, SUPER);
131 type op_privilege_array is array(insn_type_t) of privilege_level;
132 constant op_privilege: op_privilege_array := (
141 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
144 if op_privilege(op) = SUPER then
146 elsif op = OP_MFSPR or op = OP_MTSPR then
147 return insn(20) = '1';
153 procedure set_carry(e: inout Execute1ToWritebackType;
154 carry32 : in std_ulogic;
155 carry : in std_ulogic) is
157 e.xerc.ca32 := carry32;
159 e.write_xerc_enable := '1';
162 procedure set_ov(e: inout Execute1ToWritebackType;
164 ov32 : in std_ulogic) is
171 e.write_xerc_enable := '1';
174 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
175 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
177 return (ca xor msb_r) and not (msb_a xor msb_b);
180 function decode_input_carry(ic : carry_in_t;
181 xerc : xer_common_t) return std_ulogic is
195 function msr_copy(msr: std_ulogic_vector(63 downto 0))
196 return std_ulogic_vector is
197 variable msr_out: std_ulogic_vector(63 downto 0);
200 -- Defined MSR bits are classified as either full func-
201 -- tion or partial function. Full function MSR bits are
202 -- saved in SRR1 or HSRR1 when an interrupt other
203 -- than a System Call Vectored interrupt occurs and
204 -- restored by rfscv, rfid, or hrfid, while partial func-
205 -- tion MSR bits are not saved or restored.
206 -- Full function MSR bits lie in the range 0:32, 37:41, and
207 -- 48:63, and partial function MSR bits lie in the range
208 -- 33:36 and 42:47. (Note this is IBM bit numbering).
209 msr_out := (others => '0');
210 msr_out(63 downto 31) := msr(63 downto 31);
211 msr_out(26 downto 22) := msr(26 downto 22);
212 msr_out(15 downto 0) := msr(15 downto 0);
216 -- Tell vivado to keep the hierarchy for the random module so that the
217 -- net names in the xdc file match.
218 attribute keep_hierarchy : string;
219 attribute keep_hierarchy of random_0 : label is "yes";
223 rotator_0: entity work.rotator
227 shift => b_in(6 downto 0),
229 is_32bit => e_in.is_32bit,
230 right_shift => right_shift,
231 arith => e_in.is_signed,
232 clear_left => rot_clear_left,
233 clear_right => rot_clear_right,
234 sign_ext_rs => rot_sign_ext,
235 result => rotator_result,
236 carry_out => rotator_carry
239 logical_0: entity work.logical
243 op => e_in.insn_type,
244 invert_in => e_in.invert_a,
245 invert_out => e_in.invert_out,
246 result => logical_result,
247 datalen => e_in.data_len
250 countzero_0: entity work.zero_counter
254 count_right => e_in.insn(10),
255 is_32bit => e_in.is_32bit,
256 result => countzero_result
259 multiply_0: entity work.multiply
262 m_in => x_to_multiply,
263 m_out => multiply_to_x
266 divider_0: entity work.divider
270 d_in => x_to_divider,
271 d_out => divider_to_x
274 random_0: entity work.random
282 dbg_msr_out <= ctrl.msr;
283 log_rd_addr <= r.log_addr_spr;
285 a_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data1 = '1' else e_in.read_data1;
286 b_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data2 = '1' else e_in.read_data2;
287 c_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data3 = '1' else e_in.read_data3;
289 busy_out <= l_in.busy or r.busy or fp_in.busy;
290 valid_in <= e_in.valid and not busy_out;
292 terminate_out <= r.terminate;
294 current <= e_in when r.busy = '0' else r.cur_instr;
297 with current.result_sel select alu_result <=
298 adder_result when "000",
299 logical_result when "001",
300 rotator_result when "010",
301 muldiv_result when "011",
302 countzero_result when "100",
303 spr_result when "101",
304 misc_result when others;
306 execute1_0: process(clk)
308 if rising_edge(clk) then
311 ctrl.tb <= (others => '0');
312 ctrl.dec <= (others => '0');
313 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
314 ctrl.irq_state <= WRITE_SRR0;
318 assert not (r.lr_update = '1' and valid_in = '1')
319 report "LR update collision with valid in EX1"
321 if r.lr_update = '1' then
322 report "LR update to " & to_hstring(r.next_lr);
328 execute1_1: process(all)
329 variable v : reg_type;
330 variable a_inv : std_ulogic_vector(63 downto 0);
331 variable b_or_m1 : std_ulogic_vector(63 downto 0);
332 variable addg6s : std_ulogic_vector(63 downto 0);
333 variable isel_result : std_ulogic_vector(63 downto 0);
334 variable darn : std_ulogic_vector(63 downto 0);
335 variable mfcr_result : std_ulogic_vector(63 downto 0);
336 variable setb_result : std_ulogic_vector(63 downto 0);
337 variable newcrf : std_ulogic_vector(3 downto 0);
338 variable sum_with_carry : std_ulogic_vector(64 downto 0);
339 variable crnum : crnum_t;
340 variable crbit : integer range 0 to 31;
341 variable scrnum : crnum_t;
342 variable lo, hi : integer;
343 variable sh, mb, me : std_ulogic_vector(5 downto 0);
344 variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
345 variable bo, bi : std_ulogic_vector(4 downto 0);
346 variable bf, bfa : std_ulogic_vector(2 downto 0);
347 variable cr_op : std_ulogic_vector(9 downto 0);
348 variable cr_operands : std_ulogic_vector(1 downto 0);
349 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
350 variable btnum, banum, bbnum : integer range 0 to 31;
351 variable crresult : std_ulogic;
352 variable l : std_ulogic;
353 variable next_nia : std_ulogic_vector(63 downto 0);
354 variable carry_32, carry_64 : std_ulogic;
355 variable sign1, sign2 : std_ulogic;
356 variable abs1, abs2 : signed(63 downto 0);
357 variable overflow : std_ulogic;
358 variable zerohi, zerolo : std_ulogic;
359 variable msb_a, msb_b : std_ulogic;
360 variable a_lt : std_ulogic;
361 variable a_lt_lo : std_ulogic;
362 variable a_lt_hi : std_ulogic;
363 variable lv : Execute1ToLoadstore1Type;
364 variable irq_valid : std_ulogic;
365 variable exception : std_ulogic;
366 variable exception_nextpc : std_ulogic;
367 variable trapval : std_ulogic_vector(4 downto 0);
368 variable illegal : std_ulogic;
369 variable is_branch : std_ulogic;
370 variable is_direct_branch : std_ulogic;
371 variable taken_branch : std_ulogic;
372 variable abs_branch : std_ulogic;
373 variable spr_val : std_ulogic_vector(63 downto 0);
374 variable addend : std_ulogic_vector(127 downto 0);
375 variable do_trace : std_ulogic;
376 variable hold_wr_data : std_ulogic;
377 variable f : Execute1ToFetch1Type;
378 variable fv : Execute1ToFPUType;
380 sum_with_carry := (others => '0');
381 newcrf := (others => '0');
383 is_direct_branch := '0';
389 v.e := Execute1ToWritebackInit;
394 v.br_offset := (others => '0');
395 v.redir_mode := ctrl.msr(MSR_IR) & not ctrl.msr(MSR_PR) &
396 not ctrl.msr(MSR_LE) & not ctrl.msr(MSR_SF);
400 lv := Execute1ToLoadstore1Init;
401 fv := Execute1ToFPUInit;
403 -- XER forwarding. To avoid having to track XER hazards, we use
404 -- the previously latched value. Since the XER common bits
405 -- (SO, OV[32] and CA[32]) are only modified by instructions that are
406 -- handled here, we can just forward the result being sent to
408 if r.e.write_xerc_enable = '1' or r.busy = '1' then
409 v.e.xerc := r.e.xerc;
411 v.e.xerc := e_in.xerc;
416 if EX1_BYPASS and e_in.bypass_cr = '1' and r.e.write_cr_enable = '1' then
418 if r.e.write_cr_mask(i) = '1' then
419 cr_in(i * 4 + 3 downto i * 4) <= r.e.write_cr_data(i * 4 + 3 downto i * 4);
425 v.mul_in_progress := '0';
426 v.div_in_progress := '0';
427 v.cntz_in_progress := '0';
430 spr_result <= (others => '0');
431 spr_val := (others => '0');
434 if e_in.invert_a = '0' then
439 if e_in.addm1 = '0' then
442 b_or_m1 := (others => '1');
444 sum_with_carry := ppc_adde(a_inv, b_or_m1,
445 decode_input_carry(e_in.input_carry, v.e.xerc));
446 adder_result <= sum_with_carry(63 downto 0);
447 carry_32 := sum_with_carry(32) xor a_inv(32) xor b_in(32);
448 carry_64 := sum_with_carry(64);
450 -- signals to multiply and divide units
453 if e_in.is_signed = '1' then
454 if e_in.is_32bit = '1' then
462 -- take absolute values
464 abs1 := signed(a_in);
466 abs1 := - signed(a_in);
469 abs2 := signed(b_in);
471 abs2 := - signed(b_in);
474 -- Interface to multiply and divide units
475 x_to_multiply <= MultiplyInputInit;
476 x_to_multiply.is_32bit <= e_in.is_32bit;
478 x_to_divider <= Execute1ToDividerInit;
479 x_to_divider.is_signed <= e_in.is_signed;
480 x_to_divider.is_32bit <= e_in.is_32bit;
481 if e_in.insn_type = OP_MOD then
482 x_to_divider.is_modulus <= '1';
485 addend := (others => '0');
486 if e_in.insn(26) = '0' then
487 -- integer multiply-add, major op 4 (if it is a multiply)
488 addend(63 downto 0) := c_in;
489 if e_in.is_signed = '1' then
490 addend(127 downto 64) := (others => c_in(63));
493 if (sign1 xor sign2) = '1' then
494 addend := not addend;
497 x_to_multiply.not_result <= sign1 xor sign2;
498 x_to_multiply.addend <= addend;
499 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
500 if e_in.is_32bit = '0' then
502 x_to_multiply.data1 <= std_ulogic_vector(abs1);
503 x_to_multiply.data2 <= std_ulogic_vector(abs2);
504 if e_in.insn_type = OP_DIVE then
505 x_to_divider.is_extended <= '1';
507 x_to_divider.dividend <= std_ulogic_vector(abs1);
508 x_to_divider.divisor <= std_ulogic_vector(abs2);
511 x_to_multiply.data1 <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
512 x_to_multiply.data2 <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
513 x_to_divider.is_extended <= '0';
514 if e_in.insn_type = OP_DIVE then -- extended forms
515 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
517 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
519 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
522 case current.sub_select(1 downto 0) is
524 muldiv_result <= multiply_to_x.result(63 downto 0);
526 muldiv_result <= multiply_to_x.result(127 downto 64);
528 muldiv_result <= multiply_to_x.result(63 downto 32) &
529 multiply_to_x.result(63 downto 32);
531 muldiv_result <= divider_to_x.write_reg_data;
534 -- Compute misc_result
535 case current.sub_select is
537 misc_result <= (others => '0');
540 addg6s := (others => '0');
541 for i in 0 to 14 loop
544 if (a_in(hi) xor b_in(hi) xor sum_with_carry(hi)) = '0' then
545 addg6s(lo + 3 downto lo) := "0110";
548 if sum_with_carry(64) = '0' then
549 addg6s(63 downto 60) := "0110";
551 misc_result <= addg6s;
554 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
555 if cr_in(31-crbit) = '1' then
560 misc_result <= isel_result;
563 darn := (others => '1');
564 if random_err = '0' then
565 case e_in.insn(17 downto 16) is
567 darn := x"00000000" & random_cond(31 downto 0);
577 misc_result <= ctrl.msr;
579 if e_in.insn(20) = '0' then
581 mfcr_result := x"00000000" & cr_in;
584 crnum := fxm_to_num(insn_fxm(e_in.insn));
585 mfcr_result := (others => '0');
590 mfcr_result(hi downto lo) := cr_in(hi downto lo);
594 misc_result <= mfcr_result;
597 bfa := insn_bfa(e_in.insn);
598 crbit := to_integer(unsigned(bfa)) * 4;
599 setb_result := (others => '0');
600 if cr_in(31 - crbit) = '1' then
601 setb_result := (others => '1');
602 elsif cr_in(30 - crbit) = '1' then
603 setb_result(0) := '1';
605 misc_result <= setb_result;
607 misc_result <= (others => '0');
610 -- compute comparison results
611 -- Note, we have done RB - RA, not RA - RB
612 if e_in.insn_type = OP_CMP then
613 l := insn_l(e_in.insn);
615 l := not e_in.is_32bit;
617 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
618 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
619 if zerolo = '1' and (l = '0' or zerohi = '1') then
625 if unsigned(a_in(30 downto 0)) < unsigned(b_in(30 downto 0)) then
628 if unsigned(a_in(62 downto 31)) < unsigned(b_in(62 downto 31)) then
635 a_lt := a_lt_hi or (zerohi and (a_in(31) xnor b_in(31)) and a_lt_lo);
642 if msb_a /= msb_b then
643 -- Comparison is clear from MSB difference.
644 -- for signed, 0 is greater; for unsigned, 1 is greater
645 trapval := msb_a & msb_b & '0' & msb_b & msb_a;
647 -- MSBs are equal, so signed and unsigned comparisons give the
649 trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
654 -- FIXME: run at 512MHz not core freq
655 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
656 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
659 if ctrl.msr(MSR_EE) = '1' then
660 if ctrl.dec(63) = '1' then
662 report "IRQ valid: DEC";
664 elsif ext_irq_in = '1' then
666 report "IRQ valid: External";
675 -- Next insn adder used in a couple of places
676 next_nia := std_ulogic_vector(unsigned(e_in.nia) + 4);
678 -- rotator control signals
679 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
680 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
681 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
682 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
684 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
685 ctrl_tmp.irq_state <= WRITE_SRR0;
688 exception_nextpc := '0';
689 v.e.exc_write_enable := '0';
690 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
691 if valid_in = '1' then
692 v.e.exc_write_data := e_in.nia;
693 v.last_nia := e_in.nia;
695 v.e.exc_write_data := r.last_nia;
698 v.e.mode_32bit := not ctrl.msr(MSR_SF);
700 do_trace := valid_in and ctrl.msr(MSR_SE);
701 if valid_in = '1' then
702 v.prev_op := e_in.insn_type;
705 -- Determine if there is any exception to be taken
706 -- before/instead of executing this instruction
707 if valid_in = '1' and e_in.second = '0' then
708 if HAS_FPU and r.fp_exception_next = '1' then
709 -- This is used for FP-type program interrupts that
710 -- become pending due to MSR[FE0,FE1] changing from 00 to non-zero.
713 ctrl_tmp.srr1(63 - 43) <= '1';
714 ctrl_tmp.srr1(63 - 47) <= '1';
715 elsif r.trace_next = '1' then
716 -- Generate a trace interrupt rather than executing the next instruction
717 -- or taking any asynchronous interrupt
720 ctrl_tmp.srr1(63 - 33) <= '1';
721 if r.prev_op = OP_LOAD or r.prev_op = OP_ICBI or r.prev_op = OP_ICBT or
722 r.prev_op = OP_DCBT or r.prev_op = OP_DCBST or r.prev_op = OP_DCBF then
723 ctrl_tmp.srr1(63 - 35) <= '1';
724 elsif r.prev_op = OP_STORE or r.prev_op = OP_DCBZ or r.prev_op = OP_DCBTST then
725 ctrl_tmp.srr1(63 - 36) <= '1';
728 elsif irq_valid = '1' then
729 -- Don't deliver the interrupt until we have a valid instruction
730 -- coming in, so we have a valid NIA to put in SRR0.
733 elsif ctrl.msr(MSR_PR) = '1' and instr_is_privileged(e_in.insn_type, e_in.insn) then
734 -- generate a program interrupt
737 -- set bit 45 to indicate privileged instruction type interrupt
738 ctrl_tmp.srr1(63 - 45) <= '1';
739 report "privileged instruction";
741 elsif not HAS_FPU and e_in.fac = FPU then
742 -- make lfd/stfd/lfs/stfs etc. illegal in no-FPU implementations
745 elsif HAS_FPU and ctrl.msr(MSR_FP) = '0' and e_in.fac = FPU then
746 -- generate a floating-point unavailable interrupt
749 report "FP unavailable interrupt";
753 if valid_in = '1' and exception = '0' and illegal = '0' and e_in.unit = ALU then
754 report "execute nia " & to_hstring(e_in.nia);
757 v.next_lr := next_nia;
760 case_0: case e_in.insn_type is
763 -- we need two cycles to write srr0 and 1
764 -- will need more when we have to write HEIR
767 -- check bit 1 of the instruction is 1 so we know this is sc;
768 -- 0 would mean scv, so generate an illegal instruction interrupt
769 -- we need two cycles to write srr0 and 1
770 if e_in.insn(1) = '1' then
772 exception_nextpc := '1';
779 -- check bits 1-10 of the instruction to make sure it's attn
780 -- if not then it is illegal
781 if e_in.insn(10 downto 1) = "0100000000" then
787 when OP_NOP | OP_DCBF | OP_DCBST | OP_DCBT | OP_DCBTST | OP_ICBT =>
790 if e_in.output_carry = '1' then
791 if e_in.input_carry /= OV then
792 set_carry(v.e, carry_32, carry_64);
794 v.e.xerc.ov := carry_64;
795 v.e.xerc.ov32 := carry_32;
796 v.e.write_xerc_enable := '1';
799 if e_in.oe = '1' then
801 calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63)),
802 calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31)));
805 -- CMP and CMPL instructions
806 if e_in.is_signed = '1' then
807 newcrf := trapval(4 downto 2) & v.e.xerc.so;
809 newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
811 bf := insn_bf(e_in.insn);
812 crnum := to_integer(unsigned(bf));
813 v.e.write_cr_enable := '1';
814 v.e.write_cr_mask := num_to_fxm(crnum);
818 v.e.write_cr_data(hi downto lo) := newcrf;
821 -- trap instructions (tw, twi, td, tdi)
823 -- set bit 46 to say trap occurred
824 ctrl_tmp.srr1(63 - 46) <= '1';
825 if or (trapval and insn_to(e_in.insn)) = '1' then
826 -- generate trap-type program interrupt
832 newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
833 bf := insn_bf(e_in.insn);
834 crnum := to_integer(unsigned(bf));
835 v.e.write_cr_enable := '1';
836 v.e.write_cr_mask := num_to_fxm(crnum);
837 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
838 newcrf & newcrf & newcrf & newcrf;
840 newcrf := ppc_cmpeqb(a_in, b_in);
841 bf := insn_bf(e_in.insn);
842 crnum := to_integer(unsigned(bf));
843 v.e.write_cr_enable := '1';
844 v.e.write_cr_mask := num_to_fxm(crnum);
845 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
846 newcrf & newcrf & newcrf & newcrf;
847 when OP_AND | OP_OR | OP_XOR | OP_POPCNT | OP_PRTY | OP_CMPB | OP_EXTS |
852 is_direct_branch := '1';
853 abs_branch := insn_aa(e_in.insn);
854 if ctrl.msr(MSR_BE) = '1' then
859 bo := insn_bo(e_in.insn);
860 bi := insn_bi(e_in.insn);
862 is_direct_branch := '1';
863 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
864 abs_branch := insn_aa(e_in.insn);
865 if ctrl.msr(MSR_BE) = '1' then
870 -- read_data2 is target register (CTR, LR or TAR)
871 bo := insn_bo(e_in.insn);
872 bi := insn_bi(e_in.insn);
874 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
876 if ctrl.msr(MSR_BE) = '1' then
881 v.redir_mode := (a_in(MSR_IR) or a_in(MSR_PR)) & not a_in(MSR_PR) &
882 not a_in(MSR_LE) & not a_in(MSR_SF);
883 -- Can't use msr_copy here because the partial function MSR
884 -- bits should be left unchanged, not zeroed.
885 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
886 ctrl_tmp.msr(26 downto 22) <= a_in(26 downto 22);
887 ctrl_tmp.msr(15 downto 0) <= a_in(15 downto 0);
888 if a_in(MSR_PR) = '1' then
889 ctrl_tmp.msr(MSR_EE) <= '1';
890 ctrl_tmp.msr(MSR_IR) <= '1';
891 ctrl_tmp.msr(MSR_DR) <= '1';
893 -- mark this as a branch so CFAR gets updated
898 v.fp_exception_next := fp_in.exception and
899 (a_in(MSR_FE0) or a_in(MSR_FE1));
905 v.cntz_in_progress := '1';
909 cr_op := insn_cr(e_in.insn);
910 report "CR OP " & to_hstring(cr_op);
911 if cr_op(0) = '0' then -- MCRF
912 bf := insn_bf(e_in.insn);
913 bfa := insn_bfa(e_in.insn);
914 v.e.write_cr_enable := '1';
915 crnum := to_integer(unsigned(bf));
916 scrnum := to_integer(unsigned(bfa));
917 v.e.write_cr_mask := num_to_fxm(crnum);
922 newcrf := cr_in(hi downto lo);
928 v.e.write_cr_data(hi downto lo) := newcrf;
931 v.e.write_cr_enable := '1';
932 bt := insn_bt(e_in.insn);
933 ba := insn_ba(e_in.insn);
934 bb := insn_bb(e_in.insn);
935 btnum := 31 - to_integer(unsigned(bt));
936 banum := 31 - to_integer(unsigned(ba));
937 bbnum := 31 - to_integer(unsigned(bb));
938 -- Bits 5-8 of cr_op give the truth table of the requested
940 cr_operands := cr_in(banum) & cr_in(bbnum);
941 crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
942 v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
943 for i in 0 to 31 loop
945 v.e.write_cr_data(i) := crresult;
947 v.e.write_cr_data(i) := cr_in(i);
952 newcrf := v.e.xerc.ov & v.e.xerc.ca & v.e.xerc.ov32 & v.e.xerc.ca32;
953 bf := insn_bf(e_in.insn);
954 crnum := to_integer(unsigned(bf));
955 v.e.write_cr_enable := '1';
956 v.e.write_cr_mask := num_to_fxm(crnum);
957 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
958 newcrf & newcrf & newcrf & newcrf;
962 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
963 "=" & to_hstring(a_in);
964 if is_fast_spr(e_in.read_reg1) = '1' then
966 if decode_spr_num(e_in.insn) = SPR_XER then
967 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
968 spr_val(63 downto 32) := (others => '0');
969 spr_val(63-32) := v.e.xerc.so;
970 spr_val(63-33) := v.e.xerc.ov;
971 spr_val(63-34) := v.e.xerc.ca;
972 spr_val(63-35 downto 63-43) := "000000000";
973 spr_val(63-44) := v.e.xerc.ov32;
974 spr_val(63-45) := v.e.xerc.ca32;
978 case decode_spr_num(e_in.insn) is
982 spr_val(63 downto 32) := (others => '0');
983 spr_val(31 downto 0) := ctrl.tb(63 downto 32);
987 spr_val := ctrl.cfar;
989 spr_val(63 downto 32) := (others => '0');
990 spr_val(31 downto 0) := PVR_MICROWATT;
991 when 724 => -- LOG_ADDR SPR
992 spr_val := log_wr_addr & r.log_addr_spr;
993 when 725 => -- LOG_DATA SPR
994 spr_val := log_rd_data;
995 v.log_addr_spr := std_ulogic_vector(unsigned(r.log_addr_spr) + 1);
997 -- mfspr from unimplemented SPRs should be a nop in
998 -- supervisor mode and a program interrupt for user mode
999 if is_fast_spr(e_in.read_reg1) = '0' and ctrl.msr(MSR_PR) = '1' then
1004 spr_result <= spr_val;
1008 v.e.write_cr_enable := '1';
1009 if e_in.insn(20) = '0' then
1011 v.e.write_cr_mask := insn_fxm(e_in.insn);
1013 -- mtocrf: We require one hot priority encoding here
1014 crnum := fxm_to_num(insn_fxm(e_in.insn));
1015 v.e.write_cr_mask := num_to_fxm(crnum);
1017 v.e.write_cr_data := c_in(31 downto 0);
1019 if e_in.insn(16) = '1' then
1020 -- just update EE and RI
1021 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
1022 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
1024 -- Architecture says to leave out bits 3 (HV), 51 (ME)
1025 -- and 63 (LE) (IBM bit numbering)
1026 if e_in.is_32bit = '0' then
1027 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
1028 ctrl_tmp.msr(59 downto 32) <= c_in(59 downto 32);
1030 ctrl_tmp.msr(31 downto 13) <= c_in(31 downto 13);
1031 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
1032 if c_in(MSR_PR) = '1' then
1033 ctrl_tmp.msr(MSR_EE) <= '1';
1034 ctrl_tmp.msr(MSR_IR) <= '1';
1035 ctrl_tmp.msr(MSR_DR) <= '1';
1038 v.fp_exception_next := fp_in.exception and
1039 (c_in(MSR_FE0) or c_in(MSR_FE1));
1043 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
1044 "=" & to_hstring(c_in);
1045 if is_fast_spr(e_in.write_reg) then
1046 if decode_spr_num(e_in.insn) = SPR_XER then
1047 v.e.xerc.so := c_in(63-32);
1048 v.e.xerc.ov := c_in(63-33);
1049 v.e.xerc.ca := c_in(63-34);
1050 v.e.xerc.ov32 := c_in(63-44);
1051 v.e.xerc.ca32 := c_in(63-45);
1052 v.e.write_xerc_enable := '1';
1056 case decode_spr_num(e_in.insn) is
1058 ctrl_tmp.dec <= c_in;
1059 when 724 => -- LOG_ADDR SPR
1060 v.log_addr_spr := c_in(31 downto 0);
1062 -- mtspr to unimplemented SPRs should be a nop in
1063 -- supervisor mode and a program interrupt for user mode
1064 if ctrl.msr(MSR_PR) = '1' then
1069 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
1070 if e_in.output_carry = '1' then
1071 set_carry(v.e, rotator_carry, rotator_carry);
1077 v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1080 icache_inval <= '1';
1082 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
1084 v.mul_in_progress := '1';
1086 x_to_multiply.valid <= '1';
1088 when OP_DIV | OP_DIVE | OP_MOD =>
1090 v.div_in_progress := '1';
1092 x_to_divider.valid <= '1';
1099 -- Mispredicted branches cause a redirect
1100 if is_branch = '1' then
1101 if taken_branch = '1' then
1102 ctrl_tmp.cfar <= e_in.nia;
1104 if taken_branch = '1' then
1105 v.br_offset := b_in;
1106 v.abs_br := abs_branch;
1108 v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1110 if taken_branch /= e_in.br_pred then
1113 v.br_last := is_direct_branch;
1114 v.taken_br := taken_branch;
1117 elsif valid_in = '1' and exception = '0' and illegal = '0' then
1118 -- instruction for other units, i.e. LDST
1119 if e_in.unit = LDST then
1121 elsif e_in.unit = NONE then
1123 elsif HAS_FPU and e_in.unit = FPU then
1126 -- Handling an ITLB miss doesn't count as having executed an instruction
1127 if e_in.insn_type = OP_FETCH_FAILED then
1132 -- The following cases all occur when r.busy = 1 and therefore
1133 -- valid_in = 0. Hence they don't happen in the same cycle as any of
1134 -- the cases above which depend on valid_in = 1.
1136 if ctrl.irq_state = WRITE_SRR1 then
1137 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
1138 v.e.exc_write_data := ctrl.srr1;
1139 v.e.exc_write_enable := '1';
1140 ctrl_tmp.msr(MSR_SF) <= '1';
1141 ctrl_tmp.msr(MSR_EE) <= '0';
1142 ctrl_tmp.msr(MSR_PR) <= '0';
1143 ctrl_tmp.msr(MSR_SE) <= '0';
1144 ctrl_tmp.msr(MSR_BE) <= '0';
1145 ctrl_tmp.msr(MSR_FP) <= '0';
1146 ctrl_tmp.msr(MSR_FE0) <= '0';
1147 ctrl_tmp.msr(MSR_FE1) <= '0';
1148 ctrl_tmp.msr(MSR_IR) <= '0';
1149 ctrl_tmp.msr(MSR_DR) <= '0';
1150 ctrl_tmp.msr(MSR_RI) <= '0';
1151 ctrl_tmp.msr(MSR_LE) <= '1';
1152 v.trace_next := '0';
1153 v.fp_exception_next := '0';
1154 report "Writing SRR1: " & to_hstring(ctrl.srr1);
1156 elsif r.cntz_in_progress = '1' then
1157 -- cnt[lt]z always takes two cycles
1159 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
1160 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
1161 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
1162 if r.mul_in_progress = '1' then
1165 overflow := divider_to_x.overflow;
1167 if r.mul_in_progress = '1' and current.oe = '1' then
1168 -- have to wait until next cycle for overflow indication
1169 v.mul_finish := '1';
1172 v.e.write_xerc_enable := current.oe;
1173 -- We must test oe because the RC update code in writeback
1174 -- will use the xerc value to set CR0:SO so we must not clobber
1175 -- xerc if OE wasn't set.
1176 if current.oe = '1' then
1177 v.e.xerc.ov := overflow;
1178 v.e.xerc.ov32 := overflow;
1179 if overflow = '1' then
1187 v.mul_in_progress := r.mul_in_progress;
1188 v.div_in_progress := r.div_in_progress;
1190 elsif r.mul_finish = '1' then
1191 hold_wr_data := '1';
1192 v.e.write_xerc_enable := current.oe;
1193 v.e.xerc.ov := multiply_to_x.overflow;
1194 v.e.xerc.ov32 := multiply_to_x.overflow;
1195 if multiply_to_x.overflow = '1' then
1200 -- When doing delayed LR update, keep r.e.write_data unchanged
1201 -- next cycle in case it is needed for a forwarded result (e.g. CTR).
1202 if r.lr_update = '1' then
1203 hold_wr_data := '1';
1206 -- Generate FP-type program interrupt. fp_in.interrupt will only
1207 -- be set during the execution of a FP instruction.
1208 -- The case where MSR[FE0,FE1] goes from zero to non-zero is
1209 -- handled above by mtmsrd and rfid setting v.fp_exception_next.
1210 if HAS_FPU and fp_in.interrupt = '1' then
1211 v.vector := 16#700#;
1212 ctrl_tmp.srr1(63 - 43) <= '1';
1216 if illegal = '1' or (HAS_FPU and fp_in.illegal = '1') then
1218 v.vector := 16#700#;
1219 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
1220 -- set bit 44 to indicate we have an illegal
1221 ctrl_tmp.srr1(63 - 44) <= '1';
1224 if exception = '1' then
1225 v.e.exc_write_enable := '1';
1226 if exception_nextpc = '1' then
1227 v.e.exc_write_data := next_nia;
1231 -- generate DSI or DSegI for load/store exceptions
1232 -- or ISI or ISegI for instruction fetch exceptions
1233 if l_in.exception = '1' then
1234 if l_in.alignment = '1' then
1235 v.vector := 16#600#;
1236 elsif l_in.instr_fault = '0' then
1237 if l_in.segment_fault = '0' then
1238 v.vector := 16#300#;
1240 v.vector := 16#380#;
1243 if l_in.segment_fault = '0' then
1244 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
1245 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
1246 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
1247 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
1248 v.vector := 16#400#;
1250 v.vector := 16#480#;
1253 v.e.exc_write_enable := '1';
1254 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1255 report "ldst exception writing srr0=" & to_hstring(r.last_nia);
1258 if exception = '1' or l_in.exception = '1' then
1259 ctrl_tmp.irq_state <= WRITE_SRR1;
1264 if do_trace = '1' then
1265 v.trace_next := '1';
1268 if hold_wr_data = '0' then
1269 v.e.write_data := alu_result;
1271 v.e.write_data := r.e.write_data;
1273 v.e.write_reg := current.write_reg;
1274 v.e.write_enable := current.write_reg_enable and v.e.valid and not exception;
1275 v.e.rc := current.rc and v.e.valid and not exception;
1277 -- Update LR on the next cycle after a branch link
1278 -- If we're not writing back anything else, we can write back LR
1279 -- this cycle, otherwise we take an extra cycle. We use the
1280 -- exc_write path since next_nia is written through that path
1282 if v.e.valid = '1' and exception = '0' and current.lr = '1' then
1283 if current.write_reg_enable = '0' then
1284 v.e.exc_write_enable := '1';
1285 v.e.exc_write_data := next_nia;
1286 v.e.exc_write_reg := fast_spr_num(SPR_LR);
1290 report "Delayed LR update to " & to_hstring(next_nia);
1294 if r.lr_update = '1' then
1295 v.e.exc_write_enable := '1';
1296 v.e.exc_write_data := r.next_lr;
1297 v.e.exc_write_reg := fast_spr_num(SPR_LR);
1301 -- Defer completion for one cycle when redirecting.
1302 -- This also ensures r.busy = 1 when ctrl.irq_state = WRITE_SRR1
1303 if v.redirect = '1' then
1307 if r.redirect = '1' then
1311 -- Outputs to fetch1
1312 f.redirect := r.redirect;
1313 f.br_nia := r.last_nia;
1314 f.br_last := r.br_last and not r.do_intr;
1315 f.br_taken := r.taken_br;
1316 if r.do_intr = '1' then
1317 f.redirect_nia := std_ulogic_vector(to_unsigned(r.vector, 64));
1320 -- XXX need an interrupt LE bit here, e.g. from LPCR
1321 f.big_endian := '0';
1322 f.mode_32bit := '0';
1324 if r.abs_br = '1' then
1325 f.redirect_nia := r.br_offset;
1327 f.redirect_nia := std_ulogic_vector(unsigned(r.last_nia) + unsigned(r.br_offset));
1329 -- send MSR[IR], ~MSR[PR], ~MSR[LE] and ~MSR[SF] up to fetch1
1330 f.virt_mode := r.redir_mode(3);
1331 f.priv_mode := r.redir_mode(2);
1332 f.big_endian := r.redir_mode(1);
1333 f.mode_32bit := r.redir_mode(0);
1336 -- Outputs to loadstore1 (async)
1337 lv.op := e_in.insn_type;
1342 lv.write_reg := e_in.write_reg;
1343 lv.length := e_in.data_len;
1344 lv.byte_reverse := e_in.byte_reverse xnor ctrl.msr(MSR_LE);
1345 lv.sign_extend := e_in.sign_extend;
1346 lv.update := e_in.update;
1347 lv.update_reg := gspr_to_gpr(e_in.read_reg1);
1348 lv.xerc := v.e.xerc;
1349 lv.reserve := e_in.reserve;
1351 lv.insn := e_in.insn;
1352 -- decode l*cix and st*cix instructions here
1353 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1354 e_in.insn(5 downto 1) = "10101" then
1357 lv.virt_mode := ctrl.msr(MSR_DR);
1358 lv.priv_mode := not ctrl.msr(MSR_PR);
1359 lv.mode_32bit := not ctrl.msr(MSR_SF);
1360 lv.is_32bit := e_in.is_32bit;
1361 lv.repeat := e_in.repeat;
1362 lv.second := e_in.second;
1365 fv.op := e_in.insn_type;
1367 fv.insn := e_in.insn;
1368 fv.single := e_in.is_32bit;
1369 fv.fe_mode := ctrl.msr(MSR_FE0) & ctrl.msr(MSR_FE1);
1373 fv.frt := e_in.write_reg;
1375 fv.out_cr := e_in.output_cr;
1385 flush_out <= f_out.redirect;
1387 exception_log <= exception;
1388 irq_valid_log <= irq_valid;
1391 e1_log: if LOG_LENGTH > 0 generate
1392 signal log_data : std_ulogic_vector(14 downto 0);
1394 ex1_log : process(clk)
1396 if rising_edge(clk) then
1397 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1398 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1401 std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
1410 log_out <= log_data;
1412 end architecture behaviour;