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
22 flush_out : out std_ulogic;
23 stall_out : out std_ulogic;
25 e_in : in Decode2ToExecute1Type;
26 l_in : in Loadstore1ToExecute1Type;
28 ext_irq_in : std_ulogic;
31 l_out : out Execute1ToLoadstore1Type;
32 f_out : out Execute1ToFetch1Type;
34 e_out : out Execute1ToWritebackType;
36 dbg_msr_out : out std_ulogic_vector(63 downto 0);
38 icache_inval : out std_ulogic;
39 terminate_out : out std_ulogic;
41 log_out : out std_ulogic_vector(14 downto 0);
42 log_rd_addr : out std_ulogic_vector(31 downto 0);
43 log_rd_data : in std_ulogic_vector(63 downto 0);
44 log_wr_addr : in std_ulogic_vector(31 downto 0)
48 architecture behaviour of execute1 is
49 type reg_type is record
50 e : Execute1ToWritebackType;
51 lr_update : std_ulogic;
52 next_lr : std_ulogic_vector(63 downto 0);
53 mul_in_progress : std_ulogic;
54 div_in_progress : std_ulogic;
55 cntz_in_progress : std_ulogic;
56 slow_op_dest : gpr_index_t;
57 slow_op_rc : std_ulogic;
58 slow_op_oe : std_ulogic;
59 slow_op_xerc : xer_common_t;
60 ldst_nia : std_ulogic_vector(63 downto 0);
61 log_addr_spr : std_ulogic_vector(31 downto 0);
63 constant reg_type_init : reg_type :=
64 (e => Execute1ToWritebackInit, lr_update => '0',
65 mul_in_progress => '0', div_in_progress => '0', cntz_in_progress => '0',
66 slow_op_rc => '0', slow_op_oe => '0', slow_op_xerc => xerc_init,
67 next_lr => (others => '0'), ldst_nia => (others => '0'), others => (others => '0'));
69 signal r, rin : reg_type;
71 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
73 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
74 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
75 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
76 signal rot_sign_ext: std_ulogic;
77 signal rotator_result: std_ulogic_vector(63 downto 0);
78 signal rotator_carry: std_ulogic;
79 signal logical_result: std_ulogic_vector(63 downto 0);
80 signal countzero_result: std_ulogic_vector(63 downto 0);
81 signal popcnt_result: std_ulogic_vector(63 downto 0);
82 signal parity_result: std_ulogic_vector(63 downto 0);
85 signal x_to_multiply: Execute1ToMultiplyType;
86 signal multiply_to_x: MultiplyToExecute1Type;
89 signal x_to_divider: Execute1ToDividerType;
90 signal divider_to_x: DividerToExecute1Type;
92 -- signals for logging
93 signal exception_log : std_ulogic;
94 signal irq_valid_log : std_ulogic;
95 signal log_data : std_ulogic_vector(14 downto 0);
97 type privilege_level is (USER, SUPER);
98 type op_privilege_array is array(insn_type_t) of privilege_level;
99 constant op_privilege: op_privilege_array := (
108 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
111 if op_privilege(op) = SUPER then
113 elsif op = OP_MFSPR or op = OP_MTSPR then
114 return insn(20) = '1';
120 procedure set_carry(e: inout Execute1ToWritebackType;
121 carry32 : in std_ulogic;
122 carry : in std_ulogic) is
124 e.xerc.ca32 := carry32;
126 e.write_xerc_enable := '1';
129 procedure set_ov(e: inout Execute1ToWritebackType;
131 ov32 : in std_ulogic) is
138 e.write_xerc_enable := '1';
141 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
142 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
144 return (ca xor msb_r) and not (msb_a xor msb_b);
147 function decode_input_carry(ic : carry_in_t;
148 xerc : xer_common_t) return std_ulogic is
160 function msr_copy(msr: std_ulogic_vector(63 downto 0))
161 return std_ulogic_vector is
162 variable msr_out: std_ulogic_vector(63 downto 0);
165 -- Defined MSR bits are classified as either full func-
166 -- tion or partial function. Full function MSR bits are
167 -- saved in SRR1 or HSRR1 when an interrupt other
168 -- than a System Call Vectored interrupt occurs and
169 -- restored by rfscv, rfid, or hrfid, while partial func-
170 -- tion MSR bits are not saved or restored.
171 -- Full function MSR bits lie in the range 0:32, 37:41, and
172 -- 48:63, and partial function MSR bits lie in the range
173 -- 33:36 and 42:47. (Note this is IBM bit numbering).
174 msr_out := (others => '0');
175 msr_out(63 downto 31) := msr(63 downto 31);
176 msr_out(26 downto 22) := msr(26 downto 22);
177 msr_out(15 downto 0) := msr(15 downto 0);
183 rotator_0: entity work.rotator
187 shift => b_in(6 downto 0),
189 is_32bit => e_in.is_32bit,
190 right_shift => right_shift,
191 arith => e_in.is_signed,
192 clear_left => rot_clear_left,
193 clear_right => rot_clear_right,
194 sign_ext_rs => rot_sign_ext,
195 result => rotator_result,
196 carry_out => rotator_carry
199 logical_0: entity work.logical
203 op => e_in.insn_type,
204 invert_in => e_in.invert_a,
205 invert_out => e_in.invert_out,
206 result => logical_result,
207 datalen => e_in.data_len,
208 popcnt => popcnt_result,
209 parity => parity_result
212 countzero_0: entity work.zero_counter
216 count_right => e_in.insn(10),
217 is_32bit => e_in.is_32bit,
218 result => countzero_result
221 multiply_0: entity work.multiply
224 m_in => x_to_multiply,
225 m_out => multiply_to_x
228 divider_0: entity work.divider
232 d_in => x_to_divider,
233 d_out => divider_to_x
236 dbg_msr_out <= ctrl.msr;
237 log_rd_addr <= r.log_addr_spr;
239 a_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data1 = '1' else e_in.read_data1;
240 b_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data2 = '1' else e_in.read_data2;
241 c_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data3 = '1' else e_in.read_data3;
243 execute1_0: process(clk)
245 if rising_edge(clk) then
248 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
249 ctrl.irq_state <= WRITE_SRR0;
253 assert not (r.lr_update = '1' and e_in.valid = '1')
254 report "LR update collision with valid in EX1"
256 if r.lr_update = '1' then
257 report "LR update to " & to_hstring(r.next_lr);
263 execute1_1: process(all)
264 variable v : reg_type;
265 variable a_inv : std_ulogic_vector(63 downto 0);
266 variable result : std_ulogic_vector(63 downto 0);
267 variable newcrf : std_ulogic_vector(3 downto 0);
268 variable result_with_carry : std_ulogic_vector(64 downto 0);
269 variable result_en : std_ulogic;
270 variable crnum : crnum_t;
271 variable crbit : integer range 0 to 31;
272 variable scrnum : crnum_t;
273 variable lo, hi : integer;
274 variable sh, mb, me : std_ulogic_vector(5 downto 0);
275 variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
276 variable bo, bi : std_ulogic_vector(4 downto 0);
277 variable bf, bfa : std_ulogic_vector(2 downto 0);
278 variable cr_op : std_ulogic_vector(9 downto 0);
279 variable cr_operands : std_ulogic_vector(1 downto 0);
280 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
281 variable btnum, banum, bbnum : integer range 0 to 31;
282 variable crresult : std_ulogic;
283 variable l : std_ulogic;
284 variable next_nia : std_ulogic_vector(63 downto 0);
285 variable carry_32, carry_64 : std_ulogic;
286 variable sign1, sign2 : std_ulogic;
287 variable abs1, abs2 : signed(63 downto 0);
288 variable overflow : std_ulogic;
289 variable negative : std_ulogic;
290 variable zerohi, zerolo : std_ulogic;
291 variable msb_a, msb_b : std_ulogic;
292 variable a_lt : std_ulogic;
293 variable lv : Execute1ToLoadstore1Type;
294 variable irq_valid : std_ulogic;
295 variable exception : std_ulogic;
296 variable exception_nextpc : std_ulogic;
297 variable trapval : std_ulogic_vector(4 downto 0);
298 variable illegal : std_ulogic;
300 result := (others => '0');
301 result_with_carry := (others => '0');
303 newcrf := (others => '0');
306 v.e := Execute1ToWritebackInit;
307 lv := Execute1ToLoadstore1Init;
309 -- XER forwarding. To avoid having to track XER hazards, we
310 -- use the previously latched value.
312 -- If the XER was modified by a multiply or a divide, those are
313 -- single issue, we'll get the up to date value from decode2 from
314 -- the register file.
316 -- If it was modified by an instruction older than the previous
317 -- one in EX1, it will have also hit writeback and will be up
318 -- to date in decode2.
320 -- That leaves us with the case where it was updated by the previous
321 -- instruction in EX1. In that case, we can forward it back here.
323 -- This will break if we allow pipelining of multiply and divide,
324 -- but ideally, those should go via EX1 anyway and run as a state
325 -- machine from here.
327 -- One additional hazard to beware of is an XER:SO modifying instruction
328 -- in EX1 followed immediately by a store conditional. Due to our
329 -- writeback latency, the store will go down the LSU with the previous
330 -- XER value, thus the stcx. will set CR0:SO using an obsolete SO value.
332 -- We will need to handle that if we ever make stcx. not single issue
334 -- We always pass a valid XER value downto writeback even when
335 -- we aren't updating it, in order for XER:SO -> CR0:SO transfer
336 -- to work for RC instructions.
338 if r.e.write_xerc_enable = '1' then
339 v.e.xerc := r.e.xerc;
341 v.e.xerc := e_in.xerc;
345 v.mul_in_progress := '0';
346 v.div_in_progress := '0';
347 v.cntz_in_progress := '0';
349 -- signals to multiply unit
350 x_to_multiply <= Execute1ToMultiplyInit;
351 x_to_multiply.insn_type <= e_in.insn_type;
352 x_to_multiply.is_32bit <= e_in.is_32bit;
354 if e_in.is_32bit = '1' then
355 if e_in.is_signed = '1' then
356 x_to_multiply.data1 <= (others => a_in(31));
357 x_to_multiply.data1(31 downto 0) <= a_in(31 downto 0);
358 x_to_multiply.data2 <= (others => b_in(31));
359 x_to_multiply.data2(31 downto 0) <= b_in(31 downto 0);
361 x_to_multiply.data1 <= '0' & x"00000000" & a_in(31 downto 0);
362 x_to_multiply.data2 <= '0' & x"00000000" & b_in(31 downto 0);
365 if e_in.is_signed = '1' then
366 x_to_multiply.data1 <= a_in(63) & a_in;
367 x_to_multiply.data2 <= b_in(63) & b_in;
369 x_to_multiply.data1 <= '0' & a_in;
370 x_to_multiply.data2 <= '0' & b_in;
374 -- signals to divide unit
377 if e_in.is_signed = '1' then
378 if e_in.is_32bit = '1' then
386 -- take absolute values
388 abs1 := signed(a_in);
390 abs1 := - signed(a_in);
393 abs2 := signed(b_in);
395 abs2 := - signed(b_in);
398 x_to_divider <= Execute1ToDividerInit;
399 x_to_divider.is_signed <= e_in.is_signed;
400 x_to_divider.is_32bit <= e_in.is_32bit;
401 if e_in.insn_type = OP_MOD then
402 x_to_divider.is_modulus <= '1';
404 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
405 if e_in.is_32bit = '0' then
407 if e_in.insn_type = OP_DIVE then
408 x_to_divider.is_extended <= '1';
410 x_to_divider.dividend <= std_ulogic_vector(abs1);
411 x_to_divider.divisor <= std_ulogic_vector(abs2);
414 x_to_divider.is_extended <= '0';
415 if e_in.insn_type = OP_DIVE then -- extended forms
416 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
418 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
420 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
424 -- FIXME: run at 512MHz not core freq
425 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
426 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
429 if ctrl.msr(MSR_EE) = '1' then
430 if ctrl.dec(63) = '1' then
431 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#900#, 64));
432 report "IRQ valid: DEC";
434 elsif ext_irq_in = '1' then
435 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#500#, 64));
436 report "IRQ valid: External";
441 terminate_out <= '0';
444 f_out <= Execute1ToFetch1TypeInit;
445 -- send MSR[IR] and ~MSR[PR] up to fetch1
446 f_out.virt_mode <= ctrl.msr(MSR_IR);
447 f_out.priv_mode <= not ctrl.msr(MSR_PR);
449 -- Next insn adder used in a couple of places
450 next_nia := std_ulogic_vector(unsigned(e_in.nia) + 4);
452 -- rotator control signals
453 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
454 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
455 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
456 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
458 ctrl_tmp.irq_state <= WRITE_SRR0;
461 exception_nextpc := '0';
462 v.e.exc_write_enable := '0';
463 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
464 v.e.exc_write_data := e_in.nia;
466 if ctrl.irq_state = WRITE_SRR1 then
467 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
468 v.e.exc_write_data := ctrl.srr1;
469 v.e.exc_write_enable := '1';
470 ctrl_tmp.msr(MSR_SF) <= '1';
471 ctrl_tmp.msr(MSR_EE) <= '0';
472 ctrl_tmp.msr(MSR_PR) <= '0';
473 ctrl_tmp.msr(MSR_IR) <= '0';
474 ctrl_tmp.msr(MSR_DR) <= '0';
475 ctrl_tmp.msr(MSR_RI) <= '0';
476 ctrl_tmp.msr(MSR_LE) <= '1';
477 f_out.redirect <= '1';
478 f_out.virt_mode <= '0';
479 f_out.priv_mode <= '1';
480 f_out.redirect_nia <= ctrl.irq_nia;
481 v.e.valid := e_in.valid;
482 report "Writing SRR1: " & to_hstring(ctrl.srr1);
484 elsif irq_valid = '1' and e_in.valid = '1' then
485 -- we need two cycles to write srr0 and 1
486 -- will need more when we have to write HEIR
487 -- Don't deliver the interrupt until we have a valid instruction
488 -- coming in, so we have a valid NIA to put in SRR0.
490 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
492 elsif e_in.valid = '1' and ctrl.msr(MSR_PR) = '1' and
493 instr_is_privileged(e_in.insn_type, e_in.insn) then
494 -- generate a program interrupt
496 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
497 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
498 -- set bit 45 to indicate privileged instruction type interrupt
499 ctrl_tmp.srr1(63 - 45) <= '1';
500 report "privileged instruction";
502 elsif e_in.valid = '1' and e_in.unit = ALU then
504 report "execute nia " & to_hstring(e_in.nia);
507 v.e.write_reg := e_in.write_reg;
508 v.slow_op_dest := gspr_to_gpr(e_in.write_reg);
509 v.slow_op_rc := e_in.rc;
510 v.slow_op_oe := e_in.oe;
511 v.slow_op_xerc := v.e.xerc;
513 case_0: case e_in.insn_type is
516 -- we need two cycles to write srr0 and 1
517 -- will need more when we have to write HEIR
520 -- check bit 1 of the instruction is 1 so we know this is sc;
521 -- 0 would mean scv, so generate an illegal instruction interrupt
522 -- we need two cycles to write srr0 and 1
523 if e_in.insn(1) = '1' then
525 exception_nextpc := '1';
526 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#C00#, 64));
527 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
533 -- check bits 1-10 of the instruction to make sure it's attn
534 -- if not then it is illegal
535 if e_in.insn(10 downto 1) = "0100000000" then
536 terminate_out <= '1';
543 when OP_ADD | OP_CMP | OP_TRAP =>
544 if e_in.invert_a = '0' then
549 result_with_carry := ppc_adde(a_inv, b_in,
550 decode_input_carry(e_in.input_carry, v.e.xerc));
551 result := result_with_carry(63 downto 0);
552 carry_32 := result(32) xor a_inv(32) xor b_in(32);
553 carry_64 := result_with_carry(64);
554 if e_in.insn_type = OP_ADD then
555 if e_in.output_carry = '1' then
556 set_carry(v.e, carry_32, carry_64);
558 if e_in.oe = '1' then
560 calc_ov(a_inv(63), b_in(63), carry_64, result_with_carry(63)),
561 calc_ov(a_inv(31), b_in(31), carry_32, result_with_carry(31)));
565 -- trap, CMP and CMPL instructions
566 -- Note, we have done RB - RA, not RA - RB
567 if e_in.insn_type = OP_CMP then
568 l := insn_l(e_in.insn);
570 l := not e_in.is_32bit;
572 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
573 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
574 if zerolo = '1' and (l = '0' or zerohi = '1') then
587 if msb_a /= msb_b then
588 -- Subtraction might overflow, but
589 -- comparison is clear from MSB difference.
590 -- for signed, 0 is greater; for unsigned, 1 is greater
591 trapval := msb_a & msb_b & '0' & msb_b & msb_a;
593 -- Subtraction cannot overflow since MSBs are equal.
594 -- carry = 1 indicates RA is smaller (signed or unsigned)
595 a_lt := (not l and carry_32) or (l and carry_64);
596 trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
599 if e_in.insn_type = OP_CMP then
600 if e_in.is_signed = '1' then
601 newcrf := trapval(4 downto 2) & v.e.xerc.so;
603 newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
605 bf := insn_bf(e_in.insn);
606 crnum := to_integer(unsigned(bf));
607 v.e.write_cr_enable := '1';
608 v.e.write_cr_mask := num_to_fxm(crnum);
612 v.e.write_cr_data(hi downto lo) := newcrf;
615 -- trap instructions (tw, twi, td, tdi)
616 if or (trapval and insn_to(e_in.insn)) = '1' then
617 -- generate trap-type program interrupt
619 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
620 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
621 -- set bit 46 to say trap occurred
622 ctrl_tmp.srr1(63 - 46) <= '1';
627 when OP_AND | OP_OR | OP_XOR =>
628 result := logical_result;
631 f_out.redirect <= '1';
632 if (insn_aa(e_in.insn)) then
633 f_out.redirect_nia <= b_in;
635 f_out.redirect_nia <= std_ulogic_vector(signed(e_in.nia) + signed(b_in));
639 bo := insn_bo(e_in.insn);
640 bi := insn_bi(e_in.insn);
641 if bo(4-2) = '0' then
642 result := std_ulogic_vector(unsigned(a_in) - 1);
644 v.e.write_reg := fast_spr_num(SPR_CTR);
646 if ppc_bc_taken(bo, bi, e_in.cr, a_in) = 1 then
647 f_out.redirect <= '1';
648 if (insn_aa(e_in.insn)) then
649 f_out.redirect_nia <= b_in;
651 f_out.redirect_nia <= std_ulogic_vector(signed(e_in.nia) + signed(b_in));
656 -- read_data2 is target register (CTR, LR or TAR)
657 bo := insn_bo(e_in.insn);
658 bi := insn_bi(e_in.insn);
659 if bo(4-2) = '0' and e_in.insn(10) = '0' then
660 result := std_ulogic_vector(unsigned(a_in) - 1);
662 v.e.write_reg := fast_spr_num(SPR_CTR);
664 if ppc_bc_taken(bo, bi, e_in.cr, a_in) = 1 then
665 f_out.redirect <= '1';
666 f_out.redirect_nia <= b_in(63 downto 2) & "00";
670 f_out.redirect <= '1';
671 f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
672 f_out.priv_mode <= not b_in(MSR_PR);
673 f_out.redirect_nia <= a_in(63 downto 2) & "00"; -- srr0
674 -- Can't use msr_copy here because the partial function MSR
675 -- bits should be left unchanged, not zeroed.
676 ctrl_tmp.msr(63 downto 31) <= b_in(63 downto 31);
677 ctrl_tmp.msr(26 downto 22) <= b_in(26 downto 22);
678 ctrl_tmp.msr(15 downto 0) <= b_in(15 downto 0);
679 if b_in(MSR_PR) = '1' then
680 ctrl_tmp.msr(MSR_EE) <= '1';
681 ctrl_tmp.msr(MSR_IR) <= '1';
682 ctrl_tmp.msr(MSR_DR) <= '1';
686 result := ppc_cmpb(c_in, b_in);
690 v.cntz_in_progress := '1';
693 -- note data_len is a 1-hot encoding
694 negative := (e_in.data_len(0) and c_in(7)) or
695 (e_in.data_len(1) and c_in(15)) or
696 (e_in.data_len(2) and c_in(31));
697 result := (others => negative);
698 if e_in.data_len(2) = '1' then
699 result(31 downto 16) := c_in(31 downto 16);
701 if e_in.data_len(2) = '1' or e_in.data_len(1) = '1' then
702 result(15 downto 8) := c_in(15 downto 8);
704 result(7 downto 0) := c_in(7 downto 0);
707 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
708 if e_in.cr(31-crbit) = '1' then
715 cr_op := insn_cr(e_in.insn);
716 report "CR OP " & to_hstring(cr_op);
717 if cr_op(0) = '0' then -- MCRF
718 bf := insn_bf(e_in.insn);
719 bfa := insn_bfa(e_in.insn);
720 v.e.write_cr_enable := '1';
721 crnum := to_integer(unsigned(bf));
722 scrnum := to_integer(unsigned(bfa));
723 v.e.write_cr_mask := num_to_fxm(crnum);
728 newcrf := e_in.cr(hi downto lo);
734 v.e.write_cr_data(hi downto lo) := newcrf;
737 v.e.write_cr_enable := '1';
738 bt := insn_bt(e_in.insn);
739 ba := insn_ba(e_in.insn);
740 bb := insn_bb(e_in.insn);
741 btnum := 31 - to_integer(unsigned(bt));
742 banum := 31 - to_integer(unsigned(ba));
743 bbnum := 31 - to_integer(unsigned(bb));
744 -- Bits 5-8 of cr_op give the truth table of the requested
746 cr_operands := e_in.cr(banum) & e_in.cr(bbnum);
747 crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
748 v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
749 for i in 0 to 31 loop
751 v.e.write_cr_data(i) := crresult;
753 v.e.write_cr_data(i) := e_in.cr(i);
761 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
762 "=" & to_hstring(a_in);
764 if is_fast_spr(e_in.read_reg1) then
766 if decode_spr_num(e_in.insn) = SPR_XER then
767 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
768 result(63 downto 32) := (others => '0');
769 result(63-32) := v.e.xerc.so;
770 result(63-33) := v.e.xerc.ov;
771 result(63-34) := v.e.xerc.ca;
772 result(63-35 downto 63-43) := "000000000";
773 result(63-44) := v.e.xerc.ov32;
774 result(63-45) := v.e.xerc.ca32;
777 case decode_spr_num(e_in.insn) is
782 when 724 => -- LOG_ADDR SPR
783 result := log_wr_addr & r.log_addr_spr;
784 when 725 => -- LOG_DATA SPR
785 result := log_rd_data;
786 v.log_addr_spr := std_ulogic_vector(unsigned(r.log_addr_spr) + 1);
788 -- mfspr from unimplemented SPRs should be a nop in
789 -- supervisor mode and a program interrupt for user mode
791 if ctrl.msr(MSR_PR) = '1' then
797 if e_in.insn(20) = '0' then
799 result := x"00000000" & e_in.cr;
802 crnum := fxm_to_num(insn_fxm(e_in.insn));
803 result := (others => '0');
808 result(hi downto lo) := e_in.cr(hi downto lo);
814 v.e.write_cr_enable := '1';
815 if e_in.insn(20) = '0' then
817 v.e.write_cr_mask := insn_fxm(e_in.insn);
819 -- mtocrf: We require one hot priority encoding here
820 crnum := fxm_to_num(insn_fxm(e_in.insn));
821 v.e.write_cr_mask := num_to_fxm(crnum);
823 v.e.write_cr_data := c_in(31 downto 0);
825 if e_in.insn(16) = '1' then
826 -- just update EE and RI
827 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
828 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
830 -- Architecture says to leave out bits 3 (HV), 51 (ME)
831 -- and 63 (LE) (IBM bit numbering)
832 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
833 ctrl_tmp.msr(59 downto 13) <= c_in(59 downto 13);
834 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
835 if c_in(MSR_PR) = '1' then
836 ctrl_tmp.msr(MSR_EE) <= '1';
837 ctrl_tmp.msr(MSR_IR) <= '1';
838 ctrl_tmp.msr(MSR_DR) <= '1';
842 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
843 "=" & to_hstring(c_in);
844 if is_fast_spr(e_in.write_reg) then
847 if decode_spr_num(e_in.insn) = SPR_XER then
848 v.e.xerc.so := c_in(63-32);
849 v.e.xerc.ov := c_in(63-33);
850 v.e.xerc.ca := c_in(63-34);
851 v.e.xerc.ov32 := c_in(63-44);
852 v.e.xerc.ca32 := c_in(63-45);
853 v.e.write_xerc_enable := '1';
857 case decode_spr_num(e_in.insn) is
859 ctrl_tmp.dec <= c_in;
860 when 724 => -- LOG_ADDR SPR
861 v.log_addr_spr := c_in(31 downto 0);
863 -- mtspr to unimplemented SPRs should be a nop in
864 -- supervisor mode and a program interrupt for user mode
865 if ctrl.msr(MSR_PR) = '1' then
871 result := popcnt_result;
874 result := parity_result;
876 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
877 result := rotator_result;
878 if e_in.output_carry = '1' then
879 set_carry(v.e, rotator_carry, rotator_carry);
884 f_out.redirect <= '1';
885 f_out.redirect_nia <= next_nia;
890 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
892 v.mul_in_progress := '1';
894 x_to_multiply.valid <= '1';
896 when OP_DIV | OP_DIVE | OP_MOD =>
898 v.div_in_progress := '1';
900 x_to_divider.valid <= '1';
903 terminate_out <= '1';
907 v.e.rc := e_in.rc and e_in.valid;
909 -- Update LR on the next cycle after a branch link
911 -- WARNING: The LR update isn't tracked by our hazard tracker. This
912 -- will work (well I hope) because it only happens on branches
913 -- which will flush all decoded instructions. By the time
914 -- fetch catches up, we'll have the new LR. This will
915 -- *not* work properly however if we have a branch predictor,
916 -- in which case the solution would probably be to keep a
917 -- local cache of the updated LR in execute1 (flushed on
918 -- exceptions) that is used instead of the value from
919 -- decode when its content is valid.
920 if e_in.lr = '1' then
922 v.next_lr := next_nia;
924 report "Delayed LR update to " & to_hstring(next_nia);
928 elsif e_in.valid = '1' then
929 -- instruction for other units, i.e. LDST
930 v.ldst_nia := e_in.nia;
932 if e_in.unit = LDST then
936 elsif r.lr_update = '1' then
939 v.e.write_reg := fast_spr_num(SPR_LR);
941 elsif r.cntz_in_progress = '1' then
942 -- cnt[lt]z always takes two cycles
943 result := countzero_result;
945 v.e.write_reg := gpr_to_gspr(v.slow_op_dest);
946 v.e.rc := v.slow_op_rc;
947 v.e.xerc := v.slow_op_xerc;
949 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
950 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
951 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
952 if r.mul_in_progress = '1' then
953 result := multiply_to_x.write_reg_data;
954 overflow := multiply_to_x.overflow;
956 result := divider_to_x.write_reg_data;
957 overflow := divider_to_x.overflow;
960 v.e.write_reg := gpr_to_gspr(v.slow_op_dest);
961 v.e.rc := v.slow_op_rc;
962 v.e.xerc := v.slow_op_xerc;
963 v.e.write_xerc_enable := v.slow_op_oe;
964 -- We must test oe because the RC update code in writeback
965 -- will use the xerc value to set CR0:SO so we must not clobber
966 -- xerc if OE wasn't set.
967 if v.slow_op_oe = '1' then
968 v.e.xerc.ov := overflow;
969 v.e.xerc.ov32 := overflow;
970 v.e.xerc.so := v.slow_op_xerc.so or overflow;
975 v.mul_in_progress := r.mul_in_progress;
976 v.div_in_progress := r.div_in_progress;
980 if illegal = '1' then
982 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
983 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
984 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
985 -- set bit 44 to indicate we have an illegal
986 ctrl_tmp.srr1(63 - 44) <= '1';
989 if exception = '1' then
990 v.e.exc_write_enable := '1';
991 if exception_nextpc = '1' then
992 v.e.exc_write_data := next_nia;
994 ctrl_tmp.irq_state <= WRITE_SRR1;
998 v.e.write_data := result;
999 v.e.write_enable := result_en;
1001 -- generate DSI or DSegI for load/store exceptions
1002 -- or ISI or ISegI for instruction fetch exceptions
1003 if l_in.exception = '1' then
1004 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
1005 if l_in.instr_fault = '0' then
1006 if l_in.segment_fault = '0' then
1007 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#300#, 64));
1009 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#380#, 64));
1012 if l_in.segment_fault = '0' then
1013 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
1014 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
1015 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
1016 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
1017 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#400#, 64));
1019 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#480#, 64));
1022 v.e.exc_write_enable := '1';
1023 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1024 v.e.exc_write_data := r.ldst_nia;
1025 report "ldst exception writing srr0=" & to_hstring(r.ldst_nia);
1026 ctrl_tmp.irq_state <= WRITE_SRR1;
1027 v.e.valid := '1'; -- complete the original load or store
1030 -- Outputs to loadstore1 (async)
1031 lv.op := e_in.insn_type;
1036 lv.write_reg := gspr_to_gpr(e_in.write_reg);
1037 lv.length := e_in.data_len;
1038 lv.byte_reverse := e_in.byte_reverse;
1039 lv.sign_extend := e_in.sign_extend;
1040 lv.update := e_in.update;
1041 lv.update_reg := gspr_to_gpr(e_in.read_reg1);
1042 lv.xerc := v.e.xerc;
1043 lv.reserve := e_in.reserve;
1045 lv.insn := e_in.insn;
1046 -- decode l*cix and st*cix instructions here
1047 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1048 e_in.insn(5 downto 1) = "10101" then
1051 lv.virt_mode := ctrl.msr(MSR_DR);
1052 lv.priv_mode := not ctrl.msr(MSR_PR);
1061 flush_out <= f_out.redirect;
1063 exception_log <= exception;
1064 irq_valid_log <= irq_valid;
1067 ex1_log : process(clk)
1069 if rising_edge(clk) then
1070 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1071 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1074 std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
1083 log_out <= log_data;
1084 end architecture behaviour;