这是来自https://github.com/freechipsproject/chisel3/wiki/Frequently-Asked-Questions的 HelloWorld.scala 示例的略微修改版本
// say hello
package HelloWorld
import chisel3._
class HelloWorld extends Module {
val io = IO(new Bundle{
val halt = Output(Bool())
val may_halt = Input(Bool())
})
printf("hello, world!\n");
when (io.may_halt) {
io.halt := true.B
} .otherwise {
io.halt := false.B
}
}
// code for building HelloWorld
object HelloWorld extends App {
chisel3.Driver.execute(args, () => new HelloWorld)
}
我使用 chisel3 构建它,然后使用 verilator 生成 C++。这是 C++ 工具中有趣的部分:
VHelloWorld *top; // Instantiation of module
int main(int argc, char** argv) {
Verilated::commandArgs(argc, argv); // Remember args
top = new VHelloWorld; // Create instance
printf("eval loop start\n");
long long cycle = 0;
for (; !Verilated::gotFinish(); ++cycle) {
printf("\tcycle: %lld\n", cycle);
if (2 <= cycle) {
printf("\t\tput io_may_halt = 1\n");
top->io_may_halt = 1;
}
top->eval(); // Evaluate model
if (top->io_halt) {
printf("\t\tgot an io_halt, so halting\n");
break; // halt when we get the signal to do so
}
}
printf("eval loop stop\n");
top->final(); // Done simulating
delete top; // (Though this example doesn't get here)
return 0;
}
我运行了几个周期,然后发出信号停止。然而,“你好,世界!” 消息永远不会出来。
HelloWorld.cppdir/HelloWorld.exe
eval loop start
cycle: 0
cycle: 1
cycle: 2
put io_may_halt = 1
got an io_halt, so halting
eval loop stop