JTAG

Four wires carry every debug request into a chip. One of them steers a small state machine, and that is what lets the other three do everything else.

You Will Be Able To

  • Name the four JTAG wires and say what the TAP controller does with each.
  • Follow one scan through capture, shift and update, and rebuild a word from its bits.
  • Predict who answers first, and how many shifts a scan needs, when two chips share the cable.
Lesson
S8.2
Time
27 min
Runs on
Your browser
Check
5 questions

You plug the debug probe into a new board, and the software reports a chip you have never heard of. Nothing is broken. A second chip now shares the cable with ours, and you have just read its name.

Before You Start

You need What a Debugger Needs From the Chip: the debug probe, serial bits on a few pins, and the debug transport.

You need hexadecimal and bit numbers from Addresses and Memory-Mapped I/O. Bit 0 is the rightmost bit of a word.

Five questions this page answers
Question
1What does each of the four JTAG wires do?
2How does a chip know whether the bits on its input are an instruction or data?
3What happens in the capture, shift and update steps of a scan?
4Which bit of a register leaves the chip first?
5Two chips share one cable. Who answers first, and how many shifts does a scan need?

The Idea: One Wire Steers, One Wire Carries

A debug request is a word of many bits, and pins are few. So the bits travel one at a time, through a shift register. That is a row of flip-flops in which every bit moves one place on each clock edge. One bit enters at one end. One bit leaves at the other.

That gives a new problem. The chip has several registers that the probe may want. One names the chip. One selects what to do. One carries a debug request. With one data wire in, how does the chip know where the bits belong?

JTAG solves it with a second wire that carries no data at all. It steers a small state machine in the chip, one move on each clock edge. The state says what the data wire means right now: an instruction, data, or nothing.

Without that wire, every register would need pins of its own. With it, four wires reach any number of registers. The price is paid in clock edges: the probe must walk the state machine to the right place before any bit moves.

New Words

New words on this page
WordMeaning
JTAGA port of four wires for test and debug, named after the Joint Test Action Group that defined it.
TCKTest clock. Driven by the probe. Every move happens on one of its edges.
TMSTest mode select. Driven by the probe. It steers the state machine: one move on each rising edge of TCK.
TDITest data in. One bit enters the chip on each shift.
TDOTest data out. One bit leaves the chip on each shift.
TAPTest access port: the four pins and the logic behind them.
TAP controllerThe state machine of the TAP. It has sixteen states.
JTAG scanOne pass through three steps, capture, shift and update, on one register of the TAP. This page calls it a scan.
Instruction register (IR)A short shift register. Its content selects which register the next data scan will use.
JTAG data register (DR)One of several shift registers behind the TAP. The instruction selects one of them.
IDCODEA JTAG data register of 32 bits that names the chip. Its bit 0 is always 1.
BYPASSA JTAG data register of one bit. It makes a chip as short as possible for a scan meant for another chip.
JTAG chainSeveral chips on one cable: the TDO of one feeds the TDI of the next.

Some boards add a fifth wire, a reset for the TAP. It is optional and our lab does not use it.

One Picture, in Two Parts: The Sixteen States

The first part is the steering. From Run-Test/Idle, a 1 on TMS moves to Select-DR-Scan. There a 0 enters the data branch, and one more 1 moves on to the instruction branch.

Choose a Scan BranchStart at Test-Logic-Reset. Each arrow label gives TMS at the next rising edge.
  • Clock / reset
  • Control / FSM
  • Memory
  • Debug
Choose a Scan BranchStart at Test-Logic-Reset. Each arrow label gives TMS at the next rising edge. state Test-Logic-Reset 1 stays here state Run-Test/Idle 0 stays here state Select-DR-Scan 0 enters Capture-DR state Select-IR-Scan 0 enters Capture-IR 0111
  1. state Run-Test/Idle0 stays here
    • 1 → Select-DR-Scan
  2. state Select-DR-Scan0 enters Capture-DR
    • 1 → Select-IR-Scan
  3. state Select-IR-Scan0 enters Capture-IR
    • 1 → Test-Logic-Reset
  4. state Test-Logic-Reset1 stays here
    • 0 → Run-Test/Idle
  1. Choose data

    From Idle take the first select. A low now enters the data branch.

  2. Choose instruction

    One more high reaches the instruction select. A low there enters Capture-IR.

