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
use self::super::super::super::{ReadWriteMarker, ReadWritable};
use num_traits::{Unsigned, PrimInt, Num};
use std::ops::{DerefMut, Deref};
use std::mem::size_of;
use std::fmt;
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SpecialPurposeRegister<T: Num + Unsigned + PrimInt> {
data: T,
name: &'static str,
short: &'static str,
rw: ReadWriteMarker,
}
impl<T: Num + Unsigned + PrimInt> SpecialPurposeRegister<T> {
pub fn new(name: &'static str, short: &'static str) -> SpecialPurposeRegister<T> {
SpecialPurposeRegister {
data: T::zero(),
name: name,
short: short,
rw: ReadWriteMarker::new(),
}
}
#[inline]
pub fn name(&self) -> &'static str {
self.name
}
#[inline]
pub fn short_name(&self) -> &'static str {
self.short
}
}
impl<T: Num + Unsigned + PrimInt> ReadWritable for SpecialPurposeRegister<T> {
#[inline]
fn was_read(&self) -> bool {
self.rw.was_read()
}
#[inline]
fn was_written(&self) -> bool {
self.rw.was_written()
}
#[inline]
fn reset_rw(&mut self) {
self.rw.reset()
}
}
impl<T: Num + Unsigned + PrimInt> Deref for SpecialPurposeRegister<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.rw.read();
&self.data
}
}
impl<T: Num + Unsigned + PrimInt> DerefMut for SpecialPurposeRegister<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.rw.written();
&mut self.data
}
}
impl<T: Num + Unsigned + PrimInt + fmt::Display + fmt::UpperHex> fmt::Display for SpecialPurposeRegister<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({:0w$X})", self.short, self.data, w = size_of::<T>() * 2)
}
}