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
#![allow(non_upper_case_globals)] use std::ffi::{CStr};
use crate::adapter::adapter_backend::*;
pub type DataHandle = i32;
pub enum MemoryBlockNumber
{
MemoryBlock0,
#[allow(dead_code)] MemoryBlock1,
#[allow(dead_code)] MemoryBlock2,
}
pub struct DataReaderWriter {
_data_handle: DataHandle,
_block_number: MemoryBlockNumber,
pub memory_block_byte_size: usize,
}
impl DataReaderWriter {
pub fn new(block_number: MemoryBlockNumber) -> DataReaderWriter {
DataReaderWriter {
_data_handle: DDR_INVALID_HANDLE,
_block_number: block_number,
memory_block_byte_size: 0
}
}
pub fn open(&mut self) -> Result<(), String> {
let block_name = _block_number_to_name(&self._block_number);
let name_ptr = block_name.as_ptr() as *const ::std::os::raw::c_char;
let mut error_code = unsafe {
ddr_open_memory_block(name_ptr, &mut self._data_handle)
};
if let DDR_ErrorCode_Success = error_code {
let mut memory_size: ::std::os::raw::c_int = 0;
error_code = unsafe { ddr_get_memory_size(self._data_handle, &mut memory_size) };
if error_code == DDR_ErrorCode_Success {
self.memory_block_byte_size = (memory_size as usize) * 4;
Ok(())
}
else {
Err(_report_ddr_error(error_code, "Opening memory block"))
}
}
else
{
Err(_report_ddr_error(error_code, "Opening memory block"))
}
}
pub fn close(&mut self) -> Result<(), String> {
let error_code = unsafe { ddr_close_memory_block(self._data_handle) };
if let DDR_ErrorCode_Success = error_code {
self._data_handle = DDR_INVALID_HANDLE;
self.memory_block_byte_size = 0;
Ok(())
} else {
Err(_report_ddr_error(error_code, "Attempting to close memory block"))
}
}
fn _drop_close(&mut self) {
if self._data_handle != DDR_INVALID_HANDLE {
let _ = self.close();
}
}
pub fn read(&self, byte_offset: i32, max_bytes: usize) -> Result<Vec<u8>, String> {
let mut data: Vec<u8> = vec![0; max_bytes];
let mut chunk_offset = byte_offset / 4; let mut value: u32 = 0;
let mut error_code = unsafe { ddr_get_data_chunk(self._data_handle, chunk_offset, &mut value) };
if let DDR_ErrorCode_Success = error_code {
let mut buffer_index = 0;
let mut byte_offset_in_chunk = byte_offset % 4;
while buffer_index < max_bytes {
data[buffer_index] = value as u8;
buffer_index += 1;
value >>= 8;
byte_offset_in_chunk += 1;
if byte_offset_in_chunk == 4 {
chunk_offset += 1;
if chunk_offset >= DDR_MAX_OFFSET {
break;
}
byte_offset_in_chunk = 0;
error_code = unsafe { ddr_get_data_chunk(self._data_handle, chunk_offset, &mut value)};
if let DDR_ErrorCode_Success = error_code {
continue;
} else {
return Err(_report_ddr_error(error_code, "Reading successive memory chunk"));
}
}
}
Ok(data)
} else {
Err(_report_ddr_error(error_code, "Reading initial memory chunk"))
}
}
pub fn write(&self, byte_offset: i32, bytes_to_write: &Vec<u8>) -> Result<usize, String> {
let mut chunk_offset = byte_offset / 4;
let mut value: u32 = 0;
let mut byte_offset_in_chunk = byte_offset % 4;
let mut buffer_index: usize = 0;
let mut byte_mask : u32 = 0xffu32 << (byte_offset_in_chunk * 8);
let mut error_code = DDR_ErrorCode_Success;
if byte_offset_in_chunk != 0 {
error_code = unsafe { ddr_get_data_chunk(self._data_handle, chunk_offset, &mut value) };
}
if let DDR_ErrorCode_Success = error_code {
while buffer_index < bytes_to_write.len() {
value &= !byte_mask;
value |= (bytes_to_write[buffer_index] as u32) << (byte_offset_in_chunk * 8);
buffer_index += 1;
byte_mask <<= 8;
byte_offset_in_chunk += 1;
if byte_offset_in_chunk == 4 {
error_code = unsafe { ddr_set_data_chunk(self._data_handle, chunk_offset, value) };
if let DDR_ErrorCode_Success = error_code {
byte_mask = 0xffu32;
byte_offset_in_chunk = 0;
chunk_offset += 1;
if chunk_offset >= DDR_MAX_OFFSET {
break;
}
error_code = unsafe { ddr_get_data_chunk(self._data_handle, chunk_offset, &mut value) };
if let DDR_ErrorCode_Success = error_code {
} else {
return Err(_report_ddr_error(error_code, "Reading memory in preparation to writing memory"));
}
} else {
return Err(_report_ddr_error(error_code, "Writing memory"));
}
}
}
if byte_offset_in_chunk != 0 {
error_code = unsafe { ddr_set_data_chunk(self._data_handle, chunk_offset, value) };
if let DDR_ErrorCode_Success = error_code {
} else {
return Err(_report_ddr_error(error_code, "Writing memory"));
}
}
Ok(buffer_index)
} else {
Err(_report_ddr_error(error_code, "Reading memory in preparation to writing memory"))
}
}
}
impl Drop for DataReaderWriter {
fn drop(&mut self) {
self._drop_close();
}
}
fn _block_number_to_name<'a>(block_number: &MemoryBlockNumber) -> &'a str {
match block_number {
MemoryBlockNumber::MemoryBlock0 => {
let var_name = unsafe { CStr::from_ptr(block_name_0) };
var_name.to_str().unwrap()
},
MemoryBlockNumber::MemoryBlock1 => {
let var_name = unsafe { CStr::from_ptr(block_name_1) };
var_name.to_str().unwrap()
},
MemoryBlockNumber::MemoryBlock2 => {
let var_name = unsafe { CStr::from_ptr(block_name_2) };
var_name.to_str().unwrap()
},
}
}
fn _errorcode_to_string(error_code: DDR_ErrorCode) -> String {
match error_code {
DDR_ErrorCode_Success => String::from("Operation succeeded"),
DDR_ErrorCode_Block_Already_Opened => String::from("Memory block is already open and cannot be opened again"),
DDR_ErrorCode_Block_Not_Opened => String::from("Memory block is closed and cannot be accessed"),
DDR_ErrorCode_Invalid_Block_Name => String::from("The given name is not a recognized memory block name"),
DDR_ErrorCode_Invalid_Handle => String::from("The handle argument does not correspond to a valid open memory block"),
DDR_ErrorCode_Invalid_Offset => String::from("The given offset is out of bounds"),
DDR_ErrorCode_Null_Argument => String::from("The block name pointer or return handle pointer argument is NULL"),
_ => String::from("Unknown error"),
}
}
fn _report_error_message(message: String, prompt: &str) -> String {
format!("Error! {prompt}: {message}.")
}
fn _report_ddr_error(error_code: DDR_ErrorCode, prompt: &str) -> String {
let message = _errorcode_to_string(error_code);
_report_error_message(message, prompt)
}
pub fn adapter_buffer_to_string(data: &Vec<u8>, indent: usize) -> Result<String, String> {
let mut output = String::from("");
let bytes_per_row = 32;
let mut row = 0;
while row < data.len() {
let row_start = format!("{:>indent$}0x{row:04x} --", "");
output.push_str(&row_start);
let mut col = 0;
while col < bytes_per_row && (row + col) < data.len() {
let data_index = row + col;
let space = if col > 0 { " " } else { "" };
let field = format!("{space}{:02x}", data[data_index]);
output.push_str(&field);
col += 1;
}
output.push_str("\n");
row += bytes_per_row;
}
Ok(output)
}