Ejemplo n.º 1
0
 def test12(self):
     x = bits.bits().set_bytes((0xff, ), 7)
     self.assertEqual(str(x), '(7) 1111111')
     x = bits.bits().set_bytes((0x1, ), 7)
     self.assertEqual(str(x), '(7) 0000001')
     x = bits.bits().set_bytes((64, ), 7)
     self.assertEqual(str(x), '(7) 1000000')
Ejemplo n.º 2
0
 def reset_idcodes(self):
     """return a tuple of the idcodes for the JTAG chain"""
     # a JTAG reset leaves DR as the 32 bit idcode for each device.
     self.driver.trst()
     tdi = bits.bits(self.ndevs * _idcode_length)
     tdo = bits.bits()
     self.driver.scan_dr(tdi, tdo)
     return tdo.scan((_idcode_length, ) * self.ndevs)
Ejemplo n.º 3
0
Archivo: jtag.py Proyecto: deadsy/pycs
 def reset_idcodes(self):
     """return a tuple of the idcodes for the JTAG chain"""
     # a JTAG reset leaves DR as the 32 bit idcode for each device.
     self.driver.trst()
     tdi = bits.bits(self.ndevs * _idcode_length)
     tdo = bits.bits()
     self.driver.scan_dr(tdi, tdo)
     return tdo.scan((_idcode_length, ) * self.ndevs)
Ejemplo n.º 4
0
 def cmd_sir(self, args):
     """command: SIR length TDI (tdi) SMASK (smask) [TDO (tdo) MASK (mask)]"""
     vals = self.parse_tdi_tdo(args[1:])
     tdi = bits.bits(vals['NBITS'], vals['TDI'])
     if vals.has_key('TDO'):
         tdo = bits.bits()
         self.jtag.rw_ir(tdi, tdo)
         self.validate_tdo(tdo, vals)
     else:
         self.jtag.wr_ir(tdi)
Ejemplo n.º 5
0
def test_file(directory, filename, window, symbol, search, _verbose=False):
    data = bits()
    with open(os.path.join(directory, filename), "rb") as file:
        data.fromfile(file)

    if _verbose:
        print("[{} W:{} S:{} L:{}] Starting compression at {}".format(
            filename, window, symbol, search, datetime.now()))

    comp_start = datetime.now()
    compressed = compress_lz77(data, symbol, window, search)
    comp_end = datetime.now()
    if _verbose:
        print(
            "[{} w{} s{} l{}] Data compressed, took {}, A compression ratio of {}"
            .format(filename, window, symbol, search, comp_end - comp_start,
                    len(data) / len(compressed)))
    dec_start = datetime.now()
    decompressed = decode_lz77(compressed)
    dec_end = datetime.now()

    if _verbose:
        print("[{} w{} s{} l{}] Data decompressed, took {}".format(
            filename, window, symbol, search, dec_end - dec_start))
    return filename, len(data), symbol, window, search, len(compressed), (
        comp_end - comp_start).total_seconds(), (dec_end -
                                                 dec_start).total_seconds()
Ejemplo n.º 6
0
def cmd_compress(args):
    assert os.path.isfile(args.input), "Input wasn't a file"
    assert args.symbolSize > 0, "Positive symbol size required"
    assert args.windowSize > 0, "Positive window size required"
    output = args.output if args.output is not None else "{}.lz77".format(
        args.input)

    directory = os.path.dirname(output)
    if not os.path.exists(directory):
        print("Creating output directory...")
        os.makedirs(directory)

    data = bits()
    with open(args.input, "rb") as file:
        data.fromfile(file)

    if args.lzss:
        encoding = compress_lzss(data, args.symbolSize, args.windowSize,
                                 args.maxSearch)
    else:
        encoding = compress_lz77(data, args.symbolSize, args.windowSize,
                                 args.maxSearch)

    with open(output, "wb+") as file:
        encoding.tofile(file)
