+ static unsigned char last_data = 0;
+ static unsigned char last_write = 0x00;
+ static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
+ static unsigned char readbuf[USBBUFSIZE], *readpos;
+ unsigned char data, prev_data, last_cyc_write;
+ struct jtagkey_reader_arg targ;
+ pthread_t reader_thread;
+
+ /* Count reads */
+ for (i = 0; i < num; i++)
+ if (tr[i].cmdTrans == PP_READ)
+ nread++;
+
+ /* Write combining */
+ if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
+ unsigned char *pos = writebuf;
+ int len;
+
+ DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
+ jtagkey_latency(BULK_LATENCY);
+
+ targ.num = writepos-pos;
+ targ.buf = readbuf;
+ pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
+
+ while (pos < writepos) {
+ len = writepos-pos;
+
+ if (len > USBBUFSIZE)
+ len = USBBUFSIZE;
+
+ DPRINTF("combined write of %d/%d\n",len,writepos-pos);
+ ftdi_write_data(&ftdic, pos, len);
+ pos += len;
+ }
+ pthread_join(reader_thread, NULL);
+
+ writepos = writebuf;
+ }
+
+ last_cyc_write = last_write;