L2CacheTest.scala 7.4 KB
Newer Older
L
LinJiawei 已提交
1 2 3 4 5 6 7
package cache

import chipsalliance.rocketchip.config.{Field, Parameters}
import chisel3._
import chisel3.util._
import chiseltest.experimental.TestOptionBuilder._
import chiseltest.internal.VerilatorBackendAnnotation
L
LinJiawei 已提交
8
import chiseltest.internal.LineCoverageAnnotation
L
LinJiawei 已提交
9 10
import chiseltest._
import chisel3.experimental.BundleLiterals._
L
LinJiawei 已提交
11
import firrtl.stage.RunFirrtlTransformAnnotation
L
LinJiawei 已提交
12 13 14 15 16
import chiseltest.ChiselScalatestTester
import device.AXI4RAM
import freechips.rocketchip.amba.axi4.AXI4UserYanker
import freechips.rocketchip.diplomacy.{AddressSet, LazyModule, LazyModuleImp}
import freechips.rocketchip.tilelink.{TLBuffer, TLCacheCork, TLToAXI4, TLXbar}
J
Jiuyang liu 已提交
17 18
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers
L
LinJiawei 已提交
19 20 21 22 23
import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
import utils.{DebugIdentityNode, HoldUnless, XSDebug}
import xiangshan.HasXSLog
import xiangshan.cache.{DCache, DCacheLineReq, DCacheWordReq, MemoryOpConstants}
import xiangshan.testutils.AddSinks
L
LinJiawei 已提交
24
import xstransforms.PrintModuleName
L
LinJiawei 已提交
25 26 27 28 29 30 31 32 33 34

import scala.util.Random


case class L2CacheTestParams
(
  ways: Int = 4,
  banks: Int = 1,
  capacityKB: Int = 4,
  blockBytes: Int = 64,
35 36
  beatBytes: Int = 32,
  writeBytes: Int = 8
L
LinJiawei 已提交
37 38 39 40 41 42
) {
  require(blockBytes >= beatBytes)
}

case object L2CacheTestKey extends Field[L2CacheTestParams]

43 44 45 46 47 48 49 50 51 52 53 54 55 56
case class L3CacheTestParams
(
  ways: Int = 4,
  banks: Int = 1,
  capacityKB: Int = 4,
  blockBytes: Int = 64,
  beatBytes: Int = 32,
  writeBytes: Int = 8
) {
  require(blockBytes >= beatBytes)
}

case object L3CacheTestKey extends Field[L3CacheTestParams]

L
LinJiawei 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

class L2TestTopIO extends Bundle {
  val in = Flipped(DecoupledIO(new Bundle() {
    val wdata = Input(UInt(64.W))
    val waddr = Input(UInt(20.W))
    val hartId = Input(UInt(1.W))
  }))
  val out = DecoupledIO(new Bundle() {
    val rdata = Output(UInt(64.W))
  })
}

class L2TestTop()(implicit p: Parameters) extends LazyModule{

  val cores = Array.fill(2)(LazyModule(new DCache()))
  val l2params = p(L2CacheTestKey)
A
Allen 已提交
73 74
  val l2s = (0 until 2) map (i =>
    LazyModule(new InclusiveCache(
L
LinJiawei 已提交
75 76 77 78 79
    CacheParameters(
      level = 2,
      ways = l2params.ways,
      sets = l2params.capacityKB * 1024 / (l2params.blockBytes * l2params.ways * l2params.banks),
      blockBytes = l2params.blockBytes,
A
Allen 已提交
80 81
      beatBytes = l2params.beatBytes,
      cacheName = s"L2_$i"
L
LinJiawei 已提交
82 83
    ),
    InclusiveCacheMicroParameters(
84 85 86 87 88 89 90
      writeBytes = l2params.writeBytes
    )
  )))

  val l3params = p(L3CacheTestKey)
  val l3 = LazyModule(new InclusiveCache(
    CacheParameters(
A
Allen 已提交
91
      level = 3,
92 93 94
      ways = l3params.ways,
      sets = l3params.capacityKB * 1024 / (l3params.blockBytes * l3params.ways * l3params.banks),
      blockBytes = l3params.blockBytes,
A
Allen 已提交
95 96
      beatBytes = l3params.beatBytes,
      cacheName = "L3"
97 98 99
    ),
    InclusiveCacheMicroParameters(
      writeBytes = l3params.writeBytes
L
LinJiawei 已提交
100 101 102 103
    )
  ))

  val ram = LazyModule(new AXI4RAM(
104
    Seq(AddressSet(0x0L, 0xffffffffffL)),
L
LinJiawei 已提交
105 106 107 108 109 110
    memByte = 128 * 1024 * 1024,
    useBlackBox = false
  ))

  val xbar = TLXbar()

111 112 113
  for(i <- 0 until 2) {
    val core = cores(i)
    val l2 = l2s(i)
A
Allen 已提交
114
    xbar := l2.node := core.clientNode
L
LinJiawei 已提交
115 116
  }

A
Allen 已提交
117
  l3.node := xbar
L
LinJiawei 已提交
118 119 120 121 122 123

  ram.node :=
    AXI4UserYanker() :=
    TLToAXI4() :=
    TLBuffer() :=
    TLCacheCork() :=
124
    l3.node
L
LinJiawei 已提交
125 126 127 128 129 130 131 132 133 134