Ejemplo n.º 7
0
 def chain_length(self, scan):
     """return the length of the JTAG chain"""
     tdo = bits.bits()
     # build a 000...001000...000 flush buffer for tdi
     tdi = bits.bits(_flush_size)
     tdi.append_ones(1)
     tdi.append_zeroes(_flush_size)
     scan(tdi, tdo)
     # the first bits are junk
     tdo.drop_lsb(_flush_size)
     # work out how many bits tdo is behind tdi
     s = tdo.bit_str()
     s = s.lstrip('0')
     if len(s.replace('0', '')) != 1:
         raise Error('unexpected result from jtag chain - multiple 1\'s')
     return len(s) - 1
Ejemplo n.º 8
0
def decode_lzss(data: bits):
    symbol_bits, distance_bits, length_bits = (data.sub(i, 8).toint()
                                               for i in range(0, 17, 8))
    # print(f"symbol_bits={symbol_bits} distance_bits={distance_bits} length_bits={length_bits}")
    bits_per = sum((symbol_bits, distance_bits, length_bits))
    dec = bits()
    dec_pos = 24
    # print(data[24:])
    while dec_pos < len(data):
        flag, dec_pos = data.sub(dec_pos, 1).toint(), dec_pos + 1
        if flag:
            distance, dec_pos = data.sub(
                dec_pos, distance_bits).toint(), dec_pos + distance_bits
            length, dec_pos = data.sub(
                dec_pos, length_bits).toint(), dec_pos + length_bits
            dec.extend(
                dec.sub(
                    len(dec) - ((distance) * symbol_bits),
                    length * symbol_bits))
        nextSymbol, dec_pos = data.sub(dec_pos,
                                       symbol_bits), dec_pos + symbol_bits
        # print(f"Decoding dist={distance}, length={length}, nextSymbol={nextSymbol}")
        dec.extend(nextSymbol)
    # print(-(len(dec) % 8))
    return dec
Ejemplo n.º 9
0
def compress_lzss(data: bits, symbol: int, window: int, max_length: int):
    enc, enc_pos = bits().fromints(
        8, (symbol, window.bit_length(), max_length.bit_length())), 0
    data.extend(False for i in range((symbol - (len(data) % symbol))))
    while enc_pos * symbol < len(data):
        buffer_start, buffer_end, buffer_size = max(
            0, enc_pos - window), enc_pos, min(enc_pos, window)
        buffer = list(
            data.sub(i * symbol, symbol)
            for i in range(buffer_start, buffer_end))
        distance, length, nextSymbol = 0, 0, data.sub(enc_pos * symbol, symbol)
        matches = [
            buffer_size - i for i, s in enumerate(buffer) if s == nextSymbol
        ]
        # print(buffer, matches)
        while len(matches) > 0 and length < max_length:
            distance = matches[-1]
            length += 1
            nextSymbol = data.sub((enc_pos + length) * symbol, symbol)
            matches = list(
                filter(
                    lambda match: match - length > 0 and buffer[
                        -match + length] == nextSymbol, matches))
        enc_pos += length + 1
        # print(nextSymbol)
        if distance + length > 0:
            enc.append(True)
            enc.fromint(window.bit_length(),
                        distance).fromint(max_length.bit_length(), length)
        else:
            enc.append(False)
        enc.extend(nextSymbol)
    return enc
Ejemplo n.º 10
0
Archivo: jtag.py Proyecto: deadsy/pycs
 def chain_length(self, scan):
     """return the length of the JTAG chain"""
     tdo = bits.bits()
     # build a 000...001000...000 flush buffer for tdi
     tdi = bits.bits(_flush_size)
     tdi.append_ones(1)
     tdi.append_zeroes(_flush_size)
     scan(tdi, tdo)
     # the first bits are junk
     tdo.drop_lsb(_flush_size)
     # work out how many bits tdo is behind tdi
     s = tdo.bit_str()
     s = s.lstrip('0')
     if len(s.replace('0', '')) != 1:
         raise Error, 'unexpected result from jtag chain - multiple 1\'s'
     return len(s) - 1
