diff --git a/src/util/bitvec_format.rs b/src/util/bitvec_format.rs index 78d9914d..ec706a81 100644 --- a/src/util/bitvec_format.rs +++ b/src/util/bitvec_format.rs @@ -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::::new(); + + let mut flush_bytes = | + read_index: usize, + accum_index: &mut usize, + accum_bytes: &mut Vec::| { - 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 } diff --git a/tests/driver/ok_format_intelhex/main.asm b/tests/driver/ok_format_intelhex/main.asm new file mode 100644 index 00000000..3b97a978 --- /dev/null +++ b/tests/driver/ok_format_intelhex/main.asm @@ -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 \ No newline at end of file diff --git a/tests/driver/ok_format_intelhex/out.txt b/tests/driver/ok_format_intelhex/out.txt new file mode 100644 index 00000000..a412168a --- /dev/null +++ b/tests/driver/ok_format_intelhex/out.txt @@ -0,0 +1,3 @@ +:20000000555568656C6C6F2C20776F726C642168656C6C6F2C20776F726C642168656C6C3F +:160020006F2C20776F726C642168656C6C6F2C20776F726C64211D +:00000001FF \ No newline at end of file diff --git a/tests/driver/ok_format_intelhex_blank/main.asm b/tests/driver/ok_format_intelhex_blank/main.asm new file mode 100644 index 00000000..783de06c --- /dev/null +++ b/tests/driver/ok_format_intelhex_blank/main.asm @@ -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 \ No newline at end of file diff --git a/tests/driver/ok_format_intelhex_blank/out.txt b/tests/driver/ok_format_intelhex_blank/out.txt new file mode 100644 index 00000000..41484b60 --- /dev/null +++ b/tests/driver/ok_format_intelhex_blank/out.txt @@ -0,0 +1,3 @@ +:030008005500554B +:0502000068656C6C6FE5 +:00000001FF \ No newline at end of file