Models of the Outside World

A chip bench needs a board: a UART, an SPI flash, GPIO levels and a JTAG debugger. Learn what each model must do and how little it may do. A model decides whether every byte the chip sends is right.

You Will Be Able To

  • Name the four pad models of soc_tb and the pads each one owns.
  • Read the pad edges of a UART frame, an SPI frame and a GPIO loop.
  • Decide how little a model must model for the tests at hand.
  • Explain why a wrong model turns into a wrong verdict on the chip.
Lesson
V3.4
Time
26 min
Runs on
Your browser
Check
5 questions

Picture a UART test that passes on one chip and fails on the next. The bench's UART model samples each bit at its edge, and the new chip's clock runs a little slow. The chip is fine. The model is the bug, and nothing in the log says so.

The Board Around the ChipEach model plays one device of the board on the chip's pads, and models only what the tests need.
  • Processor
  • External
  • Debug
  • Data
  • Debug
The Board Around the ChipEach model plays one device of the board on the chip's pads, and models only what the tests need. processor ap_soc v1.5 IO0 to IO15, JTAG external UART model send, receive, frameerrors external SPI flash model three commands external GPIO board levels, pulses, IO6 toIO7 debug JTAG host TAP scans IO0 TX, IO1 RXIO2 to IO5any IO padTCK TMS TDI TDO
  1. external UART modelsend, receive, frame errors
    • IO0 TX, IO1 RX → ap_soc v1.5
  2. processor ap_soc v1.5IO0 to IO15, JTAG
  3. external SPI flash modelthree commands
    • IO2 to IO5 → ap_soc v1.5
  4. external GPIO boardlevels, pulses, IO6 to IO7
    • any IO pad → ap_soc v1.5
  5. debug JTAG hostTAP scans
    • TCK TMS TDI TDO → ap_soc v1.5
  1. UART

    Ten bits a frame at the board's bit time. The model sends to RX and checks what TX sends.

  2. SPI flash

    Mode 0, one command byte, 24 reply bits; three commands and a 256-byte image.

  3. GPIO and JTAG

    The board's levels and wires on the IO pads, and a debugger on the four JTAG pads.

Read the diagram as text
  • ap_soc v1.5: IO0 to IO15, JTAG
  • UART model: send, receive, frame errors
  • SPI flash model: three commands
  • GPIO board: levels, pulses, IO6 to IO7
  • JTAG host: TAP scans
  • UART model → ap_soc v1.5: IO0 TX, IO1 RX
  • SPI flash model → ap_soc v1.5: IO2 to IO5
  • GPIO board → ap_soc v1.5: any IO pad
  • JTAG host → ap_soc v1.5: TCK TMS TDI TDO

The models of soc_pads.sv, wired in soc_tb.sv.

Before You Start

Read Back-Door Loading first. The serial blocks themselves are in Serial Peripherals.

Five questions this page answers
Question
1What is a pad model, and which pads does each model of soc_tb own?
2What does a UART frame look like on the pad, and where should a receiver sample it?
3What does the SPI flash model answer, and in which bit timing?
4What must a GPIO pad model decide besides the level?
5How little may a model model?

The Idea: The Board Is Part of the Test

On a desk, the chip talks to real devices: a terminal, a flash chip, buttons, a debugger. On the bench those devices are code, and that code decides whether the chip is right. A UART model that reads a byte wrongly fails a good chip. One that ignores a bad stop bit passes a broken one.

So a model must be exactly right about what it models, and it should model no more than the tests need. Every extra feature is code that can be wrong and that nobody tests. soc_tb's four models are small on purpose.

New Words

New words on this page
WordMeaning
FrameOne UART character on the line: a start bit 0, eight data bits low bit first, a stop bit 1.
Bit timeHow long one UART bit lasts: on ap_soc, DIV cycles of clk_sys (8 cycles = 80 ns in the tests).
Mode 0The SPI timing in which data changes on the falling clock edge and is sampled on the rising edge.
ContentionTwo drivers on one pad at different levels; on a board, two outputs shorted together.
TAPThe test access port behind the JTAG pads; it moves bits in and out on TCK.
ScanOne shift of bits through the TAP, from TDI in and to TDO out.

The Smallest Example: One Frame by Hand

The chip sends U, 0x55, at 80 ns a bit. Write down the line, bit by bit, from the start bit.

The frame of 0x55
BitStarts atLevel
start00
data 0801
data 11600
data 2 to 7240 to 6401 0 1 0 1 0
stop7201

0x55 is 0101 0101; low bit first gives 1, 0, 1, 0, 1, 0, 1, 0.

A receiver finds the falling edge of the start bit and waits half a bit, to its middle. Then it takes one sample a bit time later for each data bit. Sampling in the middle leaves half a bit of room on each side for clocks that differ a little.

