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/inhx.rs | |
| parent | dba769e13ca5029643c6068e53fa34ae0fea8421 (diff) | |
| download | torque-asm-da5c8173a56d5be7fa23d2b18eaba1542aa31dd6.zip | |
Implement inhx format
inhx is the original Intel hex format.
Diffstat (limited to 'src/formats/inhx.rs')
| -rw-r--r-- | src/formats/inhx.rs | 38 | 
1 files changed, 38 insertions, 0 deletions
diff --git a/src/formats/inhx.rs b/src/formats/inhx.rs new file mode 100644 index 0000000..e83e870 --- /dev/null +++ b/src/formats/inhx.rs @@ -0,0 +1,38 @@ +use crate::*; + + +pub fn format_inhx(words: &[Word]) -> String { +    let mut records = Vec::new(); +    for (i, chunk) in words.chunks(16).enumerate() { +        records.push(data_record(chunk, (i * 16) as u16)); +    } +    records.push(terminating_record()); + +    let mut output = String::new(); +    for record in records { +        output.push_str(&record.to_string()); +    } +    return output; +} + +fn data_record(words: &[Word], address: u16) -> InhxRecord { +    let mut record = InhxRecord::new(); +    record.byte((words.len()) as u8); +    record.be_double(address); +    record.byte(0x00); +    for word in words { +        match word.bits <= 8 { +            true => record.byte(word.value as u8), +            false => panic!("Word '{word}' has more than 8 bits."), +        }; +    } +    return record; +} + +fn terminating_record() -> InhxRecord { +    let mut record = InhxRecord::new(); +    record.byte(0x00); +    record.be_double(0x0000); +    record.byte(0x01); +    return record; +}  | 
