V9.3.3 The Tag That Came Back Wrong

MediumDebug20 minap_soc v1.5 · recorded bootReview the topic

A firmware image boots from the SPI flash through a new boot ROM loader. Its first line prints, and then it fails its own check of a tag in its data. Find the first transfer that went wrong, and say why.

The image is 836 bytes. A new boot ROM loader copies it from the flash to SRAM, three bytes a SPI frame, then jumps to 0x8000_0000. The program checks a tag it keeps in its initialised data, and fails with code 1 if the tag is wrong.

The firmware of the recorded run (main.c).
#include "hal.h"
#include "tb.h"

volatile unsigned boot_tag = 0x5aa5c33cu;   /* initialised data: part of the image */
volatile unsigned boot_count = 1;

int main(void)
{
    tb_puts("boot "); tb_put_hex(boot_count); tb_putc('\n');
    if (boot_tag != 0x5aa5c33cu) {
        tb_puts("tag "); tb_put_hex(boot_tag); tb_putc('\n');
        return 1;
    }
    tb_puts("image ok: the tag checks out\n");
    return 0;
}
The loader in ROM, as it ran (LENGTH = 836).
# The boot ROM's loader of this build. It copies LENGTH bytes from the SPI flash to SRAM, three a frame,
# then jumps to the image at 0x8000_0000.
    li   s0, 0x10005000        # SPI
    li   s1, 0x10004010        # PINMUX
    li   t0, 0xaaa
    sw   t0, 0(s1)             # IO0 to IO5 serial: IO2 to IO5 carry the SPI
    li   s2, 0x80000000        # where the image goes
    li   s3, 836             # bytes left
    li   a0, 0x66              # the flash, back to byte 0
    jal  ra, frame
copy:
    li   t0, 3
    blt  s3, t0, enter        # fewer than three left: done
    li   a0, 3                 # the next three bytes
    jal  ra, frame
    li   t2, 16
store:
    srl  t0, a0, t2
    sb   t0, 0(s2)
    addi s2, s2, 1
    addi s3, s3, -1
    nop
    addi t2, t2, -8
    bge  t2, zero, store
    j    copy
enter:
    sw   zero, 0(s1)           # PINMUX back to its reset value
    li   t0, 0x80000000
    jalr zero, 0(t0)
frame:
    li   t1, 2
    sw   t1, 4(s0)             # clear DONE
    sw   a0, 0(s0)             # the command starts a frame
busy:
    lw   t0, 4(s0)
    andi t0, t0, 2
    beq  t0, zero, busy
    lw   a0, 0(s0)
    ret

The run used 279 SPI frames. The trace below starts with the loader's last stores into SRAM (their pc is in ROM), then main.

The trace shows 12 of 7334 retired loads and stores, cycles 50852 to 51455. Name the first transfer whose data broke an assumption of the test. Give its cycle, the wrong part, its address and its register or variable. Then choose the cause.

Full specification

The register list names every address the answer may use. Addresses in the trace are hex without 0x; write them with or without 0x in the form.

A load's data is what came back; a store's is what was written. A byte store shows its byte in the low eight bits.

Diagnose the Failing Run

Recorded Console and Result

CONSOLE 51426: boot 00000001
CONSOLE 51610: tag 0000c33c
verdict=fail
reason=status_fail
status=bad00001
cycles=51646
traps=0
interrupts=0
mismatches=0
checker_errors=0
reset_debug=0
dma_bytes=0
spi_frames=279

Retired Memory Transfers

CyclePC (hex)OperationAddress (hex)Data (hex)
5085200000040store8000033e00000000
5101700000040store8000033f00000000
5102600000040store800003400000003c
5103500000040store80000341000000c3
5122780000078store1000402000000002
5141280000260load800002f900000031
5141580000268store1000402400000031
5142780000214store100040240000000a
51431800002a4load800003400000c33c
5144380000224load8000031400000074
5145180000234store1000402400000074
5145580000224load8000031500000061
Step 1 · The first bad cycle and signal
Step 2 · The cause

Given Files

Use the register map to identify the transfer in the recording.

tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif

hal.h
#ifndef AP_HAL_H
#define AP_HAL_H
#include "ap_soc_regs.h"

/* V13.4 supplies these two functions through DPI in a host-native build. */
#ifdef AP_HOST_DPI
unsigned reg_read32(unsigned address);
void reg_write32(unsigned address, unsigned value);
#else
static inline unsigned reg_read32(unsigned address)
{
    return *(volatile unsigned *)address;
}
static inline void reg_write32(unsigned address, unsigned value)
{
    *(volatile unsigned *)address = value;
}
#endif

#define FIELD_GET(field, word) (((word) & field##_MASK) >> field##_SHIFT)
#define FIELD_PREP(field, value) (((unsigned)(value) << field##_SHIFT) & field##_MASK)
#endif

SPI · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000DATA7:0commandwonot storedWrite: a command byte; the write starts a frame of 32 serial clocks. Read: the last 24 bits received. Command byte.
0x000DATA23:0receivedroblockLast 24 bits received.
0x004STATUS0busyroblockFrame state. A frame is running, and chip select is low.
0x004STATUS1donew1cblockA frame has ended (new in v1.5).
0x008IRQ_ENABLE0donerw0x0regsLets DONE raise interrupt source 4 (new in v1.5). 1 enables the request.
ap_sysctl · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000CHIP_ID31:0valuero0x2A5310C5constThe chip's identity: the IDCODE of the TAP. Fixed number.
0x004RESET_CAUSE0power_onroblockSticky record of reset sources since power-on. Power-on reset.
0x004RESET_CAUSE1pinroblockReset pin.
0x004RESET_CAUSE2watchdogroblockWatchdog.
0x004RESET_CAUSE3softwareroblockSoftware request.
0x004RESET_CAUSE4debugroblockDebug request.
0x008SOFT_RESET0timerwonot storedReset requests from software. 1 arms one restart of the timer.
0x008SOFT_RESET1chipwonot stored1 requests the software reset (new in v1.5).
0x00CSTRAPS0bootroblockStraps as sampled at power-up (new in v1.5). 0: start in the boot ROM. 1: start in SRAM.
0x010PINMUX31:0selectrw0x00000000regsTwo bits for each of sixteen pads: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Pad n uses bits 2n+1 and 2n.
0x014CLK_GATE0uart0rw0x1regs1 lets that block's clock run (new in v1.5). UART0.
0x014CLK_GATE1gpiorw0x1regsGPIO.
0x014CLK_GATE2timerrw0x1regsTimer and PWM.
0x014CLK_GATE3spirw0x1regsSPI.
0x014CLK_GATE4dmarw0x1regsDMA.
0x018LOCK0pinmuxw1s0x0regsWrite-once locks. Only a reset clears one (new in v1.5). 1: PINMUX ignores writes.
0x018LOCK1clk_gatew1s0x0regs1: CLK_GATE ignores writes.
0x018LOCK2debugw1s0x0regs1: the debug lock is closed.
0x020TB_STATUS31:0valuerw0x00000000regs (power-on reset only)Scratch word 0. The firmware kit reports progress, pass and fail here. Plain storage.
0x024TB_PUTC31:0valuerw0x00000000regs (power-on reset only)Scratch word 1. The firmware kit writes one character here. Plain storage.
0x028TB_EVENT31:0valuerw0x00000000regs (power-on reset only)Scratch word 2. The firmware kit writes an event number here. Plain storage.
0x02CTB_ARG31:0valuerw0x00000000regs (power-on reset only)Scratch word 3. A bench leaves a word for the firmware here. Plain storage.