Add Tercel PHY reset synchronization
[microwatt.git] / execute1.vhdl
index 2690424812cf45c46c99f766d6c7ae3b9830368f..c0434a026f6cbc566bf2e035241f3bfdf1120cb9 100644 (file)
@@ -22,7 +22,7 @@ entity execute1 is
         rst   : in std_ulogic;
 
        -- asynchronous
-       flush_out : out std_ulogic;
+       flush_in : in std_ulogic;
        busy_out : out std_ulogic;
 
        e_in  : in Decode2ToExecute1Type;
@@ -30,13 +30,15 @@ entity execute1 is
         fp_in : in FPUToExecute1Type;
 
        ext_irq_in : std_ulogic;
+        interrupt_in : std_ulogic;
 
        -- asynchronous
         l_out : out Execute1ToLoadstore1Type;
-       f_out : out Execute1ToFetch1Type;
         fp_out : out Execute1ToFPUType;
 
        e_out : out Execute1ToWritebackType;
+        bypass_data : out bypass_data_t;
+        bypass_cr_data : out cr_bypass_data_t;
 
         dbg_msr_out : out std_ulogic_vector(63 downto 0);
 
@@ -59,21 +61,11 @@ architecture behaviour of execute1 is
         fp_exception_next : std_ulogic;
         trace_next : std_ulogic;
         prev_op : insn_type_t;
-       next_lr : std_ulogic_vector(63 downto 0);
         br_taken : std_ulogic;
        mul_in_progress : std_ulogic;
         mul_finish : std_ulogic;
         div_in_progress : std_ulogic;
         cntz_in_progress : std_ulogic;
-        last_nia : std_ulogic_vector(63 downto 0);
-        redirect : std_ulogic;
-        abs_br : std_ulogic;
-        taken_br : std_ulogic;
-        br_last : std_ulogic;
-        do_intr : std_ulogic;
-        vector : integer range 0 to 16#fff#;
-        br_offset : std_ulogic_vector(63 downto 0);
-        redir_mode : std_ulogic_vector(3 downto 0);
         log_addr_spr : std_ulogic_vector(31 downto 0);
     end record;
     constant reg_type_init : reg_type :=
@@ -82,19 +74,17 @@ architecture behaviour of execute1 is
          busy => '0', terminate => '0',
          fp_exception_next => '0', trace_next => '0', prev_op => OP_ILLEGAL, br_taken => '0',
          mul_in_progress => '0', mul_finish => '0', div_in_progress => '0', cntz_in_progress => '0',
-         next_lr => (others => '0'), last_nia => (others => '0'),
-         redirect => '0', abs_br => '0', taken_br => '0', br_last => '0', do_intr => '0', vector => 0,
-         br_offset => (others => '0'), redir_mode => "0000",
          others => (others => '0'));
 
     signal r, rin : reg_type;
 
     signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
     signal cr_in : std_ulogic_vector(31 downto 0);
+    signal xerc_in : xer_common_t;
 
     signal valid_in : std_ulogic;
-    signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
-    signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
+    signal ctrl: ctrl_t := (others => (others => '0'));
+    signal ctrl_tmp: ctrl_t := (others => (others => '0'));
     signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
     signal rot_sign_ext: std_ulogic;
     signal rotator_result: std_ulogic_vector(63 downto 0);
@@ -111,6 +101,16 @@ architecture behaviour of execute1 is
     signal next_nia : std_ulogic_vector(63 downto 0);
     signal current: Decode2ToExecute1Type;
 
+    signal carry_32 : std_ulogic;
+    signal carry_64 : std_ulogic;
+    signal overflow_32 : std_ulogic;
+    signal overflow_64 : std_ulogic;
+
+    signal trapval : std_ulogic_vector(4 downto 0);
+
+    signal write_cr_mask : std_ulogic_vector(7 downto 0);
+    signal write_cr_data : std_ulogic_vector(31 downto 0);
+
     -- multiply signals
     signal x_to_multiply: MultiplyInputType;
     signal multiply_to_x: MultiplyOutputType;
@@ -157,7 +157,6 @@ architecture behaviour of execute1 is
     begin
        e.xerc.ca32 := carry32;
        e.xerc.ca := carry;
