Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _read_bytes(self, length: int) -> BitSequence:
"""Read out bytes from TDO"""
if length > JtagController.FTDI_PIPE_LEN:
raise JtagError("Cannot fit into FTDI fifo")
alen = length-1
cmd = bytearray((Ftdi.READ_BYTES_NVE_LSB, alen & 0xff,
(alen >> 8) & 0xff))
self._stack_cmd(cmd)
self.sync()
data = self._ftdi.read_data_bytes(length, 4)
bs = BitSequence(bytes_=data, length=8*length)
# print("READ BYTES %s" % bs)
return bs
def _read_bits(self, length: int):
"""Read out bits from TDO"""
if length > 8:
raise JtagError("Cannot fit into FTDI fifo")
cmd = bytearray((Ftdi.READ_BITS_NVE_LSB, length-1))
self._stack_cmd(cmd)
self.sync()
data = self._ftdi.read_data_bytes(1, 4)
# need to shift bits as they are shifted in from the MSB in FTDI
byte = data[0] >> 8-length
bs = BitSequence(byte, length=length)
# print("READ BITS %s" % bs)
return bs
# print("push %d bytes" % byte_count)
if bit_count:
# print("RW OUT %s" % out[pos:])
cmd = bytearray((Ftdi.RW_BITS_PVE_NVE_LSB, bit_count-1))
cmd.append(out[pos:].tobyte())
self._stack_cmd(cmd)
# print("push %d bits" % bit_count)
self.sync()
bs = BitSequence()
byte_count = length//8
pos = 8*byte_count
bit_count = length-pos
if byte_count:
data = self._ftdi.read_data_bytes(byte_count, 4)
if not data:
raise JtagError('Unable to read data from FTDI')
byteseq = BitSequence(bytes_=data, length=8*byte_count)
# print("RW IN %s" % byteseq)
bs.append(byteseq)
# print("pop %d bytes" % byte_count)
if bit_count:
data = self._ftdi.read_data_bytes(1, 4)
if not data:
raise JtagError('Unable to read data from FTDI')
byte = data[0]
# need to shift bits as they are shifted in from the MSB in FTDI
byte >>= 8-bit_count
bitseq = BitSequence(byte, length=bit_count)
bs.append(bitseq)
# print("pop %d bits" % bit_count)
if len(bs) != length:
raise ValueError("Internal error")
tdo = None
if stuck is None:
stuck = tdo
if stuck != tdo:
stuck = None
rcv >>= length
if rcv == inj:
ok = True
else:
break
inj.inc()
if ok:
print("Register detected length: %d" % length)
return length
if stuck is not None:
raise JtagError('TDO seems to be stuck')
raise JtagError('Unable to detect register length')
def shift_register(self, length) -> BitSequence:
if not self._sm.state_of('shift'):
raise JtagError("Invalid state: %s" % self._sm.state())
if self._sm.state_of('capture'):
bs = BitSequence(False)
self._ctrl.write_tms(bs)
self._sm.handle_events(bs)
return self._ctrl.shift_register(length)
def sync(self) -> None:
if not self._ftdi.is_connected:
raise JtagError("FTDI controller terminated")
if self._write_buff:
self._ftdi.write_data(self._write_buff)
self._write_buff = bytearray()
def _stack_cmd(self, cmd: Union[bytes, bytearray]):
if not isinstance(cmd, (bytes, bytearray)):
raise TypeError('Expect bytes or bytearray')
if not self._ftdi:
raise JtagError("FTDI controller terminated")
# Currrent buffer + new command + send_immediate
if (len(self._write_buff)+len(cmd)+1) >= JtagController.FTDI_PIPE_LEN:
self.sync()
self._write_buff.extend(cmd)
def shift_register(self, out: BitSequence,
use_last: bool = False) -> BitSequence:
"""Shift a BitSequence into the current register and retrieve the
register output"""
if not isinstance(out, BitSequence):
return JtagError('Expect a BitSequence')
length = len(out)
if use_last:
(out, self._last) = (out[:-1], int(out[-1]))
byte_count = len(out)//8
pos = 8*byte_count
bit_count = len(out)-pos
if not byte_count and not bit_count:
raise JtagError("Nothing to shift")
if byte_count:
blen = byte_count-1
# print("RW OUT %s" % out[:pos])
cmd = bytearray((Ftdi.RW_BYTES_PVE_NVE_LSB,
blen, (blen >> 8) & 0xff))
cmd.extend(out[:pos].tobytes(msby=True))
self._stack_cmd(cmd)
# print("push %d bytes" % byte_count)
def detect_register_size(self) -> int:
# Freely inpired from UrJTAG
stm = self._engine.state_machine
if not stm.state_of('shift'):
raise JtagError("Invalid state: %s" % stm.state())
if stm.state_of('capture'):
bs = BitSequence(False)
self._engine.controller.write_tms(bs)
stm.handle_events(bs)
MAX_REG_LEN = 1024
PATTERN_LEN = 8
stuck = None
for length in range(1, MAX_REG_LEN):
print("Testing for length %d" % length)
if length > 5:
return
zero = BitSequence(length=length)
inj = BitSequence(length=length+PATTERN_LEN)
inj.inc()
ok = False
for p in range(1, 1 << PATTERN_LEN):