Skip to content

Add ValueInt.from_bits() for signed/unsigned bit pattern conversion#249

Draft
Copilot wants to merge 5 commits intomasterfrom
copilot/add-signed-helper-method
Draft

Add ValueInt.from_bits() for signed/unsigned bit pattern conversion#249
Copilot wants to merge 5 commits intomasterfrom
copilot/add-signed-helper-method

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 8, 2025

Hardware interface values stored in ValueInt are misinterpreted in Python—bit patterns from DUT registers are always treated as unsigned, breaking signed value semantics.

Changes

API Addition (src/vsc/model/value_scalar.py)

  • Added ValueInt.from_bits(value, width, signed=False) class method
  • Masks input to specified bit width
  • Applies two's complement conversion when signed=True and MSB is set

Tests (ve/unit/test_value_int.py)

  • Covers 1/4/8/16/32-bit widths, masking, sign detection, edge cases

Documentation (doc/source/data_types.rst)

  • New section: "Converting Bit Patterns to Values"

Usage

import vsc

# DUT returns 0xFFFFFFF0 from 32-bit signed register
dut_val = read_register()
python_val = vsc.ValueInt.from_bits(dut_val, width=32, signed=True)
# Result: -16 (not 4294967280)

# Unsigned interpretation
vsc.ValueInt.from_bits(0xFF, width=8, signed=False)  # 255

# Signed interpretation
vsc.ValueInt.from_bits(0xFF, width=8, signed=True)   # -1

Addresses #248

Original prompt

This section details on the original issue you should resolve

<issue_title>ValueInt class needs something like $signed method in SV</issue_title>
<issue_description>So im using the value stored in an ValueInt object in python and the stored binary value which works correctly with dut interface always gets misinterpreted in Python world because python infers the given numbers as unsigend. I think having a $signed helper method in ValueInt class can be helpful to prevent such problems. I have currently implemented a $signed function in this way:

def signed(numb, width):
if (numb >> (width-1)):
return(-((2 ** width) - numb))
else:
return numb

</issue_description>

<agent_instructions>Implement 'from_bits' as described in #248. Use the algorithm from the initial comment. Write tests to ensure value conversion (specifically, value masking, sign detection, and conversion) are implemented properly.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mballance Useful idea, @Amirk97 . I'm wondering whether it would make sense to have this be a way to construct a ValueInt. For example:
dut_val = <path>
vsc_val = vsc.ValueInt.from_bits(dut_val, width=32, signed=True)

Thoughts?</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 8, 2025 22:46
Co-authored-by: mballance <1340805+mballance@users.noreply.github.com>
Co-authored-by: mballance <1340805+mballance@users.noreply.github.com>
Copilot AI changed the title [WIP] Add $signed helper method to ValueInt class Add ValueInt.from_bits() for signed/unsigned bit pattern conversion Nov 8, 2025
Copilot AI requested a review from mballance November 8, 2025 22:52
@mballance
Copy link
Copy Markdown
Member

@copilot, let's revisit this issue. Can we get everything running?

Copilot AI and others added 2 commits March 25, 2026 20:44
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 25, 2026

@copilot, let's revisit this issue. Can we get everything running?

Two things were needed to get everything running (commit 2d6f21c):

  1. Install dependenciespyboolector, pyucis, pytest, and toposort are now available. The test suite can be run with:

    PYTHONPATH=./src python3 -m pytest ve/unit/
    
  2. Fix pyboolector API break — newer pyboolector moved BTOR_OPT_INCREMENTAL and BTOR_OPT_MODEL_GEN from module-level constants into the BtorOption enum. Updated randomizer.py to use pyboolector.BtorOption.BTOR_OPT_INCREMENTAL / pyboolector.BtorOption.BTOR_OPT_MODEL_GEN. This unblocks the ~193 tests that were previously failing with AttributeError.

The ValueInt.from_bits tests and the broader test suite (types, constraints, randomization) now all pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueInt class needs something like $signed method in SV

2 participants