V13.3.3 Send a Message Inside an Instruction Budget

HardOptimize35 minap_soc v1.5 · ISS + RTL hybrid · serverReview the topic

Send bytes from the SPI flash out of the UART pad, on the hybrid. The output is easy; the budget is not. Your image may retire only so many instructions, and every wait costs.

Hardware context: The hybrid: ap_iss with RTL peripherals behind a synthesizable AHB transactor. A message sent inside an instruction budget.

On the hybrid, an instruction that stays in the model's own memory costs one step. A read of a device register crosses the transactor and costs several bus edges, and so does every poll. This problem makes you pay for that, in retired instructions.

Write main.c. Set PINMUX to 0xAAA and UART0 DIV to 128 clocks a bit. Send command 0x66 to the flash, then read frames with 0x03. Send the first TB_ARG bytes of the flash out of UART0, in flash order. Wait until the transmitter is idle after the last byte, then return 0.

Each case has a budget of retired instructions, shown below for the public cases. It is twice the count of the reference on that case, recorded on the hybrid at one instruction a step. The kit's polling drivers do the job correctly and spend several times the budget.

On every case, hidden ones too, the nearest recorded wrong answer retires at least 1.33 times the budget. That answer is the kit's interrupt handlers, correct but heavier.

Full specification
CaseBudget (retired)ReferenceKit handlersKit polling drivers
Six bytes88043711733647
Twelve bytes139069119577003
RegisterAddressUse
UART0 TXDATA, STATUS0x1000_0000, 0x1000_0004STATUS bit 0 ROOM: the transmitter is idle
UART0 IRQ_ENABLE0x1000_0008bit 0: ROOM raises interrupt source 1
SPI DATA, STATUS, IRQ_ENABLE0x1000_5000, 0x1000_5004, 0x1000_5008STATUS bit 1 DONE (write 1 to clear); IRQ_ENABLE bit 0: DONE raises source 4
PLIC PRIORITY(n), ENABLE, CLAIM0x0C00_0000 + 4n, 0x0C00_2000, 0x0C20_0004a source needs a priority above 0 and its enable bit; read CLAIM to take it, write the ID back to complete

A wfi instruction sleeps until an enabled interrupt is pending: its bit in mie must be set, but mstatus.MIE need not be. While the model sleeps, the RTL peripherals keep running, one system edge per sleeping step, and nothing is retired.

Given Files

Read-only files supplied by the bench.

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

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

UART0 · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000TXDATA7:0datawonot storedThe low byte is the next byte to send. Byte to send.
0x004STATUS0roomroblockTransmit room and receive queue state. The transmitter can take a byte.
0x004STATUS1rx_readyroblockAt least one received byte waits.
0x004STATUS2rx_fullroblockThe receive queue is full.
0x008IRQ_ENABLE0roomrw0x0regsWhich status bits raise interrupt source 1. ROOM raises the request.
0x008IRQ_ENABLE1rx_readyrw0x0regsRX_READY raises the request (new in v1.5).
0x00CRXDATA7:0dataroblockThe oldest received byte. The read removes it from the queue. Received byte.
0x010DIV15:0cyclesrw0x0010regsBit time in clk_sys cycles (new in v1.5). clk_sys cycles a bit.
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_plic · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x004 + 4 × (n − 1), n = 1 to 16PRIORITY2:0levelrw0x0blockPriority of source n, 0 to 7. 0 never competes. Priority.
0x001000PENDING16:1sourcesroblockPending bits; bit number = source ID. One bit for each source.
0x002000ENABLE16:1sourcesrw0x0000blockEnable bits; bit number = source ID. One bit for each source.
0x200000THRESHOLD2:0levelrw0x0blockThe core sees a request only above this priority. Threshold.
0x200004CLAIM4:0idrw0x00blockRead: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID.

Examples

Example 1

Six Bytes
Input
Six bytes
Output
six bytes on the UART pad; 437 retired against a budget of 880

The kit's polling drivers send the same six bytes with 3647 retired instructions.

Example 2

Twelve Bytes
Input
Twelve bytes
Output
twelve bytes; 691 retired

Polling grows with the time it waits: 7003 retired here. A sleeping answer grows only with the number of wakes.

Example 3

The Kit's Handlers
Input
irq_enable for sources 1 and 4, wfi between
Output
1173 retired: over the budget

It sleeps, but each wake is a full trap with register saves and a dispatch.

Constraints and Contract

  • C firmware with the kit, SRAM layout. The hybrid runs at one instruction a step (IPC 1).
  • The UART pad must carry exactly the first TB_ARG flash bytes, in order.
  • Your image must retire at most the budget of each case.

Hints

Hint 1

Count what a poll costs: an instruction or three in the model, and a device read across the transactor each time.

Hint 2

What does the model do while it sleeps in wfi?

Hint 3

A handler is not required to wake from wfi. What must you still do at the PLIC so the next wfi sleeps?

What the grader checks: Each case runs your image once on the hybrid at IPC 1. It checks the pass word, the number of UART bytes, their hash, and retired instructions against the ceiling. The grader cannot see wall time or the hybrid's bus edges, only retired instructions. The judge builds every answer with the same compiler, riscv64-unknown-elf-gcc 14.2 at -Os, so your count does not depend on your machine. A change of the judge's compiler would move counts a little. The budget is twice the reference, and the nearest wrong answer sits 1.33 times above it.

5 checks on ap_soc v1.5 · ISS + RTL hybrid · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your text only; the chip, the bench and the hidden checks stay on the server. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 problems on this path

Report a problem with this page

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

Six Bytes

Input
Six bytes
Expected output
six bytes on the UART pad; 437 retired against a budget of 880

The kit's polling drivers send the same six bytes with 3647 retired instructions.

Run samples executes all 2 public checks. Check all tests runs 5 checks on the server.

ap_soc v1.5 · ISS + RTL hybrid · server