Re: [myhdl-list] intbv addition
Brought to you by:
jandecaluwe
|
From: Sami Al D. <sam...@gm...> - 2009-06-06 21:08:57
|
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:])
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
>
--
Best Regards
Sami Aldalahmeh
|