gpt4 book ai didi

scala - Chisel switch 语句似乎无法按照官方教程中概述的方式工作

转载 作者:行者123 更新时间:2023-12-01 19:47:19 27 4
gpt4 key购买 nike

当我收到与我使用的 switch 语句相关的多个错误时,我试图在 chisel 中为电路创建控制逻辑。我决定运行官方chisel tutorial第9页和第10页提供的switch语句示例代码隔离问题。

Scala 代码:

package Testbed

import Chisel._


class Testbed extends Module {
val io = new Bundle {
val nickel = Bool(dir = INPUT)
val dime = Bool(dir = INPUT)
val rdy = Bool(dir = OUTPUT) }

val s_idle :: s_5 :: s_10 :: s_15 :: s_ok :: Nil = Enum(UFix(), 5)
val state = Reg(init = s_idle)

switch (state) {
is (s_idle) {
when (io.nickel) { state := s_5 }
when (io.dime) { state := s_10 }
} is (s_5) {
when (io.nickel) { state := s_10 }
when (io.dime) { state := s_15 }
} is (s_10) {
when (io.nickel) { state := s_15 }
when (io.dime) { state := s_ok }
} is (s_15) {
when (io.nickel) { state := s_ok }
when (io.dime) { state := s_ok }
} is (s_ok) {
state := s_idle
}
}
io.rdy := (state === s_ok)
}


class TestbedTests(c: Testbed) extends Tester(c) {
}

object Testbed {
def main(args: Array[String]): Unit = {
val tutArgs = args.slice(1, args.length)
chiselMainTest(tutArgs, () => Module(new Testbed())) {
c => new TestbedTests(c) }
}
}

但我收到与 UFix 相关的错误:

[error] /home/chisel-tutorial/test/Testbed.scala:12: not found: value UFix
[error] val s_idle :: s_5 :: s_10 :: s_15 :: s_ok :: Nil = Enum(UFix(), 5)
[error] ^
[error] /home/chisel-tutorial/test/Testbed.scala:13: inferred type arguments [Any] do not conform to method apply's type parameter bounds [T <: Chisel.Data]
[error] val state = Reg(init = s_idle)
[error] ^
[error] /home/chisel-tutorial/test/Testbed.scala:16: overloaded method value apply with alternatives:
[error] (v: Iterable[Chisel.Bits])(block: => Unit)Unit <and>
[error] (v: Chisel.Bits,vr: Chisel.Bits*)(block: => Unit)Unit <and>
[error] (v: Chisel.Bits)(block: => Unit)Unit
[error] cannot be applied to (Any)
[error] is (s_idle) {
[error] ^
[error] three errors found
[error] (compile:compile) Compilation failed

本教程实际上将其写为带有大写字母 I 的 UFIx,但我尝试了两种方法均无济于事。我认为这只是一个旧类型,因此我用 UInt 替换了 UFix,但其他所有内容保持不变。然后我收到以下错误:

[error] /home/chisel-tutorial/test/Testbed.scala:19: value is is not a member of Unit
[error] possible cause: maybe a semicolon is missing before `value is'?
[error] } is (s_5) {
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed

注意到错误消息,我尝试通过在除第一个之外的每个“is”语句之前添加分号来解决错误:

package Testbed

import Chisel._


class Testbed extends Module {
val io = new Bundle {
val nickel = Bool(dir = INPUT)
val dime = Bool(dir = INPUT)
val rdy = Bool(dir = OUTPUT) }

val s_idle :: s_5 :: s_10 :: s_15 :: s_ok :: Nil = Enum(UInt(), 5)
val state = Reg(init = s_idle)

switch (state) {
is (s_idle) {
when (io.nickel) { state := s_5 }
when (io.dime) { state := s_10 }
}; is (s_5) {
when (io.nickel) { state := s_10 }
when (io.dime) { state := s_15 }
}; is (s_10) {
when (io.nickel) { state := s_15 }
when (io.dime) { state := s_ok }
}; is (s_15) {
when (io.nickel) { state := s_ok }
when (io.dime) { state := s_ok }
}; is (s_ok) {
state := s_idle
}
}
io.rdy := (state === s_ok)
}


class TestbedTests(c: Testbed) extends Tester(c) {
}

object Testbed {
def main(args: Array[String]): Unit = {
val tutArgs = args.slice(1, args.length)
chiselMainTest(tutArgs, () => Module(new Testbed())) {
c => new TestbedTests(c) }
}
}

最终代码成功生成verilog。然后我还尝试删除分号,但将上一个 switch 语句中的右大括号放在上面的行中,这也有效:

package Testbed

import Chisel._


class Testbed extends Module {
val io = new Bundle {
val nickel = Bool(dir = INPUT)
val dime = Bool(dir = INPUT)
val rdy = Bool(dir = OUTPUT) }

val s_idle :: s_5 :: s_10 :: s_15 :: s_ok :: Nil = Enum(UInt(), 5)
val state = Reg(init = s_idle)

switch (state) {
is (s_idle) {
when (io.nickel) { state := s_5 }
when (io.dime) { state := s_10 }}
is (s_5) {
when (io.nickel) { state := s_10 }
when (io.dime) { state := s_15 }}
is (s_10) {
when (io.nickel) { state := s_15 }
when (io.dime) { state := s_ok }}
is (s_15) {
when (io.nickel) { state := s_ok }
when (io.dime) { state := s_ok }}
is (s_ok) {
state := s_idle
}
}
io.rdy := (state === s_ok)
}


class TestbedTests(c: Testbed) extends Tester(c) {
}

object Testbed {
def main(args: Array[String]): Unit = {
val tutArgs = args.slice(1, args.length)
chiselMainTest(tutArgs, () => Module(new Testbed())) {
c => new TestbedTests(c) }
}
}

我现在关心的是凿子教程中提供的 switch 语句版本是否适用于其他人,如果有效,有谁知道为什么我必须小心地以非常特殊的方式格式化我的 switch 语句为了让他们正常工作?如果是这种情况,我该如何解决?

最佳答案

原因与scala语法有关。重要的是要记住,您同时使用 scala 和 Chisel 进行编码。您的“is”错误类似于以下 scala 语法:

hashmap getOrElse (foo, bar)

“is”被定义为 https://github.com/ucbbar/chisel/blob/master/src/main/scala/when.scala 中的对象

本质上 scala 将其解释为:

is().is

它不存在,所以它认为你打算将它定义为一个 val 并且搞砸了

关于scala - Chisel switch 语句似乎无法按照官方教程中概述的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com