Skip to content

Commit

Permalink
Fix comments from review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekcyjna committed Oct 28, 2023
1 parent 44f7423 commit b54aeb3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion test/transactions/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def __init__(self, n=2, ready_function=lambda arg: arg.data != 3):
def elaborate(self, platform):
m = TModule()

@def_method(m, self.method, self.ready, ready_function=self.ready_function)
@def_method(m, self.method, self.ready, validate_arguments=self.ready_function)
def _(data):
m.d.comb += self.out_m.eq(1)

Expand Down
43 changes: 24 additions & 19 deletions transactron/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def rec(transaction: Transaction, source: TransactionBase):
raise RuntimeError(f"Method '{method.name}' can't be called twice from the same transaction")
self.methods_by_transaction[transaction].append(method)
self.transactions_by_method[method].append(transaction)
self.readiness_by_method_and_transaction[(transaction, method)] = method._ready_function(arg_rec)
self.readiness_by_method_and_transaction[(transaction, method)] = method._validate_arguments(arg_rec)
rec(transaction, method)

for transaction in transactions:
Expand Down Expand Up @@ -999,7 +999,7 @@ def __init__(
self.data_out = Record(o)
self.nonexclusive = nonexclusive
self.single_caller = single_caller
self.ready_function: Optional[Callable[[Record], ValueLike]] = None
self.validate_arguments: Optional[Callable[..., ValueLike]] = None
if nonexclusive:
assert len(self.data_in) == 0

Expand Down Expand Up @@ -1049,7 +1049,7 @@ def body(
*,
ready: ValueLike = C(1),
out: ValueLike = C(0, 0),
ready_function: Optional[Callable[[Record], ValueLike]] = None,
validate_arguments: Optional[Callable[..., ValueLike]] = None,
) -> Iterator[Record]:
"""Define method body
Expand All @@ -1073,11 +1073,12 @@ def body(
Data generated by the `Method`, which will be passed to
the caller (a `Transaction` or another `Method`). Assigned
combinationally to the `data_out` attribute.
ready_function: Optional[Callable[[Record], ValueLike]]
Function to instantiate a combinational circuit for each
method caller. It should take input arguments and return
if the method can be called with those arguments. By default
there is no function, so all arguments are accepted.
validate_arguments: Optional[Callable[..., ValueLike]]
Function that takes input arguments used to call the method
and checks whether the method can be called with those arguments.
It instantiates a combinational circuit for each
method caller. By default, there is no function, so all arguments
are accepted.
Returns
-------
Expand All @@ -1099,7 +1100,7 @@ def body(
if self.defined:
raise RuntimeError(f"Method '{self.name}' already defined")
self.def_order = next(TransactionBase.def_counter)
self.ready_function = ready_function
self.validate_arguments = validate_arguments

try:
m.d.av_comb += self.ready.eq(ready)
Expand All @@ -1110,9 +1111,9 @@ def body(
finally:
self.defined = True

def _ready_function(self, arg_rec: Record) -> ValueLike:
if self.ready_function is not None:
return self.ready & method_def_helper(self, self.ready_function, arg_rec, **arg_rec.fields)
def _validate_arguments(self, arg_rec: Record) -> ValueLike:
if self.validate_arguments is not None:
return self.ready & method_def_helper(self, self.validate_arguments, arg_rec, **arg_rec.fields)
return self.ready

def __call__(
Expand Down Expand Up @@ -1187,7 +1188,10 @@ def debug_signals(self) -> SignalBundle:


def def_method(
m: TModule, method: Method, ready: ValueLike = C(1), ready_function: Optional[Callable[[Record], ValueLike]] = None
m: TModule,
method: Method,
ready: ValueLike = C(1),
validate_arguments: Optional[Callable[..., ValueLike]] = None,
):
"""Define a method.
Expand All @@ -1213,11 +1217,12 @@ def def_method(
Signal to indicate if the method is ready to be run. By
default it is `Const(1)`, so the method is always ready.
Assigned combinationally to the `ready` attribute.
ready_function: Optional[Callable[[Record], ValueLike]]
Function to instantiate a combinational circuit for each
method caller. It should take input arguments and return
if the method can be called with those arguments. By default
there is no function, so all arguments are accepted.
validate_arguments: Optional[Callable[..., ValueLike]]
Function that takes input arguments used to call the method
and checks whether the method can be called with those arguments.
It instantiates a combinational circuit for each
method caller. By default, there is no function, so all arguments
are accepted.
Examples
--------
Expand Down Expand Up @@ -1253,7 +1258,7 @@ def decorator(func: Callable[..., Optional[RecordDict]]):
out = Record.like(method.data_out)
ret_out = None

with method.body(m, ready=ready, out=out, ready_function=ready_function) as arg:
with method.body(m, ready=ready, out=out, validate_arguments=validate_arguments) as arg:
ret_out = method_def_helper(method, func, arg, **arg.fields)

if ret_out is not None:
Expand Down

0 comments on commit b54aeb3

Please sign in to comment.