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 busy_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 f : Execute1ToFetch1Type;
53 terminate: std_ulogic;
54 lr_update : std_ulogic;
55 next_lr : std_ulogic_vector(63 downto 0);
56 mul_in_progress : std_ulogic;
57 div_in_progress : std_ulogic;
58 cntz_in_progress : std_ulogic;
59 slow_op_insn : insn_type_t;
60 slow_op_dest : gpr_index_t;
61 slow_op_rc : std_ulogic;
62 slow_op_oe : std_ulogic;
63 slow_op_xerc : xer_common_t;
64 last_nia : std_ulogic_vector(63 downto 0);
65 log_addr_spr : std_ulogic_vector(31 downto 0);
67 constant reg_type_init : reg_type :=
68 (e => Execute1ToWritebackInit, f => Execute1ToFetch1Init,
69 busy => '0', lr_update => '0', terminate => '0',
70 mul_in_progress => '0', div_in_progress => '0', cntz_in_progress => '0',
71 slow_op_insn => OP_ILLEGAL, slow_op_rc => '0', slow_op_oe => '0', slow_op_xerc => xerc_init,
72 next_lr => (others => '0'), last_nia => (others => '0'), others => (others => '0'));
74 signal r, rin : reg_type;
76 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
78 signal valid_in : std_ulogic;
79 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
80 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
81 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
82 signal rot_sign_ext: std_ulogic;
83 signal rotator_result: std_ulogic_vector(63 downto 0);
84 signal rotator_carry: std_ulogic;
85 signal logical_result: std_ulogic_vector(63 downto 0);
86 signal countzero_result: std_ulogic_vector(63 downto 0);
89 signal x_to_multiply: Execute1ToMultiplyType;
90 signal multiply_to_x: MultiplyToExecute1Type;
93 signal x_to_divider: Execute1ToDividerType;
94 signal divider_to_x: DividerToExecute1Type;
96 -- signals for logging
97 signal exception_log : std_ulogic;
98 signal irq_valid_log : std_ulogic;
99 signal log_data : std_ulogic_vector(14 downto 0);
101 type privilege_level is (USER, SUPER);
102 type op_privilege_array is array(insn_type_t) of privilege_level;
103 constant op_privilege: op_privilege_array := (
112 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
115 if op_privilege(op) = SUPER then
117 elsif op = OP_MFSPR or op = OP_MTSPR then
118 return insn(20) = '1';
124 procedure set_carry(e: inout Execute1ToWritebackType;
125 carry32 : in std_ulogic;
126 carry : in std_ulogic) is
128 e.xerc.ca32 := carry32;
130 e.write_xerc_enable := '1';
133 procedure set_ov(e: inout Execute1ToWritebackType;
135 ov32 : in std_ulogic) is
142 e.write_xerc_enable := '1';
145 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
146 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
148 return (ca xor msb_r) and not (msb_a xor msb_b);
151 function decode_input_carry(ic : carry_in_t;
152 xerc : xer_common_t) return std_ulogic is
164 function msr_copy(msr: std_ulogic_vector(63 downto 0))
165 return std_ulogic_vector is
166 variable msr_out: std_ulogic_vector(63 downto 0);
169 -- Defined MSR bits are classified as either full func-
170 -- tion or partial function. Full function MSR bits are
171 -- saved in SRR1 or HSRR1 when an interrupt other
172 -- than a System Call Vectored interrupt occurs and
173 -- restored by rfscv, rfid, or hrfid, while partial func-
174 -- tion MSR bits are not saved or restored.
175 -- Full function MSR bits lie in the range 0:32, 37:41, and
176 -- 48:63, and partial function MSR bits lie in the range
177 -- 33:36 and 42:47. (Note this is IBM bit numbering).
178 msr_out := (others => '0');
179 msr_out(63 downto 31) := msr(63 downto 31);
180 msr_out(26 downto 22) := msr(26 downto 22);
181 msr_out(15 downto 0) := msr(15 downto 0);
187 rotator_0: entity work.rotator
191 shift => b_in(6 downto 0),
193 is_32bit => e_in.is_32bit,
194 right_shift => right_shift,
195 arith => e_in.is_signed,
196 clear_left => rot_clear_left,
197 clear_right => rot_clear_right,
198 sign_ext_rs => rot_sign_ext,
199 result => rotator_result,
200 carry_out => rotator_carry
203 logical_0: entity work.logical
207 op => e_in.insn_type,
208 invert_in => e_in.invert_a,
209 invert_out => e_in.invert_out,
210 result => logical_result,
211 datalen => e_in.data_len
214 countzero_0: entity work.zero_counter
218 count_right => e_in.insn(10),
219 is_32bit => e_in.is_32bit,
220 result => countzero_result
223 multiply_0: entity work.multiply
226 m_in => x_to_multiply,
227 m_out => multiply_to_x
230 divider_0: entity work.divider
234 d_in => x_to_divider,
235 d_out => divider_to_x
238 dbg_msr_out <= ctrl.msr;
239 log_rd_addr <= r.log_addr_spr;
241 a_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data1 = '1' else e_in.read_data1;
242 b_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data2 = '1' else e_in.read_data2;
243 c_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data3 = '1' else e_in.read_data3;
245 busy_out <= l_in.busy or r.busy;
246 valid_in <= e_in.valid and not busy_out;
248 terminate_out <= r.terminate;
250 execute1_0: process(clk)
252 if rising_edge(clk) then
255 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
256 ctrl.irq_state <= WRITE_SRR0;
260 assert not (r.lr_update = '1' and valid_in = '1')
261 report "LR update collision with valid in EX1"
263 if r.lr_update = '1' then
264 report "LR update to " & to_hstring(r.next_lr);
270 execute1_1: process(all)
271 variable v : reg_type;
272 variable a_inv : std_ulogic_vector(63 downto 0);
273 variable result : std_ulogic_vector(63 downto 0);
274 variable newcrf : std_ulogic_vector(3 downto 0);
275 variable result_with_carry : std_ulogic_vector(64 downto 0);
276 variable result_en : std_ulogic;
277 variable crnum : crnum_t;
278 variable crbit : integer range 0 to 31;
279 variable scrnum : crnum_t;
280 variable lo, hi : integer;
281 variable sh, mb, me : std_ulogic_vector(5 downto 0);
282 variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
283 variable bo, bi : std_ulogic_vector(4 downto 0);
284 variable bf, bfa : std_ulogic_vector(2 downto 0);
285 variable cr_op : std_ulogic_vector(9 downto 0);
286 variable cr_operands : std_ulogic_vector(1 downto 0);
287 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
288 variable btnum, banum, bbnum : integer range 0 to 31;
289 variable crresult : std_ulogic;
290 variable l : std_ulogic;
291 variable next_nia : std_ulogic_vector(63 downto 0);
292 variable carry_32, carry_64 : std_ulogic;
293 variable sign1, sign2 : std_ulogic;
294 variable abs1, abs2 : signed(63 downto 0);
295 variable overflow : std_ulogic;
296 variable zerohi, zerolo : std_ulogic;
297 variable msb_a, msb_b : std_ulogic;
298 variable a_lt : std_ulogic;
299 variable lv : Execute1ToLoadstore1Type;
300 variable irq_valid : std_ulogic;
301 variable exception : std_ulogic;
302 variable exception_nextpc : std_ulogic;
303 variable trapval : std_ulogic_vector(4 downto 0);
304 variable illegal : std_ulogic;
305 variable is_branch : std_ulogic;
306 variable taken_branch : std_ulogic;
307 variable abs_branch : std_ulogic;
308 variable spr_val : std_ulogic_vector(63 downto 0);
310 result := (others => '0');
311 result_with_carry := (others => '0');
313 newcrf := (others => '0');
319 v.e := Execute1ToWritebackInit;
320 lv := Execute1ToLoadstore1Init;
323 -- XER forwarding. To avoid having to track XER hazards, we
324 -- use the previously latched value.
326 -- If the XER was modified by a multiply or a divide, those are
327 -- single issue, we'll get the up to date value from decode2 from
328 -- the register file.
330 -- If it was modified by an instruction older than the previous
331 -- one in EX1, it will have also hit writeback and will be up
332 -- to date in decode2.
334 -- That leaves us with the case where it was updated by the previous
335 -- instruction in EX1. In that case, we can forward it back here.
337 -- This will break if we allow pipelining of multiply and divide,
338 -- but ideally, those should go via EX1 anyway and run as a state
339 -- machine from here.
341 -- One additional hazard to beware of is an XER:SO modifying instruction
342 -- in EX1 followed immediately by a store conditional. Due to our
343 -- writeback latency, the store will go down the LSU with the previous
344 -- XER value, thus the stcx. will set CR0:SO using an obsolete SO value.
346 -- We will need to handle that if we ever make stcx. not single issue
348 -- We always pass a valid XER value downto writeback even when
349 -- we aren't updating it, in order for XER:SO -> CR0:SO transfer
350 -- to work for RC instructions.
352 if r.e.write_xerc_enable = '1' then
353 v.e.xerc := r.e.xerc;
355 v.e.xerc := e_in.xerc;
359 v.mul_in_progress := '0';
360 v.div_in_progress := '0';
361 v.cntz_in_progress := '0';
363 -- signals to multiply and divide units
366 if e_in.is_signed = '1' then
367 if e_in.is_32bit = '1' then
375 -- take absolute values
377 abs1 := signed(a_in);
379 abs1 := - signed(a_in);
382 abs2 := signed(b_in);
384 abs2 := - signed(b_in);
387 x_to_multiply <= Execute1ToMultiplyInit;
388 x_to_multiply.is_32bit <= e_in.is_32bit;
390 x_to_divider <= Execute1ToDividerInit;
391 x_to_divider.is_signed <= e_in.is_signed;
392 x_to_divider.is_32bit <= e_in.is_32bit;
393 if e_in.insn_type = OP_MOD then
394 x_to_divider.is_modulus <= '1';
397 x_to_multiply.neg_result <= sign1 xor sign2;
398 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
399 if e_in.is_32bit = '0' then
401 x_to_multiply.data1 <= std_ulogic_vector(abs1);
402 x_to_multiply.data2 <= std_ulogic_vector(abs2);
403 if e_in.insn_type = OP_DIVE then
404 x_to_divider.is_extended <= '1';
406 x_to_divider.dividend <= std_ulogic_vector(abs1);
407 x_to_divider.divisor <= std_ulogic_vector(abs2);
410 x_to_multiply.data1 <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
411 x_to_multiply.data2 <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
412 x_to_divider.is_extended <= '0';
413 if e_in.insn_type = OP_DIVE then -- extended forms
414 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
416 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
418 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
422 -- FIXME: run at 512MHz not core freq
423 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
424 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
427 if ctrl.msr(MSR_EE) = '1' then
428 if ctrl.dec(63) = '1' then
429 v.f.redirect_nia := std_logic_vector(to_unsigned(16#900#, 64));
430 report "IRQ valid: DEC";
432 elsif ext_irq_in = '1' then
433 v.f.redirect_nia := std_logic_vector(to_unsigned(16#500#, 64));
434 report "IRQ valid: External";
442 -- send MSR[IR] and ~MSR[PR] up to fetch1
443 v.f.virt_mode := ctrl.msr(MSR_IR);
444 v.f.priv_mode := not ctrl.msr(MSR_PR);
446 -- Next insn adder used in a couple of places
447 next_nia := std_ulogic_vector(unsigned(e_in.nia) + 4);
449 -- rotator control signals
450 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
451 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
452 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
453 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
455 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
456 ctrl_tmp.irq_state <= WRITE_SRR0;
459 exception_nextpc := '0';
460 v.e.exc_write_enable := '0';
461 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
462 v.e.exc_write_data := e_in.nia;
463 if valid_in = '1' then
464 v.last_nia := e_in.nia;
467 if ctrl.irq_state = WRITE_SRR1 then
468 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
469 v.e.exc_write_data := ctrl.srr1;
470 v.e.exc_write_enable := '1';
471 ctrl_tmp.msr(MSR_SF) <= '1';
472 ctrl_tmp.msr(MSR_EE) <= '0';
473 ctrl_tmp.msr(MSR_PR) <= '0';
474 ctrl_tmp.msr(MSR_IR) <= '0';
475 ctrl_tmp.msr(MSR_DR) <= '0';
476 ctrl_tmp.msr(MSR_RI) <= '0';
477 ctrl_tmp.msr(MSR_LE) <= '1';
479 report "Writing SRR1: " & to_hstring(ctrl.srr1);
481 elsif irq_valid = '1' and valid_in = '1' then
482 -- we need two cycles to write srr0 and 1
483 -- will need more when we have to write HEIR
484 -- Don't deliver the interrupt until we have a valid instruction
485 -- coming in, so we have a valid NIA to put in SRR0.
488 elsif valid_in = '1' and ctrl.msr(MSR_PR) = '1' and
489 instr_is_privileged(e_in.insn_type, e_in.insn) then
490 -- generate a program interrupt
492 v.f.redirect_nia := std_logic_vector(to_unsigned(16#700#, 64));
493 -- set bit 45 to indicate privileged instruction type interrupt
494 ctrl_tmp.srr1(63 - 45) <= '1';
495 report "privileged instruction";
497 elsif valid_in = '1' and e_in.unit = ALU then
499 report "execute nia " & to_hstring(e_in.nia);
502 v.e.write_reg := e_in.write_reg;
503 v.slow_op_insn := e_in.insn_type;
504 v.slow_op_dest := gspr_to_gpr(e_in.write_reg);
505 v.slow_op_rc := e_in.rc;
506 v.slow_op_oe := e_in.oe;
507 v.slow_op_xerc := v.e.xerc;
509 case_0: case e_in.insn_type is
512 -- we need two cycles to write srr0 and 1
513 -- will need more when we have to write HEIR
516 -- check bit 1 of the instruction is 1 so we know this is sc;
517 -- 0 would mean scv, so generate an illegal instruction interrupt
518 -- we need two cycles to write srr0 and 1
519 if e_in.insn(1) = '1' then
521 exception_nextpc := '1';
522 v.f.redirect_nia := std_logic_vector(to_unsigned(16#C00#, 64));
528 -- check bits 1-10 of the instruction to make sure it's attn
529 -- if not then it is illegal
530 if e_in.insn(10 downto 1) = "0100000000" then
538 when OP_ADD | OP_CMP | OP_TRAP =>
539 if e_in.invert_a = '0' then
544 result_with_carry := ppc_adde(a_inv, b_in,
545 decode_input_carry(e_in.input_carry, v.e.xerc));
546 result := result_with_carry(63 downto 0);
547 carry_32 := result(32) xor a_inv(32) xor b_in(32);
548 carry_64 := result_with_carry(64);
549 if e_in.insn_type = OP_ADD then
550 if e_in.output_carry = '1' then
551 set_carry(v.e, carry_32, carry_64);
553 if e_in.oe = '1' then
555 calc_ov(a_inv(63), b_in(63), carry_64, result_with_carry(63)),
556 calc_ov(a_inv(31), b_in(31), carry_32, result_with_carry(31)));
560 -- trap, CMP and CMPL instructions
561 -- Note, we have done RB - RA, not RA - RB
562 if e_in.insn_type = OP_CMP then
563 l := insn_l(e_in.insn);
565 l := not e_in.is_32bit;
567 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
568 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
569 if zerolo = '1' and (l = '0' or zerohi = '1') then
582 if msb_a /= msb_b then
583 -- Subtraction might overflow, but
584 -- comparison is clear from MSB difference.
585 -- for signed, 0 is greater; for unsigned, 1 is greater
586 trapval := msb_a & msb_b & '0' & msb_b & msb_a;
588 -- Subtraction cannot overflow since MSBs are equal.
589 -- carry = 1 indicates RA is smaller (signed or unsigned)
590 a_lt := (not l and carry_32) or (l and carry_64);
591 trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
594 if e_in.insn_type = OP_CMP then
595 if e_in.is_signed = '1' then
596 newcrf := trapval(4 downto 2) & v.e.xerc.so;
598 newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
600 bf := insn_bf(e_in.insn);
601 crnum := to_integer(unsigned(bf));
602 v.e.write_cr_enable := '1';
603 v.e.write_cr_mask := num_to_fxm(crnum);
607 v.e.write_cr_data(hi downto lo) := newcrf;
610 -- trap instructions (tw, twi, td, tdi)
611 if or (trapval and insn_to(e_in.insn)) = '1' then
612 -- generate trap-type program interrupt
614 v.f.redirect_nia := std_logic_vector(to_unsigned(16#700#, 64));
615 -- set bit 46 to say trap occurred
616 ctrl_tmp.srr1(63 - 46) <= '1';
621 when OP_AND | OP_OR | OP_XOR | OP_POPCNT | OP_PRTY | OP_CMPB | OP_EXTS =>
622 result := logical_result;
627 abs_branch := insn_aa(e_in.insn);
630 bo := insn_bo(e_in.insn);
631 bi := insn_bi(e_in.insn);
632 if bo(4-2) = '0' then
633 result := std_ulogic_vector(unsigned(a_in) - 1);
635 v.e.write_reg := fast_spr_num(SPR_CTR);
638 taken_branch := ppc_bc_taken(bo, bi, e_in.cr, a_in);
639 abs_branch := insn_aa(e_in.insn);
642 -- read_data2 is target register (CTR, LR or TAR)
643 bo := insn_bo(e_in.insn);
644 bi := insn_bi(e_in.insn);
645 if bo(4-2) = '0' and e_in.insn(10) = '0' then
646 result := std_ulogic_vector(unsigned(a_in) - 1);
648 v.e.write_reg := fast_spr_num(SPR_CTR);
651 taken_branch := ppc_bc_taken(bo, bi, e_in.cr, a_in);
655 v.f.virt_mode := a_in(MSR_IR) or a_in(MSR_PR);
656 v.f.priv_mode := not a_in(MSR_PR);
657 -- Can't use msr_copy here because the partial function MSR
658 -- bits should be left unchanged, not zeroed.
659 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
660 ctrl_tmp.msr(26 downto 22) <= a_in(26 downto 22);
661 ctrl_tmp.msr(15 downto 0) <= a_in(15 downto 0);
662 if a_in(MSR_PR) = '1' then
663 ctrl_tmp.msr(MSR_EE) <= '1';
664 ctrl_tmp.msr(MSR_IR) <= '1';
665 ctrl_tmp.msr(MSR_DR) <= '1';
667 -- mark this as a branch so CFAR gets updated
674 v.cntz_in_progress := '1';
677 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
678 if e_in.cr(31-crbit) = '1' then
685 cr_op := insn_cr(e_in.insn);
686 report "CR OP " & to_hstring(cr_op);
687 if cr_op(0) = '0' then -- MCRF
688 bf := insn_bf(e_in.insn);
689 bfa := insn_bfa(e_in.insn);
690 v.e.write_cr_enable := '1';
691 crnum := to_integer(unsigned(bf));
692 scrnum := to_integer(unsigned(bfa));
693 v.e.write_cr_mask := num_to_fxm(crnum);
698 newcrf := e_in.cr(hi downto lo);
704 v.e.write_cr_data(hi downto lo) := newcrf;
707 v.e.write_cr_enable := '1';
708 bt := insn_bt(e_in.insn);
709 ba := insn_ba(e_in.insn);
710 bb := insn_bb(e_in.insn);
711 btnum := 31 - to_integer(unsigned(bt));
712 banum := 31 - to_integer(unsigned(ba));
713 bbnum := 31 - to_integer(unsigned(bb));
714 -- Bits 5-8 of cr_op give the truth table of the requested
716 cr_operands := e_in.cr(banum) & e_in.cr(bbnum);
717 crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
718 v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
719 for i in 0 to 31 loop
721 v.e.write_cr_data(i) := crresult;
723 v.e.write_cr_data(i) := e_in.cr(i);
731 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
732 "=" & to_hstring(a_in);
734 if is_fast_spr(e_in.read_reg1) then
736 if decode_spr_num(e_in.insn) = SPR_XER then
737 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
738 result(63 downto 32) := (others => '0');
739 result(63-32) := v.e.xerc.so;
740 result(63-33) := v.e.xerc.ov;
741 result(63-34) := v.e.xerc.ca;
742 result(63-35 downto 63-43) := "000000000";
743 result(63-44) := v.e.xerc.ov32;
744 result(63-45) := v.e.xerc.ca32;
748 case decode_spr_num(e_in.insn) is
752 spr_val(63 downto 32) := (others => '0');
753 spr_val(31 downto 0) := ctrl.tb(63 downto 32);
757 spr_val := ctrl.cfar;
758 when 724 => -- LOG_ADDR SPR
759 spr_val := log_wr_addr & r.log_addr_spr;
760 when 725 => -- LOG_DATA SPR
761 spr_val := log_rd_data;
762 v.log_addr_spr := std_ulogic_vector(unsigned(r.log_addr_spr) + 1);
764 -- mfspr from unimplemented SPRs should be a nop in
765 -- supervisor mode and a program interrupt for user mode
766 if ctrl.msr(MSR_PR) = '1' then
773 if e_in.insn(20) = '0' then
775 result := x"00000000" & e_in.cr;
778 crnum := fxm_to_num(insn_fxm(e_in.insn));
779 result := (others => '0');
784 result(hi downto lo) := e_in.cr(hi downto lo);
790 v.e.write_cr_enable := '1';
791 if e_in.insn(20) = '0' then
793 v.e.write_cr_mask := insn_fxm(e_in.insn);
795 -- mtocrf: We require one hot priority encoding here
796 crnum := fxm_to_num(insn_fxm(e_in.insn));
797 v.e.write_cr_mask := num_to_fxm(crnum);
799 v.e.write_cr_data := c_in(31 downto 0);
801 if e_in.insn(16) = '1' then
802 -- just update EE and RI
803 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
804 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
806 -- Architecture says to leave out bits 3 (HV), 51 (ME)
807 -- and 63 (LE) (IBM bit numbering)
808 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
809 ctrl_tmp.msr(59 downto 13) <= c_in(59 downto 13);
810 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
811 if c_in(MSR_PR) = '1' then
812 ctrl_tmp.msr(MSR_EE) <= '1';
813 ctrl_tmp.msr(MSR_IR) <= '1';
814 ctrl_tmp.msr(MSR_DR) <= '1';
818 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
819 "=" & to_hstring(c_in);
820 if is_fast_spr(e_in.write_reg) then
823 if decode_spr_num(e_in.insn) = SPR_XER then
824 v.e.xerc.so := c_in(63-32);
825 v.e.xerc.ov := c_in(63-33);
826 v.e.xerc.ca := c_in(63-34);
827 v.e.xerc.ov32 := c_in(63-44);
828 v.e.xerc.ca32 := c_in(63-45);
829 v.e.write_xerc_enable := '1';
833 case decode_spr_num(e_in.insn) is
835 ctrl_tmp.dec <= c_in;
836 when 724 => -- LOG_ADDR SPR
837 v.log_addr_spr := c_in(31 downto 0);
839 -- mtspr to unimplemented SPRs should be a nop in
840 -- supervisor mode and a program interrupt for user mode
841 if ctrl.msr(MSR_PR) = '1' then
846 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
847 result := rotator_result;
848 if e_in.output_carry = '1' then
849 set_carry(v.e, rotator_carry, rotator_carry);
855 v.f.redirect_nia := next_nia;
860 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
862 v.mul_in_progress := '1';
864 x_to_multiply.valid <= '1';
866 when OP_DIV | OP_DIVE | OP_MOD =>
868 v.div_in_progress := '1';
870 x_to_divider.valid <= '1';
877 v.e.rc := e_in.rc and valid_in;
879 -- Mispredicted branches cause a redirect
880 if is_branch = '1' then
881 if taken_branch = '1' then
882 ctrl_tmp.cfar <= e_in.nia;
884 if e_in.br_pred = '0' then
885 if abs_branch = '1' then
886 v.f.redirect_nia := b_in;
888 v.f.redirect_nia := std_ulogic_vector(signed(e_in.nia) + signed(b_in));
891 v.f.redirect_nia := next_nia;
893 if taken_branch /= e_in.br_pred then
898 -- Update LR on the next cycle after a branch link
899 -- If we're not writing back anything else, we can write back LR
900 -- this cycle, otherwise we take an extra cycle. We use the
901 -- exc_write path since next_nia is written through that path
903 if e_in.lr = '1' then
904 if result_en = '0' then
905 v.e.exc_write_enable := '1';
906 v.e.exc_write_data := next_nia;
907 v.e.exc_write_reg := fast_spr_num(SPR_LR);
910 v.next_lr := next_nia;
912 report "Delayed LR update to " & to_hstring(next_nia);
917 elsif valid_in = '1' then
918 -- instruction for other units, i.e. LDST
919 if e_in.unit = LDST then
923 elsif r.f.redirect = '1' then
925 elsif r.lr_update = '1' then
926 v.e.exc_write_enable := '1';
927 v.e.exc_write_data := r.next_lr;
928 v.e.exc_write_reg := fast_spr_num(SPR_LR);
930 elsif r.cntz_in_progress = '1' then
931 -- cnt[lt]z always takes two cycles
932 result := countzero_result;
934 v.e.write_reg := gpr_to_gspr(v.slow_op_dest);
935 v.e.rc := v.slow_op_rc;
936 v.e.xerc := v.slow_op_xerc;
938 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
939 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
940 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
941 if r.mul_in_progress = '1' then
943 case r.slow_op_insn is
945 result := multiply_to_x.result(63 downto 32) &
946 multiply_to_x.result(63 downto 32);
948 result := multiply_to_x.result(127 downto 64);
951 result := multiply_to_x.result(63 downto 0);
952 overflow := multiply_to_x.overflow;
955 result := divider_to_x.write_reg_data;
956 overflow := divider_to_x.overflow;
959 v.e.write_reg := gpr_to_gspr(v.slow_op_dest);
960 v.e.rc := v.slow_op_rc;
961 v.e.xerc := v.slow_op_xerc;
962 v.e.write_xerc_enable := v.slow_op_oe;
963 -- We must test oe because the RC update code in writeback
964 -- will use the xerc value to set CR0:SO so we must not clobber
965 -- xerc if OE wasn't set.
966 if v.slow_op_oe = '1' then
967 v.e.xerc.ov := overflow;
968 v.e.xerc.ov32 := overflow;
969 v.e.xerc.so := v.slow_op_xerc.so or overflow;
974 v.mul_in_progress := r.mul_in_progress;
975 v.div_in_progress := r.div_in_progress;
979 if illegal = '1' then
981 v.f.redirect_nia := std_logic_vector(to_unsigned(16#700#, 64));
982 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
983 -- set bit 44 to indicate we have an illegal
984 ctrl_tmp.srr1(63 - 44) <= '1';
987 if exception = '1' then
988 v.e.exc_write_enable := '1';
989 if exception_nextpc = '1' then
990 v.e.exc_write_data := next_nia;
994 v.e.write_data := result;
995 v.e.write_enable := result_en and not exception;
997 -- generate DSI or DSegI for load/store exceptions
998 -- or ISI or ISegI for instruction fetch exceptions
999 if l_in.exception = '1' then
1000 if l_in.instr_fault = '0' then
1001 if l_in.segment_fault = '0' then
1002 v.f.redirect_nia := std_logic_vector(to_unsigned(16#300#, 64));
1004 v.f.redirect_nia := std_logic_vector(to_unsigned(16#380#, 64));
1007 if l_in.segment_fault = '0' then
1008 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
1009 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
1010 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
1011 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
1012 v.f.redirect_nia := std_logic_vector(to_unsigned(16#400#, 64));
1014 v.f.redirect_nia := std_logic_vector(to_unsigned(16#480#, 64));
1017 v.e.exc_write_enable := '1';
1018 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1019 v.e.exc_write_data := r.last_nia;
1020 report "ldst exception writing srr0=" & to_hstring(r.last_nia);
1023 if exception = '1' or l_in.exception = '1' then
1024 ctrl_tmp.irq_state <= WRITE_SRR1;
1025 v.f.redirect := '1';
1026 v.f.virt_mode := '0';
1027 v.f.priv_mode := '1';
1030 if v.f.redirect = '1' then
1035 -- Outputs to loadstore1 (async)
1036 lv.op := e_in.insn_type;
1041 lv.write_reg := gspr_to_gpr(e_in.write_reg);
1042 lv.length := e_in.data_len;
1043 lv.byte_reverse := e_in.byte_reverse;
1044 lv.sign_extend := e_in.sign_extend;
1045 lv.update := e_in.update;
1046 lv.update_reg := gspr_to_gpr(e_in.read_reg1);
1047 lv.xerc := v.e.xerc;
1048 lv.reserve := e_in.reserve;
1050 lv.insn := e_in.insn;
1051 -- decode l*cix and st*cix instructions here
1052 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1053 e_in.insn(5 downto 1) = "10101" then
1056 lv.virt_mode := ctrl.msr(MSR_DR);
1057 lv.priv_mode := not ctrl.msr(MSR_PR);
1066 flush_out <= f_out.redirect;
1068 exception_log <= exception;
1069 irq_valid_log <= irq_valid;
1072 ex1_log : process(clk)
1074 if rising_edge(clk) then
1075 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1076 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1079 std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
1088 log_out <= log_data;
1089 end architecture behaviour;