-       e.write_xerc_enable := '1';
     end;
 
     procedure set_ov(e: inout Execute1ToWritebackType;
@@ -169,7 +168,6 @@ architecture behaviour of execute1 is
        if ov = '1' then
            e.xerc.so := '1';
        end if;
-       e.write_xerc_enable := '1';
     end;
 
     function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
@@ -283,12 +281,23 @@ begin
     dbg_msr_out <= ctrl.msr;
     log_rd_addr <= r.log_addr_spr;
 
-    a_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data1 = '1' else e_in.read_data1;
-    b_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data2 = '1' else e_in.read_data2;
-    c_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data3 = '1' else e_in.read_data3;
+    a_in <= e_in.read_data1;
+    b_in <= e_in.read_data2;
+    c_in <= e_in.read_data3;
+    cr_in <= e_in.cr;
+
+    -- XER forwarding. To avoid having to track XER hazards, we use
+    -- the previously latched value.  Since the XER common bits
+    -- (SO, OV[32] and CA[32]) are only modified by instructions that are
+    -- handled here, we can just forward the result being sent to
+    -- writeback.
+    xerc_in <= r.e.xerc when r.e.write_xerc_enable = '1' or r.busy = '1' else e_in.xerc;
 
-    busy_out <= l_in.busy or r.busy or fp_in.busy;
-    valid_in <= e_in.valid and not busy_out;
+    with e_in.unit select busy_out <=
+        l_in.busy or r.busy or fp_in.busy when LDST,
+        l_in.busy or l_in.in_progress or r.busy or fp_in.busy when others;
+
+    valid_in <= e_in.valid and not busy_out and not flush_in;
 
     terminate_out <= r.terminate;
 
@@ -313,121 +322,50 @@ begin
                 ctrl.tb <= (others => '0');
                 ctrl.dec <= (others => '0');
                 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
-                ctrl.irq_state <= WRITE_SRR0;
             else
                 r <= rin;
                 ctrl <= ctrl_tmp;
                 if valid_in = '1' then
                     report "execute " & to_hstring(e_in.nia) & " op=" & insn_type_t'image(e_in.insn_type) &
-                        " wr=" & to_hstring(rin.e.write_reg);
+                        " wr=" & to_hstring(rin.e.write_reg) & " we=" & std_ulogic'image(rin.e.write_enable) &
+                        " tag=" & integer'image(rin.e.instr_tag.tag) & std_ulogic'image(rin.e.instr_tag.valid);
                 end if;
             end if;
        end if;
     end process;
 
-    execute1_1: process(all)
-       variable v : reg_type;
+    -- Data path for integer instructions
+    execute1_dp: process(all)
        variable a_inv : std_ulogic_vector(63 downto 0);
        variable b_or_m1 : std_ulogic_vector(63 downto 0);
+       variable sum_with_carry : std_ulogic_vector(64 downto 0);
+        variable sign1, sign2 : std_ulogic;
+        variable abs1, abs2 : signed(63 downto 0);
+        variable addend : std_ulogic_vector(127 downto 0);
        variable addg6s : std_ulogic_vector(63 downto 0);
+       variable crbit : integer range 0 to 31;
        variable isel_result : std_ulogic_vector(63 downto 0);
        variable darn : std_ulogic_vector(63 downto 0);
-       variable mfcr_result : std_ulogic_vector(63 downto 0);
        variable setb_result : std_ulogic_vector(63 downto 0);
-       variable newcrf : std_ulogic_vector(3 downto 0);
-       variable sum_with_carry : std_ulogic_vector(64 downto 0);
-       variable crnum : crnum_t;
-       variable crbit : integer range 0 to 31;
-       variable scrnum : crnum_t;
+       variable mfcr_result : std_ulogic_vector(63 downto 0);
        variable lo, hi : integer;
-       variable sh, mb, me : std_ulogic_vector(5 downto 0);
-       variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
-       variable bo, bi : std_ulogic_vector(4 downto 0);
-       variable bf, bfa : std_ulogic_vector(2 downto 0);
-       variable cr_op : std_ulogic_vector(9 downto 0);
-        variable cr_operands : std_ulogic_vector(1 downto 0);
-       variable bt, ba, bb : std_ulogic_vector(4 downto 0);
-       variable btnum, banum, bbnum : integer range 0 to 31;
-       variable crresult : std_ulogic;
        variable l : std_ulogic;
-        variable carry_32, carry_64 : std_ulogic;
-        variable sign1, sign2 : std_ulogic;
-        variable abs1, abs2 : signed(63 downto 0);
-       variable overflow : std_ulogic;
         variable zerohi, zerolo : std_ulogic;
         variable msb_a, msb_b : std_ulogic;
         variable a_lt : std_ulogic;
         variable a_lt_lo : std_ulogic;
         variable a_lt_hi : std_ulogic;
-        variable lv : Execute1ToLoadstore1Type;
-       variable irq_valid : std_ulogic;
-       variable exception : std_ulogic;
-        variable exception_nextpc : std_ulogic;
-        variable trapval : std_ulogic_vector(4 downto 0);
-        variable illegal : std_ulogic;
-        variable is_branch : std_ulogic;
-        variable is_direct_branch : std_ulogic;
-        variable taken_branch : std_ulogic;
-        variable abs_branch : std_ulogic;
-        variable spr_val : std_ulogic_vector(63 downto 0);
-        variable addend : std_ulogic_vector(127 downto 0);
-        variable do_trace : std_ulogic;
-        variable hold_wr_data : std_ulogic;
-        variable f : Execute1ToFetch1Type;
-        variable fv : Execute1ToFPUType;
+       variable newcrf : std_ulogic_vector(3 downto 0);
+       variable bf, bfa : std_ulogic_vector(2 downto 0);
+       variable crnum : crnum_t;
+       variable scrnum : crnum_t;
+        variable cr_operands : std_ulogic_vector(1 downto 0);
+       variable crresult : std_ulogic;
+       variable bt, ba, bb : std_ulogic_vector(4 downto 0);
+        variable btnum : integer range 0 to 3;
+       variable banum, bbnum : integer range 0 to 31;
+        variable j : integer;
     begin
-       sum_with_carry := (others => '0');
-       newcrf := (others => '0');
-        is_branch := '0';
-        is_direct_branch := '0';
-        taken_branch := '0';
-        abs_branch := '0';
-        hold_wr_data := '0';
-
-       v := r;
-       v.e := Execute1ToWritebackInit;
-        v.redirect := '0';
-        v.abs_br := '0';
-        v.do_intr := '0';
-        v.vector := 0;
-        v.br_offset := (others => '0');
-        v.redir_mode := ctrl.msr(MSR_IR) & not ctrl.msr(MSR_PR) &
-                        not ctrl.msr(MSR_LE) & not ctrl.msr(MSR_SF);
-        v.taken_br := '0';
-        v.br_last := '0';
-
-        lv := Execute1ToLoadstore1Init;
-        fv := Execute1ToFPUInit;
-
-       -- XER forwarding. To avoid having to track XER hazards, we use
-        -- the previously latched value.  Since the XER common bits
-       -- (SO, OV[32] and CA[32]) are only modified by instructions that are
-        -- handled here, we can just forward the result being sent to
-        -- writeback.
-       if r.e.write_xerc_enable = '1' or r.busy = '1' then
-           v.e.xerc := r.e.xerc;
-       else
-           v.e.xerc := e_in.xerc;
-       end if;
-
-        -- CR forwarding
-        cr_in <= e_in.cr;
-        if EX1_BYPASS and e_in.bypass_cr = '1' and r.e.write_cr_enable = '1' then
-            for i in 0 to 7 loop
-                if r.e.write_cr_mask(i) = '1' then
-                    cr_in(i * 4 + 3 downto i * 4) <= r.e.write_cr_data(i * 4 + 3 downto i * 4);
-                end if;
-            end loop;
-        end if;
-
-       v.mul_in_progress := '0';
-        v.div_in_progress := '0';
-        v.cntz_in_progress := '0';
-        v.mul_finish := '0';
-
-        spr_result <= (others => '0');
-        spr_val := (others => '0');
-
         -- Main adder
         if e_in.invert_a = '0' then
             a_inv := a_in;
@@ -440,10 +378,12 @@ begin
             b_or_m1 := (others => '1');
         end if;
         sum_with_carry := ppc_adde(a_inv, b_or_m1,
-                                   decode_input_carry(e_in.input_carry, v.e.xerc));
+                                   decode_input_carry(e_in.input_carry, xerc_in));
         adder_result <= sum_with_carry(63 downto 0);
