diff options
| -rw-r--r-- | src/bin/tq.rs | 5 | ||||
| -rw-r--r-- | src/formats/mod.rs | 5 | ||||
| -rw-r--r-- | src/formats/raw.rs | 22 |
3 files changed, 25 insertions, 7 deletions
diff --git a/src/bin/tq.rs b/src/bin/tq.rs index 885c46c..7970017 100644 --- a/src/bin/tq.rs +++ b/src/bin/tq.rs @@ -97,6 +97,8 @@ Output formats: raw Assembled words are converted to big-endian bytestrings and concatenated. Each word is padded to the nearest byte. Words must all be the same width. + raw-le + Same as 'raw', but converts words to little-endian bytestrings instead. source Print the source file before assembly, with symbols resolved. "); @@ -183,7 +185,8 @@ Output formats: Format::Debug => format_debug(&segments), Format::Inhx => format_inhx(&segments), Format::Inhx32 => format_inhx32(&segments), - Format::Raw => format_raw(&segments, width), + Format::Raw => format_raw(&segments, width, Endian::Big), + Format::RawLE => format_raw(&segments, width, Endian::Little), Format::Source => unreachable!("Source output is handled before full assembly"), }; match result { diff --git a/src/formats/mod.rs b/src/formats/mod.rs index e15bfbd..8292a96 100644 --- a/src/formats/mod.rs +++ b/src/formats/mod.rs @@ -20,6 +20,7 @@ pub enum Format { Inhx, Inhx32, Raw, + RawLE, Source, } @@ -31,8 +32,9 @@ impl Format { "inhx" => Self::Inhx, "inhx32" => Self::Inhx32, "raw" => Self::Raw, + "raw-le" => Self::RawLE, "source" => Self::Source, - _ => fatal!("Unknown format '{string}', expected 'debug', 'inhx', 'inhx32', 'raw', or 'source'"), + _ => fatal!("Unknown format '{string}', expected 'debug', 'inhx', 'inhx32', 'raw', 'raw-le', or 'source'"), } } } @@ -45,6 +47,7 @@ impl std::fmt::Display for Format { Self::Inhx => "inhx", Self::Inhx32 => "inhx32", Self::Raw => "raw", + Self::RawLE => "raw-le", Self::Source => "source", }; write!(f, "{string}") diff --git a/src/formats/raw.rs b/src/formats/raw.rs index ecc6473..4cd0338 100644 --- a/src/formats/raw.rs +++ b/src/formats/raw.rs @@ -1,7 +1,11 @@ use crate::*; +pub enum Endian { + Big, + Little, +} -pub fn format_raw(segments: &[Segment], width: Option<u32>) -> Result<Vec<u8>, FormatError> { +pub fn format_raw(segments: &[Segment], width: Option<u32>, endian: Endian) -> Result<Vec<u8>, FormatError> { let Some(width) = width.or_else(|| calculate_fixed_width(&segments)) else { return Err(FormatError::ExpectedFixedWidth); }; @@ -17,10 +21,18 @@ pub fn format_raw(segments: &[Segment], width: Option<u32>) -> Result<Vec<u8>, F 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); - } + match endian { + Endian::Big => + for i in (0..bytes_per_word).rev() { + let byte = (value >> (i*8) & 0xff) as u8; + bytes.push(byte); + } + Endian::Little => + for i in 0..bytes_per_word { + let byte = (value >> (i*8) & 0xff) as u8; + bytes.push(byte); + } + }; address += 1; } } |
