# Myhdl signed assignment fails at runtime

**URL:** <https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440>\
**Category:** Support\
**Created:** [December 23, 2020, 3:54pm UTC](https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440 "2020-12-23T15:54:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stekar](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stekar](https://discourse.myhdl.org/u/stekar)\
**Post date:** [December 23, 2020, 3:54pm UTC](https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440/1 "2020-12-23T15:54:39Z")

</div>

I describe in py post in stackoverflow -with a short testcase- the following assignment of a 16 bit unsigned rxdata and a signed 12 bit bipolar:

bipolar.next=rxdata[:4].signed()

this assignment fails at runtime with negative numbers in rxdata. (The checker assumes rxdata is unsigned and does not take into account the .signed() )

Maybe I need to use shadow Signals and .signed() to extract a signal bipolar (12 bit signed) from a unsigned Register rxdata (16 bit from a spi Transfer):

bipolar.next=rxdata(16,4).signed()

This makes the Simulation work – but unfortunately is not compiling and running in my fpga.

I’m sure there must be a ‘right’ way to do it. Can someone give me a hint?

---

<div class="post-metadata">

**Author:** ![DrPi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/drpi/32/36_2.png) [@DrPi](https://discourse.myhdl.org/u/DrPi)\
**Post date:** [December 23, 2020, 4:49pm UTC](https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440/2 "2020-12-23T16:49:49Z")

</div>

Can you try the following (not tested) ?

```python
        s = intbv(0)[len(bipolar):]
        s[:] = rxdata[:(nspi-n)]
        bipolar.next = s.signed()

```

---

<div class="post-metadata">

**Author:** ![stekar](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stekar](https://discourse.myhdl.org/u/stekar)\
**Post date:** [December 23, 2020, 5:30pm UTC](https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440/3 "2020-12-23T17:30:25Z")

</div>

Thanks for your help!  
I’ m about to try this.

The response with rxdata=0x8000 is still:  
ValueError: intbv value 2048 \>= maximum 2048

nspi is 16  
n is 12 is len(bipolar)

Note: shadow signal works. But shadow and signed() are not translated , can not be used in hw.

---

<div class="post-metadata">

**Author:** ![stekar](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stekar](https://discourse.myhdl.org/u/stekar)\
**Post date:** [December 23, 2020, 5:54pm UTC](https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440/4 "2020-12-23T17:54:46Z")

</div>

Just found the culprit:

defining  
bipolar=modbv(0,min=-2 **11,max=2** 11)  
(instead of intbv) works fine!

Thanks!

---

<div class="post-metadata">

**Author:** ![DrPi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/drpi/32/36_2.png) [@DrPi](https://discourse.myhdl.org/u/DrPi)\
**Post date:** [December 23, 2020, 6:50pm UTC](https://discourse.myhdl.org/t/myhdl-signed-assignment-fails-at-runtime/440/5 "2020-12-23T18:50:05Z")

</div>

Applying `.signed()` to a slice should work. This is a MyHDL limitation.  
The use of a variable is a workaround. If you look at the generated code, it is ok but not ideal.

> Thanks!

You’re welcome.
