Skip to content

Commit

Permalink
resolve #206: ignore blank space for intelhex format
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Jun 30, 2024
1 parent 762c03b commit 1ebf76e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 23 deletions.
83 changes: 60 additions & 23 deletions src/util/bitvec_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,42 +211,79 @@ impl util::BitVec
{
let mut result = String::new();

let mut bytes_left = self.len() / 8 + if self.len() % 8 != 0 { 1 } else { 0 };
let mut read_index = 0;

let mut index = 0;
while index < self.len()
let mut accum_index = 0;
let mut accum_bytes = Vec::<u8>::new();

let mut flush_bytes = |
read_index: usize,
accum_index: &mut usize,
accum_bytes: &mut Vec::<u8>|
{
let bytes_in_row = if bytes_left > 32 { 32 } else { bytes_left };
while let Some(0_u8) = accum_bytes.last()
{
accum_bytes.pop();
}

result.push(':');
result.push_str(&format!("{:02X}", bytes_in_row));
result.push_str(&format!("{:04X}", index / address_unit));
result.push_str("00");
while let Some(0_u8) = accum_bytes.first()
{
accum_bytes.remove(0);
*accum_index += 8;
}

let mut checksum = 0_u8;
checksum = checksum.wrapping_add(bytes_in_row as u8);
checksum = checksum.wrapping_add(((index / address_unit) >> 8) as u8);
checksum = checksum.wrapping_add((index / address_unit) as u8);
let length = accum_bytes.len() as u8;

for _ in 0..bytes_in_row
if length > 0
{
let mut byte: u8 = 0;
for _ in 0..8
let addr_hi = ((*accum_index / address_unit) >> 8) as u8;
let addr_lo = (*accum_index / address_unit) as u8;

result.push(':');
result.push_str(&format!("{:02X}", length));
result.push_str(&format!("{:02X}", addr_hi));
result.push_str(&format!("{:02X}", addr_lo));
result.push_str("00");

let mut checksum = 0_u8;
checksum = checksum.wrapping_add(length);
checksum = checksum.wrapping_add(addr_hi);
checksum = checksum.wrapping_add(addr_lo);

for byte in accum_bytes.iter().copied()
{
byte <<= 1;
byte |= if self.read_bit(index) { 1 } else { 0 };
index += 1;
result.push_str(&format!("{:02X}", byte));
checksum = checksum.wrapping_add(byte);
}

result.push_str(&format!("{:02X}", (!checksum).wrapping_add(1)));
result.push('\n');
}

result.push_str(&format!("{:02X}", byte));
checksum = checksum.wrapping_add(byte);
accum_bytes.clear();
*accum_index = read_index;
};

while read_index < self.len()
{
let mut byte: u8 = 0;
for _ in 0..8
{
byte <<= 1;
byte |= if self.read_bit(read_index) { 1 } else { 0 };
read_index += 1;
}

bytes_left -= bytes_in_row;
result.push_str(&format!("{:02X}", (!checksum).wrapping_add(1)));
result.push('\n');
accum_bytes.push(byte);

if accum_bytes.len() >= 32
{
flush_bytes(read_index, &mut accum_index, &mut accum_bytes);
}
}

flush_bytes(read_index, &mut accum_index, &mut accum_bytes);

result.push_str(":00000001FF");
result
}
Expand Down
14 changes: 14 additions & 0 deletions tests/driver/ok_format_intelhex/main.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ruledef test
{
halt => 0x55
}

halt
halt
#d "hello, world!"
#d "hello, world!"
#d "hello, world!"
#d "hello, world!"

; command: main.asm -f intelhex -o out.txt
; output: out.txt
3 changes: 3 additions & 0 deletions tests/driver/ok_format_intelhex/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:20000000555568656C6C6F2C20776F726C642168656C6C6F2C20776F726C642168656C6C3F
:160020006F2C20776F726C642168656C6C6F2C20776F726C64211D
:00000001FF
18 changes: 18 additions & 0 deletions tests/driver/ok_format_intelhex_blank/main.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ruledef test
{
halt => 0x55
}

#d64 0

halt

#d8 0

halt

#addr 0x200
#d "hello"

; command: main.asm -f intelhex -o out.txt
; output: out.txt
3 changes: 3 additions & 0 deletions tests/driver/ok_format_intelhex_blank/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:030008005500554B
:0502000068656C6C6FE5
:00000001FF

0 comments on commit 1ebf76e

Please sign in to comment.