From 2f97973b3ac7523d48e33c582e5f416aaf478fcf Mon Sep 17 00:00:00 2001 From: Vaughn Kottler Date: Mon, 18 Sep 2023 03:25:40 -0500 Subject: [PATCH] Add "where" string for fields --- runtimepy/primitives/field/__init__.py | 16 ++++++++++++++++ tests/primitives/field/test_fields.py | 3 +++ 2 files changed, 19 insertions(+) diff --git a/runtimepy/primitives/field/__init__.py b/runtimepy/primitives/field/__init__.py index 05062433..ce47d53a 100644 --- a/runtimepy/primitives/field/__init__.py +++ b/runtimepy/primitives/field/__init__.py @@ -37,6 +37,22 @@ def __init__( self.mask = (2**self.width) - 1 self.shifted_mask = self.mask << self.index + def where_str(self) -> str: + """ + Get a simple string representing the bit locations this field occupies. + """ + + result = "" + + for idx in range(self.raw.size * 8): + val = 1 << idx + if val & self.shifted_mask: + result += "^" + else: + result += "-" + + return result + def __call__(self, val: int = None) -> int: """ Set (or get) the underlying value of this field. Return the actual diff --git a/tests/primitives/field/test_fields.py b/tests/primitives/field/test_fields.py index 1598e4bc..8c01ef24 100644 --- a/tests/primitives/field/test_fields.py +++ b/tests/primitives/field/test_fields.py @@ -18,7 +18,10 @@ def test_bit_fields_basic(): byte = BitFields.new() flag1 = byte.flag("flag1") + assert flag1.where_str() == "^-------" + flag2 = byte.flag("flag2") + assert flag2.where_str() == "-^------" flag1.set() assert byte.raw.value == 1