这是来自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发布于 2020-02-16 06:49:34
我弄明白了: printf()只在上升时钟发生,所以围绕在verilator代码周围的C++工具必须显式地(1)驱动时钟低,(2) eval(),(2)驱动时钟高,(3) eval(),然后打印printf()。(实际上,我不确定(2)是必需的,但不这样做会很奇怪。)
我在上面的评论中说过,verilator示例并没有显示C++工具如何做到这一点,但是在一个更复杂的示例和更复杂的方式中,它们做到了这一点。
https://stackoverflow.com/questions/60202900
复制相似问题