1/*************************************************************************************** 2 * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3 * Copyright (c) 2020-2021 Peng Cheng Laboratory 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 xiangshan.backend.issue 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25 26class NewAgeDetector(numEntries: Int, numEnq: Int, numDeq: Int)(implicit p: Parameters) extends XSModule { 27 val io = IO(new Bundle { 28 val enq = Vec(numEnq, Input(Bool())) 29 val canIssue = Vec(numDeq, Input(UInt(numEntries.W))) 30 val out = Vec(numDeq, Output(UInt(numEntries.W))) 31 }) 32 33 // age(i)(j): entry i enters queue before entry j 34 val age = Seq.fill(numEntries)(Seq.fill(numEntries)(RegInit(false.B))) 35 val nextAge = Seq.fill(numEntries)(Seq.fill(numEntries)(Wire(Bool()))) 36 37 // to reduce reg usage, only use upper matrix 38 def get_age(row: Int, col: Int): Bool = if (row <= col) age(row)(col) else !age(col)(row) 39 def get_next_age(row: Int, col: Int): Bool = if (row <= col) nextAge(row)(col) else !nextAge(col)(row) 40 41 //only for row <= col 42 for((row, i) <- nextAge.zipWithIndex) { 43 for((elem, j) <- row.zipWithIndex) { 44 if (i == j) { 45 // an entry is always older than itself 46 elem := true.B 47 } 48 else if (i < j) { 49 when (io.enq(j)) { 50 // (1) when entry j enqueues from port k, 51 // (1.1) if entry i (<j) enqueues from previous ports, it is older than entry j 52 // (1.2) if entry i does not enqueue, set col(j) older than entry j 53 elem := true.B 54 }.elsewhen (io.enq(i)) { 55 // (2) when entry i enqueues, set row(i) to false 56 elem := false.B 57 }.otherwise { 58 // default: unchanged 59 elem := get_age(i, j) 60 } 61 } 62 else { 63 elem := !nextAge(j)(i) 64 } 65 age(i)(j) := elem 66 } 67 } 68 69 def getOldestCanIssue(get: (Int, Int) => Bool, canIssue: UInt): UInt = { 70 VecInit((0 until numEntries).map(i => { 71 (VecInit((0 until numEntries).map(j => get(i, j))).asUInt | ~canIssue).andR & canIssue(i) 72 })).asUInt 73 } 74 75 io.out.zip(io.canIssue).foreach { case (out, canIssue) => 76 out := getOldestCanIssue(get_age, canIssue) 77 } 78} 79 80object NewAgeDetector { 81 def apply(numEntries: Int, enq: Vec[Bool], canIssue: Vec[UInt])(implicit p: Parameters): Vec[Valid[UInt]] = { 82 val age = Module(new NewAgeDetector(numEntries, enq.length, canIssue.length)) 83 age.io.enq := enq 84 age.io.canIssue := canIssue 85 val outVec = Wire(Vec(canIssue.length, Valid(UInt(numEntries.W)))) 86 outVec.zipWithIndex.foreach { case (out, i) => 87 out.valid := canIssue(i).orR 88 out.bits := age.io.out(i) 89 when (out.valid) { 90 assert(PopCount(out.bits) === 1.U, s"out ($i) is not ont-hot when there is at least one entry can be issued\n") 91 } 92 } 93 outVec 94 } 95}