I made some testing to understand a bit better how MyHDL works, because the documentation is not clear for me.
To my understanding MyHDL integers are using two-complement logic. The question if a value is considered “signed” or “unsigned” is only relevant for sign extension: When a signed int/modbv is assigned to a longer int/modbv its highest bit will be extended into the additional bits.
According to my experiments this only happens when there is a defined length:
>>> y=modbv(0)[32:] # Our "target"
>>> u=intbv(5) # intbv with undefined length
>>> y[:]=u.signed() # Signed makes no difference here
>>> d=intbv(5)[3:] # intbv with a defined length
>>> y[:]=d.signed() # Now it will sign extend the highest bit of d
>>> bin(y.signed()) # This is surprising, signed() seems to short return the shortest possible representation
If a intbv is initialized with a negative value it is considered signed by default
What I have not found out yet how to legally assign a negative value to an intbv of a defined bit length
>>> z=intbv(min=-2**31,max=2**31) # intbv without defined bit length
>>> len(z) # The min/max constraints implicitly define a bit length
>>> z[:]=n # Works
>>> z=intbv(min=-2**31,max=2**31)[32:] # Supposed to be 32 Bit signed integer
>>> z.max # ???
>>> z.min # ???
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/myhdl/_intbv.py", line 162, in __setitem__
File "/usr/local/lib/python2.7/dist-packages/myhdl/_intbv.py", line 79, in _handleBounds
ValueError: intbv value -1 < minimum 0
>>> xx=intbv(d.signed()) # works as expcted
>>> xx=intbv(d.signed())[32:] # strange...
So my observation is so far:
intbv are considered signed when they are either initialized with a negative value or get a negative min constraint.
The signed() method requires the int/modbv to have a defined bit length to be useful. In this case the highest bit is considered as sign bit.
Maybe I took the wrong conclusions out of my experiments.