-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgate_library_manager.cpp
232 lines (200 loc) · 9.1 KB
/
gate_library_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "hal_core/netlist/gate_library/gate_library_manager.h"
#include "hal_core/netlist/gate_library/gate_library.h"
#include "hal_core/netlist/gate_library/gate_library_parser/gate_library_parser_manager.h"
#include "hal_core/netlist/gate_library/gate_library_writer/gate_library_writer_manager.h"
#include "hal_core/utilities/log.h"
#include "hal_core/utilities/utils.h"
#include <iostream>
namespace hal
{
namespace gate_library_manager
{
namespace
{
std::map<std::filesystem::path, std::unique_ptr<GateLibrary>> m_gate_libraries;
Result<std::monostate> prepare_library(const std::unique_ptr<GateLibrary>& lib)
{
auto gate_types = lib->get_gate_types();
for (const auto& [gt_name, gt] : gate_types)
{
if (gt->has_property(GateTypeProperty::power))
{
lib->mark_vcc_gate_type(gt);
}
else if (gt->has_property(GateTypeProperty::ground))
{
lib->mark_gnd_gate_type(gt);
}
}
if (lib->get_gnd_gate_types().empty())
{
std::string name = "HAL_GND";
if (gate_types.find(name) != gate_types.end())
{
return ERR("could not prepare gate library '" + lib->get_name() + "': no GND gate type found within gate library, but gate type 'HAL_GND' already exists");
}
GateType* gt = lib->create_gate_type(name, {GateTypeProperty::combinational, GateTypeProperty::ground});
if (auto res = gt->create_pin("O", PinDirection::output, PinType::ground); res.is_error())
{
return ERR_APPEND(res.get_error(), "could not prepare gate library '" + lib->get_name() + "': failed to create output pin 'O' for gate type 'HAL_GND'");
}
gt->add_boolean_function("O", BooleanFunction::Const(BooleanFunction::Value::ZERO));
lib->mark_gnd_gate_type(gt);
log_info("gate_library_manager", "gate library did not contain a GND gate, auto-generated type '{}'.", name);
}
if (lib->get_vcc_gate_types().empty())
{
std::string name = "HAL_VDD";
if (gate_types.find(name) != gate_types.end())
{
return ERR("could not prepare gate library '" + lib->get_name() + "': no VDD gate type found within gate library, but gate type 'HAL_VDD' already exists");
}
GateType* gt = lib->create_gate_type(name, {GateTypeProperty::combinational, GateTypeProperty::power});
if (auto res = gt->create_pin("O", PinDirection::output, PinType::power); res.is_error())
{
return ERR_APPEND(res.get_error(), "could not prepare gate library '" + lib->get_name() + "': failed to create output pin 'O' for gate type 'HAL_VDD'");
}
gt->add_boolean_function("O", BooleanFunction::Const(BooleanFunction::Value::ONE));
lib->mark_vcc_gate_type(gt);
log_info("gate_library_manager", "gate library did not contain a VDD gate, auto-generated type '{}'.", name);
}
return OK({});
}
} // namespace
GateLibrary* load(std::filesystem::path file_path, bool reload)
{
if (!std::filesystem::exists(file_path))
{
log_error("gate_library_manager", "gate library file '{}' does not exist.", file_path.string());
return nullptr;
}
if (!file_path.is_absolute())
{
file_path = std::filesystem::absolute(file_path);
}
if (!reload)
{
if (auto it = m_gate_libraries.find(file_path); it != m_gate_libraries.end())
{
log_info("gate_library_parser", "the gate library file '{}' is already loaded.", file_path.string());
return it->second.get();
}
}
std::unique_ptr<GateLibrary> gate_lib = gate_library_parser_manager::parse(file_path);
if (gate_lib == nullptr)
{
return nullptr;
}
if (auto res = prepare_library(gate_lib); res.is_error())
{
log_error("gate_library_parser", "error encountered while loading gate library:\n{}", res.get_error().get());
return nullptr;
}
GateLibrary* res = gate_lib.get();
m_gate_libraries[file_path.string()] = std::move(gate_lib);
return res;
}
void load_all(bool reload)
{
std::vector<std::filesystem::path> lib_dirs = utils::get_gate_library_directories();
for (const auto& lib_dir : lib_dirs)
{
if (!std::filesystem::exists(lib_dir))
{
continue;
}
log_info("gate_library_manager", "loading all gate library files from {}.", lib_dir.string());
for (const auto& lib_path : utils::RecursiveDirectoryRange(lib_dir))
{
load(lib_path.path(), reload);
}
}
}
std::vector<std::filesystem::path> get_all_path()
{
std::vector<std::filesystem::path> retval;
for (const auto& lib_dir : utils::get_gate_library_directories())
{
if (!std::filesystem::exists(lib_dir))
continue;
for (const auto& lib_path : utils::RecursiveDirectoryRange(lib_dir))
retval.push_back(lib_path.path());
}
return retval;
}
bool save(std::filesystem::path file_path, GateLibrary* gate_lib, bool overwrite)
{
if (std::filesystem::exists(file_path))
{
if (overwrite)
{
log_info("gate_library_manager", "gate library file '{}' already exists and will be overwritten.", file_path.string());
}
else
{
log_error("gate_library_manager", "gate library file '{}' already exists, aborting.", file_path.string());
return false;
}
}
if (!file_path.is_absolute())
{
file_path = std::filesystem::absolute(file_path);
}
return gate_library_writer_manager::write(gate_lib, file_path);
}
void remove(std::filesystem::path file_path)
{
m_gate_libraries.erase(file_path);
}
GateLibrary* get_gate_library(const std::string& file_path)
{
std::filesystem::path absolute_path;
if (std::filesystem::exists(file_path))
{
// if an existing file is queried, load it by its absolute path
absolute_path = std::filesystem::absolute(file_path);
}
else
{
// if a non existing file is queried, search for it in the standard directories
auto stripped_name = std::filesystem::path(file_path).filename();
log_info("gate_library_manager", "file '{}' does not exist, searching for '{}' in the default gate library directories...", file_path, stripped_name.string());
auto lib_path = utils::get_file(stripped_name, utils::get_gate_library_directories());
if (lib_path.empty())
{
log_info("gate_library_manager", "could not find gate library file '{}'.", stripped_name.string());
return nullptr;
}
absolute_path = std::filesystem::absolute(lib_path);
}
// absolute path to file is known, check if it is already loaded
if (auto it = m_gate_libraries.find(absolute_path.string()); it != m_gate_libraries.end())
{
return it->second.get();
}
// not loaded yet -> load
return load(absolute_path);
}
GateLibrary* get_gate_library_by_name(const std::string& lib_name)
{
for (const auto& it : m_gate_libraries)
{
if (it.second->get_name() == lib_name)
{
return it.second.get();
}
}
return nullptr;
}
std::vector<GateLibrary*> get_gate_libraries()
{
std::vector<GateLibrary*> res;
res.reserve(m_gate_libraries.size());
for (const auto& it : m_gate_libraries)
{
res.push_back(it.second.get());
}
return res;
}
} // namespace gate_library_manager
} // namespace hal