简体   繁体   English

Chisel3。 功能模块Mux4

[英]Chisel3. Functional Module Mux4

I'm learning Chisel following the documentation on Github 我正在按照Github上的文档学习Chisel

Thus far, everything worked flawlessly. 到目前为止,一切都完美无缺。 But i'm stuck at chapter 13, "Functional Module Creation" 但是我陷入了第13章“功能模块的创建”

I can't get the code to work . 无法使代码正常工作 I created all my .scala classes in a copy of the chisel-template-project. 我在chisel-template-project的副本中创建了所有.scala类。 Here is what i wrote / copied to create a Mux4 with variable bit width : 这是我编写/复制的内容,以创建具有可变位宽Mux4

/chisel-template/src/main/scala/ Mux4.scala / 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
  }
}

The Tester scala class i wrote: 我写的Tester scala类:

/chisel-template/src/test/scala/ Mux4Test.scala / 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)
    }
  }
}

The important part from the output 输出的重要部分

STEP 0 -> 1
Circuit: 0  Expected: 5

The Mux4 class (Circuit) returns 0 as output, whereas it should be 5, because the selection process is as follows: Mux4类(电路)返回0作为输出,而应为5,因为选择过程如下:

00 -> io.out = in3 = 15 00-> io.out = in3 = 15

01 -> io.out = in2 = 11 01-> io.out = in2 = 11

10 -> io.out = in1 = 7 10-> io.out = in1 = 7

11 -> io.out = in0 = 5 11-> io.out = in0 = 5

In the Mux4Test.scala class i wrote val sel = 3 . 在Mux4Test.scala类中,我编写了val sel = 3 The bit representation of this is 11 and therefore i'd expect in0 = 5 . 它的位表示为11 ,因此我希望in0 = 5

Where am i wrong? 我哪里错了?

Thank you for your interest in Chisel! 感谢您对凿子的关注!

I ran your example, and after scratching my head for a while I found the problem: when you instantiate a Chisel Module, you need to make sure to wrap it in Module(...) (EDIT: The code on the wiki omitted this wrapper. This has been fixed). 我运行了您的示例,摸索了一段时间之后,我发现了问题:实例化凿子模块时,需要确保将其包装在Module(...) (编辑:Wiki上的代码省略了此步骤)包装器。此问题已修复)。 Thus, object Mux2 should instead be: 因此,对象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
  }
}

With this change, it looks like the code works! 进行此更改后,代码看起来可以正常工作!

didn't read all your code, but I think the Mux2 arguments are in the wrong order here: 并没有阅读所有代码,但是我认为Mux2参数在这里的顺序错误:

Mux2(io.sel(0), io.in0, io.in1) Mux2(io.sel(0),io.in0,io.in1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM