Exemplo n.º 1
0
    def __setitem__(self, i, y):
        """ Implements auto-resize feature, ie resizes all assigns to Sfix registers.
        Also implements the register behaviour i.e saves assigned value to shadow variable, that is later used by the '_pyha_update_registers' function.
        """
        if hasattr(self.data[0], '_pyha_update_registers'):
            # object already knows how to handle registers
            # copy relevant stuff only
            for k, v in y.__dict__.items():
                if k.startswith('_pyha'):
                    continue
                setattr(self.data[i], k, v)
                # self.data[i].__dict__['_pyha_next'][k] = v

        else:
            if isinstance(self.data[i], (Sfix, Complex)):
                with SimPath(f'{self.var_name}[{i}]='):
                    y = auto_resize(self.data[i], y)

                # lazy bounds feature, if bounds is None, take the bound from assigned value
                if self.data[i].left is None:
                    for x, xn in zip(self.data, self._pyha_next):
                        x.left = y.left
                        xn.left = y.left

                if self.data[i].right is None:
                    for x, xn in zip(self.data, self._pyha_next):
                        x.right = y.right
                        xn.right = y.right

            if RegisterBehaviour.is_enabled():
                self._pyha_next[i] = y
            else:
                self.data[i] = y
Exemplo n.º 2
0
    def __setattr__(self, name, value):
        """ Implements auto-resize feature, ie resizes all assigns to Sfix registers.
        Also implements the register behaviour i.e saves assigned value to shadow variable, that is later used by the '_pyha_update_registers' function.
        """
        if hasattr(self, '_pyha_is_initialization') or self._pyha_is_local() or not RegisterBehaviour.is_enabled():
            self.__dict__[name] = value
            return

        if AutoResize.is_enabled():
            target = getattr(self._pyha_initial_self, name)
            with SimPath(f'{name}='):
                value = auto_resize(target, value)

        if isinstance(value, list):
            # list assign
            # example: self.i = [i] + self.i[:-1]
            assert isinstance(self.__dict__[name], PyhaList)
            if hasattr(self.__dict__[name][0], '_pyha_update_registers'):
                # list of submodules -> need to copy each value to submodule next
                for elem, new in zip(self.__dict__[name], value):
                    # for deeper submodules, deepcopy was not necessary..
                    # copy relevant stuff only
                    for k, v in new.__dict__.items():
                        if k.startswith('_pyha'):
                            continue
                        elem.__dict__['_pyha_next'][k] = v
            else:
                self.__dict__[name]._pyha_next = value
            return

        if isinstance(value, Hardware):
            for k, v in value.__dict__.items():
                if k.startswith('_pyha'):
                    continue
                n = self.__dict__[name]
                setattr(n, k, v)
            return

        self.__dict__['_pyha_next'][name] = value