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
//! Provides safe, strongly-typed access to registers (e.g. memory-mapped or system registers).
use core::cmp;
use core::ops::{self, RangeInclusive};

pub mod memory_mapped;
pub mod system;

/// Useful types for implementing registers: [`RegisterSpec`] and associated marker traits, along
/// with [`RegisterReader`] and [`RegisterWriter`].
pub mod prelude {
    pub use super::RegisterSpec;

    // Markers for RegisterSpec.
    pub use super::{RegisterInitial, RegisterReadable, RegisterWritable};

    // Required to implement named bit/field accessors.
    pub use super::{RegisterReader, RegisterWriter};
}

/// Values which can be used as the underlying storage for a register.
pub trait RegisterBits:
    Copy
    + From<bool>
    + cmp::PartialEq<Self>
    + ops::BitAnd<Self, Output = Self>
    + ops::BitOr<Self, Output = Self>
    + ops::Not<Output = Self>
    + ops::Shl<usize, Output = Self>
    + ops::Shr<usize, Output = Self>
{
    /// Returns `0`.
    fn zero() -> Self;

    /// Returns a contiguous sequence of `width` bits with value `1`, beginning at the LSB.
    fn mask(width: usize) -> Self;
}

pub trait RegisterSpec {
    /// The type of the raw value of the register.
    type Bits: RegisterBits;
}

/// Marker for register specs (i.e. types implementing [`RegisterSpec`]) indicating that the
/// underlying register can be read from.
pub trait RegisterReadable: RegisterSpec {}

/// Marker for register specs (i.e. types implementing [`RegisterSpec`]) indicating that the
/// underlying register can be written to.
pub trait RegisterWritable: RegisterSpec {}

/// Marker for writable register specs (i.e. types implementing [`RegisterSpec`] and
/// [`RegisterWritable`]) which provides a safe, potentially non-zero initial value for the register
/// during write operations.
///
/// Writing the initial value to the register must **not** result in any undefined behaviour.
pub trait RegisterInitial: RegisterWritable {
    /// The initial value.
    const INITIAL_VALUE: Self::Bits;
}

/// Provides read access to the fields of a register.
pub struct RegisterReader<S: RegisterSpec> {
    bits: S::Bits,
}

/// Provides write access to the fields of a register.
pub struct RegisterWriter<S: RegisterSpec> {
    bits: S::Bits,
}

impl<S: RegisterSpec> RegisterReader<S> {
    fn new(bits: S::Bits) -> Self {
        Self { bits }
    }
}

impl<S: RegisterSpec> RegisterWriter<S> {
    fn zero() -> Self {
        Self {
            bits: S::Bits::zero(),
        }
    }
}

impl<S: RegisterSpec + RegisterInitial> RegisterWriter<S> {
    fn initial() -> Self {
        Self {
            bits: S::INITIAL_VALUE,
        }
    }
}

impl<S: RegisterSpec> RegisterReader<S> {
    /// Returns the raw value.
    pub fn bits(&self) -> S::Bits {
        self.bits
    }

    /// Returns the value of the bit at offset `offset`.
    pub fn bit(&self, offset: usize) -> bool {
        self.field(offset..=offset) != S::Bits::zero()
    }

    /// Returns the value of a contiguous bit field with its LSB at the offset `range.start()` and
    /// MSB at the offset `range.end()`.
    ///
    /// ```text
    ///   bits: | ... | |x|x|x| ... |x|x| | ... | | | | |
    ///                  ^             ^               ^
    /// offset:          range.end()   range.start()   0
    /// ```
    pub fn field(&self, range: RangeInclusive<usize>) -> S::Bits {
        let FieldSpec { offset, size } = range.field_spec();

        (self.bits >> offset) & S::Bits::mask(size)
    }
}

impl<S: RegisterSpec> RegisterWriter<S> {
    /// Sets the raw value.
    ///
    /// # Safety
    /// Setting an unsupported value may result in undefined behaviour. Refer to the register's
    /// definition to determine valid values.
    pub unsafe fn bits(&mut self, bits: S::Bits) {
        self.bits = bits;
    }

    /// Sets the value of the bit at offset `offset`.
    ///
    /// # Safety
    /// Setting an unsupported value may result in undefined behaviour. Refer to the register's
    /// definition to determine valid values.
    pub unsafe fn bit(&mut self, offset: usize, bit: bool) {
        self.field(offset..=offset, bit.into());
    }

    /// Sets the value of a contiguous bit field with its LSB at the offset `range.start()` and MSB
    /// at the offset `range.end()`.
    ///
    /// ```text
    ///   bits: | ... | |x|x|x| ... |x|x| | ... | | | | |
    ///                  ^             ^               ^
    /// offset:          range.end()   range.start()   0
    /// ```
    ///
    /// # Safety
    /// Setting an unsupported value may result in undefined behaviour. Refer to the register's
    /// definition to determine valid values.
    pub unsafe fn field(&mut self, range: RangeInclusive<usize>, field: S::Bits) {
        let FieldSpec { offset, size } = range.field_spec();
        let mask = S::Bits::mask(size);

        self.bits = (self.bits & !(mask << offset)) | ((field & mask) << offset);
    }
}

macro_rules! register_bits {
    ($ty:ty, $width:literal) => {
        impl RegisterBits for $ty {
            fn zero() -> Self {
                0
            }

            fn mask(width: usize) -> Self {
                <$ty>::MAX >> ($width - width)
            }
        }

        impl RegisterSpec for $ty {
            type Bits = $ty;
        }
    };
}

register_bits!(u64, 64);
register_bits!(u32, 32);
register_bits!(u16, 16);
register_bits!(u8, 8);

/// Offset and size of a field.
struct FieldSpec {
    /// Offset of the LSB of this field.
    offset: usize,
    /// Number of bits within this field.
    size: usize,
}

/// Extension trait to provide offset and size values from a [`RangeInclusive`].
trait RangeInclusiveExt {
    /// Field offset and size represented by this [`RangeInclusive`].
    fn field_spec(&self) -> FieldSpec;
}

impl RangeInclusiveExt for RangeInclusive<usize> {
    fn field_spec(&self) -> FieldSpec {
        FieldSpec {
            offset: *self.start(),
            size: self.end() + 1 - self.start(),
        }
    }
}