V4.8.1 Three Things at Once

MediumImplement30 minap_soc v1.5 · firmware · serverReview the topic

Bytes arrive on the UART pad and the DMA carries them into memory. At the same time, the core counts pulses on a pin. Write the firmware that sets the three going and reports when the DMA says it is done.

Hardware context: A scenario on ap_soc v1.5 where the DMA, UART0, GPIO and ap_plic work at once. Bytes received by DMA while edges are counted.

TB_ARG holds n, from 1 to 64. After your TB_EVENT 0x8100 the bench sends n bytes on the UART receive pad IO1. Meanwhile it pulses pad IO8 now and then, always before the last byte has arrived. Three things must happen at once. UART0 receives the bytes. The DMA carries each byte from RXDATA into a buffer in SRAM, waiting for each one by itself. The core counts every rising edge of pin 8 in an interrupt handler.

When the DMA's interrupt says the job is done, compute sum = sum × 31 + byte over the n bytes. Start from sum = 0 and go in order. Print bytes, n, sum , the sum, edges , the count, each as eight hex digits, and a new line.

Full specification
DMA registerAddressUse
SRC0x1000_6000Where to read; a device register stays fixed
DST0x1000_6004Where to write; SRAM addresses count up
LEN0x1000_6008Bytes to move
START0x1000_600CWrite 1 to start
STATUS0x1000_6014Bit 0 BUSY, 1 DONE, 2 FAULT; write 1 to clear DONE or FAULT
IRQ_ENABLE0x1000_6018Bit 0 DONE, bit 1 FAULT: ap_plic source 5

With UART0 RXDATA as its source, the DMA waits for RX_READY before each byte. The kit has no DMA driver yet: use the registers. The bench counts the DMA's bytes, the GPIO interrupts and all external interrupts.

Given Files

Read-only files supplied by the bench.

drivers/gpio.h
#ifndef AP_GPIO_H
#define AP_GPIO_H
#include "hal.h"

static inline void gpio_drive(unsigned outputs, unsigned value)
{
    reg_write32(GPIO_OUT, value);
    reg_write32(GPIO_DIR, outputs);
}

static inline unsigned gpio_read(void)
{
    return reg_read32(GPIO_IN);
}

/* Select the rising-edge inputs for this test and discard stale edges. */
static inline void gpio_rising_irq(unsigned pins)
{
    reg_write32(GPIO_LEVEL_MODE, 0);
    reg_write32(GPIO_ACTIVE_HIGH, pins);
    reg_write32(GPIO_STATUS, pins);
    reg_write32(GPIO_IRQ_ENABLE, pins);
}

static inline void gpio_ack(unsigned pins)
{
    reg_write32(GPIO_STATUS, pins);
}
#endif

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

irq.h
#ifndef AP_IRQ_H
#define AP_IRQ_H
typedef void (*irq_handler)(void);
unsigned plic_claim(void);
void plic_complete(unsigned id);
void irq_enable(unsigned id, irq_handler handler);
unsigned long long mtime_read(void);
void mtime_set_compare(unsigned long long deadline);
void msip_enable(irq_handler handler);
void mtime_enable(irq_handler handler);
#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

DMA · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000SRC31:0addressrw0x00000000regsThe address of the first byte to read. Byte address.
0x004DST31:0addressrw0x00000000regsThe address of the first byte to write. Byte address.
0x008LEN31:0bytesrw0x00000000regsThe number of bytes. Byte count.
0x00CSTART0startwonot storedBit 0 = 1 starts the job. Software writes it last. 1 starts the job.
0x014STATUS0busyroblockJob state (new in v1.5). A job is running.
0x014STATUS1donew1cblockThe last write of a job has finished.
0x014STATUS2faultw1cblockA bus error stopped the job.
0x018IRQ_ENABLE0donerw0x0regsWhich status bits raise interrupt source 5 (new in v1.5). DONE raises the request.
0x018IRQ_ENABLE1faultrw0x0regsFAULT raises the request.
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

Nine Bytes, Three Pulses
Input
"hello dma" with three pulses
Output
bytes 00000009 sum ... edges 00000003

Three GPIO interrupts and one DMA interrupt: four external interrupts.

Example 2

One Byte
Input
one byte, no pulse
Output
bytes 00000001 sum 0000005a edges 00000000

sum = 0 × 31 + 0x5A.

Example 3

Twenty-Four Bytes
Input
24 bytes, two pulses
Output
bytes 00000018 sum ... edges 00000002

The DMA moves 24 bytes while two edges are counted.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • The DMA must move the bytes, and the end of the job must arrive by interrupt. The bench requires n DMA bytes, and one external interrupt more than there are pulses.
  • Count the edges by interrupt.

Hints

Hint 1

Start the DMA before you tell the bench to send: who else would take the first byte?

Hint 2

Two interrupt sources, two handlers, both enabled at the device and at ap_plic.

Hint 3

How does main know the DMA is done without reading its STATUS?

What the grader checks: Each case sends chosen bytes with pulses between them. It compares the console line, the DMA's byte count and the two interrupt counts. It does not check the order of events inside the chip.

7 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 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

Nine Bytes, Three Pulses

Input
"hello dma" with three pulses
Expected output
bytes 00000009 sum ... edges 00000003

Three GPIO interrupts and one DMA interrupt: four external interrupts.

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

ap_soc v1.5 · firmware · server