litex/migen/genlib/divider.py

39 lines
804 B
Python
Raw Normal View History

from migen.fhdl.std import *
2011-12-16 10:02:55 -05: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()
self.dividend_i = Signal(w)
self.divisor_i = Signal(w)
2011-12-18 15:54:28 -05:00
self.ready_o = Signal()
self.quotient_o = Signal(w)
self.remainder_o = Signal(w)
2011-12-08 15:19:40 -05:00
###
2011-12-18 15:47:48 -05:00
qr = Signal(2*w)
counter = Signal(max=w+1)
divisor_r = Signal(w)
diff = Signal(w+1)
2011-12-18 15:47:48 -05: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),
2011-12-18 15:47:48 -05:00
diff.eq(self.remainder_o - divisor_r)
2011-12-08 15:19:40 -05: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
]