HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
saleae_writer.cpp
Go to the documentation of this file.
4 #include <fstream>
5 #include <sstream>
6 #include <QDebug>
7 #include <sys/resource.h>
8 #include <iostream>
9 
10 namespace hal
11 {
12  SaleaeWriter::SaleaeWriter(const std::string& filename)
13  : mSaleaeDirectory(filename,true)
14  {
15  std::filesystem::path csvpath(filename);
16  mDir = csvpath.parent_path();
17  std::filesystem::create_directory(mDir);
18  }
19 
21  {
22  SaleaeDirectoryStoreRequest save(&mSaleaeDirectory);
23  std::unordered_map<int,SaleaeDirectoryFileIndex> fileIndexes;
24  for (auto it = mDataFiles.begin(); it != mDataFiles.end(); ++it)
25  {
26  SaleaeOutputFile* sof = it->second;
27  fileIndexes.insert(std::make_pair(it->first,sof->fileIndex()));
28  sof->close();
29  delete sof;
30  }
31  mSaleaeDirectory.update_file_indexes(fileIndexes);
32  }
33 
34  void SaleaeWriter::add_directory_entry(int inx, const std::string &name, uint32_t id)
35  {
38  mSaleaeDirectory.add_or_replace_net(sdne);
39  }
40 
42  {
43  std::filesystem::path path;
44  SaleaeOutputFile* sof = nullptr;
45  bool updateDirectory = false;
46  int fileIndex = -1;
47  if ( (fileIndex = mSaleaeDirectory.get_datafile_index(name,id)) >= 0)
48  {
49  // replace existing
50  path = mSaleaeDirectory.get_datafile_path(name, id);
51  auto it = mDataFiles.find(fileIndex);
52  if (it != mDataFiles.end())
53  {
54  it->second->close();
55  delete it->second;
56  mDataFiles.erase(it);
57  }
58  }
59  else
60  {
61  // add new waveform, might need to increase ulimit
62  struct rlimit rlim;
63  getrlimit(RLIMIT_NOFILE, &rlim);
64 
65  fileIndex = mSaleaeDirectory.get_next_available_index();
66  unsigned int required = fileIndex + 256;
67  if (rlim.rlim_max < required) return nullptr;
68  if (rlim.rlim_cur < required)
69  {
70  rlim.rlim_cur = required;
71  setrlimit(RLIMIT_NOFILE, &rlim);
72  }
73 
74  std::ostringstream fname;
75  fname << "digital_" << fileIndex << ".bin";
76  path = mDir / fname.str();
77 
78  updateDirectory = true;
79  }
80  sof = new SaleaeOutputFile(path.string(), fileIndex);
81  if (!sof->good())
82  {
83  delete sof;
84  return nullptr;
85  }
86 
87  mDataFiles.insert(std::make_pair(fileIndex,sof));
88 
89  if (updateDirectory)
90  // create directory entry
91  add_directory_entry(fileIndex,name,id);
92  return sof;
93  }
94 
95 
96 }
The SaleaeDirectoryFileIndex class represents a single SALEAE data file. The class comprises the inde...
int get_datafile_index(const std::string &nam, uint32_t id) const
Get waveform datafile index for net identified by name and id.
void add_or_replace_net(SaleaeDirectoryNetEntry &sdne)
Add net entry if not yet existing, replace existing entry otherwise.
int get_next_available_index() const
Getter for next available file index ('digital_XXX.bin' with lowest number XXX not in use)
void update_file_indexes(std::unordered_map< int, SaleaeDirectoryFileIndex > &fileIndexes)
Update file index information (like number of transitions, end time ...) after writing SALEAE files.
std::string get_datafile_path(int index) const
Get full path to waveform data file.
The SaleaeDirectoryNetEntry class represents a regular waveform for a single simulated net....
void addIndex(const SaleaeDirectoryFileIndex &sdfe)
Add index for binary file to net entry instance.
The SaleaeDirectoryStoreRequest class is useful to bundle requests for updating SALEAE directory....
void close()
Update header on disk and close file.
SaleaeDirectoryFileIndex fileIndex() const
File index summary for saleae directory.
SaleaeWriter(const std::string &filename)
void add_directory_entry(int inx, const std::string &name, uint32_t id)
Adds directory entry for net/waveform with binary file index inx.
~SaleaeWriter()
Destructor closes all open binary files updating their file header. It also updates SALEAE directory.
SaleaeOutputFile * add_or_replace_waveform(const std::string &name, uint32_t id)
Create new empty output file. Will delete existing binary file if directory links already a data file...
bool save(std::filesystem::path file_path, GateLibrary *gate_lib, bool overwrite=false)
Definition: defines.h:45
std::string name