HAL  v4.5.0-299-g9ee015483
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
netlist_simulator_controller.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
29 #include "hal_core/netlist/gate.h"
31 #include "hal_core/netlist/net.h"
33 
34 #include <QMap>
35 #include <QObject>
36 #include <QString>
37 #include <QTemporaryDir>
38 #include <QDir>
39 #include <functional>
40 #include <map>
41 #include <memory>
42 #include <sstream>
43 #include <vector>
44 
48 
49 class QTemporaryDir;
50 
51 namespace hal {
52 /* forward declaration */
53 class Netlist;
54 
62  Q_OBJECT
63 
64 public:
71  EngineFailed };
72 
77  NoFilter
78  };
79 
84  {
85  std::vector<const Net*> nets;
86  std::string name;
87  bool is_clock;
88  };
89 
90  NetlistSimulatorController(u32 id, const std::string nam, const std::string& workdir, QObject* parent = nullptr);
91 
92  NetlistSimulatorController(u32 id, Netlist* nl, const std::string& filename, QObject* parent = nullptr);
93 
95 
102  SimulationEngine* create_simulation_engine(const std::string& name);
103 
108  SimulationEngine* get_simulation_engine() const;
109 
118  void add_clock_frequency(const Net* clock_net, u64 frequency, bool start_at_zero = true);
119 
129  void add_clock_period(const Net* clock_net, u64 period, bool start_at_zero = true, u64 duration=0);
130 
134  void set_no_clock_used();
135 
140  bool is_no_clock_used() const;
141 
148  u32 add_waveform_group(const std::string& name, const std::vector<Net*>& nets);
149 
156  u32 add_waveform_group(const PinGroup<ModulePin>* pin_group);
157 
165  u32 add_waveform_group(const Gate* gate, const PinGroup<GatePin>* pin_group);
166 
174  u32 add_waveform_group(const std::string& name, const PinGroup<ModulePin>* pin_group);
175 
182  u32 add_waveform_group(const SimulationInput::NetGroup& net_group);
183 
189  u32 add_boolean_expression_waveform(const std::string& expression);
190 
197  u32 add_boolean_accept_list_waveform(const std::vector<WaveData*>& input_waves, const std::vector<int>& accepted_combination);
198 
205  u32 add_trigger_time(const std::vector<WaveData*>& trigger_waves, const std::vector<int>& trigger_on_values = std::vector<int>());
206 
211  void remove_waveform_group(u32 group_id);
212 
219  void add_gates(const std::vector<Gate*>& gates);
220 
227  void compute_waveform_groups();
228 
234  const std::vector<SimulationInput::NetGroup>& get_net_groups() const;
235 
241  void load_waveform_groups(bool inputs);
242 
249  void set_input(const Net* net, BooleanFunction::Value value);
250 
257  void set_input(WaveData* wd, BooleanFunction::Value value);
258 
265  void set_input(const std::vector<Net*>& nets, const std::vector<BooleanFunction::Value>& values);
266 
273  void set_input(const WaveDataGroup* wdg, const std::vector<BooleanFunction::Value>& values);
274 
281  void set_input(const u32 id, const std::vector<BooleanFunction::Value>& values);
282 
289  void set_input(const PinGroup<ModulePin>* pin_group, const std::vector<BooleanFunction::Value>& values);
290 
298  void initialize();
299 
306  void simulate(u64 picoseconds);
307 
312  void reset();
313 
317  const std::unordered_set<const Gate*>& get_gates() const;
318 
322  const std::unordered_set<const Net*>& get_input_nets() const;
323 
327  std::vector<InputColumnHeader> get_input_column_headers() const;
328 
332  const std::vector<const Net*>& get_output_nets() const;
333 
337  const std::vector<const Net*>& get_partial_netlist_nets() const;
338 
343  std::vector<std::string> get_engine_names() const;
344 
345  // SimulationInput* input() const { return mSimulationInput; }
346 
351  QString name() const { return mName; }
352 
357  std::string get_name() const { return mName.toStdString(); }
358 
363  u32 get_id() const { return mId; }
364 
369  bool get_results();
370 
375  u64 get_max_simulated_time() const;
376 
381  bool run_simulation();
382 
388  bool import_vcd(const std::string& filename, FilterInputFlag filter);
389 
396  void import_csv(const std::string& filename, FilterInputFlag filter, u64 timescale = 1000000000);
397 
404  void import_saleae(const std::string& dirname, std::unordered_map<Net*,int> lookupTable, u64 timescale = 1000000000);
405 
412  void import_simulation(const std::string& dirname, FilterInputFlag filter, u64 timescale = 1000000000);
413 
418  void set_saleae_timescale(u64 timescale = 1000000000);
419 
429  bool generate_vcd(const std::filesystem::path& path, u32 start_time=0, u32 end_time=0, std::set<const Net*> nets = {}) const;
430 
435  std::string get_working_directory() const;
436 
441  bool is_legal_directory_name() const;
442 
447  std::filesystem::path get_saleae_directory_filename() const;
448 
453  WaveDataList* get_waves() const { return mWaveDataList; }
454 
459  SimulationState get_state() const { return mState; }
460 
466  WaveData* get_waveform_by_net(const Net* n) const;
467 
473  void rename_waveform(WaveData* wd, std::string name);
474 
480  WaveDataGroup* get_waveform_group_by_id(u32 id) const;
481 
487  WaveDataBoolean* get_waveform_boolean_by_id(u32 id) const;
488 
494  WaveDataTrigger* get_trigger_time_by_id(u32 id) const;
495 
501  void set_timeframe(u64 tmin=0, u64 tmax=0);
502 
507  void emitLoadProgress(int percent);
508 
513  bool can_import_data() const;
514 
519  void simulate_only_probes(const std::vector<const Net*>& probes);
520 
525  void simulate_only_probes(const QSet<u32>& probes);
526 
531  bool persist() const;
532 
537  void setLogReceiver(SimulationLogReceiver* logrec);
538 
539  static const char* sPersistFile;
540 
541 public Q_SLOTS:
542  void handleOpenInputFile(const QString& filename);
543  void handleSelectGates();
544  void handleRunFinished(bool success);
545 
546 Q_SIGNALS:
548  void engineFinished(bool success);
550  void loadProgress(int percent);
551 
552 private:
553  std::vector<const Net*> getFilterNets(FilterInputFlag filter) const;
554  void initSimulator();
555  void setState(SimulationState stat);
556  bool getResultsInternal();
557 
558  bool isInputSet() const;
559  void checkReadyState();
560  void restoreComposed(const SaleaeDirectory& sd);
561  void loadStoredController(const QDir& workDir);
562 
563  u32 mId;
564  QString mName;
565 
566  SimulationState mState;
567  SimulationEngine* mSimulationEngine;
568 
569  QTemporaryDir* mTempDir;
570  QString mWorkDir;
571  WaveDataList* mWaveDataList;
572 
573  SimulationInput* mSimulationInput;
574  QSet<u32> mSimulateOnlyProbes;
575 
576  QHash<u32,int> mBadAssignInputWarnings;
577  SimulationLogReceiver* mLogReceiver;
578 };
579 
584  Q_OBJECT
585 
588  static NetlistSimulatorControllerMap* sInst;
589 
590 Q_SIGNALS:
593 
594 public:
597  void removeController(u32 id);
598  void shutdown() { mMap.clear(); }
599  void clearAll();
600  QList<NetlistSimulatorController*> toList() const { return mMap.values(); }
601  NetlistSimulatorController* controller(u32 id) const { return mMap.value(id); }
602 };
603 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Value
represents the type of the node
Definition: gate.h:59
Definition: net.h:58
void engineFinished(bool success)
void stateChanged(hal::NetlistSimulatorController::SimulationState state)
void loadProgress(int percent)
NetlistSimulatorController * controller(u32 id) const
static NetlistSimulatorControllerMap * instance()
void addController(NetlistSimulatorController *ctrl)
QList< NetlistSimulatorController * > toList() const
The SaleaeDirectory class provides the API for the SALEAE directory file. The SALEAE directory file w...
uint64_t u64
Definition: defines.h:42
uint32_t u32
Definition: defines.h:41
std::vector< Net * > get_input_nets(const std::vector< Gate * > &gates)
Get input nets from a list of gates.
Definition: utils.cpp:25
std::vector< Net * > get_output_nets(const std::vector< Gate * > &gates, bool only_external_destinations=true)
Get output nets from a list of gates.
Definition: utils.cpp:61
Definition: defines.h:45
void initialize()
Definition: event_log.cpp:302
Net * net
std::string name
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS