From b469d0595321c44723075632fa5414d2339cc629 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Mon, 9 Nov 2026 16:21:17 +1300 Subject: Add support for disjoint fields in word templates The AVR instruction set requires that fields in some instructions are split across multiple ranges, requiring words that look like: #0001_11rd_dddd_rrrr This commit makes it possible to define fields with this syntax. The syntactic error DuplicateFieldNameInWord has been removed. --- src/types/word_template.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'src/types') diff --git a/src/types/word_template.rs b/src/types/word_template.rs index 634e1d5..e319f05 100644 --- a/src/types/word_template.rs +++ b/src/types/word_template.rs @@ -6,18 +6,36 @@ pub struct WordTemplate { pub value: usize, /// Width of the word in bits. pub width: u32, - pub fields: Vec>, + pub fields: Vec, } #[derive(Clone)] pub struct BitField { pub name: char, - /// Width of the field in bits. + /// Guaranteed to contain at least one range. Field is disjoint if it + /// contains more than one range. Fields are ordered from most to least + /// significant. + pub ranges: Vec>, +} + +#[derive(Clone)] +pub struct BitRange { + /// Width of the range in bits. pub width: u32, - /// Number of bits to the right of the field in the word. + /// Number of bits to the right of the range in the word. pub shift: u32, } +impl BitField { + pub fn width(&self) -> u32 { + let mut width = 0; + for range in &self.ranges { + width += range.width; + } + return width; + } +} + impl std::fmt::Display for WordTemplate { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { @@ -31,10 +49,12 @@ impl std::fmt::Display for WordTemplate { write!(f, "_")?; } for field in &self.fields { - let i = i as u32; - if i <= field.width + field.shift - 1 && i >= field.shift { - write!(f, "{}", field.name)?; - continue 'bit; + for range in &field.ranges { + let i = i as u32; + if i <= range.width + range.shift - 1 && i >= range.shift { + write!(f, "{}", field.name)?; + continue 'bit; + } } } match (self.value >> i) & 1 { -- cgit v1.3.1