Repository navigation
Expand file tree
/
Copy pathtestbench.cpp
More file actions
93 lines (81 loc) · 2.25 KB
/
Copy pathtestbench.cpp
File metadata and controls
93 lines (81 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "verilated.h"
#include "VArgonTB.h" // Generated by Verilator from Argon.sv
#include "verilated_fst_c.h" // For FST waveform tracing
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
VArgonTB* top;
VerilatedFstC* tfp; // Trace file pointer
vluint64_t sim_time = 0; // Manual simulation time counter
uint64_t clock_count = 0; // Track clock cycles
bool debug_mode = false; // Default to false
void simClockFall() {
top->i_clk = 0;
top->eval();
tfp->dump(sim_time);
sim_time++;
if (debug_mode) {
std::cout << "Clock fall, time: " << sim_time << "\n";
}
}
void simClockRise() {
top->i_clk = 1;
top->eval();
tfp->dump(sim_time);
sim_time++;
if (debug_mode) {
std::cout << "Clock rise, time: " << sim_time << "\n";
}
}
void simClock(int cycles = 1) {
for (int i = 0; i < cycles; i++) {
simClockRise();
simClockFall();
clock_count++;
}
}
void simreset() {
top->i_reset = 1;
simClock();
top->i_reset = 0;
std::cout << "Reset complete\n";
}
// Helper function to format 32-bit value as binary with underscores
std::string to_binary(uint32_t value) {
std::string bin;
for (int i = 31; i >= 0; i--) {
bin += (value & (1U << i)) ? "1" : "0";
if (i % 4 == 0 && i > 0) bin += "_";
}
return bin;
}
int main(int argc, char** argv) {
Verilated::commandArgs(argc, argv);
top = new VArgonTB; // Instantiate the top module
// Initialize FST tracing
Verilated::traceEverOn(true);
tfp = new VerilatedFstC;
top->trace(tfp, 99); // Trace 99 levels of hierarchy
tfp->open("dump.fst"); // Root directory
if (!tfp->isOpen()) {
std::cerr << "Failed to open dump.fst for writing\n";
delete top;
return 1;
}
std::cout << "Tracing initialized, writing to dump.fst\n";
std::cout << "\n=== Simulation Begin ===\n";
// simreset();
// Simulation
simClock(1); // initial instruction load
for (int j = 0; j < 20; j++) {
simClock(5); // instruction
}
// Cleanup
std::cout << "Simulation complete, clock cycles: " << clock_count << "\n";\
std::cout << top->debug_reg << "\n";
tfp->close();
delete tfp;
delete top;
return 0;
}