52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import re
|
|
from typing import TYPE_CHECKING, Match
|
|
|
|
from .identifier_filter import kw_filter as kwf
|
|
|
|
if TYPE_CHECKING:
|
|
from systemrdl.node import Node, SignalNode
|
|
from typing import Optional
|
|
from .dereferencer import Dereferencer
|
|
|
|
def get_indexed_path(top_node: 'Node', target_node: 'Node') -> str:
|
|
"""
|
|
TODO: Add words about indexing and why i'm doing this. Copy from logbook
|
|
"""
|
|
path = target_node.get_rel_path(top_node, empty_array_suffix="[!]")
|
|
|
|
# replace unknown indexes with incrementing iterators i0, i1, ...
|
|
class ReplaceUnknown:
|
|
def __init__(self) -> None:
|
|
self.i = 0
|
|
def __call__(self, match: Match) -> str:
|
|
s = f'i{self.i}'
|
|
self.i += 1
|
|
return s
|
|
path = re.sub(r'!', ReplaceUnknown(), path)
|
|
|
|
# Sanitize any SV keywords
|
|
def kw_filter_repl(m: Match) -> str:
|
|
return kwf(m.group(0))
|
|
path = re.sub(r'\w+', kw_filter_repl, path)
|
|
|
|
return path
|
|
|
|
|
|
def get_always_ff_event(dereferencer: 'Dereferencer', resetsignal: 'Optional[SignalNode]') -> str:
|
|
if resetsignal is None:
|
|
return "@(posedge clk)"
|
|
if resetsignal.get_property('async') and resetsignal.get_property('activehigh'):
|
|
return f"@(posedge clk or posedge {dereferencer.get_value(resetsignal)})"
|
|
elif resetsignal.get_property('async') and not resetsignal.get_property('activehigh'):
|
|
return f"@(posedge clk or negedge {dereferencer.get_value(resetsignal)})"
|
|
return "@(posedge clk)"
|
|
|
|
def clog2(n: int) -> int:
|
|
return (n-1).bit_length()
|
|
|
|
def is_pow2(x: int) -> bool:
|
|
return (x > 0) and ((x & (x - 1)) == 0)
|
|
|
|
def roundup_pow2(x: int) -> int:
|
|
return 1<<(x-1).bit_length()
|