litex/migen/bus/csr.py

102 lines
2.5 KiB
Python
Raw Normal View History

2011-12-16 15:30:14 -05:00
from migen.fhdl.structure import *
2013-02-22 11:56:35 -05:00
from migen.fhdl.specials import Memory
from migen.bus.simple import *
2012-07-07 16:36:15 -04:00
from migen.bus.transactions import *
from migen.sim.generic import PureSimulable
2012-12-06 11:16:17 -05:00
from migen.bank.description import RegisterField
2011-12-04 18:16:44 -05:00
2012-08-26 15:19:34 -04:00
data_width = 8
2011-12-08 12:47:32 -05:00
class Interface(SimpleInterface):
def __init__(self):
2012-12-18 08:54:33 -05:00
SimpleInterface.__init__(self, Description(
2012-08-26 15:19:34 -04:00
(M_TO_S, "adr", 14),
(M_TO_S, "we", 1),
(M_TO_S, "dat_w", data_width),
(S_TO_M, "dat_r", data_width)))
2011-12-04 18:16:44 -05:00
class Interconnect(SimpleInterconnect):
pass
2012-07-07 16:36:15 -04:00
class Initiator(PureSimulable):
def __init__(self, generator, bus=None):
2012-07-07 16:36:15 -04:00
self.generator = generator
if bus is None:
bus = Interface()
self.bus = bus
2012-07-07 16:36:15 -04:00
self.transaction = None
self.done = False
def do_simulation(self, s):
if not self.done:
if self.transaction is not None:
if isinstance(self.transaction, TRead):
self.transaction.data = s.rd(self.bus.dat_r)
else:
s.wr(self.bus.we, 0)
try:
self.transaction = next(self.generator)
except StopIteration:
self.transaction = None
self.done = True
if self.transaction is not None:
s.wr(self.bus.adr, self.transaction.address)
if isinstance(self.transaction, TWrite):
s.wr(self.bus.we, 1)
s.wr(self.bus.dat_w, self.transaction.data)
2012-12-06 11:16:17 -05:00
def _compute_page_bits(nwords):
npages = (nwords - 1)//512
if npages > 0:
return bits_for(npages-1)
else:
return 0
class SRAM:
def __init__(self, mem_or_size, address, bus=None):
2012-12-06 11:16:17 -05:00
if isinstance(mem_or_size, Memory):
assert(mem_or_size.width <= data_width)
self.mem = mem_or_size
else:
self.mem = Memory(data_width, mem_or_size//(data_width//8))
self.address = address
page_bits = _compute_page_bits(self.mem.depth)
if page_bits:
self._page = RegisterField(self.mem.name_override + "_page", page_bits)
2012-12-06 11:16:17 -05:00
else:
self._page = None
if bus is None:
bus = Interface()
2012-12-06 11:16:17 -05:00
self.bus = bus
def get_registers(self):
if self._page is None:
return []
else:
return [self._page]
def get_fragment(self):
port = self.mem.get_port(write_capable=True)
sel = Signal()
sel_r = Signal()
sync = [sel_r.eq(sel)]
comb = [
sel.eq(self.bus.adr[9:] == self.address),
port.we.eq(sel & self.bus.we),
port.dat_w.eq(self.bus.dat_w),
If(sel_r,
self.bus.dat_r.eq(port.dat_r)
)
]
if self._page is None:
comb.append(port.adr.eq(self.bus.adr[:len(port.adr)]))
else:
pv = self._page.field.r
comb.append(port.adr.eq(Cat(self.bus.adr[:len(port.adr)-len(pv)], pv)))
2013-02-22 11:56:35 -05:00
return Fragment(comb, sync, specials={self.mem})