soc_core/add_memory_region: fix memory overlap detection
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 28 Oct 2019 16:07:37 +0000 (17:07 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 28 Oct 2019 16:07:37 +0000 (17:07 +0100)
litex/soc/integration/soc_core.py

index 7c8abecf6c3d741bf2960afead4c6dc463dda107..dea4b345e61836da294bb82e9463cee22947662c 100644 (file)
@@ -374,11 +374,15 @@ class SoCCore(Module):
     def add_memory_region(self, name, origin, length, io_region=False):
         if io_region:
             self.check_io_region(name, origin, length)
-        def in_this_region(addr):
-            return addr >= origin and addr < origin + length
+        def memory_overlap(o0, l0, o1, l1):
+            if o0 >= (o1 + l1):
+                return False
+            if o1 >= (o0 + l0):
+                return False
+            return True
         for n, r in self.mem_regions.items():
             r.length = 2**log2_int(r.length, False)
-            if n == name or in_this_region(r.origin) or in_this_region(r.origin + r.length - 1):
+            if n == name or memory_overlap(o0=r.origin, l0=r.length, o1=origin, l1=length):
                 raise ValueError("Memory region conflict between {} and {}".format(n, name))
         self.mem_regions[name] = SoCMemRegion(origin, length)