凿子3。功能模块 Mux4
Chisel3. Functional Module Mux4
我正在按照文档学习 Chisel on Github
到目前为止,一切都完美无缺。但是我卡在了第 13 章,"Functional Module Creation"
我无法使代码工作。我在 chisel-template-project 的副本中创建了我所有的 .scala classes。这是我编写/复制的内容,用于创建具有可变位宽的 Mux4:
/chisel-template/src/main/scala/Mux4.scala
import Chisel._
class Mux4(w: Int) extends Module {
val io = IO(new Bundle {
val sel = UInt(INPUT, 2)
val in0 = UInt(INPUT, w)
val in1 = UInt(INPUT, w)
val in2 = UInt(INPUT, w)
val in3 = UInt(INPUT, w)
val out = UInt(OUTPUT, w)
})
io.out := Mux2(io.sel(1),
Mux2(io.sel(0), io.in0, io.in1),
Mux2(io.sel(0), io.in2, io.in3))
}
class Mux2(w: Int) extends Module {
val io = IO(new Bundle {
val sel = Bool(INPUT)
val in0 = UInt(INPUT, w)
val in1 = UInt(INPUT, w)
val out = UInt(OUTPUT, w)
})
when(io.sel) {
io.out := io.in0
}.otherwise {
io.out := io.in1
}
}
object Mux2 {
def apply(sel: UInt, in0: UInt, in1: UInt): UInt = {
val m = new Mux2(in0.getWidth)
m.io.sel := sel.toBool()
m.io.in0 := in0
m.io.in1 := in1
m.io.out
}
}
Tester scala class 我写道:
/chisel-template/src/test/scala/Mux4Test.scala
import Chisel.iotesters.{ChiselFlatSpec, Driver, PeekPokeTester}
class Mux4Test(c: Mux4) extends PeekPokeTester(c) {
val sel = 3
val (in0, in1, in2, in3) = (5, 7, 11, 15)
poke(c.io.sel, sel)
poke(c.io.in0, in0)
poke(c.io.in1, in1)
poke(c.io.in2, in2)
poke(c.io.in3, in3)
step(1)
System.out.println("Circuit: "+peek(c.io.out)
+" Expected: "+TestMux4.result(sel, in0, in1, in2, in3))
}
object TestMux4{
def result(sel: Int, in0: Int, in1: Int, in2: Int, in3: Int): Int = {
val out = sel match{
case 0 => in3
case 1 => in2
case 2 => in1
case 3 => in0
}
out
}
}
class Mux4Tester extends ChiselFlatSpec {
behavior of "Mux4"
backends foreach {backend =>
it should s"do Mux4 $backend" in {
Driver(() => new Mux4(4), backend)(c => new Mux4Test(c)) should be (true)
}
}
}
来自输出的重要部分
STEP 0 -> 1
Circuit: 0 Expected: 5
Mux4 class (Circuit) returns 0作为输出,而应该是5,因为选择过程如下:
00 -> io.out = in3 = 15
01 -> io.out = in2 = 11
10 -> io.out = in1 = 7
11 -> io.out = in0 = 5
在 Mux4Test.scala class 中我写了 val sel = 3。这个的位表示是 11 因此我期望 in0 = 5.
我哪里错了?
没有阅读你所有的代码,但我认为 Mux2 参数在这里的顺序错误:
Mux2(io.sel(0), io.in0, io.in1)
感谢您对 Chisel 的关注!
我运行你的例子,摸索了一会儿后我发现了问题:当你实例化一个凿子模块时,你需要确保将它包装在Module(...)
中(编辑: wiki 上的代码省略了这个包装器。这已被修复)。因此,对象 Mux2 应该改为:
object Mux2 {
def apply(sel: UInt, in0: UInt, in1: UInt): UInt = {
val m = Module(new Mux2(in0.getWidth)) // <- See Here
m.io.sel := sel.toBool()
m.io.in0 := in0
m.io.in1 := in1
m.io.out
}
}
经过此更改,代码看起来可以正常工作了!
我正在按照文档学习 Chisel on Github
到目前为止,一切都完美无缺。但是我卡在了第 13 章,"Functional Module Creation"
我无法使代码工作。我在 chisel-template-project 的副本中创建了我所有的 .scala classes。这是我编写/复制的内容,用于创建具有可变位宽的 Mux4:
/chisel-template/src/main/scala/Mux4.scala
import Chisel._
class Mux4(w: Int) extends Module {
val io = IO(new Bundle {
val sel = UInt(INPUT, 2)
val in0 = UInt(INPUT, w)
val in1 = UInt(INPUT, w)
val in2 = UInt(INPUT, w)
val in3 = UInt(INPUT, w)
val out = UInt(OUTPUT, w)
})
io.out := Mux2(io.sel(1),
Mux2(io.sel(0), io.in0, io.in1),
Mux2(io.sel(0), io.in2, io.in3))
}
class Mux2(w: Int) extends Module {
val io = IO(new Bundle {
val sel = Bool(INPUT)
val in0 = UInt(INPUT, w)
val in1 = UInt(INPUT, w)
val out = UInt(OUTPUT, w)
})
when(io.sel) {
io.out := io.in0
}.otherwise {
io.out := io.in1
}
}
object Mux2 {
def apply(sel: UInt, in0: UInt, in1: UInt): UInt = {
val m = new Mux2(in0.getWidth)
m.io.sel := sel.toBool()
m.io.in0 := in0
m.io.in1 := in1
m.io.out
}
}
Tester scala class 我写道:
/chisel-template/src/test/scala/Mux4Test.scala
import Chisel.iotesters.{ChiselFlatSpec, Driver, PeekPokeTester}
class Mux4Test(c: Mux4) extends PeekPokeTester(c) {
val sel = 3
val (in0, in1, in2, in3) = (5, 7, 11, 15)
poke(c.io.sel, sel)
poke(c.io.in0, in0)
poke(c.io.in1, in1)
poke(c.io.in2, in2)
poke(c.io.in3, in3)
step(1)
System.out.println("Circuit: "+peek(c.io.out)
+" Expected: "+TestMux4.result(sel, in0, in1, in2, in3))
}
object TestMux4{
def result(sel: Int, in0: Int, in1: Int, in2: Int, in3: Int): Int = {
val out = sel match{
case 0 => in3
case 1 => in2
case 2 => in1
case 3 => in0
}
out
}
}
class Mux4Tester extends ChiselFlatSpec {
behavior of "Mux4"
backends foreach {backend =>
it should s"do Mux4 $backend" in {
Driver(() => new Mux4(4), backend)(c => new Mux4Test(c)) should be (true)
}
}
}
来自输出的重要部分
STEP 0 -> 1
Circuit: 0 Expected: 5
Mux4 class (Circuit) returns 0作为输出,而应该是5,因为选择过程如下:
00 -> io.out = in3 = 15
01 -> io.out = in2 = 11
10 -> io.out = in1 = 7
11 -> io.out = in0 = 5
在 Mux4Test.scala class 中我写了 val sel = 3。这个的位表示是 11 因此我期望 in0 = 5.
我哪里错了?
没有阅读你所有的代码,但我认为 Mux2 参数在这里的顺序错误:
Mux2(io.sel(0), io.in0, io.in1)
感谢您对 Chisel 的关注!
我运行你的例子,摸索了一会儿后我发现了问题:当你实例化一个凿子模块时,你需要确保将它包装在Module(...)
中(编辑: wiki 上的代码省略了这个包装器。这已被修复)。因此,对象 Mux2 应该改为:
object Mux2 {
def apply(sel: UInt, in0: UInt, in1: UInt): UInt = {
val m = Module(new Mux2(in0.getWidth)) // <- See Here
m.io.sel := sel.toBool()
m.io.in0 := in0
m.io.in1 := in1
m.io.out
}
}
经过此更改,代码看起来可以正常工作了!