Guest User

counter_module

a guest
Jul 5th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import amaranth as amrth
  2.  
  3. class Counter(amrth.Elaboratable):
  4. """
  5. A basic Counter
  6.  
  7. Parameters
  8. ----------
  9. width : int
  10. Bit width of this Counter
  11.  
  12. Attributes
  13. ----------
  14. en : Signal, input
  15. Enables the counter if ``en`` is asserted, ignores inputs otherwise.
  16. ovf: Signal, output
  17. Is asserted when the counter reaches its limit.
  18. """
  19.  
  20. def __init__(self, width: int):
  21. self.width = width
  22. self._maxval = (2 ** self.width) - 1
  23.  
  24. # Ports
  25. self.en = amrth.Signal()
  26. self.ovf = amrth.Signal()
  27.  
  28. # Internal state
  29. self.count = amrth.Signal(self.width)
  30.  
  31. def elaborate(self, platform):
  32. m = amrth.Module()
  33.  
  34. m.d.comb += self.ovf.eq(self.count == self._maxval)
  35.  
  36. with m.If(self.en):
  37. with m.If(self.ovf):
  38. m.d.sync += self.count.eq(0)
  39. with m.Else():
  40. m.d.sync += self.count.eq(self.count + 1)
  41.  
  42. return m
Advertisement
Add Comment
Please, Sign In to add comment