-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
278 lines (258 loc) · 9.23 KB
/
Copy pathmain.cpp
File metadata and controls
278 lines (258 loc) · 9.23 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// dcs — 数字电路仿真 CLI(合并 tool + simulator)
//
// 用法:
// dcs circuit.json -t 10 -m 仿真运行
// dcs --json-to-c circuit.json 输出 C 代码
// dcs --list-types 列出元件类型
// dcs --dll-info <path> 查看 DLL 元件引脚信息
// dcs --help 帮助
#include <chrono>
#include <cstdio>
#include <cxxopts.hpp>
#include <dcs/Circuit.h>
#include <dcs/ComponentFactory.h>
#include <dcs/dll_interface.h>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <thread>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
namespace {
// ---- list-types ----
void listTypes() {
auto &f = dsc::ComponentFactory::instance();
const auto &metas = f.metas();
std::cout << std::format("已注册元件类型: {} 种\n\n", metas.size());
for (auto &[name, meta]: metas) {
std::cout << std::format(" [{}] ({})\n", meta.type_name, dsc::categoryName(meta.category));
if (!meta.description.empty())
std::cout << std::format(" {}\n", meta.description);
if (meta.params.empty()) {
std::cout << " 参数: (无)\n";
}
else {
std::cout << " 参数:\n";
for (auto &p: meta.params) {
std::cout << std::format(" {} (默认: \"{}\")", p.name, p.default_val);
if (!p.valid_values.empty()) {
std::cout << " 可选: {";
for (size_t i = 0; i < p.valid_values.size(); i++)
std::cout << (i ? ", " : "") << p.valid_values[i];
std::cout << "}";
}
std::cout << "\n";
}
}
if (meta.input_pins.empty()) {
std::cout << " 输入引脚: (动态)\n";
}
else {
std::cout << " 输入引脚: ";
for (size_t i = 0; i < meta.input_pins.size(); i++)
std::cout << (i ? ", " : "") << meta.input_pins[i];
std::cout << "\n";
}
if (meta.output_pins.empty()) {
std::cout << " 输出引脚: (动态)\n";
}
else {
std::cout << " 输出引脚: ";
for (size_t i = 0; i < meta.output_pins.size(); i++)
std::cout << (i ? ", " : "") << meta.output_pins[i];
std::cout << "\n";
}
std::cout << "\n";
}
}
// ---- dll-info ----
int dllInfo(const std::string &path) {
#ifdef _WIN32
HMODULE h = LoadLibraryA(path.c_str());
if (!h) {
std::cerr << std::format("错误: 无法加载 DLL '{}'\n", path);
return 1;
}
FARPROC fp = GetProcAddress(h, "dcs_dll_desc");
if (!fp) {
std::cerr << "DLL 缺少导出符号: dcs_dll_desc\n";
FreeLibrary(h);
return 1;
}
dcs_dll_descriptor_t *desc = nullptr;
std::memcpy(&desc, &fp, sizeof(fp));
#else
void *h = dlopen(path.c_str(), RTLD_NOW);
if (!h) {
std::cerr << std::format("错误: 无法加载 DLL '{}'\n", path);
return 1;
}
void *sym = dlsym(h, "dcs_dll_desc");
if (!sym) {
std::cerr << "DLL 缺少导出符号: dcs_dll_desc\n";
dlclose(h);
return 1;
}
dcs_dll_descriptor_t *desc = reinterpret_cast<dcs_dll_descriptor_t *>(sym);
#endif
std::cout << std::format("DLL 元件: {}\n", desc->type_name);
std::cout << std::format(" 输入引脚 ({} 个):\n", desc->num_inputs);
for (int i = 0; i < desc->num_inputs; i++) {
auto &p = desc->inputs[i];
std::cout << std::format(" {} ({} 位)\n", p.name, p.bit_width);
}
std::cout << std::format(" 输出引脚 ({} 个):\n", desc->num_outputs);
for (int i = 0; i < desc->num_outputs; i++) {
auto &p = desc->outputs[i];
const char *attr = p.is_sequential ? "时序" : "组合";
const char *ts = p.is_tri_state ? " 三态" : "";
std::cout << std::format(" {} ({} 位, {}{})\n", p.name, p.bit_width, attr, ts);
}
#ifdef _WIN32
FreeLibrary(h);
#else
dlclose(h);
#endif
return 0;
}
// ---- json-to-c ----
int jsonToC(const std::string &path) {
std::ifstream ifs(path);
if (!ifs) {
std::cerr << std::format("错误: 无法打开 '{}'\n", path);
return 1;
}
std::ostringstream oss;
oss << ifs.rdbuf();
std::string error;
std::unique_ptr<dsc::Circuit> circuit;
try {
circuit = dsc::Circuit::fromJson(oss.str(), error, std::filesystem::path(path).parent_path().string());
} catch (const std::exception &e) {
std::cerr << std::format("错误: {}\n", e.what());
return 1;
}
if (!circuit) {
std::cerr << std::format("JSON 错误: {}\n", error);
return 1;
}
auto err = circuit->compile();
if (err.code != dsc::ErrorCode::OK)
std::cerr << std::format("// 编译错误: {}\n", err.message);
std::cout << circuit->generateC();
return err.code == dsc::ErrorCode::OK ? 0 : 1;
}
// ---- simulate ----
int simulate(const std::string &path, int ticks, int delay_ms, bool monitor) {
std::ifstream ifs(path);
if (!ifs) {
std::cerr << std::format("错误: 无法打开 '{}'\n", path);
return 1;
}
std::ostringstream oss;
oss << ifs.rdbuf();
std::string error;
auto circuit = dsc::Circuit::fromJson(oss.str(), error, std::filesystem::path(path).parent_path().string());
if (!circuit) {
std::cerr << std::format("JSON 错误: {}\n", error);
return 1;
}
std::cout << std::format("电路: {} 线网, {} 元件\n", circuit->netCount(), circuit->componentCount());
auto err = circuit->compile();
if (err.code != dsc::ErrorCode::OK) {
std::cerr << std::format("编译错误: {}\n", err.message);
return 1;
}
std::cout << "JIT 编译成功\n\n";
circuit->init();
for (int t = 0; t < ticks; t++) {
circuit->tick();
if (monitor) {
std::cout << std::format("=== Tick {} ===\n", t);
for (int i = 0; i < circuit->netCount(); i++) {
auto val = circuit->getWire(i);
bool nonzero = false;
for (auto b: val)
if (b) {
nonzero = true;
break;
}
if (nonzero) {
std::cout << std::format(" net {:2}: ", i);
for (int j = 0; j < 16; j++)
std::cout << std::format("{:02X}", val[j]);
std::cout << "\n";
}
}
std::cout << std::flush;
}
if (t < ticks - 1)
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
circuit->deinit();
return 0;
}
} // namespace
int main(int argc, char *argv[]) {
cxxopts::Options opts("dcs", "数字电路仿真 CLI");
// 仿真选项
opts.add_options("仿真")("circuit", "JSON 电路描述文件(位置参数)", cxxopts::value<std::string>())(
"t,ticks", "仿真 tick 数(默认 10)", cxxopts::value<int>()->default_value("10"))(
"d,delay", "每 tick 间隔毫秒(默认 1000)",
cxxopts::value<int>()->default_value("1000"))("m,monitor", "逐 tick 打印线网值");
// 工具选项
opts.add_options("工具")("l,list-types", "列出所有已注册的元件类型")("j,json-to-c", "JSON 电路转为 C 代码输出",
cxxopts::value<std::string>())(
"dll-info", "查看 DLL 元件引脚信息", cxxopts::value<std::string>());
opts.add_options("其他")("h,help", "显示帮助")("v,version", "显示版本号");
opts.parse_positional({"circuit"});
opts.positional_help("circuit.json");
cxxopts::ParseResult result;
try {
result = opts.parse(argc, argv);
} catch (const cxxopts::exceptions::exception &e) {
std::cerr << std::format("参数错误: {}\n{}\n", e.what(), opts.help());
return 1;
}
if (result.count("version")) {
std::cout << "dcs 0.0.1\n";
return 0;
}
if (result.count("help") || argc == 1) {
std::cout << opts.help() << "\n";
#ifdef DCS_USE_TCC
std::cout << "JIT 后端: TinyCC\n\n";
#else
std::cout << "JIT 后端: LLVM/Clang\n\n";
#endif
std::cout << R"(示例:
dcs circuit.json -t 20 -m 仿真 20 tick 并监控
dcs --json-to-c circuit.json 输出等效 C 代码
dcs --list-types 列出元件类型
dcs --dll-info adder8_dll.dll 查看 DLL 引脚信息
)";
return 0;
}
if (result.count("list-types")) {
listTypes();
return 0;
}
if (result.count("dll-info")) {
return dllInfo(result["dll-info"].as<std::string>());
}
if (result.count("json-to-c")) {
return jsonToC(result["json-to-c"].as<std::string>());
}
if (result.count("circuit")) {
return simulate(result["circuit"].as<std::string>(), result["ticks"].as<int>(), result["delay"].as<int>(),
result.count("monitor") != 0);
}
std::cerr << "错误: 请指定 JSON 电路文件\n\n" << opts.help() << "\n";
return 1;
}