-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsim_system_test.cpp
More file actions
419 lines (357 loc) · 9.42 KB
/
Copy pathsim_system_test.cpp
File metadata and controls
419 lines (357 loc) · 9.42 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#include "linx/model.hpp"
namespace {
namespace fs = std::filesystem;
class PipelineModule : public linx::model::Module<PipelineModule, int> {
public:
explicit PipelineModule(bool root = false)
: linx::model::Module<PipelineModule, int>(root ? "pipe_root" : "pipe_stage"),
build_root_(root) {
DescribeInput("flag_in", "Visible input token for this pipeline stage");
DescribeOutput("flag_out", "Visible output token for this pipeline stage");
if (!root) {
DescribeInner("stage_pipe", "Internal registered token");
} else {
DescribeInner("root_pipe", "Top-level internal pipeline link");
}
}
int work_count = 0;
int xfer_count = 0;
protected:
void BuildSelf() override {
if (!build_root_) {
return;
}
auto *input_q = CreateOwnedQueue(8, 0, "pipe_input_q");
auto *inner_q = CreateOwnedQueue(8, 1, "pipe_inner_q");
auto *output_q = CreateOwnedQueue(8, 0, "pipe_output_q");
BindInput(0, input_q);
BindInner(0, inner_q);
BindOutput(0, output_q);
auto &child = AddSubmodule(std::make_unique<PipelineModule>(false));
ConnectInput(child, 0, input_q);
ConnectInner(child, 0, inner_q);
ConnectOutput(child, 0, output_q);
}
void WorkSelf() override {
++work_count;
if (build_root_) {
return;
}
INPUT(in0, 0);
INNER(pipe0, 0);
OUTPUT(out0, 0);
if (!pipe0->Empty() && !out0->Full()) {
const int ready = pipe0->Read();
out0->Write(ready + 10);
return;
}
if (!in0->Empty() && !pipe0->Full()) {
const int head = in0->Front();
pipe0->Write(head + 1);
in0->Pop();
}
}
void XferSelf() override {
++xfer_count;
}
private:
bool build_root_ = false;
};
class CountingModule : public linx::model::SimObject {
public:
explicit CountingModule(std::vector<std::string> *events)
: linx::model::SimObject("counter"), events_(events) {}
void Build() override {
events_->push_back("build");
}
void Reset() override {
events_->push_back("reset");
}
void Report() override {
events_->push_back("report");
}
void Work() override {
events_->push_back("work");
}
void Xfer() override {
events_->push_back("xfer");
}
private:
std::vector<std::string> *events_ = nullptr;
};
class ToySim : public linx::model::SimSystem {
public:
explicit ToySim(int terminate_cycle) : terminate_cycle_(terminate_cycle) {
pipeline_ = &EmplaceOwnedModule<PipelineModule>(true);
AddModule(external_counter_);
}
void RunReference(std::optional<std::uint64_t> stop_pc) override {
reference_calls.push_back(stop_pc);
}
void PrintPipeView(std::ostream &os) const override {
os << "cycle=" << Cycle() << '\n';
}
bool NeedTerminate() const override {
return Cycle() >= static_cast<std::uint64_t>(terminate_cycle_);
}
PipelineModule &Pipeline() {
return *pipeline_;
}
std::vector<std::optional<std::uint64_t>> reference_calls;
private:
void BuildSystem() override {
build_system_calls++;
}
void ResetSystem() override {
reset_system_calls++;
}
void ReportSystem() override {
report_system_calls++;
}
public:
int build_system_calls = 0;
int reset_system_calls = 0;
int report_system_calls = 0;
private:
int terminate_cycle_ = 0;
PipelineModule *pipeline_ = nullptr;
std::vector<std::string> external_events_;
CountingModule external_counter_{&external_events_};
};
int RunArgParseSmoke() {
ToySim sim(2);
std::ostringstream out;
std::ostringstream err;
const char *argv[] = {
"toy-sim", "--stop-pc", "0x20", "--max-cycles", "2", "--log-level", "debug", "--no-report",
};
const int rc = linx::model::RunSimMain(static_cast<int>(std::size(argv)),
const_cast<char **>(argv), sim, out, err);
if (rc != 0) {
return 1;
}
if (!err.str().empty()) {
return 2;
}
if (sim.Cycle() != 2) {
return 3;
}
if (sim.reference_calls.size() != 2) {
return 4;
}
if (sim.reference_calls[0] != 0x20 || sim.reference_calls[1] != 0x20) {
return 5;
}
if (out.str() != "cycle=1\ncycle=2\n") {
return 6;
}
if (sim.report_system_calls != 0) {
return 7;
}
if (sim.build_system_calls != 1 || sim.reset_system_calls != 1) {
return 8;
}
if (sim.Logger().MinLevel() != linx::model::LogLevel::Debug) {
return 9;
}
return 0;
}
int RunResultDumpArgParseSmoke() {
std::ostringstream out;
std::ostringstream err;
int exit_code = -1;
const char *argv[] = {
"linx-model",
"--result-dump",
"/tmp/result.bin",
"--result-address",
"0x20000",
"--result-size",
"12",
};
const auto parsed = linx::model::detail::ParseSimMainArgs(
static_cast<int>(std::size(argv)), const_cast<char **>(argv), out, err, exit_code);
if (!parsed.has_value() || exit_code != 0 || !err.str().empty() ||
parsed->result_dump_path != "/tmp/result.bin" || parsed->result_address != 0x20000 ||
parsed->result_size != 12) {
return 10;
}
return 0;
}
std::vector<std::uint8_t> EncodeAddBytes() {
const auto *form = linx::model::isa::LookupFormByMnemonic("ADD");
if (form == nullptr) {
return {};
}
linx::model::isa::Minst inst;
inst.SetForm(form);
for (const auto &field : linx::model::isa::FieldsFor(*form)) {
inst.SetDecodedField(field.name, 0, field.signed_hint > 0, field.bit_width);
}
inst.RebuildTypedViews();
const auto encoded = linx::model::isa::EncodeMinst(inst);
if (!encoded.valid) {
return {};
}
std::vector<std::uint8_t> bytes(static_cast<std::size_t>(encoded.length_bits / 8));
for (std::size_t idx = 0; idx < bytes.size(); ++idx) {
bytes[idx] = static_cast<std::uint8_t>((encoded.bits >> (idx * 8U)) & 0xffU);
}
return bytes;
}
void WriteBinaryFile(const fs::path &path, const std::vector<std::uint8_t> &bytes) {
std::ofstream out(path, std::ios::binary);
out.write(reinterpret_cast<const char *>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
}
int RunDisasmCliSmoke() {
const auto bytes = EncodeAddBytes();
if (bytes.empty()) {
return 90;
}
const auto path = fs::temp_directory_path() / "linx_model_cli_disasm.bin";
{
std::error_code ec;
fs::remove(path, ec);
}
WriteBinaryFile(path, bytes);
ToySim sim(2);
std::ostringstream out;
std::ostringstream err;
const std::string path_text = path.string();
const char *argv[] = {
"toy-sim", "--bin", path_text.c_str(), "--raw-base", "0x3000", "--disasm-only",
};
const int rc = linx::model::RunSimMain(static_cast<int>(std::size(argv)),
const_cast<char **>(argv), sim, out, err);
std::error_code ec;
fs::remove(path, ec);
if (rc != 0 || !err.str().empty()) {
return 91;
}
if (sim.Cycle() != 0 || sim.build_system_calls != 0 || sim.reset_system_calls != 0) {
return 92;
}
const auto text = out.str();
if (text.find("source:") == std::string::npos || text.find("0x3000") == std::string::npos ||
text.find("add") == std::string::npos) {
return 93;
}
return 0;
}
int RunStepOrderSmoke() {
std::vector<std::string> events;
CountingModule counter(&events);
linx::model::SimSystem sim;
sim.AddModule(counter);
sim.Build();
sim.Reset();
sim.Step();
sim.Report();
const std::vector<std::string> expected = {"build", "reset", "work", "xfer", "report"};
if (events != expected) {
return 10;
}
return 0;
}
int RunInnerQueueSmoke() {
PipelineModule root(true);
root.Build();
if (root.InputCount() != 1 || root.InnerCount() != 1 || root.OutputCount() != 1) {
return 20;
}
if (root.InputPortInfo(0).name != "flag_in" || root.InnerPortInfo(0).name != "root_pipe") {
return 21;
}
root.Input(0)->Write(5);
root.Work();
if (!root.Inner(0)->Empty()) {
return 22;
}
if (!root.Output(0)->Empty()) {
return 23;
}
if (root.Submodule(0).RanWorkSelfLastCycle()) {
return 231;
}
root.Work();
if (root.Inner(0)->Empty()) {
return 24;
}
if (!root.Output(0)->Empty()) {
return 25;
}
if (!root.Submodule(0).RanWorkSelfLastCycle()) {
return 251;
}
root.Work();
if (root.Output(0)->Empty()) {
return 26;
}
if (root.Output(0)->Read() != 16) {
return 27;
}
if (root.Submodule(0).work_count != 2 || root.Submodule(0).xfer_count != 0) {
return 28;
}
root.Xfer();
if (root.Submodule(0).xfer_count != 1) {
return 29;
}
return 0;
}
int RunIdleSkipSmoke() {
PipelineModule root(true);
root.Build();
root.Work();
root.Xfer();
const int child_work_after_first = root.Submodule(0).work_count;
root.Work();
root.Xfer();
if (root.Submodule(0).work_count != child_work_after_first) {
return 30;
}
if (root.Submodule(0).RanWorkSelfLastCycle()) {
return 31;
}
root.Input(0)->Write(9);
root.Work();
if (root.Submodule(0).RanWorkSelfLastCycle()) {
return 32;
}
root.Work();
if (!root.Submodule(0).RanWorkSelfLastCycle()) {
return 33;
}
return 0;
}
} // namespace
int main() {
if (RunStepOrderSmoke() != 0) {
return 1;
}
if (RunArgParseSmoke() != 0) {
return 2;
}
if (RunResultDumpArgParseSmoke() != 0) {
return 6;
}
if (RunInnerQueueSmoke() != 0) {
return 3;
}
if (RunIdleSkipSmoke() != 0) {
return 4;
}
if (RunDisasmCliSmoke() != 0) {
return 5;
}
return 0;
}