V0.3.1 Drain the Receive Queue

MediumImplement25 minVerilator + your C over DPI · serverReview the topic

Write uart_rx_drain with the generated header. Copy every waiting UART0 byte into a buffer, never more than max, and never read an empty queue.

Hardware context: A host-native model of the UART0 receive queue of ap_soc, reached through the generated header. A receive driver that drains a UART without losing or inventing bytes.

Received bytes wait in a small queue inside UART0. Reading RXDATA hands you the oldest one and removes it, so a read you did not need loses a byte for good. STATUS says whether a byte waits.

Write int uart_rx_drain(uint8_t *buf, int max) using only the names in ap_soc_regs.h. Copy waiting bytes into buf, oldest first, until none waits or max bytes are copied. Return the number copied.

Full specification

The register addresses are not repeated here on purpose: they live in ap_soc_regs.h, the header generated from the register descriptions. Use AP_UART0_BASE plus the offsets, and the STATUS bit names.

More bytes may be on the line than the queue holds. Each byte you take lets the next one in, so read STATUS again before every byte.

hal.h (given, read only)
/* hal.h: register access for chip tests (ASIC Point, SoC Verification V0).
 * Host-native build: every call is one bus transfer, carried to the bench over DPI.
 * On the chip, the same two functions are volatile pointer accesses (lesson V0.1). */
#ifndef HAL_H
#define HAL_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
int bus_read(int addr);              /* provided by the bench */
void bus_write(int addr, int data);  /* provided by the bench */
#ifdef __cplusplus
}
#endif
static inline uint32_t reg_read32(uint32_t addr) { return (uint32_t)bus_read((int)addr); }
static inline void reg_write32(uint32_t addr, uint32_t value) { bus_write((int)addr, (int)value); }
#endif
ap_soc_regs.h (given, read only)
/* ap_soc_regs.h: GENERATED from the register descriptions. Do not edit by hand.
 * ap_soc map v1.0 subset: UART0, GPIO, ap_sysctl PINMUX. Access types are in the comments. */
#ifndef AP_SOC_REGS_H
#define AP_SOC_REGS_H

/* ---- UART0 ---- */
#define AP_UART0_BASE              0x10000000u
#define AP_UART_TXDATA             0x00u  /* WO  low byte = next byte to send */
#define AP_UART_STATUS             0x04u  /* RO */
#define AP_UART_STATUS_ROOM        (1u << 0)  /* transmitter can take a byte */
#define AP_UART_STATUS_RX_READY    (1u << 1)  /* at least one received byte waits */
#define AP_UART_STATUS_RX_FULL     (1u << 2)  /* the receive queue is full */
#define AP_UART_IRQ_ENABLE         0x08u  /* WO  bit 0: ROOM may raise the request */
#define AP_UART_RXDATA             0x0Cu  /* RO  oldest received byte; THE READ REMOVES IT */

/* ---- GPIO (bit n = pin n, 16 pins) ---- */
#define AP_GPIO_BASE               0x10001000u
#define AP_GPIO_DIR                0x00u  /* RW  1 = output driver on; reset 0 */
#define AP_GPIO_IN                 0x04u  /* RO  pin levels */
#define AP_GPIO_OUT                0x08u  /* RW  saved output value */
#define AP_GPIO_SET                0x0Cu  /* WO  1 sets that OUT bit */
#define AP_GPIO_CLR                0x10u  /* WO  1 clears that OUT bit */
#define AP_GPIO_TGL                0x14u  /* WO  1 inverts that OUT bit */
#define AP_GPIO_IRQ_ENABLE         0x18u  /* RW  reset 0 */
#define AP_GPIO_LEVEL_MODE         0x1Cu  /* RW  1 = level cause, 0 = edge cause; reset 0 */
#define AP_GPIO_ACTIVE_HIGH        0x20u  /* RW  1 = high level or rising edge; reset all ones */
#define AP_GPIO_STATUS             0x24u  /* R/W1C  read: raw status; write 1: clear that edge flag */
#define AP_GPIO_MASKED_STATUS      0x28u  /* RO  STATUS & IRQ_ENABLE */