Read the diagram as text
  • Test-Logic-Reset: 1 stays here
  • Run-Test/Idle: 0 stays here
  • Select-DR-Scan: 0 enters Capture-DR
  • Select-IR-Scan: 0 enters Capture-IR
  • Test-Logic-Reset → Run-Test/Idle: 0
  • Run-Test/Idle → Select-DR-Scan: 1
  • Select-DR-Scan → Select-IR-Scan: 1
  • Select-IR-Scan → Test-Logic-Reset: 1

Four steering states. Follow either capture exit into the matching branch below.

The second part is one branch. The data branch and the instruction branch have the same six states, so each card carries both names.

Walk the Data or Instruction BranchRead the first name on each card for a DR scan and the second for an IR scan.
  • Memory
  • Bus
  • Control / FSM
  • Debug
Walk the Data or Instruction BranchRead the first name on each card for a DR scan and the second for an IR scan. state Capture-DR /Capture-IR Load a snapshot state Shift-DR /Shift-IR 0 shifts and stayshere state Exit1-DR /Exit1-IR Pause or finish state Pause-DR /Pause-IR 0 holds here state Exit2-DR /Exit2-IR Resume or finish state Update-DR /Update-IR 0 to Idle, 1 toSelect-DR 01101101
  1. state Capture-DR / Capture-IRLoad a snapshot
    • 0 → Shift-DR / Shift-IR
    • 1 → Exit1-DR / Exit1-IR
  2. state Shift-DR / Shift-IR0 shifts and stays here
    • 1 → Exit1-DR / Exit1-IR
  3. state Exit1-DR / Exit1-IRPause or finish
    • 0 → Pause-DR / Pause-IR
    • 1 → Update-DR / Update-IR
  4. state Pause-DR / Pause-IR0 holds here
    • 1 → Exit2-DR / Exit2-IR
  5. state Exit2-DR / Exit2-IRResume or finish
    • 0 → Shift-DR / Shift-IR
    • 1 → Update-DR / Update-IR
  6. state Update-DR / Update-IR0 to Idle, 1 to Select-DR
  1. Trade bits

    Leave capture on a low. Each rising edge in Shift exchanges one bit.

  2. Park the scan

    The edge leaving Shift transfers a final bit. Take the pause exit to preserve the partial word.

  3. Pick up again

    Exit2 can return straight to Shift. Nothing recaptures the data.

  4. Finish

    Either exit can reach Update. Both branches return to Idle on zero or Select-DR on one.

Read the diagram as text
  • Capture-DR / Capture-IR: Load a snapshot
  • Shift-DR / Shift-IR: 0 shifts and stays here
  • Exit1-DR / Exit1-IR: Pause or finish
  • Pause-DR / Pause-IR: 0 holds here
  • Exit2-DR / Exit2-IR: Resume or finish
  • Update-DR / Update-IR: 0 to Idle, 1 to Select-DR
  • Capture-DR / Capture-IR → Shift-DR / Shift-IR: 0
  • Capture-DR / Capture-IR → Exit1-DR / Exit1-IR: 1
  • Shift-DR / Shift-IR → Exit1-DR / Exit1-IR: 1
  • Exit1-DR / Exit1-IR → Pause-DR / Pause-IR: 0
  • Exit1-DR / Exit1-IR → Update-DR / Update-IR: 1
  • Pause-DR / Pause-IR → Exit2-DR / Exit2-IR: 1
  • Exit2-DR / Exit2-IR → Shift-DR / Shift-IR: 0
  • Exit2-DR / Exit2-IR → Update-DR / Update-IR: 1

Six paired cards represent twelve distinct states. Arrow labels give TMS. Card details include self loops and exits.

Three of those states do the work. Capture loads a fresh word into the shift register. Shift moves one bit out on TDO and one bit in from TDI, on every edge. Update hands the word that was shifted in to the logic that uses it.

The Smallest Example, Worked by Hand

Read the low hex digit of an IDCODE. No simulator yet. The chip holds the ID 142000b1. Its low digit is 1, which is 0001 in binary. After Capture-DR the shift register holds the ID, and bit 0 sits at the TDO end.