The Rules

  1. Rule 1: A model owns pads, and as little else as it can.

    soc_tb states its exceptions. By default the UART model takes its bit time from UART0 DIV. It listens only while PINMUX gives pad 0 to the UART. The board reads the pads' output enables in place of resistors.

  2. Rule 2: Model the protocol exactly where the tests touch it.

    Bit order, sampling point, edges of mode 0, stop bits: the model decides these.

  3. Rule 3: Model nothing the tests do not need.

    The flash answers three commands and a 256-byte image; everything else answers like the identity command.

  4. Rule 4: Report, do not hide.

    A low stop bit is a frame error, and the bench adds it to its error count. The flash counts short frames. soc_tb's board never fights the chip: it lets go of every pad the chip drives.

  5. Rule 5: Let the test choose the board's rate.

    A board UART fixed at its own rate (+uart_div) finds a chip with a wrong divider; one that follows DIV cannot.

Sources for These Rules

The four models and what each models: tools/soc/ap_soc/bench/soc_tb/soc_pads.sv and README.md; facts 27.12.

SPI mode 0 timing as the chip's controller does it: ap_spi.sv and the recorded frame below. Mode numbering is common practice; no standard of the library covers SPI.

IEEE 1149.1 was not fetched for this track (plan D10); the scan format is the chip's, facts 27.10.

The Lab: Four Devices, Seen at the Pads

