简体   繁体   中英

Why does implicit type conversion from Int to UInt not work?

I'm trying to learn chisel3, and I also try to be able to use implicit type conversion from Int to UInt in specific case.

Following is my code.

package VecTest

import chisel3._
import scala.language.implicitConversions

object VecTestMain extends App {
  Driver.execute(args, () => new VecTest)
}


object VecTest {
  implicit def int2UInt(x: Int) = new fromtIntToLiteral(x).U
}

class VecTest extends Module {
  import VecTest._

  val io = IO(new Bundle{
    val in  = Input(UInt(1.W))
    val out = Output(UInt(8.W))
  })

  val v = VecInit(0x20, 0x30)

  io.out := v(io.in)
}

I expected scala compiler will try to convert two values in VecInit from Int to UInt, but compiler reports error like below.

[error] /src/directory/this/code/VecTest/VecTest.scala:23:11: inferred type arguments [Int] do not conform to macro method apply's type parameter bounds [T <: chisel3.core.Data]
[error]   val v = VecInit(0x20, 0x30)
[error]           ^
[error] /src/directory/this/code/VecTest/VecTest.scala:23:19: type mismatch;
[error]  found   : Int(32)
[error]  required: T
[error]   val v = VecInit(0x20, 0x30)
[error]                   ^
[error] /src/directory/this/code/VecTest/VecTest.scala:23:25: type mismatch;
[error]  found   : Int(48)
[error]  required: T
[error]   val v = VecInit(0x20, 0x30)
[error]                         ^
[error] three errors found

First, I thougth compiler can not get int2UInt (implicit type converter function in object VecTest ) because of out of scope. However, when I fixed the code like below, it will work.

val v = VecInit(int2UInt(0x20), int2UInt(0x30))

I also make a hypotheses that chisel3 already has a implicit type converter like my converter, but that is probably not correct.

Where is my misstakes?

I think the closest answer is the second answer here . In short, because VecInit is parameterized with [T <: Data] the entire space of T is not searched to see what Implicit conversions might return a T.

You can manually force the proper implicit like this

val v = VecInit[UInt](0x20, 0x30)

I would like to point out that earlier versions of chisel allowed Int parameters on VecInit and its allies. Our experience was that requiring specific Hardware Types was less error prone and easier to read. Adding .U to the numbers is fairly low boilerplate overhead.

val v = VecInit(0x20.U, 0x30.U)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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