Re: [myhdl-list] intbv addition
Brought to you by:
jandecaluwe
|
From: Christopher L. F. <cf...@uc...> - 2009-06-06 23:10:49
|
On Jun 6, 2009, at 4:32 PM, Christopher L. Felton wrote:
>
>> Dear Chris,
>>
>> Thank you for your reply. I am trying to add numbers in MyHDL, I
>> tried
>> to add numbers modeled by 'intbv' in two stages; the first was simple
>> program with no 'next' command included, and the other with 'next'.
>> The first one worked as expected but the second one strangely gave
>> weird results, when the numbers are positive, but when they are
>> extended to negative ranges the interpreter complains about exceeding
>> the minimum range of 0. Here is the code:
>>
>> from myhdl import Signal, delay, always, intbv, Simulation, bin
>> from random import randrange
>> from math import pow
>>
>> def AddNum(B):
>>
>> #interval = delay(10)
>> max_num = pow(2,B-1) - 1
>> min_num = -1 * pow(2,B-1)
>>
>> a = Signal(intbv(0)[B:])
>> b = Signal(intbv(0)[B:])
>> c = Signal(intbv(0)[B+1:])
I probably didn't explain very well. Below is code that works.
from myhdl import Signal, delay, always, intbv, Simulation, bin
from random import randrange
from math import pow
def AddNum(B):
max_num = pow(2,B-1) - 1
min_num = -1 * pow(2,B-1)
a = Signal(intbv(0, min_num, max_num))
b = Signal(intbv(0, min_num, max_num))
c = Signal(intbv(0, 2*min_num, 2*max_num))
for i in range(10):
a.next = randrange(min_num+1, max_num)
b.next = randrange(min_num+1, max_num)
c.next = a + b
yield delay(10)
print "i: %s a: %s, b: %s sum = %s binary %s" %(i, a, b, c,
bin(c, B))
def main():
bitwidth = 9
inst = AddNum(bitwidth)
sim = Simulation(inst)
sim.run()
if __name__ == '__main__':
main()
>>
> The error you are seeing is correct. There is basically two modes
> for the intbv. Constraint integer (see Jan's essay for more info)
> and the regular bit-vector. When using the intbv for integer
> representation, as in your case with negative and positive numbers,
> you need to define the maximum and minimum. Instead of defining the
> number of bits you only need to define the expected max and min for
> the design.
>
> Example
> a = Signal(intbv(0, min=min_num, max=max_num))
> b = Signal(intbv(0, min=min_num, max=max_num))
> c = Signal(intbv(0, min=min_num, max=max_num))
> This is a little different than what you maybe use to in Verilog or
> VHDL but it can be very powerful. When you define the bit width it
> is expected that the intbv is used as a generic bit array (no
> numeric value). But the implicit numeric min is zero and the max is
> the size of the bit vector.
>
> Hope that helps
>
>> for i in range(10):
>>
>> a.next = randrange(0, max_num)
>> b.next = randrange(0, max_num)
>> c.next = a + b
>>
>> yield delay(10)
>> print "i: %s a: %s, b: %s sum = %s binary %s" %(i, a, b, c,
>> bin(c, B))
>>
>>
>> def main():
>> bitwidth = 9
>> inst = AddNum(bitwidth)
>> sim = Simulation(inst)
>> sim.run()
>>
>>
>> if __name__ == '__main__':
>> main()
>>
>> As I said, when negative numbers are used, i.e.
>>
>> a.next = randrange(min_num, max_num)
>> b.next = randrange(min_num, max_num)
>>
>> Python complains. What do you think is the problem?
>>
>> On Sat, Jun 6, 2009 at 5:34 PM, Christopher L.
>> Felton<chr...@gm...> wrote:
>>
>>>> a=intbv(3)[2:]
>>>> b=intbv(1)[2:]
>>>> c=a+b
>>>>
>>>> the sum (c) is not 'intbv' class, its 'long' if I'm not mistaken.
>>>> How
>>>> can we get the sum to be 'intbv' of the same bitwidth (in this case
>>>> 2)?
>>>>
>>>>
>>>>
>>> You would want to do the following (which is the same as other HDLs)
>>>
>>> a = intbv(3)[2:]
>>> b = intbv(1)[2:]
>>> c = intbv(1)[2:]
>>> c[:] = a+b
>>>
>>> As you noticed, the result of the intbv math operations returns an
>>> integer (long). What you need to do is create an intbv type for the
>>> left hand side of the operation. For the above example you will
>>> get an
>>> error. The intbv will check the limits of addition. You will
>>> need to
>>> create "c" to be large enough for the result of the addition.
>>>
>>> c = intbv(1)[3:]
>>>
>>>
>>> Hope that helps, for more background information see Jan's essay on
>>> constraint integer, http://www.jandecaluwe.com/hdldesign/counting.html
>>> .
>>>
>>> Chris
>>>
>>> ------------------------------------------------------------------------------
>>> OpenSolaris 2009.06 is a cutting edge operating system for
>>> enterprises
>>> looking to deploy the next generation of Solaris that includes the
>>> latest
>>> innovations from Sun and the OpenSource community. Download a copy
>>> and
>>> enjoy capabilities such as Networking, Storage and Virtualization.
>>> Go to: http://p.sf.net/sfu/opensolaris-get
>>> _______________________________________________
>>> myhdl-list mailing list
>>> myh...@li...
>>> https://lists.sourceforge.net/lists/listinfo/myhdl-list
>>>
>>>
>>
>>
> ------------------------------------------------------------------------------
> OpenSolaris 2009.06 is a cutting edge operating system for enterprises
> looking to deploy the next generation of Solaris that includes the
> latest
> innovations from Sun and the OpenSource community. Download a copy and
> enjoy capabilities such as Networking, Storage and Virtualization.
> Go to: http://p.sf.net/sfu/opensolaris-get_______________________________________________
> myhdl-list mailing list
> myh...@li...
> https://lists.sourceforge.net/lists/listinfo/myhdl-list
|