2023-10-19 18:31:26 +08:00
|
|
|
package Switch
|
|
|
|
|
|
|
|
|
|
import chisel3._
|
|
|
|
|
import chisel3.util._
|
|
|
|
|
|
|
|
|
|
|
2023-10-20 14:42:15 +08:00
|
|
|
import org.chipsalliance.cde.config._
|
|
|
|
|
import freechips.rocketchip.diplomacy._
|
|
|
|
|
import freechips.rocketchip.tilelink._
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 18:27:58 +08:00
|
|
|
abstract class SwitchMuxBase(edgeOut: TLEdgeOut)(implicit p: Parameters) extends SwitchModule{
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
class MacTileLinkMasterIO extends Bundle{
|
|
|
|
|
val A = Decoupled(new TLBundleA(edgeOut.bundle))
|
|
|
|
|
val D = Flipped(Decoupled(new TLBundleD(edgeOut.bundle)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SwitchMuxIO(implicit p: Parameters) extends SwitchBundle{
|
|
|
|
|
val dmaMst = new MacTileLinkMasterIO
|
|
|
|
|
|
|
|
|
|
val triTx = Input(Bool())
|
2023-10-24 17:42:25 +08:00
|
|
|
val triRx = Output(Bool())
|
|
|
|
|
val r_TxPtr = Input(UInt(32.W))
|
|
|
|
|
val r_RxPtr = Input(UInt(32.W))
|
|
|
|
|
val r_TxLen = Input(UInt(16.W))
|
|
|
|
|
val r_RxLen = Output(UInt(16.W))
|
2023-10-19 18:31:26 +08:00
|
|
|
|
2023-10-20 14:42:15 +08:00
|
|
|
val rxEnq = Vec(chn, Flipped(new Receive_Enq_Bundle))
|
|
|
|
|
val txDeq = Vec(chn, new Transmit_Bundle)
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val io: SwitchMuxIO = IO(new SwitchMuxIO)
|
2023-10-20 14:42:15 +08:00
|
|
|
val (_, _, isLastD, transDCnt) = edgeOut.count(io.dmaMst.D)
|
|
|
|
|
|
|
|
|
|
val rxBuff = Module(new RxBuff)
|
|
|
|
|
rxBuff.io.enq <> io.rxEnq(0)
|
2023-10-19 18:31:26 +08:00
|
|
|
|
2023-10-24 14:00:51 +08:00
|
|
|
|
2023-10-20 14:42:15 +08:00
|
|
|
val txBuff = Module(new TxBuff)
|
|
|
|
|
txBuff.io.deq <> io.txDeq(0)
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
def stateIdle = 0.U
|
|
|
|
|
def stateRx = 1.U
|
|
|
|
|
def stateTx = 2.U
|
|
|
|
|
|
|
|
|
|
val stateNxt = Wire(UInt(2.W))
|
|
|
|
|
val stateCur = RegNext(stateNxt, stateIdle)
|
2023-10-27 18:27:58 +08:00
|
|
|
val stateDMA = RegInit(0.U(2.W))
|
|
|
|
|
|
|
|
|
|
when( stateNxt =/= stateIdle && stateCur === stateIdle ){
|
|
|
|
|
stateDMA := 1.U
|
|
|
|
|
} .elsewhen( io.dmaMst.A.fire ){
|
|
|
|
|
stateDMA := 2.U
|
|
|
|
|
} .elsewhen( io.dmaMst.D.fire & isLastD ){
|
|
|
|
|
when( stateCur === stateTx & dmaAddress < (io.r_TxPtr + io.r_TxLen) ){
|
|
|
|
|
stateDMA := 0.U
|
|
|
|
|
} .elsewhen( stateCur === stateRx & rxBuff.io.deq.data.valid ){
|
|
|
|
|
stateDMA := 0.U
|
|
|
|
|
} .otherwise{
|
|
|
|
|
stateDMA := 1.U
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
stateNxt :=
|
|
|
|
|
Mux1H(Seq(
|
2023-10-27 18:27:58 +08:00
|
|
|
( stateCur === stateIdle ) -> Mux( (io.triTx & io.r_TxLen > 32.U), stateTx, Mux( rxBuff.io.deq.header.fire, stateRx, stateIdle ) ),
|
2023-10-19 18:31:26 +08:00
|
|
|
( stateCur === stateRx ) -> Mux( rxBuff.io.deq.ctrl.fire, stateIdle, stateRx ),
|
|
|
|
|
( stateCur === stateTx ) -> Mux( txBuff.io.enq.resp.fire, stateIdle, stateTx ),
|
|
|
|
|
))
|
|
|
|
|
|
2023-10-24 17:42:25 +08:00
|
|
|
io.triRx := rxBuff.io.deq.ctrl.fire
|
|
|
|
|
io.r_RxLen := RegEnable( rxBuff.io.deq.ctrl.bits.LatchedRxLength, rxBuff.io.deq.ctrl.fire )
|
2023-10-20 15:49:24 +08:00
|
|
|
rxBuff.io.deq.ctrl.ready := stateCur === stateRx & io.dmaMst.D.fire & isLastD
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
txBuff.io.enq.resp.ready := true.B
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 18:27:58 +08:00
|
|
|
val dmaAValid = RegInit(false.B)
|
|
|
|
|
val dmaABits = Reg(new TLBundleA(edgeOut.bundle))
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
val dmaAddress = Reg( UInt(32.W) )
|
|
|
|
|
|
2023-10-24 17:42:25 +08:00
|
|
|
when( stateCur === stateIdle & stateNxt === stateTx ){
|
|
|
|
|
dmaAddress := io.r_TxPtr
|
|
|
|
|
} .elsewhen( stateCur === stateIdle & stateNxt === stateRx ){
|
|
|
|
|
dmaAddress := io.r_RxPtr
|
2023-10-20 14:42:15 +08:00
|
|
|
} .elsewhen( io.dmaMst.A.fire ){
|
2023-10-21 21:08:40 +08:00
|
|
|
dmaAddress := dmaAddress + 1.U
|
2023-10-19 18:31:26 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-20 15:49:24 +08:00
|
|
|
rxBuff.io.deq.data.ready := io.dmaMst.A.fire & (stateCur === stateRx)
|
|
|
|
|
assert( ~(rxBuff.io.deq.data.ready & ~rxBuff.io.deq.data.valid) )
|
|
|
|
|
|
2023-10-27 18:27:58 +08:00
|
|
|
rxBuff.io.deq.header.ready := ~(io.triTx & io.r_TxLen > 32.U) & stateCur === stateIdle
|
2023-10-19 18:31:26 +08:00
|
|
|
|
2023-10-20 15:49:24 +08:00
|
|
|
when( io.dmaMst.A.fire ){
|
2023-10-27 18:27:58 +08:00
|
|
|
dmaAValid := false.B
|
|
|
|
|
}.elsewhen( stateDMA === 1.U & ~io.dmaMst.A.valid & stateCur === stateTx ){
|
|
|
|
|
dmaAValid := true.B
|
|
|
|
|
dmaABits :=
|
2023-10-19 18:31:26 +08:00
|
|
|
edgeOut.Get(
|
|
|
|
|
fromSource = 0.U,
|
2023-10-21 21:08:40 +08:00
|
|
|
toAddress = dmaAddress,
|
|
|
|
|
lgSize = log2Ceil(8/8).U,
|
2023-10-19 18:31:26 +08:00
|
|
|
)._2
|
2023-10-27 18:27:58 +08:00
|
|
|
} .elsewhen( stateDMA === 1.U & ~io.dmaMst.A.valid & stateCur === stateRx ) {
|
|
|
|
|
dmaAValid := true.B
|
|
|
|
|
dmaABits :=
|
2023-10-19 18:31:26 +08:00
|
|
|
edgeOut.Put(
|
|
|
|
|
fromSource = 0.U,
|
2023-10-21 21:08:40 +08:00
|
|
|
toAddress = dmaAddress,
|
|
|
|
|
lgSize = log2Ceil(8/8).U,
|
2023-10-19 18:31:26 +08:00
|
|
|
data = rxBuff.io.deq.data.bits,
|
2023-10-21 21:08:40 +08:00
|
|
|
mask = "b1".U,
|
2023-10-20 15:49:24 +08:00
|
|
|
)._2
|
2023-10-19 18:31:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 18:27:58 +08:00
|
|
|
io.dmaMst.A.valid := dmaAValid
|
|
|
|
|
io.dmaMst.A.bits := dmaABits
|
2023-10-20 15:49:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 18:27:58 +08:00
|
|
|
txBuff.io.enq.req.valid := io.dmaMst.D.valid & stateCur === stateTx
|
|
|
|
|
txBuff.io.enq.req.bits.data := io.dmaMst.D.bits.data
|
|
|
|
|
txBuff.io.enq.req.bits.isStart := dmaAddress === io.r_TxPtr
|
|
|
|
|
txBuff.io.enq.req.bits.isLast := dmaAddress === (io.r_TxPtr + io.r_TxLen)
|
2023-10-19 18:31:26 +08:00
|
|
|
|
|
|
|
|
|
2023-10-20 14:42:15 +08:00
|
|
|
io.dmaMst.D.ready :=
|
2023-10-19 18:31:26 +08:00
|
|
|
Mux1H(Seq(
|
2023-10-20 14:42:15 +08:00
|
|
|
( stateCur === stateRx ) -> true.B,
|
2023-10-27 18:27:58 +08:00
|
|
|
( stateCur === stateTx ) -> txBuff.io.enq.req.ready,
|
2023-10-19 18:31:26 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 18:27:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
trait SwitchMuxDMATx{ this: SwitchMuxBase =>
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SwitchMux(edgeOut: TLEdgeOut)(implicit p: Parameters) extends SwitchMuxBase(TLEdgeOut)
|
|
|
|
|
with SwitchMuxDMATx
|