-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add TrueSettle #552
Open
lekcyjna123
wants to merge
4
commits into
kuznia-rdzeni:master
Choose a base branch
from
lekcyjna123:lekcyjna/add-true-settle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add TrueSettle #552
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import random | ||
import unittest | ||
import functools | ||
from enum import Enum | ||
from contextlib import contextmanager, nullcontext | ||
from typing import TypeVar, Generic, Type, TypeGuard, Any, Union, Callable, cast, TypeAlias | ||
from abc import ABC | ||
|
@@ -109,29 +110,59 @@ class Now(CoreblocksCommand): | |
pass | ||
|
||
|
||
class TrueSettle(CoreblocksCommand): | ||
"""Wait till all process are waiting for the next cycle or for the TrueSettle""" | ||
|
||
pass | ||
|
||
|
||
class SyncProcessState(Enum): | ||
"""State of SyncProcessWrapper.""" | ||
|
||
sleeping = 0 # Wait for the next cycle | ||
running = 1 | ||
ended = 2 | ||
true_settle = 3 # Wait for the TrueSettle | ||
|
||
|
||
class SyncProcessWrapper: | ||
def __init__(self, f): | ||
self.org_process = f | ||
self.current_cycle = 0 | ||
self.state = None | ||
self.blocked = None | ||
|
||
def _wrapping_function(self): | ||
response = None | ||
org_coroutine = self.org_process() | ||
self.state = SyncProcessState.running | ||
try: | ||
while True: | ||
# call orginal test process and catch data yielded by it in `command` variable | ||
command = org_coroutine.send(response) | ||
# If process wait for new cycle | ||
if command is None: | ||
self.current_cycle += 1 | ||
self.state = SyncProcessState.sleeping | ||
# forward to amaranth | ||
yield | ||
elif isinstance(command, Now): | ||
response = self.current_cycle | ||
self.state = SyncProcessState.running | ||
elif isinstance(command, CoreblocksCommand): | ||
if isinstance(command, Now): | ||
response = self.current_cycle | ||
elif isinstance(command, TrueSettle): | ||
self.state = SyncProcessState.true_settle | ||
self.blocked = True | ||
while self.blocked: | ||
yield Settle() | ||
self.state = SyncProcessState.running | ||
else: | ||
raise RuntimeError(f"Not known CoreblocksCommand: {command}") | ||
# Pass everything else to amaranth simulator without modifications | ||
else: | ||
response = yield command | ||
except StopIteration: | ||
self.state = SyncProcessState.ended | ||
pass | ||
|
||
|
||
|
@@ -168,15 +199,30 @@ def __init__(self, module: HasElaborate, max_cycles: float = 10e4, add_transacti | |
self.ctx = nullcontext() | ||
|
||
self.deadline = clk_period * max_cycles | ||
self.sync_proc_list = [] | ||
|
||
def add_sync_process(self, f: Callable[[], TestGen]): | ||
f_wrapped = SyncProcessWrapper(f) | ||
self.sync_proc_list.append(f_wrapped) | ||
super().add_sync_process(f_wrapped._wrapping_function) | ||
|
||
def run(self) -> bool: | ||
with self.ctx: | ||
self.run_until(self.deadline) | ||
def _check_true_settle_ready(self): | ||
return all(p.state != SyncProcessState.running for p in self.sync_proc_list) | ||
|
||
def _unblock_sync_processes(self): | ||
for p in self.sync_proc_list: | ||
if p.blocked: | ||
p.blocked = False | ||
|
||
def run(self) -> bool: | ||
deadline = self.deadline * 1e12 | ||
assert self._engine.now <= deadline | ||
last_now = self._engine.now | ||
while self.advance() and self._engine.now < deadline: | ||
if last_now == self._engine.now: | ||
if self._check_true_settle_ready(): | ||
self._unblock_sync_processes() | ||
last_now = self._engine.now | ||
Comment on lines
+217
to
+225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unfortunate that you had to dig into Amaranth's internals this much. But still, this gives us the functionality we need. |
||
return not self.advance() | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't these kinds of tests prone to race condition?