-}
-
-/* Goes to the end state. */
-static void vsllink_state_move_normal(void)
-{
- if (vsllink_tms_data_len > 0)
- {
- vsllink_append_tms();
- }
- else
- {
- vsllink_tap_ensure_space(0, 2);
-
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
- }
-
- tap_set_state(tap_get_end_state());
-}
-static void vsllink_state_move_dma(void)
-{
- int i, insert_length = (tap_length % 8) ? (8 - (tap_length % 8)) : 0;
- struct insert_insignificant_operation *insert = \
- &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
- uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
-
- if (tap_get_state() == TAP_RESET)
- {
- vsllink_tap_ensure_space(0, 8);
-
- for (i = 0; i < 8; i++)
- {
- vsllink_tap_append_step(1, 0);
- }
- }
-
- if (insert_length > 0)
- {
- vsllink_tap_ensure_space(0, 16);
-
- for (i = 0; i < insert->insert_position; i++)
- {
- vsllink_tap_append_step((tms_scan >> i) & 1, 0);
- }
- for (i = 0; i < insert_length; i++)
- {
- vsllink_tap_append_step(insert->insert_value, 0);
- }
- for (i = insert->insert_position; i < 8; i++)
- {
- vsllink_tap_append_step((tms_scan >> i) & 1, 0);
- }
- }
- else
- {
- vsllink_tap_ensure_space(0, 8);
-
- for (i = 0; i < 8; i++)
- {
- vsllink_tap_append_step((tms_scan >> i) & 1, 0);
- }
- }
-
- tap_set_state(tap_get_end_state());
-}
-
-// write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
-static void vsllink_add_path(int start, int num, tap_state_t *path)
-{
- int i;
-
- for (i = start; i < (start + num); i++)
- {
- if ((i & 7) == 0)
- {
- if (i > 0)
- {
- vsllink_usb_out_buffer_idx++;
- }
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
- }
-
- if (path[i - start] == tap_state_transition(tap_get_state(), true))
- {
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
- }
- else if (path[i - start] == tap_state_transition(tap_get_state(), false))
- {
- // nothing to do
- }
- else
- {
- LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
- exit(-1);
- }
- tap_set_state(path[i - start]);
- }
- if ((i > 0) && ((i & 7) == 0))
- {
- vsllink_usb_out_buffer_idx++;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
- }
-
- tap_set_end_state(tap_get_state());
-}
-
-static void vsllink_path_move_normal(int num_states, tap_state_t *path)
-{
- int i, tms_len, tms_cmd_pos, path_idx = 0;
-
- if (vsllink_tms_data_len > 0)
- {
- // there are vsllink_tms_data_len more tms bits to be shifted
- // so there are vsllink_tms_data_len + num_states tms bits in all
- tms_len = vsllink_tms_data_len + num_states;
- if (tms_len <= 16)
- {
- // merge into last tms shift
- if (tms_len < 8)
- {
- // just append tms data to the last tms byte
- vsllink_add_path(vsllink_tms_data_len, num_states, path);
- }
- else if (tms_len == 8)
- {
- // end last tms shift command
- (*vsllink_tms_cmd_pos)--;
- vsllink_add_path(vsllink_tms_data_len, num_states, path);
- }
- else if (tms_len < 16)
- {
- if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
- {
- // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
- // there is enought tms length in the current tms shift command
- (*vsllink_tms_cmd_pos)++;
- vsllink_add_path(vsllink_tms_data_len, num_states, path);
- }
- else
- {
- // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
- // not enough tms length in the current tms shift command
- // so a new command should be added
- // first decrease byte length of last tms shift command
- (*vsllink_tms_cmd_pos)--;
- // append tms data to the last tms byte
- vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
- path += 8 - vsllink_tms_data_len;
- // add new command(3 bytes)
- vsllink_tap_ensure_space(0, 3);
- vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
- vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
- }
- }
- else if (tms_len == 16)
- {
- // end last tms shift command
- vsllink_add_path(vsllink_tms_data_len, num_states, path);
- }
-
- vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
- if (vsllink_tms_data_len == 0)
- {
- vsllink_tms_cmd_pos = NULL;
- }
- num_states = 0;
- }
- else
- {
- vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
-
- path += 16 - vsllink_tms_data_len;
- num_states -= 16 - vsllink_tms_data_len;
- vsllink_tms_data_len = 0;
- vsllink_tms_cmd_pos = NULL;
- }
- }
-
- if (num_states > 0)
- {
- // Normal operation, don't need to append tms data
- vsllink_tms_data_len = num_states & 7;
-
- while (num_states > 0)
- {
- if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
- {
- i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
- }
- else
- {
- i = num_states;
- }
- tms_len = (i + 7) >> 3;
- vsllink_tap_ensure_space(0, tms_len + 2);
- tms_cmd_pos = vsllink_usb_out_buffer_idx;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
-
- vsllink_add_path(0, i, path + path_idx);
-
- path_idx += i;
- num_states -= i;
- }
-
- if (vsllink_tms_data_len > 0)
- {
- if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
- {
- vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
- (*vsllink_tms_cmd_pos)++;
- }
- else
- {
- vsllink_usb_out_buffer[tms_cmd_pos]--;
-
- tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
- vsllink_tap_ensure_space(0, 3);
- vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
- }
- }
- }
-}
-static void vsllink_path_move_dma(int num_states, tap_state_t *path)
-{
- int i, j = 0;
-
- if (tap_length & 7)
- {
- if ((8 - (tap_length & 7)) < num_states)
- {
- j = 8 - (tap_length & 7);
- }
- else
- {
- j = num_states;
- }
- for (i = 0; i < j; i++)
- {
- if (path[i] == tap_state_transition(tap_get_state(), false))
- {
- vsllink_tap_append_step(0, 0);
- }
- else if (path[i] == tap_state_transition(tap_get_state(), true))
- {
- vsllink_tap_append_step(1, 0);
- }
- else
- {
- LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
- exit(-1);
- }
- tap_set_state(path[i]);
- }
- num_states -= j;
- }
-
- if (num_states > 0)
- {
- vsllink_tap_ensure_space(0, num_states);
-
- for (i = 0; i < num_states; i++)
- {
- if (path[j + i] == tap_state_transition(tap_get_state(), false))
- {
- vsllink_tap_append_step(0, 0);
- }
- else if (path[j + i] == tap_state_transition(tap_get_state(), true))
- {
- vsllink_tap_append_step(1, 0);
- }
- else
- {
- LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
- exit(-1);
- }
- tap_set_state(path[j + i]);
- }
- }
-
- tap_set_end_state(tap_get_state());
-}
-
-static void vsllink_stableclocks_normal(int num_cycles, int tms)
-{
- int tms_len;
- uint16_t tms_append_byte;
-
- if (vsllink_tms_data_len > 0)
- {
- // there are vsllink_tms_data_len more tms bits to be shifted
- // so there are vsllink_tms_data_len + num_cycles tms bits in all
- tms_len = vsllink_tms_data_len + num_cycles;
- if (tms > 0)
- {
- // append '1' for tms
- tms_append_byte = (uint16_t)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
- }
- else
- {
- // append '0' for tms
- tms_append_byte = 0;
- }
- if (tms_len <= 16)
- {
- // merge into last tms shift
- if (tms_len < 8)
- {
- // just add to vsllink_tms_data_len
- // same result if tun through
- //vsllink_tms_data_len += num_cycles;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (uint8_t)(tms_append_byte & 0xFF);
- }
- else if (tms_len == 8)
- {
- // end last tms shift command
- // just reduce it, and append last tms byte
- (*vsllink_tms_cmd_pos)--;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
- }
- else if (tms_len < 16)
- {
- if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
- {
- // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
- // there is enought tms length in the current tms shift command
- // increase the tms byte length by 1 and set the last byte to 0
- (*vsllink_tms_cmd_pos)++;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
- }
- else
- {
- // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
- // not enough tms length in the current tms shift command
- // so a new command should be added
- // first decrease byte length of last tms shift command
- (*vsllink_tms_cmd_pos)--;
- // append last tms byte and move the command pointer to the next empty position
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
- // add new command(3 bytes)
- vsllink_tap_ensure_space(0, 3);
- vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
- }
- }
- else if (tms_len == 16)
- {
- // end last tms shift command
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
- }
-
- vsllink_tms_data_len = tms_len & 7;
- if (vsllink_tms_data_len == 0)
- {
- vsllink_tms_cmd_pos = NULL;
- }
- num_cycles = 0;
- }
- else
- {
- // more shifts will be needed
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
-
- num_cycles -= 16 - vsllink_tms_data_len;
- vsllink_tms_data_len = 0;
- vsllink_tms_cmd_pos = NULL;
- }
- }
- // from here vsllink_tms_data_len == 0 or num_cycles == 0
-
- if (vsllink_tms_data_len > 0)
- {
- // num_cycles == 0
- // no need to shift
- if (num_cycles > 0)
- {
- LOG_ERROR("There MUST be some bugs in the driver");
- exit(-1);
- }
- }
- else
- {
- // get number of bytes left to be sent
- tms_len = num_cycles >> 3;
- if (tms_len > 0)
- {
- vsllink_tap_ensure_space(1, 5);
- // if tms_len > 0, vsllink_tms_data_len == 0
- // so just add new command
- // LSB of the command byte is the tms value when do the shifting
- if (tms > 0)
- {
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
- }
- else
- {
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
- }
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
-
- vsllink_usb_in_want_length += 1;
- pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
- pending_scan_results_length++;
-
- if (tms_len > 0xFFFF)
- {
- vsllink_tap_execute();
- }
- }
-
- // post-process
- vsllink_tms_data_len = num_cycles & 7;
- if (vsllink_tms_data_len > 0)
- {
- vsllink_tap_ensure_space(0, 3);
- vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
- if (tms > 0)
- {
- // append '1' for tms
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
- }
- else
- {
- // append '0' for tms
- vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
- }
- }
- }
-}
-static void vsllink_stableclocks_dma(int num_cycles, int tms)
-{
- int i, cur_cycles;
-
- if (tap_length & 7)
- {
- if ((8 - (tap_length & 7)) < num_cycles)
- {
- cur_cycles = 8 - (tap_length & 7);
- }
- else
- {
- cur_cycles = num_cycles;
- }
- for (i = 0; i < cur_cycles; i++)
- {
- vsllink_tap_append_step(tms, 0);
- }
- num_cycles -= cur_cycles;
- }
-
- while (num_cycles > 0)
- {
- if (num_cycles > 8 * tap_buffer_size)
- {
- cur_cycles = 8 * tap_buffer_size;
- }
- else
- {
- cur_cycles = num_cycles;
- }
-
- vsllink_tap_ensure_space(0, cur_cycles);
-
- for (i = 0; i < cur_cycles; i++)
- {
- vsllink_tap_append_step(tms, 0);
- }
-
- num_cycles -= cur_cycles;
- }
-}
-
-static void vsllink_runtest(int num_cycles)
-{
- tap_state_t saved_end_state = tap_get_end_state();
-
- if (tap_get_state() != TAP_IDLE)
- {
- // enter into IDLE state
- vsllink_end_state(TAP_IDLE);
- vsllink_state_move();
- }
-
- vsllink_stableclocks(num_cycles, 0);
-
- // post-process
- // set end_state
- vsllink_end_state(saved_end_state);
- tap_set_state(TAP_IDLE);
- if (tap_get_end_state() != TAP_IDLE)
- {
- vsllink_state_move();
- }
-}
-
-static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
-{
- tap_state_t saved_end_state;
- uint8_t bits_left, tms_tmp, tdi_len;
- int i;
-
- if (0 == scan_size)
- {
- return;
- }
-
- tdi_len = ((scan_size + 7) >> 3);
- if ((tdi_len + 7) > VSLLINK_BufferSize)
- {
- LOG_ERROR("Your implementation of VSLLink has not enough buffer");
- exit(-1);
- }
-
- saved_end_state = tap_get_end_state();