Four shifts, by hand
ShiftBit on TDOWhere the probe puts itThe probe has so far
01bit 0???1
10bit 1??01
20bit 2?001
30bit 30001

The bits arrive as 1, 0, 0, 0. Written in order of arrival they look like 1000. The probe does not write them that way: the first bit out is bit 0, so the digit is 0001.

Now put two chips on the cable. The TDI of the probe enters our chip. The TDO of our chip enters a neighbour. The TDO of the neighbour goes back to the probe. Both see the same TCK and TMS, so both walk the same states.

Three scans of the chain, by hand
Our chip selectsThe neighbour selectsShifts neededWhat comes out first
IDCODE, 32 bitsIDCODE, 32 bits64The ID of the neighbour, then ours
IDCODE, 32 bitsBYPASS, 1 bit33One 0 from the neighbour, then our ID
BYPASS, 1 bitBYPASS, 1 bitas many as you sendTwo 0 bits, then your own bits, two shifts late

Take the last row. Send the five bits 00101, bit 0 first: 1, 0, 1, 0, 0. Two captured 0 bits come out first. Then come your first three bits: 1, 0, 1. The probe has received 0, 0, 1, 0, 1, which it writes as 10100. Your last two bits are still inside the chips.

The Rules

  1. Rule 1: TMS chooses the next state on every rising edge of TCK.

    Five edges with TMS at 1 reach Test-Logic-Reset from any state.

  2. Rule 2: The instruction selects one JTAG data register.

    A new instruction takes effect in Update-IR. Until then the old one stands.

  3. Rule 3: Capture loads, Shift exchanges, Update hands over.

    Each shift edge moves one bit out on TDO and one bit in from TDI.

  4. Rule 4: The edge that leaves Shift still shifts.

    So TMS goes to 1 together with the last bit, not after it.

  5. Rule 5: Bit 0 leaves first.

    The probe places the bits in order of arrival, from bit 0 upward.

  6. Rule 6: In a chain the selected registers form one long shift register.

    Their lengths add. The chip nearest the TDO of the probe comes out first.

The same four wires also test the pins of a board, which is where they began. The lesson on pads and the board returns to that.

Sources for These Rules

JTAG is IEEE Std 1149.1. That standard is not in our reference library. The pin names, the sixteen states, IDCODE with its bit 0 at 1, BYPASS and the order of bits are well-known facts about it. They were not checked against its text.

Boundary scan, the test access port and chips connected in series on a board: Weste and Harris, CMOS VLSI Design, fourth edition, section 15.7.

Shift registers: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 5.4.2.

The two IDs, the instruction register of two bits, the opcode 01 for IDCODE and the order of the two chips are choices of our lab.

Now Run It

The lab at the end of this page is a board with two TAPs. One is ours. The other is a neighbour, between us and the TDO of the probe. Test code plays the probe. It runs three scans.

Printed by the lab on Icarus. IDs are in hexadecimal.
shift 0 tdo=1 outgoing=142000b1
shift 1 tdo=0 outgoing=8a100058
shift 2 tdo=0 outgoing=4508002c
shift 3 tdo=0 outgoing=a2840016
reset scan near_TDO=142000b1 near_TDI=2a5310c5
mixed bypass=0 upstream_ID=2a5310c5
two bypass cells sent=00101 received=10100
Four Bits Arrive at the Probe
0123clkTDOoutgoing142000b18a1000584508002ca2840016received???1??01?0010001
Scroll horizontally ↔

Recorded TDO and target register before each shift edge. The received lane places those samples in host bit positions.

Rule 5 at work, in the rows shift 0 to shift 3. TDO reads 1, 0, 0, 0 while the register holds 142000b1. The probe builds 0001, the low digit of that ID.

Rule 6, in the row "reset scan". After a reset both chips select IDCODE. The scan of 64 bits returns 142000b1 first. That is the neighbour, the chip nearest TDO. It was the unknown name on your screen.

Rule 6 again, in the row "mixed". The neighbour is in BYPASS, so one 0 comes first and then our ID, 2a5310c5. That is 33 shifts. Count 32 and every bit of the ID lands one place off.