-        carry_32 := sum_with_carry(32) xor a_inv(32) xor b_in(32);
-        carry_64 := sum_with_carry(64);
+        carry_32 <= sum_with_carry(32) xor a_inv(32) xor b_in(32);
+        carry_64 <= sum_with_carry(64);
+        overflow_32 <= calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31));
+        overflow_64 <= calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63));
 
         -- signals to multiply and divide units
         sign1 := '0';
@@ -470,12 +410,10 @@ begin
         end if;
 
         -- Interface to multiply and divide units
-       x_to_multiply <= MultiplyInputInit;
-       x_to_multiply.is_32bit <= e_in.is_32bit;
-
-        x_to_divider <= Execute1ToDividerInit;
         x_to_divider.is_signed <= e_in.is_signed;
        x_to_divider.is_32bit <= e_in.is_32bit;
+        x_to_divider.is_extended <= '0';
+        x_to_divider.is_modulus <= '0';
         if e_in.insn_type = OP_MOD then
             x_to_divider.is_modulus <= '1';
         end if;
@@ -492,6 +430,7 @@ begin
             addend := not addend;
         end if;
 
+       x_to_multiply.is_32bit <= e_in.is_32bit;
         x_to_multiply.not_result <= sign1 xor sign2;
         x_to_multiply.addend <= addend;
         x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
@@ -616,7 +555,7 @@ begin
         zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
         if zerolo = '1' and (l = '0' or zerohi = '1') then
             -- values are equal
-            trapval := "00100";
+            trapval <= "00100";
         else
             a_lt_lo := '0';
             a_lt_hi := '0';
@@ -640,14 +579,124 @@ begin
             if msb_a /= msb_b then
                 -- Comparison is clear from MSB difference.
                 -- for signed, 0 is greater; for unsigned, 1 is greater
-                trapval := msb_a & msb_b & '0' & msb_b & msb_a;
+                trapval <= msb_a & msb_b & '0' & msb_b & msb_a;
             else
                 -- MSBs are equal, so signed and unsigned comparisons give the
                 -- same answer.
-                trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
+                trapval <= a_lt & not a_lt & '0' & a_lt & not a_lt;
             end if;
         end if;
 
