Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import amaranth as amrth
- class Counter(amrth.Elaboratable):
- """
- A basic Counter
- Parameters
- ----------
- width : int
- Bit width of this Counter
- Attributes
- ----------
- en : Signal, input
- Enables the counter if ``en`` is asserted, ignores inputs otherwise.
- ovf: Signal, output
- Is asserted when the counter reaches its limit.
- """
- def __init__(self, width: int):
- self.width = width
- self._maxval = (2 ** self.width) - 1
- # Ports
- self.en = amrth.Signal()
- self.ovf = amrth.Signal()
- # Internal state
- self.count = amrth.Signal(self.width)
- def elaborate(self, platform):
- m = amrth.Module()
- m.d.comb += self.ovf.eq(self.count == self._maxval)
- with m.If(self.en):
- with m.If(self.ovf):
- m.d.sync += self.count.eq(0)
- with m.Else():
- m.d.sync += self.count.eq(self.count + 1)
- return m
Advertisement
Add Comment
Please, Sign In to add comment