[otbn] Don't jump to the instruction we're choosing in rig

The code that picks a jump target is careful to pick somewhere that
doesn't yet have any instruction data, but it didn't know not to pick
the address whose instruction we're currently defining.

We'll probably need something a bit cleverer in the future (when we
start generating branches and function calls), but this is a simple
fix for now.

Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
diff --git a/hw/ip/otbn/util/rig/gens/jump.py b/hw/ip/otbn/util/rig/gens/jump.py
index 1babc50..a8eccec 100644
--- a/hw/ip/otbn/util/rig/gens/jump.py
+++ b/hw/ip/otbn/util/rig/gens/jump.py
@@ -105,7 +105,7 @@
         # Pick a branch target. "1" here is the minimum number of instructions
         # that must fit. One is enough (we'll just end up generating another
         # branch immediately)
-        tgt = program.pick_branch_target(1, tgt_min, tgt_max)
+        tgt = program.pick_branch_target(model.pc, 1, tgt_min, tgt_max)
         if tgt is None:
             return None
         assert tgt_min <= tgt <= tgt_max
diff --git a/hw/ip/otbn/util/rig/program.py b/hw/ip/otbn/util/rig/program.py
index f1bd45f..fae42b4 100644
--- a/hw/ip/otbn/util/rig/program.py
+++ b/hw/ip/otbn/util/rig/program.py
@@ -324,12 +324,17 @@
         out_file.write('}\n')
 
     def pick_branch_targets(self,
+                            cur_pc: int,
                             min_len: int,
                             count: int,
                             tgt_min: Optional[int],
                             tgt_max: Optional[int]) -> Optional[List[int]]:
         '''Pick count random targets for a branch destination
 
+        As well as avoiding addresses that have program data, this also treats
+        behaves as if there is an instruction at cur_pc (this is the
+        instruction that's being picked).
+
         There is guaranteed to be at least space for min_len instructions at
         each target, but the weighting tries to favour places with some space
         for instruction sequences.
@@ -345,10 +350,12 @@
         # sections in which they should land. To do *that*, we start by making
         # a list of all the gaps between sections in ascending order of base
         # address.
-        section_list = list(self._sections.items())
+        section_list = [(base, len(insns))
+                        for base, insns in self._sections.items()]
         if self._cur_section is not None:
-            cur_base, cur_open_section = self._cur_section
-            section_list.append((cur_base, cur_open_section.insns))
+            base, open_section = self._cur_section
+            section_list.append((base, len(open_section.insns)))
+        section_list.append((cur_pc, 1))
         section_list.sort()
 
         gap_vma = 0
@@ -376,7 +383,7 @@
                 if gap_lo <= gap_hi:
                     gap_list.append((gap_lo, gap_hi - gap_lo + 1))
 
-            gap_vma = section_base + 4 * len(section_insns)
+            gap_vma = section_base + 4 * section_insns
 
         # Deal with any final gap above all known sections in the same way as
         # the internal gaps.
@@ -467,6 +474,7 @@
         return ret
 
     def pick_branch_target(self,
+                           cur_pc: int,
                            min_len: int,
                            tgt_min: Optional[int],
                            tgt_max: Optional[int]) -> Optional[int]:
@@ -476,7 +484,7 @@
         function.
 
         '''
-        tgts = self.pick_branch_targets(min_len, 1, tgt_min, tgt_max)
+        tgts = self.pick_branch_targets(cur_pc, min_len, 1, tgt_min, tgt_max)
         if tgts is None:
             return None