+        -- CR result mux
+        bf := insn_bf(e_in.insn);
+        crnum := to_integer(unsigned(bf));
+        newcrf := (others => '0');
+        case current.sub_select is
+            when "000" =>
+                -- CMP and CMPL instructions
+                if e_in.is_signed = '1' then
+                    newcrf := trapval(4 downto 2) & xerc_in.so;
+                else
+                    newcrf := trapval(1 downto 0) & trapval(2) & xerc_in.so;
+                end if;
+            when "001" =>
+                newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
+            when "010" =>
+                newcrf := ppc_cmpeqb(a_in, b_in);
+            when "011" =>
+                if current.insn(1) = '1' then
+                    -- CR logical instructions
+                    j := (7 - crnum) * 4;
+                    newcrf := cr_in(j + 3 downto j);
+                    bt := insn_bt(e_in.insn);
+                    ba := insn_ba(e_in.insn);
+                    bb := insn_bb(e_in.insn);
+                    btnum := 3 - to_integer(unsigned(bt(1 downto 0)));
+                    banum := 31 - to_integer(unsigned(ba));
+                    bbnum := 31 - to_integer(unsigned(bb));
+                    -- Bits 6-9 of the instruction word give the truth table
+                    -- of the requested logical operation
+                    cr_operands := cr_in(banum) & cr_in(bbnum);
+                    crresult := e_in.insn(6 + to_integer(unsigned(cr_operands)));
+                    for i in 0 to 3 loop
+                        if i = btnum then
+                            newcrf(i) := crresult;
+                        end if;
+                    end loop;
+                else
+                    -- MCRF
+                    bfa := insn_bfa(e_in.insn);
+                    scrnum := to_integer(unsigned(bfa));
+                    j := (7 - scrnum) * 4;
+                    newcrf := cr_in(j + 3 downto j);
+                end if;
+            when "100" =>
+                -- MCRXRX
+                newcrf := xerc_in.ov & xerc_in.ca & xerc_in.ov32 & xerc_in.ca32;
+            when others =>
+        end case;
+        if current.insn_type = OP_MTCRF then
+            if e_in.insn(20) = '0' then
+                -- mtcrf
+                write_cr_mask <= insn_fxm(e_in.insn);
+            else
+                -- mtocrf: We require one hot priority encoding here
+                crnum := fxm_to_num(insn_fxm(e_in.insn));
+                write_cr_mask <= num_to_fxm(crnum);
+            end if;
+            write_cr_data <= c_in(31 downto 0);
+        else
+            write_cr_mask <= num_to_fxm(crnum);
+            write_cr_data <= newcrf & newcrf & newcrf & newcrf &
+                             newcrf & newcrf & newcrf & newcrf;
+        end if;
+
+    end process;
+
+    execute1_1: process(all)
+       variable v : reg_type;
+       variable lo, hi : integer;
+       variable sh, mb, me : std_ulogic_vector(5 downto 0);
+       variable bo, bi : std_ulogic_vector(4 downto 0);
+       variable overflow : std_ulogic;
+        variable lv : Execute1ToLoadstore1Type;
+       variable irq_valid : std_ulogic;
+       variable exception : std_ulogic;
+        variable illegal : std_ulogic;
+        variable is_branch : std_ulogic;
+        variable is_direct_branch : std_ulogic;
+        variable taken_branch : std_ulogic;
+        variable abs_branch : std_ulogic;
+        variable spr_val : std_ulogic_vector(63 downto 0);
+        variable do_trace : std_ulogic;
+        variable hold_wr_data : std_ulogic;
+        variable fv : Execute1ToFPUType;
+    begin
+        is_branch := '0';
+        is_direct_branch := '0';
+        taken_branch := '0';
+        abs_branch := '0';
+        hold_wr_data := '0';
+
+       v := r;
+       v.e := Execute1ToWritebackInit;
+        v.e.redir_mode := ctrl.msr(MSR_IR) & not ctrl.msr(MSR_PR) &
+                          not ctrl.msr(MSR_LE) & not ctrl.msr(MSR_SF);
+        v.e.xerc := xerc_in;
+
+        lv := Execute1ToLoadstore1Init;
+        fv := Execute1ToFPUInit;
+
+        x_to_multiply.valid <= '0';
+        x_to_divider.valid <= '0';
+       v.mul_in_progress := '0';
+        v.div_in_progress := '0';
+        v.cntz_in_progress := '0';
+        v.mul_finish := '0';
+
+        spr_result <= (others => '0');
+        spr_val := (others => '0');
+
        ctrl_tmp <= ctrl;
        -- FIXME: run at 512MHz not core freq
        ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
@@ -656,11 +705,11 @@ begin
        irq_valid := '0';
        if ctrl.msr(MSR_EE) = '1' then
            if ctrl.dec(63) = '1' then
-               v.vector := 16#900#;
+               v.e.intr_vec := 16#900#;
                report "IRQ valid: DEC";
                irq_valid := '1';
            elsif ext_irq_in = '1' then
-               v.vector := 16#500#;
+               v.e.intr_vec := 16#500#;
                report "IRQ valid: External";
                irq_valid := '1';
            end if;
@@ -679,21 +728,17 @@ begin
        rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
         rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
 
-        ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
-       ctrl_tmp.irq_state <= WRITE_SRR0;
+        v.e.srr1 := (others => '0');
        exception := '0';
         illegal := '0';
-        exception_nextpc := '0';
-        v.e.exc_write_enable := '0';
-        v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
         if valid_in = '1' then
-            v.e.exc_write_data := e_in.nia;
-            v.last_nia := e_in.nia;
+            v.e.last_nia := e_in.nia;
         else
-            v.e.exc_write_data := r.last_nia;
+            v.e.last_nia := r.e.last_nia;
         end if;
 
         v.e.mode_32bit := not ctrl.msr(MSR_SF);
+        v.e.instr_tag := current.instr_tag;
 
         do_trace := valid_in and ctrl.msr(MSR_SE);
         if valid_in = '1' then
@@ -702,25 +747,25 @@ begin
 
         -- Determine if there is any exception to be taken
         -- before/instead of executing this instruction
-        if valid_in = '1' and e_in.second = '0' then
+        if valid_in = '1' and e_in.second = '0' and l_in.in_progress = '0' then
             if HAS_FPU and r.fp_exception_next = '1' then
                 -- This is used for FP-type program interrupts that
                 -- become pending due to MSR[FE0,FE1] changing from 00 to non-zero.
                 exception := '1';
-                v.vector := 16#700#;
-                ctrl_tmp.srr1(63 - 43) <= '1';
-                ctrl_tmp.srr1(63 - 47) <= '1';
+                v.e.intr_vec := 16#700#;
+                v.e.srr1(47 - 43) := '1';
+                v.e.srr1(47 - 47) := '1';
             elsif r.trace_next = '1' then
                 -- Generate a trace interrupt rather than executing the next instruction
                 -- or taking any asynchronous interrupt
                 exception := '1';
