2011-12-16 15:30:14 -05:00
|
|
|
from migen.fhdl.structure import *
|
2011-12-16 10:02:55 -05:00
|
|
|
|
2012-01-20 17:07:32 -05:00
|
|
|
class Divider:
|
2011-12-08 15:19:40 -05:00
|
|
|
def __init__(self, w):
|
|
|
|
self.w = w
|
|
|
|
|
2011-12-18 15:54:28 -05:00
|
|
|
self.start_i = Signal()
|
2012-11-29 15:22:38 -05:00
|
|
|
self.dividend_i = Signal(w)
|
|
|
|
self.divisor_i = Signal(w)
|
2011-12-18 15:54:28 -05:00
|
|
|
self.ready_o = Signal()
|
2012-11-29 15:22:38 -05:00
|
|
|
self.quotient_o = Signal(w)
|
|
|
|
self.remainder_o = Signal(w)
|
2011-12-08 15:19:40 -05:00
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
2011-12-18 15:47:48 -05:00
|
|
|
w = self.w
|
|
|
|
|
2012-11-29 15:22:38 -05:00
|
|
|
qr = Signal(2*w)
|
|
|
|
counter = Signal(bits_for(w))
|
|
|
|
divisor_r = Signal(w)
|
|
|
|
diff = Signal(w+1)
|
2011-12-18 15:47:48 -05:00
|
|
|
|
2011-12-08 15:19:40 -05:00
|
|
|
comb = [
|
2011-12-18 15:47:48 -05:00
|
|
|
self.quotient_o.eq(qr[:w]),
|
|
|
|
self.remainder_o.eq(qr[w:]),
|
2012-11-28 17:18:43 -05:00
|
|
|
self.ready_o.eq(counter == 0),
|
2011-12-18 15:47:48 -05:00
|
|
|
diff.eq(self.remainder_o - divisor_r)
|
2011-12-08 15:19:40 -05:00
|
|
|
]
|
|
|
|
sync = [
|
2011-12-16 15:30:14 -05:00
|
|
|
If(self.start_i,
|
2011-12-18 15:47:48 -05:00
|
|
|
counter.eq(w),
|
|
|
|
qr.eq(self.dividend_i),
|
|
|
|
divisor_r.eq(self.divisor_i)
|
2011-12-16 15:30:14 -05:00
|
|
|
).Elif(~self.ready_o,
|
2011-12-18 15:47:48 -05:00
|
|
|
If(diff[w],
|
|
|
|
qr.eq(Cat(0, qr[:2*w-1]))
|
2011-12-16 15:30:14 -05:00
|
|
|
).Else(
|
2011-12-18 15:47:48 -05:00
|
|
|
qr.eq(Cat(1, qr[:w-1], diff[:w]))
|
2011-12-16 15:30:14 -05:00
|
|
|
),
|
2012-11-28 17:18:43 -05:00
|
|
|
counter.eq(counter - 1)
|
2011-12-16 15:30:14 -05:00
|
|
|
)
|
2011-12-08 15:19:40 -05:00
|
|
|
]
|
2011-12-16 15:30:14 -05:00
|
|
|
return Fragment(comb, sync)
|