whoops, no ability to add comments in between functions in pseudocode
[openpower-isa.git] / src / openpower / decoder / isa / caller.py
index e2cf01ebc7bf0faa201a0ca87c9773b2de5e9db8..8150785ba00d8210f54bbc0952468adcad01515d 100644 (file)
@@ -36,6 +36,8 @@ from openpower.decoder.power_svp64 import SVP64RM, decode_extra
 from openpower.decoder.isa.radixmmu import RADIX
 from openpower.decoder.isa.mem import Mem, swap_order, MemException
 from openpower.decoder.isa.svshape import SVSHAPE
+from openpower.decoder.isa.svstate import SVP64State
+
 
 from openpower.util import log
 
@@ -74,7 +76,6 @@ REG_SORT_ORDER = {
     "TAR": 0,
     "MSR": 0,
     "SVSTATE": 0,
-    "SVREMAP": 0,
     "SVSHAPE0": 0,
     "SVSHAPE1": 0,
     "SVSHAPE2": 0,
@@ -248,19 +249,6 @@ class PC:
         namespace['NIA'] = self.NIA
 
 
-# Simple-V: see https://libre-soc.org/openpower/sv
-class SVP64State:
-    def __init__(self, init=0):
-        self.spr = SelectableInt(init, 32)
-        # fields of SVSTATE, see https://libre-soc.org/openpower/sv/sprs/
-        self.maxvl = FieldSelectableInt(self.spr, tuple(range(0,7)))
-        self.vl = FieldSelectableInt(self.spr, tuple(range(7,14)))
-        self.srcstep = FieldSelectableInt(self.spr, tuple(range(14,21)))
-        self.dststep = FieldSelectableInt(self.spr, tuple(range(21,28)))
-        self.subvl = FieldSelectableInt(self.spr, tuple(range(28,30)))
-        self.svstep = FieldSelectableInt(self.spr, tuple(range(30,32)))
-
-
 # SVP64 ReMap field
 class SVP64RMFields:
     def __init__(self, init=0):
@@ -493,6 +481,12 @@ def get_pdecode_idx_out(dec2, name):
                                       dec2.dec.RT)
         if out_sel == OutSel.RT.value:
             return out, o_isvec
+    elif name == 'RT_OR_ZERO':
+        log ("get_pdecode_idx_out", out_sel, OutSel.RT.value,
+                                      OutSel.RT_OR_ZERO.value, out, o_isvec,
+                                      dec2.dec.RT)
+        if out_sel == OutSel.RT_OR_ZERO.value:
+            return out, o_isvec
     elif name == 'FRA':
         log ("get_pdecode_idx_out", out_sel, OutSel.FRA.value, out, o_isvec)
         if out_sel == OutSel.FRA.value:
@@ -652,7 +646,7 @@ class ISACaller:
                                'memassign': self.memassign,
                                'NIA': self.pc.NIA,
                                'CIA': self.pc.CIA,
-                               'SVSTATE': self.svstate.spr,
+                               'SVSTATE': self.svstate,
                                'SVSHAPE0': self.spr['SVSHAPE0'],
                                'SVSHAPE1': self.spr['SVSHAPE1'],
                                'SVSHAPE2': self.spr['SVSHAPE2'],
@@ -733,6 +727,7 @@ class ISACaller:
         # then "yield" fields only from op_fields rather than hard-coded
         # list, here.
         fields = self.decoder.sigforms[formname]
+        log("prep_namespace", formname, op_fields)
         for name in op_fields:
             if name == 'spr':
                 sig = getattr(fields, name.upper())
@@ -752,8 +747,8 @@ class ISACaller:
         self.namespace['CA32'] = self.spr['XER'][XER_bits['CA32']].value
 
         # add some SVSTATE convenience variables
-        vl = self.svstate.vl.asint(msb0=True)
-        srcstep = self.svstate.srcstep.asint(msb0=True)
+        vl = self.svstate.vl
+        srcstep = self.svstate.srcstep
         self.namespace['VL'] = vl
         self.namespace['srcstep'] = srcstep
 
@@ -868,6 +863,7 @@ class ISACaller:
         SO = self.spr['XER'][XER_bits['SO']]
         log("handle_comparison SO", SO)
         cr_field = selectconcat(negative, positive, zero, SO)
+        log("handle_comparison cr_field", self.cr, cr_idx, cr_field)
         self.crl[cr_idx].eq(cr_field)
 
     def set_pc(self, pc_val):
