Signal types, pin role expectations, and a doctest suite.

Domain
- Signal carries a SignalType (Power/GndShield/Other), auto-inferred
  from the name in Signal::Signal via infer_signal_type. Override with
  the new `set-signal-type` command.
- SignalType extracted to its own header so Pin can store an
  `expected_signal_type` without a pins↔signals include cycle.
- pin_role(connector_type, pin_name) → SignalType lookup, called from
  set-type to populate each Pin's expected_signal_type. The VPX 3U
  table is currently a stub (returns Other).
- New `verify` command walks typed parts and reports pins whose
  connected signal's type doesn't match the expectation.
- ODS importer no longer drops pins with empty signal column — they
  stay in the part as NC, matching the rule "a pin is either NC or
  connected to a signal".
- persist: new S tag for non-default signal type overrides.

Tests
- doctest v2.4.11 via FetchContent (with CMAKE_POLICY_VERSION_MINIMUM
  shim, doctest's CMakeLists has a too-old floor for current CMake).
- Source files moved into a static library `essim_lib` so both `essim`
  and `essim_tests` reuse the same compilation. main.cpp is the only
  file kept out of the lib.
- Layer 1 (pure helpers): ToLower, LongestCommonPrefix, Tokenize,
  NaturalLess (numeric/case/leading-zero edge cases + total-order
  invariants), signal_type round-trips and infer_signal_type families,
  VpxTransform registry + symmetry + reference-table mapping for
  connector P0 row 1, IdentityTransform same-name wiring.
- Layer 2 (round-trip): build a synthetic 2-module system in code,
  save → restore → assert modules / parts / connector_types / NC pins
  / signal type overrides / connections + pin_map are all preserved.
- Tui::Tokenize moved to a free function in tui_helpers so tests can
  call it without dragging ftxui into the unit-test layer.
- 27 test cases, 123 assertions, ~150 ms.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-08 20:28:03 +02:00
parent f3920964f0
commit 4f27686e94
23 changed files with 732 additions and 32 deletions

View File

@@ -101,14 +101,14 @@ void ImportOds::parse(Signals *signals)
const std::string &pin_name = cells[0];
const std::string &sig_name = cells[1];
if (pin_name.empty() || sig_name.empty()) continue;
if (pin_name.empty()) continue;
Pin *pin = new Pin(pin_name);
try { prt->add(pin); }
catch (const std::exception &) { delete pin; continue; }
// "NC" = Not Connected: keep the pin in the part, no signal hookup.
if (sig_name == "NC") continue;
// NC: empty signal name OR explicit "NC" → keep the pin, no hookup.
if (sig_name.empty() || sig_name == "NC") continue;
Signal *s = signals->merge(sig_name);
s->add(pin);

View File

@@ -49,6 +49,12 @@ bool save_system(const System *sys, const std::string &filename, std::string &er
f << "N\t" << pin->name << "\t" << (s ? s->name : "") << "\n";
}
}
// Signal types: only persist non-default (Other) overrides.
for (auto &skv : *mod->signals) {
Signal *s = skv.second;
if (s->type == SignalType::Other) continue;
f << "S\t" << s->name << "\t" << signal_type_name(s->type) << "\n";
}
}
for (auto &ckv : *sys->connections()) {
@@ -122,6 +128,14 @@ System *restore_system(const std::string &filename, std::string &error)
s->add(pin);
pin->connect(s);
}
} else if (tag == "S") {
if (!cur_mod) return fail("S outside module");
if (fs.size() < 3) return fail("S needs <signal> <type>");
Signal *s;
try { s = cur_mod->signals->get(fs[1]); }
catch (const std::exception &) { continue; }
SignalType t;
if (signal_type_from_name(fs[2], t)) s->type = t;
} else if (tag == "C") {
if (fs.size() < 7) return fail("C needs <name> <m1> <p1> <m2> <p2> <transform>");
Module *m1 = sys->modules()->get(fs[2]);

55
src/system/pin_role.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include "pin_role.hpp"
#include <cctype>
#include <exception>
#include <string>
// VPX 3U built-in pin role tables.
//
// NOTE: real VITA 46 pin roles are connector-/profile-specific (data lanes,
// power planes, GND chassis, etc.). The placeholders below are intentionally
// minimal — fill in the actual per-(col,row) roles for your design when the
// reference is available; the rest of the chain (set-type → verify) is
// already wired through this single function.
namespace {
// Quick char column dispatch — returns Other when the column isn't recognised.
SignalType vpx_3u_role(char /*col*/, int /*row*/, int /*connector_idx*/) {
// TODO: encode the real layout. Right now everything is "other"; verify()
// therefore reports nothing for VPX until this table is filled in.
return SignalType::Other;
}
bool parse_pin(const std::string &s, char &col, int &row) {
if (s.size() < 2) return false;
if (!std::isalpha((unsigned char)s[0])) return false;
for (size_t i = 1; i < s.size(); ++i)
if (!std::isdigit((unsigned char)s[i])) return false;
col = (char)std::toupper((unsigned char)s[0]);
try { row = std::stoi(s.substr(1)); }
catch (const std::exception &) { return false; }
return true;
}
} // namespace
SignalType pin_role(const std::string &kind, const std::string &pin_name)
{
if (kind.empty()) return SignalType::Other;
char col; int row;
if (!parse_pin(pin_name, col, row)) return SignalType::Other;
int idx = -1;
bool is_vpx_3u = false;
if (kind.rfind("vpx-3u-bkp-p", 0) == 0
|| kind.rfind("vpx-3u-payload-p", 0) == 0) {
is_vpx_3u = true;
const std::string suffix = kind.substr(kind.size() - 1);
try { idx = std::stoi(suffix); } catch (const std::exception &) {}
}
if (is_vpx_3u && idx >= 0) return vpx_3u_role(col, row, idx);
return SignalType::Other;
}

18
src/system/pin_role.hpp Normal file
View File

@@ -0,0 +1,18 @@
#ifndef _PIN_ROLE_HPP_
#define _PIN_ROLE_HPP_
#include "signal_type.hpp"
#include <string>
// For a given connector type and pin position, return the expected SignalType
// (Power / GndShield / Other). Used at `set-type` to populate each pin's
// `expected_signal_type`, then later by `verify` to flag mismatches between
// the connector's expectation and the actual signal's inferred/declared type.
//
// Returns SignalType::Other for unknown connector types or unmatched pins —
// caller can treat that as "no expectation, no constraint".
SignalType pin_role(const std::string &connector_type,
const std::string &pin_name);
#endif // _PIN_ROLE_HPP_

View File

@@ -2,7 +2,9 @@
#include "parts.hpp"
#include "signals.hpp"
Pin::Pin(std::string name) : SystemElement(name), sig(nullptr), prnt(nullptr) {};
Pin::Pin(std::string name)
: SystemElement(name), sig(nullptr), prnt(nullptr),
expected_signal_type(SignalType::Other) {};
bool Pin::connected()
{

View File

@@ -1,6 +1,7 @@
#ifndef _PINS_HPP_
#define _PINS_HPP_
#include "signal_type.hpp"
#include "syselmts.hpp"
#pragma once
@@ -15,6 +16,7 @@ class Pin : public SystemElement
public:
Pin(std::string name);
Part *prnt; ///< Pointer to the parent part.
SignalType expected_signal_type; ///< Set from connector_type at set-type.
bool connected();
Signal *signal() const { return sig; }
void connect(Signal *signal);

View File

@@ -0,0 +1,12 @@
#ifndef _SIGNAL_TYPE_HPP_
#define _SIGNAL_TYPE_HPP_
#include <string>
enum class SignalType { Power, GndShield, Other };
const char *signal_type_name(SignalType t);
bool signal_type_from_name(const std::string &s, SignalType &out);
SignalType infer_signal_type(const std::string &signal_name);
#endif // _SIGNAL_TYPE_HPP_

View File

@@ -2,7 +2,70 @@
#include "signals.hpp"
#include "parts.hpp"
Signal::Signal(std::string name) : SystemElementContainer<Pin>(name), prnt(nullptr) {};
#include <algorithm>
#include <cctype>
#include <cstring>
const char *signal_type_name(SignalType t) {
switch (t) {
case SignalType::Power: return "power";
case SignalType::GndShield: return "gnd";
case SignalType::Other: return "other";
}
return "other";
}
bool signal_type_from_name(const std::string &s, SignalType &out) {
std::string l = s;
std::transform(l.begin(), l.end(), l.begin(),
[](unsigned char c) { return std::tolower(c); });
if (l == "power" || l == "p") { out = SignalType::Power; return true; }
if (l == "gnd" || l == "g" || l == "shield" || l == "ground")
{ out = SignalType::GndShield; return true; }
if (l == "other" || l == "o" || l == "signal")
{ out = SignalType::Other; return true; }
return false;
}
// Heuristic. Names like GND, GNDA, SHIELD, CHASSIS → GndShield.
// Names containing PWR/POWER/VCC/VDD/VEE/VSS, or matching V±N or +N.NV
// patterns, or starting with VS_/VS3_ → Power. Else Other.
SignalType infer_signal_type(const std::string &name) {
if (name.empty()) return SignalType::Other;
std::string u = name;
std::transform(u.begin(), u.end(), u.begin(),
[](unsigned char c) { return std::toupper(c); });
auto contains = [&](const char *needle) {
return u.find(needle) != std::string::npos;
};
auto starts_with = [&](const char *needle) {
return u.rfind(needle, 0) == 0;
};
if (u == "GND" || u == "GROUND") return SignalType::GndShield;
if (starts_with("GND_")
|| (starts_with("GND") && u.size() >= 4
&& std::isalpha((unsigned char)u[3]))) {
return SignalType::GndShield;
}
if (contains("SHIELD") || contains("CHASSIS") || contains("EARTH"))
return SignalType::GndShield;
if (contains("PWR") || contains("POWER")
|| contains("VCC") || contains("VDD") || contains("VEE") || contains("VSS")
|| starts_with("VS_") || starts_with("VS1_") || starts_with("VS2_")
|| starts_with("VS3_") || starts_with("VS4_")
|| starts_with("VBAT") || starts_with("VBUS")
|| starts_with("+") || starts_with("-")) {
return SignalType::Power;
}
return SignalType::Other;
}
Signal::Signal(std::string name)
: SystemElementContainer<Pin>(name), prnt(nullptr),
type(infer_signal_type(name)) {};
void Signal::add(Pin *pin)
{

View File

@@ -1,6 +1,7 @@
#ifndef _SIGNALS_HPP_
#define _SIGNALS_HPP_
#include "signal_type.hpp"
#include "syselmts.hpp"
#include "pins.hpp"
@@ -11,6 +12,7 @@ class Signal : public SystemElementContainer<Pin>
{
public:
Signals *prnt; ///< Pointer to the parent signals object.
SignalType type;
Signal(std::string name);
void add(Pin *pin) override;
};

View File

@@ -5,6 +5,8 @@
#include "system/modules.hpp"
#include "system/parts.hpp"
#include "system/persist.hpp"
#include "system/pin_role.hpp"
#include "system/pins.hpp"
#include "system/signals.hpp"
#include "system/system.hpp"
#include "system/transform.hpp"
@@ -152,6 +154,66 @@ void Tui::RegisterCommands() {
"replace the current system with a saved snapshot",
};
commands["verify"] = { {}, [this](auto &) {
if (!sys) { Print("no system: run 'new' first."); return; }
int checked = 0;
int mismatches = 0;
for (auto &mkv : *sys->modules()) {
Module *mod = mkv.second;
for (auto &pkv : *mod) {
Part *prt = pkv.second;
if (prt->connector_type.empty()) continue;
for (auto &nkv : *prt) {
Pin *pin = nkv.second;
++checked;
SignalType expected = pin->expected_signal_type;
if (expected == SignalType::Other) continue;
Signal *s = pin->signal();
SignalType actual = s ? s->type : SignalType::Other;
if (actual == expected) continue;
++mismatches;
std::string sig_label = s ? s->name : std::string("(NC)");
Print(" " + mod->name + "/" + prt->name + "/" + pin->name
+ ": expected " + signal_type_name(expected)
+ ", got " + signal_type_name(actual)
+ " (signal: " + sig_label + ")");
}
}
}
Print("verify: " + std::to_string(mismatches) + " mismatch(es) over "
+ std::to_string(checked) + " typed pin(s).");
}, true,
"check that each pin's connected signal matches its connector_type's expected role" };
commands["set-signal-type"] = {
{{"module", Completion::None},
{"signal name", Completion::None},
{"type [power|gnd|other]", Completion::None}},
[this](const std::vector<std::string> &args) {
if (!sys) { Print("no system: run 'new' first."); return; }
Module *mod;
try { mod = sys->modules()->get(args[0]); }
catch (const std::exception &) {
Print("unknown module: " + args[0]); return;
}
Signal *sig;
try { sig = mod->signals->get(args[1]); }
catch (const std::exception &) {
Print("unknown signal: " + mod->name + "/" + args[1]); return;
}
SignalType t;
if (!signal_type_from_name(args[2], t)) {
Print("type must be one of: power, gnd, other (got: " + args[2] + ")");
return;
}
sig->type = t;
Print(mod->name + "/" + sig->name + ": signal type = "
+ signal_type_name(t));
},
/*prompt_for_missing=*/ true,
"override the auto-detected signal type (power | gnd | other)",
};
commands["set-type"] = {
{{"module", Completion::None},
{"part (name or pattern)", Completion::None},
@@ -205,6 +267,8 @@ void Tui::RegisterCommands() {
return;
}
prt->connector_type = args[2];
for (auto &kv : *prt)
kv.second->expected_signal_type = pin_role(args[2], kv.first);
Print(mod->name + "/" + prt->name + ": connector_type = "
+ (args[2].empty() ? "(none)" : args[2]));
},

View File

@@ -106,7 +106,7 @@ Component Tui::BuildExploreScreen() {
std::vector<std::pair<std::string, std::string>> rows;
for (auto &pin_kv : *p) {
Signal *s = pin_kv.second->signal();
rows.emplace_back(pin_kv.first, s ? s->name : std::string(""));
rows.emplace_back(pin_kv.first, s ? s->name : std::string("(NC)"));
}
std::sort(rows.begin(), rows.end(),
[](const auto &a, const auto &b) { return NaturalLess(a.first, b.first); });
@@ -123,7 +123,7 @@ Component Tui::BuildExploreScreen() {
Signal *s = cur_mod->signals->get(cname);
explore_header = cur_mod->name + "/" + s->name
+ "" + std::to_string(s->size())
+ " pins";
+ " pins • type: " + signal_type_name(s->type);
std::vector<std::string> rows;
for (auto &pin_kv : *s) {
Pin *pin = pin_kv.second;

View File

@@ -3,6 +3,8 @@
#include "system/modules.hpp"
#include "system/parts.hpp"
#include "system/pin_role.hpp"
#include "system/pins.hpp"
#include "system/system.hpp"
#include "system/transform_vpx.hpp"
@@ -51,6 +53,8 @@ Component Tui::BuildSettypeScreen() {
return;
}
prt->connector_type = settype_type;
for (auto &kv : *prt)
kv.second->expected_signal_type = pin_role(settype_type, kv.first);
std::string msg = mod->name + "/" + prt->name + " = "
+ (settype_type.empty() ? "(none)" : settype_type);
settype_status = "applied: " + msg;

View File

@@ -41,22 +41,6 @@ void Tui::CancelPending() {
Print("(cancelled)");
}
std::vector<std::string> Tui::Tokenize(const std::string &s) {
std::vector<std::string> out;
std::string cur;
bool in_q = false;
for (char c : s) {
if (c == '"') { in_q = !in_q; continue; }
if (!in_q && std::isspace((unsigned char)c)) {
if (!cur.empty()) { out.push_back(std::move(cur)); cur.clear(); }
} else {
cur.push_back(c);
}
}
if (!cur.empty()) out.push_back(std::move(cur));
return out;
}
void Tui::Submit() {
if (!pending.empty()) {
if (input.empty()) { Print("(empty — Esc to cancel)"); return; }

View File

@@ -117,7 +117,6 @@ private:
void LoadHistory();
void AppendHistory(const std::string &cmd);
void Source(const std::string &filename);
static std::vector<std::string> Tokenize(const std::string &s);
// Completion (completion.cpp)
void CompleteCommand(size_t start = 0);

View File

@@ -52,3 +52,19 @@ std::string LongestCommonPrefix(const std::vector<std::string> &v) {
}
return lcp;
}
std::vector<std::string> Tokenize(const std::string &s) {
std::vector<std::string> out;
std::string cur;
bool in_q = false;
for (char c : s) {
if (c == '"') { in_q = !in_q; continue; }
if (!in_q && std::isspace((unsigned char)c)) {
if (!cur.empty()) { out.push_back(std::move(cur)); cur.clear(); }
} else {
cur.push_back(c);
}
}
if (!cur.empty()) out.push_back(std::move(cur));
return out;
}

View File

@@ -14,4 +14,7 @@ bool NaturalLess(const std::string &a, const std::string &b);
std::string LongestCommonPrefix(const std::vector<std::string> &v);
// Whitespace tokeniser with `"…"` quoting (preserved-as-content).
std::vector<std::string> Tokenize(const std::string &s);
#endif // _TUI_HELPERS_HPP_