V4.3.2 The Log That Costs Too Much

MediumOptimize20 minap_soc v1.5 · firmware · serverReview the topic

A test logs twelve lines and sends one result line out of the UART pad. It works, and it takes far too long. Make it finish inside the cycle budget without changing a byte of what the console or the pad receives.

Hardware context: The console channel of ap_soc v1.5 against the UART0 path to pad IO0. A log that fits a cycle budget.

A test makes twelve words from TB_ARG and logs each as a line: word, the index and the word, in hex. Then it sends PASS, a carriage return and a new line out of the UART pad, and prints result sent. The bench reads the log on the console and the result on the pad.

It is correct and slow: about 56,200 cycles on our bench. A regression runs thousands of tests like it. Make it finish within 12,000 cycles. The console text and the result bytes on the pad must stay exactly as the given program sends them. Only the log leaves the pad.

Full specification
PathCost of one character (measured, ap_soc v1.5)
TB_PUTC (the mailbox)About 13 cycles: one store, and the loop around it
UART0 at DIV = 8About 88 cycles: a frame of 10 bits at 8 cycles a bit, and the wait for room
A division (no divide instruction)A call to the kit's shift-and-subtract helper: tens of cycles each

The pad must carry exactly the 6 bytes of the result line. The console must hold the 12 log lines and result sent.

Given Files

Read-only files supplied by the bench.

drivers/uart.h
#ifndef AP_UART_H
#define AP_UART_H
#include "hal.h"

static inline void uart_flush(void)
{
    while (!FIELD_GET(UART0_STATUS_ROOM, reg_read32(UART0_STATUS))) { }
}

static inline void uart_putc(unsigned char byte)
{
    uart_flush();
    reg_write32(UART0_TXDATA, byte);
}

static inline unsigned char uart_getc(void)
{
    while (!FIELD_GET(UART0_STATUS_RX_READY, reg_read32(UART0_STATUS))) { }
    return reg_read32(UART0_RXDATA);
}

static inline void uart_enable_rx_irq(void)
{
    reg_write32(UART0_IRQ_ENABLE, UART0_IRQ_ENABLE_RX_READY_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

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

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.

Examples

Example 1

TB_ARG = 1234
Input
TB_ARG = 1234
Output
console: twelve "word" lines, then "result sent"; pad: "PASS\r\n"; cycles at most 12,000

The words come from a xorshift of TB_ARG | 1, as the given program makes them.

Example 2

TB_ARG = 0
Input
TB_ARG = 0
Output
the same shape; the first word is 0x00042021

TB_ARG | 1 makes the start value 1, never 0.

Example 3

TB_ARG = 0xFFFF_FFFF
Input
TB_ARG = 0xFFFF_FFFF
Output
the same shape, other words

Every case has 264 log characters and 6 result bytes, so the cost does not depend on the data.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • The run must end within 12,000 cycles. The budget leaves room for another compiler; it does not leave room for division or for the log on the pad.
  • Keep the words, the log format and the result line.

Hints

Hint 1

Which output has to go through the UART, and which only has to reach the bench?

Hint 2

How many divisions does the given hex printer make for one word?

Hint 3

A hex digit is four bits: a shift and a mask find it.

What the grader checks: Each case compares the console text, the bytes on the UART pad and the run's cycle count against 12,000. The budget is a ceiling, not a target: any faster correct program passes.

5 checks on ap_soc v1.5 · firmware · 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 2 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

TB_ARG = 1234

Input
TB_ARG = 1234
Expected output
console: twelve "word" lines, then "result sent"; pad: "PASS\r\n"; cycles at most 12,000

The words come from a xorshift of TB_ARG | 1, as the given program makes them.

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

ap_soc v1.5 · firmware · server