-                v.vector := 16#d00#;
-                ctrl_tmp.srr1(63 - 33) <= '1';
+                v.e.intr_vec := 16#d00#;
+                v.e.srr1(47 - 33) := '1';
                 if r.prev_op = OP_LOAD or r.prev_op = OP_ICBI or r.prev_op = OP_ICBT or
                     r.prev_op = OP_DCBT or r.prev_op = OP_DCBST or r.prev_op = OP_DCBF then
-                    ctrl_tmp.srr1(63 - 35) <= '1';
+                    v.e.srr1(47 - 35) := '1';
                 elsif r.prev_op = OP_STORE or r.prev_op = OP_DCBZ or r.prev_op = OP_DCBTST then
-                    ctrl_tmp.srr1(63 - 36) <= '1';
+                    v.e.srr1(47 - 36) := '1';
                 end if;
 
             elsif irq_valid = '1' then
@@ -731,9 +776,9 @@ begin
             elsif ctrl.msr(MSR_PR) = '1' and instr_is_privileged(e_in.insn_type, e_in.insn) then
                 -- generate a program interrupt
                 exception := '1';
-                v.vector := 16#700#;
+                v.e.intr_vec := 16#700#;
                 -- set bit 45 to indicate privileged instruction type interrupt
-                ctrl_tmp.srr1(63 - 45) <= '1';
+                v.e.srr1(47 - 45) := '1';
                 report "privileged instruction";
 
             elsif not HAS_FPU and e_in.fac = FPU then
@@ -743,16 +788,13 @@ begin
             elsif HAS_FPU and ctrl.msr(MSR_FP) = '0' and e_in.fac = FPU then
                 -- generate a floating-point unavailable interrupt
                 exception := '1';
-                v.vector := 16#800#;
+                v.e.intr_vec := 16#800#;
                 report "FP unavailable interrupt";
             end if;
         end if;
 
        if valid_in = '1' and exception = '0' and illegal = '0' and e_in.unit = ALU then
-           report "execute nia " & to_hstring(e_in.nia);
-
             v.cur_instr := e_in;
-            v.next_lr := next_nia;
            v.e.valid := '1';
 
            case_0: case e_in.insn_type is
@@ -767,8 +809,8 @@ begin
                -- we need two cycles to write srr0 and 1
                 if e_in.insn(1) = '1' then
                     exception := '1';
-                    exception_nextpc := '1';
-                    v.vector := 16#C00#;
+                    v.e.intr_vec := 16#C00#;
+                    v.e.last_nia := next_nia;
                     report "sc";
                 else
                     illegal := '1';
@@ -791,35 +833,17 @@ begin
                     else
                         v.e.xerc.ov := carry_64;
                         v.e.xerc.ov32 := carry_32;
-                        v.e.write_xerc_enable := '1';
                     end if;
                 end if;
                 if e_in.oe = '1' then
-                    set_ov(v.e,
-                           calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63)),
-                           calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31)));
+                    set_ov(v.e, overflow_64, overflow_32);
                 end if;
             when OP_CMP =>
-                -- CMP and CMPL instructions
-                if e_in.is_signed = '1' then
-                    newcrf := trapval(4 downto 2) & v.e.xerc.so;
-                else
-                    newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
-                end if;
-                bf := insn_bf(e_in.insn);
-                crnum := to_integer(unsigned(bf));
-                v.e.write_cr_enable := '1';
-                v.e.write_cr_mask := num_to_fxm(crnum);
-                for i in 0 to 7 loop
-                    lo := i*4;
-                    hi := lo + 3;
-                    v.e.write_cr_data(hi downto lo) := newcrf;
-                end loop;
             when OP_TRAP =>
                 -- trap instructions (tw, twi, td, tdi)
-                v.vector := 16#700#;
+                v.e.intr_vec := 16#700#;
                 -- set bit 46 to say trap occurred
-                ctrl_tmp.srr1(63 - 46) <= '1';
+                v.e.srr1(47 - 46) := '1';
                 if or (trapval and insn_to(e_in.insn)) = '1' then
                     -- generate trap-type program interrupt
                     exception := '1';
@@ -827,21 +851,7 @@ begin
                 end if;
             when OP_ADDG6S =>
             when OP_CMPRB =>
-                newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
-                bf := insn_bf(e_in.insn);
-                crnum := to_integer(unsigned(bf));
-                v.e.write_cr_enable := '1';
-                v.e.write_cr_mask := num_to_fxm(crnum);
-                v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
-                                     newcrf & newcrf & newcrf & newcrf;
             when OP_CMPEQB =>
-                newcrf := ppc_cmpeqb(a_in, b_in);
-                bf := insn_bf(e_in.insn);
-                crnum := to_integer(unsigned(bf));
-                v.e.write_cr_enable := '1';
-                v.e.write_cr_mask := num_to_fxm(crnum);
-                v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
-                                     newcrf & newcrf & newcrf & newcrf;
             when OP_AND | OP_OR | OP_XOR | OP_POPCNT | OP_PRTY | OP_CMPB | OP_EXTS |
                     OP_BPERM | OP_BCD =>
 
@@ -880,8 +890,8 @@ begin
                 end if;
 
            when OP_RFID =>
-                v.redir_mode := (a_in(MSR_IR) or a_in(MSR_PR)) & not a_in(MSR_PR) &
-                                not a_in(MSR_LE) & not a_in(MSR_SF);
+                v.e.redir_mode := (a_in(MSR_IR) or a_in(MSR_PR)) & not a_in(MSR_PR) &
+                                  not a_in(MSR_LE) & not a_in(MSR_SF);
                 -- Can't use msr_copy here because the partial function MSR
                 -- bits should be left unchanged, not zeroed.
                 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
