TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
topology.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Netlist views of a board for component recognition (doc 15 §3.1 "Topology", §8.0 topology detectors): pads per
4// net, nets per footprint, part kinds from reference letters, and the decoupling-capacitor ties of decision D25.
5// Every list is in ascending index order so results never depend on hash order (CLAUDE.md rule 2).
6#include <cstdint>
7#include <functional>
8#include <string>
9#include <vector>
10
11#include "model/board.hpp"
12
13namespace tmk::crules {
14
15using geom::Point;
16
18 public:
19 explicit BoardIndex(const model::Board& b);
20
21 const model::Board& board() const { return b_; }
22 const std::vector<int>& net_pads(model::NetId n) const { return net_pads_[static_cast<std::size_t>(n)]; }
23 // Distinct nets (non-zero) on a footprint's pads, ascending.
24 const std::vector<model::NetId>& fp_nets(int fi) const { return fp_nets_[static_cast<std::size_t>(fi)]; }
25 // Footprints with a pad on the net, ascending.
26 std::vector<int> parts_on(model::NetId n) const;
27 // First pad (ascending index) of footprint `fi` on net `n`, or -1.
28 int pad_on(int fi, model::NetId n) const;
29 const std::string& letters(int fi) const { return letters_[static_cast<std::size_t>(fi)]; }
30 const std::string& net_name(model::NetId n) const { return b_.nets[static_cast<std::size_t>(n)].name; }
31 int pad_count(int fi) const; // pads with a net or copper (mechanical NPTH excluded)
32
33 bool ground(model::NetId n) const { return n > 0 && ground_[static_cast<std::size_t>(n)] != 0; }
34 bool supply(model::NetId n) const { return n > 0 && supply_[static_cast<std::size_t>(n)] != 0; } // not ground
35 // Neither ground nor supply.
36 bool signal(model::NetId n) const { return n > 0 && !ground(n) && !supply(n); }
37
38 bool is_ic(int fi) const; // U*, IC* (D25's definition)
39 bool is_regulator_ref(int fi) const; // U*, IC*, VR*, REG*, PS*
40 bool is_cap(int fi) const; // C* with exactly two pads
41 bool is_series(int fi) const; // two-pad R*, FB*, L*, RN? (series element for net walks)
42 bool is_connector(int fi) const; // J*, P*, CN*, CON*, USB*, XS* followed by a digit (doc 04 connectors)
43 bool has_pin_names() const { return has_pin_names_; }
44
45 private:
46 const model::Board& b_;
47 std::vector<std::vector<int>> net_pads_;
48 std::vector<std::vector<model::NetId>> fp_nets_;
49 std::vector<std::string> letters_;
50 std::vector<std::uint8_t> ground_, supply_;
51 bool has_pin_names_ = false;
52};
53
54// Decision D25 (doc 04 §2): a two-pad capacitor (C*) between a supply net and a ground net is tied to the nearest
55// pad, in the input placement, of an IC (U*, IC*) on the same supply; ties keep the lowest pad index.
56// `usable(fp)` filters footprints (the placer passes "has a part"). With `generalised` (doc 15 ic_decoupling),
57// the supply may also be an analog supply/reference name (VREF, AREF, VCAP, ...) or a net that reaches an IC pad
58// whose pin name is a supply pin, and the ground may be a net reaching an IC ground pin by name.
59struct DecapTie {
60 int cap_fp = -1;
61 int cap_pad = -1; // the capacitor's supply pad
62 int ic_pad = -1; // the IC pad it is tied to
63};
64std::vector<DecapTie> decap_ties(const model::Board& b, const std::function<bool(int)>& usable, bool generalised);
65
66} // namespace tmk::crules
The board model (design doc 02 §3): every object TraceMaker reads from a .kicad_pcb,...
std::vector< int > parts_on(model::NetId n) const
Footprints with a pad on the net, ascending.
bool is_series(int fi) const
two-pad R*, FB*, L*, RN? (series element for net walks)
const model::Board & board() const
Definition topology.hpp:21
bool signal(model::NetId n) const
Neither ground nor supply.
Definition topology.hpp:36
bool has_pin_names() const
Definition topology.hpp:43
const std::vector< int > & net_pads(model::NetId n) const
Definition topology.hpp:22
bool is_connector(int fi) const
J, P, CN*, CON*, USB*, XS* followed by a digit (doc 04 connectors).
const std::vector< model::NetId > & fp_nets(int fi) const
Distinct nets (non-zero) on a footprint's pads, ascending.
Definition topology.hpp:24
int pad_count(int fi) const
pads with a net or copper (mechanical NPTH excluded)
bool supply(model::NetId n) const
Definition topology.hpp:34
bool is_ic(int fi) const
U, IC (D25's definition).
int pad_on(int fi, model::NetId n) const
First pad (ascending index) of footprint fi on net n, or -1.
bool is_cap(int fi) const
C* with exactly two pads.
BoardIndex(const model::Board &b)
const std::string & letters(int fi) const
Definition topology.hpp:29
const std::string & net_name(model::NetId n) const
Definition topology.hpp:30
bool is_regulator_ref(int fi) const
U, IC, VR*, REG*, PS*.
bool ground(model::NetId n) const
Definition topology.hpp:33
std::vector< DecapTie > decap_ties(const model::Board &b, const std::function< bool(int)> &usable, bool generalised)
std::int32_t NetId
0 = no net
Definition board.hpp:21
Decision D25 (doc 04 §2): a two-pad capacitor (C*) between a supply net and a ground net is tied to t...
Definition topology.hpp:59
int cap_pad
the capacitor's supply pad
Definition topology.hpp:61
int ic_pad
the IC pad it is tied to
Definition topology.hpp:62