V13.2.2 The Test That Hangs on the Model

MediumDebug22 minIcarus Verilog · serverReview the topic

One firmware image passes on chip RTL and on the hybrid, and hangs on the virtual platform. Read the three recorded runs and say who is wrong, where and why.

Hardware context: ap_vp: the instruction-level core with C models of ap_soc's devices. A firmware failure read from recorded platform runs.

The image sends 6 bytes from the SPI flash out of the UART, each wait asleep in wfi. It prints S when spi() starts, s in on_spi(), P when put() starts and u in on_uart(). The same image ran on each platform (riscv64-unknown-elf-gcc 14.2).

This worksheet uses named constants. The first three entries show how to record your decisions.

Replace each unresolved value of 65535 with your result.

Signals in the Timeline

SignalWidthRole
question8input of each check
Full specification
main.c, as it ran on every platform
#include "hal.h"
#include "tb.h"
#include "irq.h"

static volatile unsigned spi_done, room;

static void on_spi(void)  { tb_putc('s'); reg_write32(SPI_STATUS, SPI_STATUS_DONE_MASK); spi_done = 1; }
static void on_uart(void) { tb_putc('u'); reg_write32(UART0_IRQ_ENABLE, 0); room = 1; }

static unsigned spi(unsigned command)
{
    tb_putc('S');
    spi_done = 0;
    reg_write32(SPI_DATA, command);
    while (!spi_done) __asm__ volatile ("wfi");
    return reg_read32(SPI_DATA) & 0xffffff;
}

static void put(unsigned char c)
{
    tb_putc('P');
    room = 0;
    reg_write32(UART0_IRQ_ENABLE, UART0_IRQ_ENABLE_ROOM_MASK);
    while (!room) __asm__ volatile ("wfi");
    reg_write32(UART0_TXDATA, c);
}

int main(void)
{
    unsigned n = tb_arg(0);
    reg_write32(SYSCTL_PINMUX, 0xaaa);
    reg_write32(UART0_DIV, 64);
    reg_write32(SPI_IRQ_ENABLE, SPI_IRQ_ENABLE_DONE_MASK);
    irq_enable(SPI_IRQ, on_spi);
    irq_enable(UART0_IRQ, on_uart);
    spi(0x66);
    for (unsigned i = 0; i < n; i += 3) {
        unsigned w = spi(0x03);
        for (unsigned b = 0; b < 3 && i + b < n; b++) put(w >> (16 - 8 * b));
    }
    room = 0;
    reg_write32(UART0_IRQ_ENABLE, UART0_IRQ_ENABLE_ROOM_MASK);
    while (!room) __asm__ volatile ("wfi");
    return 0;
}

PlatformConsoleEnd
Virtual platformSsSsPuThe run reached the limit of 200,000 cycles before a pass or fail word was written to TB_STATUS.
Hybrid, IPC 1SsSsPuPuPuSsPuPuPuuThe test ended with the pass word.
Hybrid, IPC 16SsSsPuPuPuSsPuPuPuuThe test ended with the pass word.
Chip RTLSsSsPuPuPuSsPuPuPuuThe test ended with the pass word.
CodeWho is wrong
1The VP has a bug in how it raises interrupts
2The firmware has a bug that the VP's timing shows
3The chip RTL has a bug that the VP does not have
4The bench of the RTL run hides a hang
CodeWhere it stops
1spi()
2put()
3on_spi()
4on_uart()
5the last wait of main()
CodeWhat happens
1put() reads ROOM from the wrong bit of UART0 STATUS
2The VP never raises the UART interrupt after the first byte
3The flag is tested, then wfi sleeps; a wake in between is lost
4The handler clears its source after the PLIC completes the claim
CodeThe fix
1Give the UART interrupt a higher PLIC priority
2Run the VP with more clocks per instruction
3Poll the flag without wfi, on the VP only
4Mask interrupts around the test of the flag and the wfi
EntryYour decision
HANDLERS_VPHow many times did a handler run on the VP before the hang?
BYTES_HYBRIDHow many times did put() start on the hybrid at IPC 1?
FRAMES_HYBRIDHow many times did spi() start on the hybrid at IPC 1?
WHOWho is wrong? (code)
WHEREIn which function does the VP stop? (code)
WHATWhat happens there? (code)
FIXWhich change makes the image correct on every platform? (code)
SLEEPS_FOREVERAfter put() writes IRQ_ENABLE (step 1) it loads room (step 2), branches on it (step 3) and runs wfi (step 4). ROOM's interrupt is taken just before step D; D = 5 means while asleep in wfi. Enter 1 if the core then sleeps for ever, else 0.

Examples

Example 1

HANDLERS VP
Input
How many times did a handler run on the VP before the hang?
Output
matches = 1

Count the lower-case letters of the VP console.

Example 2

BYTES HYBRID
Input
How many times did put() start on the hybrid at IPC 1?
Output
matches = 1

One P for each byte.

Example 3

FRAMES HYBRID
Input
How many times did spi() start on the hybrid at IPC 1?
Output
matches = 1

One S for the pointer reset and one for each frame of three bytes.

Constraints and Contract

  • Keep the parameter and named constants. Use the numeric codes and units on this page.
  • For SLEEPS_FOREVER, enter a constant expression using D. Only this entry changes with the parameter. The supported integer range is 2 through 5, inclusive.

Hints

Hint 1

All four agree until the VP stops after P and u.

Hint 2

The VP takes one clock per instruction and no bus wait states, so interrupts land at other instructions.

Hint 3

Between the test of the flag and the wfi, a handler can run and leave nothing to wake the core.

What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.

12 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

HANDLERS VP

Input
How many times did a handler run on the VP before the hang?
Expected output
matches = 1

Count the lower-case letters of the VP console.

Run samples executes all 3 public checks. Check all tests runs 12 checks on the server.

Icarus Verilog · server