Myhdl signed assignment fails at runtime

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?

Can you try the following (not tested) ?

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

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.

Just found the culprit:

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

Thanks!

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.