soc: add add_ram/add_rom methods
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Fri, 7 Feb 2020 14:57:46 +0000 (15:57 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Fri, 7 Feb 2020 15:06:32 +0000 (16:06 +0100)
litex/soc/integration/soc.py
litex/soc/integration/soc_core.py

index 0d550662537e402e91481522da16b47cf001794f..59b321a3fa280f56872ba90bcd7744cf7806e3b5 100755 (executable)
@@ -38,11 +38,12 @@ def buildtime(with_time=True):
 # SoCRegion ----------------------------------------------------------------------------------------
 
 class SoCRegion:
-    def __init__(self, origin=None, size=None, cached=True):
-        self.logger = logging.getLogger("SoCRegion")
-        self.origin = origin
-        self.size   = size
-        self.cached = cached
+    def __init__(self, origin=None, size=None, cached=True, read_only=False):
+        self.logger    = logging.getLogger("SoCRegion")
+        self.origin    = origin
+        self.size      = size
+        self.cached    = cached
+        self.read_only = read_only
 
     def decoder(self):
         origin = self.origin
@@ -64,6 +65,8 @@ class SoCRegion:
         if self.size is not None:
             r += "Size: {}, ".format(colorer("0x{:08x}".format(self.size)))
         r += "Cached: {}".format(colorer(self.cached))
+        if self.read_only:
+            r += ", Read Only"
         return r
 
 
@@ -534,6 +537,25 @@ class SoC(Module):
         self.logger.info(self.irq)
         self.logger.info(colorer("-"*80, color="bright"))
 
+
+    # SoC main components --------------------------------------------------------------------------
+    def add_ram(self, name, origin, size, contents=[], read_only=False):
+        ram_bus = wishbone.Interface(data_width=self.bus.data_width)
+        ram     = wishbone.SRAM(size, bus=ram_bus, init=contents, read_only=read_only)
+        self.bus.add_slave(name, ram.bus, SoCRegion(origin=origin, size=size, read_only=read_only))
+        if hasattr(self, name):
+            self.logger.error("{} name already used.".format(colorer(name, "red")))
+            raise
+        self.logger.info("RAM {} {} {}.".format(
+            colorer(name),
+            colorer("added", "green"),
+            self.bus.regions[name]))
+        setattr(self.submodules, name, ram)
+
+    def add_rom(self, name, origin, size, contents=[]):
+        self.add_ram(name, origin, size, contents, read_only=True)
+
+    # SoC finalization -----------------------------------------------------------------------------
     def do_finalize(self):
         self.logger.info(colorer("-"*80, color="bright"))
         self.logger.info(colorer("Finalized SoC:", color="cyan"))
index 5340e8f239502ca92b7fea3546475fc438aac12f..cc2fd0ddfb90748eb74f4f4e53ee919b8fbd0241 100644 (file)
@@ -124,6 +124,7 @@ class SoCCore(SoC):
         self.csr_data_width             = csr_data_width
         self.csr_address_width          = csr_address_width
 
+        self.with_wishbone              = with_wishbone
         self.wishbone_timeout_cycles    = wishbone_timeout_cycles
 
         # Modules instances ------------------------------------------------------------------------
@@ -192,13 +193,11 @@ class SoCCore(SoC):
 
         # Add integrated SRAM
         if integrated_sram_size:
-            self.submodules.sram = wishbone.SRAM(integrated_sram_size, init=integrated_sram_init)
-            self.register_mem("sram", self.soc_mem_map["sram"], self.sram.bus, integrated_sram_size)
+            self.add_ram("sram", self.soc_mem_map["sram"], integrated_sram_size)
 
         # Add integrated MAIN_RAM (only useful when no external SRAM/SDRAM is available)
         if integrated_main_ram_size:
-            self.submodules.main_ram = wishbone.SRAM(integrated_main_ram_size, init=integrated_main_ram_init)
-            self.register_mem("main_ram", self.soc_mem_map["main_ram"], self.main_ram.bus, integrated_main_ram_size)
+            self.add_ram("main_ram", self.soc_mem_map["main_ram"], integrated_main_ram_size)
 
         # Add UART
         if with_uart: