Esempio n. 1
0
    def get_data_pm(self, address, length=0):
        """Get data from PM.

        This method works in the exactly the same way as method, but it
        returns data from PM region instead of DM.

        Args:
            address
            length (int, optional)
        """
        try:
            address = int(address, 16)
        except TypeError:
            pass
        address = cu.get_correct_addr(address, Arch.addr_per_word)
        if length == 0:
            return self.pm[address]

        # note: Even the PM RAM is saved in self.pm data section.
        rawdata = []
        length = cu.convert_byte_len_word(length, Arch.addr_per_word)
        for i in range(0, length, Arch.addr_per_word):
            if address + i in self.pm:
                rawdata.append(self.pm[address + i])
            else:
                if i == 0:
                    raise KeyError("Key " + hex(address + i) + " is invalid")
                else:
                    raise OutOfRangeError(
                        "Key " + hex(address + i) + " is invalid",
                        address + i - Arch.addr_per_word)
        return tuple(rawdata)
    def get_data_pm(self, address, length=0):
        """Get data from PM.

        This method works in the exactly the same way as get_data(self,
        address, length = 0) method, but it returns data from PM region
        instead of DM.

        Args:
            address
            length (int, optional)
        """
        # We helpfully accept address as either a hex string or an integer
        try:
            # Try to convert address from a hex string to an integer
            address = int(address, 16)
        except TypeError:
            # Probably means address was already an integer
            pass

        address = cu.get_correct_addr(address, Arch.addr_per_word)

        if length == 0:
            mem_region = Arch.get_pm_region(address)
        else:
            mem_region = Arch.get_pm_region(
                address +
                cu.convert_byte_len_word(length, Arch.addr_per_word) -
                Arch.addr_per_word)
        try:
            if ('PMRAM' in mem_region or 'PMCACHE' in mem_region
                    or 'SLT' in mem_region):
                if length == 0:
                    return self.pm[address:address + Arch.addr_per_word][0]

                return tuple([
                    int(x) for x in self.
                    pm[address:address +
                       cu.convert_byte_len_word(length, Arch.addr_per_word)]
                ])
            else:
                if length == 0:
                    raise KeyError(
                        "Key " + hex(address + Arch.addr_per_word) +
                        " is not a valid PM RAM address",
                        address + Arch.addr_per_word)

                raise OutOfRangeError(
                    "Key " +
                    hex(address +
                        cu.convert_byte_len_word(length, Arch.addr_per_word) -
                        Arch.addr_per_word) + " is not a valid PM RAM address",
                    address +
                    cu.convert_byte_len_word(length, Arch.addr_per_word) -
                    Arch.addr_per_word)
        except Exception as exception:
            if "Transport failure (Unable to read)" in exception:
                sys.stderr.write(str(exception))
                raise ChipNotPoweredError
            else:
                raise exception
Esempio n. 3
0
    def get_data(self, address, length=0, ignore_overflow=False):
        """Returns the contents of one or more addresses.

        This allows you to grab a chunk of memory, e.g. get_data(0x600,
        50). Addresses can be from any valid DM region (DM1, PM RAM,
        mapped NVMEM, memory-mapped registers, etc.).

        Note:
            The length supplied is measured in addressable units.

                get_data(addr) will return a single value;
                get_data(addr, 1) will return a list with a single member.
                get_data(addr, 10) will return a list with ten members or
                    a list with three members (when the memory is octet
                    addressed, 32bit words).

        Note that reading PM RAM via DM is not supported (since not all chips
        map PM into DM). Use `get_data_pm()` instead.

        Args:
            address
            length
            ignore_overflow (bool, optional): Ignore if the read goes out
                from the memory region and append zeros. This is useful if
                an union is at the end of the memory region.

        Raises:
            KeyError: If the address is out of range.
            OutOfRangeError: If the address is valid but the length is not
                (i.e. address+length is not a valid address).

        """
        # We helpfully accept address as either a hex string or an integer
        try:
            # Try to convert address from a hex string to an integer
            address = int(address, 16)
        except TypeError:
            # Probably means address was already an integer
            pass
        address = cu.get_correct_addr(address, Arch.addr_per_word)

        if length == 0:
            mem_region = Arch.get_dm_region(address, False)
        else:
            # kalaccess will wrap the memory if the read goes out of boundary.
            # So to ignore overflow just get the memory region from the
            # beginning.
            if ignore_overflow:
                mem_region = Arch.get_dm_region(address)
            else:
                mem_region = Arch.get_dm_region(
                    address +
                    cu.convert_byte_len_word(length, Arch.addr_per_word) -
                    Arch.addr_per_word)
        try:
            if ('DM' in mem_region or 'SLT' in mem_region
                    or 'MMR' in mem_region or 'NVMEM' in mem_region
                    or 'PMRAM' in mem_region or 'MCU' in mem_region):
                if length == 0:
                    return self.dm[address:address + Arch.addr_per_word][0]

                return tuple([
                    int(x) for x in self.
                    dm[address:address +
                       cu.convert_byte_len_word(length, Arch.addr_per_word)]
                ])
            else:
                if length == 0:
                    raise KeyError(
                        "Key " + hex(address + Arch.addr_per_word) +
                        " is not a valid DM address",
                        address + Arch.addr_per_word)

                raise OutOfRangeError(
                    "Key " +
                    hex(address +
                        cu.convert_byte_len_word(length, Arch.addr_per_word) -
                        Arch.addr_per_word) + " is not a valid DM address",
                    address +
                    cu.convert_byte_len_word(length, Arch.addr_per_word) -
                    Arch.addr_per_word)

        except (SystemExit, KeyboardInterrupt, GeneratorExit):
            raise

        except Exception as exception:
            if "Transport failure (Unable to read)" in exception:
                sys.stderr.write(str(exception))
                raise ChipNotPoweredError

            # Will also spit exceptions to log file, if set.
            sys.stderr.write(traceback.format_exc())

            raise exception
Esempio n. 4
0
    def set_data(self, address, value):
        """Sets the contents of one or more addresses.

        This allows you to write a list of values to a chunk of memory.
        Addresses can only be from any valid DM RAM or memory mapped
        register region.  e.g. set_data(0x3e5d, [1 2 3])

        Note:
            set_data(address, [val]) writes address with a single value val.
            set_data(address, [val1 ... valn]) writes the list of values
                to memory starting from address.

        This function should only be implemented for live chips. And should not
        be called if the chip is not live.


        Args:
            address
            value

        Raises:
            KeyError: If the address is out of range.
            OutOfRangeError: If the address is valid but the length is not
                (i.e. address+length is not a valid address).
        """
        # We helpfully accept address as either a hex string or an integer
        try:
            # Try to convert address from a hex string to an integer
            address = int(address, 16)
        except TypeError:
            # Probably means address was already an integer
            pass

        address = cu.get_correct_addr(address, Arch.addr_per_word)

        length = len(value)
        if length == 1:
            mem_region = Arch.get_dm_region(address)
        else:
            mem_region = Arch.get_dm_region(
                address +
                cu.convert_byte_len_word(length, Arch.addr_per_word) -
                Arch.addr_per_word)

        try:
            if 'DM' in mem_region or 'MMR' in mem_region:
                self.dm[address] = value
            else:
                if length == 1:
                    raise KeyError(
                        "Key " + hex(Arch.addr_per_word + address) +
                        " is not in a DM or PM region",
                        address + Arch.addr_per_word)
                else:
                    raise OutOfRangeError(
                        "Key " + hex(address + cu.convert_byte_len_word(
                            length, Arch.addr_per_word) - Arch.addr_per_word) +
                        " is not in a DM or PM region", address +
                        cu.convert_byte_len_word(length, Arch.addr_per_word) -
                        Arch.addr_per_word)

        except (SystemExit, KeyboardInterrupt, GeneratorExit):
            raise

        except Exception as exception:
            if "Transport failure (Unable to read)" in exception:
                sys.stderr.write(str(exception))
                raise ChipNotPoweredError
            else:
                raise exception