55 lines
1.2 KiB
Scala
55 lines
1.2 KiB
Scala
package exercises
|
|
|
|
import spinal.core._
|
|
import spinal.core.sim._
|
|
|
|
object PopcntSim {
|
|
def runSim(width: BitCount, multiCycle: Boolean, input: Int): Unit = {
|
|
SimConfig.withWave.compile(new Popcnt(width, multiCycle)).doSim {dut =>
|
|
dut.clockDomain.forkStimulus(10)
|
|
|
|
dut.value #= input
|
|
dut.start #= true
|
|
dut.clockDomain.waitSampling()
|
|
dut.start #= false
|
|
|
|
while (!dut.ready.toBoolean) {
|
|
dut.clockDomain.waitSampling()
|
|
}
|
|
|
|
dut.clockDomain.waitSampling()
|
|
println(s"popcnt($input) = ${dut.count.toBigInt}")
|
|
}
|
|
}
|
|
|
|
def main(args: Array[String]) {
|
|
runSim(4 bits, false, 15)
|
|
}
|
|
}
|
|
|
|
object ShiftRegSim {
|
|
def createShiftReg: ShiftReg = {
|
|
val sr = new ShiftReg
|
|
sr.addReg("foo", 8 bits, 3)
|
|
sr.addReg("bar", 16 bits, 5)
|
|
sr
|
|
}
|
|
|
|
def main(args: Array[String]) {
|
|
SimConfig.withWave.compile(createShiftReg).doSim {dut =>
|
|
dut.clockDomain.forkStimulus(10)
|
|
|
|
val (fooIn, fooOut) = dut.getReg("foo")
|
|
val (barIn, barOut) = dut.getReg("bar")
|
|
|
|
var tick = 0
|
|
|
|
while (tick < 10) {
|
|
fooIn #= tick
|
|
barIn #= 10 - tick
|
|
dut.clockDomain.waitSampling()
|
|
tick += 1
|
|
}
|
|
}
|
|
}
|
|
}
|