2011-12-16 15:30:14 -05:00
|
|
|
from migen.fhdl.structure import *
|
2012-08-26 15:19:34 -04:00
|
|
|
from migen.bus import csr
|
2011-12-16 10:02:55 -05:00
|
|
|
from migen.bank.description import *
|
2011-12-05 11:43:56 -05:00
|
|
|
|
|
|
|
class Bank:
|
2012-12-06 11:28:28 -05:00
|
|
|
def __init__(self, description, address=0, bus=None):
|
2011-12-05 11:43:56 -05:00
|
|
|
self.description = description
|
|
|
|
self.address = address
|
2012-12-06 11:28:28 -05:00
|
|
|
if bus is None:
|
|
|
|
bus = csr.Interface()
|
2012-12-06 11:15:34 -05:00
|
|
|
self.bus = bus
|
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-18 15:47:48 -05:00
|
|
|
sel = Signal()
|
2012-12-06 11:15:34 -05:00
|
|
|
comb.append(sel.eq(self.bus.adr[9:] == self.address))
|
2011-12-05 11:43:56 -05:00
|
|
|
|
2012-08-26 15:19:34 -04:00
|
|
|
desc_exp = expand_description(self.description, csr.data_width)
|
2012-02-06 10:15:27 -05:00
|
|
|
nbits = bits_for(len(desc_exp)-1)
|
2011-12-05 11:43:56 -05:00
|
|
|
|
|
|
|
# Bus writes
|
2012-11-28 19:11:15 -05:00
|
|
|
bwcases = {}
|
2012-02-06 10:15:27 -05:00
|
|
|
for i, reg in enumerate(desc_exp):
|
2012-02-06 07:55:50 -05:00
|
|
|
if isinstance(reg, RegisterRaw):
|
2012-12-06 11:15:34 -05:00
|
|
|
comb.append(reg.r.eq(self.bus.dat_w[:reg.size]))
|
2012-02-06 07:55:50 -05:00
|
|
|
comb.append(reg.re.eq(sel & \
|
2012-12-06 11:15:34 -05:00
|
|
|
self.bus.we & \
|
|
|
|
(self.bus.adr[:nbits] == i)))
|
2012-02-06 07:55:50 -05:00
|
|
|
elif isinstance(reg, RegisterFields):
|
2012-11-28 19:11:15 -05:00
|
|
|
bwra = []
|
2012-02-06 10:15:27 -05:00
|
|
|
offset = 0
|
|
|
|
for field in reg.fields:
|
2011-12-17 18:28:04 -05:00
|
|
|
if field.access_bus == WRITE_ONLY or field.access_bus == READ_WRITE:
|
2012-12-06 11:15:34 -05:00
|
|
|
bwra.append(field.storage.eq(self.bus.dat_w[offset:offset+field.size]))
|
2012-02-06 10:15:27 -05:00
|
|
|
offset += field.size
|
2012-11-28 19:11:15 -05:00
|
|
|
if bwra:
|
|
|
|
bwcases[i] = bwra
|
2012-10-08 12:43:18 -04:00
|
|
|
# commit atomic writes
|
|
|
|
for field in reg.fields:
|
|
|
|
if isinstance(field, FieldAlias) and field.commit_list:
|
|
|
|
commit_instr = [hf.commit_to.eq(hf.storage) for hf in field.commit_list]
|
2012-12-06 11:15:34 -05:00
|
|
|
sync.append(If(sel & self.bus.we & self.bus.adr[:nbits] == i, *commit_instr))
|
2011-12-17 18:28:04 -05:00
|
|
|
else:
|
2012-02-06 07:55:50 -05:00
|
|
|
raise TypeError
|
2011-12-05 11:43:56 -05:00
|
|
|
if bwcases:
|
2012-12-06 11:15:34 -05:00
|
|
|
sync.append(If(sel & self.bus.we, Case(self.bus.adr[:nbits], bwcases)))
|
2011-12-05 11:43:56 -05:00
|
|
|
|
|
|
|
# Bus reads
|
2012-11-28 19:11:15 -05:00
|
|
|
brcases = {}
|
2012-02-06 10:15:27 -05:00
|
|
|
for i, reg in enumerate(desc_exp):
|
2012-02-06 07:55:50 -05:00
|
|
|
if isinstance(reg, RegisterRaw):
|
2012-12-06 11:15:34 -05:00
|
|
|
brcases[i] = [self.bus.dat_r.eq(reg.w)]
|
2012-02-06 07:55:50 -05:00
|
|
|
elif isinstance(reg, RegisterFields):
|
2011-12-17 18:28:04 -05:00
|
|
|
brs = []
|
|
|
|
reg_readable = False
|
2012-02-06 10:15:27 -05:00
|
|
|
for field in 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:
|
2012-11-28 17:18:43 -05:00
|
|
|
brs.append(Replicate(0, field.size))
|
2011-12-17 18:28:04 -05:00
|
|
|
if reg_readable:
|
2012-12-06 11:15:34 -05:00
|
|
|
brcases[i] = [self.bus.dat_r.eq(Cat(*brs))]
|
2011-12-17 18:28:04 -05:00
|
|
|
else:
|
2012-02-06 07:55:50 -05:00
|
|
|
raise TypeError
|
2011-12-05 11:43:56 -05:00
|
|
|
if brcases:
|
2012-12-06 11:15:34 -05:00
|
|
|
sync.append(self.bus.dat_r.eq(0))
|
|
|
|
sync.append(If(sel, Case(self.bus.adr[:nbits], brcases)))
|
2011-12-05 11:43:56 -05:00
|
|
|
else:
|
2012-12-06 11:15:34 -05:00
|
|
|
comb.append(self.bus.dat_r.eq(0))
|
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:
|
2012-02-15 12:23:31 -05:00
|
|
|
if field.access_bus == READ_ONLY and field.access_dev == WRITE_ONLY:
|
|
|
|
comb.append(field.storage.eq(field.w))
|
|
|
|
else:
|
|
|
|
if field.access_dev == READ_ONLY or field.access_dev == READ_WRITE:
|
|
|
|
comb.append(field.r.eq(field.storage))
|
|
|
|
if field.access_dev == WRITE_ONLY or field.access_dev == READ_WRITE:
|
|
|
|
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)
|
2013-03-09 18:45:16 -05:00
|
|
|
|
|
|
|
# address_map(name, memory) returns the CSR offset at which to map
|
|
|
|
# the CSR object (register bank or memory).
|
|
|
|
# If memory=None, the object is the register bank of object source.name.
|
|
|
|
# Otherwise, it is a memory object belonging to source.name.
|
|
|
|
# address_map is called exactly once for each object at each call to
|
|
|
|
# scan(), so it can have side effects.
|
|
|
|
class BankArray:
|
|
|
|
def __init__(self, source, address_map):
|
|
|
|
self.source = source
|
|
|
|
self.address_map = address_map
|
|
|
|
self.scan()
|
|
|
|
|
|
|
|
def scan(self):
|
|
|
|
self.banks = []
|
|
|
|
self.srams = []
|
|
|
|
for name, obj in self.source.__dict__.items():
|
|
|
|
if hasattr(obj, "get_registers"):
|
|
|
|
registers = obj.get_registers()
|
|
|
|
else:
|
|
|
|
registers = []
|
|
|
|
if hasattr(obj, "get_memories"):
|
|
|
|
memories = obj.get_memories()
|
|
|
|
for memory in memories:
|
|
|
|
mapaddr = self.address_map(name, memory)
|
|
|
|
mmap = csr.SRAM(memory, mapaddr)
|
|
|
|
registers += mmap.get_registers()
|
|
|
|
self.srams.append(mmap)
|
|
|
|
if registers:
|
|
|
|
mapaddr = self.address_map(name, None)
|
|
|
|
rmap = Bank(registers, mapaddr)
|
|
|
|
self.banks.append(rmap)
|
|
|
|
|
|
|
|
def get_banks(self):
|
|
|
|
return self.banks
|
|
|
|
|
|
|
|
def get_srams(self):
|
|
|
|
return self.srams
|
|
|
|
|
|
|
|
def get_buses(self):
|
|
|
|
return [i.bus for i in self.banks + self.srams]
|
|
|
|
|
|
|
|
def get_fragment(self):
|
|
|
|
return sum([i.get_fragment() for i in self.banks + self.srams], Fragment())
|