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 AgeDetector(numEntries: Int, numEnq: Int, numDeq: Int)(implicit p: Parameters) extends XSModule { 27 val io = IO(new Bundle { 28 // NOTE: now we do not consider deq. 29 // keeping these old invalid entries 30 // does not affect the selection of the oldest entry that can be issued. 31 val enq = Vec(numEnq, Input(UInt(numEntries.W))) 32 val canIssue = Vec(numDeq, Input(UInt(numEntries.W))) 33 val out = Vec(numDeq, Output(UInt(numEntries.W))) 34 }) 35 36 // age(i)(j): entry i enters queue before entry j 37 val age = Seq.fill(numEntries)(Seq.fill(numEntries)(RegInit(false.B))) 38 val nextAge = Seq.fill(numEntries)(Seq.fill(numEntries)(Wire(Bool()))) 39 40 // to reduce reg usage, only use upper matrix 41 def get_age(row: Int, col: Int): Bool = if (row <= col) age(row)(col) else !age(col)(row) 42 def get_next_age(row: Int, col: Int): Bool = if (row <= col) nextAge(row)(col) else !nextAge(col)(row) 43 def isEnq(i: Int): Bool = { 44 VecInit(io.enq.map(_(i))).asUInt.orR 45 } 46 def isEnqNport(i: Int, numPorts: Int = 0): Bool = { 47 numPorts match { 48 case 0 => false.B 49 case n => VecInit(io.enq.take(n).map(_(i))).asUInt.orR 50 } 51 } 52 53 for ((row, i) <- nextAge.zipWithIndex) { 54 for ((elem, j) <- row.zipWithIndex) { 55 if (i == j) { 56 // an entry is always older than itself 57 elem := true.B 58 } 59 else if (i < j) { 60 when (isEnq(i) && isEnq(j)) { 61 // (1) when entry i enqueues from port k, 62 // (1.1) if entry j enqueues from previous ports, set to false 63 // (1.2) otherwise, set to true 64 val sel = io.enq.map(_(i)) 65 val result = (0 until numEnq).map(k => isEnqNport(j, k)) 66 elem := !ParallelMux(sel, result) 67 }.elsewhen (isEnq(i)) { 68 // (2) when entry i enqueues, set row(i) to false 69 elem := false.B 70 }.elsewhen (isEnq(j)) { 71 // (3) when entry j enqueues, set col(j) to true 72 elem := true.B 73 }.otherwise { 74 // default: unchanged 75 elem := get_age(i, j) 76 } 77 } 78 else { 79 elem := !nextAge(j)(i) 80 } 81 age(i)(j) := elem 82 } 83 } 84 85 def getOldestCanIssue(get: (Int, Int) => Bool, canIssue: UInt): UInt = { 86 VecInit((0 until numEntries).map(i => { 87 (VecInit((0 until numEntries).map(j => get(i, j))).asUInt | ~canIssue).andR & canIssue(i) 88 })).asUInt 89 } 90 91 io.out.zip(io.canIssue).foreach { case (out, canIssue) => 92 out := getOldestCanIssue(get_age, canIssue) 93 } 94 95 for (i <- 0 until numEnq) { 96 assert(PopCount(io.enq(i)) <= 1.U, s"enq port ($i) is not ont-hot\n") 97 } 98} 99 100object AgeDetector { 101 def apply(numEntries: Int, enq: Vec[UInt], canIssue: Vec[UInt])(implicit p: Parameters): Vec[Valid[UInt]] = { 102 val age = Module(new AgeDetector(numEntries, enq.length, canIssue.length)) 103 age.io.enq := enq 104 age.io.canIssue := canIssue 105 val outVec = Wire(Vec(canIssue.length, Valid(UInt(numEntries.W)))) 106 outVec.zipWithIndex.foreach { case (out, i) => 107 out.valid := canIssue(i).orR 108 out.bits := age.io.out(i) 109 when (out.valid) { 110 assert(PopCount(out.bits) === 1.U, s"out ($i) is not ont-hot when there is at least one entry can be issued\n") 111 } 112 } 113 outVec 114 } 115 def apply(numEntries: Int, enq: Vec[UInt], canIssue: UInt)(implicit p: Parameters): Valid[UInt] = { 116 val age = Module(new AgeDetector(numEntries, enq.length, 1)) 117 age.io.enq := enq 118 age.io.canIssue(0) := canIssue 119 val out = Wire(Valid(UInt(numEntries.W))) 120 out.valid := canIssue.orR 121 out.bits := age.io.out(0) 122 when (out.valid) { 123 assert(PopCount(out.bits) === 1.U, "out is not ont-hot when there is at least one entry can be issued\n") 124 } 125 out 126 } 127} 128