You need to update an intbv() variable using the [:] notation to explicitly set the value held by that intbv(); in the other case (as you did) MyHDL considers it to be an integer.
IMO it is always best to make the result of any arithmetic operation fit: in your case you multiply 16 bits by 25 which only produces a 41-bit result. If you subsequently assign that result to an object with a wider bitwidth MyHDL will then correctly extend the value.
I assume that at least one of the two operands is signed.
You may have stumbled on one of the issues @hackfin knows all about
Can you make a minimal example of your code? I will try it out.
PS.: 2^42 doesn’t work; in Python we use 2**42
I don’t think there’s an arithmetic issue in this context, although I’m puzzled about the signedness of the multiplicands, you might want to post the full example. I’d rather have assumed that data is unsigned and factor signed, so data would be extended instead (still with the resulting bit width: 24+17). If both are signed, you get by with 40 bits in the result.
But: Variable usage (instead of signals) comes with a few caveats, potentially leading to a number of confusing scenarios, so you might want to turn that into a Signal and use the .next assignment notation, although josyb’s [:] suggestion should synthesize as well.
To note:
an intbv() multiplied by an intbv() is of type int, not intbv.
First time assignment in a @always* generator of a variable in the Python code defines the type for the HDL (that’s why you got the integer)
So: the variable is instanced locally per process in the result, but might have to be declared in the @block body for vectors/arrays, not entirely sure about that, but I remember some inconsistent behaviour.
Using multiply_reg[:] = internal_data * factor solved the problem.
Internal_data is a signed number of 17 bits, factor is an unsigned number of 24 bits. I also trimmed the width of the multiply_reg to 41 bits.
In VHDL it looks like this:
multiply_reg := resize(internal_data * signed(resize(factor, 25)), 41); which is correct VHDL.
josyb, I used 2^42 because it was not correctly shown in this message box. In Python indeed I used 242 (must be 2 * * 42) (Goes wrong, maybe due to “----” or "*" I used on the lines before and after the Python code)
By the way how does this text formatting works in this message box?