From 4014fbffe135086e86020d6571832b0a8fd7d11f Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Mon, 28 Oct 2019 17:07:37 +0100 Subject: [PATCH] soc_core/add_memory_region: fix memory overlap detection --- litex/soc/integration/soc_core.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/litex/soc/integration/soc_core.py b/litex/soc/integration/soc_core.py index 7c8abecf6..dea4b345e 100644 --- a/litex/soc/integration/soc_core.py +++ b/litex/soc/integration/soc_core.py @@ -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)