summaryrefslogtreecommitdiff
path: root/src/formats/inhx32.rs
blob: fd7fd7b8e083509742e4a7111f1b6958df359045 (plain) (blame)
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
use crate::*;


pub fn format_inhx32(words: &[Word]) -> String {
    let mut records = Vec::new();
    records.push(extended_linear_address(0x0000));
    for (i, chunk) in words.chunks(8).enumerate() {
        records.push(data_record(chunk, (i * 8) 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() * 2) as u8);
    record.be_double(address * 2);
    record.byte(0x00);
    for word in words {
        match word.bits <= 16 {
            true => record.le_double(word.value as u16),
            false => panic!("Word '{word}' has more than 16 bits."),
        };
    }
    return record;
}

fn extended_linear_address(address: u16) -> InhxRecord {
    let mut record = InhxRecord::new();
    record.byte(0x02);
    record.be_double(0x0000);
    record.byte(0x04);
    record.be_double(address);
    return record;
}

fn terminating_record() -> InhxRecord {
    let mut record = InhxRecord::new();
    record.byte(0x00);
    record.be_double(0x0000);
    record.byte(0x01);
    return record;
}