aboutsummaryrefslogtreecommitdiffstats
path: root/funreg.hpp
blob: db0bb37ec9053fd78adbd6f5905d223044c424df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/**
 * funreg.hpp - Functional register I/O using modern C++.
 * Written by Clyne Sullivan.
 * <https://github.com/tcsullivan/funreg>
 */

#ifndef FUNCTIONAL_REGISTER_IO_H
#define FUNCTIONAL_REGISTER_IO_H

/**
 * Comment to disable external/custom register access.
 * When disabled, only memory-mapped register access is supported.
 * fr::Register can then also be used instead of fr::MemRegister.
 */
#define FUNREG_ENABLE_EXTERNAL_IO

#include <stdint.h>

#ifdef FUNREG_ENABLE_EXTERNAL_IO
#include <type_traits>
#endif

namespace fr {

// A utility to measure a bit-mask's offset from bit zero.
template<auto Mask, unsigned int N = 0>
constexpr auto BitOffset = []() constexpr {
    if constexpr (Mask & 1)
        return N;
    else
        return BitOffset<(Mask >> 1), (N + 1)>;
}();

/**
 * @struct MemoryIO
 * @brief  Specifies how to access a memory-mapped register.
 * @tparam T    The size of the register.
 * @tparam Addr The memory address of the register.
 *
 * To create an I/O access type for external register access, use this
 * structure as a template.
 */
template<typename T, uintptr_t Addr>
struct MemoryIO {
    using type = T;
    constexpr static auto addr = Addr;

    /**
     * Reads the register's value.
     */
    constexpr static T read() {
        return *reinterpret_cast<volatile T*>(Addr);
    }

    /**
     * Overwrites the register's value.
     */
    constexpr static void write(const T& value) {
        *reinterpret_cast<volatile T*>(Addr) = value;
    }
};

/**
 * @struct Register
 * @brief  Defines a register, given how to access it.
 * @tparam Access Specifies register access. See MemoryIO for an example.
 *
 * When FUNREG_ENABLE_EXTERNAL_IO is not defined, Register assumes MemoryIO
 * access. The template parameters become that of MemoryIO.
 */
#ifdef FUNREG_ENABLE_EXTERNAL_IO
template<typename Access>
struct Register {
    using access = Access;
    using T = typename Access::type;
    constexpr static auto Addr = Access::addr;
#else
template<typename T, uintptr_t Addr>
struct Register {
    using RegAccess = MemoryIO<T, Addr>;
#endif // FUNREG_ENABLE_EXTERNAL_IO

    /**
     * Gets a pointer to the register.
     */
    constexpr static T read() {
        return Access::read();
    }

    /**
     * Overwrites the register's value.
     */
    constexpr static void write(const T& value) {
        Access::write(value);
    }

    /**
     * Sets register bits to '1' according to the given RegisterMasks.
     */
    template<typename... Masks>
    static void set() {
        apply<Masks...>([](auto r, auto m) { return r | m; });
    }

    /**
     * Sets register bits to '1' according to the given mask.
     */
    static void set(const T& mask) {
        write(read() | mask);
    }

    /**
     * Clears register bits to '0' according to the given RegisterMasks.
     */
    template<typename... Masks>
    static void clear() {
        apply<Masks...>([](auto r, auto m) { return r & ~m; });
    }

    /**
     * Clears register bits to '0' according to the given mask.
     */
    static void clear(const T& mask) {
        write(read() & ~mask);
    }

    /**
     * Toggles bits in the register according to the given RegisterMasks.
     */
    template<typename... Masks>
    static void toggle() {
        apply<Masks...>([](auto r, auto m) { return r ^m; });
    }

    /**
     * Toggles bits in the register according to the given mask.
     */
    static void toggle(const T& mask) {
        write(read() ^ mask);
    }

    /**
     * Reads the current value stored in the register, masking bits according to
     * the given RegisterMasks.
     * If no masks are given, all register bits are returned.
     */
    template<typename... Masks>
    static auto read() {
        if constexpr (sizeof...(Masks) > 0)
            return read() & mergeMasks<Masks...>();
        else
            return read();
    }

    /**
     * Reads the register, and tests if all of the given bits are set.
     * If no masks are given, tests if the register has a non-zero value.
     */
    template<typename... Masks>
    static bool test() {
        if constexpr (sizeof...(Masks) > 0) {
            auto mask = mergeMasks<Masks...>();
            return (read() & mask) == mask;
        } else {
            return read() != 0;
        }
    }

