Implement a memory-mapped I/O driver in C with a macro-based abstraction layer that lets…
- User
Implement a memory-mapped I/O driver in C with a macro-based abstraction layer that lets the same code compile for both physical hardware (writing to raw pointers) and PC simulation (writing to a byte array) for unit testing.
- Assistant
Implementing macro-based memory-mapped I/O abstraction
The plan defines a macro-based C abstraction that switches between real hardware register access via volatile pointers and simulation via a byte array with address offsetting, demonstrated with a simple LED device driver and a minimal test harness using the same code compiled under different modes. A simple way to make the same driver source usable on two very different targets is to push all “how do I turn an address into storage?” logic behind a handful of macros. The driver then only ever uses those macros to read and write its registers.
The example below shows an imaginary LED controller with three 32‑bit registers. On a real target the registers live at
0x4000_0000,0x4000_0004, … and you have to access them through volatile pointers. In the simulation build the same addresses are mapped into an element of a byte array so you can run the driver in a unit test:/* mmio.h - abstraction layer */ #ifndef MMIO_H #define MMIO_H #include <stdint.h> /* * Define SIMULATION when building on your PC. * For hardware builds, leave SIMULATION undefined. */ #ifdef SIMULATION /* Backing store for our simulated peripheral registers */ extern uint8_t g_sim_hw_mem[]; /* The base address your device is mapped to in the real address map */ #define SIM_HW_BASE 0x40000000U /* Helpers to turn an absolute address into an index into g_sim_hw_mem */ #define __SIM_IDX(addr) ((addr) - SIM_HW_BASE) /* Width-specific accessors */ #define MMIO8(addr) (g_sim_hw_mem[__SIM_IDX(addr)]) #define MMIO16(addr) (*((uint16_t *)&g_sim_hw_mem[__SIM_IDX(addr)])) #define MMIO32(addr) (*((uint32_t *)&g_sim_hw_mem[__SIM_IDX(addr)])) #else /* hardware build */ #define MMIO8(addr) (*(volatile uint8_t *)(addr)) #define MMIO16(addr) (*(volatile uint16_t *)(addr)) #define MMIO32(addr) (*(volatile uint32_t *)(addr)) #endif /* SIMULATION */ #endif /* MMIO_H */The driver itself only ever uses
MMIO8/16/32:/* led_driver.h */ #ifndef LED_DRIVER_H #define LED_DRIVER_H #include <stdint.h> /* Base and offsets of the LED controller's registers */ #define LED_BASE 0x40000000U #define LED_REG_CTRL (LED_BASE + 0x00) #define LED_REG_STATUS (LED_BASE + 0x04) #define LED_REG_DATA (LED_BASE + 0x08) /* Bit definitions */ #define LED_CTRL_ENABLE (1u << 0) #define LED_STATUS_READY (1u << 0) /* Driver API */ void led_init(void); void led_set(uint32_t pattern); uint32_t led_get(void); #endif /* LED_DRIVER_H *//* led_driver.c */ #include "mmio.h" #include "led_driver.h" void led_init(void) { /* Enable the controller */ MMIO32(LED_REG_CTRL) = LED_CTRL_ENABLE; } void led_set(uint32_t pattern) { /* Wait until controller is ready to accept new data */ while ((MMIO32(LED_REG_STATUS) & LED_STATUS_READY) == 0) { /* spin */ } MMIO32(LED_REG_DATA) = pattern; } uint32_t led_get(void) { return MMIO32(LED_REG_DATA); }On hardware you simply compile without
-DSIMULATION, andMMIO32()becomes a volatile pointer dereference.For unit testing on a PC you provide a backing array and compile with
-DSIMULATION:/* test_led.c */ #include <assert.h> #include <string.h> #define SIMULATION 1 #include "mmio.h" #include "led_driver.h" /* Allocate enough bytes to cover our device's address range */ uint8_t g_sim_hw_mem[0x1000]; int main(void) { /* Make status register look ready in simulation */ memset(g_sim_hw_mem, 0, sizeof g_sim_hw_mem); /* In this simple example status is at offset 4 from base */ *(uint32_t *)&g_sim_hw_mem[LED_REG_STATUS - SIM_HW_BASE] = LED_STATUS_READY; led_init(); assert(*(uint32_t *)&g_sim_hw_mem[LED_REG_CTRL - SIM_HW_BASE] == LED_CTRL_ENABLE); led_set(0xA5A5A5A5); assert(*(uint32_t *)&g_sim_hw_mem[LED_REG_DATA - SIM_HW_BASE] == 0xA5A5A5A5); return 0; }Because the driver only ever sees
MMIO32()reads and writes, the same source compiles and runs in both environments. You can extend the abstraction with additional helpers (barriers, bit‑set/clear, etc.) as your codebase requires.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.