litex/migen/bank/csrgen.py

70 lines
2.1 KiB
Python
Raw Normal View History

2011-12-16 15:30:14 -05:00
from migen.fhdl.structure import *
2011-12-16 10:02:55 -05:00
from migen.bus.csr import *
from migen.bank.description import *
2011-12-05 11:43:56 -05:00
class Bank:
def __init__(self, description, address=0):
self.description = description
self.address = address
self.interface = Slave()
2011-12-16 15:30:14 -05:00
declare_signal(self, "_sel")
2011-12-05 11:43:56 -05:00
2011-12-16 10:02:55 -05:00
def get_fragment(self):
2011-12-05 11:43:56 -05:00
comb = []
sync = []
2011-12-16 15:30:14 -05:00
comb.append(self._sel.eq(self.interface.a_i[10:] == Constant(self.address, BV(4))))
2011-12-05 11:43:56 -05:00
nregs = len(self.description)
2011-12-16 15:30:14 -05:00
nbits = bits_for(nregs-1)
2011-12-05 11:43:56 -05:00
# Bus writes
bwcases = []
for i in range(nregs):
reg = self.description[i]
nfields = len(reg.fields)
2011-12-16 15:30:14 -05:00
bwra = [Constant(i, BV(nbits))]
2011-12-05 11:43:56 -05:00
for j in range(nfields):
field = reg.fields[j]
if field.access_bus == WRITE_ONLY or field.access_bus == READ_WRITE:
2011-12-16 15:30:14 -05:00
bwra.append(field.storage.eq(self.interface.d_i[j]))
if len(bwra) > 1:
bwcases.append(bwra)
2011-12-05 11:43:56 -05:00
if bwcases:
2011-12-16 15:30:14 -05:00
sync.append(If(self._sel & self.interface.we_i, Case(self.interface.a_i[:nbits], *bwcases)))
2011-12-05 11:43:56 -05:00
# Bus reads
brcases = []
for i in range(nregs):
reg = self.description[i]
nfields = len(reg.fields)
brs = []
reg_readable = False
for j in range(nfields):
field = reg.fields[j]
if field.access_bus == READ_ONLY or field.access_bus == READ_WRITE:
brs.append(field.storage)
reg_readable = True
else:
2011-12-16 15:30:14 -05:00
brs.append(Constant(0, BV(field.size)))
2011-12-05 11:43:56 -05:00
if reg_readable:
if len(brs) > 1:
2011-12-16 15:30:14 -05:00
brcases.append([Constant(i, BV(nbits)), self.interface.d_o.eq(f.Cat(*brs))])
2011-12-05 11:43:56 -05:00
else:
2011-12-16 15:30:14 -05:00
brcases.append([Constant(i, BV(nbits)), self.interface.d_o.eq(brs[0])])
2011-12-05 11:43:56 -05:00
if brcases:
2011-12-16 15:30:14 -05:00
sync.append(self.interface.d_o.eq(Constant(0, BV(32))))
sync.append(If(self._sel, Case(self.interface.a_i[:nbits], *brcases)))
2011-12-05 11:43:56 -05:00
else:
2011-12-16 15:30:14 -05:00
comb.append(self.interface.d_o.eq(Constant(0, BV(32))))
2011-12-05 11:43:56 -05:00
# Device access
for reg in self.description:
for field in reg.fields:
if field.access_dev == READ_ONLY or field.access_dev == READ_WRITE:
2011-12-16 15:30:14 -05:00
comb.append(field.dev_r.eq(field.storage))
2011-12-05 11:43:56 -05:00
if field.access_dev == WRITE_ONLY or field.access_dev == READ_WRITE:
2011-12-16 15:30:14 -05:00
sync.append(If(field.dev_we, field.storage.eq(field.dev_w)))
2011-12-05 11:43:56 -05:00
2011-12-16 15:30:14 -05:00
return Fragment(comb, sync)