Ejemplo n.º 11
0
def held_karp_dicts(cities, count, verbose=True):
    """ ([point],int,bool) -> float

    Calculate the min-cost TSP tour for a list of cities.

    Keyword arguments:
    cities  -- list of points in a 2-D plane
    count   -- number of cities
    verbose -- optional parameter for extra diagnostics
    """

    distances = distance_matrix(cities)

    # Only non-trivial base case; if not in the table then assume +Inf
    B = {1^(1<<count):0}

    if verbose:
        i = 0
        start = time.clock()

    for m in range(2, count+1):
        # Get all the cities subsets of size m that include city 0
        size_m_cities = bits.combinations_with_0(count, m)
        A = {}

        # subtract 1 b/c we ignore city 0
        if verbose:
            diagnostics_next_size(m, choose(count-1, m-1))

        for S in size_m_cities:
            if verbose:
                if i % 100000 == 0:
                    diagnostics_set(i, time.clock()-start, sys.getsizeof(A),
                                    sys.getsizeof(B))
                    start = time.clock()
                i += 1

            items = bits.bits(S)

            for j in items[1:]: # ignore city 0
                res = list()

                for k in items:
                    if k == j:
                        continue

                    index_k = bits.generate_index(bits.delete_city(S, j),
                                                  count, k)

                    if index_k in B:
                        # calculate A[S-{j},k] + ckj
                        res.append(B[index_k] + distances[k][j])

                # print res
                A[bits.generate_index(S, count, j)] = min(res)
        B = A

    final_set = list(bits.combinations_with_0(count, count))[0]

    return final_hop(count, final_set, B, distances)
Ejemplo n.º 12
0
 def validate_tdo(self, tdo, vals):
     """validate returned tdo bits against expectations"""
     if vals.has_key('TDO'):
         n = vals['NBITS']
         tdo_expected = bits.bits(n, vals['TDO'])
         if vals.has_key('MASK'):
             # new tdo mask value
             self.mask = bits.bits(n, vals['MASK'])
         else:
             # validate old tdo mask value
             if self.mask is None:
                 raise Error, 'line %d: no mask value set for tdo' % self.line
             if len(self.mask) != n:
                 raise Error, 'line %d: bad mask length for tdo' % self.line
         if (tdo & self.mask) != (tdo_expected & self.mask):
             raise Error, 'line %d: tdo actual/expected mismatch' % self.line
Ejemplo n.º 13
0
 def num_devices(self):
     """return the number of JTAG devices in the chain"""
     # put every device into bypass mode (IR = all 1's)
     tdi = bits.bits()
     tdi.ones(_flush_size)
     self.driver.scan_ir(tdi)
     # now each DR is a single bit
     # the DR chain length is the number of devices
     return self.dr_length()
Ejemplo n.º 14
0
Archivo: jtag.py Proyecto: deadsy/pycs
 def num_devices(self):
     """return the number of JTAG devices in the chain"""
     # put every device into bypass mode (IR = all 1's)
     tdi = bits.bits()
     tdi.ones(_flush_size)
     self.driver.scan_ir(tdi)
     # now each DR is a single bit
     # the DR chain length is the number of devices
     return self.dr_length()
Ejemplo n.º 15
0
Archivo: jtag.py Proyecto: deadsy/pycs
 def wr_dr(self, wr):
     """
     write to DR for a device
     wr: bitbuffer to be written to dr for this device
     note - other devices are assumed to be in bypass mode
     """
     tdi = bits.bits()
     tdi.append_ones(self.ndevs_before)
     tdi.append(wr)
     tdi.append_ones(self.ndevs_after)
     self.driver.scan_dr(tdi)
Ejemplo n.º 16
0
Archivo: jtag.py Proyecto: deadsy/pycs
 def wr_ir(self, wr):
     """
     write to IR for a device
     wr: the bitbuffer to be written to ir for this device
     note - other devices will be placed in bypass mode (ir = all 1's)
     """
     tdi = bits.bits()
     tdi.append_ones(self.irlen_before)
     tdi.append(wr)
     tdi.append_ones(self.irlen_after)
     self.driver.scan_ir(tdi)
