Stub out reading registers and memory

This commit is contained in:
2025-01-01 18:15:41 -05:00
parent 9519897711
commit d016538408
5 changed files with 97 additions and 24 deletions
+17 -13
View File
@@ -1,3 +1,5 @@
use num_traits::ToBytes;
pub struct Response {
buffer: Vec<u8>,
checksum: u8,
@@ -17,22 +19,24 @@ impl Response {
}
pub fn write_str(mut self, str: &str) -> Self {
for char in str.as_bytes() {
self.buffer.push(*char);
self.checksum = self.checksum.wrapping_add(*char);
for byte in str.as_bytes() {
self.buffer.push(*byte);
self.checksum = self.checksum.wrapping_add(*byte);
}
self
}
pub fn write_hex_u8(mut self, value: u8) -> Self {
for digit in [(value >> 4), (value & 0xf)] {
let char = if digit > 9 {
b'a' + digit - 10
} else {
b'0' + digit
};
self.buffer.push(char);
self.checksum = self.checksum.wrapping_add(char);
pub fn write_hex<T: ToBytes>(mut self, value: T) -> Self {
for byte in value.to_be_bytes().as_ref() {
for digit in [(byte >> 4), (byte & 0xf)] {
let char = if digit > 9 {
b'a' + digit - 10
} else {
b'0' + digit
};
self.buffer.push(char);
self.checksum = self.checksum.wrapping_add(char);
}
}
self
}
@@ -40,6 +44,6 @@ impl Response {
pub fn finish(mut self) -> Vec<u8> {
let checksum = self.checksum;
self.buffer.push(b'#');
self.write_hex_u8(checksum).buffer
self.write_hex(checksum).buffer
}
}