代码之家  ›  专栏  ›  技术社区  ›  aayupov

凿子:如何对展开循环中递增的变量建模

  •  0
  • aayupov  · 技术社区  · 8 年前

    考虑到我的HLS背景和编码风格,我想写以下内容:

      def foo ( in : Vec[UInt] ) = {
    
        val out = Vec.fill(in.size) {UInt(in.size)}
        val nextInd = Wire(init = 0.U)
    
        in.zipWithIndex.foreach {case(val, ind) => 
          when(val === true.B) {
            out(ind) := nextInd
            nextInd := Wire(init = nextInd+1.U)
          }
        }
      }
    

    2 回复  |  直到 8 年前
        1
  •  2
  •   Chick Markley    8 年前

    以下内容似乎更简单:

      // return is a Vec with each element equal to the sum of bits up to it's index
      def foo(inBits: Vec[Bool]): Vec[UInt] = {
        val counts = Vec(size, UInt(log2Ceil(size + 1).W))
        inBits.zipWithIndex.foldLeft(0.U) { case (lastValue, (bit, index)) =>
          counts(index) := bit + lastValue
          counts(index)
        }
        counts
      }
    
        2
  •  0
  •   aayupov    8 年前

      def foo ( in : Vec[UInt] ) = {
        val vecSize = in.size
        val out = Vec.fill(vecSize) {UInt(vecSize)}
    
        in.zipWithIndex.foldLeft (Wire(init = 0.U)) {case(prevInd,(val, ind)) => 
          // val nextInd = Wire(init = prevInd) // this will not work due to bitwidth be not enough to hold an incremented value, so i do the following instead
          val nextInd = Wire(init = UInt(vecSize)) // here i just make sure the width is enough
          nextInd := prevInd
          when(val === true.B) {
            out(ind) := prevInd
            nextInd := Wire(init = prevInd+1.U)
          }
          nextInd
        }
      }
    
    推荐文章