Skip to content

Commit

Permalink
feat: add CIRCTSRAMInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
unlsycn committed Nov 12, 2024
1 parent abf74af commit 8b5d415
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/main/scala/chisel3/util/experimental/CIRCTSRAMInterface.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-License-Identifier: Apache-2.0

package chisel3.util.experimental

import chisel3._
import chisel3.experimental.BaseModule
import chisel3.experimental.hierarchy.Instance
import chisel3.util.log2Ceil

import scala.collection.immutable.SeqMap

object CIRCTSRAMParameter {
implicit val rw: upickle.default.ReadWriter[CIRCTSRAMParameter] = upickle.default.macroRW
}

case class CIRCTSRAMParameter(
moduleName: String,
read: Int,
write: Int,
readwrite: Int,
depth: Int,
width: Int,
maskGranularity: Int) {
def masked: Boolean = maskGranularity != 0
}

class CIRCTSRAMInterface(memoryParameter: CIRCTSRAMParameter) extends Record {

class R extends Record {
lazy val clock = Input(Clock())
lazy val address = Input(UInt(log2Ceil(memoryParameter.depth).W))
lazy val data = Output(UInt(memoryParameter.width.W))
lazy val enable = Input(Bool())

val elements: SeqMap[String, Data] = SeqMap(
"clk" -> clock,
"addr" -> address,
"data" -> data,
"en" -> enable
)
}
class W extends Record {
lazy val clock = Input(Clock())
lazy val address = Input(UInt(log2Ceil(memoryParameter.depth).W))
lazy val data = Input(UInt(memoryParameter.width.W))
lazy val mask = Input(UInt(memoryParameter.width.W))
lazy val enable = Input(Bool())

val elements: SeqMap[String, Data] = SeqMap(
"clk" -> clock,
"addr" -> address,
"data" -> data,
"en" -> enable
) ++ Option.when(memoryParameter.masked)("mask" -> mask)
}
class RW extends Record {
lazy val clock = Input(Clock())
lazy val address = Input(UInt(log2Ceil(memoryParameter.depth).W))
lazy val writeData = Input(UInt(memoryParameter.width.W))
lazy val writeMask = Input(UInt(memoryParameter.width.W))
lazy val writeEnable = Input(Bool())
lazy val readData = Output(UInt(memoryParameter.width.W))
lazy val enable = Input(Bool())

val elements: SeqMap[String, Data] = SeqMap(
"clk" -> clock,
"addr" -> address,
"wdata" -> writeData,
"wmode" -> writeEnable,
"rdata" -> readData,
"en" -> enable
) ++ Option.when(memoryParameter.masked)("wmask" -> writeMask)
}

def R(idx: Int): R = elements(s"R$idx").asInstanceOf[R]
def W(idx: Int): W = elements(s"W$idx").asInstanceOf[W]
def RW(idx: Int): RW = elements(s"RW$idx").asInstanceOf[RW]

val elements: SeqMap[String, Data] =
(Seq.tabulate(memoryParameter.read)(i => s"R$i" -> new R) ++
Seq.tabulate(memoryParameter.write)(i => s"W$i" -> new W) ++
Seq.tabulate(memoryParameter.readwrite)(i => s"RW$i" -> new RW))
.to(SeqMap)
}

abstract class CIRCTSRAM[T <: RawModule](memoryParameter: CIRCTSRAMParameter)
extends FixedIORawModule[CIRCTSRAMInterface](new CIRCTSRAMInterface(memoryParameter)) {
override def desiredName: String = memoryParameter.moduleName
val memoryInstance: Instance[_ <: BaseModule]
}

0 comments on commit 8b5d415

Please sign in to comment.