Skip to content

Commit 3f8cef6

Browse files
Copilotmballance
andcommitted
Add documentation for ValueInt.from_bits method
Co-authored-by: mballance <[email protected]>
1 parent f9a5027 commit 3f8cef6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/source/data_types.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,37 @@ PyVSC also provides operator overloading for `randobj`-decorated classes that
151151
allows the value of class attributes to be accessed directly.
152152

153153

154+
Converting Bit Patterns to Values
155+
==================================
156+
157+
When interfacing with hardware (DUT), bit patterns often need to be interpreted
158+
as signed or unsigned values. PyVSC provides the `ValueInt.from_bits()` class method
159+
to convert bit patterns to properly-interpreted integer values:
160+
161+
.. code-block:: python3
162+
163+
import vsc
164+
165+
# Convert unsigned 8-bit value
166+
unsigned_val = vsc.ValueInt.from_bits(0xFF, width=8, signed=False)
167+
# Result: 255
168+
169+
# Convert signed 8-bit value
170+
signed_val = vsc.ValueInt.from_bits(0xFF, width=8, signed=True)
171+
# Result: -1
172+
173+
# Practical example: reading from DUT
174+
dut_register_value = 0xFFFFFFF0
175+
python_value = vsc.ValueInt.from_bits(dut_register_value, width=32, signed=True)
176+
# Result: -16
177+
178+
The method properly:
179+
180+
* Masks the value to the specified bit width
181+
* Detects the sign bit (MSB) when `signed=True`
182+
* Converts negative values using two's complement representation
183+
184+
154185
List-type Attributes
155186
================================
156187

0 commit comments

Comments
 (0)