The last row is the hand example: 00101 sent, 10100 received.

This lab leaves things out. Each TAP has only IDCODE and BYPASS, and an instruction register of two bits. No processor is behind them. A real debug transport adds registers that carry debug requests.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
One steering wire and a state machineA pin for each register and each stepFour pins, however many registers. Walking the states costs clock edges.
Chips in series on one cableA cable for each chipOne connector on the board. Scans grow longer, and one dead chip breaks the chain.
A BYPASS register of one bitEvery chip always shifts its longest registerA scan for one chip crosses the others in one bit each. The probe must count those bits.

The Trap: One Shift Too Many

A new probe script reads every ID shifted by one place. Its author sent all 32 bits with TMS at 0, and then raised TMS to leave. That last edge shifted once more. Rule 4 is the fix: raise TMS together with bit 31.

The second trap is the chain. A script that worked on a board with one chip reads nonsense on the next board. A second chip was added, in BYPASS. Its one bit was not counted.

The third trap is the order of the bits. Someone prints the bits as they arrive and reads the string as a number. A good board is then reported as an unknown one.

The last trap is the reset. Five edges with TMS at 1 reset the TAP, and nothing else. The processor and its program run on. To restart those, the debugger must ask for it by a request of its own.

At Scale

This page2 TAPs
A large chiptens of TAPs

This section looks ahead. Two chips on a cable become many TAPs inside one large chip.

  1. Count the TAPs on the path

    A large chip has TAPs inside its blocks. The length of a scan depends on which of them are on the path now.

  2. Measure the path

    Put every TAP in BYPASS and send a marker. The number of shifts before it returns is the number of TAPs.

  3. A block without power

    A TAP in a block that is switched off does not shift. Everything behind it is cut off, unless the path can go around it.

  4. Two wires in place of four

    Small chips cannot spare four pins. Other debug ports carry the same requests on two wires. The debug chain stays the same.

Try It

Run the board model. Compare the first four values of tdo with the low hex digit of the ID of the chip nearest TDO.

Change the ID of the neighbour (the parameter ID of near_tdo). Keep its bit 0 at 1. Which half of the "reset scan" row changes?

Change the marker of five bits. Predict which of its bits are still inside the two chips when the scan ends.

Names Printed by the Lab
NameMeaning
shift 0 to shift 3The first four shift edges of the first scan.
tdoThe bit on TDO that the probe samples on that edge.
outgoingThe shift register of the chip nearest TDO, before the edge.
near_TDO, near_TDIThe two chips of the board. near_TDI is our chip. near_TDO is its neighbour.
reset scanA scan of 64 bits just after a reset of both TAPs.
mixedA scan of 33 bits with the neighbour in BYPASS.
two bypass cellsA scan of 5 bits with both chips in BYPASS.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: shift 0 tdo=1 outgoing=142000b1 shift 1 tdo=0 outgoing=8a100058 shift 2 tdo=0 outgoing=4508002c shift 3 tdo=0 outgoing=a2840016 reset scan near_TDO=142000b1 near_TDI=2a5310c5 mixed bypass=0 upstream_ID=2a5310c5 two bypass cells sent=00101 received=10100
Read included file: soc-s81-tap.sv
// Small board model. DR and IR share the same capture/shift/pause sequencer.
// Only IDCODE and BYPASS have data paths in this fixture.
module board_tap #(parameter logic [31:0] ID = 32'h2a53_10c5) (
  input logic tck, tms, tdi, output logic tdo
);
  typedef enum {RESET, IDLE, SELECT_DR, SELECT_IR, CAPTURE, SHIFT,
                EXIT1, PAUSE_SCAN, EXIT2, UPDATE_SCAN} phase_t;
  phase_t phase = RESET;
  logic ir_branch = 0;
  logic [1:0] opcode = 1, ir_shift;
  logic [31:0] data_shift;
  always @(posedge tck) begin
    case (phase)
      RESET: if (!tms) phase <= IDLE;
      IDLE: if (tms) phase <= SELECT_DR;
      SELECT_DR: if (tms) phase <= SELECT_IR;
                 else begin phase <= CAPTURE; ir_branch <= 0; end
      SELECT_IR: if (tms) phase <= RESET;
                 else begin phase <= CAPTURE; ir_branch <= 1; end
      CAPTURE: begin
        if (ir_branch) ir_shift <= 2'b01;
        else data_shift <= (opcode == 1) ? ID : 0;
        if (tms) phase <= EXIT1; else phase <= SHIFT;
      end
      SHIFT: begin
        if (ir_branch) ir_shift <= {tdi, ir_shift[1]};
        else if (opcode == 1) data_shift <= {tdi, data_shift[31:1]};
        else data_shift[0] <= tdi;
        if (tms) phase <= EXIT1;
      end
      EXIT1: if (tms) phase <= UPDATE_SCAN; else phase <= PAUSE_SCAN;
      PAUSE_SCAN: if (tms) phase <= EXIT2;
      EXIT2: if (tms) phase <= UPDATE_SCAN; else phase <= SHIFT;
      UPDATE_SCAN: if (tms) phase <= SELECT_DR; else phase <= IDLE;
    endcase
  end
  always @(negedge tck) begin
    if (phase == RESET) opcode <= 1;
    if (phase == UPDATE_SCAN && ir_branch) opcode <= ir_shift;
    if (phase == SHIFT) tdo <= ir_branch ? ir_shift[0] : data_shift[0];
    else tdo <= 1'bz;
  end