    /**
     * Modifies the register's contents according to the given operations.
     * The register will only be read and written once.
     * Possible operations include RegisterMask::set, RegisterMask::clear,
     * RegisterMask::toggle, RegisterMask::write<>, RegisterMaskValue::set,
     * and RegisterMaskValue::clear.
     */
    template<typename... Ops>
    static void modify() {
        if constexpr ((isThis<typename Ops::reg> | ...)) {
            auto mask = read();
            ([&mask] {
                if constexpr (isThis<typename Ops::reg>)
                    mask = Ops(mask);
            }(), ...);
            write(mask);
        }
    }

    // Below is meant for internal use only.

    // Applies bit-masks to the register through the provided function.
    // The provided function receives a pointer to the register's data and a
    // bit-mask created by merging all provided bit-masks.
    // If no masks are given, a mask selecting all bits is used.
    template<typename... Masks>
    static constexpr void apply(auto fn) {
        if constexpr (sizeof...(Masks) > 0) {
            constexpr auto mask = mergeMasks<Masks...>();
            if constexpr (mask)
                write(fn(read(), mask));
        } else {
            write(fn(read(), T(0) - 1));
        }
    }

    // Takes a list of bit-masks, and returns a merged mask of those which are
    // meant for this register.
    template<typename... Masks>
    static constexpr auto mergeMasks() {
        if constexpr (sizeof...(Masks) > 0) {
            if constexpr ((isThis<typename Masks::reg> | ...)) {
                auto mask =
                    ([] {
                        return isThis<typename Masks::reg> ? Masks::mask : 0;
                    }() | ...);
                return mask;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

#ifdef FUNREG_ENABLE_EXTERNAL_IO
    // Determines if the given register matches this one.
    template<typename Reg>
    constexpr static bool isThis = [] {
        return std::is_same_v<typename Reg::access, access> && Addr == Reg::Addr;
    }();
#else
    // Determines if the given register matches this one.
    template<typename Reg>
    constexpr static bool isThis = [] {
        return Addr == Reg::Addr;
    }();
#endif // FUNREG_ENABLE_EXTERNAL_IO

    Register() = delete;

    using type = T;
};

/**
 * @struct RegisterMask
 * @brief  Defines a bit mask that can be used with the specified register.
 * @tparam Reg  The Register that this mask belongs to.
 * @tparam Mask A mask selecting the bits that the RegisterMask can modify.
 *
 * Pairs together a bit mask and the register the mask is meant for.
 * For example, a single LED is controlled by bit 2 on the GPIO_OUT register:
 *     using LED_RED = RegisterMask<GPIO_OUT, (1 << 2)>;
 */
template<typename Reg, typename Reg::type Mask>
struct RegisterMask
{
    using T = typename Reg::type;

    /**
     * Sets all bits in the bit-mask to "1".
     * Call with no arguments (LED_REG::set()) to affect the paired register.
     * Calling with an argument sets bits in the argument as if it were a
     * register, returning the resulting value.
     */
    struct set {
        constexpr set() {
            Reg::write(Reg::read() | Mask);
        }

        // For internal use.
        using reg = Reg;
        T modmask;
        constexpr set(auto r): modmask(r | Mask) {}
        constexpr operator T() const { return modmask; }
    };

    /**
     * Clears all bits in the bit-mask to "0".
     * See RegisterMask::set for calling conventions.
     */
    struct clear {
        constexpr clear() {
            Reg::write(Reg::read() & ~Mask);
        }

        // For internal use.
        using reg = Reg;
        T modmask;
        constexpr clear(auto r): modmask(r & ~Mask) {}
        constexpr operator T() const { return modmask; }
    };

    /**
     * Toggles all bits in the bit-mask.
     * See RegisterMask::set for calling conventions.
     */
    struct toggle {
        constexpr toggle() {
            Reg::write(Reg::read() ^ Mask);
        }

        // For internal use.
        using reg = Reg;
        T modmask;
        constexpr toggle(auto r): modmask(r ^ Mask) {}
        constexpr operator T() const { return modmask; }
    };

    /**
     * Reads from the paired register, applying the bit-mask.
     */
    static auto read() {
        return Reg::read() & Mask;
    }

    /**
     * Applies the bit-mask to the given register value, returning the result.
     * This is useful in case the register's value has already been read; or, if
     * the mask needs to be applied to a different value or register.
     * @see Mask<>
     */
    static auto read(const T& regval) {
        return regval & Mask;
    }

    /**
     * Writes the given value to the register.
     * Writing is accomplished by clearing the bit-mask, then OR-ing the value
     * to the bit-mask's offset.
     * See RegisterMask::set for calling conventions, but note the additional
     * template parameter "value".
     */
    template<T value>
    struct write {
        constexpr write() {
            auto r = Reg::read();
            r &= ~Mask;
            r |= value << BitOffset<Mask>;
            Reg::write(r);
        }

        // For internal use.
        using reg = Reg;
        T modmask;
        constexpr write(auto r):
            modmask((r & ~Mask) | (value << BitOffset<Mask>)) {}
        constexpr operator T() const { return modmask; }
    };

    /**
     * Tests if all masked bits are set in the register.
     */
    static bool test() {
        return read() == Mask;
    }

    /**
     * Tests if all masked bits are set in the given register value.
     */
    static bool test(const T& regval) {
        return read(regval) == Mask;
    }

    RegisterMask() = delete;

    using reg = Reg;
    constexpr static auto mask = Mask;
};

///**
// * @struct RegisterMaskValue
// * @brief  Used to name the possible values of a multi-bit bit-mask.
// * @tparam Mask  The RegisterMask this value is associated with.
// * @tparam value The value to be used for the given Mask.
// */
//template<typename Mask, Mask::T value>
//struct RegisterMaskValue
//{
//    /**
//     * Call this directly to write the value into the register.
//     * Can also be used in modify() chains.
//     * @see RegisterMask::write()
//     * @see Register::modify()
//     */
//    using set = typename Mask::write<value>;
//
//    /**
//     * Call this to clear the value from the register.
//     */
//    using clear = typename Mask::clear;
//
//    /**
//     * Tests if this value is currently set in the register.
//     */
//    static bool test() {
//        return (Mask::read() & Mask::mask) == (value << BitOffset<Mask>);
//    }
//};

/**
 * @class  RegisterGroup
 * @brief  Groups registers together for unified operations.
 * @tparam Registers The registers to be included in this group.
 *
 * Allows for single operations to be carried out on multiple registers.
 * Masks for the same register are merged, resulting in single load/stores for
 * each register.
 */
template<typename... Registers>
class RegisterGroup
{
public:
    /**
     * Sets bits throughout this group's registers according to the given masks.
     * Bit-masks for the same register will be merged so that each register is
     * only written once.
     */
    template<typename... Masks>
    static void set() {
        apply<Masks...>([](auto r, auto m) { return r | m; });
    }

    /**
     * Clears bits throughout this group's registers according to the given
     * masks.
     * Only reads and writes each register once; see set().
     */
    template<typename... Masks>
    static void clear() {
        apply<Masks...>([](auto r, auto m) { return r & ~m; });
    }

    /**
     * Toggles bits throughout this group's registers according to the given
     * masks.
     * Only reads and writes each register once; see set().
     */
    template<typename... Masks>
    static void toggle() {
        apply<Masks...>([](auto r, auto m) { return r ^ m; });
    }

    /**
     * Modifies registers in this group according to the given operations.
     * Each register will only be read and written once.
     * Possible operations include RegisterMask::set, RegisterMask::clear,
     * RegisterMask::toggle, RegisterMask::write<>, RegisterMaskValue::set,
     * and RegisterMaskValue::clear.
     */
    template<typename... Ops>
    static void modify() {
        (Registers::template modify<Ops...>(), ...);
    }

private:
    template<typename... Masks>
    static void apply(auto fn) {
        (Registers::template apply<Masks...>(fn), ...);
    }
};

/**
 * Merges the bit-masks of the given RegisterMasks, ignoring register
 * assignment.
 * Useful if the bit-masks are needed for something besides the assigned
 * register.
 */
template<typename... RegMasks>
constexpr auto Masks = (RegMasks::mask | ...);

#ifdef FUNREG_ENABLE_EXTERNAL_IO
/**
 * Defines a register that is accessed through memory, i.e. memory-mapped.
 * @tparam T    The variable type used to access the register (e.g. uint32_t).
 * @tparam Addr The memory address of the register.
 */
template<typename T, uintptr_t Addr>
using MemRegister = Register<MemoryIO<T, Addr>>;

/**
 * Defines a register that is accessed through external or custom means.
 * @tparam ExtIO A type that provides access functionality (e.g. MemoryIO).
 * @tparam T     The variable type used to access the register (e.g. uint32_t).
 * @tparam Addr  The memory address of the register.
 *
 * Custom access types should be defined using MemoryIO as a template.
 */
template<template<typename, uintptr_t> typename ExtIO, typename T, uintptr_t Addr>
using ExtRegister = Register<ExtIO<T, Addr>>;
#else
/**
 * Defines a register that is accessed through memory, i.e. memory-mapped.
 * @tparam T    The variable type used to access the register (e.g. uint32_t).
 * @tparam Addr The memory address of the register.
 *
 * With external I/O disabled, the Register type may be used directly instead.
 */
template<typename T, uintptr_t Addr>
using MemRegister = Register<T, Addr>;
#endif // FUNREG_ENABLE_EXTERNAL_IO

} // namespace fr

#endif // FUNCTIONAL_REGISTER_IO_H