PriorityMuxDefault.scala 1.0 KB
Newer Older
L
LinJiawei 已提交
1
package utils
L
LinJiawei 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

import chisel3._

object PriorityMuxDefault {
  def apply[T <: Data](in: Seq[(Bool, T)], default: T): T = {
    in.size match {
      case 1=>
        Mux(in.head._1, in.head._2, default)
      case _ =>
        Mux(in.head._1, in.head._2, PriorityMuxDefault(in.tail, default))
    }
  }
}

object PriorityEncoderDefault {
  def apply(in: Seq[Bool], default: UInt): UInt = {
    PriorityMuxDefault(in.zipWithIndex.map(x => x._1 -> x._2.U), default)
  }
L
LinJiawei 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
}

object PriorityMuxWithFlag {
  def apply[T <: Data](in: Seq[(Bool, T)]): (T, Bool) = {
    in.size match {
      case 1 =>
        (in.head._2, in.head._1)
      case _ =>
        val (d_tail, f_tail) = PriorityMuxWithFlag(in.tail)
        val d_head = in.head._2
        val f_head = in.head._1
        (Mux(f_head, d_head, d_tail), f_head || f_tail)
    }
  }
}

object PriorityEncoderWithFlag {
  def apply(in: Seq[Bool]): (UInt, Bool) = {
    PriorityMuxWithFlag(in.zipWithIndex.map(x => x._1 -> x._2.U))
  }
  def apply(in: Bits): (UInt, Bool) = {
    PriorityEncoderWithFlag(in.asBools())
  }
L
LinJiawei 已提交
43
}