summaryrefslogtreecommitdiff
path: root/src/formats/raw.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/formats/raw.rs')
-rw-r--r--src/formats/raw.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/formats/raw.rs b/src/formats/raw.rs
new file mode 100644
index 0000000..ecc6473
--- /dev/null
+++ b/src/formats/raw.rs
@@ -0,0 +1,29 @@
+use crate::*;
+
+
+pub fn format_raw(segments: &[Segment], width: Option<u32>) -> Result<Vec<u8>, FormatError> {
+ let Some(width) = width.or_else(|| calculate_fixed_width(&segments)) else {
+ return Err(FormatError::ExpectedFixedWidth);
+ };
+
+ let mut address = 0;
+ let bytes_per_word = ((width + 7) / 8) as usize;
+ let mut bytes = Vec::new();
+
+ for segment in segments {
+ // Pad to the segment start address.
+ let padding = segment.address.saturating_sub(address);
+ bytes.resize(bytes.len() + (padding * bytes_per_word), 0);
+ for word in &segment.words {
+ // Decompose word value into bytes.
+ let value = word.value.value;
+ for i in (0..bytes_per_word).rev() {
+ let byte = (value >> (i*8) & 0xff) as u8;
+ bytes.push(byte);
+ }
+ address += 1;
+ }
+ }
+
+ return Ok(bytes);
+}