Ejemplo n.º 17
0
 def rd_dr(self, rd):
   """
   read n-bits from a DR register
   note - other devices are assumed to be in bypass mode
   """
   # add bits for the bypassed devices
   tdi = bits.bits(rd.n + self.ndevs_before + self.ndevs_after)
   self.driver.scan_dr(tdi, rd)
   # strip bits from the bypassed devices
   rd.drop_msb(self.ndevs_after)
   rd.drop_lsb(self.ndevs_before)
Ejemplo n.º 18
0
 def wr_ir(self, wr):
     """
     write to IR for a device
     wr: the bitbuffer to be written to ir for this device
     note - other devices will be placed in bypass mode (ir = all 1's)
     """
     tdi = bits.bits()
     tdi.append_ones(self.irlen_before)
     tdi.append(wr)
     tdi.append_ones(self.irlen_after)
     self.driver.scan_ir(tdi)
Ejemplo n.º 19
0
 def wr_dr(self, wr):
     """
     write to DR for a device
     wr: bitbuffer to be written to dr for this device
     note - other devices are assumed to be in bypass mode
     """
     tdi = bits.bits()
     tdi.append_ones(self.ndevs_before)
     tdi.append(wr)
     tdi.append_ones(self.ndevs_after)
     self.driver.scan_dr(tdi)
Ejemplo n.º 20
0
Archivo: jtag.py Proyecto: deadsy/pycs
 def rw_dr(self, wr, rd):
     """
     read/write DR for a device
     wr: bitbuffer to be written to dr for this device
     rd: bitbuffer to be read from dr for this device
     note - other devices are assumed to be in bypass mode
     """
     tdi = bits.bits()
     tdi.append_ones(self.ndevs_before)
     tdi.append(wr)
     tdi.append_ones(self.ndevs_after)
     self.driver.scan_dr(tdi, rd)
     # strip the dr bits from the bypassed devices
     rd.drop_msb(self.ndevs_before)
     rd.drop_lsb(self.ndevs_after)
Ejemplo n.º 21
0
 def rw_dr(self, wr, rd):
     """
     read/write DR for a device
     wr: bitbuffer to be written to dr for this device
     rd: bitbuffer to be read from dr for this device
     note - other devices are assumed to be in bypass mode
     """
     tdi = bits.bits()
     tdi.append_ones(self.ndevs_before)
     tdi.append(wr)
     tdi.append_ones(self.ndevs_after)
     self.driver.scan_dr(tdi, rd)
     # strip the dr bits from the bypassed devices
     rd.drop_msb(self.ndevs_before)
     rd.drop_lsb(self.ndevs_after)
Ejemplo n.º 22
0
def cmd_decompress(args):
    assert os.path.isfile(args.input), "Input wasn't a file"
    output = args.output if args.output is not None else args.input.replace(
        ".lz77", "")

    directory = os.path.dirname(output)
    if not os.path.exists(directory):
        print("Creating output directory...")
        os.makedirs(directory)

    encoding = bits()
    with open(args.input, "rb") as file:
        encoding.fromfile(file)

    if args.lzss:
        data = decode_lzss(encoding)
    else:
        data = decode_lz77(encoding)

    with open(output, "wb+") as file:
        data.tofile(file)
Ejemplo n.º 23
0
 def rd_dr(self, n):
     """read n bits from the current data register"""
     wr = bits.bits(n)
     rd = bits.bits(n)
     self.jtag.rw_dr(wr, rd)
     return rd.scan((n, ))[0]
Ejemplo n.º 24
0
 def wr_ir(self, val):
     """write instruction register"""
     wr = bits.bits(self._IR_LEN, val)
     self.jtag.wr_ir(wr)
