Skip to content

Commit

Permalink
fix #210: output intelhex by blocks of valid code
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Feb 1, 2025
1 parent 9f039c7 commit 7088421
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 35 deletions.
63 changes: 63 additions & 0 deletions src/util/bitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ pub struct BitVecSpan
}


#[derive(Clone, Debug)]
pub struct BitVecBlock
{
pub offset: usize,
pub size: usize,
}


impl BitVec
{
pub fn new() -> BitVec
Expand Down Expand Up @@ -128,6 +136,61 @@ impl BitVec
bigint.size = Some(self.len);
bigint
}


pub fn get_blocks(&self) -> Vec<BitVecBlock>
{
let mut result = Vec::new();

let mut sorted_spans = self.spans.clone();
sorted_spans.sort_by(|a, b| a.offset.cmp(&b.offset));

let mut current_origin: Option<usize> = None;
let mut current_size = 0;

for span in &sorted_spans
{
let Some(span_offset) = span.offset
else { continue };

if let Some(origin) = current_origin
{
if span_offset != origin + current_size
{
if current_size != 0
{
result.push(BitVecBlock {
offset: origin,
size: current_size,
});
}

current_origin = None;
}
}

if let None = current_origin
{
current_origin = Some(span_offset);
current_size = 0;
}

current_size += span.size;
}

if let Some(origin) = current_origin
{
if current_size != 0
{
result.push(BitVecBlock {
offset: origin,
size: current_size,
});
}
}

result
}
}


Expand Down
49 changes: 20 additions & 29 deletions src/util/bitvec_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,11 @@ impl util::BitVec
{
let mut result = String::new();

let mut read_index = 0;

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>|
{
while let Some(0_u8) = accum_bytes.last()
{
accum_bytes.pop();
}

while let Some(0_u8) = accum_bytes.first()
{
accum_bytes.remove(0);
*accum_index += 8;
}

let length = accum_bytes.len() as u8;

if length > 0
Expand Down Expand Up @@ -264,25 +248,32 @@ impl util::BitVec
*accum_index = read_index;
};

while read_index < self.len()
for block in self.get_blocks()
{
let mut byte: u8 = 0;
for _ in 0..8
let mut read_index = block.offset;
let mut accum_index = block.offset;
let mut accum_bytes = Vec::<u8>::new();

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

accum_bytes.push(byte);
accum_bytes.push(byte);

if accum_bytes.len() >= 32
{
flush_bytes(read_index, &mut accum_index, &mut accum_bytes);
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);
flush_bytes(read_index, &mut accum_index, &mut accum_bytes);
}

result.push_str(":00000001FF");
result
Expand Down
3 changes: 0 additions & 3 deletions tests/driver/ok_format_intelhex_blank/out.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
}

#d64 0

halt

#d8 0

halt

#addr 0x200
Expand Down
3 changes: 3 additions & 0 deletions tests/driver/ok_format_intelhex_blocks1/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:0B00000000000000000000005500554B
:0502000068656C6C6FE5
:00000001FF
27 changes: 27 additions & 0 deletions tests/driver/ok_format_intelhex_blocks2/main.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ruledef test
{
halt => 0x55
}

#addr 0x100

#d64 0
halt
#d8 0
halt

#addr 0x200
label1:
#d "hello"
#d256 0

#addr 0x400
#d "world"
label2:
#d64 0

#addr 0x600
label3:

; command: main.asm -f intelhex -o out.txt
; output: out.txt
5 changes: 5 additions & 0 deletions tests/driver/ok_format_intelhex_blocks2/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:0B01000000000000000000005500554A
:2002000068656C6C6F000000000000000000000000000000000000000000000000000000CA
:050220000000000000D9
:0D040000776F726C640000000000000000C7
:00000001FF
12 changes: 12 additions & 0 deletions tests/issue210/main.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ruledef
{
nop => 0x00
ldi sp, {value: i8} => 0x08 @ value
}

nop
ldi sp, 0xff
nop

; command: main.asm -f intelhex -o out.txt
; output: out.txt
2 changes: 2 additions & 0 deletions tests/issue210/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:040000000008FF00F5
:00000001FF

0 comments on commit 7088421

Please sign in to comment.