@@ -26,6 +26,9 @@ class WaveDumper {
26
26
/// The file to write dumped output waveform to.
27
27
final File _outputFile;
28
28
29
+ /// A sink to write contents into [_outputFile] .
30
+ late final IOSink _outFileSink;
31
+
29
32
/// A counter for tracking signal names in the VCD file.
30
33
int _signalMarkerIdx = 0 ;
31
34
@@ -51,6 +54,8 @@ class WaveDumper {
51
54
'Module must be built before passed to dumper. Call build() first.' );
52
55
}
53
56
57
+ _outFileSink = _outputFile.openWrite ();
58
+
54
59
_collectAllSignals ();
55
60
56
61
_writeHeader ();
@@ -66,11 +71,24 @@ class WaveDumper {
66
71
}
67
72
});
68
73
69
- Simulator .simulationEnded. then ((args) {
74
+ Simulator .registerEndOfSimulationAction (() async {
70
75
_captureTimestamp (Simulator .time);
76
+
77
+ await _terminate ();
71
78
});
72
79
}
73
80
81
+ /// Writes [contents] to the output file.
82
+ void _writeToFile (String contents) {
83
+ _outFileSink.write (contents);
84
+ }
85
+
86
+ /// Terminates the waveform dumping, including closing the file.
87
+ Future <void > _terminate () async {
88
+ await _outFileSink.flush ();
89
+ await _outFileSink.close ();
90
+ }
91
+
74
92
/// Registers all signal value changes to write updates to the dumped VCD.
75
93
void _collectAllSignals () {
76
94
var modulesToParse = < Module > [module];
@@ -113,19 +131,19 @@ class WaveDumper {
113
131
\$ end
114
132
\$ timescale $timescale \$ end
115
133
''' ;
116
- _outputFile. writeAsStringSync (header);
134
+ _writeToFile (header);
117
135
}
118
136
119
137
/// Writes the scope of the VCD, including signal and hierarchy declarations, as well as initial values.
120
138
void _writeScope () {
121
139
var scopeString = _computeScopeString (module);
122
140
scopeString += '\$ enddefinitions \$ end\n ' ;
123
141
scopeString += '\$ dumpvars\n ' ;
124
- _outputFile. writeAsStringSync (scopeString, mode : FileMode .append );
142
+ _writeToFile (scopeString);
125
143
for (var element in _signalToMarkerMap.keys) {
126
144
_writeSignalValueUpdate (element);
127
145
}
128
- _outputFile. writeAsStringSync ('\$ end\n ' , mode : FileMode .append );
146
+ _writeToFile ('\$ end\n ' );
129
147
}
130
148
131
149
/// Generates the top of the scope string (signal and hierarchy definitions).
@@ -160,7 +178,7 @@ class WaveDumper {
160
178
/// Writes the current timestamp to the VCD.
161
179
void _captureTimestamp (int timestamp) {
162
180
var timestampString = '#$timestamp \n ' ;
163
- _outputFile. writeAsStringSync (timestampString, mode : FileMode .append );
181
+ _writeToFile (timestampString);
164
182
165
183
for (var signal in _changedLogicsThisTimestamp) {
166
184
_writeSignalValueUpdate (signal);
@@ -180,7 +198,7 @@ class WaveDumper {
180
198
: signal.value.toString (includeWidth: false );
181
199
var marker = _signalToMarkerMap[signal];
182
200
var updateString = '$updateValue $marker \n ' ;
183
- _outputFile. writeAsStringSync (updateString, mode : FileMode .append );
201
+ _writeToFile (updateString);
184
202
}
185
203
}
186
204
0 commit comments