Skip to content

Commit

Permalink
implement mtimer apply
Browse files Browse the repository at this point in the history
  • Loading branch information
PorterLu committed May 18, 2023
1 parent 116d696 commit 8a5c491
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
32 changes: 11 additions & 21 deletions src/main/scala/devices/tilelink/MSWI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,17 @@ class MSWI(mswiParams: MSWIParams, mtimerParams: MTIMERParams, isACLINT: Boolean
val nTiles = intnode.out.size
val ipi = Seq.fill(nTiles) { RegInit(0.U(1.W)) }

val io = IO(new Bundle {
val rtcTick = (!isACLINT).option(Input(Bool()))
})

val timecmp = (!isACLINT).option(Seq.fill(nTiles) { Reg(UInt(MTIMERConsts.mtimeWidth.W)) })
val time = (!isACLINT).option(RegInit(0.U(MTIMERConsts.mtimeWidth.W)))

io.rtcTick.map { tick =>
when (tick) { time.get := time.get + 1.U }
}

val (intnode_out, _) = intnode.out.unzip
intnode_out.zipWithIndex.foreach { case (int, i) =>
int(0) := ShiftRegister(ipi(i)(0), mswiParams.intStages)
if (!isACLINT) {
int(1) := ShiftRegister(time.get.asUInt >= (timecmp.get)(i).asUInt, mtimerParams.intStages)
}
}

val io = IO(new Bundle {
val rtcTick = (!isACLINT).option(Input(Bool()))
})

val mtimerRegGroup: Option[(Seq[RegField], Seq[RegField])] = (!isACLINT).option(MTIMER(io.rtcTick.get, intnode, 1, mtimerParams.intStages))

/* aclint:
* 0 msip hart 0
* 4 msip hart 1
Expand All @@ -120,19 +112,17 @@ class MSWI(mswiParams: MSWIParams, mtimerParams: MTIMERParams, isACLINT: Boolean
* bff8 mtime lo
* bffc mtime hi
*/
val mtimerRegGroup = if(!isACLINT) Seq(
0x4000 -> timecmp.get.zipWithIndex.flatMap{ case (t, i) => RegFieldGroup(s"mtimecmp_$i", Some(s"MTIMECMP for hart $i"),
RegField.bytes(t, Some(RegFieldDesc(s"mtimecmp_$i", "", reset=None))))},
0xbff8 -> RegFieldGroup("mtime", Some("Timer Register"),
RegField.bytes(time.get, Some(RegFieldDesc("mtime", "", reset=Some(0), volatile=true))))
val mtimerRegMapping = if(!isACLINT) Seq(
0x4000 -> mtimerRegGroup.get._1,
0xbff8 -> mtimerRegGroup.get._2
) else Nil

val mswiRegGroup = Seq(
val mswiRegMapping = Seq(
0 -> RegFieldGroup ("msip", Some("MSIP Bits"), ipi.zipWithIndex.flatMap{ case (r, i) =>
RegField(1, r, RegFieldDesc(s"msip_$i", s"MSIP bit for Hart $i", reset=Some(0))) :: RegField(MSWIConsts.ipiWidth - 1) :: Nil })
)

val mapping = mswiRegGroup ++ mtimerRegGroup
val mapping = mswiRegMapping ++ mtimerRegMapping
node.regmap(mapping:_*)
}
}
Expand Down
47 changes: 28 additions & 19 deletions src/main/scala/devices/tilelink/Mtimer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,7 @@ class MTIMER(params: MTIMERParams, beatBytes: Int)(implicit p: Parameters) exten
val rtcTick = Input(Bool())
})

val time = RegInit(0.U(mtimeWidth.W))
when (io.rtcTick) { time := time + 1.U }

val nTiles = intnode.out.size
val timecmp = Seq.fill(nTiles) { Reg(UInt(mtimeWidth.W)) }

val (intnode_out, _) = intnode.out.unzip
intnode_out.zipWithIndex.foreach { case (int, i) =>
int(0) := ShiftRegister(time.asUInt >= timecmp(i).asUInt, params.intStages)
}
val (timecmpRegGroup, timeRegGroup): (Seq[RegField], Seq[RegField]) = MTIMER(io.rtcTick, intnode, 0, params.intStages)

/*
* Two base addresses:
Expand All @@ -92,14 +83,32 @@ class MTIMER(params: MTIMERParams, beatBytes: Int)(implicit p: Parameters) exten
* 0 mtime lo
* 4 mtime hi
*/
mtimecmpNode.regmap(
0 -> timecmp.zipWithIndex.flatMap{ case (t, i) => RegFieldGroup(s"mtimecmp_$i", Some(s"MTIMECMP for hart $i"),
RegField.bytes(t, Some(RegFieldDesc(s"mtimecmp_$i", "", reset=None))))}
)

mtimeNode.regmap(
0 -> RegFieldGroup("mtime", Some("Timer Register"),
RegField.bytes(time, Some(RegFieldDesc("mtime", "", reset=Some(0), volatile=true))))
)
mtimecmpNode.regmap(0 -> timecmpRegGroup)
mtimeNode.regmap(0 -> timeRegGroup)
}
}

object MTIMER {
def apply(rtcTick: Bool, intnode: IntNexusNode, intIndex: Int, intStages: Int) = {
import MTIMERConsts._

val time = RegInit(0.U(mtimeWidth.W))
when(rtcTick) { time := time + 1.U }

val nTiles = intnode.out.size
val timecmp = Seq.fill(nTiles) { Reg(UInt(mtimeWidth.W)) }

val (intnode_out, _) = intnode.out.unzip
intnode_out.zipWithIndex.foreach { case (int, i) =>
int(intIndex) := ShiftRegister(time.asUInt >= timecmp(i).asUInt, intStages)
}

val timecmpRegGroup = timecmp.zipWithIndex.flatMap{ case (t, i) => RegFieldGroup(s"mtimecmp_$i", Some(s"MTIMECMP for hart $i"),
RegField.bytes(t, Some(RegFieldDesc(s"mtimecmp_$i", "", reset=None))))}

val timeRegGroup = RegFieldGroup("mtime", Some("Timer Register"),
RegField.bytes(time, Some(RegFieldDesc("mtime", "", reset=Some(0), volatile=true))))

(timecmpRegGroup, timeRegGroup)
}
}

0 comments on commit 8a5c491

Please sign in to comment.