bank/csrgen: add get_offset function to pre-calculate register addresses
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Fri, 2 Aug 2013 21:05:54 +0000 (23:05 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Fri, 2 Aug 2013 21:05:54 +0000 (23:05 +0200)
migen/bank/csrgen.py

index 3624d90d3b9fee3be7b074ea2e150c058ccbadd3..320b3ec0c1e7f8ee8042f804dcc40865340252d4 100644 (file)
@@ -4,6 +4,14 @@ from migen.fhdl.std import *
 from migen.bus import csr
 from migen.bank.description import *
 
+def get_offset(description, name, csr_data_width=8):
+       offset = 0
+       for c in description:
+               if c.name == name:
+                       return offset
+               offset += (c.size + csr_data_width - 1)//csr_data_width
+       raise KeyError("CSR not found: "+name)
+
 class Bank(Module):
        def __init__(self, description, address=0, bus=None):
                if bus is None: