<commit_before>'''

Copyright (C) 2011 by <NAME>.  All rights reserved.
License information at: http://playtag.googlecode.com/svn/trunk/LICENSE.txt
'''

import collections
import itertools

from .template import JtagTemplate
from ..bsdl.lookup import PartInfo

binnum = '{0:b}'.format

class Chain(list):
    mindev_idcode = 2
    maxdev_idcode = 32
    maxdev_noid = 32
    max_irbits = 10     # Max instruction length
    min_irbits = 2      # At least INTEST, EXTEST, and BYPASS
    repeat_count = 4

    def error(self, msg):
        raise SystemExit('\nError: %s\n' % msg)

    def __init__(self, jtagrw, **kw):
        bad = set(kw) - set(vars(type(self)))
        if bad:
            self.error("Bad argument(s): %s" % ', '.join(sorted(bad)))
        vars(self).update(kw)
        self.jtagrw = jtagrw
        idcodes = self.repeat_read(self.read_ids, 'IDCODE')
        self.dev_ids = dev_ids = self.find_ids(idcodes)
        self.numdevs = len(dev_ids)
        ir = self.repeat_read(self.read_ir, 'IR')
        ilengths = self.find_ilengths(ir)
        if len(ilengths) > 1 and len(set(dev_ids)) != len(dev_ids):
            self.stripdups(ilengths)
        icapture = set(self.icapture_values(ir, x) for x in ilengths)
        self[:] = [PartInfo(x) for x in dev_ids]
        self.constrain_parts(icapture)
        if len(icapture) != 1:
            self.diagnose_chain(ir)
            raise SystemExit
        icapture, = icapture
        self.updateparts(icapture)
        self.reverse()
        self.add_bypass_info()

    def repeat_read(self, func, info):
        readset = set(func() for i in range(self.repeat_count))
        if len(readset) > 1:
            self.error("Inconsistent JTAG reads (%s):\n    %s" % (
                    info, "\n    ".join(sorted(binnum(x) for x in readset))))
        value, = readset
        return value

    def read_ids(self):
        while 1:
            maxlen = 32 * self.mindev_idcode + self.maxdev_noid + 1
            idinfo = JtagTemplate(self.jtagrw).readd(maxlen+33, tdi=1)().next()
            if self.checkread(idinfo, maxlen, "IDCODE/BYPASS"):
                break
            if self.mindev_idcode >= self.maxdev_idcode:
                self.error("JTAG chain appears to have more than %s devices in it." % self.maxdev_idcode)
            self.mindev_idcode = min(self.mindev_idcode * 2, self.maxdev_idcode)
        return idinfo

    def read_ir(self):
        max_irbits = self.max_irbits
        maxlen = self.numdevs * max_irbits + 1<commit_msg>Allow multiple IR values as long as length doesn't change<commit_after>'''

Copyright (C) 2011 by <NAME>.  All rights reserved.
License information at: http://playtag.googlecode.com/svn/trunk/LICENSE.txt
'''

import collections
import itertools

from .template import JtagTemplate
from ..bsdl.lookup import PartInfo

binnum = '{0:b}'.format

class Chain(list):
    mindev_idcode = 2
    maxdev_idcode = 32
    maxdev_noid = 32
    max_irbits = 10     # Max instruction length
    min_irbits = 2      # At least INTEST, EXTEST, and BYPASS
    repeat_count = 4

    def error(self, msg):
        raise SystemExit('\nError: %s\n' % msg)

    def __init__(self, jtagrw, **kw):
        bad = set(kw) - set(vars(type(self)))
        if bad:
            self.error("Bad argument(s): %s" % ', '.join(sorted(bad)))
        vars(self).update(kw)
        self.jtagrw = jtagrw
        idcodes = self.repeat_read(self.read_ids, 'IDCODE')
        self.dev_ids = dev_ids = self.find_ids(idcodes)
        self.numdevs = len(dev_ids)
        ir = self.repeat_read(self.read_ir, 'IR')
        ilengths = self.find_ilengths(ir)
        if len(ilengths) > 1 and len(set(dev_ids)) != len(dev_ids):
            self.stripdups(ilengths)
        icapture = set(self.icapture_values(ir, x) for x in ilengths)
        self[:] = [PartInfo(x) for x in dev_ids]
        self.constrain_parts(icapture)
        if len(icapture) != 1:
            self.diagnose_chain(ir)
            raise SystemExit
        icapture, = icapture
        self.updateparts(icapture)
        self.reverse()
        self.add_bypass_info()

    def repeat_read(self, func, info):
        readset = set(func() for i in range(self.repeat_count))
        if len(readset) > 1:
            readset = sorted(readset)
            badlist = "\n    ".join(binnum(x) for x in readset)
            if info == 'IR':
                print "Multiple IR values:", badlist
                minval = maxval = readset[0]
                for x in readset:
                    minval &= x
                    maxval |= x
                if maxval < 2 * minval:
                    readset = [minval]
            if len(readset) > 1:
                self.error("Inconsistent JTAG reads (%s):\n    %s" % (
                    info, badlist))
        value, = readset
        return value

    def read_ids(self):
        while 1:
            maxlen = 32 * self.mindev_idcode + self.maxdev_noid + 1
            idinfo = JtagTemplate(self.jtagrw).readd(maxlen+33, tdi=1)().next()
            if self.checkread(idinfo, maxlen, "IDCODE/BYPASS"):
                break
            if self.mindev_idcode >= self.maxdev_idcode:
                self.error("JTAG chain appears to have more than %s devices in it." % self.maxdev_idcode)
            self.mindev_idcode = min(self.mindev_idcode * 2, self.maxdev_idcode)
        return idinfo

    def read_ir(self):
        max_irbits = self.max_irbits
        maxlen = self.numdevs * max_irbits + 1
{'fraction_non_alphanumeric': 0.11378995433789954, 'fraction_numerical': 0.01004566210045662, 'mean_word_length': 2.148936170212766, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4789753', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1805, 'n_words': 426}