Template-Based Register Access

Medium C++ Architecture
Solve this problem

Implement a C++ template wrapper class to access memory-mapped hardware registers. The class must enforce type safety, provide read/write, and allow compile-time address specification.

Constraints

  • Template parameter = address.
  • volatile uint32_t* used internally.
  • read() returns register content.
  • write() writes value.
  • set_bit() and clear_bit() manipulate bit.
  • inline and constexpr allowed.
Starter codeC++
1#include <stdint.h>2template<uint32_t addr>3class reg32 {4public:5    uint32_t read();6    void write(uint32_t v);7    void set_bit(int b);8    void clear_bit(int b);9};