@@ -907,57 +917,8 @@ begin
                 v.cntz_in_progress := '1';
                 v.busy := '1';
            when OP_ISEL =>
-           when OP_CROP =>
-               cr_op := insn_cr(e_in.insn);
-               report "CR OP " & to_hstring(cr_op);
-               if cr_op(0) = '0' then -- MCRF
-                   bf := insn_bf(e_in.insn);
-                   bfa := insn_bfa(e_in.insn);
-                   v.e.write_cr_enable := '1';
-                   crnum := to_integer(unsigned(bf));
-                   scrnum := to_integer(unsigned(bfa));
-                   v.e.write_cr_mask := num_to_fxm(crnum);
-                   for i in 0 to 7 loop
-                       lo := (7-i)*4;
-                       hi := lo + 3;
-                       if i = scrnum then
-                           newcrf := cr_in(hi downto lo);
-                       end if;
-                   end loop;
-                   for i in 0 to 7 loop
-                       lo := i*4;
-                       hi := lo + 3;
-                       v.e.write_cr_data(hi downto lo) := newcrf;
-                   end loop;
-               else
-                   v.e.write_cr_enable := '1';
-                   bt := insn_bt(e_in.insn);
-                   ba := insn_ba(e_in.insn);
-                   bb := insn_bb(e_in.insn);
-                   btnum := 31 - to_integer(unsigned(bt));
-                   banum := 31 - to_integer(unsigned(ba));
-                   bbnum := 31 - to_integer(unsigned(bb));
-                    -- Bits 5-8 of cr_op give the truth table of the requested
-                    -- logical operation
-                    cr_operands := cr_in(banum) & cr_in(bbnum);
-                    crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
-                   v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
-                   for i in 0 to 31 loop
-                       if i = btnum then
-                           v.e.write_cr_data(i) := crresult;
-                       else
-                           v.e.write_cr_data(i) := cr_in(i);
-                       end if;
-                   end loop;
-               end if;
+            when OP_CROP =>
             when OP_MCRXRX =>
-                newcrf := v.e.xerc.ov & v.e.xerc.ca & v.e.xerc.ov32 & v.e.xerc.ca32;
-                bf := insn_bf(e_in.insn);
-                crnum := to_integer(unsigned(bf));
-                v.e.write_cr_enable := '1';
-                v.e.write_cr_mask := num_to_fxm(crnum);
-                v.e.write_cr_data := newcrf & newcrf & newcrf & newcrf &
-                                     newcrf & newcrf & newcrf & newcrf;
             when OP_DARN =>
            when OP_MFMSR =>
            when OP_MFSPR =>
@@ -968,12 +929,12 @@ begin
                     if decode_spr_num(e_in.insn) = SPR_XER then
                        -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
                        spr_val(63 downto 32) := (others => '0');
-                       spr_val(63-32) := v.e.xerc.so;
-                       spr_val(63-33) := v.e.xerc.ov;
-                       spr_val(63-34) := v.e.xerc.ca;
+                       spr_val(63-32) := xerc_in.so;
+                       spr_val(63-33) := xerc_in.ov;
+                       spr_val(63-34) := xerc_in.ca;
                        spr_val(63-35 downto 63-43) := "000000000";
-                       spr_val(63-44) := v.e.xerc.ov32;
-                       spr_val(63-45) := v.e.xerc.ca32;
+                       spr_val(63-44) := xerc_in.ov32;
+                       spr_val(63-45) := xerc_in.ca32;
                     end if;
                else
                     spr_val := c_in;
@@ -1007,16 +968,6 @@ begin
 
            when OP_MFCR =>
            when OP_MTCRF =>
-               v.e.write_cr_enable := '1';
-               if e_in.insn(20) = '0' then
-                   -- mtcrf
-                   v.e.write_cr_mask := insn_fxm(e_in.insn);
-               else
-                   -- mtocrf: We require one hot priority encoding here
-                   crnum := fxm_to_num(insn_fxm(e_in.insn));
-                   v.e.write_cr_mask := num_to_fxm(crnum);
-               end if;
-               v.e.write_cr_data := c_in(31 downto 0);
             when OP_MTMSRD =>
                 if e_in.insn(16) = '1' then
                     -- just update EE and RI
@@ -1051,7 +1002,6 @@ begin
                        v.e.xerc.ca := c_in(63-34);
                        v.e.xerc.ov32 := c_in(63-44);
                        v.e.xerc.ca32 := c_in(63-45);
-                       v.e.write_xerc_enable := '1';
                    end if;
                else
                    -- slow spr
@@ -1075,8 +1025,8 @@ begin
             when OP_SETB =>
 
            when OP_ISYNC =>
-               v.redirect := '1';
-                v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
+               v.e.redirect := '1';
+                v.e.br_offset := std_ulogic_vector(to_unsigned(4, 64));
 
            when OP_ICBI =>
                icache_inval <= '1';
@@ -1104,16 +1054,16 @@ begin
                     ctrl_tmp.cfar <= e_in.nia;
                 end if;
                 if taken_branch = '1' then
-                    v.br_offset := b_in;
-                    v.abs_br := abs_branch;
+                    v.e.br_offset := b_in;
+                    v.e.abs_br := abs_branch;
                 else
-                    v.br_offset := std_ulogic_vector(to_unsigned(4, 64));
+                    v.e.br_offset := std_ulogic_vector(to_unsigned(4, 64));
                 end if;
                 if taken_branch /= e_in.br_pred then
