1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2024 Institute of Computing Technology, Chinese Academy of Sciences 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package utils 18 19import chisel3._ 20import chisel3.util._ 21import freechips.rocketchip.amba.axi4.AXI4Bundle 22 23class AXI4LiteBundleA(addrWidth: Int, idWidth: Int = 0) extends Bundle { 24 val id = UInt(idWidth.W) 25 val addr = UInt(addrWidth.W) 26} 27 28class AXI4LiteBundleAR(addrWidth: Int, idWidth: Int = 0) extends AXI4LiteBundleA(addrWidth, idWidth) 29 30class AXI4LiteBundleAW(addrWidth: Int, idWidth: Int = 0) extends AXI4LiteBundleA(addrWidth, idWidth) 31 32class AXI4LiteBundleW(dataWidth: Int) extends Bundle { 33 val data = UInt(dataWidth.W) 34} 35 36class AXI4LiteBundleR(dataWidth: Int, idWidth: Int = 0) extends Bundle { 37 val id = UInt(idWidth.W) 38 val data = UInt(dataWidth.W) 39 val resp = UInt(2.W) 40} 41 42class AXI4LiteBundleB(idWidth: Int = 0) extends Bundle { 43 val id = UInt(idWidth.W) 44 val resp = UInt(2.W) 45} 46 47class AXI4LiteBundle(val addrWidth: Int, val dataWidth: Int, val idWidth: Int = 0) extends Bundle { 48 val aw = Irrevocable(new AXI4LiteBundleAW(addrWidth, idWidth)) 49 val w = Irrevocable(new AXI4LiteBundleW(dataWidth)) 50 val b = Flipped(Irrevocable(new AXI4LiteBundleB(idWidth))) 51 val ar = Irrevocable(new AXI4LiteBundleAR(addrWidth, idWidth)) 52 val r = Flipped(Irrevocable(new AXI4LiteBundleR(dataWidth, idWidth))) 53 54 private def connectExisting(left: Bundle, right: Bundle): Unit = { 55 for ((name, data) <- left.elements) 56 if (right.elements.contains(name)) 57 data := right.elements(name) 58 else 59 data := (name match { 60 case "size" => log2Ceil(dataWidth).U 61 case "last" => true.B.asTypeOf(data) 62 case _: String => 0.U.asTypeOf(data) 63 }) 64 } 65 66 def connectToAXI4(axi4: AXI4Bundle): Unit = { 67 axi4.aw.valid := aw.valid 68 aw.ready := axi4.aw.ready 69 connectExisting(axi4.aw.bits, aw.bits) 70 71 axi4.w.valid := w.valid 72 w.ready := axi4.w.ready 73 connectExisting(axi4.w.bits, w.bits) 74 75 axi4.ar.valid := ar.valid 76 ar.ready := axi4.ar.ready 77 connectExisting(axi4.ar.bits, ar.bits) 78 79 b.valid := axi4.b.valid 80 axi4.b.ready := b.ready 81 connectExisting(b.bits, axi4.b.bits) 82 83 r.valid := axi4.r.valid 84 axi4.r.ready := r.ready 85 connectExisting(r.bits, axi4.r.bits) 86 } 87 88 def connectFromAXI4(axi4: AXI4Bundle): Unit = { 89 aw.valid := axi4.aw.valid 90 axi4.aw.ready := aw.ready 91 connectExisting(aw.bits, axi4.aw.bits) 92 93 w.valid := axi4.w.valid 94 axi4.w.ready := w.ready 95 connectExisting(w.bits, axi4.w.bits) 96 97 ar.valid := axi4.ar.valid 98 axi4.ar.ready := ar.ready 99 connectExisting(ar.bits, axi4.ar.bits) 100 101 axi4.b.valid := b.valid 102 b.ready := axi4.b.ready 103 connectExisting(axi4.b.bits, b.bits) 104 105 axi4.r.valid := r.valid 106 r.ready := axi4.r.ready 107 connectExisting(axi4.r.bits, r.bits) 108 } 109} 110