@@ -905,7 +901,7 @@ class ISACaller:
         yield self.dec2.state.msr.eq(self.msr.value)
         yield self.dec2.state.pc.eq(pc)
         if self.svstate is not None:
-            yield self.dec2.state.svstate.eq(self.svstate.spr.value)
+            yield self.dec2.state.svstate.eq(self.svstate.value)
 
         # SVP64.  first, check if the opcode is EXT001, and SVP64 id bits set
         yield Settle()
@@ -921,20 +917,22 @@ class ISACaller:
         self.pc.update_nia(self.is_svp64_mode)
         yield self.dec2.is_svp64_mode.eq(self.is_svp64_mode) # set SVP64 decode
         self.namespace['NIA'] = self.pc.NIA
-        self.namespace['SVSTATE'] = self.svstate.spr
+        self.namespace['SVSTATE'] = self.svstate
         if not self.is_svp64_mode:
             return
 
         # in SVP64 mode.  decode/print out svp64 prefix, get v3.0B instruction
         log ("svp64.rm", bin(pfx.rm.asint(msb0=True)))
-        log ("    svstate.vl", self.svstate.vl.asint(msb0=True))
-        log ("    svstate.mvl", self.svstate.maxvl.asint(msb0=True))
+        log ("    svstate.vl", self.svstate.vl)
+        log ("    svstate.mvl", self.svstate.maxvl)
         sv_rm = pfx.rm.asint(msb0=True)
         ins = self.imem.ld(pc+4, 4, False, True, instr_fetch=True)
         log("     svsetup: 0x%x 0x%x %s" % (pc+4, ins & 0xffffffff, bin(ins)))
         yield self.dec2.dec.raw_opcode_in.eq(ins & 0xffffffff) # v3.0B suffix
         yield self.dec2.sv_rm.eq(sv_rm)                        # svp64 prefix
         yield Settle()
