Created
August 23, 2022 21:13
-
-
Save pepijndevos/5da164f09b621b3a02c81b507d86dfcd to your computer and use it in GitHub Desktop.
Amaranth in Hy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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