HAL  v4.5.0-83-g30c8f0afc
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 
181  u32 add_boolean_expression_waveform(const std::string& expression);
182 
189  u32 add_boolean_accept_list_waveform(const std::vector<WaveData*>& input_waves, const std::vector<int>& accepted_combination);
190 
197  u32 add_trigger_time(const std::vector<WaveData*>& trigger_waves, const std::vector<int>& trigger_on_values = std::vector<int>());
198 
203  void remove_waveform_group(u32 group_id);
204 
211  void add_gates(const std::vector<Gate*>& gates);
212 
219  void compute_waveform_groups();
220 
226  void load_waveform_groups(bool inputs);
227 
234  void set_input(const Net* net, BooleanFunction::Value value);
235 
242  void set_input(WaveData* wd, BooleanFunction::Value value);
243 
250  void set_input(const std::vector<Net*>& nets, const std::vector<BooleanFunction::Value>& values);
251 
258  void set_input(const WaveDataGroup* wdg, const std::vector<BooleanFunction::Value>& values);
259 
266  void set_input(const u32 id, const std::vector<BooleanFunction::Value>& values);
267 
274  void set_input(const PinGroup<ModulePin>* pin_group, const std::vector<BooleanFunction::Value>& values);
275 
280  void initialize();
281 
289  void simulate(u64 picoseconds);
290 
295  void reset();
296 
300  const std::unordered_set<const Gate*>& get_gates() const;
301 
305  const std::unordered_set<const Net*>& get_input_nets() const;
306 
310  std::vector<InputColumnHeader> get_input_column_headers() const;
311 
315  const std::vector<const Net*>& get_output_nets() const;
316 
320  const std::vector<const Net*>& get_partial_netlist_nets() const;
321 
326  std::vector<std::string> get_engine_names() const;
327 
328  // SimulationInput* input() const { return mSimulationInput; }
329 
334  QString name() const { return mName; }
335 
340  std::string get_name() const { return mName.toStdString(); }
341 
346  u32 get_id() const { return mId; }
347 
352  bool get_results();
353 
358  u64 get_max_simulated_time() const;
359 
364  bool run_simulation();
365 
371  bool import_vcd(const std::string& filename, FilterInputFlag filter);
372 
379  void import_csv(const std::string& filename, FilterInputFlag filter, u64 timescale = 1000000000);
380 
387  void import_saleae(const std::string& dirname, std::unordered_map<Net*,int> lookupTable, u64 timescale = 1000000000);
388 
395  void import_simulation(const std::string& dirname, FilterInputFlag filter, u64 timescale = 1000000000);
396 
401  void set_saleae_timescale(u64 timescale = 1000000000);
402 
412  bool generate_vcd(const std::filesystem::path& path, u32 start_time=0, u32 end_time=0, std::set<const Net*> nets = {}) const;
413 
418  std::string get_working_directory() const;
419 
424  bool is_legal_directory_name() const;
425 
430  std::filesystem::path get_saleae_directory_filename() const;
431 
436  WaveDataList* get_waves() const { return mWaveDataList; }
437 
442  SimulationState get_state() const { return mState; }
443 
449  WaveData* get_waveform_by_net(const Net* n) const;
450 
456  void rename_waveform(WaveData* wd, std::string name);
457 
463  WaveDataGroup* get_waveform_group_by_id(u32 id) const;
464 
470  WaveDataBoolean* get_waveform_boolean_by_id(u32 id) const;
471 
477  WaveDataTrigger* get_trigger_time_by_id(u32 id) const;
478 
484  void set_timeframe(u64 tmin=0, u64 tmax=0);
485 
490  void emitLoadProgress(int percent);
491 
496  bool can_import_data() const;
497 
502  void simulate_only_probes(const std::vector<const Net*>& probes);
503 
508  void simulate_only_probes(const QSet<u32>& probes);
509 
514  bool persist() const;
515 
520  void setLogReceiver(SimulationLogReceiver* logrec);
521 
522  static const char* sPersistFile;
523 
524 public Q_SLOTS:
525  void handleOpenInputFile(const QString& filename);
526  void handleSelectGates();
527  void handleRunFinished(bool success);
528 
529 Q_SIGNALS:
531  void engineFinished(bool success);
533  void loadProgress(int percent);
534 
535 private:
536  std::vector<const Net*> getFilterNets(FilterInputFlag filter) const;
537  void initSimulator();
538  void setState(SimulationState stat);
539  bool getResultsInternal();
540 
541  bool isInputSet() const;
542  void checkReadyState();
543  void restoreComposed(const SaleaeDirectory& sd);
544  void loadStoredController(const QDir& workDir);
545 
546  u32 mId;
547  QString mName;
548 
549  SimulationState mState;
550  SimulationEngine* mSimulationEngine;
551 
552  QTemporaryDir* mTempDir;
553  QString mWorkDir;
554  WaveDataList* mWaveDataList;
555 
556  SimulationInput* mSimulationInput;
557  QSet<u32> mSimulateOnlyProbes;
558 
559  QHash<u32,int> mBadAssignInputWarnings;
560  SimulationLogReceiver* mLogReceiver;
561 };
562 
567  Q_OBJECT
568 
571  static NetlistSimulatorControllerMap* sInst;
572 
573 Q_SIGNALS:
576 
577 public:
580  void removeController(u32 id);
581  void shutdown() { mMap.clear(); }
582  void clearAll();
583  QList<NetlistSimulatorController*> toList() const { return mMap.values(); }
584  NetlistSimulatorController* controller(u32 id) const { return mMap.value(id); }
585 };
586 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Value
represents the type of the node
Definition: gate.h:58
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