SILICON
Loading...
Searching...
No Matches
diagramSceneSimulationController.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#ifndef __EMSCRIPTEN__
21 #include <thread>
22#endif
23
24#include <atomic>
25#include <exception>
26#include <functional>
27#include <memory>
28#include <optional>
29#include <string>
30#include <vector>
31
32#include <QList>
33#include <QPair>
34#include <QStringList>
35
36#include <core/component.hpp>
37#include <core/siliconFst.hpp>
40
41class QProgressDialog;
42class QTimer;
43
44
45namespace SILICON::ui {
46using namespace SILICON::core;
47using namespace SILICON::simulation;
48using namespace SILICON::waveform;
49using namespace SILICON::waveform::fst;
50
51class DiagramScene;
52
63public:
69
74
85
89 void exitSimulationMode();
90
97 void handleInputToggled(Bus targetBus, const BusValue& value, Component_weakPtr source);
98
103 void setFstTraceFile(std::optional<std::string> fileName);
104
109
113 void clearWaveformTrace();
114
120 void simulateEditedWaveform(qulonglong duration,
121 std::vector<Sample> inputSnapshots);
122
126 [[nodiscard]] bool isFstTracingEnabled() const { return fstTraceFile.has_value(); }
127
128private:
131 std::vector<CircuitWriter::NamedBus> buses;
132 QList<int> widths;
133 int inputCount = 0;
134 };
135
140
144 [[nodiscard]] TraceConfiguration collectTraceConfiguration() const;
145
150 void startJob(std::function<Simulator::RunResult()> job);
151
155 void setupJobUI();
156
158 void finishJob();
159
161 void cancelAndWait();
162
164 [[nodiscard]] bool isJobFinished() const;
165
167 void setJobFinished(bool finished);
168
170 [[nodiscard]] bool isJobCancellationRequested() const;
171
174
177
179 void resetTrace(const TraceConfiguration& trace);
180
187 const std::optional<std::string>& traceFile);
188
191
193 std::unique_ptr<SILICON::simulation::Session> simulator;
194
196 std::optional<std::string> fstTraceFile;
197
198#ifndef __EMSCRIPTEN__
200 std::jthread worker;
201#endif
202
204 std::atomic_bool jobCancellationRequested = false;
205 std::atomic_bool jobFinished = true;
206
209
211 std::exception_ptr jobException;
212
214 QProgressDialog* progressDialog = nullptr;
215
217 QTimer* completionTimer = nullptr;
218
220 QTimer* dialogTimer = nullptr;
221
223 QList<QPair<qulonglong, std::vector<BusValue>>> pendingWaveformSnapshots;
224};
225
226} // namespace SILICON::ui
RunResult
Outcome of a cancellable simulator operation.
Definition simulator.hpp:92
void resetTrace(const TraceConfiguration &trace)
Rebuilds the waveform viewer's signal-name configuration.
void setupJobUI()
Configures progress dialog, timers, and locks the views before a job.
std::optional< std::string > fstTraceFile
Optional output file used for live FST tracing.
void cancelAndWait()
Requests cancellation and waits for the active job before teardown.
void setFstTraceFile(std::optional< std::string > fileName)
Enables or disables FST tracing for the active simulation session.
void clearWaveformTrace()
Clears waveform state after the scene has been fully emptied.
DiagramSceneSimulationController(DiagramScene &scene)
Constructs a simulation controller bound to a scene.
void refreshGraphicalOutputs()
Refreshes the visual state of all graphical outputs from bus values.
DiagramScene & scene
Host scene that owns the graphical items and Qt signals.
void exitSimulationMode()
Leaves interactive simulation mode and clears runtime state.
Simulator::RunResult jobResult
Result produced by the latest simulation operation.
QTimer * completionTimer
GUI timer that detects job completion without blocking the event loop.
void handleInputToggled(Bus targetBus, const BusValue &value, Component_weakPtr source)
Applies a user input toggle to the running simulator.
void setJobFinished(bool finished)
Publishes whether the active job has completed.
std::unique_ptr< SILICON::simulation::Session > simulator
Runtime simulation session active only during simulation mode.
void finishJob()
Finalizes a completed job on the GUI thread.
std::exception_ptr jobException
Exception captured during a job and reported on the GUI thread.
void refreshTraceConfiguration()
Refreshes waveform, simulator, and FST tracing from one scene snapshot.
bool isFstTracingEnabled() const
Returns whether FST tracing is currently enabled.
bool isJobCancellationRequested() const
Returns whether cancellation has been requested for the active job.
bool enterSimulationMode()
Enters interactive simulation mode for the scene.
void configureSimulatorTrace(const TraceConfiguration &trace, const std::optional< std::string > &traceFile)
Applies trace buses, snapshot collection, and optional FST output.
void startJob(std::function< Simulator::RunResult()> job)
Starts one cancellable simulation operation and its progress UI.
QTimer * dialogTimer
GUI timer that prevents the progress dialog flashing for short jobs.
std::atomic_bool jobCancellationRequested
Platform-agnostic thread-safe job state.
void resetJobCancellation()
Clears cancellation state before starting a job.
TraceConfiguration collectTraceConfiguration() const
Collects ordered trace buses and their input count.
QList< QPair< qulonglong, std::vector< BusValue > > > pendingWaveformSnapshots
Worker-produced snapshots delivered to the viewer as one GUI batch.
void simulateEditedWaveform(qulonglong duration, std::vector< Sample > inputSnapshots)
Runs the circuit from an edited input waveform and publishes output trace.
bool isJobFinished() const
Returns whether the active job has completed.
void requestJobCancellation()
Publishes a cancellation request for the active job.
QProgressDialog * progressDialog
Delayed cancellation dialog for long-running jobs.
~DiagramSceneSimulationController()
Destroys the controller and releases any simulator resources.
Graphics scene for the circuit diagram editor.
std::weak_ptr< Component > Component_weakPtr
Definition wire.hpp:52
std::vector< State > BusValue
Definition wire.hpp:120