endmodule
Read included file: soc-s81-tl9.svh
// Include in a bench with tck, tms, tdi and tdo. Begin each scan in Idle.
task automatic pulse(input bit mode, serial_in);
  tms = mode;
  tdi = serial_in;
  #5 tck = 1;
  #5 tck = 0;
  #1;
endtask

task automatic reset_tap;
  repeat (5) pulse(1, 0);
  pulse(0, 0);
endtask

task automatic scan(input bit instruction, input int width,
                    input logic [63:0] outgoing, output logic [63:0] incoming);
  incoming = 0;
  pulse(1, 0);
  if (instruction) pulse(1, 0);
  pulse(0, 0);
  pulse(0, 0);
  for (int n = 0; n < width; n++) begin
    incoming[n] = tdo;
    pulse(n == width-1, outgoing[n]);
  end
  pulse(1, 0);
  pulse(0, 0);
endtask

task automatic read_register(input int ir_width, data_width,
                             input logic [63:0] selection, output logic [63:0] value);
  logic [63:0] discard;
  scan(1, ir_width, selection, discard);
  scan(0, data_width, 0, value);
endtask

Check Yourself

Predict the outputIn this run the marker of five bits is 00011. Both chips are in BYPASS. What is the last row the board prints?
module tb;
  bit tck, tms, tdi;
  wire middle, tdo;
  board_tap near_tdi(tck,tms,tdi,middle);
  board_tap #(.ID(32'h1420_00b1)) near_tdo(tck,tms,middle,tdo);
  `include "soc-s81-tl9.svh"
  logic [63:0] value;
  localparam logic [4:0] MARKER = 5'b00011;
  int bit_number;
  always @(posedge tck) begin
    if (near_tdo.phase == near_tdo.SHIFT && !near_tdo.ir_branch) begin
      if (bit_number < 4) $display("shift %0d tdo=%b outgoing=%08h",bit_number,tdo,near_tdo.data_shift);
      bit_number++;
    end
  end
  initial begin
    reset_tap;
    scan(0,64,0,value);
    $display("reset scan near_TDO=%08h near_TDI=%08h",value[31:0],value[63:32]);
    // Bits for the chip nearest TDO leave the host first.
    read_register(4,33,4'b0111,value);
    $display("mixed bypass=%b upstream_ID=%08h",value[0],value[32:1]);
    scan(1,4,4'b1111,value);
    scan(0,5,MARKER,value);
    $display("two bypass cells sent=%05b received=%05b",MARKER,value[4:0]);
    $finish;
  end
endmodule
Read included file: soc-s81-tap.sv
// Small board model. DR and IR share the same capture/shift/pause sequencer.
// Only IDCODE and BYPASS have data paths in this fixture.
module board_tap #(parameter logic [31:0] ID = 32'h2a53_10c5) (
  input logic tck, tms, tdi, output logic tdo
);
  typedef enum {RESET, IDLE, SELECT_DR, SELECT_IR, CAPTURE, SHIFT,
                EXIT1, PAUSE_SCAN, EXIT2, UPDATE_SCAN} phase_t;
  phase_t phase = RESET;
  logic ir_branch = 0;
  logic [1:0] opcode = 1, ir_shift;
  logic [31:0] data_shift;
  always @(posedge tck) begin
    case (phase)
      RESET: if (!tms) phase <= IDLE;
      IDLE: if (tms) phase <= SELECT_DR;
      SELECT_DR: if (tms) phase <= SELECT_IR;
                 else begin phase <= CAPTURE; ir_branch <= 0; end
      SELECT_IR: if (tms) phase <= RESET;
                 else begin phase <= CAPTURE; ir_branch <= 1; end
      CAPTURE: begin
        if (ir_branch) ir_shift <= 2'b01;
        else data_shift <= (opcode == 1) ? ID : 0;
        if (tms) phase <= EXIT1; else phase <= SHIFT;
      end
      SHIFT: begin
        if (ir_branch) ir_shift <= {tdi, ir_shift[1]};
        else if (opcode == 1) data_shift <= {tdi, data_shift[31:1]};
        else data_shift[0] <= tdi;
        if (tms) phase <= EXIT1;
      end
      EXIT1: if (tms) phase <= UPDATE_SCAN; else phase <= PAUSE_SCAN;
      PAUSE_SCAN: if (tms) phase <= EXIT2;
      EXIT2: if (tms) phase <= UPDATE_SCAN; else phase <= SHIFT;
      UPDATE_SCAN: if (tms) phase <= SELECT_DR; else phase <= IDLE;
    endcase
  end
  always @(negedge tck) begin
    if (phase == RESET) opcode <= 1;
    if (phase == UPDATE_SCAN && ir_branch) opcode <= ir_shift;
    if (phase == SHIFT) tdo <= ir_branch ? ir_shift[0] : data_shift[0];
    else tdo <= 1'bz;
  end
endmodule
Read included file: soc-s81-tl9.svh
// Include in a bench with tck, tms, tdi and tdo. Begin each scan in Idle.
task automatic pulse(input bit mode, serial_in);
  tms = mode;
  tdi = serial_in;
  #5 tck = 1;
  #5 tck = 0;
  #1;
endtask

task automatic reset_tap;
  repeat (5) pulse(1, 0);
  pulse(0, 0);
endtask

task automatic scan(input bit instruction, input int width,
                    input logic [63:0] outgoing, output logic [63:0] incoming);
  incoming = 0;
  pulse(1, 0);
  if (instruction) pulse(1, 0);
  pulse(0, 0);
  pulse(0, 0);
  for (int n = 0; n < width; n++) begin
    incoming[n] = tdo;
    pulse(n == width-1, outgoing[n]);
  end
  pulse(1, 0);
  pulse(0, 0);
endtask

task automatic read_register(input int ir_width, data_width,
                             input logic [63:0] selection, output logic [63:0] value);
  logic [63:0] discard;
  scan(1, ir_width, selection, discard);
  scan(0, data_width, 0, value);
endtask
ConceptWhich wire decides the next state of the TAP controller?
ConceptHow does a chip know whether the bits on TDI are an instruction or data?
ConceptWhat does Update do at the end of a scan?
ConceptTwo chips share one cable and both select IDCODE. Whose ID reaches the probe first, and after how many shifts is the scan complete?

Common Mistakes

Wrong Send one more clock edge after the last bit to leave Shift.

The edge that leaves Shift still shifts.

Fix Set TMS to 1 together with the last bit.

Wrong Count a chip in BYPASS as zero bits.

Its register of one bit is still in the path.

Fix Add one shift for each chip in BYPASS.

Wrong Expect a reset of the TAP to restart the program.

It resets only the TAP controller.

Fix Ask which reset reaches the block you need.

Remember

  • TCK clocks, TMS steers, TDI carries bits in and TDO carries bits out.
  • A scan is capture, shift and update. Bit 0 leaves first.
  • The edge that leaves Shift still shifts.
  • In a JTAG chain the lengths of the selected registers add, and the chip nearest TDO answers first.

Practice

Report a problem with this page