@@ -1946,6 +1946,57 @@ def __call__(cls, shape=None, src_loc_at=0, **kwargs):
19461946 return signal
19471947
19481948
1949+ # also used for MemoryData.Init
1950+ def _get_init_value (init , shape , what = "signal" ):
1951+ orig_init = init
1952+ orig_shape = shape
1953+ shape = Shape .cast (shape )
1954+ if isinstance (orig_shape , ShapeCastable ):
1955+ try :
1956+ init = Const .cast (orig_shape .const (init ))
1957+ except Exception :
1958+ raise TypeError (f"Initial value must be a constant initializer of { orig_shape !r} " )
1959+ if init .shape () != Shape .cast (shape ):
1960+ raise ValueError (f"Constant returned by { orig_shape !r} .const() must have the shape "
1961+ f"that it casts to, { shape !r} , and not { init .shape ()!r} " )
1962+ return init .value
1963+ else :
1964+ if init is None :
1965+ init = 0
1966+ try :
1967+ init = Const .cast (init )
1968+ except TypeError :
1969+ raise TypeError ("Initial value must be a constant-castable expression, not {!r}"
1970+ .format (orig_init ))
1971+ # Avoid false positives for all-zeroes and all-ones
1972+ if orig_init is not None and not (isinstance (orig_init , int ) and orig_init in (0 , - 1 )):
1973+ if init .shape ().signed and not shape .signed :
1974+ warnings .warn (
1975+ message = f"Initial value { orig_init !r} is signed, "
1976+ f"but the { what } shape is { shape !r} " ,
1977+ category = SyntaxWarning ,
1978+ stacklevel = 2 )
1979+ elif (init .shape ().width > shape .width or
1980+ init .shape ().width == shape .width and
1981+ shape .signed and not init .shape ().signed ):
1982+ warnings .warn (
1983+ message = f"Initial value { orig_init !r} will be truncated to "
1984+ f"the { what } shape { shape !r} " ,
1985+ category = SyntaxWarning ,
1986+ stacklevel = 2 )
1987+
1988+ if isinstance (orig_shape , range ) and orig_init is not None and orig_init not in orig_shape :
1989+ if orig_init == orig_shape .stop :
1990+ raise SyntaxError (
1991+ f"Initial value { orig_init !r} equals the non-inclusive end of the { what } "
1992+ f"shape { orig_shape !r} ; this is likely an off-by-one error" )
1993+ else :
1994+ raise SyntaxError (
1995+ f"Initial value { orig_init !r} is not within the { what } shape { orig_shape !r} " )
1996+
1997+ return Const (init .value , shape ).value
1998+
1999+
19492000@final
19502001class Signal (Value , DUID , metaclass = _SignalMeta ):
19512002 """A varying integer value.
@@ -2016,54 +2067,9 @@ def __init__(self, shape=None, *, name=None, init=None, reset=None, reset_less=F
20162067 DeprecationWarning , stacklevel = 2 )
20172068 init = reset
20182069
2019- orig_init = init
2020- if isinstance (orig_shape , ShapeCastable ):
2021- try :
2022- init = Const .cast (orig_shape .const (init ))
2023- except Exception :
2024- raise TypeError ("Initial value must be a constant initializer of {!r}"
2025- .format (orig_shape ))
2026- if init .shape () != Shape .cast (orig_shape ):
2027- raise ValueError ("Constant returned by {!r}.const() must have the shape that "
2028- "it casts to, {!r}, and not {!r}"
2029- .format (orig_shape , Shape .cast (orig_shape ),
2030- init .shape ()))
2031- else :
2032- if init is None :
2033- init = 0
2034- try :
2035- init = Const .cast (init )
2036- except TypeError :
2037- raise TypeError ("Initial value must be a constant-castable expression, not {!r}"
2038- .format (orig_init ))
2039- # Avoid false positives for all-zeroes and all-ones
2040- if orig_init is not None and not (isinstance (orig_init , int ) and orig_init in (0 , - 1 )):
2041- if init .shape ().signed and not self ._signed :
2042- warnings .warn (
2043- message = "Initial value {!r} is signed, but the signal shape is {!r}"
2044- .format (orig_init , shape ),
2045- category = SyntaxWarning ,
2046- stacklevel = 2 )
2047- elif (init .shape ().width > self ._width or
2048- init .shape ().width == self ._width and
2049- self ._signed and not init .shape ().signed ):
2050- warnings .warn (
2051- message = "Initial value {!r} will be truncated to the signal shape {!r}"
2052- .format (orig_init , shape ),
2053- category = SyntaxWarning ,
2054- stacklevel = 2 )
2055- self ._init = Const (init .value , shape ).value
2070+ self ._init = _get_init_value (init , unsigned (1 ) if orig_shape is None else orig_shape )
20562071 self ._reset_less = bool (reset_less )
20572072
2058- if isinstance (orig_shape , range ) and orig_init is not None and orig_init not in orig_shape :
2059- if orig_init == orig_shape .stop :
2060- raise SyntaxError (
2061- f"Initial value { orig_init !r} equals the non-inclusive end of the signal "
2062- f"shape { orig_shape !r} ; this is likely an off-by-one error" )
2063- else :
2064- raise SyntaxError (
2065- f"Initial value { orig_init !r} is not within the signal shape { orig_shape !r} " )
2066-
20672073 self ._attrs = OrderedDict (() if attrs is None else attrs )
20682074
20692075 if isinstance (orig_shape , ShapeCastable ):
0 commit comments