Here's an example of Chisel code for an SRT divider:
scala
import chisel3._
import chisel3.util._
class SRTDivider(width: Int) extends Module {
val io = IO(new Bundle {
val dividend = Input(UInt(width.W))
val divisor = Input(UInt(width.W))
val quotient = Output(UInt(width.W))
val remainder = Output(UInt(width.W))
val valid = Output(Bool())
})
val divisor_reg = RegNext(io.divisor)
val dividend_reg = RegNext(io.dividend)
val quotient_reg = RegInit(0.U(width.W))
val remainder_reg = RegInit(0.U(width.W))
val valid_reg = RegInit(false.B)
val shift_amount = log2Ceil(width)
val divisor_shifted = divisor_reg << shift_amount
val dividend_shifted = dividend_reg << shift_amount
val subtractor = remainder_reg - divisor_shifted
val adder = remainder_reg + divisor_shifted
val quotient_bit = Wire(UInt(1.W))
val remainder_next = Wire(UInt(width.W))
when(subtractor >= 0.U) {
quotient_bit := 1.U
remainder_next := subtractor
}.otherwise {
quotient_bit := 0.U
remainder_next := remainder_reg
}
val quotient_next = Cat(quotient_reg, quotient_bit)
val done = remainder_reg < divisor_reg
when(done) {
valid_reg := true.B
}.otherwise {
remainder_reg := remainder_next >> 1
quotient_reg := quotient_next >> 1
}
io.quotient := quotient_reg
io.remainder := remainder_reg
io.valid := valid_reg
}
This code defines an SRT divider module in Chisel. Here's a breakdown of the code:
The SRTDivider class takes a parameter width which represents the bit width of the dividend and divisor.
The module has input ports for the dividend and divisor, and output ports for the quotient, remainder, and a valid signal indicating when the division is complete.
The divisor and dividend are registered using RegNext to store their values for the duration of the division operation.
The quotient and remainder are initialized to zero using RegInit, and the valid signal is initialized to false.
The divisor and dividend are shifted left by shift_amount bits to align them for the division operation.
The subtractor calculates the difference between the current remainder and the shifted divisor, while the adder calculates their sum.
The quotient_bit and remainder_next wires are used to determine the next quotient bit and remainder based on the result of the subtraction.
If the subtraction result is greater than or equal to zero, the quotient_bit is set to 1 and remainder_next is updated with the subtraction result. Otherwise, the quotient_bit is set to 0 and remainder_next remains unchanged.
The quotient_next is formed by concatenating the current quotient_reg with the quotient_bit.
The done signal is set when the current remainder is less than the divisor, indicating that the division is complete.
When done is true, the valid_reg is set to true. Otherwise, the remainder_reg and quotient_reg are updated by shifting them right by 1 bit.
Finally, the output ports are assigned their respective values from the registers.
This SRT divider implementation performs division iteratively, generating one quotient bit per iteration until the remainder becomes smaller than the divisor.