5f74ecc4c17e75d5d26fb295cda1634dd203dd17
[litex.git] / litex / build / sim / core / veril.cpp
1 /* Copyright (C) 2017 LambdaConcept */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdint.h>
7 #include "Vsim.h"
8 #include "verilated.h"
9 #ifdef TRACE_FST
10 #include "verilated_fst_c.h"
11 #else
12 #include "verilated_vcd_c.h"
13 #endif
14
15 #ifdef TRACE_FST
16 VerilatedFstC* tfp;
17 #else
18 VerilatedVcdC* tfp;
19 #endif
20 uint64_t tfp_start;
21 uint64_t tfp_end;
22 uint64_t main_time = 0;
23
24 extern "C" void litex_sim_eval(void *vsim)
25 {
26 Vsim *sim = (Vsim*)vsim;
27 sim->eval();
28 }
29
30 extern "C" void litex_sim_increment_time(unsigned long dt_ps) {
31 main_time += dt_ps;
32 }
33
34 extern "C" void litex_sim_init_cmdargs(int argc, char *argv[])
35 {
36 Verilated::commandArgs(argc, argv);
37 }
38
39 extern "C" void litex_sim_init_tracer(void *vsim, long start, long end)
40 {
41 Vsim *sim = (Vsim*)vsim;
42 tfp_start = start;
43 tfp_end = end >= 0 ? end : UINT64_MAX;
44 Verilated::traceEverOn(true);
45 #ifdef TRACE_FST
46 tfp = new VerilatedFstC;
47 sim->trace(tfp, 99);
48 tfp->open("sim.fst");
49 #else
50 tfp = new VerilatedVcdC;
51 sim->trace(tfp, 99);
52 tfp->open("sim.vcd");
53 #endif
54 tfp->set_time_unit("1ps");
55 tfp->set_time_resolution("1ps");
56 }
57
58 extern "C" void litex_sim_tracer_dump()
59 {
60 if (tfp_start <= main_time && main_time <= tfp_end) {
61 tfp->dump(main_time);
62 }
63 }
64
65 extern "C" int litex_sim_got_finish()
66 {
67 return Verilated::gotFinish();
68 }
69
70 #if VM_COVERAGE
71 extern "C" void litex_sim_coverage_dump()
72 {
73 VerilatedCov::write("sim.cov");
74 }
75 #endif
76
77 double sc_time_stamp()
78 {
79 return main_time;
80 }