litex/milkymist/timer/__init__.py

35 lines
844 B
Python
Raw Normal View History

2012-05-21 13:46:04 -04:00
from migen.fhdl.structure import *
from migen.fhdl.module import Module
2012-05-21 13:46:04 -04:00
from migen.bank.description import *
from migen.bank.eventmanager import *
2013-03-30 12:28:15 -04:00
class Timer(Module, AutoCSR):
def __init__(self, width=32):
2013-05-13 11:18:30 -04:00
self._load = CSRStorage(width)
2013-03-30 12:28:15 -04:00
self._reload = CSRStorage(width)
2013-05-13 11:18:30 -04:00
self._en = CSRStorage()
self._update_value = CSR()
self._value = CSRStatus(width)
2012-05-21 13:46:04 -04:00
self.submodules.ev = EventManager()
self.ev.zero = EventSourceProcess()
self.ev.finalize()
###
2012-05-21 13:46:04 -04:00
2013-05-13 11:18:30 -04:00
value = Signal(width)
self.sync += [
If(self._en.storage,
If(value == 0,
# set reload to 0 to disable reloading
value.eq(self._reload.storage)
).Else(
value.eq(value - 1)
)
2012-05-21 13:46:04 -04:00
).Else(
2013-05-13 11:18:30 -04:00
value.eq(self._load.storage)
2012-05-21 13:46:04 -04:00
),
2013-05-13 11:18:30 -04:00
If(self._update_value.re, self._value.status.eq(value))
2012-05-21 13:46:04 -04:00
]
2013-05-13 11:18:30 -04:00
self.comb += self.ev.zero.trigger.eq(value != 0)