  lazy val module = new LazyModuleImp(this) with HasXSLog {

    val io = IO(new L2TestTopIO)

    val in = HoldUnless(io.in.bits, io.in.fire())

    cores.foreach(_.module.io <> DontCare)

    val storePorts = cores.map(_.module.io.lsu.store)
A
Allen 已提交
135
    val loadPorts  = cores.map(_.module.io.lsu.atomics)
L
LinJiawei 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148

    def sendStoreReq(addr: UInt, data: UInt): DCacheLineReq = {
      val req = Wire(new DCacheLineReq)
      req.cmd := MemoryOpConstants.M_XWR
      req.addr := addr
      req.data := data
      req.mask := Fill(req.mask.getWidth, true.B)
      req.meta := DontCare
      req
    }

    def sendLoadReq(addr: UInt): DCacheWordReq = {
      val req = Wire(new DCacheWordReq)
A
Allen 已提交
149
      req.cmd := MemoryOpConstants.M_XA_ADD
L
LinJiawei 已提交
150
      req.addr := addr
A
Allen 已提交
151
      req.data := 0.U
L
LinJiawei 已提交
152
      req.mask := Fill(req.mask.getWidth, true.B)
L
LinJiawei 已提交
153 154 155 156
      req.meta := DontCare
      req
    }

L
LinJiawei 已提交
157
    val s_idle :: s_write_req :: s_write_resp :: s_read_req :: s_read_resp :: s_finish :: Nil = Enum(6)
L
LinJiawei 已提交
158 159
    val state = RegInit(s_idle)

L
LinJiawei 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    switch(state){
      is(s_idle){
        when(io.in.fire()){
          state := s_write_req
        }
      }
      is(s_write_req){
        when(storePorts.map(_.req.fire()).reduce(_||_)){
          state := s_write_resp
        }
      }
      is(s_write_resp){
        when(storePorts.map(_.resp.fire()).reduce(_||_)){
          state := s_read_req
        }
      }
      is(s_read_req){
        when(loadPorts.map(_.req.fire()).reduce(_||_)){
          state := s_read_resp
        }
      }
      is(s_read_resp){
        when(loadPorts.map(_.resp.fire()).reduce(_||_)){
          state := s_finish
        }
      }
L
LinJiawei 已提交
186 187 188 189 190 191 192 193 194 195 196
    }

    io.in.ready := state === s_idle

    val storeReq = Wire(new DCacheLineReq)

    storeReq := sendStoreReq(in.waddr, Fill(8, in.wdata))

    storePorts.zipWithIndex.foreach{
      case (port, i) =>
        port.req.bits := storeReq
L
LinJiawei 已提交
197
        port.req.valid := state===s_write_req && i.U===in.hartId
L
LinJiawei 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        port.resp.ready := true.B
        XSDebug(
          port.req.fire(),
          "write data %x to dcache [%d]\n",
          port.req.bits.data,
          i.U
        )
    }

    XSDebug(p"state: $state\n")

    val loadReq = sendLoadReq(in.waddr)

    loadPorts.zipWithIndex.foreach{
      case (port, i) =>
        port.req.bits := loadReq
L
LinJiawei 已提交
214
        port.req.valid := state===s_read_req && i.U=/=in.hartId
L
LinJiawei 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        port.resp.ready := true.B
        XSDebug(
          port.resp.fire(),
          "read data %x form dcache [%d]\n",
          port.resp.bits.data,
          i.U
        )
    }

    val rdata = Reg(UInt(64.W))

    when(loadPorts.map(_.resp.fire()).reduce(_||_)){
      state := s_finish
      rdata := PriorityMux(
        loadPorts.map(p => p.resp.fire() -> p.resp.bits.data)
      )
    }

    io.out.bits.rdata := rdata
    io.out.valid := state === s_finish

    when(io.out.fire()){
      state := s_idle
    }
  }

}

class L2TestTopWrapper()(implicit p: Parameters) extends LazyModule {

  val testTop = LazyModule(new L2TestTop())

  lazy val module = new LazyModuleImp(this){
    val io = IO(new L2TestTopIO)

    AddSinks()

    io <> testTop.module.io
  }
}

J
Jiuyang liu 已提交
256
class L2CacheTest extends AnyFlatSpec with ChiselScalatestTester with Matchers{
L
LinJiawei 已提交
257 258 259

  top.Parameters.set(top.Parameters.debugParameters)

260 261
  val annos = Seq(
    VerilatorBackendAnnotation,
L
LinJiawei 已提交
262
    LineCoverageAnnotation,
263 264 265
    RunFirrtlTransformAnnotation(new PrintModuleName)
  )

L
LinJiawei 已提交
266 267 268 269 270
  it should "run" in {

    implicit val p = Parameters((site, up, here) => {
      case L2CacheTestKey =>
        L2CacheTestParams()
271 272
      case L3CacheTestKey =>
        L3CacheTestParams()
L
LinJiawei 已提交
273 274
    })

275 276
     test(LazyModule(new L2TestTopWrapper()).module)
      .withAnnotations(annos){ c =>
L
LinJiawei 已提交
277 278 279 280 281 282

        c.io.in.initSource().setSourceClock(c.clock)
        c.io.out.initSink().setSinkClock(c.clock)

        c.clock.step(100)

L
LinJiawei 已提交
283
        for(i <- 0 until 100000){
L
LinJiawei 已提交
284
          val addr = Random.nextInt(0xfffff) & 0xffe00 // align to block size
L
LinJiawei 已提交
285
          val data = Random.nextLong() & 0x7fffffffffffffffL
L
LinJiawei 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298
          c.io.in.enqueue(chiselTypeOf(c.io.in.bits).Lit(
            _.waddr -> addr.U,
            _.wdata -> data.U,
            _.hartId -> Random.nextInt(2).U
          ))
          c.io.out.expectDequeue(chiselTypeOf(c.io.out.bits).Lit(
            _.rdata -> data.U
          ))
        }
    }
  }

}