Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Created August 23, 2022 21:13
Show Gist options
  • Save pepijndevos/5da164f09b621b3a02c81b507d86dfcd to your computer and use it in GitHub Desktop.
Save pepijndevos/5da164f09b621b3a02c81b507d86dfcd to your computer and use it in GitHub Desktop.
Amaranth in Hy
(import amaranth *)
(defn domain [domain #* expr] (domain.__iadd__ expr))
(defmacro mwhen [m condition #* body]
`(do
(with [(m.If ~condition)]
~body)))
(defmacro mif [m condition pos neg]
`(do
(with [(m.If ~condition)]
~pos)
(with [(m.Else)]
~neg)))
(defclass UpCounter [Elaboratable]
(defn __init__ [self limit]
(setv
self.limit limit
self.en (Signal)
self.ovf (Signal)
self.count (Signal 16)))
(defn elaborate [self platform]
(let [m (Module)]
(domain m.d.comb (self.ovf.eq (= self.count self.limit)))
(mwhen m self.en
(mif m self.ovf
(domain m.d.sync (self.count.eq 0))
(domain m.d.sync (self.count.eq (+ self.count 1)))))
m)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment