learn-fpga-amaranth icon indicating copy to clipboard operation
learn-fpga-amaranth copied to clipboard

adding clockwork to top module gives error

Open chrisjohgorman opened this issue 1 year ago • 9 comments

Hello @bl0x,

First off let me thank you for your project. It is interesting and I am going to try and follow it to gain an understanding of amaranth.

I am trying to build with Vivado and have had success with the first step. I now have lighted leds on my cmod a7. When trying to build step 2, I get the following error.

step = 2
Traceback (most recent call last):
  File "/home/chris/src/amaranth/learn-fpga-amaranth/boards/digilent_cmod_a7.py", line 25, in <module>
    platform.build(Top(leds, uart), do_program=True)
  File "/usr/lib/python3.12/site-packages/amaranth/build/plat.py", line 99, in build
    plan = self.prepare(elaboratable, name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/amaranth/build/plat.py", line 140, in prepare
    fragment._propagate_domains(self.create_missing_domain, platform=self)
  File "/usr/lib/python3.12/site-packages/amaranth/hdl/_ir.py", line 240, in _propagate_domains
    self._propagate_domains_up()
  File "/usr/lib/python3.12/site-packages/amaranth/hdl/_ir.py", line 141, in _propagate_domains_up
    subfrag._propagate_domains_up(hierarchy + (hier_name,))
  File "/usr/lib/python3.12/site-packages/amaranth/hdl/_ir.py", line 184, in _propagate_domains_up
    self.add_domains(domain)
  File "/usr/lib/python3.12/site-packages/amaranth/hdl/_ir.py", line 91, in add_domains
    assert domain.name not in self.domains
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Systematically looking at the commands makes the following code from 02_slower_blinky/soc.py responsible as commenting it out starts the build for me.

# Add the clockwork to the top module. If this is not done,
# the logic will not be instantiated.
m.submodules.cw = cw

Do you have any idea on how to set the domain.name in self.domains?

chrisjohgorman avatar Oct 04 '24 21:10 chrisjohgorman

hello again,

Just to update you, I have circumvented this with IMHO a horrible hack to _ir.py. The hack seems to work and I can now process step 2.

The following patch is the one I used to get this to work...

--- /home/chris/install/python-amaranth/src/amaranth/amaranth/hdl/_ir.py       2024-10-04 16:12:59.534701199 -0400
+++ /usr/lib/python3.12/site-packages/amaranth/hdl/_ir.py       2024-10-05 11:00:50.106393002 -0400
@@ -86,9 +86,12 @@
         self.domain_renames = {}
 
     def add_domains(self, *domains):
+        count=0
         for domain in flatten(domains):
             assert isinstance(domain, _cd.ClockDomain)
-            assert domain.name not in self.domains
+            if (count != 0):
+                assert domain.name not in self.domains
+            count=count+1
             self.domains[domain.name] = domain
 
     def iter_domains(self):

Apologies for the spam on a weekend.

chrisjohgorman avatar Oct 05 '24 15:10 chrisjohgorman

I've decided to move this to the amaranth github issues page. I will report back on a fix if I get one.

Chris

chrisjohgorman avatar Oct 05 '24 16:10 chrisjohgorman

I have a "fix" for this problem for me. I wonder if there is a typo in clockworks.py. When I change the assignment

module.domains += ClockDomain(clockworks_domain_name)

to

module.domains = ClockDomain(clockworks_domain_name)

this fixes the build problem for me.

chrisjohgorman avatar Oct 07 '24 16:10 chrisjohgorman

I just got a note from @whitequark saying that my solution was not proper, so please disregard it. I will post something if I find a better solution.

chrisjohgorman avatar Oct 07 '24 17:10 chrisjohgorman

Hello Chris - I tried for the first time this repo (commit [4084aff] ) with amaranth 0.5.6 nd clockworks now has the initial issue you reported.

I had to change clockworks.py (basically now using the "proper" way , so amaranth developer(s) fixed the core issue.

Change from: module.domains += ClockDomain(clockworks_domain_name) To: module.domains = ClockDomain(clockworks_domain_name)

I tried the first 3 steps in the tutorial and so far so good. It would've taken me a long time to find a solution without your post so thank you!

timoksn avatar May 25 '25 19:05 timoksn

Hello timoksn,

I was warned by Catherine (whitequark) from amaranth that my change from

module.domains += ClockDomain(clockworks_domain_name) to

module.domains = ClockDomain(clockworks_domain_name)

should NOT be done and set some internal variables incorrectly. So I do not recommend following my lead on this. What I did end up with was something a little different. I've attached a tar ball of my git repository if you want a template. Perhaps you can do better than I did. :) It should work with amaranth 0.6.70 or so. I got it working to 19_verilator, which I couldn't decipher.

Hope this helps.

Chris

On Sun, May 25, 2025 at 3:58 PM timoksn @.***> wrote:

timoksn left a comment (bl0x/learn-fpga-amaranth#8) https://github.com/bl0x/learn-fpga-amaranth/issues/8#issuecomment-2908048920

Hello Chris - I tried for the first time this repo (commit [4084aff https://github.com/bl0x/learn-fpga-amaranth/commit/4084affd5d0490988aacdd105686945cf0e388c1] ) with amaranth 0.5.6 nd clockworks now has the initial issue you reported.

I had to change clockworks.py (basically now using the "proper" way , so amaranth developer(s) fixed the core issue.

Change from: module.domains += ClockDomain(clockworks_domain_name) To: module.domains = ClockDomain(clockworks_domain_name)

I tried the first 3 steps in the tutorial and so far so good. It would've taken me a long time to find a solution without your post so thank you!

— Reply to this email directly, view it on GitHub https://github.com/bl0x/learn-fpga-amaranth/issues/8#issuecomment-2908048920, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG76X4ZKKZO7SBKLKPDYHYL3AIOHRAVCNFSM6AAAAABPMTLXD6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMBYGA2DQOJSGA . You are receiving this because you authored the thread.Message ID: @.***>

chrisjohgorman avatar May 25 '25 20:05 chrisjohgorman

Thank you. I completely misread what NOT to do :) Do you have the changes on your main branch ?

timoksn avatar May 25 '25 20:05 timoksn

Sorry for not thinking of it, my changes, if I recall correctly, are on the branch local-changes. I think I left the main branch alone.

Chris

On Sun, May 25, 2025 at 4:51 PM timoksn @.***> wrote:

timoksn left a comment (bl0x/learn-fpga-amaranth#8) https://github.com/bl0x/learn-fpga-amaranth/issues/8#issuecomment-2908073430

Thank you. I completely misread what NOT to do :) Do you have the changes on your main branch ?

— Reply to this email directly, view it on GitHub https://github.com/bl0x/learn-fpga-amaranth/issues/8#issuecomment-2908073430, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG76X464YX2LUT2HRTGBXGL3AIULTAVCNFSM6AAAAABPMTLXD6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMBYGA3TGNBTGA . You are receiving this because you authored the thread.Message ID: @.***>

chrisjohgorman avatar May 25 '25 20:05 chrisjohgorman

Thank You! Regardless you saved me time.

timoksn avatar May 25 '25 20:05 timoksn