在使用" scanLeft"生成抽头移位寄存器时,我需要使用寄存器的IndexedSeq并将scanLeft的结果显式复制到输出线。以下是四个示例(使用testbench)。只有第一个有效。 我最喜欢第二个,并想知道为什么它不正确。
package tsr
import chisel3._
import chisel3.util._
import chisel3.iotesters._
import org.scalatest.{Matchers, FlatSpec}
object TappedShiftRegister {
def apply[ T <: Data]( d : T, n : Int) : Vec[T] = {
val rs = IndexedSeq.tabulate( n){ (i) => Reg( d.cloneType)}
val rs0 = rs.scanLeft( d){ case (x, r) => r := x; r}
val ys = Wire( Vec( n+1, d.cloneType))
(ys zip rs0).foreach{ case (y,r) => y := r}
ys
}
def apply1[ T <: Data]( d : T, n : Int) : Vec[T] = { // ChiselException
val rs = Vec.tabulate( n){ (i) => Reg( d.cloneType)}
Vec( rs.scanLeft( d){ case (x, r) => r := x; r})
}
def apply2[ T <: Data]( d : T, n : Int) : Vec[T] = { // ChiselException
val rs = IndexedSeq.tabulate( n){ (i) => Reg( d.cloneType)}
Vec( rs.scanLeft( d){ case (x, r) => r := x; r})
}
def apply3[ T <: Data]( d : T, n : Int) : Vec[T] = { // Wrong values
val rs = Vec.tabulate( n){ (i) => Reg( d.cloneType)}
val rs0 = rs.scanLeft( d){ case (x, r) => r := x; r}
val ys = Wire( Vec( n+1, d.cloneType))
(ys zip rs0).foreach{ case (y,r) => y := r}
ys
}
}
class TappedShiftRegisterIfc extends Module {
val io = IO( new Bundle {
val inp = Input( UInt(8.W))
val out = Output( Vec( 5, UInt(8.W)))
})
}
class GenericTSRTest( factory : () => TappedShiftRegisterIfc) extends FlatSpec with Matchers {
it should "meet all PeekPokeTester expectations" in {
chisel3.iotesters.Driver( factory, "firrtl") { c => new PeekPokeTester(c) {
val N = 4
val off = 47
for { i <- 0 until 100} {
poke( c.io.inp, off+i)
expect( c.io.out(0), off+i) // mealy output
step(1)
for { j <- 0 until N if i > j} {
expect( c.io.out(j+1), off+i-j) // moore outputs
}
}
}} should be (true)
}
}
class TSRTest extends GenericTSRTest( () => new TappedShiftRegisterIfc { io.out := TappedShiftRegister( io.inp, 4) })
class TSRTest1 extends GenericTSRTest( () => new TappedShiftRegisterIfc { io.out := TappedShiftRegister.apply1( io.inp, 4)})
class TSRTest2 extends GenericTSRTest( () => new TappedShiftRegisterIfc { io.out := TappedShiftRegister.apply2( io.inp, 4)})
class TSRTest3 extends GenericTSRTest( () => new TappedShiftRegisterIfc { io.out := TappedShiftRegister.apply3( io.inp, 4)})
答案 0 :(得分:1)
首先。以下,最接近您喜欢的,有效。我加入了你的测试套件。
def apply4[ T <: Data]( d : T, n : Int) : Vec[T] = { // No ChiselException!
val rs = Seq.fill(n){ Reg(d.cloneType)}
VecInit(rs.scanLeft(d){ case (x, r) => r := x; r})
}
class TSRTest4 extends GenericTSRTest( () => new TappedShiftRegisterIfc { io.out := TappedShiftRegister.apply4( io.inp, 4)})
详细信息: