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 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
2011-12-05 11:43:56 -05:00
|
|
|
comb = []
|
|
|
|
sync = []
|
|
|
|
|
2011-12-18 15:47:48 -05:00
|
|
|
sel = Signal()
|
|
|
|
comb.append(sel.eq(self.interface.a_i[9:] == Constant(self.address, BV(5))))
|
2011-12-05 11:43:56 -05:00
|
|
|
|
2012-02-06 05:18:30 -05:00
|
|
|
nbits = bits_for(len(self.description)-1)
|
2011-12-05 11:43:56 -05:00
|
|
|
|
|
|
|
# Bus writes
|
|
|
|
bwcases = []
|
2012-02-06 05:18:30 -05:00
|
|
|
for i, reg in enumerate(self.description):
|
2012-02-06 07:55:50 -05:00
|
|
|
if isinstance(reg, RegisterRaw):
|
|
|
|
comb.append(reg.r.eq(self.interface.d_i[:reg.size]))
|
|
|
|
comb.append(reg.re.eq(sel & \
|
|
|
|
self.interface.we_i & \
|
|
|
|
(self.interface.a_i[:nbits] == Constant(i, BV(nbits)))))
|
|
|
|
elif isinstance(reg, RegisterFields):
|
2011-12-17 18:28:04 -05:00
|
|
|
bwra = [Constant(i, BV(nbits))]
|
2012-02-06 05:18:30 -05:00
|
|
|
for j, field in enumerate(reg.fields):
|
2011-12-17 18:28:04 -05:00
|
|
|
if field.access_bus == WRITE_ONLY or field.access_bus == READ_WRITE:
|
|
|
|
bwra.append(field.storage.eq(self.interface.d_i[j]))
|
|
|
|
if len(bwra) > 1:
|
|
|
|
bwcases.append(bwra)
|
|
|
|
else:
|
2012-02-06 07:55:50 -05:00
|
|
|
raise TypeError
|
2011-12-05 11:43:56 -05:00
|
|
|
if bwcases:
|
2011-12-18 15:47:48 -05:00
|
|
|
sync.append(If(sel & self.interface.we_i, Case(self.interface.a_i[:nbits], *bwcases)))
|
2011-12-05 11:43:56 -05:00
|
|
|
|
|
|
|
# Bus reads
|
|
|
|
brcases = []
|
2012-02-06 05:18:30 -05:00
|
|
|
for i, reg in enumerate(self.description):
|
2012-02-06 07:55:50 -05:00
|
|
|
if isinstance(reg, RegisterRaw):
|
|
|
|
brcases.append([Constant(i, BV(nbits)), self.interface.d_o.eq(reg.w)])
|
|
|
|
elif isinstance(reg, RegisterFields):
|
2011-12-17 18:28:04 -05:00
|
|
|
brs = []
|
|
|
|
reg_readable = False
|
2012-02-06 05:18:30 -05:00
|
|
|
for j, field in enumerate(reg.fields):
|
2011-12-17 18:28:04 -05:00
|
|
|
if field.access_bus == READ_ONLY or field.access_bus == READ_WRITE:
|
|
|
|
brs.append(field.storage)
|
|
|
|
reg_readable = True
|
|
|
|
else:
|
|
|
|
brs.append(Constant(0, BV(field.size)))
|
|
|
|
if reg_readable:
|
|
|
|
if len(brs) > 1:
|
|
|
|
brcases.append([Constant(i, BV(nbits)), self.interface.d_o.eq(f.Cat(*brs))])
|
|
|
|
else:
|
|
|
|
brcases.append([Constant(i, BV(nbits)), self.interface.d_o.eq(brs[0])])
|
|
|
|
else:
|
2012-02-06 07:55:50 -05:00
|
|
|
raise TypeError
|
2011-12-05 11:43:56 -05:00
|
|
|
if brcases:
|
2011-12-17 09:54:49 -05:00
|
|
|
sync.append(self.interface.d_o.eq(Constant(0, BV(8))))
|
2011-12-18 15:47:48 -05:00
|
|
|
sync.append(If(sel, Case(self.interface.a_i[:nbits], *brcases)))
|
2011-12-05 11:43:56 -05:00
|
|
|
else:
|
2011-12-17 09:54:49 -05:00
|
|
|
comb.append(self.interface.d_o.eq(Constant(0, BV(8))))
|
2011-12-05 11:43:56 -05:00
|
|
|
|
|
|
|
# Device access
|
|
|
|
for reg in self.description:
|
2012-02-06 07:55:50 -05:00
|
|
|
if isinstance(reg, RegisterFields):
|
2011-12-17 18:28:04 -05:00
|
|
|
for field in reg.fields:
|
|
|
|
if field.access_dev == READ_ONLY or field.access_dev == READ_WRITE:
|
2012-02-06 07:55:50 -05:00
|
|
|
comb.append(field.r.eq(field.storage))
|
2011-12-17 18:28:04 -05:00
|
|
|
if field.access_dev == WRITE_ONLY or field.access_dev == READ_WRITE:
|
2012-02-06 07:55:50 -05:00
|
|
|
sync.append(If(field.we, field.storage.eq(field.w)))
|
2011-12-05 11:43:56 -05:00
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
return Fragment(comb, sync)
|