Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sim: Fix clock phase in add_clock having to be specified in ps. #679

Merged
merged 1 commit into from Feb 4, 2022

Conversation

bl0x
Copy link
Contributor

@bl0x bl0x commented Jan 30, 2022

Now more in line with the documentation, which states that the process will wait phase seconds.

Ref:

phase : None or float
Clock phase. The process will wait ``phase`` seconds before the first clock transition.
If not specified, defaults to ``period / 2``.

This was not true so far.
One had to give the clock phase wait time in ps.

@codecov-commenter
Copy link

codecov-commenter commented Jan 30, 2022

Codecov Report

Merging #679 (e7233ff) into main (c83b51d) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #679      +/-   ##
==========================================
+ Coverage   81.32%   81.34%   +0.01%     
==========================================
  Files          49       49              
  Lines        6464     6465       +1     
  Branches     1526     1526              
==========================================
+ Hits         5257     5259       +2     
  Misses       1015     1015              
+ Partials      192      191       -1     
Impacted Files Coverage Δ
amaranth/sim/core.py 84.84% <100.00%> (+1.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c83b51d...e7233ff. Read the comment docs.

@whitequark
Copy link
Member

cc @modwizcode

@bl0x
Copy link
Contributor Author

bl0x commented Jan 30, 2022

Illustrative example:

from amaranth import *
from amaranth.sim import *

phase0_fail = Signal()
phase90_fail = Signal()
phase180_fail = Signal()
phase270_fail = Signal()
phase0_correct = Signal()
phase90_correct = Signal()
phase180_correct = Signal()
phase270_correct = Signal()

m = Module()
m.domains += ClockDomain("phase0_fail")
m.domains += ClockDomain("phase90_fail")
m.domains += ClockDomain("phase180_fail")
m.domains += ClockDomain("phase270_fail")
m.domains += ClockDomain("phase0_correct")
m.domains += ClockDomain("phase90_correct")
m.domains += ClockDomain("phase180_correct")
m.domains += ClockDomain("phase270_correct")

m.d.comb += [
        phase0_fail.eq(ClockSignal("phase0_fail")),
        phase90_fail.eq(ClockSignal("phase90_fail")),
        phase180_fail.eq(ClockSignal("phase180_fail")),
        phase270_fail.eq(ClockSignal("phase270_fail")),
        phase0_correct.eq(ClockSignal("phase0_correct")),
        phase90_correct.eq(ClockSignal("phase90_correct")),
        phase180_correct.eq(ClockSignal("phase180_correct")),
        phase270_correct.eq(ClockSignal("phase270_correct"))
        ]

sim = Simulator(m)
p = 1

# This is what is desired (?)
sim.add_clock(p, phase=0*p/4, domain="phase0_fail")
sim.add_clock(p, phase=1*p/4, domain="phase90_fail")
sim.add_clock(p, phase=2*p/4, domain="phase180_fail")
sim.add_clock(p, phase=3*p/4, domain="phase270_fail")

# This is what is needed (!)
sim.add_clock(p, phase=1e12 * (p/2 + 0*p/8), domain="phase0_correct")
sim.add_clock(p, phase=1e12 * (p/2 + 1*p/8), domain="phase90_correct")
sim.add_clock(p, phase=1e12 * (p/2 + 2*p/8), domain="phase180_correct")
sim.add_clock(p, phase=1e12 * (p/2 + 3*p/8), domain="phase270_correct")

def proc():
    for i in range(5):
        yield

sim.add_sync_process(proc, domain="phase0_fail")
with sim.write_vcd("bug_clock_phase.vcd"):
    sim.run_until(5)

@Lunaphied
Copy link
Contributor

Thanks! Can you add this as an actual testcase as part of the PR for regression purposes?

@bl0x
Copy link
Contributor Author

bl0x commented Jan 30, 2022

Where would that go? tests/test_sim.py?

@Lunaphied
Copy link
Contributor

Yes; that's a good place to put it.

@bl0x
Copy link
Contributor Author

bl0x commented Jan 30, 2022

On it.

@bl0x
Copy link
Contributor Author

bl0x commented Jan 30, 2022

That should do it.

Copy link
Contributor

@Lunaphied Lunaphied left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the suggested changes.

tests/test_sim.py Outdated Show resolved Hide resolved
tests/test_sim.py Outdated Show resolved Hide resolved
tests/test_sim.py Outdated Show resolved Hide resolved
tests/test_sim.py Outdated Show resolved Hide resolved
tests/test_sim.py Outdated Show resolved Hide resolved
@bl0x
Copy link
Contributor Author

bl0x commented Feb 1, 2022

@modwizcode Thanks for the review

Copy link
Contributor

@Lunaphied Lunaphied left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was a bit of a misunderstanding, I clarified the style I intended, sorry about that.

tests/test_sim.py Outdated Show resolved Hide resolved
tests/test_sim.py Outdated Show resolved Hide resolved
Copy link
Member

@whitequark whitequark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@whitequark whitequark merged commit 02364a4 into amaranth-lang:main Feb 4, 2022
@whitequark
Copy link
Member

Thanks @bl0x and @modwizcode!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants