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 register | Address | Use |
|---|---|---|
| SRC | 0x1000_6000 | Where to read; a device register stays fixed |
| DST | 0x1000_6004 | Where to write; SRAM addresses count up |
| LEN | 0x1000_6008 | Bytes to move |
| START | 0x1000_600C | Write 1 to start |
| STATUS | 0x1000_6014 | Bit 0 BUSY, 1 DONE, 2 FAULT; write 1 to clear DONE or FAULT |
| IRQ_ENABLE | 0x1000_6018 | Bit 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
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000 | SRC | 31:0 | address | rw | 0x00000000 | regs | The address of the first byte to read. Byte address. |
0x004 | DST | 31:0 | address | rw | 0x00000000 | regs | The address of the first byte to write. Byte address. |
0x008 | LEN | 31:0 | bytes | rw | 0x00000000 | regs | The number of bytes. Byte count. |
0x00C | START | 0 | start | wo | not stored | Bit 0 = 1 starts the job. Software writes it last. 1 starts the job. | |
0x014 | STATUS | 0 | busy | ro | block | Job state (new in v1.5). A job is running. | |
0x014 | STATUS | 1 | done | w1c | block | The last write of a job has finished. | |
0x014 | STATUS | 2 | fault | w1c | block | A bus error stopped the job. | |
0x018 | IRQ_ENABLE | 0 | done | rw | 0x0 | regs | Which status bits raise interrupt source 5 (new in v1.5). DONE raises the request. |
0x018 | IRQ_ENABLE | 1 | fault | rw | 0x0 | regs | FAULT raises the request. |
## DMA (base 0x1000_6000, interrupt source 5, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000` | SRC | 31:0 | address | rw | `0x00000000` | regs | The address of the first byte to read. Byte address. | | `0x004` | DST | 31:0 | address | rw | `0x00000000` | regs | The address of the first byte to write. Byte address. | | `0x008` | LEN | 31:0 | bytes | rw | `0x00000000` | regs | The number of bytes. Byte count. | | `0x00C` | START | 0 | start | wo | | not stored | Bit 0 = 1 starts the job. Software writes it last. 1 starts the job. | | `0x014` | STATUS | 0 | busy | ro | | block | Job state (new in v1.5). A job is running. | | `0x014` | STATUS | 1 | done | w1c | | block | The last write of a job has finished. | | `0x014` | STATUS | 2 | fault | w1c | | block | A bus error stopped the job. | | `0x018` | IRQ_ENABLE | 0 | done | rw | `0x0` | regs | Which status bits raise interrupt source 5 (new in v1.5). DONE raises the request. | | `0x018` | IRQ_ENABLE | 1 | fault | rw | `0x0` | regs | FAULT raises the request. |
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. |
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