Write uart_putc in C: poll the UART0 status register for room, send one byte, and give up cleanly after 1000 reads.
Hardware context: A host-native model of the UART0 transmit register and status word of ap_soc. Bring-up firmware that prints through a UART without losing bytes or hanging.
Your C runs on the host and reaches a model of UART0 through hal.h. Every reg_read32 or reg_write32 call is one bus transfer, exactly as one load or one store would be on the chip.
The transmitter can be full. A byte written to TXDATA while STATUS bit 0 (ROOM) is 0 is lost. So a test must read STATUS, and read it again each time it looks, until ROOM is 1.
Write int uart_putc(int c). Read STATUS at most 1000 times. As soon as a read shows ROOM = 1, write the low byte of c to TXDATA once and return 0. If 1000 reads all show ROOM = 0, write nothing and return -1.
Full specification
| Address | Register | Access | Meaning |
|---|---|---|---|
| 0x1000_0000 | UART0 TXDATA | write | The low byte is the next byte to send |
| 0x1000_0004 | UART0 STATUS | read | Bit 0 ROOM: the transmitter can take a byte |
In this bench, time passes only while you read STATUS. In each call, ROOM is 0 for some number of reads, then 1. Any other access is counted as a bad access.
/* 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
/* uart_task.h: the function you write. */
#ifndef UART_TASK_H
#define UART_TASK_H
#include "hal.h"
#ifdef __cplusplus
extern "C" {
#endif
int uart_putc(int c);
#ifdef __cplusplus
}
#endif
#endif
Examples
Example 1
h with Room at Once- Input
uart_putc(0x68); ROOM after 0 STATUS reads
- Output
returned = 0x0, byte_taken = 0x68, bad_accesses = 0, timeout_check = 0
ROOM is there at the first read: send 68 and return 0.
Example 2
i After Three Empty Reads- Input
uart_putc(0x69); ROOM after 3 STATUS reads
- Output
returned = 0x0, byte_taken = 0x69, bad_accesses = 0, timeout_check = 0
Three reads show no room and the fourth shows ROOM: send 69 and return 0.
Example 3
No Room for 1000 Reads- Input
uart_putc(0x21); ROOM after 1000 STATUS reads
- Output
returned = 0xff, byte_taken = 0xee, bad_accesses = 0, timeout_check = 1
All 1000 reads show no room: write nothing, return -1 (FF), and the check byte is 1.
Constraints and Contract
- Touch only STATUS (reads) and TXDATA (one write). Never write TXDATA while ROOM is 0.
- Reach the registers only through reg_read32 and reg_write32. On the host, a raw pointer to 0x1000_0004 is not a register: it would crash the run.
- Count reads of STATUS. The 1000th read still counts: if it shows ROOM = 1, send the byte.
- Each case packs four bytes. They are your return value (low byte), the byte TXDATA took (EE if none), and the number of bad accesses.
- The fourth byte is a timeout check: 1 when exactly 1000 reads came before you gave up.
Hints
Hint 1
Put the read inside the loop. A copy of STATUS taken once never changes, even when the transmitter does.
Hint 2
Loop 1000 times at most, and write the byte from inside the loop, at the first read that shows ROOM.
Hint 3
After the loop, nothing has been written yet. Return -1.
What the grader checks: A host-native bench: each call is one transfer to a register model of UART0. It checks the order and number of accesses, not bus timing.
12 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