The four models, wired to the pads, and the UART model's bit time (soc_tb.sv).
  int uart_div = 0;   // +uart_div=<cycles>: the board's bit time. 0: the board follows UART0 DIV
  wire [15:0] uart_period = (dut.u_uart.regs.div_q == 16'h0) ? 16'd1 : dut.u_uart.regs.div_q;
  int uart_bit_ns;
  always_comb uart_bit_ns = 10 * ((uart_div != 0) ? uart_div : int'(uart_period));
  soc_uart_model uart (.tx(IO[0]), .rx(uart_rx_line), .bit_ns(uart_bit_ns),
                       .listen(dut.u_sysctl.pinmux[1:0] == 2'd2));
  soc_gpio_model gpio (.pad(IO), .level(gpio_level));
  soc_spi_flash  flash (.sclk(IO[2]), .cs_n(IO[5]), .mosi(IO[4]), .miso(flash_miso));
  soc_jtag_host  jtag (.tck(TCK), .tms(TMS), .tdi(TDI), .tdo(TDO));

Rule 1 at work: each model connects to pads. The two exceptions are in plain sight. The UART model's bit time is UART0 DIV unless the test sets +uart_div, and listen reads PINMUX inside the chip.

The chip sends U on IO0 at 8 cycles a bit.
PAD t=635 IO0=0
PAD t=715 IO0=1
PAD t=795 IO0=0
PAD t=875 IO0=1
PAD t=955 IO0=0
PAD t=1035 IO0=1
PAD t=1115 IO0=0
PAD t=1195 IO0=1
PAD t=1275 IO0=0
PAD t=1355 IO0=1

The start bit falls at 635 ns and every bit lasts 80 ns, as in the table by hand. The data bits are 1, 0, 1, 0 and so on; the stop bit starts at +720 ns. The UART model received one byte.

The start of the SPI frame: command 0x9F to the flash model.
PAD t=1555 CS_N=0
PAD t=1555 MOSI=1
PAD t=1575 SCLK=1
PAD t=1595 SCLK=0
PAD t=1595 MOSI=0
PAD t=1615 SCLK=1
PAD t=1635 SCLK=0
PAD t=1655 SCLK=1
PAD t=1675 SCLK=0
PAD t=1675 MOSI=1
PAD t=1695 SCLK=1
PAD t=1715 SCLK=0
PAD t=1735 SCLK=1

CS_N falls at 1555 ns. SCLK has a period of 40 ns; MOSI changes on its falling edges and is read on the rising ones: mode 0. The frame has 20 rising edges and CS_N rises at 2835 ns. The flash model answers on MISO 1 ns after each falling edge, from the ninth clock on: rule 2.

GPIO pad 6, driven by the chip, and pad 7, which the board wires to it.
PAD t=2985 IO7=1
PAD t=2985 IO6=1
PAD t=4995 IO7=0
PAD t=4995 IO6=0

IO6 and IO7 rise together. The chip drives pad 6, and the board's wire carries it to pad 7, where the chip reads it back. Then both fall when the program clears the bit.

One clock of the JTAG host (soc_pads.sv): TMS and TDI change at even times, TDO is read just before TCK rises.
  task automatic tick(input logic mode, input logic serial_in);
    tms = mode; tdi = serial_in;
    #22; tdo_seen = tdo; tck = 1'b1; #22; tck = 1'b0;
  endtask

TCK rising edges: 43: one IDCODE scan, from the reset of the TAP to the value. The model is 56 lines; the UART 47, the flash 39, the GPIO board 22. Rule 3 in numbers.

Why It Is Built This Way

Why not buy or write a complete flash model, with every command of a real part? Because each command the tests do not use is untested code inside the bench's checking. When the boot test needs fast read or quad mode, the model grows by those commands, with a test for each.

Why does soc_tb's UART model follow DIV by default at all? Because most firmware tests pick their own divider, and a board that follows it lets every one of them talk. The price is a blind spot, the first trap below. A real board does not know DIV, so a test of the divider itself sets +uart_div and fixes the board's rate.

Traps

The first trap is a model that follows the chip. soc_tb's UART model reads the chip's divider register to learn the bit time, unless a test fixes it. While it follows, it agrees with the chip even when the divider is wrong. Every customer's terminal would see garbage, and the bench would see perfect bytes. A test of the divider must fix the board's rate.

The second trap is a pad model that picks a winner. When the chip and the board both drive a pad at different levels, the real board has a short circuit. The board of soc_tb avoids the question: it lets go of any pad the chip drives. A board model with hard drivers, such as a button or a jumper, must count the fight. One that quietly lets the chip win hides a pin-mux bug.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has four simple devices on its pads.

  1. Verification IP

    USB, PCIe, DDR and Ethernet models are bought: thousands of lines, with their own checkers. The rule stays: they own pads only.

  2. Timing on the pads

    Fast interfaces need setup and hold at the pads, skew between lanes and jitter. Those models measure time, not just levels.

  3. Models as checkers

    A protocol model usually checks the chip too: bad parity, illegal sequences. Its errors go into the bench's error count, never only to a log.

Try It

Run it: the board's send task makes a frame of 0x55. Each edge is compared with the edges the chip's own transmitter made on IO0.

Change the bit time to 72 ns (the predict question below). Which edges still match?

Change the byte to 0x5A. Why do the edges stop matching even at 80 ns?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: edge 0 at +0 ns: line = 0 edge 1 at +80 ns: line = 1 edge 2 at +160 ns: line = 0 edge 3 at +240 ns: line = 1 edge 4 at +320 ns: line = 0 edge 5 at +400 ns: line = 1 edge 6 at +480 ns: line = 0 edge 7 at +560 ns: line = 1 edge 8 at +640 ns: line = 0 edge 9 at +720 ns: line = 1 10 of 10 edges at the times the chip's transmitter used

Check Yourself

Predict the outputThe board's model now sends at 72 ns a bit while the chip's frame used 80. What is the last line printed?
`timescale 1ns/1ns
// The board side of a UART line, low bit first, one stop bit, 72 ns a bit.
module tb;
  logic line = 1;
  int t0, n = 0, same = 0;
  int chip [10];   // the edges ap_soc's own transmitter made for the same byte, from its start bit
  task automatic send(input logic [7:0] value);
    line = 0; #72;
    for (int i = 0; i < 8; i++) begin line = value[i]; #72; end
    line = 1; #72;
  endtask
  always @(line) begin
    if (n == 0) t0 = $time;
    if (n < 10 && $time - t0 == chip[n]) same++;
    $display("edge %0d at +%0d ns: line = %b", n, $time - t0, line);
    n++;
  end
  initial begin
    chip[0] = 0;
    chip[1] = 80;
    chip[2] = 160;
    chip[3] = 240;
    chip[4] = 320;
    chip[5] = 400;
    chip[6] = 480;
    chip[7] = 560;
    chip[8] = 640;
    chip[9] = 720;
    #20 send(8'h55);
    $display("%0d of 10 edges at the times the chip's transmitter used", same);
  end
endmodule
ConceptWhich pads does soc_tb's SPI flash model own?
ConceptIn the recorded SPI frame, when does MOSI change?
ConceptA GPIO pad is driven high by the chip and low by the board. What should a pad model do?
ConceptWhy does soc_tb's flash model answer only three commands?

Common Mistakes

Wrong Let a pad model follow a register of the chip in the test of that register.

The model then agrees with the chip even when the chip is wrong.

Fix Give the model its rate from the bench, as a board would have it (+uart_div in soc_tb).

Wrong Sample a UART bit at its edge.

A clock a few percent off then reads the neighbouring bit.

Fix Sample in the middle of each bit, half a bit after the start edge and one bit apart.

Wrong Model every command of a real flash part.

Untested model code becomes part of what checks the chip.

Fix Model the commands the tests use, and add more with a test each.

Remember

  • Pad models play the board: UART on IO0 and IO1, the SPI flash on IO2 to IO5, GPIO levels and wires, JTAG.
  • A model decides what is right for what it models; get its protocol exactly right and sample UART bits in their middle.
  • Model nothing the tests do not use; report frame errors, short frames and fights.
  • A model that follows a chip register cannot catch a wrong value in it; fix the board's rate to test the register.

Practice

Report a problem with this page