2013-11-02 11:03:47 -04:00
|
|
|
from migen.util.misc import xdir
|
2013-05-22 11:11:09 -04:00
|
|
|
from migen.fhdl.std import *
|
2012-02-06 11:39:32 -05:00
|
|
|
from migen.bank.description import *
|
2013-02-22 17:19:37 -05:00
|
|
|
from migen.genlib.misc import optree
|
2012-02-06 11:39:32 -05:00
|
|
|
|
2013-03-10 14:47:21 -04:00
|
|
|
class _EventSource(HUID):
|
2012-02-06 11:39:32 -05:00
|
|
|
def __init__(self):
|
2013-03-10 14:29:05 -04:00
|
|
|
HUID.__init__(self)
|
2013-05-08 12:12:26 -04:00
|
|
|
self.status = Signal() # value in the status register
|
|
|
|
self.pending = Signal() # value in the pending register + assert irq if unmasked
|
|
|
|
self.trigger = Signal() # trigger signal interface to the user design
|
|
|
|
self.clear = Signal() # clearing attempt by W1C to pending register, ignored by some event sources
|
2012-02-06 11:39:32 -05:00
|
|
|
|
2013-05-08 12:12:26 -04:00
|
|
|
# set on a positive trigger pulse
|
|
|
|
class EventSourcePulse(Module, _EventSource):
|
|
|
|
def __init__(self):
|
|
|
|
_EventSource.__init__(self)
|
|
|
|
self.comb += self.status.eq(0)
|
|
|
|
self.sync += [
|
|
|
|
If(self.clear, self.pending.eq(0)),
|
|
|
|
If(self.trigger, self.pending.eq(1))
|
|
|
|
]
|
|
|
|
|
|
|
|
# set on the falling edge of the trigger, status = trigger
|
|
|
|
class EventSourceProcess(Module, _EventSource):
|
|
|
|
def __init__(self):
|
|
|
|
_EventSource.__init__(self)
|
|
|
|
self.comb += self.status.eq(self.trigger)
|
|
|
|
old_trigger = Signal()
|
|
|
|
self.sync += [
|
|
|
|
If(self.clear, self.pending.eq(0)),
|
|
|
|
old_trigger.eq(self.trigger),
|
|
|
|
If(~self.trigger & old_trigger, self.pending.eq(1))
|
|
|
|
]
|
2012-02-06 11:39:32 -05:00
|
|
|
|
2013-05-08 12:12:26 -04:00
|
|
|
# all status set by external trigger
|
|
|
|
class EventSourceLevel(Module, _EventSource):
|
|
|
|
def __init__(self):
|
|
|
|
_EventSource.__init__(self)
|
|
|
|
self.comb += [
|
|
|
|
self.status.eq(self.trigger),
|
|
|
|
self.pending.eq(self.trigger)
|
|
|
|
]
|
2012-02-06 11:39:32 -05:00
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
class EventManager(Module, AutoCSR):
|
2013-03-10 14:29:05 -04:00
|
|
|
def __init__(self):
|
2012-02-06 11:39:32 -05:00
|
|
|
self.irq = Signal()
|
2013-03-10 14:29:05 -04:00
|
|
|
|
|
|
|
def do_finalize(self):
|
2013-11-02 11:03:47 -04:00
|
|
|
sources_u = [v for k, v in xdir(self, True) if isinstance(v, _EventSource)]
|
2013-03-10 14:29:05 -04:00
|
|
|
sources = sorted(sources_u, key=lambda x: x.huid)
|
|
|
|
n = len(sources)
|
2013-03-30 12:28:41 -04:00
|
|
|
self.status = CSR(n)
|
|
|
|
self.pending = CSR(n)
|
|
|
|
self.enable = CSRStorage(n)
|
2013-03-10 14:29:05 -04:00
|
|
|
|
|
|
|
for i, source in enumerate(sources):
|
2013-05-08 12:12:26 -04:00
|
|
|
self.comb += [
|
|
|
|
self.status.w[i].eq(source.status),
|
|
|
|
If(self.pending.re & self.pending.r[i], source.clear.eq(1)),
|
|
|
|
self.pending.w[i].eq(source.pending)
|
|
|
|
]
|
2012-02-06 11:39:32 -05:00
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
irqs = [self.pending.w[i] & self.enable.storage[i] for i in range(n)]
|
2013-03-10 14:29:05 -04:00
|
|
|
self.comb += self.irq.eq(optree("|", irqs))
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
object.__setattr__(self, name, value)
|
2013-05-08 12:12:26 -04:00
|
|
|
if isinstance(value, _EventSource):
|
|
|
|
if self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
self.submodules += value
|