2012-02-25 04:22:35 +01:00
|
|
|
package Top
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
import Chisel._
|
2012-02-25 04:22:35 +01:00
|
|
|
import Node._
|
|
|
|
import Constants._
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
class rocketDivider(width: Int) extends Component {
|
|
|
|
val io = new ioMultiplier
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
val s_ready :: s_neg_inputs :: s_busy :: s_neg_outputs :: s_done :: Nil = Enum(5) { UFix() };
|
|
|
|
val state = Reg(resetVal = s_ready);
|
|
|
|
|
2012-01-02 01:09:40 +01:00
|
|
|
val count = Reg() { UFix() };
|
|
|
|
val divby0 = Reg() { Bool() };
|
|
|
|
val neg_quo = Reg() { Bool() };
|
|
|
|
val neg_rem = Reg() { Bool() };
|
2012-02-25 04:22:35 +01:00
|
|
|
val reg_tag = Reg() { Bits() };
|
2012-01-02 01:09:40 +01:00
|
|
|
val rem = Reg() { Bool() };
|
|
|
|
val half = Reg() { Bool() };
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-01-02 01:09:40 +01:00
|
|
|
val divisor = Reg() { UFix() };
|
|
|
|
val remainder = Reg() { UFix() };
|
2011-10-26 08:02:47 +02:00
|
|
|
val subtractor = remainder(2*width, width).toUFix - divisor;
|
2012-02-25 04:22:35 +01:00
|
|
|
|
|
|
|
val dw = io.req.bits.fn(io.req.bits.fn.width-1)
|
|
|
|
val fn = io.req.bits.fn(io.req.bits.fn.width-2,0)
|
|
|
|
val tc = (fn === DIV_D) || (fn === DIV_R);
|
2012-01-03 00:42:39 +01:00
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
val do_kill = io.req_kill && Reg(io.req.ready) // kill on 1st cycle only
|
2012-02-12 02:20:33 +01:00
|
|
|
|
2011-10-26 08:02:47 +02:00
|
|
|
switch (state) {
|
|
|
|
is (s_ready) {
|
2012-02-25 04:22:35 +01:00
|
|
|
when (io.req.valid) {
|
2012-02-12 02:20:33 +01:00
|
|
|
state := Mux(tc, s_neg_inputs, s_busy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is (s_neg_inputs) {
|
|
|
|
state := Mux(do_kill, s_ready, s_busy)
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_busy) {
|
2012-02-12 02:20:33 +01:00
|
|
|
when (do_kill) {
|
|
|
|
state := s_ready
|
|
|
|
}
|
|
|
|
.elsewhen (count === UFix(width)) {
|
|
|
|
state := Mux(neg_quo || neg_rem, s_neg_outputs, s_done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is (s_neg_outputs) {
|
|
|
|
state := s_done
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_done) {
|
2012-02-25 04:22:35 +01:00
|
|
|
when (io.resp_rdy) {
|
2012-02-12 02:20:33 +01:00
|
|
|
state := s_ready
|
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
|
|
|
|
// state machine
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
val lhs_sign = tc && Mux(dw === DW_64, io.req.bits.in0(width-1), io.req.bits.in0(width/2-1)).toBool
|
|
|
|
val lhs_hi = Mux(dw === DW_64, io.req.bits.in0(width-1,width/2), Fill(width/2, lhs_sign))
|
|
|
|
val lhs_in = Cat(lhs_hi, io.req.bits.in0(width/2-1,0))
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
val rhs_sign = tc && Mux(dw === DW_64, io.req.bits.in1(width-1), io.req.bits.in1(width/2-1)).toBool
|
|
|
|
val rhs_hi = Mux(dw === DW_64, io.req.bits.in1(width-1,width/2), Fill(width/2, rhs_sign))
|
|
|
|
val rhs_in = Cat(rhs_hi, io.req.bits.in1(width/2-1,0))
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
when ((state === s_ready) && io.req.valid) {
|
2012-02-12 02:20:33 +01:00
|
|
|
count := UFix(0, log2up(width+1));
|
2012-02-25 04:22:35 +01:00
|
|
|
half := (dw === DW_32);
|
2012-02-12 02:20:33 +01:00
|
|
|
neg_quo := Bool(false);
|
|
|
|
neg_rem := Bool(false);
|
2012-02-25 04:22:35 +01:00
|
|
|
rem := (fn === DIV_R) || (fn === DIV_RU);
|
|
|
|
reg_tag := io.req_tag;
|
2012-02-12 02:20:33 +01:00
|
|
|
divby0 := Bool(true);
|
|
|
|
divisor := rhs_in.toUFix;
|
|
|
|
remainder := Cat(UFix(0,width+1), lhs_in).toUFix;
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
when (state === s_neg_inputs) {
|
2012-02-12 02:20:33 +01:00
|
|
|
neg_rem := remainder(width-1).toBool;
|
|
|
|
neg_quo := (remainder(width-1) != divisor(width-1));
|
2011-10-26 08:02:47 +02:00
|
|
|
when (remainder(width-1).toBool) {
|
2012-02-12 02:20:33 +01:00
|
|
|
remainder := Cat(remainder(2*width, width), -remainder(width-1,0)).toUFix;
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
when (divisor(width-1).toBool) {
|
2012-02-12 02:20:33 +01:00
|
|
|
divisor := subtractor(width-1,0);
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
when (state === s_neg_outputs) {
|
|
|
|
when (neg_rem && neg_quo && !divby0) {
|
2012-02-12 02:20:33 +01:00
|
|
|
remainder := Cat(-remainder(2*width, width+1), remainder(width), -remainder(width-1,0)).toUFix;
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
.elsewhen (neg_quo && !divby0) {
|
|
|
|
remainder := Cat(remainder(2*width, width), -remainder(width-1,0)).toUFix;
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
.elsewhen (neg_rem) {
|
|
|
|
remainder := Cat(-remainder(2*width, width+1), remainder(width,0)).toUFix;
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
|
2011-10-26 08:02:47 +02:00
|
|
|
when (divisor(width-1).toBool) {
|
2012-02-12 02:20:33 +01:00
|
|
|
divisor := subtractor(width-1,0);
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
when (state === s_busy) {
|
2012-02-12 02:20:33 +01:00
|
|
|
count := count + UFix(1);
|
|
|
|
divby0 := divby0 && !subtractor(width).toBool;
|
|
|
|
remainder := Mux(subtractor(width).toBool,
|
2011-10-26 08:02:47 +02:00
|
|
|
Cat(remainder(2*width-1, width), remainder(width-1,0), ~subtractor(width)),
|
|
|
|
Cat(subtractor(width-1, 0), remainder(width-1,0), ~subtractor(width))).toUFix;
|
|
|
|
}
|
|
|
|
|
|
|
|
val result = Mux(rem, remainder(2*width, width+1), remainder(width-1,0));
|
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
io.resp_bits := Mux(half, Cat(Fill(width/2, result(width/2-1)), result(width/2-1,0)), result);
|
|
|
|
io.resp_tag := reg_tag;
|
|
|
|
io.resp_val := (state === s_done);
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-02-25 04:22:35 +01:00
|
|
|
io.req.ready := (state === s_ready);
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|