From 5d47718a8d9160b4343e693bcdb6164e368b428d Mon Sep 17 00:00:00 2001 From: Jaby Date: Thu, 20 Apr 2023 21:01:27 +0200 Subject: [PATCH] Move the bit types to tool_helper and merge them --- src/Tools/psxcdgen_ex/src/types/bits.rs | 42 ------------------- src/Tools/psxcdgen_ex/src/types/mod.rs | 1 - .../psxcdgen_ex/src/types/overlay/mod.rs | 4 +- src/Tools/tool_helper/src/bits.rs | 42 ++++++++++++++++--- 4 files changed, 39 insertions(+), 50 deletions(-) delete mode 100644 src/Tools/psxcdgen_ex/src/types/bits.rs diff --git a/src/Tools/psxcdgen_ex/src/types/bits.rs b/src/Tools/psxcdgen_ex/src/types/bits.rs deleted file mode 100644 index c9b899df..00000000 --- a/src/Tools/psxcdgen_ex/src/types/bits.rs +++ /dev/null @@ -1,42 +0,0 @@ -pub struct BitRange { - start: usize, - length: usize -} - -impl BitRange { - pub const fn from_to(start_bit: usize, end_bit: usize) -> Self { - Self{start: start_bit, length: (end_bit - start_bit) + 1} - } - - pub const fn get_mask(&self) -> usize { - self.max_value() - } - - pub const fn max_value(&self) -> usize { - (1 << self.length) - 1 - } - - pub const fn or_value(&self, dst_value: usize, value: usize) -> usize { - dst_value | ((value & self.get_mask()) << self.start) - } -} - -pub struct Bit { - pos: usize -} - -impl Bit { - pub const fn at(pos: usize) -> Self { - Bit{pos} - } - - pub const fn or_value(&self, dst_value: usize, is_set: bool) -> usize { - if is_set { - dst_value | (1 << self.pos) - } - - else { - dst_value - } - } -} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index a25af325..cad43dd0 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -1,4 +1,3 @@ -pub mod bits; pub (super) mod helper; pub mod layout; pub mod file_map; diff --git a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs index 2cb8482b..58cf1b65 100644 --- a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs @@ -1,8 +1,8 @@ -use super::{bits::{Bit, BitRange}, layout::Layout, File, FileSystemMap}; +use super::{layout::Layout, File, FileSystemMap}; use super::super::encoder::LengthCalculatorFunction; use std::path::PathBuf; use no_comment::{IntoWithoutComments as _, languages}; -use tool_helper::{Error, format_if_error, read_file, read_file_to_string, format_if_error_drop_cause}; +use tool_helper::{bits::{Bit, BitRange}, Error, format_if_error, read_file, read_file_to_string, format_if_error_drop_cause}; pub type LBANameVec = Vec; diff --git a/src/Tools/tool_helper/src/bits.rs b/src/Tools/tool_helper/src/bits.rs index 3f03cf56..7dec95a7 100644 --- a/src/Tools/tool_helper/src/bits.rs +++ b/src/Tools/tool_helper/src/bits.rs @@ -1,11 +1,43 @@ pub struct BitRange { - pub start: usize, - pub len: usize, + start: usize, + length: usize } impl BitRange { - pub const fn from_to(start: usize, end: usize) -> BitRange { - BitRange{start, len: (end - start + 1)} + pub const fn from_to(start_bit: usize, end_bit: usize) -> Self { + Self{start: start_bit, length: (end_bit - start_bit) + 1} + } + + pub const fn get_mask(&self) -> usize { + self.max_value() + } + + pub const fn max_value(&self) -> usize { + (1 << self.length) - 1 + } + + pub const fn or_value(&self, dst_value: usize, value: usize) -> usize { + dst_value | ((value & self.get_mask()) << self.start) + } +} + +pub struct Bit { + pos: usize +} + +impl Bit { + pub const fn at(pos: usize) -> Self { + Bit{pos} + } + + pub const fn or_value(&self, dst_value: usize, is_set: bool) -> usize { + if is_set { + dst_value | (1 << self.pos) + } + + else { + dst_value + } } } @@ -13,7 +45,7 @@ macro_rules! create_bit_functions { ($type_val:ty) => { paste::item! { const fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val { - (1 << range.len) - 1 + range.get_mask() as $type_val } pub const fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val {