~chloekek/watch

watch/firmware/memory.d -rw-r--r-- 711 bytes View raw
88ad0bb7 — ChloĆ© Keep track of the time and display seconds using LEDs a month ago
                                                                                
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
module watch.memory;

public import core.volatile : volatileLoad, volatileStore;

import std.traits : rvalueOf;

/**
 * Load the value at the address,
 * pass it to the given function,
 * and store the result back.
 * The load and the store are both volatile.
 */
void volatileModify(alias F)(ubyte* addr)
    if (is(typeof(F(rvalueOf!ubyte)) : ubyte))
{
    const value = volatileLoad(addr);
    volatileStore(addr, F(value));
}

/**
 * Set the ith bit of the value x.
 */
nothrow pure @nogc @safe
ubyte set(ubyte i)(ubyte x)
{
    return cast(ubyte) (x | 1 << i);
}

/**
 * Clear the ith bit of the value x.
 */
nothrow pure @nogc @safe
ubyte clr(ubyte i)(ubyte x)
{
    return cast(ubyte) (x & ~(1 << i));
}