scala - 使用泛型类型关闭数据路径中的逻辑(Chisel)

标签 scala generic-programming hdl riscv chisel

我正在开发 Z 规模 RISCV 处理器,我已在数据路径中实现了新的功能和逻辑。我想知道是否存在一种简单的方法来“关闭”代码的某些部分而不使用一堆 if 循环?我希望能够轻松地在 Z 尺度处理器的常规实现和扩展实现之间进行切换。

我实现的新逻辑并没有取代数据路径的主要组件,而是扩展了功能。

最佳答案

这个问题确实触及了 Chisel 强大的核心。作为嵌入 Scala 中的 DSL,您可以使用面向对象和函数式编程语言的全部功能。

虽然我不确定你到底在做什么,但这似乎是使用继承的好地方。您可以创建一个新的模块类来扩展数据路径模块并添加附加功能。

一个玩具示例:

import Chisel._

class ParentIO extends Bundle {
  val foo = UInt(width = 32).asInput
  val bar = UInt(width = 32).asOutput
}

class Parent extends Module {
  // Note the use of lazy val
  // Due to Scala initialization order (http://docs.scala-lang.org/tutorials/FAQ/initialization-order.html)
  //   this cannot be a val
  // Due to quirks of Chisel, this cannot be a def
  lazy val io = new ParentIO
  io.bar := io.foo
}

class ChildIO extends ParentIO {
  val isOdd = Bool().asOutput
}

class Child extends Parent {
  override lazy val io = new ChildIO
  io.isOdd := io.foo(0)
}

// Note use of call-by-name for passing gen
// Chisel Modules must be constructed within a call to the Module(...) function
class Top(gen: => Parent) extends Module {
  val dut = Module(gen)
  val io = dut.io.cloneType
  io <> dut.io
}

Module Top 由它实例化的模块类型参数化:父模块或子模块。因此,您可以有条件地实例化 Parent 或 Child,而不是区分它们的所有逻辑。如果您希望子级覆盖父级的某些功能,Chisel 的最后连接语义允许子级中的任何连接覆盖父级中的连接。

关于scala - 使用泛型类型关闭数据路径中的逻辑(Chisel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40650510/

相关文章:

java - Scala 不会在循环内将 Element 转换为产生值

C _Generic 错误 - '_Bool' 之前的预期表达式

vhdl - 模拟与硬件不匹配

amazon-web-services - 从 Swagger 错误导入 Amazon API Gateway - 不采用泛型

c++ - 使用一个参数初始化 boost::hana::tuple

verilog - Verilog HDL 中的“始终”与“永远”

syntax-error - 我如何查看为什么在vivado的 “syntax error files”中列出了文件

scala - 有没有办法验证原始spark sql查询的语法?

Scala将递归有界类型参数(F-bounded)转换为类型成员

scala 泛型函数 `not found: type ?`