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.
| Question | |
|---|---|
| 1 | What does each of the four JTAG wires do? |
| 2 | How does a chip know whether the bits on its input are an instruction or data? |
| 3 | What happens in the capture, shift and update steps of a scan? |
| 4 | Which bit of a register leaves the chip first? |
| 5 | Two 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
| Word | Meaning |
|---|---|
| JTAG | A port of four wires for test and debug, named after the Joint Test Action Group that defined it. |
| TCK | Test clock. Driven by the probe. Every move happens on one of its edges. |
| TMS | Test mode select. Driven by the probe. It steers the state machine: one move on each rising edge of TCK. |
| TDI | Test data in. One bit enters the chip on each shift. |
| TDO | Test data out. One bit leaves the chip on each shift. |
| TAP | Test access port: the four pins and the logic behind them. |
| TAP controller | The state machine of the TAP. It has sixteen states. |
| JTAG scan | One 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. |
| IDCODE | A JTAG data register of 32 bits that names the chip. Its bit 0 is always 1. |
| BYPASS | A JTAG data register of one bit. It makes a chip as short as possible for a scan meant for another chip. |
| JTAG chain | Several 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.
- Clock / reset
- Control / FSM
- Memory
- Debug
- Choose data
From Idle take the first select. A low now enters the data branch.
- 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.
- Memory
- Bus
- Control / FSM
- Debug
- Trade bits
Leave capture on a low. Each rising edge in Shift exchanges one bit.
- Park the scan
The edge leaving Shift transfers a final bit. Take the pause exit to preserve the partial word.
- Pick up again
Exit2 can return straight to Shift. Nothing recaptures the data.
- 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.
| Shift | Bit on TDO | Where the probe puts it | The probe has so far |
|---|---|---|---|
| 0 | 1 | bit 0 | ???1 |
| 1 | 0 | bit 1 | ??01 |
| 2 | 0 | bit 2 | ?001 |
| 3 | 0 | bit 3 | 0001 |
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.
| Our chip selects | The neighbour selects | Shifts needed | What comes out first |
|---|---|---|---|
| IDCODE, 32 bits | IDCODE, 32 bits | 64 | The ID of the neighbour, then ours |
| IDCODE, 32 bits | BYPASS, 1 bit | 33 | One 0 from the neighbour, then our ID |
| BYPASS, 1 bit | BYPASS, 1 bit | as many as you send | Two 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
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.
Rule 2: The instruction selects one JTAG data register.
A new instruction takes effect in Update-IR. Until then the old one stands.
Rule 3: Capture loads, Shift exchanges, Update hands over.
Each shift edge moves one bit out on TDO and one bit in from TDI.
Rule 4: The edge that leaves Shift still shifts.
So TMS goes to 1 together with the last bit, not after it.
Rule 5: Bit 0 leaves first.
The probe places the bits in order of arrival, from bit 0 upward.
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.
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=10100Recorded 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| One steering wire and a state machine | A pin for each register and each step | Four pins, however many registers. Walking the states costs clock edges. |
| Chips in series on one cable | A cable for each chip | One connector on the board. Scans grow longer, and one dead chip breaks the chain. |
| A BYPASS register of one bit | Every chip always shifts its longest register | A 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 section looks ahead. Two chips on a cable become many TAPs inside one large chip.
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.
Measure the path
Put every TAP in BYPASS and send a marker. The number of shifts before it returns is the number of TAPs.
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.
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
| Name | Meaning |
|---|---|
| shift 0 to shift 3 | The first four shift edges of the first scan. |
| tdo | The bit on TDO that the probe samples on that edge. |
| outgoing | The shift register of the chip nearest TDO, before the edge. |
| near_TDO, near_TDI | The two chips of the board. near_TDI is our chip. near_TDO is its neighbour. |
| reset scan | A scan of 64 bits just after a reset of both TAPs. |
| mixed | A scan of 33 bits with the neighbour in BYPASS. |
| two bypass cells | A scan of 5 bits with both chips in BYPASS. |
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=10100Read 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
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.