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
| Case | Budget (retired) | Reference | Kit handlers | Kit polling drivers |
|---|---|---|---|---|
| Six bytes | 880 | 437 | 1173 | 3647 |
| Twelve bytes | 1390 | 691 | 1957 | 7003 |
| Register | Address | Use |
|---|---|---|
| UART0 TXDATA, STATUS | 0x1000_0000, 0x1000_0004 | STATUS bit 0 ROOM: the transmitter is idle |
| UART0 IRQ_ENABLE | 0x1000_0008 | bit 0: ROOM raises interrupt source 1 |
| SPI DATA, STATUS, IRQ_ENABLE | 0x1000_5000, 0x1000_5004, 0x1000_5008 | STATUS bit 1 DONE (write 1 to clear); IRQ_ENABLE bit 0: DONE raises source 4 |
| PLIC PRIORITY(n), ENABLE, CLAIM | 0x0C00_0000 + 4n, 0x0C00_2000, 0x0C20_0004 | a 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
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000 | TXDATA | 7:0 | data | wo | not stored | The low byte is the next byte to send. Byte to send. | |
0x004 | STATUS | 0 | room | ro | block | Transmit room and receive queue state. The transmitter can take a byte. | |
0x004 | STATUS | 1 | rx_ready | ro | block | At least one received byte waits. | |
0x004 | STATUS | 2 | rx_full | ro | block | The receive queue is full. | |
0x008 | IRQ_ENABLE | 0 | room | rw | 0x0 | regs | Which status bits raise interrupt source 1. ROOM raises the request. |
0x008 | IRQ_ENABLE | 1 | rx_ready | rw | 0x0 | regs | RX_READY raises the request (new in v1.5). |
0x00C | RXDATA | 7:0 | data | ro | block | The oldest received byte. The read removes it from the queue. Received byte. | |
0x010 | DIV | 15:0 | cycles | rw | 0x0010 | regs | Bit time in clk_sys cycles (new in v1.5). clk_sys cycles a bit. |
## UART0 (base 0x1000_0000, interrupt source 1, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000` | TXDATA | 7:0 | data | wo | | not stored | The low byte is the next byte to send. Byte to send. | | `0x004` | STATUS | 0 | room | ro | | block | Transmit room and receive queue state. The transmitter can take a byte. | | `0x004` | STATUS | 1 | rx_ready | ro | | block | At least one received byte waits. | | `0x004` | STATUS | 2 | rx_full | ro | | block | The receive queue is full. | | `0x008` | IRQ_ENABLE | 0 | room | rw | `0x0` | regs | Which status bits raise interrupt source 1. ROOM raises the request. | | `0x008` | IRQ_ENABLE | 1 | rx_ready | rw | `0x0` | regs | RX_READY raises the request (new in v1.5). | | `0x00C` | RXDATA | 7:0 | data | ro | | block | The oldest received byte. The read removes it from the queue. Received byte. | | `0x010` | DIV | 15:0 | cycles | rw | `0x0010` | regs | Bit time in clk_sys cycles (new in v1.5). clk_sys cycles a bit. |
SPI · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000 | DATA | 7:0 | command | wo | not stored | Write: a command byte; the write starts a frame of 32 serial clocks. Read: the last 24 bits received. Command byte. | |
0x000 | DATA | 23:0 | received | ro | block | Last 24 bits received. | |
0x004 | STATUS | 0 | busy | ro | block | Frame state. A frame is running, and chip select is low. | |
0x004 | STATUS | 1 | done | w1c | block | A frame has ended (new in v1.5). | |
0x008 | IRQ_ENABLE | 0 | done | rw | 0x0 | regs | Lets DONE raise interrupt source 4 (new in v1.5). 1 enables the request. |
## SPI (base 0x1000_5000, interrupt source 4, clk_sys, PD_PER) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000` | DATA | 7:0 | command | wo | | not stored | Write: a command byte; the write starts a frame of 32 serial clocks. Read: the last 24 bits received. Command byte. | | `0x000` | DATA | 23:0 | received | ro | | block | Last 24 bits received. | | `0x004` | STATUS | 0 | busy | ro | | block | Frame state. A frame is running, and chip select is low. | | `0x004` | STATUS | 1 | done | w1c | | block | A frame has ended (new in v1.5). | | `0x008` | IRQ_ENABLE | 0 | done | rw | `0x0` | regs | Lets DONE raise interrupt source 4 (new in v1.5). 1 enables the request. |
ap_plic · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x004 + 4 × (n − 1), n = 1 to 16 | PRIORITY | 2:0 | level | rw | 0x0 | block | Priority of source n, 0 to 7. 0 never competes. Priority. |
0x001000 | PENDING | 16:1 | sources | ro | block | Pending bits; bit number = source ID. One bit for each source. | |
0x002000 | ENABLE | 16:1 | sources | rw | 0x0000 | block | Enable bits; bit number = source ID. One bit for each source. |
0x200000 | THRESHOLD | 2:0 | level | rw | 0x0 | block | The core sees a request only above this priority. Threshold. |
0x200004 | CLAIM | 4:0 | id | rw | 0x00 | block | Read: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID. |
## ap_plic (base 0x0C00_0000, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x004` + 4 × (n − 1), n = 1 to 16 | PRIORITY | 2:0 | level | rw | `0x0` | block | Priority of source n, 0 to 7. 0 never competes. Priority. | | `0x001000` | PENDING | 16:1 | sources | ro | | block | Pending bits; bit number = source ID. One bit for each source. | | `0x002000` | ENABLE | 16:1 | sources | rw | `0x0000` | block | Enable bits; bit number = source ID. One bit for each source. | | `0x200000` | THRESHOLD | 2:0 | level | rw | `0x0` | block | The core sees a request only above this priority. Threshold. | | `0x200004` | CLAIM | 4:0 | id | rw | `0x00` | block | Read: 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