def tms_mpsse(bits):
    """convert a tms bit sequence to an mpsee (len, bits) tuple"""
    n = len(bits)
    assert (n > 0) and (n <= 7)
    x = 0
    # tms is shifted lsb first
    for i in range(n - 1, -1, -1):
        x = (x << 1) + bits[i]
    # only bits 0 thru 6 are shifted on tms - tdi is set to bit 7 (and is left there)
    # len = n means clock out n + 1 bits
    return (n - 1, x & 127)