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;
31 ext_irq_in : std_ulogic;
34 l_out : out Execute1ToLoadstore1Type;
35 f_out : out Execute1ToFetch1Type;
37 e_out : out Execute1ToWritebackType;
39 dbg_msr_out : out std_ulogic_vector(63 downto 0);
41 icache_inval : out std_ulogic;
42 terminate_out : out std_ulogic;
44 log_out : out std_ulogic_vector(14 downto 0);
45 log_rd_addr : out std_ulogic_vector(31 downto 0);
46 log_rd_data : in std_ulogic_vector(63 downto 0);
47 log_wr_addr : in std_ulogic_vector(31 downto 0)
51 architecture behaviour of execute1 is
52 type reg_type is record
53 e : Execute1ToWritebackType;
54 f : Execute1ToFetch1Type;
56 terminate: std_ulogic;
57 trace_next : std_ulogic;
58 prev_op : insn_type_t;
59 lr_update : std_ulogic;
60 next_lr : std_ulogic_vector(63 downto 0);
61 mul_in_progress : std_ulogic;
62 mul_finish : std_ulogic;
63 div_in_progress : std_ulogic;
64 cntz_in_progress : std_ulogic;
65 slow_op_insn : insn_type_t;
66 slow_op_dest : gpr_index_t;
67 slow_op_rc : std_ulogic;
68 slow_op_oe : std_ulogic;
69 slow_op_xerc : xer_common_t;
70 last_nia : std_ulogic_vector(63 downto 0);
71 log_addr_spr : std_ulogic_vector(31 downto 0);
73 constant reg_type_init : reg_type :=
74 (e => Execute1ToWritebackInit, f => Execute1ToFetch1Init,
75 busy => '0', lr_update => '0', terminate => '0', trace_next => '0', prev_op => OP_ILLEGAL,
76 mul_in_progress => '0', mul_finish => '0', div_in_progress => '0', cntz_in_progress => '0',
77 slow_op_insn => OP_ILLEGAL, slow_op_rc => '0', slow_op_oe => '0', slow_op_xerc => xerc_init,
78 next_lr => (others => '0'), last_nia => (others => '0'), others => (others => '0'));
80 signal r, rin : reg_type;
82 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
83 signal cr_in : std_ulogic_vector(31 downto 0);
85 signal valid_in : std_ulogic;
86 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
87 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
88 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
89 signal rot_sign_ext: std_ulogic;
90 signal rotator_result: std_ulogic_vector(63 downto 0);
91 signal rotator_carry: std_ulogic;
92 signal logical_result: std_ulogic_vector(63 downto 0);
93 signal countzero_result: std_ulogic_vector(63 downto 0);
96 signal x_to_multiply: MultiplyInputType;
97 signal multiply_to_x: MultiplyOutputType;
100 signal x_to_divider: Execute1ToDividerType;
101 signal divider_to_x: DividerToExecute1Type;
103 -- random number generator signals
104 signal random_raw : std_ulogic_vector(63 downto 0);
105 signal random_cond : std_ulogic_vector(63 downto 0);
106 signal random_err : std_ulogic;
108 -- signals for logging
109 signal exception_log : std_ulogic;
110 signal irq_valid_log : std_ulogic;
112 type privilege_level is (USER, SUPER);
113 type op_privilege_array is array(insn_type_t) of privilege_level;
114 constant op_privilege: op_privilege_array := (
123 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
126 if op_privilege(op) = SUPER then
128 elsif op = OP_MFSPR or op = OP_MTSPR then
129 return insn(20) = '1';
135 procedure set_carry(e: inout Execute1ToWritebackType;
136 carry32 : in std_ulogic;
137 carry : in std_ulogic) is
139 e.xerc.ca32 := carry32;
141 e.write_xerc_enable := '1';
144 procedure set_ov(e: inout Execute1ToWritebackType;
146 ov32 : in std_ulogic) is
153 e.write_xerc_enable := '1';
156 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
157 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
159 return (ca xor msb_r) and not (msb_a xor msb_b);
162 function decode_input_carry(ic : carry_in_t;
163 xerc : xer_common_t) return std_ulogic is
177 function msr_copy(msr: std_ulogic_vector(63 downto 0))
178 return std_ulogic_vector is
179 variable msr_out: std_ulogic_vector(63 downto 0);
182 -- Defined MSR bits are classified as either full func-
183 -- tion or partial function. Full function MSR bits are
184 -- saved in SRR1 or HSRR1 when an interrupt other
185 -- than a System Call Vectored interrupt occurs and
186 -- restored by rfscv, rfid, or hrfid, while partial func-
187 -- tion MSR bits are not saved or restored.
188 -- Full function MSR bits lie in the range 0:32, 37:41, and
189 -- 48:63, and partial function MSR bits lie in the range
190 -- 33:36 and 42:47. (Note this is IBM bit numbering).
191 msr_out := (others => '0');
192 msr_out(63 downto 31) := msr(63 downto 31);
193 msr_out(26 downto 22) := msr(26 downto 22);
194 msr_out(15 downto 0) := msr(15 downto 0);
198 -- Tell vivado to keep the hierarchy for the random module so that the
199 -- net names in the xdc file match.
200 attribute keep_hierarchy : string;
201 attribute keep_hierarchy of random_0 : label is "yes";
205 rotator_0: entity work.rotator
209 shift => b_in(6 downto 0),
211 is_32bit => e_in.is_32bit,
212 right_shift => right_shift,
213 arith => e_in.is_signed,
214 clear_left => rot_clear_left,
215 clear_right => rot_clear_right,
216 sign_ext_rs => rot_sign_ext,
217 result => rotator_result,
218 carry_out => rotator_carry
221 logical_0: entity work.logical
225 op => e_in.insn_type,
226 invert_in => e_in.invert_a,
227 invert_out => e_in.invert_out,
228 result => logical_result,
229 datalen => e_in.data_len
232 countzero_0: entity work.zero_counter
236 count_right => e_in.insn(10),
237 is_32bit => e_in.is_32bit,
238 result => countzero_result
241 multiply_0: entity work.multiply
244 m_in => x_to_multiply,
245 m_out => multiply_to_x
248 divider_0: entity work.divider
252 d_in => x_to_divider,
253 d_out => divider_to_x
256 random_0: entity work.random
264 dbg_msr_out <= ctrl.msr;
265 log_rd_addr <= r.log_addr_spr;
267 a_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data1 = '1' else e_in.read_data1;
268 b_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data2 = '1' else e_in.read_data2;
269 c_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data3 = '1' else e_in.read_data3;
271 busy_out <= l_in.busy or r.busy;
272 valid_in <= e_in.valid and not busy_out;
274 terminate_out <= r.terminate;
276 execute1_0: process(clk)
278 if rising_edge(clk) then
281 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
282 ctrl.irq_state <= WRITE_SRR0;
286 assert not (r.lr_update = '1' and valid_in = '1')
287 report "LR update collision with valid in EX1"
289 if r.lr_update = '1' then
290 report "LR update to " & to_hstring(r.next_lr);
296 execute1_1: process(all)
297 variable v : reg_type;
298 variable a_inv : std_ulogic_vector(63 downto 0);
299 variable result : std_ulogic_vector(63 downto 0);
300 variable newcrf : std_ulogic_vector(3 downto 0);
301 variable sum_with_carry : std_ulogic_vector(64 downto 0);
302 variable result_en : std_ulogic;
303 variable crnum : crnum_t;
304 variable crbit : integer range 0 to 31;
305 variable scrnum : crnum_t;
306 variable lo, hi : integer;
307 variable sh, mb, me : std_ulogic_vector(5 downto 0);
308 variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
309 variable bo, bi : std_ulogic_vector(4 downto 0);
310 variable bf, bfa : std_ulogic_vector(2 downto 0);
311 variable cr_op : std_ulogic_vector(9 downto 0);
312 variable cr_operands : std_ulogic_vector(1 downto 0);
313 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
314 variable btnum, banum, bbnum : integer range 0 to 31;
315 variable crresult : std_ulogic;
316 variable l : std_ulogic;
317 variable next_nia : std_ulogic_vector(63 downto 0);
318 variable carry_32, carry_64 : std_ulogic;
319 variable sign1, sign2 : std_ulogic;
320 variable abs1, abs2 : signed(63 downto 0);
321 variable overflow : std_ulogic;
322 variable zerohi, zerolo : std_ulogic;
323 variable msb_a, msb_b : std_ulogic;
324 variable a_lt : std_ulogic;
325 variable lv : Execute1ToLoadstore1Type;
326 variable irq_valid : std_ulogic;
327 variable exception : std_ulogic;
328 variable exception_nextpc : std_ulogic;
329 variable trapval : std_ulogic_vector(4 downto 0);
330 variable illegal : std_ulogic;
331 variable is_branch : std_ulogic;
332 variable taken_branch : std_ulogic;
333 variable abs_branch : std_ulogic;
334 variable spr_val : std_ulogic_vector(63 downto 0);
335 variable addend : std_ulogic_vector(127 downto 0);
336 variable do_trace : std_ulogic;
338 result := (others => '0');
339 sum_with_carry := (others => '0');
341 newcrf := (others => '0');
347 v.e := Execute1ToWritebackInit;
348 lv := Execute1ToLoadstore1Init;
351 -- XER forwarding. To avoid having to track XER hazards, we
352 -- use the previously latched value.
354 -- If the XER was modified by a multiply or a divide, those are
355 -- single issue, we'll get the up to date value from decode2 from
356 -- the register file.
358 -- If it was modified by an instruction older than the previous
359 -- one in EX1, it will have also hit writeback and will be up
360 -- to date in decode2.
362 -- That leaves us with the case where it was updated by the previous
363 -- instruction in EX1. In that case, we can forward it back here.
365 -- This will break if we allow pipelining of multiply and divide,
366 -- but ideally, those should go via EX1 anyway and run as a state
367 -- machine from here.
369 -- One additional hazard to beware of is an XER:SO modifying instruction
370 -- in EX1 followed immediately by a store conditional. Due to our
371 -- writeback latency, the store will go down the LSU with the previous
372 -- XER value, thus the stcx. will set CR0:SO using an obsolete SO value.
374 -- We will need to handle that if we ever make stcx. not single issue
376 -- We always pass a valid XER value downto writeback even when
377 -- we aren't updating it, in order for XER:SO -> CR0:SO transfer
378 -- to work for RC instructions.
380 if r.e.write_xerc_enable = '1' then
381 v.e.xerc := r.e.xerc;
383 v.e.xerc := e_in.xerc;
388 if EX1_BYPASS and e_in.bypass_cr = '1' and r.e.write_cr_enable = '1' then
390 if r.e.write_cr_mask(i) = '1' then
391 cr_in(i * 4 + 3 downto i * 4) <= r.e.write_cr_data(i * 4 + 3 downto i * 4);
397 v.mul_in_progress := '0';
398 v.div_in_progress := '0';
399 v.cntz_in_progress := '0';
403 if e_in.invert_a = '0' then
408 sum_with_carry := ppc_adde(a_inv, b_in,
409 decode_input_carry(e_in.input_carry, v.e.xerc));
411 -- signals to multiply and divide units
414 if e_in.is_signed = '1' then
415 if e_in.is_32bit = '1' then
423 -- take absolute values
425 abs1 := signed(a_in);
427 abs1 := - signed(a_in);
430 abs2 := signed(b_in);
432 abs2 := - signed(b_in);
435 x_to_multiply <= MultiplyInputInit;
436 x_to_multiply.is_32bit <= e_in.is_32bit;
438 x_to_divider <= Execute1ToDividerInit;
439 x_to_divider.is_signed <= e_in.is_signed;
440 x_to_divider.is_32bit <= e_in.is_32bit;
441 if e_in.insn_type = OP_MOD then
442 x_to_divider.is_modulus <= '1';
445 addend := (others => '0');
446 if e_in.insn(26) = '0' then
447 -- integer multiply-add, major op 4 (if it is a multiply)
448 addend(63 downto 0) := c_in;
449 if e_in.is_signed = '1' then
450 addend(127 downto 64) := (others => c_in(63));
453 if (sign1 xor sign2) = '1' then
454 addend := not addend;
457 x_to_multiply.not_result <= sign1 xor sign2;
458 x_to_multiply.addend <= addend;
459 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
460 if e_in.is_32bit = '0' then
462 x_to_multiply.data1 <= std_ulogic_vector(abs1);
463 x_to_multiply.data2 <= std_ulogic_vector(abs2);
464 if e_in.insn_type = OP_DIVE then
465 x_to_divider.is_extended <= '1';
467 x_to_divider.dividend <= std_ulogic_vector(abs1);
468 x_to_divider.divisor <= std_ulogic_vector(abs2);
471 x_to_multiply.data1 <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
472 x_to_multiply.data2 <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
473 x_to_divider.is_extended <= '0';
474 if e_in.insn_type = OP_DIVE then -- extended forms
475 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
477 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
479 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
483 -- FIXME: run at 512MHz not core freq
484 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
485 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
488 if ctrl.msr(MSR_EE) = '1' then
489 if ctrl.dec(63) = '1' then
490 v.f.redirect_nia := std_logic_vector(to_unsigned(16#900#, 64));
491 report "IRQ valid: DEC";
493 elsif ext_irq_in = '1' then
494 v.f.redirect_nia := std_logic_vector(to_unsigned(16#500#, 64));
495 report "IRQ valid: External";
503 -- send MSR[IR], ~MSR[PR], ~MSR[LE] and ~MSR[SF] up to fetch1
504 v.f.virt_mode := ctrl.msr(MSR_IR);
505 v.f.priv_mode := not ctrl.msr(MSR_PR);
506 v.f.big_endian := not ctrl.msr(MSR_LE);
507 v.f.mode_32bit := not ctrl.msr(MSR_SF);
509 -- Next insn adder used in a couple of places
510 next_nia := std_ulogic_vector(unsigned(e_in.nia) + 4);
512 -- rotator control signals
513 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
514 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
515 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
516 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
518 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
519 ctrl_tmp.irq_state <= WRITE_SRR0;
522 exception_nextpc := '0';
523 v.e.exc_write_enable := '0';
524 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
525 v.e.exc_write_data := e_in.nia;
526 if valid_in = '1' then
527 v.last_nia := e_in.nia;
530 v.e.mode_32bit := not ctrl.msr(MSR_SF);
532 do_trace := valid_in and ctrl.msr(MSR_SE);
533 if valid_in = '1' then
534 v.prev_op := e_in.insn_type;
537 if ctrl.irq_state = WRITE_SRR1 then
538 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
539 v.e.exc_write_data := ctrl.srr1;
540 v.e.exc_write_enable := '1';
541 ctrl_tmp.msr(MSR_SF) <= '1';
542 ctrl_tmp.msr(MSR_EE) <= '0';
543 ctrl_tmp.msr(MSR_PR) <= '0';
544 ctrl_tmp.msr(MSR_SE) <= '0';
545 ctrl_tmp.msr(MSR_BE) <= '0';
546 ctrl_tmp.msr(MSR_FP) <= '0';
547 ctrl_tmp.msr(MSR_FE0) <= '0';
548 ctrl_tmp.msr(MSR_FE1) <= '0';
549 ctrl_tmp.msr(MSR_IR) <= '0';
550 ctrl_tmp.msr(MSR_DR) <= '0';
551 ctrl_tmp.msr(MSR_RI) <= '0';
552 ctrl_tmp.msr(MSR_LE) <= '1';
555 report "Writing SRR1: " & to_hstring(ctrl.srr1);
557 elsif r.trace_next = '1' and valid_in = '1' then
558 -- Generate a trace interrupt rather than executing the next instruction
559 -- or taking any asynchronous interrupt
560 v.f.redirect_nia := std_logic_vector(to_unsigned(16#d00#, 64));
561 ctrl_tmp.srr1(63 - 33) <= '1';
562 if r.prev_op = OP_LOAD or r.prev_op = OP_ICBI or r.prev_op = OP_ICBT or
563 r.prev_op = OP_DCBT or r.prev_op = OP_DCBST or r.prev_op = OP_DCBF then
564 ctrl_tmp.srr1(63 - 35) <= '1';
565 elsif r.prev_op = OP_STORE or r.prev_op = OP_DCBZ or r.prev_op = OP_DCBTST then
566 ctrl_tmp.srr1(63 - 36) <= '1';
570 elsif irq_valid = '1' and valid_in = '1' then
571 -- we need two cycles to write srr0 and 1
572 -- will need more when we have to write HEIR
573 -- Don't deliver the interrupt until we have a valid instruction
574 -- coming in, so we have a valid NIA to put in SRR0.
577 elsif valid_in = '1' and ctrl.msr(MSR_PR) = '1' and
578 instr_is_privileged(e_in.insn_type, e_in.insn) then
579 -- generate a program interrupt
581 v.f.redirect_nia := std_logic_vector(to_unsigned(16#700#, 64));
582 -- set bit 45 to indicate privileged instruction type interrupt
583 ctrl_tmp.srr1(63 - 45) <= '1';
584 report "privileged instruction";
586 elsif not HAS_FPU and valid_in = '1' and
587 (e_in.insn_type = OP_FPLOAD or e_in.insn_type = OP_FPSTORE) then
588 -- make lfd/stfd/lfs/stfs etc. illegal in no-FPU implementations
591 elsif HAS_FPU and valid_in = '1' and ctrl.msr(MSR_FP) = '0' and
592 (e_in.insn_type = OP_FPLOAD or e_in.insn_type = OP_FPSTORE) then
593 -- generate a floating-point unavailable interrupt
595 v.f.redirect_nia := std_logic_vector(to_unsigned(16#800#, 64));
596 report "FP unavailable interrupt";
598 elsif valid_in = '1' and e_in.unit = ALU then
600 report "execute nia " & to_hstring(e_in.nia);
603 v.e.write_reg := e_in.write_reg;
604 v.slow_op_insn := e_in.insn_type;
605 v.slow_op_dest := gspr_to_gpr(e_in.write_reg);
606 v.slow_op_rc := e_in.rc;
607 v.slow_op_oe := e_in.oe;
608 v.slow_op_xerc := v.e.xerc;
610 case_0: case e_in.insn_type is
613 -- we need two cycles to write srr0 and 1
614 -- will need more when we have to write HEIR
617 -- check bit 1 of the instruction is 1 so we know this is sc;
618 -- 0 would mean scv, so generate an illegal instruction interrupt
619 -- we need two cycles to write srr0 and 1
620 if e_in.insn(1) = '1' then
622 exception_nextpc := '1';
623 v.f.redirect_nia := std_logic_vector(to_unsigned(16#C00#, 64));
629 -- check bits 1-10 of the instruction to make sure it's attn
630 -- if not then it is illegal
631 if e_in.insn(10 downto 1) = "0100000000" then
637 when OP_NOP | OP_DCBF | OP_DCBST | OP_DCBT | OP_DCBTST | OP_ICBT =>
639 when OP_ADD | OP_CMP | OP_TRAP =>
640 result := sum_with_carry(63 downto 0);
641 carry_32 := result(32) xor a_inv(32) xor b_in(32);
642 carry_64 := sum_with_carry(64);
643 if e_in.insn_type = OP_ADD then
644 if e_in.output_carry = '1' then
645 if e_in.input_carry /= OV then
646 set_carry(v.e, carry_32, carry_64);
648 v.e.xerc.ov := carry_64;
649 v.e.xerc.ov32 := carry_32;
650 v.e.write_xerc_enable := '1';
653 if e_in.oe = '1' then
655 calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63)),
656 calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31)));
660 -- trap, CMP and CMPL instructions
661 -- Note, we have done RB - RA, not RA - RB
662 if e_in.insn_type = OP_CMP then
663 l := insn_l(e_in.insn);
665 l := not e_in.is_32bit;
667 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
668 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
669 if zerolo = '1' and (l = '0' or zerohi = '1') then
682 if msb_a /= msb_b then
683 -- Subtraction might overflow, but
684 -- comparison is clear from MSB difference.
685 -- for signed, 0 is greater; for unsigned, 1 is greater
686 trapval := msb_a & msb_b & '0' & msb_b & msb_a;
688 -- Subtraction cannot overflow since MSBs are equal.
689 -- carry = 1 indicates RA is smaller (signed or unsigned)
690 a_lt := (not l and carry_32) or (l and carry_64);
691 trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
694 if e_in.insn_type = OP_CMP then
695 if e_in.is_signed = '1' then
696 newcrf := trapval(4 downto 2) & v.e.xerc.so;
698 newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
700 bf := insn_bf(e_in.insn);
701 crnum := to_integer(unsigned(bf));
702 v.e.write_cr_enable := '1';
703 v.e.write_cr_mask := num_to_fxm(crnum);
707 v.e.write_cr_data(hi downto lo) := newcrf;
710 -- trap instructions (tw, twi, td, tdi)
711 v.f.redirect_nia := std_logic_vector(to_unsigned(16#700#, 64));
712 -- set bit 46 to say trap occurred
713 ctrl_tmp.srr1(63 - 46) <= '1';
714 if or (trapval and insn_to(e_in.insn)) = '1' then
715 -- generate trap-type program interrupt
722 result := (others => '0');
723 for i in 0 to 14 loop
726 if (a_in(hi) xor b_in(hi) xor sum_with_carry(hi)) = '0' then
727 result(lo + 3 downto lo) := "0110";
730 if sum_with_carry(64) = '0' then
731 result(63 downto 60) := "0110";
735 newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
736 bf := insn_bf(e_in.insn);
737 crnum := to_integer(unsigned(bf));
738 v.e.write_cr_enable := '1';
739 v.e.write_cr_mask := num_to_fxm(crnum);
740 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
741 newcrf & newcrf & newcrf & newcrf;
743 newcrf := ppc_cmpeqb(a_in, b_in);
744 bf := insn_bf(e_in.insn);
745 crnum := to_integer(unsigned(bf));
746 v.e.write_cr_enable := '1';
747 v.e.write_cr_mask := num_to_fxm(crnum);
748 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
749 newcrf & newcrf & newcrf & newcrf;
750 when OP_AND | OP_OR | OP_XOR | OP_POPCNT | OP_PRTY | OP_CMPB | OP_EXTS |
752 result := logical_result;
757 abs_branch := insn_aa(e_in.insn);
758 if ctrl.msr(MSR_BE) = '1' then
763 bo := insn_bo(e_in.insn);
764 bi := insn_bi(e_in.insn);
765 if bo(4-2) = '0' then
766 result := std_ulogic_vector(unsigned(a_in) - 1);
768 v.e.write_reg := fast_spr_num(SPR_CTR);
771 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
772 abs_branch := insn_aa(e_in.insn);
773 if ctrl.msr(MSR_BE) = '1' then
778 -- read_data2 is target register (CTR, LR or TAR)
779 bo := insn_bo(e_in.insn);
780 bi := insn_bi(e_in.insn);
781 if bo(4-2) = '0' and e_in.insn(10) = '0' then
782 result := std_ulogic_vector(unsigned(a_in) - 1);
784 v.e.write_reg := fast_spr_num(SPR_CTR);
787 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
789 if ctrl.msr(MSR_BE) = '1' then
794 v.f.virt_mode := a_in(MSR_IR) or a_in(MSR_PR);
795 v.f.priv_mode := not a_in(MSR_PR);
796 v.f.big_endian := not a_in(MSR_LE);
797 v.f.mode_32bit := not a_in(MSR_SF);
798 -- Can't use msr_copy here because the partial function MSR
799 -- bits should be left unchanged, not zeroed.
800 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
801 ctrl_tmp.msr(26 downto 22) <= a_in(26 downto 22);
802 ctrl_tmp.msr(15 downto 0) <= a_in(15 downto 0);
803 if a_in(MSR_PR) = '1' then
804 ctrl_tmp.msr(MSR_EE) <= '1';
805 ctrl_tmp.msr(MSR_IR) <= '1';
806 ctrl_tmp.msr(MSR_DR) <= '1';
808 -- mark this as a branch so CFAR gets updated
816 v.cntz_in_progress := '1';
819 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
820 if cr_in(31-crbit) = '1' then
827 cr_op := insn_cr(e_in.insn);
828 report "CR OP " & to_hstring(cr_op);
829 if cr_op(0) = '0' then -- MCRF
830 bf := insn_bf(e_in.insn);
831 bfa := insn_bfa(e_in.insn);
832 v.e.write_cr_enable := '1';
833 crnum := to_integer(unsigned(bf));
834 scrnum := to_integer(unsigned(bfa));
835 v.e.write_cr_mask := num_to_fxm(crnum);
840 newcrf := cr_in(hi downto lo);
846 v.e.write_cr_data(hi downto lo) := newcrf;
849 v.e.write_cr_enable := '1';
850 bt := insn_bt(e_in.insn);
851 ba := insn_ba(e_in.insn);
852 bb := insn_bb(e_in.insn);
853 btnum := 31 - to_integer(unsigned(bt));
854 banum := 31 - to_integer(unsigned(ba));
855 bbnum := 31 - to_integer(unsigned(bb));
856 -- Bits 5-8 of cr_op give the truth table of the requested
858 cr_operands := cr_in(banum) & cr_in(bbnum);
859 crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
860 v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
861 for i in 0 to 31 loop
863 v.e.write_cr_data(i) := crresult;
865 v.e.write_cr_data(i) := cr_in(i);
870 newcrf := v.e.xerc.ov & v.e.xerc.ca & v.e.xerc.ov32 & v.e.xerc.ca32;
871 bf := insn_bf(e_in.insn);
872 crnum := to_integer(unsigned(bf));
873 v.e.write_cr_enable := '1';
874 v.e.write_cr_mask := num_to_fxm(crnum);
875 v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
876 newcrf & newcrf & newcrf & newcrf;
878 if random_err = '0' then
879 case e_in.insn(17 downto 16) is
881 result := x"00000000" & random_cond(31 downto 0);
883 result := random_raw;
885 result := random_cond;
888 result := (others => '1');
895 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
896 "=" & to_hstring(a_in);
898 if is_fast_spr(e_in.read_reg1) then
900 if decode_spr_num(e_in.insn) = SPR_XER then
901 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
902 result(63 downto 32) := (others => '0');
903 result(63-32) := v.e.xerc.so;
904 result(63-33) := v.e.xerc.ov;
905 result(63-34) := v.e.xerc.ca;
906 result(63-35 downto 63-43) := "000000000";
907 result(63-44) := v.e.xerc.ov32;
908 result(63-45) := v.e.xerc.ca32;
912 case decode_spr_num(e_in.insn) is
916 spr_val(63 downto 32) := (others => '0');
917 spr_val(31 downto 0) := ctrl.tb(63 downto 32);
921 spr_val := ctrl.cfar;
923 spr_val(63 downto 32) := (others => '0');
924 spr_val(31 downto 0) := PVR_MICROWATT;
925 when 724 => -- LOG_ADDR SPR
926 spr_val := log_wr_addr & r.log_addr_spr;
927 when 725 => -- LOG_DATA SPR
928 spr_val := log_rd_data;
929 v.log_addr_spr := std_ulogic_vector(unsigned(r.log_addr_spr) + 1);
931 -- mfspr from unimplemented SPRs should be a nop in
932 -- supervisor mode and a program interrupt for user mode
933 if ctrl.msr(MSR_PR) = '1' then
940 if e_in.insn(20) = '0' then
942 result := x"00000000" & cr_in;
945 crnum := fxm_to_num(insn_fxm(e_in.insn));
946 result := (others => '0');
951 result(hi downto lo) := cr_in(hi downto lo);
957 v.e.write_cr_enable := '1';
958 if e_in.insn(20) = '0' then
960 v.e.write_cr_mask := insn_fxm(e_in.insn);
962 -- mtocrf: We require one hot priority encoding here
963 crnum := fxm_to_num(insn_fxm(e_in.insn));
964 v.e.write_cr_mask := num_to_fxm(crnum);
966 v.e.write_cr_data := c_in(31 downto 0);
968 if e_in.insn(16) = '1' then
969 -- just update EE and RI
970 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
971 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
973 -- Architecture says to leave out bits 3 (HV), 51 (ME)
974 -- and 63 (LE) (IBM bit numbering)
975 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
976 ctrl_tmp.msr(59 downto 13) <= c_in(59 downto 13);
977 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
978 if c_in(MSR_PR) = '1' then
979 ctrl_tmp.msr(MSR_EE) <= '1';
980 ctrl_tmp.msr(MSR_IR) <= '1';
981 ctrl_tmp.msr(MSR_DR) <= '1';
985 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
986 "=" & to_hstring(c_in);
987 if is_fast_spr(e_in.write_reg) then
990 if decode_spr_num(e_in.insn) = SPR_XER then
991 v.e.xerc.so := c_in(63-32);
992 v.e.xerc.ov := c_in(63-33);
993 v.e.xerc.ca := c_in(63-34);
994 v.e.xerc.ov32 := c_in(63-44);
995 v.e.xerc.ca32 := c_in(63-45);
996 v.e.write_xerc_enable := '1';
1000 case decode_spr_num(e_in.insn) is
1002 ctrl_tmp.dec <= c_in;
1003 when 724 => -- LOG_ADDR SPR
1004 v.log_addr_spr := c_in(31 downto 0);
1006 -- mtspr to unimplemented SPRs should be a nop in
1007 -- supervisor mode and a program interrupt for user mode
1008 if ctrl.msr(MSR_PR) = '1' then
1013 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
1014 result := rotator_result;
1015 if e_in.output_carry = '1' then
1016 set_carry(v.e, rotator_carry, rotator_carry);
1020 bfa := insn_bfa(e_in.insn);
1021 crbit := to_integer(unsigned(bfa)) * 4;
1022 result := (others => '0');
1023 if cr_in(31 - crbit) = '1' then
1024 result := (others => '1');
1025 elsif cr_in(30 - crbit) = '1' then
1030 v.f.redirect := '1';
1031 v.f.redirect_nia := next_nia;
1034 icache_inval <= '1';
1036 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
1038 v.mul_in_progress := '1';
1040 x_to_multiply.valid <= '1';
1042 when OP_DIV | OP_DIVE | OP_MOD =>
1044 v.div_in_progress := '1';
1046 x_to_divider.valid <= '1';
1053 v.e.rc := e_in.rc and valid_in;
1055 -- Mispredicted branches cause a redirect
1056 if is_branch = '1' then
1057 if taken_branch = '1' then
1058 ctrl_tmp.cfar <= e_in.nia;
1060 if e_in.br_pred = '0' then
1061 if abs_branch = '1' then
1062 v.f.redirect_nia := b_in;
1064 v.f.redirect_nia := std_ulogic_vector(signed(e_in.nia) + signed(b_in));
1067 v.f.redirect_nia := next_nia;
1069 if taken_branch /= e_in.br_pred then
1070 v.f.redirect := '1';
1074 -- Update LR on the next cycle after a branch link
1075 -- If we're not writing back anything else, we can write back LR
1076 -- this cycle, otherwise we take an extra cycle. We use the
1077 -- exc_write path since next_nia is written through that path
1079 if e_in.lr = '1' then
1080 if result_en = '0' then
1081 v.e.exc_write_enable := '1';
1082 v.e.exc_write_data := next_nia;
1083 v.e.exc_write_reg := fast_spr_num(SPR_LR);
1086 v.next_lr := next_nia;
1088 report "Delayed LR update to " & to_hstring(next_nia);
1093 elsif valid_in = '1' then
1094 -- instruction for other units, i.e. LDST
1095 if e_in.unit = LDST then
1097 elsif e_in.unit = NONE then
1101 elsif r.f.redirect = '1' then
1103 elsif r.lr_update = '1' then
1104 v.e.exc_write_enable := '1';
1105 v.e.exc_write_data := r.next_lr;
1106 v.e.exc_write_reg := fast_spr_num(SPR_LR);
1108 elsif r.cntz_in_progress = '1' then
1109 -- cnt[lt]z always takes two cycles
1110 result := countzero_result;
1112 v.e.write_reg := gpr_to_gspr(r.slow_op_dest);
1113 v.e.rc := r.slow_op_rc;
1114 v.e.xerc := r.slow_op_xerc;
1116 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
1117 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
1118 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
1119 if r.mul_in_progress = '1' then
1121 case r.slow_op_insn is
1123 result := multiply_to_x.result(63 downto 32) &
1124 multiply_to_x.result(63 downto 32);
1126 result := multiply_to_x.result(127 downto 64);
1129 result := multiply_to_x.result(63 downto 0);
1132 result := divider_to_x.write_reg_data;
1133 overflow := divider_to_x.overflow;
1135 if r.mul_in_progress = '1' and r.slow_op_oe = '1' then
1136 -- have to wait until next cycle for overflow indication
1137 v.mul_finish := '1';
1141 v.e.write_reg := gpr_to_gspr(r.slow_op_dest);
1142 v.e.rc := r.slow_op_rc;
1143 v.e.xerc := r.slow_op_xerc;
1144 v.e.write_xerc_enable := r.slow_op_oe;
1145 -- We must test oe because the RC update code in writeback
1146 -- will use the xerc value to set CR0:SO so we must not clobber
1147 -- xerc if OE wasn't set.
1148 if r.slow_op_oe = '1' then
1149 v.e.xerc.ov := overflow;
1150 v.e.xerc.ov32 := overflow;
1151 v.e.xerc.so := r.slow_op_xerc.so or overflow;
1157 v.mul_in_progress := r.mul_in_progress;
1158 v.div_in_progress := r.div_in_progress;
1160 elsif r.mul_finish = '1' then
1161 result := r.e.write_data;
1163 v.e.write_reg := gpr_to_gspr(r.slow_op_dest);
1164 v.e.rc := r.slow_op_rc;
1165 v.e.xerc := r.slow_op_xerc;
1166 v.e.write_xerc_enable := r.slow_op_oe;
1167 v.e.xerc.ov := multiply_to_x.overflow;
1168 v.e.xerc.ov32 := multiply_to_x.overflow;
1169 v.e.xerc.so := r.slow_op_xerc.so or multiply_to_x.overflow;
1173 if illegal = '1' then
1175 v.f.redirect_nia := std_logic_vector(to_unsigned(16#700#, 64));
1176 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
1177 -- set bit 44 to indicate we have an illegal
1178 ctrl_tmp.srr1(63 - 44) <= '1';
1181 if exception = '1' then
1182 v.e.exc_write_enable := '1';
1183 if exception_nextpc = '1' then
1184 v.e.exc_write_data := next_nia;
1188 if do_trace = '1' then
1189 v.trace_next := '1';
1192 v.e.write_data := result;
1193 v.e.write_enable := result_en and not exception;
1195 -- generate DSI or DSegI for load/store exceptions
1196 -- or ISI or ISegI for instruction fetch exceptions
1197 if l_in.exception = '1' then
1198 if l_in.alignment = '1' then
1199 v.f.redirect_nia := std_logic_vector(to_unsigned(16#600#, 64));
1200 elsif l_in.instr_fault = '0' then
1201 if l_in.segment_fault = '0' then
1202 v.f.redirect_nia := std_logic_vector(to_unsigned(16#300#, 64));
1204 v.f.redirect_nia := std_logic_vector(to_unsigned(16#380#, 64));
1207 if l_in.segment_fault = '0' then
1208 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
1209 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
1210 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
1211 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
1212 v.f.redirect_nia := std_logic_vector(to_unsigned(16#400#, 64));
1214 v.f.redirect_nia := std_logic_vector(to_unsigned(16#480#, 64));
1217 v.e.exc_write_enable := '1';
1218 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1219 v.e.exc_write_data := r.last_nia;
1220 report "ldst exception writing srr0=" & to_hstring(r.last_nia);
1223 if exception = '1' or l_in.exception = '1' then
1224 ctrl_tmp.irq_state <= WRITE_SRR1;
1225 v.f.redirect := '1';
1226 v.f.virt_mode := '0';
1227 v.f.priv_mode := '1';
1228 -- XXX need an interrupt LE bit here, e.g. from LPCR
1229 v.f.big_endian := '0';
1230 v.f.mode_32bit := '0';
1233 if v.f.redirect = '1' then
1238 -- Outputs to loadstore1 (async)
1239 lv.op := e_in.insn_type;
1244 lv.write_reg := e_in.write_reg;
1245 lv.length := e_in.data_len;
1246 lv.byte_reverse := e_in.byte_reverse xnor ctrl.msr(MSR_LE);
1247 lv.sign_extend := e_in.sign_extend;
1248 lv.update := e_in.update;
1249 lv.update_reg := gspr_to_gpr(e_in.read_reg1);
1250 lv.xerc := v.e.xerc;
1251 lv.reserve := e_in.reserve;
1253 lv.insn := e_in.insn;
1254 -- decode l*cix and st*cix instructions here
1255 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1256 e_in.insn(5 downto 1) = "10101" then
1259 lv.virt_mode := ctrl.msr(MSR_DR);
1260 lv.priv_mode := not ctrl.msr(MSR_PR);
1261 lv.mode_32bit := not ctrl.msr(MSR_SF);
1262 lv.is_32bit := e_in.is_32bit;
1271 flush_out <= f_out.redirect;
1273 exception_log <= exception;
1274 irq_valid_log <= irq_valid;
1277 e1_log: if LOG_LENGTH > 0 generate
1278 signal log_data : std_ulogic_vector(14 downto 0);
1280 ex1_log : process(clk)
1282 if rising_edge(clk) then
1283 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1284 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1287 std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
1296 log_out <= log_data;
1298 end architecture behaviour;