mention convenience variables
[soc.git] / src / soc / fu / trap / main_stage.py
index ff79d6460110e20910c6264015375415ebba9ef7..5d29838f34492a0043bdf671d4e568502ed22e76 100644 (file)
@@ -9,12 +9,29 @@ from nmigen import (Module, Signal, Cat, Mux, Const, signed)
 from nmutil.pipemodbase import PipeModBase
 from nmutil.extend import exts
 from soc.fu.trap.pipe_data import TrapInputData, TrapOutputData
+from soc.fu.branch.main_stage import br_ext
 from soc.decoder.power_enums import InternalOp
 
 from soc.decoder.power_fields import DecodeFields
 from soc.decoder.power_fieldsn import SignalBitRange
 
 
+# Listed in V3.0B Book III Chap 4.2.1
+# MSR bit numbers
+MSR_SF  = (63 - 0)     # Sixty-Four bit mode
+MSR_HV  = (63 - 3)     # Hypervisor state
+MSR_S   = (63 - 41)    # Secure state
+MSR_EE  = (63 - 48)    # External interrupt Enable
+MSR_PR  = (63 - 49)    # PRoblem state
+MSR_FP  = (63 - 50)    # FP available
+MSR_ME  = (63 - 51)    # Machine Check int enable
+MSR_IR  = (63 - 58)    # Instruction Relocation
+MSR_DR  = (63 - 59)    # Data Relocation
+MSR_PMM = (63 - 60)    # Performance Monitor Mark
+MSR_RI  = (63 - 62)    # Recoverable Interrupt
+MSR_LE  = (63 - 63)    # Little Endian
+
+
 class TrapMainStage(PipeModBase):
     def __init__(self, pspec):
         super().__init__(pspec, "main")
@@ -31,7 +48,11 @@ class TrapMainStage(PipeModBase):
         m = Module()
         comb = m.d.comb
         op = self.i.ctx.op
-        a_i, b_i = self.i.a, self.i.b
+
+        # convenience variables
+        a_i, b_i, cia_i, msr_i = self.i.a, self.i.b, self.i.cia, self.i.msr
+        o, msr_o, nia_o = self.o.o, self.o.msr, self.o.nia
+        srr0_o, srr1_o = self.o.srr0, self.o.srr1
 
         # take copy of D-Form TO field
         i_fields = self.fields.FormD
@@ -94,19 +115,19 @@ class TrapMainStage(PipeModBase):
                     ctrl_tmp.srr1(63 - 46) <= '1';
                 """
                 with m.If(should_trap):
-                    comb += self.o.nia.data.eq(0x700)         # trap address
-                    comb += self.o.nia.ok.eq(1)
-                    comb += self.o.srr1.data.eq(self.i.msr)   # old MSR
-                    comb += self.o.srr1.data[63-46].eq(1)     # XXX which bit?
-                    comb += self.o.srr1.ok.eq(1)
-                    comb += self.o.srr0.data.eq(self.i.cia)   # old PC
-                    comb += self.o.srr0.ok.eq(1)
+                    comb += nia_o.data.eq(0x700)         # trap address
+                    comb += nia_o.ok.eq(1)
+                    comb += srr1_o.data.eq(msr_i)   # old MSR
+                    comb += srr1_o.data[63-46].eq(1)     # XXX which bit?
+                    comb += srr1_o.ok.eq(1)
+                    comb += srr0_o.data.eq(cia_i)   # old PC
+                    comb += srr0_o.ok.eq(1)
 
             # move to SPR
             with m.Case(InternalOp.OP_MTMSR):
                 # TODO: some of the bits need zeroing?
                 """
-                if e_in.insn(16) = '1' then
+                if e_in.insn(16) = '1' then  <-- this is X-form field "L".
                     -- just update EE and RI
                     ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
                     ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
@@ -121,8 +142,27 @@ class TrapMainStage(PipeModBase):
                         ctrl_tmp.msr(MSR_IR) <= '1';
                         ctrl_tmp.msr(MSR_DR) <= '1';
                 """
-                comb += self.o.msr.data.eq(a)
-                comb += self.o.msr.ok.eq(1)
+                """
+                L = self.fields.FormXL.L[0:-1]
+                if e_in.insn(16) = '1' then  <-- this is X-form field "L".
+                -- just update EE and RI
+                ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
+                ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
+                """
+                L = self.fields.FormX.L[0:-1]
+                with m.If(L):
+                    comb += msr_o[MSR_EE].eq(msr_i[MSR_EE])
+                    comb += msr_o[MSR_RI].eq(msr_i[MSR_RI])
+
+                with m.Else():
+                    for stt, end in [(1,12), (13, 60), (61, 64)]:
+                        # TODO a_i not a
+                        comb += msr_o.data[stt:end].eq(a[stt:end])
+                    with m.If(a[MSR_PR]):
+                            msr_o[MSR_EE].eq(1)
+                            msr_o[MSR_IR].eq(1)
+                            msr_o[MSR_DR].eq(1)
+                comb += msr_o.ok.eq(1)
 
             # move from SPR
             with m.Case(InternalOp.OP_MFMSR):
@@ -132,10 +172,9 @@ class TrapMainStage(PipeModBase):
                         result := ctrl.msr;
                         result_en := '1';
                 """
-                comb += self.o.o.data.eq(self.i.msr)
-                comb += self.o.o.ok.eq(1)
+                comb += o.data.eq(msr_i)
+                comb += o.ok.eq(1)
 
-            # TODO
             with m.Case(InternalOp.OP_RFID):
                 """
                 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
@@ -152,9 +191,18 @@ class TrapMainStage(PipeModBase):
                     ctrl_tmp.msr(MSR_DR) <= '1';
                 end if;
                 """
-                pass
+                # TODO a_i not a, from the convenience variables
+                comb += nia_o.data.eq(br_ext(a[63:1] & 0))
+                comb += nia_o.ok.eq(1)
+                for stt, end in [(0,16), (22, 27), (31, 64)]:
+                    # TODO b_i not a, look above, the VHDL uses b_in
+                    comb += msr_o.data[stt:end].eq(a[stt:end])
+                with m.If(a[MSR_PR]):
+                        msr_o[MSR_EE].eq(1)
+                        msr_o[MSR_IR].eq(1)
+                        msr_o[MSR_DR].eq(1)
+                comb += msr_o.ok.eq(1)
 
-            # TODO
             with m.Case(InternalOp.OP_SC):
                 """
                 # TODO: scv must generate illegal instruction.  this is
@@ -162,8 +210,12 @@ class TrapMainStage(PipeModBase):
                 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#C00#, 64));
                 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
                 """
-                pass
+                comb += nia_o.eq(0xC00) # trap address
+                comb += nia_o.ok.eq(1)
+                comb += srr1_o.data.eq(msr_i)
+                comb += srr1_o.ok.eq(1)
 
+            # TODO (later)
             #with m.Case(InternalOp.OP_ADDPCIS):
             #    pass