soc/tools/remote/csr_builder: manage memory regions and some fixes on CSRRegister
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 23 Nov 2015 18:13:37 +0000 (19:13 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 23 Nov 2015 18:13:37 +0000 (19:13 +0100)
litex/soc/tools/remote/csr_builder.py

index 942929152de02daa5238899ca235f5af7c96d26c..e30a18ad6be92a9757a8bc7062eca8649df0f637 100644 (file)
@@ -17,6 +17,7 @@ class CSRRegister:
     def __init__(self, readfn, writefn, name, addr, length, data_width, mode):
         self.readfn = readfn
         self.writefn = writefn
+        self.name = name
         self.addr = addr
         self.length = length
         self.data_width = data_width
@@ -24,7 +25,7 @@ class CSRRegister:
 
     def read(self):
         if self.mode not in ["rw", "ro"]:
-            raise KeyError(name + "register not readable")
+            raise KeyError(self.name + "register not readable")
         datas = self.readfn(self.addr, length=self.length)
         if isinstance(datas, int):
             return datas
@@ -37,19 +38,26 @@ class CSRRegister:
 
     def write(self, value):
         if self.mode not in ["rw", "wo"]:
-            raise KeyError(name + "register not writable")
+            raise KeyError(self.name + "register not writable")
         datas = []
         for i in range(self.length):
             datas.append((value >> ((self.length-1-i)*self.data_width)) & (2**self.data_width-1))
         self.writefn(self.addr, datas)
 
 
+class CSRMemoryRegion:
+    def __init__(self, base, size):
+        self.base = base
+        self.size = size
+
+
 class CSRBuilder:
     def __init__(self, comm, csr_csv, csr_data_width):
         self.csr_data_width = csr_data_width
         self.constants = self.build_constants(csr_csv)
         self.bases = self.build_bases(csr_csv)
         self.regs = self.build_registers(csr_csv, comm.read, comm.write)
+        self.mems = self.build_memories(csr_csv)
 
     def build_bases(self, csr_csv):
         csv_reader = csv.reader(open(csr_csv), delimiter=',', quotechar='#')
@@ -82,3 +90,12 @@ class CSRBuilder:
                 except:
                     d[name] = value
         return CSRElements(d)
+
+    def build_memories(self, csr_csv):
+        csv_reader = csv.reader(open(csr_csv), delimiter=',', quotechar='#')
+        d = {}
+        for item in csv_reader:
+            group, name, base, size, dummy1 = item
+            if group == "memory_region":
+                d[name] = CSRMemoryRegion(int(base, 16), int(size))
+        return CSRElements(d)