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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use std::collections::BTreeMap;
use std::io::{BufRead, BufReader, BufWriter, Write};
use super::error::{IntelHexError, MemoryError, ParseError};
/// Represents the execution start address in Intel HEX format
#[derive(Debug)]
pub enum ExecutionStartAddress {
/// Segment address (CS:IP) for 20-bit addressing
StartSegmentAddress { cs: u16, ip: u16 },
/// Linear address (EIP) for 32-bit addressing
StartLinearAddress { eip: u32 },
}
/// Represents a continuous block of memory data
#[derive(Debug)]
pub struct MemoryDataBlock {
/// Starting address of the memory block
pub address: u32,
/// Continuous data bytes stored in this block
pub data: Vec<u8>,
}
/// Main structure for handling Intel HEX format data
pub struct IntelHex {
/// Optional execution start address (if specified in the HEX file)
execution_start_address: Option<ExecutionStartAddress>,
/// Map of memory addresses to their corresponding data bytes
memory_data_cells: BTreeMap<u32, u8>,
}
enum AddressMode {
SegmentAddress(u32),
LinearAddress(u32),
}
impl IntelHex {
/// Creates a new empty IntelHex instance
///
/// Initializes a new IntelHex object with:
/// * An empty memory cells map (no data bytes stored)
/// * No execution start address
///
/// # Returns
///
/// Returns a new empty IntelHex instance
///
/// # Examples
///
/// ```
/// use intelhex::IntelHex;
///
/// let intel_hex = IntelHex::new();
///
/// // Memory cells map is empty
/// assert!(intel_hex.memory_cells().is_empty());
///
/// // No execution start address
/// assert!(intel_hex.execution_start_address().is_none());
/// ```
pub fn new() -> IntelHex {
Self {
execution_start_address: None,
memory_data_cells: BTreeMap::new(),
}
}
/// Returns a reference to the memory cells map
///
/// The memory cells are stored in a BTreeMap where:
/// * Keys (u32) are memory addresses
/// * Values (u8) are the data bytes stored at those addresses
/// * Addresses are automatically sorted in ascending order
///
/// # Returns
///
/// Returns a reference to the BTreeMap containing all memory cells data
///
/// # Examples
///
/// ```
/// use intelhex::IntelHex;
///
/// let hex_text = ":10010000214601360121470136007EFE09D2190140\n:00000001FF";
/// let intel_hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
///
/// // Access memory cells
/// let memory = intel_hex.memory_cells();
///
/// // Check if an address contains data
/// if let Some(&value) = memory.get(&0x100) {
/// println!("Value at 0x100: {:02X}", value);
/// }
///
/// // Iterate through all memory cells in address order
/// for (&addr, &value) in memory.iter() {
/// println!("Address: {:08X}, Value: {:02X}", addr, value);
/// }
/// ```
pub fn memory_cells(&self) -> &BTreeMap<u32, u8> {
&self.memory_data_cells
}
/// Returns a reference to the execution start address if it exists
///
/// The execution start address can be either a segment address (CS:IP) or
/// a linear address (EIP), as defined in the Intel HEX format.
///
/// # Returns
///
/// * `Some(&ExecutionStartAddress)` - A reference to the execution start address if it was specified
/// * `None` - If no execution start address was specified
///
/// # Examples
///
/// ```
/// use intelhex::IntelHex;
/// use intelhex::ExecutionStartAddress;
///
/// let hex_text = ":0400000300100020C9\n:00000001FF"; // Contains a start segment address
/// let intel_hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
///
/// if let Some(start_addr) = intel_hex.execution_start_address() {
/// match start_addr {
/// ExecutionStartAddress::StartSegmentAddress { cs, ip } => {
/// println!("CS:{:04X}, IP:{:04X}", cs, ip)
/// },
/// ExecutionStartAddress::StartLinearAddress { eip } => {
/// println!("EIP:{:08X}", eip)
/// },
/// }
/// }
/// ```
pub fn execution_start_address(&self) -> Option<&ExecutionStartAddress> {
self.execution_start_address.as_ref()
}
/// Creates an IntelHex instance from a reader
///
/// # Arguments
///
/// * `reader` - Any type that implements the `Read` trait
///
/// # Returns
///
/// * `Ok(IntelHex)` - A new IntelHex instance containing the parsed data
/// * `Err(IntelHexError)` - If there was an error reading or parsing the data
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use intelhex::IntelHex;
///
/// let file = File::open("tests/fixtures/firmware.hex").unwrap();
/// let intel_hex = IntelHex::from_reader(file).unwrap();
/// ```
pub fn from_reader<R>(reader: R) -> Result<IntelHex, IntelHexError>
where
R: std::io::Read,
{
let mut intelhex = IntelHex::new();
let reader = BufReader::new(reader);
let mut base_address = AddressMode::LinearAddress(0);
for line in reader.lines() {
let line = line?;
if line.is_empty() {
continue;
}
match intelhex.decode_record(&line, &mut base_address) {
Ok(true) => break,
Err(error) => return Err(error),
_ => (),
}
}
Ok(intelhex)
}
/// Creates an IntelHex instance from Intel HEX format text
///
/// # Arguments
///
/// * `hex_text` - A string slice containing Intel HEX formatted data
///
/// # Returns
///
/// * `Ok(IntelHex)` - A new IntelHex instance containing the parsed data
/// * `Err(IntelHexError)` - If the input string is not a valid Intel HEX format
///
/// # Examples
///
/// ```
/// use intelhex::IntelHex;
///
/// let hex_text = ":10010000214601360121470136007EFE09D2190140\n:00000001FF";
/// let result = IntelHex::from_intel_hex_text(hex_text).unwrap();
/// ```
pub fn from_intel_hex_text(hex_text: &str) -> Result<IntelHex, IntelHexError> {
Self::from_reader(hex_text.as_bytes())
}
/// Decodes a single Intel HEX record line
///
/// # Arguments
///
/// * `line` - A string slice containing a single Intel HEX record
/// * `base_address` - A mutable reference to the current base address
///
/// # Returns
///
/// * `Ok(bool)` - Returns `true` if this is an End of File record, `false` otherwise
/// * `Err(IntelHexError)` - If there was an error decoding the record
///
/// # Record Types Supported
///
/// * 00 - Data Record
/// * 01 - End of File Record
/// * 02 - Extended Segment Address Record
/// * 03 - Start Segment Address Record
/// * 04 - Extended Linear Address Record
/// * 05 - Start Linear Address Record
fn decode_record(
&mut self,
line: &str,
base_address: &mut AddressMode,
) -> Result<bool, IntelHexError> {
let mut line_chars_iter = line.chars();
// Check for start code
if let Some(':') = line_chars_iter.next() {
} else {
return Err(ParseError::MissingStartCode.into());
}
// Validate hex characters and length
let mut hexdigit_length = 0;
while let Some(c) = line_chars_iter.next() {
hexdigit_length += 1;
if !c.is_ascii_hexdigit() {
return Err(ParseError::InvalidCharacters.into());
}
}
// Validate record length
if hexdigit_length < ((1 + 2 + 1 + 1) * 2) {
return Err(ParseError::RecordTooShort.into());
} else if hexdigit_length > ((1 + 2 + 1 + 255 + 1) * 2) {
return Err(ParseError::RecordTooLong.into());
} else if hexdigit_length % 2 != 0 {
return Err(ParseError::RecordNotEvenLength.into());
}
// Convert hex characters to bytes
let mut record_bytes = Vec::new();
for i in (1..hexdigit_length).step_by(2) {
record_bytes.push(u8::from_str_radix(&line[i..i + 2], 16).unwrap());
}
// Verify checksum
let expected_chksum = record_bytes.pop().unwrap();
let chksum = (0 as u8).wrapping_sub(
record_bytes
.iter()
.fold(0, |acc: u8, x| acc.wrapping_add(*x)),
);
if chksum != expected_chksum {
return Err(ParseError::ChecksumMismatch(chksum, expected_chksum).into());
}
// Parse record fields
let record_length = record_bytes[0] as usize;
let record_offset = ((record_bytes[1] as u16) << 8) | (record_bytes[2] as u16);
let record_type = record_bytes[3];
let record_payload = &record_bytes[4..];
// Validate payload length
if record_payload.len() != record_length {
return Err(ParseError::PayloadLengthMismatch.into());
}
// Process record based on type
match record_type {
0 => {
// Data Record
for i in 0..record_length {
let address = match base_address {
// Segment addressing mode:
// Physical Address = SBA + ([DRLO + DRI] MOD 65536)
// where:
// - SBA (Segment Base Address) = base
// - DRLO (Data Record Load Offset) = record_offset
// - DRI (Data Record Index) = i
// - MOD 65536 is achieved through u16 wrapping addition
AddressMode::SegmentAddress(base) => base
.wrapping_add(((record_offset as u16).wrapping_add(i as u16)) as u32),
// Linear addressing mode:
// Physical Address = Base Address + DRLO + DRI
// where:
// - Base Address = base (upper 16 bits)
// - DRLO (Data Record Load Offset) = record_offset
// - DRI (Data Record Index) = i
AddressMode::LinearAddress(base) => base
.wrapping_add(record_offset as u32)
.wrapping_add(i as u32),
};
if let None = self.memory_data_cells.get(&address) {
self.memory_data_cells.insert(address, record_payload[i]);
} else {
return Err(MemoryError::AddressOverlap.into());
}
}
}
1 => {
// End of File Record
if record_length != 0 {
return Err(ParseError::InvalidLengthForType.into());
} else if record_offset != 0 {
return Err(ParseError::InvalidLoadOffset.into());
}
return Ok(true);
}
2 => {
// Extended Segment Address Record
if record_length != 2 {
return Err(ParseError::InvalidLengthForType.into());
} else if record_offset != 0 {
return Err(ParseError::InvalidLoadOffset.into());
} else {
*base_address = AddressMode::SegmentAddress(
(((record_payload[0] as u32) << 8) | (record_payload[1] as u32)) << 4,
);
}
}
3 => {
// Start Segment Address Record
if record_length != 4 {
return Err(ParseError::InvalidLengthForType.into());
} else if record_offset != 0 {
return Err(ParseError::InvalidLoadOffset.into());
} else {
let cs: u16 = ((record_payload[0] as u16) << 8) | record_payload[1] as u16;
let ip: u16 = ((record_payload[2] as u16) << 8) | record_payload[3] as u16;
if let None = self.execution_start_address {
self.execution_start_address =
Some(ExecutionStartAddress::StartSegmentAddress { cs, ip });
} else {
return Err(ParseError::MultipleStartAddressRecord.into());
}
}
}
4 => {
// Extended Linear Address Record
if record_length != 2 {
return Err(ParseError::InvalidLengthForType.into());
} else if record_offset != 0 {
return Err(ParseError::InvalidLoadOffset.into());
} else {
*base_address = AddressMode::LinearAddress(
(((record_payload[0] as u32) << 8) | (record_payload[1] as u32)) << 16,
);
}
}
5 => {
// Start Linear Address Record
if record_length != 4 {
return Err(ParseError::InvalidLengthForType.into());
} else if record_offset != 0 {
return Err(ParseError::InvalidLoadOffset.into());
} else {
let eip: u32 = ((record_payload[0] as u32) << 24)
| ((record_payload[1] as u32) << 16)
| ((record_payload[2] as u32) << 8)
| record_payload[3] as u32;
if let None = self.execution_start_address {
self.execution_start_address =
Some(ExecutionStartAddress::StartLinearAddress { eip });
} else {
return Err(ParseError::MultipleStartAddressRecord.into());
}
}
}
_ => return Err(ParseError::UnsupportedRecordType(record_type).into()),
}
Ok(false)
}
/// Writes Intel HEX format data to a writer
///
/// # Arguments
///
/// * `writer` - Any type that implements the `Write` trait
/// * `data_byte_count` - Number of data bytes per Data Record (1-255).
/// A value of 16 is recommended for general use.
///
/// # Returns
///
/// * `Ok(())` - If the data was successfully written
/// * `Err(std::io::Error)` - If there was an error writing the data
///
/// # Examples
///
/// ```no_run
/// use intelhex::IntelHex;
/// use std::fs::File;
///
/// let intel_hex = IntelHex::new();
/// let mut file = File::create("output.hex").unwrap();
/// intel_hex.to_writer(&mut file, 16).unwrap(); // 16 data bytes per data record
/// ```
pub fn to_writer<W>(&self, writer: W, data_byte_count: u8) -> Result<(), std::io::Error>
where
W: std::io::Write,
{
// Since data_byte_count is u8, it's automatically limited to 0-255
// We only need to check the lower bound (0)
if data_byte_count == 0 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"data_byte_count must be between 1 and 255",
));
}
let mut writer = BufWriter::new(writer);
let mut is_first = true;
let mut base_address: u32 = 0;
let mut continuous_bytes: Vec<u8> = Vec::with_capacity(data_byte_count as usize);
let mut load_offset = 0;
let mut last_addr = 0;
// Write data records
for (addr, val) in &self.memory_data_cells {
let mut need_addr_record = false;
let mut is_addr_continuous = false;
if is_first {
base_address = *addr & 0xFFFF0000;
need_addr_record = true;
last_addr = *addr;
is_first = false;
}
if *addr - base_address > 0xFFFF {
need_addr_record = true;
}
if *addr == (last_addr + 1) {
is_addr_continuous = true;
}
if (!is_addr_continuous)
|| (continuous_bytes.len() >= data_byte_count as usize)
|| need_addr_record
{
if !continuous_bytes.is_empty() {
// Data Record
let bytes_length = continuous_bytes.len();
let mut chksum: u8 = (bytes_length as u8)
.wrapping_add((load_offset >> 8) as u8)
.wrapping_add(load_offset as u8);
write!(writer, ":{:02X}{:04X}00", bytes_length, load_offset)?;
for v in &continuous_bytes {
chksum = chksum.wrapping_add(*v);
write!(writer, "{:02X}", *v)?;
}
chksum = (0 as u8).wrapping_sub(chksum);
#[cfg(windows)]
write!(writer, "{:02X}\r\n", chksum)?;
#[cfg(not(windows))]
write!(writer, "{:02X}\n", chksum)?;
continuous_bytes.clear();
load_offset = *addr - base_address;
}
}
if need_addr_record {
base_address = *addr & 0xFFFF0000;
load_offset = *addr - base_address;
// Extended Linear Address Record
let mut chksum: u8 = 2 + 4;
chksum = chksum.wrapping_add((base_address >> 24) as u8);
chksum = chksum.wrapping_add((base_address >> 16) as u8);
chksum = (0 as u8).wrapping_sub(chksum);
#[cfg(windows)]
write!(
writer,
":02000004{:04X}{:02X}\r\n",
base_address >> 16,
chksum
)?;
#[cfg(not(windows))]
write!(
writer,
":02000004{:04X}{:02X}\n",
base_address >> 16,
chksum
)?;
}
continuous_bytes.push(*val);
last_addr = *addr;
}
if !continuous_bytes.is_empty() {
// Data Record
let bytes_length = continuous_bytes.len();
let mut chksum: u8 = (bytes_length as u8)
.wrapping_add((load_offset >> 8) as u8)
.wrapping_add(load_offset as u8);
write!(writer, ":{:02X}{:04X}00", bytes_length, load_offset)?;
for v in &continuous_bytes {
chksum = chksum.wrapping_add(*v);
write!(writer, "{:02X}", *v)?;
}
chksum = (0 as u8).wrapping_sub(chksum);
#[cfg(windows)]
write!(writer, "{:02X}\r\n", chksum)?;
#[cfg(not(windows))]
write!(writer, "{:02X}\n", chksum)?;
}
// Write execution start address if present
if let Some(execution_start_address) = &self.execution_start_address {
match *execution_start_address {
ExecutionStartAddress::StartSegmentAddress { cs, ip } => {
// Start Segment Address Record
let mut chksum: u8 = 4 + 3;
chksum = chksum.wrapping_add((cs >> 8) as u8);
chksum = chksum.wrapping_add(cs as u8);
chksum = chksum.wrapping_add((ip >> 8) as u8);
chksum = chksum.wrapping_add(ip as u8);
chksum = (0 as u8).wrapping_sub(chksum);
#[cfg(windows)]
write!(writer, ":04000003{:04X}{:04X}{:02X}\r\n", cs, ip, chksum)?;
#[cfg(not(windows))]
write!(writer, ":04000003{:04X}{:04X}{:02X}\n", cs, ip, chksum)?;
}
ExecutionStartAddress::StartLinearAddress { eip } => {
// Start Linear Address Record
let mut chksum: u8 = 4 + 5;
chksum = chksum.wrapping_add((eip >> 24) as u8);
chksum = chksum.wrapping_add((eip >> 16) as u8);
chksum = chksum.wrapping_add((eip >> 8) as u8);
chksum = chksum.wrapping_add(eip as u8);
chksum = (0 as u8).wrapping_sub(chksum);
#[cfg(windows)]
write!(writer, ":04000005{:08X}{:02X}\r\n", eip, chksum)?;
#[cfg(not(windows))]
write!(writer, ":04000005{:08X}{:02X}\n", eip, chksum)?;
}
}
}
// End of File Record
#[cfg(windows)]
write!(writer, ":00000001FF\r\n")?;
#[cfg(not(windows))]
write!(writer, ":00000001FF\n")?;
writer.flush()?;
Ok(())
}
/// Converts the IntelHex object to Intel HEX format text
///
/// # Returns
///
/// * `Ok(String)` - A string containing the Intel HEX formatted data
/// * `Err(std::io::Error)` - If there was an error writing the data
pub fn to_intel_hex_text(&self) -> Result<String, std::io::Error> {
let mut buf: Vec<u8> = Vec::new();
self.to_writer(&mut buf, 16)?;
Ok(String::from_utf8(buf).unwrap())
}
/// Converts memory data into a collection of continuous data blocks
///
/// # Returns
///
/// Returns a vector of MemoryDataBlock, each containing:
/// * address: Starting address of the data block
/// * data: Vector of continuous data bytes
pub fn memory_blocks(&self) -> Vec<MemoryDataBlock> {
let mut blocks = Vec::new();
if self.memory_data_cells.is_empty() {
return blocks;
}
let mut current_block = MemoryDataBlock {
address: 0,
data: Vec::new(),
};
let mut last_address: Option<u32> = None;
for (&address, &value) in &self.memory_data_cells {
match last_address {
None => {
// First data point
current_block.address = address;
current_block.data.push(value);
}
Some(last_addr) => {
if address == last_addr + 1 {
// Continuous address
current_block.data.push(value);
} else {
// Discontinuous address, create new block
blocks.push(std::mem::replace(
&mut current_block,
MemoryDataBlock {
address,
data: vec![value],
},
));
}
}
}
last_address = Some(address);
}
// Add the last block
if !current_block.data.is_empty() {
blocks.push(current_block);
}
blocks
}
/// Adds data from a MemoryDataBlock to the memory cells
///
/// # Arguments
///
/// * `block` - The MemoryDataBlock containing address and data to add
///
/// # Returns
///
/// * `Ok(())` - If the data was successfully added
/// * `Err(IntelHexError)` - If there was an error adding the data
///
/// # Examples
///
/// ```
/// use intelhex::{IntelHex, MemoryDataBlock};
///
/// let mut intel_hex = IntelHex::new();
/// let block = MemoryDataBlock {
/// address: 0x1000,
/// data: vec![0x12, 0x34, 0x56],
/// };
///
/// // Add the memory block
/// intel_hex.add_memory_block(&block).unwrap();
///
/// // Verify the data was added
/// let memory = intel_hex.memory_cells();
/// assert_eq!(memory.get(&0x1000), Some(&0x12));
/// assert_eq!(memory.get(&0x1001), Some(&0x34));
/// assert_eq!(memory.get(&0x1002), Some(&0x56));
/// ```
pub fn add_memory_block(&mut self, block: &MemoryDataBlock) -> Result<(), IntelHexError> {
// Check for address overlap before adding any data
for i in 0..block.data.len() {
let address = block.address.wrapping_add(i as u32);
if self.memory_data_cells.contains_key(&address) {
return Err(MemoryError::AddressOverlap.into());
}
}
// Add all data bytes to memory cells
for (i, &value) in block.data.iter().enumerate() {
let address = block.address.wrapping_add(i as u32);
self.memory_data_cells.insert(address, value);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
// Test helper function to create a simple IntelHex instance with test data
fn create_test_hex() -> IntelHex {
let hex_text = "\
:10010000214601360121470136007EFE09D2190140\n\
:100110002146017EB7C20001FF5F16002148011988\n\
:00000001FF";
IntelHex::from_intel_hex_text(hex_text).unwrap()
}
#[test]
fn test_new() {
// Test creating a new empty IntelHex instance
let hex = IntelHex::new();
assert!(hex.memory_cells().is_empty());
assert!(hex.execution_start_address().is_none());
}
#[test]
fn test_basic_parsing() {
let hex = create_test_hex();
let memory = hex.memory_cells();
// Verify first data record was parsed correctly
assert_eq!(memory.get(&0x0100), Some(&0x21));
assert_eq!(memory.get(&0x0101), Some(&0x46));
// Verify second data record was parsed correctly
assert_eq!(memory.get(&0x0110), Some(&0x21));
assert_eq!(memory.get(&0x0111), Some(&0x46));
}
#[test]
fn test_start_segment_address() {
// Test parsing of Start Segment Address Record
let hex_text = ":0400000300100020C9\n:00000001FF";
let hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
match hex.execution_start_address().unwrap() {
ExecutionStartAddress::StartSegmentAddress { cs, ip } => {
assert_eq!(*cs, 0x0010);
assert_eq!(*ip, 0x0020);
}
_ => panic!("Wrong execution start address type"),
}
}
#[test]
fn test_start_linear_address() {
// Test parsing of Start Linear Address Record
let hex_text = ":0400000512345678E3\n:00000001FF";
let hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
match hex.execution_start_address().unwrap() {
ExecutionStartAddress::StartLinearAddress { eip } => {
assert_eq!(*eip, 0x12345678);
}
_ => panic!("Wrong execution start address type"),
}
}
#[test]
fn test_extended_segment_address() {
// Test parsing with Extended Segment Address Record
let hex_text = "\
:020000021000EC\n\
:10010000214601360121470136007EFE09D2190140\n\
:00000001FF";
let hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
let memory = hex.memory_cells();
// Base address should be 0x10000 + offset
assert_eq!(memory.get(&0x10100), Some(&0x21));
assert_eq!(memory.get(&0x10101), Some(&0x46));
}
#[test]
fn test_extended_linear_address() {
// Test parsing with Extended Linear Address Record
let hex_text = "\
:02000004FFFFFC\n\
:10010000214601360121470136007EFE09D2190140\n\
:00000001FF";
let hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
let memory = hex.memory_cells();
// Base address should be 0xFFFF0000 + offset
assert_eq!(memory.get(&0xFFFF0100), Some(&0x21));
assert_eq!(memory.get(&0xFFFF0101), Some(&0x46));
}
#[test]
fn test_memory_blocks() {
let mut hex = IntelHex::new();
// Add discontinuous memory blocks
let block1 = MemoryDataBlock {
address: 0x1000,
data: vec![0x11, 0x22, 0x33],
};
let block2 = MemoryDataBlock {
address: 0x2000,
data: vec![0x44, 0x55],
};
hex.add_memory_block(&block1).unwrap();
hex.add_memory_block(&block2).unwrap();
let blocks = hex.memory_blocks();
assert_eq!(blocks.len(), 2);
// Verify first block
assert_eq!(blocks[0].address, 0x1000);
assert_eq!(blocks[0].data, vec![0x11, 0x22, 0x33]);
// Verify second block
assert_eq!(blocks[1].address, 0x2000);
assert_eq!(blocks[1].data, vec![0x44, 0x55]);
}
#[test]
fn test_error_handling() {
// Test invalid start code
let result = IntelHex::from_intel_hex_text("10010000214601360121470136007EFE09D2190140");
assert!(matches!(
result,
Err(IntelHexError::Parse(ParseError::MissingStartCode))
));
// Test invalid checksum
let result = IntelHex::from_intel_hex_text(":10010000214601360121470136007EFE09D2190141");
assert!(matches!(
result,
Err(IntelHexError::Parse(ParseError::ChecksumMismatch(_, _)))
));
// Test address overlap
let mut hex = IntelHex::new();
let block1 = MemoryDataBlock {
address: 0x1000,
data: vec![0x11, 0x22],
};
let block2 = MemoryDataBlock {
address: 0x1001,
data: vec![0x33, 0x44],
};
hex.add_memory_block(&block1).unwrap();
assert!(matches!(
hex.add_memory_block(&block2),
Err(IntelHexError::Memory(MemoryError::AddressOverlap))
));
}
}