为什么从Int到UInt的隐式类型转换不起作用?


我正在尝试学习chisel3,并且我也尝试能够在特定情况下使用从Int到UInt的隐式类型转换。

以下是我的代码。

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)
}

我预计scala编译器会尝试将VecInit中的两个值从Int转换为UInt,但编译器报告的错误如下所示。

[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

首先,我认为编译器无法获得int2UInt(object VecTest中的隐式类型转换函数),因为超出了作用域。但是,当我像下面这样修复代码时,它将会工作。

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

我还假设chisel3已经有一个隐式类型转换器,就像我的转换器一样,但这可能是不正确的。

我的错注在哪里?

转载请注明出处:http://www.souyuntu.com/article/20230526/1874925.html