+        # store this for use in get_src_dststeps()
+        self.ldstmode = yield self.dec2.rm_dec.ldstmode
 
     def execute_one(self):
         """execute one instruction
@@ -1030,6 +1028,44 @@ class ISACaller:
                 asmop = 'mtcrf'
         return asmop
 
+    def get_remap_indices(self):
+        """WARNING, this function stores remap_idxs and remap_loopends
+        in the class for later use.  this to avoid problems with yield
+        """
+        # go through all iterators in lock-step, advance to next remap_idx
+        srcstep, dststep = self.get_src_dststeps()
+        # get four SVSHAPEs. here we are hard-coding
+        SVSHAPE0 = self.spr['SVSHAPE0']
+        SVSHAPE1 = self.spr['SVSHAPE1']
+        SVSHAPE2 = self.spr['SVSHAPE2']
+        SVSHAPE3 = self.spr['SVSHAPE3']
+        # set up the iterators
+        remaps = [(SVSHAPE0, SVSHAPE0.get_iterator()),
+                  (SVSHAPE1, SVSHAPE1.get_iterator()),
+                  (SVSHAPE2, SVSHAPE2.get_iterator()),
+                  (SVSHAPE3, SVSHAPE3.get_iterator()),
+                 ]
+
+        self.remap_loopends = [0] * 4
+        self.remap_idxs = [0, 1, 2, 3]
+        dbg = []
+        for i, (shape, remap) in enumerate(remaps):
+            # zero is "disabled"
+            if shape.value == 0x0:
+                self.remap_idxs[i] = 0
+            # pick src or dststep depending on reg num (0-2=in, 3-4=out)
+            step = dststep if (i in [3, 4]) else srcstep
+            # this is terrible.  O(N^2) looking for the match. but hey.
+            for idx, (remap_idx, loopends) in enumerate(remap):
+                if idx == step:
+                    break
+            self.remap_idxs[i] = remap_idx
+            self.remap_loopends[i] = loopends
+            dbg.append((i, step, remap_idx, loopends))
+        for (i, step, remap_idx, loopends) in dbg:
+            log ("SVSHAPE %d idx, end" % i, step, remap_idx, bin(loopends))
+        return remaps
+
     def get_spr_msb(self):
         dec_insn = yield self.dec2.e.do.insn
         return dec_insn & (1 << 20) != 0  # sigh - XFF.spr[-1]?
@@ -1040,15 +1076,15 @@ class ISACaller:
         self.last_st_addr = None # reset the last known store address
         self.last_ld_addr = None # etc.
 
-        name = name.strip()  # remove spaces if not already done so
+        ins_name = name.strip()  # remove spaces if not already done so
         if self.halted:
-            log("halted - not executing", name)
+            log("halted - not executing", ins_name)
             return
 
         # TODO, asmregs is from the spec, e.g. add RT,RA,RB
         # see http://bugs.libre-riscv.org/show_bug.cgi?id=282
         asmop = yield from self.get_assembly_name()
-        log("call", name, asmop)
+        log("call", ins_name, asmop)
 
         # check privileged
         int_op = yield self.dec2.dec.op.internal_op
@@ -1074,62 +1110,87 @@ class ISACaller:
             return
 
         # check halted condition
-        if name == 'attn':
+        if ins_name == 'attn':
             self.halted = True
             return
 
         # check illegal instruction
         illegal = False
-        if name not in ['mtcrf', 'mtocrf']:
-            illegal = name != asmop
+        if ins_name not in ['mtcrf', 'mtocrf']:
+            illegal = ins_name != asmop
 
         # sigh deal with setvl not being supported by binutils (.long)
         if asmop.startswith('setvl'):
             illegal = False
-            name = 'setvl'
+            ins_name = 'setvl'
+
+        # and svstep not being supported by binutils (.long)
+        if asmop.startswith('svstep'):
+            illegal = False
+            ins_name = 'svstep'
+
+        # and svremap not being supported by binutils (.long)
+        if asmop.startswith('svremap'):
+            illegal = False
+            ins_name = 'svremap'
 
         # and svshape not being supported by binutils (.long)
         if asmop.startswith('svshape'):
             illegal = False
-            name = 'svshape'
+            ins_name = 'svshape'
+
+        # and fsin and fcos
+        if asmop == 'fsins':
+            illegal = False
+            ins_name = 'fsins'
+        if asmop == 'fcoss':
+            illegal = False
+            ins_name = 'fcoss'
 
         # sigh also deal with ffmadds not being supported by binutils (.long)
         if asmop == 'ffmadds':
             illegal = False
-            name = 'ffmadds'
+            ins_name = 'ffmadds'
+
+        # and fdmadds not being supported by binutils (.long)
+        if asmop == 'fdmadds':
+            illegal = False
+            ins_name = 'fdmadds'
 
         # and ffadds not being supported by binutils (.long)
         if asmop == 'ffadds':
             illegal = False
-            name = 'ffadds'
+            ins_name = 'ffadds'
 
         if illegal:
-            print("illegal", name, asmop)
+            print("illegal", ins_name, asmop)
             self.call_trap(0x700, PIb.ILLEG)
             print("name %s != %s - calling ILLEGAL trap, PC: %x" %
-                  (name, asmop, self.pc.CIA.value))
+                  (ins_name, asmop, self.pc.CIA.value))
             return
 
         # this is for setvl "Vertical" mode: if set true,
-        # srcstep/dststep is explicitly advanced
+        # srcstep/dststep is explicitly advanced. mode says which SVSTATE to
+        # test for Rc=1 end condition.  3 bits of all 3 loops are put into CR0
         self.allow_next_step_inc = False
+        self.svstate_next_mode = 0
 
         # nop has to be supported, we could let the actual op calculate
         # but PowerDecoder has a pattern for nop
-        if name is 'nop':
+        if ins_name is 'nop':
             self.update_pc_next()
             return
 
-        info = self.instrs[name]
+        info = self.instrs[ins_name]
         yield from self.prep_namespace(info.form, info.op_fields)
 
         # preserve order of register names
         input_names = create_args(list(info.read_regs) +
                                   list(info.uninit_regs))
-        log(input_names)
+        log("input names", input_names)
 
         # get SVP64 entry for the current instruction
-        sv_rm = self.svp64rm.instrs.get(name)
+        sv_rm = self.svp64rm.instrs.get(ins_name)
         if sv_rm is not None:
             dest_cr, src_cr, src_byname, dest_byname = decode_extra(sv_rm)
         else:
@@ -1137,18 +1198,20 @@ class ISACaller:
         log ("sv rm", sv_rm, dest_cr, src_cr, src_byname, dest_byname)
 
         # see if srcstep/dststep need skipping over masked-out predicate bits
-        if self.is_svp64_mode:
+        if (self.is_svp64_mode or ins_name == 'setvl' or
+           ins_name.startswith("sv")):
             yield from self.svstate_pre_inc()
+        if self.is_svp64_mode:
             pre = yield from self.update_new_svstate_steps()
             if pre:
                 self.svp64_reset_loop()
                 self.update_nia()
                 self.update_pc_next()
                 return
-            srcstep, dststep = self.new_srcstep, self.new_dststep
+            srcstep, dststep = self.get_src_dststeps()
             pred_dst_zero = self.pred_dst_zero
             pred_src_zero = self.pred_src_zero
-            vl = self.svstate.vl.asint(msb0=True)
+            vl = self.svstate.vl
 
         # VL=0 in SVP64 mode means "do nothing: skip instruction"
         if self.is_svp64_mode and vl == 0:
@@ -1157,77 +1220,60 @@ class ISACaller:
                                        self.namespace['NIA'])
             return
 
-        # for when SVSHAPE is active, a very bad hack here (to be replaced)
-        # using pre-arranged schedule.  all of this is awful but it is a
-        # start.  next job will be to put the proper activation in place
-        yield self.dec2.remap_active.eq(1 if self.last_op_svshape else 0)
+        # for when SVREMAP is active, using pre-arranged schedule.
+        # note: modifying PowerDecoder2 needs to "settle"
+        remap_en = self.svstate.SVme
+        persist = self.svstate.RMpst
+        active = (persist or self.last_op_svshape) and remap_en != 0
+        yield self.dec2.remap_active.eq(remap_en if active else 0)
         yield Settle()
-        if self.is_svp64_mode and self.last_op_svshape:
-            # get four SVSHAPEs. here we are hard-coding
-            # SVSHAPE0 to FRT, SVSHAPE1 to FRA, SVSHAPE2 to FRC and
-            # SVSHAPE3 to FRB, assuming "fmadd FRT, FRA, FRC, FRB."
-            SVSHAPE0 = self.spr['SVSHAPE0']
-            SVSHAPE1 = self.spr['SVSHAPE1']
-            SVSHAPE2 = self.spr['SVSHAPE2']
-            SVSHAPE3 = self.spr['SVSHAPE3']
+        if persist or self.last_op_svshape:
+            remaps = self.get_remap_indices()
+        if self.is_svp64_mode and (persist or self.last_op_svshape):
+            # just some convenient debug info
             for i in range(4):
                 sname = 'SVSHAPE%d' % i
                 shape = self.spr[sname]
-                print (sname, bin(shape.value))
-                print ("    lims", shape.lims)
-                print ("    mode", shape.mode)
-                print ("    skip", shape.skip)
-
-            remaps = [(SVSHAPE0, SVSHAPE0.get_iterator()),
-                      (SVSHAPE1, SVSHAPE1.get_iterator()),
-                      (SVSHAPE2, SVSHAPE2.get_iterator()),
-                      (SVSHAPE3, SVSHAPE3.get_iterator()),
-                     ]
+                log (sname, bin(shape.value))
+                log ("    lims", shape.lims)
+                log ("    mode", shape.mode)
+                log ("    skip", shape.skip)
+
+            # set up the list of steps to remap
+            mi0 = self.svstate.mi0
+            mi1 = self.svstate.mi1
+            mi2 = self.svstate.mi2
+            mo0 = self.svstate.mo0
+            mo1 = self.svstate.mo1
+            steps = [(self.dec2.in1_step, mi0), # RA
+                     (self.dec2.in2_step, mi1), # RB
+                     (self.dec2.in3_step, mi2), # RC
+                     (self.dec2.o_step, mo0),   # RT
+                     (self.dec2.o2_step, mo1),   # EA
+                    ]
+            remap_idxs = self.remap_idxs
             rremaps = []
-            for i, (shape, remap) in enumerate(remaps):
+            # now cross-index the required SHAPE for each of 3-in 2-out regs
+            rnames = ['RA', 'RB', 'RC', 'RT', 'EA']
+            for i, (dstep, shape_idx) in enumerate(steps):
+                (shape, remap) = remaps[shape_idx]
+                remap_idx = remap_idxs[shape_idx]
                 # zero is "disabled"
                 if shape.value == 0x0:
                     continue
-                # XXX hardcoded! pick dststep for out (i==0) else srcstep
-                if shape.mode == 0b00: # multiply mode
-                    step = dststep if (i == 0) else srcstep
-                if shape.mode == 0b01: # FFT butterfly mode
-                    step = srcstep # XXX HACK - for now only use srcstep
-                # this is terrible.  O(N^2) looking for the match. but hey.
-                for idx, remap_idx in enumerate(remap):
-                    if idx == step:
-                        break
-                # multiply mode
-                if shape.mode == 0b00:
-                    if i == 0:
-                        yield self.dec2.o_step.eq(remap_idx)   # RT
-                        yield self.dec2.o2_step.eq(remap_idx)  # EA
-                    elif i == 1:
-                        yield self.dec2.in1_step.eq(remap_idx) # RA
-                    elif i == 2:
-                        yield self.dec2.in3_step.eq(remap_idx) # RB
-                    elif i == 3:
-                        yield self.dec2.in2_step.eq(remap_idx) # RC
-                # FFT butterfly mode
-                if shape.mode == 0b01:
-                    if i == 0:
-                        yield self.dec2.o_step.eq(remap_idx)   # RT
-                        yield self.dec2.in2_step.eq(remap_idx) # RB
-                    elif i == 1:
-                        yield self.dec2.in1_step.eq(remap_idx) # RA
-                        yield self.dec2.o2_step.eq(remap_idx)  # EA (FRS)
-                    elif i == 2:
-                        yield self.dec2.in3_step.eq(remap_idx) # RC
-                    elif i == 3:
-                        pass # no SVSHAPE3
-                rremaps.append((shape.mode, i, idx, remap_idx)) # debug printing
+                # now set the actual requested step to the current index
+                yield dstep.eq(remap_idx)
+
+                # debug printout info
+                rremaps.append((shape.mode, i, rnames[i], shape_idx,
+                                remap_idx))
             for x in rremaps:
-                print ("shape remap", x)
+                log ("shape remap", x)
         # after that, settle down (combinatorial) to let Vector reg numbers
         # work themselves out
         yield Settle()
         remap_active = yield self.dec2.remap_active
-        print ("remap active", remap_active)
+        log ("remap active", bin(remap_active))
 
         # main input registers (RT, RA ...)
         inputs = []
@@ -1257,10 +1303,14 @@ class ISACaller:
                 reg_val = 0
             inputs.append(reg_val)
         # arrrrgh, awful hack, to get _RT into namespace
-        if asmop == 'setvl':
+        if ins_name in ['setvl', 'svstep']:
             regname = "_RT"
             RT = yield self.dec2.dec.RT
             self.namespace[regname] = SelectableInt(RT, 5)
+            if RT == 0:
+                self.namespace["RT"] = SelectableInt(0, 5)
+            regnum, is_vec = yield from get_pdecode_idx_out(self.dec2, "RT")
+            log('hack input reg %s %s' % (name, str(regnum)), is_vec)
 
         # in SVP64 mode for LD/ST work out immediate
         # XXX TODO: replace_ds for DS-Form rather than D-Form.
@@ -1274,28 +1324,39 @@ class ISACaller:
             if ldstmode == SVP64LDSTmode.BITREVERSE.value:
                 imm = yield self.dec2.dec.fields.FormSVD.SVD[0:11]
                 imm = exts(imm, 11) # sign-extend to integer
-                print ("bitrev SVD", imm)
+                log ("bitrev SVD", imm)
                 replace_d = True
             else:
-                imm = yield self.dec2.dec.fields.FormD.D[0:16]
+                if info.form == 'DS':
+                    # DS-Form, multiply by 4 then knock 2 bits off after
+                    imm = yield self.dec2.dec.fields.FormDS.DS[0:14] * 4
+                else:
+                    imm = yield self.dec2.dec.fields.FormD.D[0:16]
                 imm = exts(imm, 16) # sign-extend to integer
             # get the right step. LD is from srcstep, ST is dststep
             op = yield self.dec2.e.do.insn_type
             offsmul = 0
             if op == MicrOp.OP_LOAD.value:
-                offsmul = srcstep
-                log("D-field src", imm, offsmul)
+                if remap_active:
+                    offsmul = yield self.dec2.in1_step
+                    log("D-field REMAP src", imm, offsmul)
+                else:
+                    offsmul = srcstep
+                    log("D-field src", imm, offsmul)
             elif op == MicrOp.OP_STORE.value:
+                # XXX NOTE! no bit-reversed STORE! this should not ever be used
                 offsmul = dststep
                 log("D-field dst", imm, offsmul)
-            # bit-reverse mode
+            # bit-reverse mode, rev already done through get_src_dst_steps()
             if ldstmode == SVP64LDSTmode.BITREVERSE.value:
                 # manually look up RC, sigh
                 RC = yield self.dec2.dec.RC[0:5]
                 RC = self.gpr(RC)
-                log ("RC", RC.value, "imm", imm, "offs", bin(offsmul),
-                     "rev", bin(bitrev(offsmul, vl)))
-                imm = SelectableInt((imm * bitrev(offsmul, vl)) << RC.value, 32)
+                log ("LD-BITREVERSE:", "VL", vl,
+                      "RC", RC.value, "imm", imm,
+                     "offs", bin(offsmul),
+                     )
+                imm = SelectableInt((imm * offsmul) << RC.value, 32)
             # Unit-Strided LD/ST adds offset*width to immediate
             elif ldstmode == SVP64LDSTmode.UNITSTRIDE.value:
                 ldst_len = yield self.dec2.e.do.data_len
@@ -1305,13 +1366,20 @@ class ISACaller:
             elif ldstmode == SVP64LDSTmode.ELSTRIDE.value:
                 imm = SelectableInt(imm * offsmul, 32)
                 replace_d = True
-            ldst_ra_vec = yield self.dec2.rm_dec.ldst_ra_vec
-            ldst_imz_in = yield self.dec2.rm_dec.ldst_imz_in
-            log("LDSTmode", ldstmode, SVP64LDSTmode.BITREVERSE.value,
-                            offsmul, imm, ldst_ra_vec, ldst_imz_in)
-        # new replacement D
+            if replace_d:
+                ldst_ra_vec = yield self.dec2.rm_dec.ldst_ra_vec
+                ldst_imz_in = yield self.dec2.rm_dec.ldst_imz_in
+                log("LDSTmode", SVP64LDSTmode(ldstmode),
+                                offsmul, imm, ldst_ra_vec, ldst_imz_in)
+        # new replacement D... errr.. DS
         if replace_d:
-            self.namespace['D'] = imm
+            if info.form == 'DS':
+                # TODO: assert 2 LSBs are zero?
+                log("DS-Form, TODO, assert 2 LSBs zero?", bin(imm.value))
+                imm.value = imm.value >> 2
+                self.namespace['DS'] = imm
+            else:
+                self.namespace['D'] = imm
 
         # "special" registers
         for special in info.special_regs:
@@ -1387,7 +1455,7 @@ class ISACaller:
         if not self.is_svp64_mode or not pred_dst_zero:
             if hasattr(self.dec2.e.do, "rc"):
                 rc_en = yield self.dec2.e.do.rc.rc
-        if rc_en:
+        if rc_en and ins_name not in ['svstep']:
             regnum, is_vec = yield from get_pdecode_cr_out(self.dec2, "CR0")
             self.handle_comparison(results, regnum)
 
@@ -1431,7 +1499,7 @@ class ISACaller:
                             ftype = 'fpr'
                         else:
                             ftype = 'gpr'
-                        log('writing %s %s %s' % (regnum, ftype, str(output)),
+                        log('writing %s %s %s' % (ftype, regnum, str(output)),
                                                      is_vec)
                     if output.bits > 64:
                         output = SelectableInt(output.value, 64)
@@ -1445,41 +1513,60 @@ class ISACaller:
         pre = False
         post = False
         if self.allow_next_step_inc:
-            log("SVSTATE_NEXT: inc requested")
+            log("SVSTATE_NEXT: inc requested, mode",
+                    self.svstate_next_mode, self.allow_next_step_inc)
             yield from self.svstate_pre_inc()
             pre = yield from self.update_new_svstate_steps()
             if pre:
                 # reset at end of loop including exit Vertical Mode
                 log ("SVSTATE_NEXT: end of loop, reset")
                 self.svp64_reset_loop()
-                self.msr[MSRb.SVF] = 0
+                self.svstate.vfirst = 0
                 self.update_nia()
                 if rc_en:
                     results = [SelectableInt(0, 64)]
                     self.handle_comparison(results) # CR0
             else:
-                log ("SVSTATE_NEXT: post-inc")
+                if self.allow_next_step_inc == 2:
+                    log ("SVSTATE_NEXT: read")
+                    yield from self.svstate_post_inc()
+                else:
+                    log ("SVSTATE_NEXT: post-inc")
+                # use actual src/dst-step here to check end, do NOT
+                # use bit-reversed version
                 srcstep, dststep = self.new_srcstep, self.new_dststep
-                vl = self.svstate.vl.asint(msb0=True)
+                remaps = self.get_remap_indices()
+                remap_idxs = self.remap_idxs
+                vl = self.svstate.vl
                 end_src = srcstep == vl-1
                 end_dst = dststep == vl-1
-                if not end_src:
-                    self.svstate.srcstep += SelectableInt(1, 7)
-                if not end_dst:
-                    self.svstate.dststep += SelectableInt(1, 7)
+                if self.allow_next_step_inc != 2:
+                    if not end_src:
+                        self.svstate.srcstep += SelectableInt(1, 7)
+                    if not end_dst:
+                        self.svstate.dststep += SelectableInt(1, 7)
                 self.namespace['SVSTATE'] = self.svstate.spr
                 # set CR0 (if Rc=1) based on end
                 if rc_en:
-                    srcstep = self.svstate.srcstep.asint(msb0=True)
-                    dststep = self.svstate.srcstep.asint(msb0=True)
-                    endtest = 0 if (end_src or end_dst) else 1
-                    results = [SelectableInt(endtest, 64)]
-                    self.handle_comparison(results) # CR0
+                    srcstep = self.svstate.srcstep
+                    dststep = self.svstate.srcstep
+                    endtest = 1 if (end_src or end_dst) else 0
+                    #results = [SelectableInt(endtest, 64)]
+                    #self.handle_comparison(results) # CR0
+
+                    # see if svstep was requested, if so, which SVSTATE
+                    endings = 0b111
+                    if self.svstate_next_mode > 0:
+                        shape_idx = self.svstate_next_mode.value-1
+                        endings = self.remap_loopends[shape_idx]
+                    cr_field = SelectableInt((~endings)<<1 | endtest, 4)
+                    print ("svstep Rc=1, CR0", cr_field)
+                    self.crl[0].eq(cr_field) # CR0
                 if end_src or end_dst:
                     # reset at end of loop including exit Vertical Mode
                     log ("SVSTATE_NEXT: after increments, reset")
                     self.svp64_reset_loop()
-                    self.msr[MSRb.SVF] = 0
+                    self.svstate.vfirst = 0
 
         elif self.is_svp64_mode:
             yield from self.svstate_post_inc()
@@ -1489,30 +1576,40 @@ class ISACaller:
             # to be able to know if it should apply in the next instruction.
             # also (if going to use this instruction) should disable ability
             # to interrupt in between. sigh.
-            self.last_op_svshape = asmop == 'svshape'
+            self.last_op_svshape = asmop == 'svremap'
 
         self.update_pc_next()
 
-    def SVSTATE_NEXT(self):
+    def SVSTATE_NEXT(self, mode, submode):
         """explicitly moves srcstep/dststep on to next element, for
         "Vertical-First" mode.  this function is called from
         setvl pseudo-code, as a pseudo-op "svstep"
