2020-08-13 16:51:08 +08:00
|
|
|
/*
|
|
|
|
* Macro builder for bit masks
|
|
|
|
* $collection: Name for the bit mask collection
|
|
|
|
* $unsigned_type: Unsigned type for the data that the bit mask will be applied onto
|
|
|
|
* ($name: name of a bit mask, $shift: shift of the bit field, $width: no. of bits in the bit field)
|
|
|
|
* Note: All bit masks must concern a set of contiguous bits
|
|
|
|
*/
|
|
|
|
|
|
|
|
macro_rules! construct_bitmask {
|
|
|
|
($collection: ident; $unsigned_type: ty; $($name: ident, $shift: expr, $width: expr),+) => {
|
2020-08-26 13:18:50 +08:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2020-09-14 17:33:50 +08:00
|
|
|
#[allow(non_camel_case_types)]
|
2020-08-13 16:51:08 +08:00
|
|
|
pub enum $collection {
|
|
|
|
$(
|
|
|
|
$name,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $collection {
|
|
|
|
pub(crate) fn get_width(self) -> u8 {
|
|
|
|
match self {
|
|
|
|
$(
|
|
|
|
$collection::$name => $width,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub(crate) fn get_shift(self) -> u8 {
|
|
|
|
match self {
|
|
|
|
$(
|
|
|
|
$collection::$name => $shift,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub(crate) fn get_bitmask(self) -> $unsigned_type {
|
|
|
|
let mut mask: $unsigned_type = 0;
|
|
|
|
for bit in 0..self.get_width() {
|
2020-08-26 11:04:08 +08:00
|
|
|
mask |= (1 << (self.get_shift() + bit) % ((size_of::<$unsigned_type>() as u8) * 8));
|
2020-08-13 16:51:08 +08:00
|
|
|
}
|
|
|
|
mask
|
|
|
|
}
|
|
|
|
pub(crate) fn get_shifted_bits(self, arg: $unsigned_type) -> $unsigned_type {
|
|
|
|
assert!(arg < (2 << self.get_width()));
|
2020-08-26 13:18:50 +08:00
|
|
|
(arg << (self.get_shift() % ((size_of::<$unsigned_type>() as u8) * 8)))
|
2020-08-13 16:51:08 +08:00
|
|
|
}
|
|
|
|
pub(crate) fn set_data_by_arg(self, data: &mut $unsigned_type, arg: $unsigned_type) {
|
|
|
|
// Clear bits in field, then insert shifted argument
|
|
|
|
*data &= (!self.get_bitmask());
|
|
|
|
*data |= self.get_shifted_bits(arg);
|
|
|
|
}
|
|
|
|
pub(crate) fn get_filtered_content(self, data: $unsigned_type) -> $unsigned_type {
|
|
|
|
// Filter everything then shift bits
|
2020-08-26 11:04:08 +08:00
|
|
|
((data & self.get_bitmask()) >> (self.get_shift() % ((size_of::<$unsigned_type>() as u8) * 8)))
|
2020-08-13 16:51:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|