Creating JTAG interface with GPIO seems to have glitches

I'm using the jtagbang code that is used to create a JTAG interface using GPIO.
For some strange reason the JTAG TAP SM transitions from
test_logic_reset -> run_test_idle (with TMS=0) but then the JTAG TAP SM
changes to pauseDR state and then to shiftDR state.
It seems like there is some kind of glitching occurring on
Tck or Tms or both outputs to allow such a weird transition.

  1. How to ensure all serial cmds sent to JTAG I/O are really working properly ?
  2. How to preflush or preinitialize the serial port or GPIO ports for JTAG IO ?
    I tried to use
    void setup (
    pinMode(LED, OUTPUT);
    pinMode(TCK, OUTPUT);
    pinMode(TDI, OUTPUT); // our output, their input
    pinMode(TMS, OUTPUT);
    pinMode(TDO, INPUT); // their output, our input
    pinMode(CTS, OUTPUT);

// but it doesn't make a difference.
digitalWrite(TCK, LOW);
digitalWrite(TDI, LOW);
digitalWrite(TMS, HIGH);
Serial.print("Set all GPIO output to zeroes\n");