-                    v.redirect := '1';
+                    v.e.redirect := '1';
                 end if;
-                v.br_last := is_direct_branch;
-                v.taken_br := taken_branch;
+                v.e.br_last := is_direct_branch;
+                v.e.br_taken := taken_branch;
             end if;
 
         elsif valid_in = '1' and exception = '0' and illegal = '0' then
@@ -1134,28 +1084,7 @@ begin
         -- The following cases all occur when r.busy = 1 and therefore
         -- valid_in = 0.  Hence they don't happen in the same cycle as any of
         -- the cases above which depend on valid_in = 1.
-
-       if ctrl.irq_state = WRITE_SRR1 then
-           v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
-           v.e.exc_write_data := ctrl.srr1;
-            v.e.exc_write_enable := '1';
-            ctrl_tmp.msr(MSR_SF) <= '1';
-            ctrl_tmp.msr(MSR_EE) <= '0';
-            ctrl_tmp.msr(MSR_PR) <= '0';
-            ctrl_tmp.msr(MSR_SE) <= '0';
-            ctrl_tmp.msr(MSR_BE) <= '0';
-            ctrl_tmp.msr(MSR_FP) <= '0';
-            ctrl_tmp.msr(MSR_FE0) <= '0';
-            ctrl_tmp.msr(MSR_FE1) <= '0';
-            ctrl_tmp.msr(MSR_IR) <= '0';
-            ctrl_tmp.msr(MSR_DR) <= '0';
-            ctrl_tmp.msr(MSR_RI) <= '0';
-            ctrl_tmp.msr(MSR_LE) <= '1';
-            v.trace_next := '0';
-            v.fp_exception_next := '0';
-           report "Writing SRR1: " & to_hstring(ctrl.srr1);
-
-        elsif r.cntz_in_progress = '1' then
+        if r.cntz_in_progress = '1' then
             -- cnt[lt]z always takes two cycles
             v.e.valid := '1';
        elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
@@ -1171,7 +1100,6 @@ begin
                     v.mul_finish := '1';
                     v.busy := '1';
                 else
-                    v.e.write_xerc_enable := current.oe;
                     -- We must test oe because the RC update code in writeback
                     -- will use the xerc value to set CR0:SO so we must not clobber
                     -- xerc if OE wasn't set.
@@ -1191,7 +1119,6 @@ begin
            end if;
         elsif r.mul_finish = '1' then
             hold_wr_data := '1';
-            v.e.write_xerc_enable := current.oe;
             v.e.xerc.ov := multiply_to_x.overflow;
             v.e.xerc.ov32 := multiply_to_x.overflow;
             if multiply_to_x.overflow = '1' then
@@ -1200,68 +1127,38 @@ begin
             v.e.valid := '1';
        end if;
 
-        -- Generate FP-type program interrupt.  fp_in.interrupt will only
-        -- be set during the execution of a FP instruction.
-        -- The case where MSR[FE0,FE1] goes from zero to non-zero is
-        -- handled above by mtmsrd and rfid setting v.fp_exception_next.
-        if HAS_FPU and fp_in.interrupt = '1' then
-            v.vector := 16#700#;
-            ctrl_tmp.srr1(63 - 43) <= '1';
+        if illegal = '1' then
             exception := '1';
-        end if;
-
-        if illegal = '1' or (HAS_FPU and fp_in.illegal = '1') then
-            exception := '1';
-            v.vector := 16#700#;
+            v.e.intr_vec := 16#700#;
             -- Since we aren't doing Hypervisor emulation assist (0xe40) we
             -- set bit 44 to indicate we have an illegal
-            ctrl_tmp.srr1(63 - 44) <= '1';
+            v.e.srr1(47 - 44) := '1';
             report "illegal";
         end if;
-       if exception = '1' then
-            v.e.exc_write_enable := '1';
-            if exception_nextpc = '1' then
-                v.e.exc_write_data := next_nia;
-            end if;
-       end if;
 
-        -- generate DSI or DSegI for load/store exceptions
-        -- or ISI or ISegI for instruction fetch exceptions
-        if l_in.exception = '1' then
-            if l_in.alignment = '1' then
-                v.vector := 16#600#;
-            elsif l_in.instr_fault = '0' then
-                if l_in.segment_fault = '0' then
-                    v.vector := 16#300#;
-                else
-                    v.vector := 16#380#;
-                end if;
-            else
-                if l_in.segment_fault = '0' then
-                    ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
-                    ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
-                    ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
-                    ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
-                    v.vector := 16#400#;
-                else
-                    v.vector := 16#480#;
-                end if;
-            end if;
-            v.e.exc_write_enable := '1';
-            v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
-            report "ldst exception writing srr0=" & to_hstring(r.last_nia);
-        end if;
-
-        if exception = '1' or l_in.exception = '1' then
-            ctrl_tmp.irq_state <= WRITE_SRR1;
-            v.redirect := '1';
-            v.do_intr := '1';
-        end if;
+        v.e.interrupt := exception;
 
         if do_trace = '1' then
             v.trace_next := '1';
         end if;
 
