2017-07-20 17 views
0

假設我有一個Bool Vec。我想填充一個相同大小的新Vec,其值等於我在原始Vec中看到的這個指數的真值。我想要聯合做到這一點。鑿子:如何模擬在展開的循環中遞增的變量

隨着我的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) 
     } 
    } 
    } 

但據我所知,這是創建一個組合循環,但我不能找到一個好辦法來模擬它。不知何故,我需要生成一個新的循環變量迭代,並在迭代之間傳遞它。

回答

0

我相信我想出瞭如何在鑿子中做到這一點。我可以使用foldLeft從一個迭代通過一項新的變量下一個:

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 
    } 
    } 
1

下似乎有點簡單:

// 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 
    }