Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pyocd/utility/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def bfx(value: int, msb: int, lsb: int) -> int:
return (value & mask) >> lsb

def bfxw(value: int, lsb: int, width: int) -> int:
"""@brief Extract a value from a bitfield given the LSb and width."""
mask = bitmask((lsb + width, lsb))
"""@brief Extract a value from a bitfield given the LSb and width.

A width of 0 selects no bits and returns 0.
"""
mask = bitmask((lsb + width - 1, lsb))
return (value & mask) >> lsb

def bfi(value: int, msb: int, lsb: int, field: int) -> int:
Expand Down
42 changes: 42 additions & 0 deletions test/unit/test_mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# pyOCD debugger
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from pyocd.utility.mask import (
bfx,
bfxw,
)

class TestBfxw:
@pytest.mark.parametrize(("value", "lsb", "width"), [
(0xffffffff, 0, 1),
(0xffffffff, 31, 1),
(0x1f0, 4, 4),
(0xffffcfff, 12, 2),
(0x12345678, 8, 8),
(0x12345678, 0, 32),
(0xffffffff, 3, 5),
])
def test_matches_bfx(self, value, lsb, width):
result = bfxw(value, lsb, width)
assert result == bfx(value, lsb + width - 1, lsb)
assert result < (1 << width)

def test_nibble(self):
assert bfxw(0x1f0, 4, 4) == 0xf

def test_zero_width(self):
assert bfxw(0xffffffff, 4, 0) == 0