+       if interrupt_in = '1' then
+            ctrl_tmp.msr(MSR_SF) <= '1';
+            ctrl_tmp.msr(MSR_EE) <= '0';
+            ctrl_tmp.msr(MSR_PR) <= '0';
+            ctrl_tmp.msr(MSR_SE) <= '0';
+            ctrl_tmp.msr(MSR_BE) <= '0';
+            ctrl_tmp.msr(MSR_FP) <= '0';
+            ctrl_tmp.msr(MSR_FE0) <= '0';
+            ctrl_tmp.msr(MSR_FE1) <= '0';
+            ctrl_tmp.msr(MSR_IR) <= '0';
+            ctrl_tmp.msr(MSR_DR) <= '0';
+            ctrl_tmp.msr(MSR_RI) <= '0';
+            ctrl_tmp.msr(MSR_LE) <= '1';
+            v.trace_next := '0';
+            v.fp_exception_next := '0';
+        end if;
+
         if hold_wr_data = '0' then
             v.e.write_data := alu_result;
         else
@@ -1270,45 +1167,29 @@ begin
         v.e.write_reg := current.write_reg;
        v.e.write_enable := current.write_reg_enable and v.e.valid and not exception;
         v.e.rc := current.rc and v.e.valid and not exception;
-
-        -- Defer completion for one cycle when redirecting.
-        -- This also ensures r.busy = 1 when ctrl.irq_state = WRITE_SRR1
-        if v.redirect = '1' then
-            v.busy := '1';
-            v.e.valid := '0';
-        end if;
-        if r.redirect = '1' then
-            v.e.valid := '1';
-        end if;
-
-        -- Outputs to fetch1
-        f.redirect := r.redirect;
-        f.br_nia := r.last_nia;
-        f.br_last := r.br_last and not r.do_intr;
-        f.br_taken := r.taken_br;
-        if r.do_intr = '1' then
-            f.redirect_nia := std_ulogic_vector(to_unsigned(r.vector, 64));
-            f.virt_mode := '0';
-            f.priv_mode := '1';
-            -- XXX need an interrupt LE bit here, e.g. from LPCR
-            f.big_endian := '0';
-            f.mode_32bit := '0';
-        else
-            if r.abs_br = '1' then
-                f.redirect_nia := r.br_offset;
+        v.e.write_cr_data := write_cr_data;
+        v.e.write_cr_mask := write_cr_mask;
+        v.e.write_cr_enable := current.output_cr and v.e.valid and not exception;
+        v.e.write_xerc_enable := current.output_xer and v.e.valid and not exception;
+
+        bypass_data.tag.valid <= current.instr_tag.valid and current.write_reg_enable and v.e.valid;
+        bypass_data.tag.tag <= current.instr_tag.tag;
+        bypass_data.data <= v.e.write_data;
+
+        bypass_cr_data.tag.valid <= current.instr_tag.valid and current.output_cr and v.e.valid;
+        bypass_cr_data.tag.tag <= current.instr_tag.tag;
+        for i in 0 to 7 loop
+            if v.e.write_cr_mask(i) = '1' then
+                bypass_cr_data.data(i*4 + 3 downto i*4) <= v.e.write_cr_data(i*4 + 3 downto i*4);
             else
-                f.redirect_nia := std_ulogic_vector(unsigned(r.last_nia) + unsigned(r.br_offset));
+                bypass_cr_data.data(i*4 + 3 downto i*4) <= cr_in(i*4 + 3 downto i*4);
             end if;
-            -- send MSR[IR], ~MSR[PR], ~MSR[LE] and ~MSR[SF] up to fetch1
-            f.virt_mode := r.redir_mode(3);
-            f.priv_mode := r.redir_mode(2);
-            f.big_endian := r.redir_mode(1);
-            f.mode_32bit := r.redir_mode(0);
-        end if;
+        end loop;
 
         -- Outputs to loadstore1 (async)
         lv.op := e_in.insn_type;
         lv.nia := e_in.nia;
+        lv.instr_tag := e_in.instr_tag;
         lv.addr1 := a_in;
         lv.addr2 := b_in;
         lv.data := c_in;
@@ -1317,7 +1198,7 @@ begin
         lv.byte_reverse := e_in.byte_reverse xnor ctrl.msr(MSR_LE);
         lv.sign_extend := e_in.sign_extend;
         lv.update := e_in.update;
-        lv.xerc := v.e.xerc;
+        lv.xerc := xerc_in;
         lv.reserve := e_in.reserve;
         lv.rc := e_in.rc;
         lv.insn := e_in.insn;
@@ -1337,6 +1218,7 @@ begin
         fv.op := e_in.insn_type;
         fv.nia := e_in.nia;
         fv.insn := e_in.insn;
+        fv.itag := e_in.instr_tag;
         fv.single := e_in.is_32bit;
         fv.fe_mode := ctrl.msr(MSR_FE0) & ctrl.msr(MSR_FE1);
         fv.fra := a_in;
@@ -1350,11 +1232,10 @@ begin
        rin <= v;
 
        -- update outputs
-       f_out <= f;
         l_out <= lv;
        e_out <= r.e;
+        e_out.msr <= msr_copy(ctrl.msr);
         fp_out <= fv;
-       flush_out <= f_out.redirect;
 
         exception_log <= exception;
         irq_valid_log <= irq_valid;
@@ -1370,13 +1251,13 @@ begin
                             ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
                             exception_log &
                             irq_valid_log &
-                            std_ulogic_vector(to_unsigned(irq_state_t'pos(ctrl.irq_state), 1)) &
+                            interrupt_in &
                             "000" &
                             r.e.write_enable &
                             r.e.valid &
-                            f_out.redirect &
+                            (r.e.redirect or r.e.interrupt) &
                             r.busy &
-                            flush_out;
+                            flush_in;
             end if;
         end process;
         log_out <= log_data;