-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface.rs
228 lines (208 loc) · 7.45 KB
/
interface.rs
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
/// Error type used.
type BoxedError = Box<InterfaceError>;
/// Enum to denote register type.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RegisterType {
/// Register will hold an i32.
I32,
/// Register will hold an f32.
F32,
/// Register will hold bytes.
Bytes,
}
impl TryFrom<u32> for RegisterType {
type Error = ();
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == RegisterType::I32 as u32 => Ok(RegisterType::I32),
x if x == RegisterType::F32 as u32 => Ok(RegisterType::F32),
x if x == RegisterType::Bytes as u32 => Ok(RegisterType::Bytes),
_ => Err(()),
}
}
}
/// Interface to control the unit, the unit controller uses this to interact with the unit.
pub trait Interface {
/// Retrieve the list of module ids that are available.
fn modules(&self) -> Result<Vec<u32>, BoxedError>;
/// Retrieve the name of a particular module.
fn module_name(&self, module: u32) -> Result<String, BoxedError>;
/// Return the available register ids in a particular module.
fn registers(&self, module: u32) -> Result<Vec<u32>, BoxedError>;
/// Retrieve a register name.
fn register_name(&self, module: u32, register: u32) -> Result<String, BoxedError>;
/// Retrieve a register type.
fn register_type(&self, module: u32, register: u32) -> Result<RegisterType, BoxedError>;
/// Get an i32 register.
fn get_i32(&self, module: u32, register: u32) -> Result<i32, BoxedError>;
/// Set an i32 register.
fn set_i32(&mut self, module: u32, register: u32, value: i32) -> Result<i32, BoxedError>;
/// Get an f32 register.
fn get_f32(&self, module: u32, register: u32) -> Result<f32, BoxedError>;
/// Set an f32 register.
fn set_f32(&mut self, module: u32, register: u32, value: f32) -> Result<f32, BoxedError>;
/// Get the length required to read a byte register.
fn get_bytes_len(&self, module: u32, register: u32) -> Result<usize, BoxedError>;
/// Get the actual bytes of a byte register, returns the number of bytes written.
fn get_bytes(
&self,
module: u32,
register: u32,
destination: &mut [u8],
) -> Result<usize, BoxedError>;
/// Set a byte register.
fn set_bytes(&mut self, module: u32, register: u32, values: &[u8]) -> Result<(), BoxedError>;
}
/// If an error occurs in the interface, the following boxed error is returned.
#[derive(Debug, Clone)]
pub struct InterfaceError {
/// The module interacted with when this error occured.
pub module: u32,
/// The register interacted with when this error occured.
pub register: u32,
/// The type of error that occured.
pub error_type: InterfaceErrorType,
}
/// The error_type is further specified in the following possible failure modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InterfaceErrorType {
/// This module does not exist.
NoSuchModule,
/// This register does not exist in this module.
NoSuchRegister,
/// This register is of another type.
WrongType,
/// The destination buffer could not hold enough values.
ReadOverflow,
/// Too many values are being written, the register takes less.
WriteOverflow,
/// Too few values are being written, the register requires more.
WriteUnderflow,
}
impl TryFrom<u32> for InterfaceErrorType {
type Error = ();
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == InterfaceErrorType::NoSuchModule as u32 => {
Ok(InterfaceErrorType::NoSuchModule)
}
x if x == InterfaceErrorType::NoSuchRegister as u32 => {
Ok(InterfaceErrorType::NoSuchRegister)
}
x if x == InterfaceErrorType::WrongType as u32 => Ok(InterfaceErrorType::WrongType),
x if x == InterfaceErrorType::ReadOverflow as u32 => {
Ok(InterfaceErrorType::ReadOverflow)
}
x if x == InterfaceErrorType::WriteOverflow as u32 => {
Ok(InterfaceErrorType::WriteOverflow)
}
x if x == InterfaceErrorType::WriteUnderflow as u32 => {
Ok(InterfaceErrorType::WriteUnderflow)
}
_ => Err(()),
}
}
}
/// Finally, it implements display.
impl std::fmt::Display for InterfaceError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.error_type {
InterfaceErrorType::NoSuchModule => {
write!(f, "{:0>8x}:# does not exist", self.module)
}
InterfaceErrorType::NoSuchRegister => {
write!(
f,
"{:0>8x}:{:0>8x} does not exist for this module",
self.module, self.register
)
}
InterfaceErrorType::WrongType => {
write!(
f,
"{:0>8x}:{:0>8x} has a different type",
self.register, self.module
)
}
InterfaceErrorType::ReadOverflow => {
write!(
f,
"{:0>8x}:{:0>8x} destination buffer not large enough",
self.register, self.module
)
}
InterfaceErrorType::WriteOverflow => {
write!(
f,
"{:0>8x}:{:0>8x} input data exceeds register size",
self.register, self.module
)
}
InterfaceErrorType::WriteUnderflow => {
write!(
f,
"{:0>8x}:{:0>8x} register size exceeds input data",
self.register, self.module
)
}
}
}
}
/// And error, such that it is convertible to [`Box<dyn std::error:Error>`]
impl std::error::Error for InterfaceError {}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_error_conversion() {
assert_eq!(
InterfaceErrorType::NoSuchModule,
(InterfaceErrorType::NoSuchModule as u32)
.try_into()
.unwrap()
);
assert_eq!(
InterfaceErrorType::NoSuchRegister,
(InterfaceErrorType::NoSuchRegister as u32)
.try_into()
.unwrap()
);
assert_eq!(
InterfaceErrorType::WrongType,
(InterfaceErrorType::WrongType as u32).try_into().unwrap()
);
assert_eq!(
InterfaceErrorType::ReadOverflow,
(InterfaceErrorType::ReadOverflow as u32)
.try_into()
.unwrap()
);
assert_eq!(
InterfaceErrorType::WriteOverflow,
(InterfaceErrorType::WriteOverflow as u32)
.try_into()
.unwrap()
);
assert_eq!(
InterfaceErrorType::WriteUnderflow,
(InterfaceErrorType::WriteUnderflow as u32)
.try_into()
.unwrap()
);
}
#[test]
fn test_register_type_conversion() {
assert_eq!(
RegisterType::I32,
(RegisterType::I32 as u32).try_into().unwrap()
);
assert_eq!(
RegisterType::F32,
(RegisterType::F32 as u32).try_into().unwrap()
);
assert_eq!(
RegisterType::Bytes,
(RegisterType::Bytes as u32).try_into().unwrap()
);
}
}