Ejemplo n.º 25
0
def held_karp_scipy(cities, count, verbose=True):
    """ ([point],int,bool) -> float

    Calculate the min-cost TSP tour for a list of cities.

    Keyword arguments:
    cities  -- list of points in a 2-D plane
    count   -- number of cities
    verbose -- optional parameter for extra diagnostics
    """

    distances = distance_matrix(cities)

    # Only non-trivial base case; if not in the table then assume +Inf
    # B = {1^(1<<count):0}
    B = sparse.dok_matrix((2,1), dtype=np.float32) # or size 0?
    B[1,0] = 0 # Of B[0,1] = 0?

    if verbose:
        i = 0
        start = time.clock()

    # pdb.set_trace()

    for m in range(2, count+1):
        # Get all the cities subsets of size m that include city 0
        size_m_cities = bits.combinations_with_0(count, m)
        A = sparse.dok_matrix((1<<count,count), dtype=np.float32)

        # subtract 1 b/c we ignore city 0
        if verbose:
            diagnostics_next_size(m, choose(count-1, m-1))

        for S in size_m_cities:
            if verbose:
                if i % 100000 == 0:
                    diagnostics_set(i, time.clock()-start, sys.getsizeof(A),
                                    sys.getsizeof(B))
                    start = time.clock()
                i += 1

            items = bits.bits(S)

            for j in items[1:]: # ignore city 0
                res = list()

                for k in items:
                    if k == j:
                        continue

                    S_old = bits.delete_city(S, j)
                    if B[S_old,k] != 0 or S_old == 1:
                        # calculate A[S-{j},k] + ckj
                        res.append(B[S_old,k] + distances[k][j])

                A[S,j] = min(res)
        B = A

    final_set = list(bits.combinations_with_0(count, count))[0]

    return final_hop2(count, final_set, B, distances)
Ejemplo n.º 26
0
 def wr_dr(self, n, val):
   """write n bits to the current dr register"""
   wr = bits.bits(n, val)
   self.device.wr_dr(wr)
Ejemplo n.º 27
0
 def wr_abort(self, val):
     """write abort register"""
     self.wr_ir(_IR_ABORT)
     self.device.wr_dr(bits.bits(_DR_ABORT_LEN, val))
Ejemplo n.º 28
0
 def wr_ir(self, val):
   """write instruction register"""
   wr = bits.bits(_IR_LEN, val)
   self.device.wr_ir(wr)
Ejemplo n.º 29
0
 def rw_dr(self, n, val = 0):
   """read/write n bits from the current dr register"""
   wr = bits.bits(n, val)
   rd = bits.bits(n)
   self.device.rw_dr(wr, rd)
   return rd.scan((n,))[0]
Ejemplo n.º 30
0
 def wr_abort(self, val):
   """write abort register"""
   self.wr_ir(_IR_ABORT)
   self.device.wr_dr(bits.bits(_DR_ABORT_LEN, val))
Ejemplo n.º 31
0
 def rw_dr(self, n, val=0):
     """read/write n bits from the current dr register"""
     wr = bits.bits(n, val)
     rd = bits.bits(n)
     self.device.rw_dr(wr, rd)
     return rd.scan((n, ))[0]
Ejemplo n.º 32
0
 def wr_ir(self, val):
   """write instruction register"""
   self.device.wr_ir(bits.bits(_IR_LEN, val))
Ejemplo n.º 33
0
 def wr_dr(self, n, val):
   """write current data register"""
   self.device.wr_dr(bits.bits(n, val))
Ejemplo n.º 34
0
 def rd_dr(self, n):
   """read from current data register"""
   rd = bits.bits(n)
   self.device.rd_dr(rd)
   return rd.scan((n,))[0]
Ejemplo n.º 35
0
 def wr_rd_dr(self, n, val):
   """write and read from current data register"""
   rd = bits.bits(n)
   self.device.wr_rd_dr(bits.bits(n, val), rd)
   return rd.scan((n,))[0]
Ejemplo n.º 36
0
 def test13(self):
     x = bits.from_tuple(str0)
     y = bits.bits().set_bytes(x.get_bytes(), len(str0))
     self.assertEqual(y.bit_str(), str0)