SILICON
Loading...
Searching...
No Matches
autoplacer.cpp
Go to the documentation of this file.
1#include <QApplication>
2#include <QGraphicsRectItem>
3#include <core/circuit.hpp>
4#include <core/gates.hpp>
5#include <gtest/gtest.h>
8
9using namespace SILICON::core;
10using namespace SILICON::ui;
11
12namespace {
13class AutoplacerCancellation : public testing::Test {
14protected:
15 static void SetUpTestSuite()
16 {
17 static int argc = 1;
18 static char name[] = "autoplacer_tests";
19 static char* argv[] = {name, nullptr};
20 static QApplication app(argc, argv);
21 }
22};
23} // namespace
24
25TEST_F(AutoplacerCancellation, BeforeFirstCandidateLeavesNoGeometry)
26{
27 Circuit circuit;
29 options.isCancelled = [] { return true; };
30 auto result = CircuitAutoplacer::compute(circuit, {}, options);
31 EXPECT_TRUE(result.cancelled);
32 EXPECT_TRUE(result.components.empty());
33 EXPECT_TRUE(result.wires.empty());
34}
35
36TEST_F(AutoplacerCancellation, DuringRoutingDiscardsCandidateAndKeepsBest)
37{
38 auto net = std::make_shared<Wire>();
39 auto source = std::make_shared<AndGate>();
40 auto target = std::make_shared<AndGate>();
41 source->setInputs({Bus{std::make_shared<Wire>()}, Bus{std::make_shared<Wire>()}});
42 source->setOutputs({Bus{net}});
43 target->setOutputs({Bus{std::make_shared<Wire>()}});
44 target->setInputs({Bus{net}, Bus{std::make_shared<Wire>()}});
45 Circuit circuit(Component_set{source, target}, false);
46 GraphicalLogicComponent first(source, new QGraphicsRectItem(0, 0, 80, 80), nullptr);
47 GraphicalLogicComponent second(target, new QGraphicsRectItem(0, 0, 80, 80), nullptr);
48 first.setPorts({{"a", {-20, 20}}, {"b", {-20, 60}}}, {{"o", {100, 40}}});
49 second.setPorts({{"a", {-20, 20}}, {"b", {-20, 60}}}, {{"o", {100, 40}}});
50 std::vector<GraphicalLogicComponent*> components{&first, &second};
52 options.candidateCount = 2;
53 // Enumerate every cancellation checkpoint through the first completed
54 // candidate, including router and wire-merger polls. None may expose a prefix.
55 unsigned checks = 0, firstCompleteChecks = 0;
56 options.isCancelled = [&] {
57 ++checks;
58 return false;
59 };
60 options.progress = [&](int done, int) {
61 if (done == 1 && !firstCompleteChecks)
62 firstCompleteChecks = checks;
63 };
64 auto complete = CircuitAutoplacer::compute(circuit, components, options);
65 ASSERT_FALSE(complete.cancelled);
66 ASSERT_FALSE(complete.wires.empty());
67 ASSERT_GT(firstCompleteChecks, 2u);
68 for (unsigned cancelAt = 1; cancelAt <= firstCompleteChecks; ++cancelAt) {
69 checks = 0;
70 options.progress = {};
71 options.isCancelled = [&] { return ++checks >= cancelAt; };
72 auto result = CircuitAutoplacer::compute(circuit, components, options);
73 EXPECT_TRUE(result.cancelled) << cancelAt;
74 EXPECT_TRUE(result.components.empty()) << cancelAt;
75 EXPECT_TRUE(result.wires.empty()) << cancelAt;
76 }
77 bool cancelled = false;
78 options.isCancelled = [&] { return cancelled; };
79 options.progress = [&](int done, int) {
80 if (done == 1)
81 cancelled = true;
82 };
83 auto best = CircuitAutoplacer::compute(circuit, components, options);
84 EXPECT_FALSE(best.cancelled);
85 EXPECT_FALSE(best.wires.empty());
86 // Interrupt inside the next attempt, after the previous candidate was saved.
87 cancelled = false;
88 checks = 0;
89 options.isCancelled = [&] { return cancelled && ++checks >= 4; };
90 auto interrupted = CircuitAutoplacer::compute(circuit, components, options);
91 EXPECT_FALSE(interrupted.cancelled);
92 EXPECT_EQ(interrupted.components, best.components);
93 ASSERT_EQ(interrupted.wires.size(), best.wires.size());
94 for (std::size_t i = 0; i < best.wires.size(); ++i) {
95 EXPECT_EQ(interrupted.wires[i].bus, best.wires[i].bus);
96 EXPECT_EQ(interrupted.wires[i].points, best.wires[i].points);
97 }
98 EXPECT_EQ(first.pos(), QPointF());
99 EXPECT_EQ(second.pos(), QPointF());
100}
TEST_F(AutoplacerCancellation, BeforeFirstCandidateLeavesNoGeometry)
Represents a digital circuit as a directed graph of components.
Definition circuit.hpp:125
static CircuitAutoplacement compute(const core::Circuit &circuit, std::span< GraphicalLogicComponent *const > components, const CircuitAutoplacerOptions &options={})
Computes component placements and routed wire paths for a circuit.
Base class for logic-gate graphical components.
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
std::set< Component_ptr, std::owner_less< Component_ptr > > Component_set
Definition wire.hpp:54
Configuration for full circuit autoplacement and wire routing.
std::function< bool()> isCancelled
Optional cooperative cancellation check, also polled during routing. A candidate finishes routing bef...
std::function< void(int completed, int total)> progress
Optional notification after each completed candidate.
int candidateCount
Maximum number of deterministic candidates. Large designs use fewer candidates and refinements to bou...