2013-05-22 11:11:09 -04:00
|
|
|
from migen.fhdl.std import *
|
2011-12-16 10:02:55 -05:00
|
|
|
|
2013-05-22 11:11:09 -04:00
|
|
|
class Divider(Module):
|
2011-12-08 15:19:40 -05:00
|
|
|
def __init__(self, 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
|
|
|
|
2013-05-22 11:11:09 -04:00
|
|
|
###
|
2011-12-18 15:47:48 -05:00
|
|
|
|
2012-11-29 15:22:38 -05:00
|
|
|
qr = Signal(2*w)
|
2012-11-29 15:53:36 -05:00
|
|
|
counter = Signal(max=w+1)
|
2012-11-29 15:22:38 -05:00
|
|
|
divisor_r = Signal(w)
|
|
|
|
diff = Signal(w+1)
|
2011-12-18 15:47:48 -05:00
|
|
|
|
2013-05-22 11:11:09 -04:00
|
|
|
self.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),
|
2013-12-02 11:56:03 -05:00
|
|
|
diff.eq(qr[w-1:] - divisor_r)
|
2011-12-08 15:19:40 -05:00
|
|
|
]
|
2013-05-22 11:11:09 -04:00
|
|
|
self.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
|
|
|
]
|