简体   繁体   English

Chisel模块中的条件端口

[英]Conditional port in a Chisel Module

I have a selectable feature which is not normally required. 我有一个可选择的功能,通常不需要。 However to support this feature, some I/O ports should be added to the origin Module I/O port. 但是,要支持此功能,应将一些I / O端口添加到原始模块I / O端口。

I am doing it in this way: 我是这样做的:

import Chisel._

class TestModule extends Module {

  class IOBundle extends Bundle {
    val i = Bool(INPUT)
    val o = Bool(OUTPUT)
  }

  class IOBundle_EXT extends IOBundle {
    val o_ext = Bool(OUTPUT)
  }

  val io = if(true) new IOBundle_EXT else new IOBundle;

  io.o := io.i
  io.o_ext := io.i

}

After running sbt "run TestModule --backend c --compile --test --genHarness", the compiler complains: 运行sbt“运行TestModule --backend c --compile --test --genHarness”后,编译器会抱怨:

[error] xxxx/test/condi_port.scala:17: value o_ext is not a member of TestModule.this.IOBundle
[error]   io.o_ext := io.i
[error]      ^
[error] one error found
[error] (compile:compile) Compilation failed

So the if statement has no effect. 所以if语句没有效果。 val io is still assigned to IOBundle, rather than the extended IOBoundle_EXT, which makes no sense to me. val io仍然分配给IOBundle,而不是扩展的IOBoundle_EXT,这对我来说毫无意义。

Chisel now supports Options in IO bundles. Chisel现在支持IO包中的选项。

As an example, I explored Options here ( https://github.com/ucb-bar/riscv-boom/commit/da6edcb4b7bec341e31a55567ee04c8a1431d659 ), but here's a summary: 作为一个例子,我在这里探讨了选项( https://github.com/ucb-bar/riscv-boom/commit/da6edcb4b7bec341e31a55567ee04c8a1431d659 ),但这里是一个总结:

class MyBundle extends Bundle
{
   val my_ext = if (SOME_SWITCH) Some(ExtBundle) else None
}

...

io.my_ext match
{
   case Some(b: ExtBundle) =>
      my_ext.a := Bool(false)
      ...
   case _ => require (!SOME_SWITCH)
}

It's incredibly verbose, but I was able to get it working even when doing bulk connects and hiding bundles within bundles, etc. 它令人难以置信的冗长,但即使在批量连接和捆绑包中捆绑等时,我也能够使它工作。

Even though the compiler could determine that only one result is possible (the expression is always true), the type system sets the type of the result equal to the greatest common subtype of the two possible (true or false) sub-expressions. 即使编译器可以确定只有一个结果是可能的(表达式总是为真),类型系统将结果的类型设置为等于两个可能(真或假)子表达式的最大公共子类型。

You can verify this trivially with the following: 您可以通过以下方式轻松验证:

scala> val result = if (true) 1 else "one"
result: Any = 1

Try this: 试试这个:

import Chisel._

class TestModule(val useEXT : Boolean) extends Module {

  class IOBundle extends Bundle {
    val i = Bool(INPUT)
    val o = Bool(OUTPUT)
  }

  class IOBundle_EXT extends IOBundle {
    val o_ext = Bool(OUTPUT)
  }

  val io = { 
    if(useEXT) { 
      val res = new IOBundle_EXT; res.o_ext := res.i; res 
    } else {
      new IOBundle }};

  io.o := io.i

}

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

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