V0.2.1 Select a Pad Function

MediumImplement20 minVerilator + your C over DPI · serverReview the topic

Write pinmux_select in C: change one pad's two-bit field in ap_sysctl PINMUX with one read and one write, and refuse bad arguments.

Hardware context: Host-native models of ap_sysctl PINMUX and the GPIO interrupt words of ap_soc. Pin setup code shared by several drivers.

PINMUX in ap_sysctl gives each of four pads two bits. The value picks what the pad serves: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Other tests and drivers own the other pads, so a write that touches their bits breaks them.

Write int pinmux_select(int pad, int func). For pad 0 to 3 and func 0 to 3, read PINMUX once and replace only the field of that pad. Then write PINMUX once and return 0. For any other pad or func: return -1 and make no bus access at all.

Full specification
AddressRegisterBitsMeaning
0x1000_4010ap_sysctl PINMUX1:0, 3:2, 5:4, 7:6Pad 0, pad 1, pad 2, pad 3: 0 released, 1 GPIO, 2 serial, 3 auxiliary

Bits 31:8 read 0 and ignore writes. One write per call: two writes would make the pad pass through another function for a moment.

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
pinmux_task.h (given, read only)
/* pinmux_task.h: the function you write. */
#ifndef PINMUX_TASK_H
#define PINMUX_TASK_H
#include "hal.h"
#ifdef __cplusplus
extern "C" {
#endif
int pinmux_select(int pad, int func);
#ifdef __cplusplus
}
#endif
#endif

Examples

Example 1

GPIO on Pad 0 of an Empty Mux
Input
PINMUX = 0x00; pinmux_select(0, 1)
Output
returned = 0x0, pinmux_after = 0x1, reads = 1, writes = 1, bad_accesses = 0

Pad 0 is bits 1:0. The register becomes 01: one read, one write.

Example 2

Release Pad 2, Others Set
Input
PINMUX = 0xff; pinmux_select(2, 0)
Output
returned = 0x0, pinmux_after = 0xcf, reads = 1, writes = 1, bad_accesses = 0

Pad 2 is bits 5:4. Clearing it turns ff into cf; the other pads keep 3.

Example 3

Pad 4 Does Not Exist
Input
PINMUX = 0x5a; pinmux_select(4, 1)
Output
returned = 0xff, pinmux_after = 0x5a, reads = 0, writes = 0, bad_accesses = 0

There is no pad 4. Return -1 (FF), leave 5a, and make no access.

Constraints and Contract

  • Exactly one read and one write of PINMUX for a good call; no access at all for a refused call. No other address.
  • Each case packs four bytes. The first two are your return value (low byte) and PINMUX after the call.
  • The third holds the read and write counts, one hex digit each.
  • The last byte counts accesses to other addresses.

Hints

Hint 1

The field of pad p starts at bit 2 * p and is two bits wide. So its mask is 3 shifted left by 2 * p.

Hint 2

Clear the field with AND NOT mask, then OR in the new value at the same shift.

Hint 3

Check the arguments before the first bus access.

What the grader checks: A host-native bench with a register model of PINMUX. It checks the value written and the number of accesses, not the pads themselves.

14 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

GPIO on Pad 0 of an Empty Mux

Input
PINMUX = 0x00; pinmux_select(0, 1)
Expected output
returned = 0x0, pinmux_after = 0x1, reads = 1, writes = 1, bad_accesses = 0

Pad 0 is bits 1:0. The register becomes 01: one read, one write.

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

Verilator + your C over DPI · server