/* ---- ap_sysctl ---- */
#define AP_SYSCTL_BASE             0x10004000u
#define AP_SYSCTL_PINMUX           0x10u  /* RW  two bits per pad, pads 0 to 3 */
#define AP_PINMUX_PAD_SHIFT(pad)   (2u * (pad))
#define AP_PINMUX_PAD_MASK(pad)    (3u << AP_PINMUX_PAD_SHIFT(pad))
#define AP_PINMUX_RELEASED         0u
#define AP_PINMUX_GPIO             1u
#define AP_PINMUX_SERIAL           2u
#define AP_PINMUX_AUX              3u

#endif
uart_rx_task.h (given, read only)
/* uart_rx_task.h: the function you write. */
#ifndef UART_RX_TASK_H
#define UART_RX_TASK_H
#include "hal.h"
#ifdef __cplusplus
extern "C" {
#endif
int uart_rx_drain(uint8_t *buf, int max);
#ifdef __cplusplus
}
#endif
#endif
drain_bench.cpp (given, read only)
/* drain_bench.c: calls your uart_rx_drain with a 16-byte buffer full of 0xAA canaries. */
#include "uart_rx_task.h"
extern "C" int bench_drain(int max) {
  uint8_t buf[16];
  for (int i = 0; i < 16; i++) buf[i] = 0xAA;
  int n = uart_rx_drain(buf, max);
  unsigned sum = 0;
  for (int i = 0; i < 16; i++) sum = (sum * 31u + buf[i]) & 0xFFFFu;
  return (int)((((unsigned)n & 0xFFu) << 16) | sum);
}

Examples

Example 1

Three Bytes Waiting, Room for Eight
Input
3 bytes arrive (seed 5); uart_rx_drain(buf, 8)
Output
count = 3, buffer_checksum = 0x93b1, left_on_chip = 0, bad_accesses = 0

Three bytes wait and the buffer takes eight: copy all three and return 3. Nothing is left on the chip.

Example 2

Six Bytes, Room for Four
Input
6 bytes arrive (seed 9); uart_rx_drain(buf, 4)
Output
count = 4, buffer_checksum = 0xaaea, left_on_chip = 2, bad_accesses = 0

Six bytes but room for four: stop at four. Two stay on the chip for the next call.

Example 3

Nothing Waiting
Input
0 bytes arrive (seed 2); uart_rx_drain(buf, 8)
Output
count = 0, buffer_checksum = 0xea00, left_on_chip = 0, bad_accesses = 0

Nothing waits: return 0 and do not touch RXDATA.

Constraints and Contract

  • Never read RXDATA when RX_READY is 0. Never write past buf[max - 1]. max may be 0. Do not write any register.
  • Each case packs your count and a 16-bit checksum of the whole 16-byte buffer (untouched bytes stay 0xAA).
  • Then come the bytes left on the chip and the number of bad accesses.

Hints

Hint 1

RX_READY means at least one byte waits. RX_FULL means the queue is full: a different question.

Hint 2

Check count < max before you read STATUS, so max = 0 reads nothing.

Hint 3

One read of RXDATA per byte, straight into the buffer.

What the grader checks: A host-native bench with a register model of the UART0 receive queue. It checks the bytes copied and the accesses made, not serial timing.

11 checks on Verilator + your C over DPI · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. 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

Three Bytes Waiting, Room for Eight

Input
3 bytes arrive (seed 5); uart_rx_drain(buf, 8)
Expected output
count = 3, buffer_checksum = 0x93b1, left_on_chip = 0, bad_accesses = 0

Three bytes wait and the buffer takes eight: copy all three and return 3. Nothing is left on the chip.

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

Verilator + your C over DPI · server