diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-28 14:35:04 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-28 14:35:04 +1300 |
commit | da5c8173a56d5be7fa23d2b18eaba1542aa31dd6 (patch) | |
tree | 6ef7aa76568bfa2488b0d0d9a878016eabadcae0 /src/formats/mod.rs | |
parent | dba769e13ca5029643c6068e53fa34ae0fea8421 (diff) | |
download | torque-asm-da5c8173a56d5be7fa23d2b18eaba1542aa31dd6.zip |
Implement inhx format
inhx is the original Intel hex format.
Diffstat (limited to 'src/formats/mod.rs')
-rw-r--r-- | src/formats/mod.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/formats/mod.rs b/src/formats/mod.rs index 42d198c..82f19f1 100644 --- a/src/formats/mod.rs +++ b/src/formats/mod.rs @@ -1,2 +1,45 @@ +mod inhx; mod inhx32; + +pub use inhx::*; pub use inhx32::*; + + +pub struct InhxRecord { + bytes: Vec<u8>, +} + +impl InhxRecord { + pub fn new() -> Self { + Self { bytes: Vec::new() } + } + + pub fn byte(&mut self, byte: u8) { + self.bytes.push(byte); + } + + pub fn be_double(&mut self, double: u16) { + let [high, low] = double.to_be_bytes(); + self.byte(high); + self.byte(low); + } + + pub fn le_double(&mut self, double: u16) { + let [high, low] = double.to_be_bytes(); + self.byte(low); + self.byte(high); + } + + pub fn to_string(self) -> String { + let mut sum: u8 = 0; + for byte in &self.bytes { + sum = sum.wrapping_add(*byte); + } + let checksum = sum.wrapping_neg(); + let mut output = String::new(); + for byte in &self.bytes { + output.push_str(&format!("{byte:0>2X}")); + } + format!(":{output}{checksum:0>2X}\n") + } +} |