+
+        WARNING: this function uses information that was created EARLIER
+        due to it being in the middle of a yield, but this function is
+        *NOT* called from yield (it's called from compiled pseudocode).
         """
-        log("SVSTATE_NEXT")
-        self.allow_next_step_inc = True
+        self.allow_next_step_inc = submode.value + 1
+        log("SVSTATE_NEXT mode", mode, submode, self.allow_next_step_inc)
+        self.svstate_next_mode = mode
+        if self.svstate_next_mode > 0:
+            shape_idx = self.svstate_next_mode.value-1
+            return SelectableInt(self.remap_idxs[shape_idx], 7)
+        return SelectableInt(0, 7)
 
     def svstate_pre_inc(self):
         """check if srcstep/dststep need to skip over masked-out predicate bits
         """
         # get SVSTATE VL (oh and print out some debug stuff)
-        vl = self.svstate.vl.asint(msb0=True)
-        srcstep = self.svstate.srcstep.asint(msb0=True)
-        dststep = self.svstate.dststep.asint(msb0=True)
+        vl = self.svstate.vl
+        srcstep = self.svstate.srcstep
+        dststep = self.svstate.dststep
         sv_a_nz = yield self.dec2.sv_a_nz
         fft_mode = yield self.dec2.use_svp64_fft
         in1 = yield self.dec2.e.read_reg1.data
-        log ("SVP64: VL, srcstep, dststep, sv_a_nz, in1 fft",
-                vl, srcstep, dststep, sv_a_nz, in1, fft_mode)
+        log ("SVP64: VL, srcstep, dststep, sv_a_nz, in1 fft, svp64",
+                vl, srcstep, dststep, sv_a_nz, in1, fft_mode,
+                self.is_svp64_mode)
 
         # get predicate mask
         srcmask = dstmask = 0xffff_ffff_ffff_ffff
@@ -1567,20 +1664,39 @@ class ISACaller:
         log ("    new srcstep", srcstep)
         log ("    new dststep", dststep)
 
+    def get_src_dststeps(self):
+        """gets srcstep and dststep but performs bit-reversal on srcstep if
+        required.  use this ONLY to perform calculations, do NOT update
+        SVSTATE with the bit-reversed value of srcstep
+
+        ARGH, had to store self.ldstmode and VL due to yield issues
+        """
+        srcstep, dststep = self.new_srcstep, self.new_dststep
+        if self.is_svp64_mode:
+            if self.ldstmode == SVP64LDSTmode.BITREVERSE.value:
+                vl = self.svstate.vl
+                log ("SRCSTEP-BITREVERSE:", "VL", vl, "srcstep", srcstep,
+                     "rev", bin(bitrev(srcstep, vl)))
+                srcstep = bitrev(srcstep, vl)
+
+        return (srcstep, dststep)
+
     def update_new_svstate_steps(self):
+        # note, do not get the bit-reversed srcstep here!
         srcstep, dststep = self.new_srcstep, self.new_dststep
 
         # update SVSTATE with new srcstep
-        self.svstate.srcstep[0:7] = srcstep
-        self.svstate.dststep[0:7] = dststep
-        self.namespace['SVSTATE'] = self.svstate.spr
-        yield self.dec2.state.svstate.eq(self.svstate.spr.value)
+        self.svstate.srcstep = srcstep
+        self.svstate.dststep = dststep
+        self.namespace['SVSTATE'] = self.svstate
+        yield self.dec2.state.svstate.eq(self.svstate.value)
         yield Settle() # let decoder update
-        srcstep = self.svstate.srcstep.asint(msb0=True)
-        dststep = self.svstate.dststep.asint(msb0=True)
-        vl = self.svstate.vl.asint(msb0=True)
+        srcstep = self.svstate.srcstep
+        dststep = self.svstate.dststep
+        vl = self.svstate.vl
         log ("    srcstep", srcstep)
         log ("    dststep", dststep)
+        log ("         vl", vl)
 
         # check if end reached (we let srcstep overrun, above)
         # nothing needs doing (TODO zeroing): just do next instruction
@@ -1588,18 +1704,19 @@ class ISACaller:
 
     def svstate_post_inc(self, vf=0):
         # check if SV "Vertical First" mode is enabled
-        log ("    SV Vertical First", vf, self.msr[MSRb.SVF].value)
-        if not vf and self.msr[MSRb.SVF].value == 1:
+        vfirst = self.svstate.vfirst
+        log ("    SV Vertical First", vf, vfirst)
+        if not vf and vfirst == 1:
             self.update_nia()
             return True
 
         # check if it is the SVSTATE.src/dest step that needs incrementing
         # this is our Sub-Program-Counter loop from 0 to VL-1
         # XXX twin predication TODO
-        vl = self.svstate.vl.asint(msb0=True)
-        mvl = self.svstate.maxvl.asint(msb0=True)
-        srcstep = self.svstate.srcstep.asint(msb0=True)
-        dststep = self.svstate.dststep.asint(msb0=True)
+        vl = self.svstate.vl
+        mvl = self.svstate.maxvl
+        srcstep = self.svstate.srcstep
+        dststep = self.svstate.dststep
         rm_mode = yield self.dec2.rm_dec.mode
         reverse_gear = yield self.dec2.rm_dec.reverse_gear
         sv_ptype = yield self.dec2.dec.op.SV_Ptype
@@ -1627,7 +1744,7 @@ class ISACaller:
             self.svstate.dststep += SelectableInt(1, 7)
             self.pc.NIA.value = self.pc.CIA.value
             self.namespace['NIA'] = self.pc.NIA
-            self.namespace['SVSTATE'] = self.svstate.spr
+            self.namespace['SVSTATE'] = self.svstate
             log("end of sub-pc call", self.namespace['CIA'],
                                  self.namespace['NIA'])
             return False # DO NOT allow PC update whilst Sub-PC loop running
@@ -1647,10 +1764,10 @@ class ISACaller:
                              self.namespace['SVSTATE'])
 
     def svp64_reset_loop(self):
-        self.svstate.srcstep[0:7] = 0
-        self.svstate.dststep[0:7] = 0
+        self.svstate.srcstep = 0
+        self.svstate.dststep = 0
         log ("    svstate.srcstep loop end (PC to update)")
-        self.namespace['SVSTATE'] = self.svstate.spr
+        self.namespace['SVSTATE'] = self.svstate
 
     def update_nia(self):
         self.pc.update_nia(self.is_svp64_mode)
@@ -1678,6 +1795,7 @@ def inject():
 
             context = args[0].namespace  # variables to be injected
             saved_values = func_globals.copy()  # Shallow copy of dict.
+            log("globals before", context.keys())
             func_globals.update(context)
             result = func(*args, **kwargs)
             log("globals after", func_globals['CIA'], func_globals['NIA'])