board-logic.mjs 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. const VERIFIED = "cross_verified";
  2. const POWER_NET = /^(?:GND|AGND|DGND|PGND|EARTH|VCC|VDD|VBAT|PWR|\+)/i;
  3. const RTL_PORT = /^[A-Za-z_][A-Za-z0-9_$]*(?:\[[0-9]+\])?$/;
  4. const RESISTOR_REF = /^r(\d+)$/i;
  5. const RESISTOR_PAD = /^r(\d+)\.(\d+)$/i;
  6. const BALL_PREFIX = /^ball:(.+)$/i;
  7. const NET_PREFIX = /^net:(.+)$/i;
  8. const VALUE_PREFIX = /^value:(.+)$/i;
  9. const ENDPOINT = /^u\d+\.[a-z]+\d+$/i;
  10. const ANNOTATION_RESISTOR = /(?<![A-Za-z0-9.])R(\d+)\b/g;
  11. const VALUE_GRAMMAR = /^(?:\d+(?:\.\d+)?[rRkKmM]\d*|\d+[rRkKmM]|\d+(?:\.\d+)?(?:ohm|ohms|Ω)?)$/i;
  12. const PACKAGE_OR_DNP = /^(?:0603|0805|0402|1206|1210|2010|2512|dnp|nc|n\/?c)$/i;
  13. const CAPABILITY_ALIASES = {
  14. pwm: ["pwm", "pwm_output"],
  15. gpio_output: ["gpio_output", "digital_output"],
  16. gpio_input: ["gpio_input", "digital_input"],
  17. tach: ["tach", "tach_input"],
  18. fan_tach: ["fan_tach", "tach_input"],
  19. presence: ["presence", "presence_input"],
  20. };
  21. function text(value) {
  22. return value == null ? "" : String(value);
  23. }
  24. function normalized(value) {
  25. return text(value).trim().toLocaleLowerCase();
  26. }
  27. function naturalCompare(left, right) {
  28. return text(left).localeCompare(text(right), undefined, {numeric: true, sensitivity: "base"});
  29. }
  30. function capabilities(pin) {
  31. return Array.isArray(pin?.fpga?.capabilities) ? pin.fpga.capabilities.map(text) : [];
  32. }
  33. function baseBlockers(pin) {
  34. const blockers = [];
  35. if (pin?.evidence_tier !== VERIFIED || pin?.fpga?.evidence_tier !== VERIFIED) {
  36. blockers.push("unverified");
  37. }
  38. if (!pin?.fpga?.ball || !pin?.fpga?.io_standard) blockers.push("missing_fpga_assignment");
  39. if (pin?.mechanical === true || pin?.electrical !== true) blockers.push("mechanical_or_non_electrical");
  40. if (pin?.pad_role !== "contact") blockers.push("not_connector_contact");
  41. if (["power", "ground"].includes(pin?.function_category) || POWER_NET.test(text(pin?.net))) {
  42. blockers.push("power_or_ground");
  43. }
  44. if (pin?.reserved === true) blockers.push("reserved");
  45. if (pin?.fixed_load_conflict === true) blockers.push("fixed_load_conflict");
  46. if (["", "unknown"].includes(normalized(pin?.external_level))) blockers.push("external_level_unknown");
  47. return blockers;
  48. }
  49. export function connectorPinGroups(touchpoint) {
  50. const pins = Array.isArray(touchpoint?.pins) ? touchpoint.pins : [];
  51. const connector = touchpoint?.connector;
  52. const reviewed = connector?.review_status === VERIFIED;
  53. const grid = reviewed && Array.isArray(connector?.contact_grid) ? connector.contact_grid : [];
  54. const byNumber = new Map(
  55. pins
  56. .filter((pin) => pin?.pad_role === "contact" && text(pin?.number))
  57. .map((pin) => [text(pin.number), pin]),
  58. );
  59. const contactRows = grid.map((row) =>
  60. (Array.isArray(row) ? row : []).map((number) => byNumber.get(text(number))).filter(Boolean),
  61. );
  62. return {
  63. contactRows,
  64. contacts: contactRows.flat(),
  65. auxiliary: pins.filter((pin) => pin?.pad_role !== "contact"),
  66. };
  67. }
  68. function pinSearchFields(touchpoint, pin, boardVersion = "v2_0") {
  69. const programming = pin?.programming ?? {};
  70. const logical = resolveLogicalPin(touchpoint, pin, boardVersion);
  71. return [
  72. ["pin", `${touchpoint.reference}.${pin.number}`],
  73. ["reference", touchpoint.reference],
  74. ["pin_number", pin.number],
  75. ["net", pin.net],
  76. ["summary", pin.summary],
  77. ["function", pin.function_category],
  78. ["direction", pin.direction],
  79. ["external_level", pin.external_level],
  80. ["fpga_ball", pin.fpga?.ball],
  81. ["board_signal", pin.fpga?.board_signal],
  82. ["capability", capabilities(pin).join(" ")],
  83. ["mcu_device", pin.mcu?.device],
  84. ["mcu_family", pin.mcu?.family],
  85. ["mcu_pin", pin.mcu?.device && pin.mcu?.pin ? `${pin.mcu.device}.${pin.mcu.pin}` : ""],
  86. ["mcu_signal", pin.mcu?.signal],
  87. ["mcu_capability", Array.isArray(pin.mcu?.capabilities) ? pin.mcu.capabilities.join(" ") : ""],
  88. ["path", Array.isArray(pin.path) ? pin.path.join(" ") : ""],
  89. ["programming_conclusion", programming.conclusion],
  90. ["network_explanation", programming.network_explanation],
  91. ["hdl_guidance", programming.hdl_guidance],
  92. ["connection_guidance", Array.isArray(programming.connection_guidance) ? programming.connection_guidance.join(" ") : ""],
  93. ["programming_constraints", Array.isArray(programming.constraints) ? programming.constraints.join(" ") : ""],
  94. ["programming_risks", Array.isArray(programming.risks) ? programming.risks.join(" ") : ""],
  95. ["peripheral_tag", Array.isArray(logical?.tags) ? logical.tags.join(" ") : ""],
  96. ["logical_pin", logical?.displayBadge],
  97. ["peripheral_role", logical?.peripheralRole],
  98. ["pin_definition", logical?.pinDefinition],
  99. ];
  100. }
  101. /**
  102. * Safe reference / identity match: exact, or prefix only when the next char is not a digit.
  103. * Prevents R12 ⊆ R1225 false positives.
  104. */
  105. export function identityMatch(candidate, query) {
  106. const value = normalized(candidate);
  107. const needle = normalized(query);
  108. if (!value || !needle) return 0;
  109. if (value === needle) return 3;
  110. if (value.startsWith(needle)) {
  111. const next = value.charAt(needle.length);
  112. if (!next || !/\d/.test(next)) return 2;
  113. return 0;
  114. }
  115. return 0;
  116. }
  117. function fieldScore(field, value, query, {allowIncludes = true} = {}) {
  118. const candidate = normalized(value);
  119. if (!candidate) return 0;
  120. if (field === "peripheral_tag") {
  121. const tokens = candidate.split(/\s+/).filter(Boolean);
  122. if (tokens.includes(query)) return 960;
  123. return 0;
  124. }
  125. const weights = {
  126. pin: 1200,
  127. reference: 1000,
  128. resistor: 1600,
  129. resistor_pad: 1500,
  130. value: 1400,
  131. fpga_ball: 900,
  132. mcu_pin: 900,
  133. mcu_signal: 840,
  134. mcu_device: 800,
  135. mcu_family: 780,
  136. mcu_capability: 720,
  137. net: 850,
  138. board_signal: 820,
  139. function: 760,
  140. logical_pin: 740,
  141. capability: 720,
  142. peripheral_role: 710,
  143. pin_number: 620,
  144. pin_definition: 520,
  145. summary: 500,
  146. path: 460,
  147. programming_conclusion: 700,
  148. network_explanation: 610,
  149. hdl_guidance: 590,
  150. connection_guidance: 570,
  151. programming_constraints: 540,
  152. programming_risks: 480,
  153. external_level: 420,
  154. direction: 360,
  155. };
  156. const weight = weights[field] ?? 200;
  157. if (field === "reference" || field === "resistor" || field === "resistor_pad" || field === "pin") {
  158. const grade = identityMatch(candidate, query);
  159. if (grade === 3) return weight;
  160. if (grade === 2) return weight - 120;
  161. return 0;
  162. }
  163. if (field === "fpga_ball") {
  164. // Identifiers: exact / token boundary only — never bare includes.
  165. if (candidate === query) return weight;
  166. return 0;
  167. }
  168. if (candidate === query) return weight;
  169. if (candidate.startsWith(query)) return weight - 120;
  170. if (allowIncludes && candidate.includes(query)) return weight - 240;
  171. return 0;
  172. }
  173. function resultId(type, reference, pinNumber = null) {
  174. return pinNumber == null ? `${type}:${reference}` : `${type}:${reference}.${pinNumber}`;
  175. }
  176. export function buildBoardIndex(data) {
  177. const resistorByReference = new Map();
  178. const resistorPadById = new Map();
  179. const valueTokenIndex = new Map();
  180. const netMembers = new Map();
  181. const relatedByPinId = new Map();
  182. const pushNet = (net, member) => {
  183. const key = text(net);
  184. if (!key) return;
  185. if (!netMembers.has(key)) netMembers.set(key, []);
  186. netMembers.get(key).push(member);
  187. };
  188. for (const resistor of data?.resistors ?? []) {
  189. const reference = text(resistor.reference);
  190. resistorByReference.set(reference, resistor);
  191. resistorByReference.set(normalized(reference), resistor);
  192. for (const token of resistor.value_tokens ?? []) {
  193. const key = normalized(token);
  194. if (!key) continue;
  195. if (!valueTokenIndex.has(key)) valueTokenIndex.set(key, []);
  196. valueTokenIndex.get(key).push(resistor);
  197. }
  198. for (const pad of resistor.pads ?? []) {
  199. const padId = text(pad.id) || `${reference}.pad-${pad.number}`;
  200. resistorPadById.set(padId, {resistor, pad});
  201. resistorPadById.set(`${reference}.${pad.number}`, {resistor, pad});
  202. pushNet(pad.net, {
  203. kind: "resistor_pad",
  204. reference,
  205. pinNumber: text(pad.number),
  206. touchpoint: null,
  207. resistor,
  208. pin: pad,
  209. });
  210. }
  211. }
  212. for (const touchpoint of data?.touchpoints ?? []) {
  213. for (const pin of touchpoint.pins ?? []) {
  214. pushNet(pin.net, {
  215. kind: "touchpoint",
  216. reference: touchpoint.reference,
  217. pinNumber: text(pin.number),
  218. touchpoint,
  219. resistor: null,
  220. pin,
  221. });
  222. if (Array.isArray(pin.related_resistors) && pin.related_resistors.length) {
  223. relatedByPinId.set(text(pin.id), pin.related_resistors);
  224. }
  225. }
  226. }
  227. for (const members of netMembers.values()) {
  228. members.sort((left, right) => {
  229. const referenceOrder = naturalCompare(left.reference, right.reference);
  230. return referenceOrder || naturalCompare(left.pinNumber, right.pinNumber);
  231. });
  232. }
  233. return {
  234. resistorByReference,
  235. resistorPadById,
  236. valueTokenIndex,
  237. netMembers,
  238. relatedByPinId,
  239. evidenceConflicts: Array.isArray(data?.evidence_conflicts) ? data.evidence_conflicts : [],
  240. };
  241. }
  242. function looksLikeResistanceToken(raw) {
  243. const needle = text(raw).trim();
  244. if (!needle) return false;
  245. if (PACKAGE_OR_DNP.test(needle)) return false;
  246. if (!VALUE_GRAMMAR.test(needle)) return false;
  247. if (/^\d{1,2}$/.test(needle)) return false; // short bare numbers need value: or 22R
  248. // Bare package-sized integers like 0805 already rejected; reject 4-digit bare
  249. // numbers that look like imperial package codes when no unit suffix present.
  250. if (/^\d{4}$/.test(needle) && !/[rRkKmM]|ohm|Ω/i.test(needle)) return false;
  251. return true;
  252. }
  253. /** Resolve exact net name from board index; case-sensitive first, then CI. */
  254. export function resolveExactNetName(data, query) {
  255. const raw = text(query).trim();
  256. if (!raw) return null;
  257. const index = buildBoardIndex(data);
  258. if (index.netMembers.has(raw)) return raw;
  259. const needle = normalized(raw);
  260. for (const name of index.netMembers.keys()) {
  261. if (normalized(name) === needle) return name;
  262. }
  263. return null;
  264. }
  265. export function parseBoardQuery(query) {
  266. const raw = text(query).trim();
  267. const needle = normalized(raw);
  268. if (!needle) return {kind: "empty", raw, needle};
  269. const ball = BALL_PREFIX.exec(raw);
  270. if (ball) return {kind: "ball", raw, needle: normalized(ball[1]), value: ball[1].trim()};
  271. const net = NET_PREFIX.exec(raw);
  272. if (net) return {kind: "net", raw, needle: normalized(net[1]), value: net[1].trim()};
  273. const value = VALUE_PREFIX.exec(raw);
  274. if (value) return {kind: "value", raw, needle: normalized(value[1]), value: value[1].trim()};
  275. const pad = RESISTOR_PAD.exec(needle);
  276. if (pad) {
  277. return {
  278. kind: "resistor_pad",
  279. raw,
  280. needle,
  281. reference: `R${pad[1]}`,
  282. padNumber: pad[2],
  283. };
  284. }
  285. const ref = RESISTOR_REF.exec(needle);
  286. if (ref) {
  287. return {
  288. kind: "resistor_ref",
  289. raw,
  290. needle,
  291. reference: `R${ref[1]}`,
  292. };
  293. }
  294. if (ENDPOINT.test(needle)) {
  295. return {kind: "endpoint", raw, needle, value: raw};
  296. }
  297. if (looksLikeResistanceToken(raw)) {
  298. return {kind: "value", raw, needle, value: raw};
  299. }
  300. return {kind: "text", raw, needle};
  301. }
  302. function sortResults(results) {
  303. const typeOrder = {
  304. resistor: 0,
  305. resistor_pad: 1,
  306. evidence_conflict: 2,
  307. touchpoint: 3,
  308. pin: 4,
  309. };
  310. return results.sort((left, right) => {
  311. if (right.score !== left.score) return right.score - left.score;
  312. const leftType = typeOrder[left.type] ?? 9;
  313. const rightType = typeOrder[right.type] ?? 9;
  314. if (leftType !== rightType) return leftType - rightType;
  315. const referenceOrder = naturalCompare(left.reference, right.reference);
  316. if (referenceOrder) return referenceOrder;
  317. return naturalCompare(left.pinNumber, right.pinNumber);
  318. });
  319. }
  320. function searchTouchpoints(data, needle, {ballOnly = false, endpoint = null} = {}) {
  321. const results = [];
  322. const boardVersion = normalizeBoardVersion(null, data);
  323. for (const touchpoint of data?.touchpoints ?? []) {
  324. if (!ballOnly && !endpoint) {
  325. const touchpointFields = [
  326. ["reference", touchpoint.reference],
  327. ["summary", touchpoint.summary],
  328. ["function", touchpoint.category],
  329. ];
  330. let touchpointScore = 0;
  331. let touchpointField = "";
  332. for (const [field, value] of touchpointFields) {
  333. const score = fieldScore(field, value, needle);
  334. if (score > touchpointScore) {
  335. touchpointScore = score;
  336. touchpointField = field;
  337. }
  338. }
  339. if (touchpointScore) {
  340. results.push({
  341. id: resultId("touchpoint", touchpoint.reference),
  342. type: "touchpoint",
  343. reference: touchpoint.reference,
  344. pinNumber: null,
  345. touchpoint,
  346. resistor: null,
  347. pin: null,
  348. matchedField: touchpointField,
  349. score: touchpointScore + 20,
  350. });
  351. }
  352. }
  353. for (const pin of touchpoint.pins ?? []) {
  354. let score = 0;
  355. let matchedField = "";
  356. if (ballOnly) {
  357. const ballScore = fieldScore("fpga_ball", pin.fpga?.ball, needle, {allowIncludes: false});
  358. if (!ballScore) continue;
  359. score = ballScore;
  360. matchedField = "fpga_ball";
  361. } else if (endpoint) {
  362. const endpointNeedle = normalized(endpoint);
  363. const candidates = [
  364. ["fpga_ball", `u3.${text(pin.fpga?.ball)}`],
  365. ["path", Array.isArray(pin.path) ? pin.path.join(" ") : ""],
  366. ["board_signal", pin.fpga?.board_signal],
  367. ["pin", `${touchpoint.reference}.${pin.number}`],
  368. ];
  369. for (const [field, value] of candidates) {
  370. const candidate = normalized(value);
  371. if (!candidate) continue;
  372. if (candidate === endpointNeedle || candidate.split(/[^a-z0-9_.]+/).includes(endpointNeedle)) {
  373. const candidateScore = field === "fpga_ball" ? 950 : 700;
  374. if (candidateScore > score) {
  375. score = candidateScore;
  376. matchedField = field;
  377. }
  378. }
  379. }
  380. if (!score) continue;
  381. } else {
  382. for (const [field, value] of pinSearchFields(touchpoint, pin, boardVersion)) {
  383. const candidateScore = fieldScore(field, value, needle, {
  384. allowIncludes: field !== "fpga_ball" && field !== "reference" && field !== "pin",
  385. });
  386. if (candidateScore > score) {
  387. score = candidateScore;
  388. matchedField = field;
  389. }
  390. }
  391. if (!score) continue;
  392. if (baseBlockers(pin).length === 0) score += 40;
  393. }
  394. results.push({
  395. id: resultId("pin", touchpoint.reference, text(pin.number)),
  396. type: "pin",
  397. reference: touchpoint.reference,
  398. pinNumber: text(pin.number),
  399. touchpoint,
  400. resistor: null,
  401. pin,
  402. matchedField,
  403. score,
  404. });
  405. }
  406. }
  407. return results;
  408. }
  409. function pushNetMemberResults(results, members, score = 1400) {
  410. for (const member of members) {
  411. const isResistor = member.kind === "resistor_pad";
  412. results.push({
  413. id: resultId(isResistor ? "resistor_pad" : "pin", member.reference, member.pinNumber),
  414. type: isResistor ? "resistor_pad" : "pin",
  415. reference: member.reference,
  416. pinNumber: member.pinNumber,
  417. touchpoint: member.touchpoint,
  418. resistor: member.resistor,
  419. pin: member.pin,
  420. matchedField: "net",
  421. score,
  422. assemblyStatus: member.resistor?.assembly_status || null,
  423. });
  424. }
  425. }
  426. export function searchBoard(data, query) {
  427. let parsed = parseBoardQuery(query);
  428. if (parsed.kind === "empty") return [];
  429. const index = buildBoardIndex(data);
  430. let results = [];
  431. // Bare string that exactly equals an existing net → exact net query (same as net:).
  432. if (parsed.kind === "text") {
  433. const exactNet = resolveExactNetName(data, parsed.raw);
  434. if (exactNet) {
  435. parsed = {kind: "net", raw: parsed.raw, needle: normalized(exactNet), value: exactNet};
  436. }
  437. }
  438. if (parsed.kind === "resistor_pad") {
  439. const hit = index.resistorPadById.get(`${parsed.reference}.${parsed.padNumber}`);
  440. if (hit) {
  441. results.push({
  442. id: resultId("resistor_pad", parsed.reference, parsed.padNumber),
  443. type: "resistor_pad",
  444. reference: parsed.reference,
  445. pinNumber: parsed.padNumber,
  446. touchpoint: null,
  447. resistor: hit.resistor,
  448. pin: hit.pad,
  449. matchedField: "resistor_pad",
  450. score: 1550,
  451. });
  452. }
  453. return sortResults(results);
  454. }
  455. if (parsed.kind === "resistor_ref") {
  456. const resistor = index.resistorByReference.get(parsed.reference)
  457. || index.resistorByReference.get(normalized(parsed.reference));
  458. if (resistor) {
  459. results.push({
  460. id: resultId("resistor", parsed.reference),
  461. type: "resistor",
  462. reference: parsed.reference,
  463. pinNumber: null,
  464. touchpoint: null,
  465. resistor,
  466. pin: null,
  467. matchedField: "resistor",
  468. score: 1600,
  469. });
  470. }
  471. return sortResults(results);
  472. }
  473. if (parsed.kind === "ball") {
  474. return sortResults(searchTouchpoints(data, parsed.needle, {ballOnly: true}));
  475. }
  476. if (parsed.kind === "endpoint") {
  477. return sortResults(searchTouchpoints(data, parsed.needle, {endpoint: parsed.value}));
  478. }
  479. if (parsed.kind === "net") {
  480. const members =
  481. index.netMembers.get(text(parsed.value))
  482. || index.netMembers.get(parsed.value)
  483. || [];
  484. pushNetMemberResults(results, members, 1400);
  485. if (!results.length) {
  486. for (const [net, rows] of index.netMembers.entries()) {
  487. if (normalized(net) !== parsed.needle) continue;
  488. pushNetMemberResults(results, rows, 1400);
  489. }
  490. }
  491. return sortResults(results);
  492. }
  493. if (parsed.kind === "value") {
  494. const seen = new Set();
  495. const addResistor = (resistor, score = 1400) => {
  496. const reference = text(resistor.reference);
  497. if (seen.has(reference)) return;
  498. seen.add(reference);
  499. const notFitted = resistor.assembly_status === "not_fitted";
  500. results.push({
  501. id: resultId("resistor", reference),
  502. type: "resistor",
  503. reference,
  504. pinNumber: null,
  505. touchpoint: null,
  506. resistor,
  507. pin: null,
  508. matchedField: "value",
  509. score: notFitted ? score - 200 : score,
  510. assemblyStatus: resistor.assembly_status || "unknown",
  511. notFitted,
  512. });
  513. };
  514. for (const resistor of index.valueTokenIndex.get(parsed.needle) ?? []) {
  515. addResistor(resistor, 1450);
  516. }
  517. for (const resistor of data?.resistors ?? []) {
  518. for (const token of resistor.value_tokens ?? []) {
  519. if (normalized(token) === parsed.needle) addResistor(resistor, 1450);
  520. }
  521. if (normalized(resistor.component_value) === parsed.needle) addResistor(resistor, 1400);
  522. }
  523. return sortResults(results);
  524. }
  525. // Free-text (non-exact-net): touchpoints/pins; value tokens; never bare R# here.
  526. results = searchTouchpoints(data, parsed.needle);
  527. for (const resistor of index.valueTokenIndex.get(parsed.needle) ?? []) {
  528. results.push({
  529. id: resultId("resistor", resistor.reference),
  530. type: "resistor",
  531. reference: resistor.reference,
  532. pinNumber: null,
  533. touchpoint: null,
  534. resistor,
  535. pin: null,
  536. matchedField: "value",
  537. score: resistor.assembly_status === "not_fitted" ? 1100 : 1300,
  538. assemblyStatus: resistor.assembly_status || "unknown",
  539. notFitted: resistor.assembly_status === "not_fitted",
  540. });
  541. }
  542. for (const conflict of index.evidenceConflicts) {
  543. const hay = normalized([
  544. conflict.subject_reference,
  545. conflict.summary,
  546. ...(conflict.claims || []).map((claim) => JSON.stringify(claim)),
  547. ].join(" "));
  548. if (hay.includes(parsed.needle) && conflict.status === "unresolved") {
  549. results.push({
  550. id: resultId("evidence_conflict", conflict.subject_reference || conflict.id),
  551. type: "evidence_conflict",
  552. reference: conflict.subject_reference || conflict.id,
  553. pinNumber: null,
  554. touchpoint: null,
  555. resistor: index.resistorByReference.get(conflict.subject_reference) || null,
  556. pin: null,
  557. matchedField: "evidence_conflict",
  558. score: 200,
  559. evidenceConflict: conflict,
  560. });
  561. }
  562. }
  563. return sortResults(results);
  564. }
  565. /** Compatibility: touchpoint pins only (does not include resistor pads). */
  566. export function sameNetPins(data, net) {
  567. const wanted = text(net);
  568. if (!wanted) return [];
  569. const matches = [];
  570. for (const touchpoint of data?.touchpoints ?? []) {
  571. for (const pin of touchpoint.pins ?? []) {
  572. if (pin.net === wanted) {
  573. matches.push({
  574. kind: "touchpoint",
  575. reference: touchpoint.reference,
  576. pinNumber: text(pin.number),
  577. touchpoint,
  578. resistor: null,
  579. pin,
  580. });
  581. }
  582. }
  583. }
  584. return matches.sort((left, right) => {
  585. const referenceOrder = naturalCompare(left.reference, right.reference);
  586. return referenceOrder || naturalCompare(left.pinNumber, right.pinNumber);
  587. });
  588. }
  589. /** Touchpoint pins + resistor pads sharing the PCB net. */
  590. export function sameNetMembers(data, net) {
  591. const wanted = text(net);
  592. if (!wanted) return [];
  593. const index = buildBoardIndex(data);
  594. const members = index.netMembers.get(wanted) || [];
  595. if (members.length) return members.map((item) => ({...item}));
  596. // Case-insensitive fallback
  597. for (const [name, rows] of index.netMembers.entries()) {
  598. if (normalized(name) === normalized(wanted)) {
  599. return rows.map((item) => ({...item}));
  600. }
  601. }
  602. return [];
  603. }
  604. export function extractPathResistors(data, ...texts) {
  605. const index = new Map((data?.resistors ?? []).map((item) => [text(item.reference), item]));
  606. const ordered = [];
  607. const seen = new Set();
  608. for (const value of texts) {
  609. const chunks = Array.isArray(value) ? value : [value];
  610. for (const chunk of chunks) {
  611. const source = text(chunk);
  612. if (!source) continue;
  613. ANNOTATION_RESISTOR.lastIndex = 0;
  614. let match;
  615. while ((match = ANNOTATION_RESISTOR.exec(source))) {
  616. const reference = `R${match[1]}`;
  617. if (seen.has(reference)) continue;
  618. seen.add(reference);
  619. const resistor = index.get(reference);
  620. if (!resistor) {
  621. throw new Error(`annotation resistor ${reference} missing from resistor index`);
  622. }
  623. ordered.push(resistor);
  624. }
  625. }
  626. }
  627. return ordered;
  628. }
  629. export function findCandidates(data, capability) {
  630. const wanted = normalized(capability);
  631. if (!wanted) return [];
  632. const acceptedCapabilities = new Set(
  633. (CAPABILITY_ALIASES[wanted] ?? [wanted]).map(normalized),
  634. );
  635. const candidates = [];
  636. for (const touchpoint of data?.touchpoints ?? []) {
  637. for (const pin of touchpoint.pins ?? []) {
  638. const pinCapabilities = capabilities(pin).map(normalized);
  639. if (!pinCapabilities.some((item) => acceptedCapabilities.has(item))) continue;
  640. if (wanted === "gpio" && !["gpio", "direct_gpio"].includes(normalized(pin.function_category))) {
  641. continue;
  642. }
  643. if (baseBlockers(pin).length) continue;
  644. const purpose = text(pin.summary).trim();
  645. const polarity = normalized(pin.polarity);
  646. const details = [];
  647. if (purpose) details.push(`用途 ${purpose}`);
  648. if (polarity && polarity !== "unknown") details.push(`极性 ${pin.polarity}`);
  649. candidates.push({
  650. reference: touchpoint.reference,
  651. touchpoint,
  652. pin,
  653. reason: `${details.length ? `${details.join(";")};` : ""}能力 ${capability} 匹配;已验证 FPGA 路径至 ${pin.fpga.ball};外部电平 ${pin.external_level}。`,
  654. warning: "只证明物理可用性,仍需检查当前工程约束冲突。",
  655. });
  656. }
  657. }
  658. return candidates.sort((left, right) => {
  659. const categoryMatchLeft = normalized(left.pin.function_category) === wanted ? 1 : 0;
  660. const categoryMatchRight = normalized(right.pin.function_category) === wanted ? 1 : 0;
  661. if (categoryMatchRight !== categoryMatchLeft) return categoryMatchRight - categoryMatchLeft;
  662. const referenceOrder = naturalCompare(left.reference, right.reference);
  663. return referenceOrder || naturalCompare(left.pin.number, right.pin.number);
  664. });
  665. }
  666. export function isValidRtlPortName(name) {
  667. return RTL_PORT.test(text(name));
  668. }
  669. /** Back SVG export uses KiCad --mirror about the vertical/Y axis (left-right flip). */
  670. export const BACK_MIRROR_AXIS = "y";
  671. export function mirrorBoardX(x, boardWidth) {
  672. const width = Number(boardWidth);
  673. const value = Number(x);
  674. if (!Number.isFinite(width) || !Number.isFinite(value)) return value;
  675. return width - value;
  676. }
  677. export function displayPoint(point, boardWidth, viewingSide) {
  678. const x = Number(point?.x);
  679. const y = Number(point?.y);
  680. if (viewingSide !== "back") return {x, y};
  681. return {x: mirrorBoardX(x, boardWidth), y};
  682. }
  683. export function displayBounds(bounds, boardWidth, viewingSide) {
  684. const x = Number(bounds?.x);
  685. const y = Number(bounds?.y);
  686. const width = Number(bounds?.width);
  687. const height = Number(bounds?.height);
  688. if (viewingSide !== "back") {
  689. return {x, y, width, height};
  690. }
  691. return {
  692. x: mirrorBoardX(x + width, boardWidth),
  693. y,
  694. width,
  695. height,
  696. };
  697. }
  698. export function itemVisibleOnSide(item, viewingSide) {
  699. return (item?.side || "front") === viewingSide;
  700. }
  701. export function generateAdcSnippet(pin, rtlPortName, runtimeStatus = {}) {
  702. if (runtimeStatus.adcCopyEnabled !== true) {
  703. throw new Error("源数据已过期,ADC 复制已关闭。");
  704. }
  705. if (!isValidRtlPortName(rtlPortName)) {
  706. throw new Error("生成 ADC 前请输入合法的 RTL 端口名。");
  707. }
  708. if (baseBlockers(pin).length) {
  709. throw new Error("此针脚不具备 ADC 生成资格。");
  710. }
  711. const ball = text(pin.fpga.ball);
  712. const ioStandard = text(pin.fpga.io_standard);
  713. if (!/^[A-Z]+[0-9]+$/.test(ball) || !/^[A-Za-z0-9_.+-]+$/.test(ioStandard)) {
  714. throw new Error("已验证 FPGA 分配包含不安全的 ADC 值。");
  715. }
  716. const assignments = [`LOCATION = ${ball}`, `IOSTANDARD = ${ioStandard}`];
  717. if (pin.adc_pulltype != null) {
  718. const pulltype = text(pin.adc_pulltype);
  719. if (!/^[A-Z][A-Z0-9_]*$/.test(pulltype)) {
  720. throw new Error("人工注释的 PULLTYPE 包含不安全值。");
  721. }
  722. assignments.push(`PULLTYPE = ${pulltype}`);
  723. }
  724. return `set_pin_assignment { ${rtlPortName} } { ${assignments.join("; ")}; }`;
  725. }
  726. export const PERIPHERAL_TYPE_ORDER = [
  727. "ALL",
  728. "PTC",
  729. "FAN",
  730. "UART",
  731. "JTAG",
  732. "MOTOR",
  733. "LIMIT",
  734. "TEMP",
  735. "SSR",
  736. "GPIO",
  737. "MCU",
  738. "POWER",
  739. ];
  740. export const PERIPHERAL_TYPE_LABELS = {
  741. ALL: "全部外设",
  742. PTC: "PTC 加热",
  743. FAN: "FAN 风扇",
  744. UART: "UART 串口",
  745. JTAG: "JTAG / SWD",
  746. MOTOR: "步进电机",
  747. LIMIT: "限位开关",
  748. TEMP: "温度探头",
  749. SSR: "SSR 继电器",
  750. GPIO: "GPIO 扩展",
  751. MCU: "MCU 接口",
  752. POWER: "电源 / 测点",
  753. };
  754. const V12_V11_PTC_CONNECTOR_MAP = {
  755. "CON18.1": {tag: "PTC1", idx: 0, ball: "F16", heat: "HEAT1", v11Role: "PTC1 (通道0)"},
  756. "CON18.3": {tag: "PTC2", idx: 1, ball: "G16", heat: "HEAT2", v11Role: "PTC2 (通道1)"},
  757. "CON18.5": {tag: "PTC3", idx: 2, ball: "T4", heat: "HEAT3", v11Role: "PTC3 (通道2)"},
  758. "CON18.7": {tag: "PTC4", idx: 3, ball: "R5", heat: "HEAT4", v11Role: "PTC4 (通道3)"},
  759. "CON18.9": {tag: "PTC5", idx: 4, ball: "T6", heat: "HEAT5", v11Role: "PTC5 (通道4)"},
  760. "CON18.11": {tag: "PTC6", idx: 5, ball: "R7", heat: "HEAT6", v11Role: "PTC6 (通道5)"},
  761. "CON18.13": {tag: "PTC6", idx: 5, ball: "R7", heat: "HEAT6", v11Role: "PTC6 (通道5 并联针)"},
  762. "CON27.1": {tag: "PTC7", idx: 6, ball: "M1", heat: "HEAT7", v11Role: "PTC7 (通道6)"},
  763. "CON27.3": {tag: "PTC8", idx: 7, ball: "N1", heat: "HEAT8", v11Role: "PTC8 (通道7)"},
  764. "CON27.5": {tag: "PTC9", idx: 8, ball: "P1", heat: "HEAT9", v11Role: "PTC9 (通道8)"},
  765. "CON27.7": {tag: "PTC10", idx: 9, ball: "R1", heat: "HEAT10", v11Role: "PTC10 (通道9)"},
  766. "CON27.9": {tag: "PTC11", idx: 10, ball: "T2", heat: "HEAT11", v11Role: "PTC11 (通道10)"},
  767. "CON27.11": {tag: "PTC12", idx: 11, ball: "R3", heat: "HEAT12", v11Role: "PTC12 (通道11)"},
  768. "CON27.13": {tag: "PTC12", idx: 11, ball: "R3", heat: "HEAT12", v11Role: "PTC12 (通道11 并联针)"},
  769. "CON28.1": {tag: "PTC13", idx: 12, ball: "P16", heat: "HEAT13", v11Role: "PTC13 (通道12)"},
  770. "CON28.3": {tag: "PTC14", idx: 13, ball: "R16", heat: "HEAT14", v11Role: "PTC14 (通道13)"},
  771. "CON28.5": {tag: "PTC15", idx: 14, ball: "M16", heat: "HEAT15", v11Role: "PTC15 (通道14)"},
  772. "CON28.7": {tag: "PTC16", idx: 15, ball: "N16", heat: "HEAT16", v11Role: "PTC16 (通道15)"},
  773. "CON28.9": {tag: "PTC17", idx: 16, ball: "P8", heat: "HEAT17", v11Role: "PTC17 (通道16)"},
  774. "CON28.11": {tag: "PTC18", idx: 17, ball: "P7", heat: "HEAT18", v11Role: "PTC18 (通道17)"},
  775. "CON28.13": {tag: "PTC18", idx: 17, ball: "P7", heat: "HEAT18", v11Role: "PTC18 (通道17 并联针)"},
  776. "CON29.1": {tag: "PTC19", idx: 18, ball: "T9", heat: "HEAT19", v11Role: "PTC19 (通道18)"},
  777. "CON29.3": {tag: "PTC20", idx: 19, ball: "P9", heat: "HEAT20", v11Role: "PTC20 (通道19)"},
  778. "CON29.5": {tag: "PTC21", idx: 20, ball: "P10", heat: "HEAT21", v11Role: "PTC21 (通道20)"},
  779. "CON29.7": {tag: "PTC22", idx: 21, ball: "R10", heat: "HEAT22", v11Role: "PTC22 (通道21)"},
  780. "CON29.9": {tag: "PTC23", idx: 22, ball: "T13", heat: "HEAT23", v11Role: "PTC23 (通道22)"},
  781. "CON29.11": {tag: "PTC24", idx: 23, ball: "T12", heat: "HEAT24", v11Role: "PTC24 (通道23)"},
  782. "CON29.13": {tag: "PTC24", idx: 23, ball: "T12", heat: "HEAT24", v11Role: "PTC24 (通道23 并联针)"},
  783. };
  784. const V20_PTC_CONNECTOR_MAP = {
  785. "CON147.1": {tag: "PTC1", idx: 0, ball: "F16", net: "PTC1", role: "1#腔 PTC1 (通道0)"},
  786. "CON147.3": {tag: "PTC2", idx: 1, ball: "G16", net: "PTC2", role: "1#腔 PTC2 (通道1)"},
  787. "CON147.5": {tag: "PTC3", idx: 2, ball: "T4", net: "PTC3", role: "1#腔 PTC3 (通道2)"},
  788. "CON147.7": {tag: "PTC4", idx: 3, ball: "R5", net: "PTC4", role: "1#腔 PTC4 (通道3)"},
  789. "CON147.9": {tag: "PTC5", idx: 4, ball: "T6", net: "PTC5", role: "1#腔 PTC5 (通道4)"},
  790. "CON147.11": {tag: "PTC6", idx: 5, ball: "R7", net: "PTC6", role: "1#腔 PTC6 (通道5)"},
  791. "CON147.13": {tag: "PTC7_SPARE", idx: 26, ball: "M1", net: "PTC7", role: "1#腔预留 PTC7 (U3.M1 空余)"},
  792. "CON147.15": {tag: "PTC8_SPARE", idx: 27, ball: "N1", net: "PTC8", role: "1#腔预留 PTC8 (U3.N1 空余)"},
  793. "CON147.17": {tag: "PTC9_SPARE", idx: 28, ball: "P1", net: "PTC9", role: "1#腔预留 PTC9 (U3.P1 空余)"},
  794. "CON147.19": {tag: "PTC10_SPARE", idx: 29, ball: "R1", net: "PTC10", role: "1#腔预留 PTC10 (U3.R1 空余)"},
  795. "CON148.1": {tag: "PTC7", altTag: "PTC11", idx: 6, ball: "T2", net: "PTC11", role: "2#腔 PTC11 (逻辑PTC7 / 通道6)"},
  796. "CON148.3": {tag: "PTC8", altTag: "PTC12", idx: 7, ball: "R3", net: "PTC12", role: "2#腔 PTC12 (逻辑PTC8 / 通道7)"},
  797. "CON148.5": {tag: "PTC9", altTag: "PTC13", idx: 8, ball: "P16", net: "PTC13", role: "2#腔 PTC13 (逻辑PTC9 / 通道8)"},
  798. "CON148.7": {tag: "PTC10", altTag: "PTC14", idx: 9, ball: "R16", net: "PTC14", role: "2#腔 PTC14 (逻辑PTC10 / 通道9)"},
  799. "CON148.9": {tag: "PTC11", altTag: "PTC15", idx: 10, ball: "M16", net: "PTC15", role: "2#腔 PTC15 (逻辑PTC11 / 通道10)"},
  800. "CON148.11": {tag: "PTC12", altTag: "PTC16", idx: 11, ball: "N16", net: "PTC16", role: "2#腔 PTC16 (逻辑PTC12 / 通道11)"},
  801. "CON148.13": {tag: "PTC17_SPARE", idx: 30, ball: "P8", net: "PTC17", role: "2#腔预留 PTC17 (U3.P8 空余)"},
  802. "CON148.15": {tag: "PTC18_SPARE", idx: 31, ball: "P7", net: "PTC18", role: "2#腔预留 PTC18 (U3.P7 空余)"},
  803. "CON148.17": {tag: "PTC19_SPARE", idx: 32, ball: "T9", net: "PTC19", role: "2#腔预留 PTC19 (U3.T9 空余)"},
  804. "CON148.19": {tag: "PTC20_SPARE", idx: 33, ball: "P9", net: "PTC20", role: "2#腔预留 PTC20 (U3.P9 空余)"},
  805. "CON149.1": {tag: "PTC13", altTag: "PTC21", idx: 12, ball: "P10", net: "PTC21", role: "3#腔 PTC21 (逻辑PTC13 / 通道12)"},
  806. "CON149.3": {tag: "PTC14", altTag: "PTC22", idx: 13, ball: "R10", net: "PTC22", role: "3#腔 PTC22 (逻辑PTC14 / 通道13)"},
  807. "CON149.5": {tag: "PTC15", altTag: "PTC23", idx: 14, ball: "T13", net: "PTC23", role: "3#腔 PTC23 (逻辑PTC15 / 通道14)"},
  808. "CON149.7": {tag: "PTC16", altTag: "PTC24", idx: 15, ball: "T12", net: "PTC24", role: "3#腔 PTC24 (逻辑PTC16 / 通道15)"},
  809. "CON149.9": {tag: "PTC17", altTag: "PTC25", idx: 16, ball: "L3", net: "PTC25", role: "3#腔 PTC25 (逻辑PTC17 / 通道16)"},
  810. "CON149.11": {tag: "PTC18", altTag: "PTC26", idx: 17, ball: "N2", net: "PTC26", role: "3#腔 PTC26 (逻辑PTC18 / 通道17)"},
  811. "CON149.13": {tag: "PTC27_SPARE", idx: 34, ball: "P2", net: "PTC27", role: "预留 PTC27(当前固件未绑定)"},
  812. "CON149.15": {tag: "PTC28_SPARE", idx: 35, ball: "M3", net: "PTC28", role: "3#腔预留 PTC28 (U3.M3 空余)"},
  813. "CON149.17": {tag: "PTC29_SPARE", idx: 36, ball: "L5", net: "PTC29", role: "3#腔预留 PTC29 (U3.L5 空余)"},
  814. "CON149.19": {tag: "PTC19", altTag: "PTC30", idx: 18, ball: "K5", net: "PTC30", role: "4#腔 PTC30(通道18)"},
  815. "CON150.1": {tag: "PTC20", altTag: "PTC31", idx: 19, ball: "L4", net: "PTC31", role: "4#腔 PTC31(通道19)"},
  816. "CON150.3": {tag: "PTC20", altTag: "PTC31", idx: 19, ball: "L4", net: "PTC31", role: "4#腔 PTC31(通道19)"},
  817. "CON150.5": {tag: "PTC21", altTag: "PTC32", idx: 20, ball: "H4", net: "PTC32", role: "4#腔 PTC32(通道20)"},
  818. "CON150.7": {tag: "PTC21", altTag: "PTC32", idx: 20, ball: "H4", net: "PTC32", role: "4#腔 PTC32(通道20)"},
  819. "CON150.9": {tag: "PTC22", altTag: "PTC33", idx: 21, ball: "J6", net: "PTC33", role: "4#腔 PTC33(通道21)"},
  820. "CON150.11": {tag: "PTC22", altTag: "PTC33", idx: 21, ball: "J6", net: "PTC33", role: "4#腔 PTC33(通道21)"},
  821. "CON150.13": {tag: "PTC23", altTag: "PTC34", idx: 22, ball: "H5", net: "PTC34", role: "4#腔 PTC34(通道22)"},
  822. "CON150.15": {tag: "PTC23", altTag: "PTC34", idx: 22, ball: "H5", net: "PTC34", role: "4#腔 PTC34(通道22)"},
  823. "CON150.17": {tag: "PTC24", altTag: "PTC35", idx: 23, ball: "C15", net: "PTC35", role: "4#腔 PTC35(通道23)"},
  824. "CON150.19": {tag: "PTC24", altTag: "PTC35", idx: 23, ball: "C15", net: "PTC35", role: "4#腔 PTC35(通道23)"},
  825. };
  826. const V12_MOTOR_MAP = {
  827. "CON104": {"axis": 0, "tag": "M1", "stepPin": "2", "stepBall": "C12", "dirPin": "3", "dirBall": "B12", "stepNet": "PUL1", "dirNet": "DIR1"},
  828. "CON105": {"axis": 1, "tag": "M2", "stepPin": "2", "stepBall": "L12", "dirPin": "3", "dirBall": "J11", "stepNet": "PUL2", "dirNet": "DIR2"},
  829. "CON106": {"axis": 2, "tag": "M3", "stepPin": "2", "stepBall": "L14", "dirPin": "3", "dirBall": "K14", "stepNet": "PUL3", "dirNet": "DIR3"},
  830. "CON113": {"axis": 3, "tag": "M4", "stepPin": "2", "stepBall": "G14", "dirPin": "3", "dirBall": "G15", "stepNet": "PUL10", "dirNet": "DIR10"},
  831. "CON107": {"axis": 4, "tag": "M5", "stepPin": "2", "stepBall": "L13", "dirPin": "3", "dirBall": "K11", "stepNet": "PUL4", "dirNet": "DIR4"},
  832. "CON108": {"axis": 5, "tag": "M6", "stepPin": "2", "stepBall": "K12", "dirPin": "3", "dirBall": "K13", "stepNet": "PUL5", "dirNet": "DIR5"},
  833. "CON109": {"axis": 6, "tag": "M7", "stepPin": "2", "stepBall": "J14", "dirPin": "3", "dirBall": "J12", "stepNet": "PUL6", "dirNet": "DIR6"},
  834. "CON114": {"axis": 7, "tag": "M8", "stepPin": "2", "stepBall": "G13", "dirPin": "3", "dirBall": "F14", "stepNet": "PUL11", "dirNet": "DIR11"},
  835. "CON110": {"axis": 8, "tag": "M9", "stepPin": "2", "stepBall": "H13", "dirPin": "3", "dirBall": "J13", "stepNet": "PUL7", "dirNet": "DIR7"},
  836. "CON111": {"axis": 9, "tag": "M10", "stepPin": "2", "stepBall": "H11", "dirPin": "3", "dirBall": "H12", "stepNet": "PUL8", "dirNet": "DIR8"},
  837. "CON112": {"axis": 10, "tag": "M11", "stepPin": "2", "stepBall": "G11", "dirPin": "3", "dirBall": "H14", "stepNet": "PUL9", "dirNet": "DIR9"},
  838. "CON115": {"axis": 11, "tag": "M12", "stepPin": "2", "stepBall": "F12", "dirPin": "3", "dirBall": "D15", "stepNet": "PUL12", "dirNet": "DIR12"},
  839. };
  840. const V12_LIMIT_MAP = {
  841. "CON12.1": {"axis": 0, "tag": "LS1", "rtl": "limit_lower_raw[0]", "ball": "A5", "role": "轴0(M1) 下限位 LS1"},
  842. "CON12.3": {"axis": 1, "tag": "LS2", "rtl": "limit_lower_raw[1]", "ball": "C8", "role": "轴1(M2) 下限位 LS2"},
  843. "CON126.1": {"axis": 2, "tag": "LS3", "rtl": "limit_lower_raw[2]", "ball": "D7", "role": "轴2(M3) 下限位 LS3"},
  844. "CON126.3": {"axis": 3, "tag": "LS4", "rtl": "limit_lower_raw[3]", "ball": "A4", "role": "轴3(M4) 下限位 LS4"},
  845. "CON127.1": {"axis": 4, "tag": "LS5", "rtl": "limit_lower_raw[4]", "ball": "A3", "role": "轴4(M5) 下限位 LS5"},
  846. "CON127.3": {"axis": 5, "tag": "LS6", "rtl": "limit_lower_raw[5]", "ball": "A12", "role": "轴5(M6) 下限位 LS6"},
  847. "CON128.1": {"axis": 6, "tag": "LS7", "rtl": "limit_lower_raw[6]", "ball": "L16", "role": "轴6(M7) 下限位 LS7"},
  848. "CON128.3": {"axis": 7, "tag": "LS8", "rtl": "limit_lower_raw[7]", "ball": "K15", "role": "轴7(M8) 下限位 LS8"},
  849. "CON129.1": {"axis": 8, "tag": "LS9", "rtl": "limit_lower_raw[8]", "ball": "K16", "role": "轴8(M9) 下限位 LS9"},
  850. "CON129.3": {"axis": 9, "tag": "LS10", "rtl": "limit_lower_raw[9]", "ball": "H15", "role": "轴9(M10) 下限位 LS10"},
  851. "CON138.1": {"axis": 10, "tag": "LS11", "rtl": "limit_lower_raw[10]", "ball": "T3", "role": "轴10(M11) 下限位 LS11"},
  852. "CON138.3": {"axis": 11, "tag": "LS12", "rtl": "limit_lower_raw[11]", "ball": "P4", "role": "轴11(M12) 下限位 LS12"},
  853. "CON12.2": {"axis": 0, "tag": "HS1", "rtl": "limit_upper_raw[0]", "ball": "B6", "role": "轴0(M1) 上限位 HS1"},
  854. "CON12.4": {"axis": 1, "tag": "HS2", "rtl": "limit_upper_raw[1]", "ball": "A8", "role": "轴1(M2) 上限位 HS2"},
  855. "CON126.2": {"axis": 2, "tag": "HS3", "rtl": "limit_upper_raw[2]", "ball": "E7", "role": "轴2(M3) 上限位 HS3"},
  856. "CON126.4": {"axis": 3, "tag": "HS4", "rtl": "limit_upper_raw[3]", "ball": "C5", "role": "轴3(M4) 上限位 HS4"},
  857. "CON127.2": {"axis": 4, "tag": "HS5", "rtl": "limit_upper_raw[4]", "ball": "B4", "role": "轴4(M5) 上限位 HS5"},
  858. "CON127.4": {"axis": 5, "tag": "HS6", "rtl": "limit_upper_raw[5]", "ball": "B11", "role": "轴5(M6) 上限位 HS6"},
  859. "CON128.2": {"axis": 6, "tag": "HS7", "rtl": "limit_upper_raw[6]", "ball": "L15", "role": "轴6(M7) 上限位 HS7"},
  860. "CON128.4": {"axis": 7, "tag": "HS8", "rtl": "limit_upper_raw[7]", "ball": "J16", "role": "轴7(M8) 上限位 HS8"},
  861. "CON129.2": {"axis": 8, "tag": "HS9", "rtl": "limit_upper_raw[8]", "ball": "J15", "role": "轴8(M9) 上限位 HS9"},
  862. "CON129.4": {"axis": 9, "tag": "HS10", "rtl": "limit_upper_raw[9]", "ball": "H16", "role": "轴9(M10) 上限位 HS10"},
  863. "CON138.2": {"axis": 10, "tag": "HS11", "rtl": "limit_upper_raw[10]", "ball": "R4", "role": "轴10(M11) 上限位 HS11"},
  864. "CON138.4": {"axis": 11, "tag": "HS12", "rtl": "limit_upper_raw[11]", "ball": "T5", "role": "轴11(M12) 上限位 HS12"},
  865. };
  866. function normalizeBoardVersion(boardVersion, data) {
  867. const v = text(boardVersion).trim().toLowerCase();
  868. if (v === "v2_0" || v === "v2_0_fan" || v === "v1_2" || v === "v1_1") return v;
  869. const id = `${text(data?.board?.id)} ${text(data?.board?.name)}`.toLowerCase();
  870. if (id.includes("fan")) return "v2_0_fan";
  871. if (id.includes("v2")) return "v2_0";
  872. if (id.includes("v1.2") || id.includes("v1_2") || id.includes("20260717")) return "v1_2";
  873. return "v1_1";
  874. }
  875. /**
  876. * Add reviewed project roles to board JSON facts. Inferred paths remain inferred; this is not a continuity measurement.
  877. */
  878. export function resolveLogicalPin(touchpoint, pin, boardVersion = "v2_0") {
  879. const result = describeLogicalPin(touchpoint, pin, boardVersion);
  880. // The connector net remains a board JSON fact, independent of project role labels.
  881. result.boardSignal = text(pin?.net).trim() || result.boardSignal;
  882. const nativeBall = text(pin?.fpga?.ball).trim();
  883. const describedBall = /^U3\.([A-Z]+[0-9]+)$/.exec(result.physicalPin || "")?.[1];
  884. if (nativeBall && describedBall && nativeBall !== describedBall) {
  885. return {
  886. ...result,
  887. physicalPin: `U3.${nativeBall}`,
  888. logicalSignal: "板数据与项目角色表冲突,未绑定角色",
  889. pinDefinition: `板数据标注 U3.${nativeBall};项目角色表预期 U3.${describedBall},请核对当前 ADC 与原生网表。`,
  890. displayBadge: `FPGA U3.${nativeBall} · 映射冲突`,
  891. isUsed: false,
  892. };
  893. }
  894. return result;
  895. }
  896. function describeLogicalPin(touchpoint, pin, boardVersion = "v2_0") {
  897. const ver = normalizeBoardVersion(boardVersion);
  898. const ref = text(touchpoint?.reference).trim();
  899. const pnum = text(pin?.number).trim();
  900. const key = `${ref}.${pnum}`;
  901. const ball = text(pin?.fpga?.ball).trim();
  902. const boardSig = text(pin?.fpga?.board_signal).trim();
  903. const mcuDev = text(pin?.mcu?.device).trim();
  904. const mcuPin = text(pin?.mcu?.pin).trim();
  905. const mcuSig = text(pin?.mcu?.signal).trim();
  906. const net = text(pin?.net).trim();
  907. // Mechanical / Shield pads
  908. if (pin?.mechanical === true || pin?.pad_role === "shield" || pin?.pad_role === "mechanical" || pnum.startsWith("SH")) {
  909. return {
  910. chip: "MECHANICAL",
  911. physicalPin: "SHIELD/GND",
  912. logicalSignal: "GND_SHIELD (外壳固定焊盘)",
  913. boardSignal: net || "GND",
  914. peripheralRole: "机械屏蔽固定脚",
  915. pinDefinition: text(pin?.summary) || `${key} 机械固定与屏蔽接地焊盘`,
  916. displayBadge: "非逻辑管脚 · 机械屏蔽/GND",
  917. tags: ["GND"],
  918. isUsed: false,
  919. };
  920. }
  921. // JTAG (J3 across V1.2 / V1.1 / V2.0)
  922. if (ref === "J3") {
  923. const j3Map = {
  924. "1": {ball: "A7", rtl: "jtag_tck", sig: "FPGA_TCK", def: "FPGA JTAG 时钟输入 (TCK, R63 串联)"},
  925. "3": {ball: "C6", rtl: "jtag_tdo", sig: "FPGA_TDO", def: "FPGA JTAG 数据输出 (TDO, R65 串联)"},
  926. "5": {ball: "B8", rtl: "jtag_tms", sig: "FPGA_TMS", def: "FPGA JTAG 模式选择 (TMS, R67 串联)"},
  927. "9": {ball: "A6", rtl: "jtag_tdi", sig: "FPGA_TDI", def: "FPGA JTAG 数据输入 (TDI, R69 串联)"},
  928. "4": {ball: "", rtl: "VREF_+3V3", sig: "+3V3_VREF", def: "JTAG 下载器目标电平参考 (+3.3V 经 R61/R62)"},
  929. "2": {ball: "", rtl: "GND", sig: "GND", def: "JTAG 信号参考地 (0V GND)"},
  930. "10": {ball: "", rtl: "GND", sig: "GND", def: "JTAG 信号参考地 (0V GND)"},
  931. }[pnum];
  932. if (j3Map?.ball) {
  933. return {
  934. chip: "FPGA U3",
  935. physicalPin: `U3.${j3Map.ball}`,
  936. logicalSignal: `${j3Map.rtl} (${j3Map.sig})`,
  937. boardSignal: j3Map.sig,
  938. peripheralRole: `JTAG ${j3Map.rtl.replace("jtag_", "").toUpperCase()}`,
  939. pinDefinition: j3Map.def,
  940. displayBadge: `FPGA U3.${j3Map.ball} · ${j3Map.rtl} (${j3Map.sig})`,
  941. tags: ["JTAG", j3Map.rtl.toUpperCase()],
  942. isUsed: true,
  943. };
  944. }
  945. return {
  946. chip: pnum === "4" ? "POWER" : (["2", "10"].includes(pnum) ? "GND" : "NC"),
  947. physicalPin: pnum === "4" ? "+3.3V" : (["2", "10"].includes(pnum) ? "GND" : "NC"),
  948. logicalSignal: j3Map ? j3Map.rtl : "NC (空脚)",
  949. boardSignal: net,
  950. peripheralRole: "JTAG 辅助针脚",
  951. pinDefinition: j3Map ? j3Map.def : `${key} JTAG 空脚 (NC)`,
  952. displayBadge: j3Map ? `板级参考 · ${j3Map.rtl}` : "NC · 未连接",
  953. tags: ["JTAG"],
  954. isUsed: true,
  955. };
  956. }
  957. // J25 MCU SWD (V1.2, V1.1, V2.0 - 5-pin header)
  958. if (ref === "J25") {
  959. const j25Map = {
  960. "1": {chip: "GND", phys: "GND", rtl: "GND", def: "板载 MCU SWD 调试参考地 (0V)"},
  961. "2": {chip: "MCU U27", phys: "U27.37", rtl: "PA14 / JTCK_SWCLK", def: "MCU SWD 时钟输入 (SWCLK, 经 R798)"},
  962. "3": {chip: "MCU U27", phys: "U27.34", rtl: "PA13 / JTMS_SWDIO", def: "MCU SWD 双向数据 (SWDIO, 经 R797)"},
  963. "4": {chip: "POWER", phys: "+3.3V", rtl: "VCC_3V3", def: "MCU SWD 目标参考电压 (+3.3V)"},
  964. "5": {chip: "MCU U27", phys: "U27.7", rtl: "NRST (MCU_RST#)", def: "MCU 硬件复位脚 (低有效, 经 R796)"},
  965. };
  966. const entry = j25Map[pnum];
  967. if (entry) {
  968. return {
  969. chip: entry.chip,
  970. physicalPin: entry.phys,
  971. logicalSignal: entry.rtl,
  972. boardSignal: net,
  973. peripheralRole: "MCU SWD 调试",
  974. pinDefinition: entry.def,
  975. displayBadge: `${entry.chip} ${entry.phys} · ${entry.rtl}`,
  976. tags: ["JTAG", "SWD", "MCU"],
  977. isUsed: false,
  978. };
  979. }
  980. }
  981. // Board-specific active peripheral mappings
  982. if (ver === "v2_0") {
  983. if (key === "CON12.3") {
  984. return {
  985. chip: "FPGA U3",
  986. physicalPin: "U3.K2",
  987. logicalSignal: "host_ttl_tx (EX_GPIO1)",
  988. boardSignal: "EX_GPIO1_TX",
  989. peripheralRole: "Host TTL TX (6400 V2.0 主通信口)",
  990. pinDefinition: "CPLD TTL 串口发送端 (3.3V LVCMOS, 经 R1875 0R 到 CON12.3;外部接收端须另接线)",
  991. displayBadge: "FPGA U3.K2 · host_ttl_tx (EX_GPIO1)",
  992. tags: ["UART", "HOST_TTL", "TTL"],
  993. isUsed: true,
  994. };
  995. }
  996. if (key === "CON12.4") {
  997. return {
  998. chip: "FPGA U3",
  999. physicalPin: "U3.J4",
  1000. logicalSignal: "host_ttl_rx (EX_GPIO2)",
  1001. boardSignal: "EX_GPIO2_RX",
  1002. peripheralRole: "Host TTL RX (6400 V2.0 主通信口)",
  1003. pinDefinition: "CPLD TTL 串口接收端 (3.3V LVCMOS, 经 R1876 0R 到 CON12.4;外部发送端须另接线)",
  1004. displayBadge: "FPGA U3.J4 · host_ttl_rx (EX_GPIO2)",
  1005. tags: ["UART", "HOST_TTL", "TTL"],
  1006. isUsed: true,
  1007. };
  1008. }
  1009. if (key === "CON103.1" || key === "CON103.2") {
  1010. const isTx = pnum === "1";
  1011. return {
  1012. chip: "MCU U27 & CP2102 U33",
  1013. physicalPin: isTx ? "U27.12 (PA2)" : "U27.13 (PA3)",
  1014. logicalSignal: isTx ? "MCU_UART1_TX (PA2 / 电气图 TX1)" : "MCU_UART1_RX (PA3 / 电气图 RX1)",
  1015. boardSignal: isTx ? "MCU_UART1_TX_CON" : "MCU_UART1_RX_CON",
  1016. peripheralRole: isTx ? "板载 STM32 MCU 串口 TX (电气图标 -CON103)" : "板载 STM32 MCU 串口 RX (电气图标 -CON103)",
  1017. pinDefinition: isTx
  1018. ? "原生 BRD 网表经 R1029 接板载 STM32 U27.12 (PA2) 及经 R1921 接 CP2102 U33(电气图第4/9页标为 -CON103 TX1 接 -U6-2 P1.26 RX1;直连 FPGA U3.K2 的为 CON12.3)"
  1019. : "原生 BRD 网表经 R1030 接板载 STM32 U27.13 (PA3) 及经 R1920 接 CP2102 U33(电气图第4/9页标为 -CON103 RX1 接 -U6-2 P1.25 TX1;直连 FPGA U3.J4 的为 CON12.4)",
  1020. displayBadge: `MCU U27.${isTx ? "12 (PA2)" : "13 (PA3)"} · ${isTx ? "MCU_UART1_TX" : "MCU_UART1_RX"} [板载MCU]`,
  1021. tags: ["UART", "MCU", "CON103"],
  1022. isUsed: false,
  1023. };
  1024. }
  1025. if (key === "CON134.1" || key === "CON134.2") {
  1026. const isTx = pnum === "1";
  1027. return {
  1028. chip: "FPGA U3",
  1029. physicalPin: isTx ? "U3.T14" : "U3.R13",
  1030. logicalSignal: isTx ? "host_rs232_tx (RS232_TX)" : "host_rs232_rx (RS232_RX)",
  1031. boardSignal: isTx ? "HOST_RS232_TX" : "HOST_RS232_RX",
  1032. peripheralRole: isTx ? "Host RS232 TX" : "Host RS232 RX",
  1033. pinDefinition: isTx ? "Host RS232 发送端 (经 U22 TRSF3232E 电平转换, 921600 8N1)" : "Host RS232 接收端 (经 U22 TRSF3232E 电平转换, 921600 8N1)",
  1034. displayBadge: `FPGA U3.${isTx ? "T14" : "R13"} · ${isTx ? "host_rs232_tx" : "host_rs232_rx"}`,
  1035. tags: ["UART", "HOST_RS232", "RS232"],
  1036. isUsed: true,
  1037. };
  1038. }
  1039. if (key === "CON77.1" || key === "CON77.2") {
  1040. const isTx = pnum === "1";
  1041. return {
  1042. chip: "FPGA U3",
  1043. physicalPin: isTx ? "U3.L10" : "U3.M9",
  1044. logicalSignal: isTx ? "bmc_tx (BMC_TX)" : "bmc_rx (BMC_RX)",
  1045. boardSignal: isTx ? "BMC_RS232_TX" : "BMC_RS232_RX",
  1046. peripheralRole: isTx ? "BMC RS232 TX" : "BMC RS232 RX",
  1047. pinDefinition: isTx ? "BMC 测温轮询串口发送端 (经 U22, 115200 8N1)" : "BMC 测温轮询串口接收端 (经 U22, 115200 8N1)",
  1048. displayBadge: `FPGA U3.${isTx ? "L10" : "M9"} · ${isTx ? "bmc_tx (BMC_TX)" : "bmc_rx (BMC_RX)"}`,
  1049. tags: ["UART", "BMC", "BMC_RS232"],
  1050. isUsed: true,
  1051. };
  1052. }
  1053. if (ref === "CON170" && (pnum === "3" || pnum === "6")) {
  1054. const isTx = pnum === "3";
  1055. return {
  1056. chip: "FPGA U3",
  1057. physicalPin: isTx ? "U3.L10" : "U3.M9",
  1058. logicalSignal: isTx ? "bmc_tx (UART1_TX / RJ45备用)" : "bmc_rx (UART1_RX / RJ45备用)",
  1059. boardSignal: isTx ? "RS232_TX1_RJ45" : "RS232_RX1_RJ45",
  1060. peripheralRole: isTx ? "RJ45 BMC RS232 TX 备用口 (CON170.3)" : "RJ45 BMC RS232 RX 备用口 (CON170.6)",
  1061. pinDefinition: isTx
  1062. ? "RJ45 备用 BMC RS232 发送端 (经 R1898 并联 CON77.1 ↔ U22 ↔ R818 ↔ U3.L10)"
  1063. : "RJ45 备用 BMC RS232 接收端 (经 R1899 并联 CON77.2 ↔ U22 ↔ R819 ↔ U3.M9)",
  1064. displayBadge: `FPGA U3.${isTx ? "L10" : "M9"} · ${isTx ? "bmc_tx" : "bmc_rx"} [RJ45备用]`,
  1065. tags: ["UART", "BMC", "CON170"],
  1066. isUsed: false,
  1067. };
  1068. }
  1069. if (key in V20_PTC_CONNECTOR_MAP) {
  1070. const ptc = V20_PTC_CONNECTOR_MAP[key];
  1071. const rtlPort = ptc.idx < 24 ? `ptc_pwm_out[${ptc.idx}]` : "未绑定输出";
  1072. const tags = ["PTC", ptc.tag];
  1073. if (ptc.altTag) tags.push(ptc.altTag);
  1074. return {
  1075. chip: "FPGA U3",
  1076. physicalPin: `U3.${ptc.ball}`,
  1077. logicalSignal: `${rtlPort} (${ptc.net})`,
  1078. boardSignal: ptc.net,
  1079. peripheralRole: ptc.role,
  1080. pinDefinition: `24V PTC 加热功率输出 (${ptc.role}, 500Hz 高有效 PWM)`,
  1081. displayBadge: `FPGA U3.${ptc.ball} · ${rtlPort} (${ptc.tag}/${ptc.net})`,
  1082. tags,
  1083. isUsed: ptc.idx < 24,
  1084. };
  1085. }
  1086. const v20FanMap = {
  1087. "CON155.5": {tag: "FAN1", alt: "Front0", rtl: "fan_front_out[0]", ball: "P12", role: "1#腔新风/进风双风扇 PWM (Front0)"},
  1088. "CON155.6": {tag: "FAN5", alt: "Rear0", rtl: "fan_rear_out[0]", ball: "A10", role: "1#腔排风/出风单风扇 PWM (Rear0)"},
  1089. "CON155.11": {tag: "FAN2", alt: "Front1", rtl: "fan_front_out[1]", ball: "D8", role: "2#腔新风/进风双风扇 PWM (Front1)"},
  1090. "CON155.12": {tag: "FAN6", alt: "Rear1", rtl: "fan_rear_out[1]", ball: "C7", role: "2#腔排风/出风单风扇 PWM (Rear1)"},
  1091. "CON154.5": {tag: "FAN3", alt: "Front2", rtl: "fan_front_out[2]", ball: "C4", role: "3#腔新风/进风双风扇 PWM (Front2)"},
  1092. "CON154.6": {tag: "FAN7", alt: "Rear2", rtl: "fan_rear_out[2]", ball: "B5", role: "3#腔排风/出风单风扇 PWM (Rear2)"},
  1093. "CON154.11": {tag: "FAN4", alt: "Front3", rtl: "fan_front_out[3]", ball: "B1", role: "4#腔新风/进风双风扇 PWM (Front3)"},
  1094. "CON154.12": {tag: "FAN8", alt: "Rear3", rtl: "fan_rear_out[3]", ball: "A14", role: "4#腔排风/出风单风扇 PWM (Rear3)"},
  1095. };
  1096. if (key in v20FanMap) {
  1097. const f = v20FanMap[key];
  1098. return {
  1099. chip: "FPGA U3",
  1100. physicalPin: `U3.${f.ball}`,
  1101. logicalSignal: `${f.rtl} (${f.alt})`,
  1102. boardSignal: f.alt,
  1103. peripheralRole: f.role,
  1104. pinDefinition: `${f.role} (500Hz PWM;连接器高电平为ON,FPGA球位低电平为ON,经主板 2N7002 NMOS 反相, FAN_BOARD_INVERT_MASK=8'hFF)`,
  1105. displayBadge: `FPGA U3.${f.ball} · ${f.rtl} (${f.alt}/${f.tag})`,
  1106. tags: ["FAN", f.tag, f.alt.toUpperCase()],
  1107. isUsed: true,
  1108. };
  1109. }
  1110. const v20SsrMap = {
  1111. "CON165.1": {tag: "SSR1", rtl: "ssr1_out (ssr_ctrl_out[0])", ball: "F3", role: "SSR1 固态继电器常开控制 (-1-7E 110W/24V)"},
  1112. "CON165.2": {tag: "SSR2", rtl: "ssr2_out (ssr_ctrl_out[1])", ball: "F1", role: "SSR2 固态继电器常开控制 (-2-7E 220W/24V)"},
  1113. "CON165.4": {tag: "SSR4", rtl: "ssr4_out (ssr_ctrl_out[2])", ball: "G4", role: "SSR4 固态继电器常开控制 (-4-7E 110W/24V)"},
  1114. };
  1115. if (key in v20SsrMap) {
  1116. const s = v20SsrMap[key];
  1117. return {
  1118. chip: "FPGA U3",
  1119. physicalPin: `U3.${s.ball}`,
  1120. logicalSignal: `${s.rtl} (${s.tag})`,
  1121. boardSignal: `${s.tag}_CTRL`,
  1122. peripheralRole: s.role,
  1123. pinDefinition: `${s.role} (配置完成后固定输出 1'b1 高电平使能主回路)`,
  1124. displayBadge: `FPGA U3.${s.ball} · ${s.rtl} (${s.tag})`,
  1125. tags: ["SSR", s.tag],
  1126. isUsed: true,
  1127. };
  1128. }
  1129. const v20Con159Map = {
  1130. "CON159.1": {tag: "M1", net: "PUL1 (X1-S)", ball: "C12", role: "1轴 -M1 (1#前部风门) STEP 脉冲 (X1-S → -U1 X-STEP)"},
  1131. "CON159.2": {tag: "M1", net: "DIR1 (X1-D)", ball: "B12", role: "1轴 -M1 (1#前部风门) DIR 方向 (X1-D → -U1 X-DIR)"},
  1132. "CON159.3": {tag: "M2", net: "PUL2 (Y1-S)", ball: "L12", role: "2轴 -M2 (1#后部风门) STEP 脉冲 (Y1-S → -U1 Y-STEP)"},
  1133. "CON159.4": {tag: "M2", net: "DIR2 (Y1-D)", ball: "J11", role: "2轴 -M2 (1#后部风门) DIR 方向 (Y1-D → -U1 Y-DIR)"},
  1134. "CON159.5": {tag: "M3", net: "PUL3 (Z1-S)", ball: "L14", role: "3轴 -M3 (1#顶部风门) STEP 脉冲 (Z1-S → -U1 Z-STEP)"},
  1135. "CON159.6": {tag: "M3", net: "DIR3 (Z1-D)", ball: "K14", role: "3轴 -M3 (1#顶部风门) DIR 方向 (Z1-D → -U1 Z-DIR)"},
  1136. "CON159.7": {tag: "M4", net: "PUL4 (A1-S)", ball: "L13", role: "轴4 -M4 (2#前部风门) STEP 脉冲 (A1-S → -U1 A-STEP)"},
  1137. "CON159.8": {tag: "M4", net: "DIR4 (A1-D)", ball: "K11", role: "轴4 -M4 (2#前部风门) DIR 方向 (A1-D → -U1 A-DIR)"},
  1138. "CON159.9": {tag: "MOTOR_EN", net: "ENA1", ball: "F3", role: "-U1 步进驱动板使能 ENA1 (与 CON165.1 SSR1 共用 U3.F3)"},
  1139. };
  1140. if (key in v20Con159Map) {
  1141. const m = v20Con159Map[key];
  1142. return {
  1143. chip: "FPGA U3",
  1144. physicalPin: `U3.${m.ball}`,
  1145. logicalSignal: `${m.net} (6400 协议空余 / 电气图第3页 -U1)`,
  1146. boardSignal: net || m.net,
  1147. peripheralRole: m.role,
  1148. pinDefinition: `电气图第3/4页 ${m.role} ↔ FPGA U3.${m.ball}(6400 32B 热控协议未启用步进轴)`,
  1149. displayBadge: `FPGA U3.${m.ball} · ${m.net} [第3页风门步进 / 协议空余]`,
  1150. tags: ["MOTOR", m.tag, "CON159"],
  1151. isUsed: false,
  1152. };
  1153. }
  1154. }
  1155. // V2.0 Fan Expansion Board
  1156. if (ver === "v2_0_fan") {
  1157. if (ball) {
  1158. return {
  1159. chip: "主板 FPGA U3 (直通)",
  1160. physicalPin: `U3.${ball}`,
  1161. logicalSignal: `${boardSig} (经 CON168 无源直通)`,
  1162. boardSignal: net,
  1163. peripheralRole: `${ref}.${pnum} 风扇 PWM 信号`,
  1164. pinDefinition: `风扇子板无源直通 PWM 信号 (${ref}.${pnum} ↔ 主板 FPGA U3.${ball})`,
  1165. displayBadge: `主板 FPGA U3.${ball} · ${boardSig} (无源直通)`,
  1166. tags: ["FAN", ref],
  1167. isUsed: true,
  1168. };
  1169. }
  1170. }
  1171. // V1.2 (MRDIMM / 8000) & V1.1 (6400)
  1172. if (ver === "v1_2" || ver === "v1_1") {
  1173. // MRDIMM dev 2.1.0: direct 3.3 V GPIO pins, not the 12 V KEYLED power output.
  1174. if (ver === "v1_2" && ref === "CON140" && (pnum === "1" || pnum === "2")) {
  1175. const isTx = pnum === "1";
  1176. const b = isTx ? "T11" : "P13";
  1177. const signal = isTx ? "host_ttl_tx" : "host_ttl_rx";
  1178. return {
  1179. chip: "FPGA U3",
  1180. physicalPin: `U3.${b}`,
  1181. logicalSignal: signal,
  1182. boardSignal: net,
  1183. peripheralRole: `MRDIMM Host TTL ${isTx ? "TX" : "RX"}`,
  1184. pinDefinition: `CON140.${pnum} 经 ${isTx ? "R1261" : "R1262"} 0Ω 直连 U3.${b},3.3V;3/4脚仍为GPIO,无信号地。共地另接 CON77.3/4。`,
  1185. displayBadge: `FPGA U3.${b} · ${signal}`,
  1186. tags: ["UART", "HOST_TTL", "TTL", "CON140"],
  1187. isUsed: true,
  1188. };
  1189. }
  1190. if (ver === "v1_2" && ref === "CON146" && (pnum === "1" || pnum === "3")) {
  1191. return {
  1192. chip: "BOARD",
  1193. physicalPin: pnum === "1" ? "12V KEYLED功率输出" : "KEY3按键输入",
  1194. logicalSignal: "当前固件未绑定",
  1195. boardSignal: net,
  1196. peripheralRole: "KEY/LED座,不能用作Host TTL",
  1197. pinDefinition: pnum === "1"
  1198. ? "H6 经 NMOS+PMOS 控制 +12V KEYLED_V3,输出有1µF和100nF电容;不是3.3V TTL。"
  1199. : "N7 按键输入含10kΩ上拉及100nF电容;当前Host RX使用CON140.2。",
  1200. displayBadge: "CON146 · 非Host串口",
  1201. tags: ["KEYLED"],
  1202. isUsed: false,
  1203. };
  1204. }
  1205. // V1.1 Host TTL on CON30
  1206. if (ver === "v1_1" && ref === "CON30" && (pnum === "3" || pnum === "4")) {
  1207. const isTx = pnum === "4";
  1208. return {
  1209. chip: "FPGA U3",
  1210. physicalPin: isTx ? "U3.A10" : "U3.B9",
  1211. logicalSignal: isTx ? "host_ttl_tx (FAN_GPIO4)" : "host_ttl_rx (FAN_GPIO3)",
  1212. boardSignal: isTx ? "MOTOR_EN_N / FAN_GPIO4" : "FAN_GPIO3",
  1213. peripheralRole: isTx ? "6400 V1.1 Host TTL TX (活动串口)" : "6400 V1.1 Host TTL RX (活动串口)",
  1214. pinDefinition: isTx
  1215. ? "6400 V1.1 Host TTL 串口发送端 (FPGA U3.A10 → CON30.4, 3.3V LVCMOS)"
  1216. : "6400 V1.1 Host TTL 串口接收端 (CON30.3 → FPGA U3.B9, 3.3V LVCMOS)",
  1217. displayBadge: isTx
  1218. ? "FPGA U3.A10 · host_ttl_tx (FAN_GPIO4)"
  1219. : "FPGA U3.B9 · host_ttl_rx (FAN_GPIO3)",
  1220. tags: ["UART", "HOST_TTL", "TTL", "CON30"],
  1221. isUsed: true,
  1222. };
  1223. }
  1224. // Host RS232 CON78 & BMC RS232 CON77
  1225. if (ref === "CON78" && (pnum === "1" || pnum === "2")) {
  1226. const isTx = pnum === "1";
  1227. return {
  1228. chip: "FPGA U3",
  1229. physicalPin: isTx ? "U3.M10" : "U3.N11",
  1230. logicalSignal: isTx ? "host_rs232_tx (UART2_TX)" : "host_rs232_rx (UART2_RX)",
  1231. boardSignal: isTx ? "UART2_TX" : "UART2_RX",
  1232. peripheralRole: isTx ? "Host RS232 TX (CON78.1)" : "Host RS232 RX (CON78.2)",
  1233. pinDefinition: isTx
  1234. ? "Host RS232 发送端 (U3.M10 → U23 RS232 收发器(V1.2 BOM: TRSF3232E) → CON78.1)"
  1235. : "Host RS232 接收端 (CON78.2 → U23 RS232 收发器(V1.2 BOM: TRSF3232E) → U3.N11)",
  1236. displayBadge: `FPGA U3.${isTx ? "M10" : "N11"} · ${isTx ? "host_rs232_tx (UART2_TX)" : "host_rs232_rx (UART2_RX)"}`,
  1237. tags: ["UART", "HOST_RS232", "RS232"],
  1238. isUsed: true,
  1239. };
  1240. }
  1241. if (ref === "CON77" && (pnum === "1" || pnum === "2")) {
  1242. const isTx = pnum === "1";
  1243. return {
  1244. chip: "FPGA U3",
  1245. physicalPin: isTx ? "U3.L10" : "U3.M9",
  1246. logicalSignal: isTx ? "bmc_tx (UART1_TX)" : "bmc_rx (UART1_RX)",
  1247. boardSignal: isTx ? "UART1_TX" : "UART1_RX",
  1248. peripheralRole: isTx ? "BMC RS232 TX (CON77.1)" : "BMC RS232 RX (CON77.2)",
  1249. pinDefinition: isTx
  1250. ? "BMC 测温轮询串口发送端 (U3.L10 → U22 RS232 收发器(V1.2 BOM: TRSF3232E) → CON77.1, 115200 8N1)"
  1251. : "BMC 测温轮询串口接收端 (CON77.2 → U22 RS232 收发器(V1.2 BOM: TRSF3232E) → U3.M9, 115200 8N1)",
  1252. displayBadge: `FPGA U3.${isTx ? "L10" : "M9"} · ${isTx ? "bmc_tx (UART1_TX)" : "bmc_rx (UART1_RX)"}`,
  1253. tags: ["UART", "BMC", "BMC_RS232"],
  1254. isUsed: true,
  1255. };
  1256. }
  1257. // PTC on V1.2 / V1.1 (CON18, CON27, CON28, CON29)
  1258. if (key in V12_V11_PTC_CONNECTOR_MAP) {
  1259. const ptc = V12_V11_PTC_CONNECTOR_MAP[key];
  1260. const isUsed = true;
  1261. const rtlPort = ver === "v1_1"
  1262. ? `ptc_pwm_out[${ptc.idx}]`
  1263. : `ptc_pwm_out[${ptc.idx}] (逻辑PTC${Math.floor(ptc.idx / 6)})`;
  1264. const role = ver === "v1_1" ? ptc.v11Role : `PTC${ptc.idx + 1} (腔${Math.floor(ptc.idx / 6) + 1},逻辑PTC${Math.floor(ptc.idx / 6)})`;
  1265. return {
  1266. chip: "FPGA U3",
  1267. physicalPin: `U3.${ptc.ball}`,
  1268. logicalSignal: `${rtlPort} (${ptc.heat})`,
  1269. boardSignal: ptc.heat,
  1270. peripheralRole: role,
  1271. pinDefinition: `+12V PTC 高边 MOS 功率输出 (${role}, 500Hz 高有效 PWM)`,
  1272. displayBadge: `FPGA U3.${ptc.ball} · ${rtlPort}`,
  1273. tags: ["PTC", ptc.tag],
  1274. isUsed,
  1275. };
  1276. }
  1277. // Current MRDIMM four buffered PWM channels; FG stays a board fact, not sampled by this protocol.
  1278. const v12ActiveFans = {
  1279. "CON25.7": {axis: 0, ball: "B1"},
  1280. "CON8.7": {axis: 1, ball: "C4"},
  1281. "CON24.7": {axis: 2, ball: "B5"},
  1282. "CON26.7": {axis: 3, ball: "A14"},
  1283. };
  1284. if (ver === "v1_2" && key in v12ActiveFans) {
  1285. const f = v12ActiveFans[key];
  1286. const rtl = `fan_pwm_out[${f.axis}]`;
  1287. return {
  1288. chip: "FPGA U3",
  1289. physicalPin: `U3.${f.ball}`,
  1290. logicalSignal: rtl,
  1291. boardSignal: net,
  1292. peripheralRole: `MRDIMM 腔${f.axis + 1} FAN${f.axis + 1} PWM`,
  1293. pinDefinition: "500Hz PWM;主板MMBT2222 NPN反相,连接器高电平为ON、FPGA球位低电平为ON。",
  1294. displayBadge: `FPGA U3.${f.ball} · ${rtl}`,
  1295. tags: ["FAN", `FAN${f.axis + 1}`],
  1296. isUsed: true,
  1297. };
  1298. }
  1299. // V1.1 Active Fans (Front0..3 on CON25/CON8/CON24/CON26; Rear0..3 on CON64/CON66/CON62/CON60)
  1300. const v11ActiveFans = {
  1301. "CON25.7": {tag: "FAN1", alt: "Front0", rtl: "fan_front_out[0]", ball: "B1", sig: "FAN_PWM3", role: "6400 V1.1 Front0 前风扇 PWM (板级反相)"},
  1302. "CON8.7": {tag: "FAN2", alt: "Front1", rtl: "fan_front_out[1]", ball: "C4", sig: "FAN_PWM1", role: "6400 V1.1 Front1 前风扇 PWM (板级反相)"},
  1303. "CON24.7": {tag: "FAN3", alt: "Front2", rtl: "fan_front_out[2]", ball: "B5", sig: "FAN_PWM2", role: "6400 V1.1 Front2 前风扇 PWM (板级反相)"},
  1304. "CON26.7": {tag: "FAN4", alt: "Front3", rtl: "fan_front_out[3]", ball: "A14", sig: "FAN_PWM4", role: "6400 V1.1 Front3 前风扇 PWM (板级反相)"},
  1305. "CON64.4": {tag: "FAN5", alt: "Rear0", rtl: "fan_rear_out[0]", ball: "P15", sig: "FAN_GPIO44", role: "6400 V1.1 Rear0 后风扇 PWM (同相)"},
  1306. "CON66.4": {tag: "FAN6", alt: "Rear1", rtl: "fan_rear_out[1]", ball: "N15", sig: "FAN_GPIO46", role: "6400 V1.1 Rear1 后风扇 PWM (同相)"},
  1307. "CON62.4": {tag: "FAN7", alt: "Rear2", rtl: "fan_rear_out[2]", ball: "M14", sig: "FAN_GPIO42", role: "6400 V1.1 Rear2 后风扇 PWM (同相)"},
  1308. "CON60.4": {tag: "FAN8", alt: "Rear3", rtl: "fan_rear_out[3]", ball: "P5", sig: "FAN_GPIO40", role: "6400 V1.1 Rear3 后风扇 PWM (同相)"},
  1309. };
  1310. if (ver === "v1_1" && key in v11ActiveFans) {
  1311. const f = v11ActiveFans[key];
  1312. return {
  1313. chip: "FPGA U3",
  1314. physicalPin: `U3.${f.ball}`,
  1315. logicalSignal: `${f.rtl} (${f.sig})`,
  1316. boardSignal: f.sig,
  1317. peripheralRole: f.role,
  1318. pinDefinition: `${f.role} (500Hz PWM, FAN_BOARD_INVERT_MASK=8'h0F)`,
  1319. displayBadge: `FPGA U3.${f.ball} · ${f.rtl} (${f.alt}/${f.tag})`,
  1320. tags: ["FAN", f.tag, f.alt.toUpperCase()],
  1321. isUsed: true,
  1322. };
  1323. }
  1324. // V1.2 12 buffered axes in generator order, plus global enable CON120
  1325. if (ver === "v1_2" && ref in V12_MOTOR_MAP) {
  1326. const m = V12_MOTOR_MAP[ref];
  1327. if (pnum === m.stepPin || pnum === m.dirPin) {
  1328. const isStep = pnum === m.stepPin;
  1329. const b = isStep ? m.stepBall : m.dirBall;
  1330. const rtl = isStep ? `motor_step_gate[${m.axis}]` : `motor_dir_gate[${m.axis}]`;
  1331. const snet = isStep ? m.stepNet : m.dirNet;
  1332. return {
  1333. chip: "FPGA U3",
  1334. physicalPin: `U3.${b}`,
  1335. logicalSignal: `${rtl} (${snet})`,
  1336. boardSignal: snet,
  1337. peripheralRole: `${m.tag} (轴${m.axis}) ${isStep ? "STEP 脉冲" : "DIR 方向"}`,
  1338. pinDefinition: `步进电机 ${m.tag} (轴${m.axis}) ${isStep ? "STEP 输出(2N7002 NMOS开漏;FPGA上升沿计步,对应座端下拉)" : "DIR 输出(2N7002 NMOS开漏反相)"}`,
  1339. displayBadge: `FPGA U3.${b} · ${rtl} (${m.tag}_${isStep ? "STEP" : "DIR"})`,
  1340. tags: ["MOTOR", m.tag],
  1341. isUsed: true,
  1342. };
  1343. }
  1344. }
  1345. if (ver === "v1_2" && key === "CON120.2") {
  1346. return {
  1347. chip: "FPGA U3",
  1348. physicalPin: "U3.C11",
  1349. logicalSignal: "motor_en_n (MOTOR_EN_N)",
  1350. boardSignal: "MOTOR_EN_N",
  1351. peripheralRole: "步进电机全局使能 (MOTOR_EN)",
  1352. pinDefinition: "电机驱动使能控制 (FPGA U3.C11 常驱高 1'b1 → Q145 2N7002 NMOS拉低座端 EN 保持有效)",
  1353. displayBadge: "FPGA U3.C11 · motor_en_n (MOTOR_EN_N)",
  1354. tags: ["MOTOR", "MOTOR_EN"],
  1355. isUsed: true,
  1356. };
  1357. }
  1358. // CON126/127 now carry active limit inputs; do not overwrite them as old X/Y/Z motor outputs.
  1359. // V1.2 active limits: CON12/126/127/128/129/138
  1360. if (ver === "v1_2" && key in V12_LIMIT_MAP) {
  1361. const lim = V12_LIMIT_MAP[key];
  1362. return {
  1363. chip: "FPGA U3",
  1364. physicalPin: `U3.${lim.ball}`,
  1365. logicalSignal: `${lim.rtl} (${lim.tag})`,
  1366. boardSignal: boardSig || net,
  1367. peripheralRole: lim.role,
  1368. pinDefinition: `${lim.role} (3.3V 限位输入, 2FF 同步, 高电平有效阻断对应方向 STEP)`,
  1369. displayBadge: `FPGA U3.${lim.ball} · ${lim.rtl} (${lim.tag})`,
  1370. tags: ["LIMIT", lim.tag, `M${lim.axis + 1}`],
  1371. isUsed: true,
  1372. };
  1373. }
  1374. }
  1375. // CT7428 Temperature Sensor Connectors (CON53, 55, 56, 57, 79..98 across all boards)
  1376. if (/^CON(53|55|56|57|79|8[0-9]|9[0-8])$/.test(ref)) {
  1377. const chOrder = [
  1378. "CON53", "CON55", "CON56", "CON57", "CON79", "CON80", "CON81", "CON82",
  1379. "CON83", "CON85", "CON87", "CON89", "CON84", "CON86", "CON88", "CON90",
  1380. "CON91", "CON93", "CON95", "CON97", "CON92", "CON94", "CON96", "CON98",
  1381. ];
  1382. const v20ProbeRole = {
  1383. CON53: "1#腔室进风温度",
  1384. CON55: "1#腔室出风温度",
  1385. CON56: "2#腔室进风温度",
  1386. CON57: "2#腔室出风温度",
  1387. CON79: "3#腔室进风温度",
  1388. CON80: "3#腔室出风温度",
  1389. CON81: "4#腔室进风温度",
  1390. CON82: "4#腔室出风温度",
  1391. }[ref];
  1392. const chIdx = chOrder.indexOf(ref) + 1;
  1393. const ctBalls = ver === "v1_1" ? "M8/N9(历史profile)" : "B14/A15";
  1394. const chipName = chIdx <= 8 ? "U6" : (chIdx <= 16 ? "U24" : "U25");
  1395. const upperNet = net.toUpperCase();
  1396. const isGndPin = upperNet.includes("GND") || pin?.function_category === "ground" || text(pin?.external_level) === "0V";
  1397. if (!isGndPin && (pnum === "1" || pnum === "2" || pnum === "3")) {
  1398. const isDp = pnum === "1";
  1399. const rolePrefix = ver === "v2_0" && v20ProbeRole ? `${v20ProbeRole} · ` : "";
  1400. return {
  1401. chip: `CT7428 ${chipName} ↔ FPGA U3`,
  1402. physicalPin: `U3.${ctBalls} (SCL/SDA)`,
  1403. logicalSignal: `temp_scl/temp_sda (${ctBalls}) → ${chipName}_CH${(chIdx-1)%8+1}_${isDp ? "DP" : "DN"}`,
  1404. boardSignal: net || `${chipName}_CH${chIdx}_${isDp ? "DP" : "DN"}`,
  1405. peripheralRole: `${rolePrefix}温度探头 CH${chIdx} ${isDp ? "DP+" : "DN-"}`,
  1406. pinDefinition: `${rolePrefix}CT7428 (${chipName}) 远端二极管测温差分输入 ${isDp ? "DP+" : "DN-"} (经 I2C U3.${ctBalls};MRDIMM板载MCU须将I2C0 GPIO设高阻)`,
  1407. displayBadge: `FPGA U3.${ctBalls} (temp_scl/temp_sda) ← ${chipName} CH${(chIdx-1)%8+1}`,
  1408. tags: ["TEMP", `TEMP${chIdx}`],
  1409. isUsed: true,
  1410. };
  1411. }
  1412. }
  1413. // Generic FPGA-mapped pin fallback
  1414. if (ball) {
  1415. const sig = boardSig || net || `${ref}_${pnum}`;
  1416. const isSpare = text(touchpoint?.summary).includes("空余") || text(pin?.summary).includes("空余") || text(touchpoint?.summary).includes("非产品");
  1417. return {
  1418. chip: "FPGA U3",
  1419. physicalPin: `U3.${ball}`,
  1420. logicalSignal: `${sig} (板级映射;以当前ADC为准)`,
  1421. boardSignal: sig,
  1422. peripheralRole: text(pin?.summary) || `${key} (${sig})`,
  1423. pinDefinition: text(pin?.programming?.conclusion) || text(pin?.summary) || `${key} → FPGA U3.${ball} (${sig})`,
  1424. displayBadge: `FPGA U3.${ball} · ${sig}${isSpare ? " [空余]" : ""}`,
  1425. tags: ["GPIO"],
  1426. isUsed: false,
  1427. };
  1428. }
  1429. // Generic MCU-mapped pin fallback
  1430. if (mcuDev && mcuPin) {
  1431. return {
  1432. chip: `MCU ${mcuDev}`,
  1433. physicalPin: `${mcuDev}.${mcuPin}`,
  1434. logicalSignal: `${mcuSig || net} (${text(pin?.mcu?.family) || "板载MCU"})`,
  1435. boardSignal: mcuSig || net,
  1436. peripheralRole: text(pin?.summary) || `${key} → MCU ${mcuDev}.${mcuPin}`,
  1437. pinDefinition: text(pin?.programming?.conclusion) || text(pin?.summary) || `直连板载 MCU ${mcuDev}.${mcuPin} (${mcuSig}),非 FPGA 管脚`,
  1438. displayBadge: `MCU ${mcuDev}.${mcuPin} · ${mcuSig || net} [板载MCU]`,
  1439. tags: ["MCU"],
  1440. isUsed: false,
  1441. };
  1442. }
  1443. // Power / Ground fallback
  1444. const upperNet = net.toUpperCase();
  1445. const isGnd = upperNet.includes("GND") || pin?.function_category === "ground" || text(pin?.external_level) === "0V";
  1446. return {
  1447. chip: isGnd ? "GND" : "POWER/BOARD",
  1448. physicalPin: isGnd ? "GND (0V)" : (net || text(pin?.external_level) || "BOARD"),
  1449. logicalSignal: isGnd ? "GND (参考地)" : `${net || "BOARD_NET"} (${text(pin?.external_level) || "板级网络"})`,
  1450. boardSignal: net,
  1451. peripheralRole: isGnd ? "电源/信号地 (GND)" : (text(pin?.summary) || `${key} (${net})`),
  1452. pinDefinition: text(pin?.summary) || (isGnd ? `${key} 板级公共参考地 (0V GND)` : `${key} 板级电源或外围网络 (${net})`),
  1453. displayBadge: isGnd ? "GND · 0V 参考地" : `板级网络 · ${net || text(pin?.external_level) || "电源"}`,
  1454. tags: [isGnd ? "GND" : "POWER"],
  1455. isUsed: false,
  1456. };
  1457. }
  1458. /**
  1459. * Classify a touchpoint/connector into its peripheral type, usage status (used vs spare),
  1460. * quick-filter tags, and enriched pin rows with explicit FPGA/MCU logical pin mappings.
  1461. */
  1462. export function classifyTouchpointPeripheral(touchpoint, boardVersion = "v2_0") {
  1463. const ver = normalizeBoardVersion(boardVersion);
  1464. const ref = text(touchpoint?.reference).trim();
  1465. const summary = text(touchpoint?.summary).trim();
  1466. const rawPins = (touchpoint?.pins ?? []).filter(
  1467. (p) => p?.mechanical !== true && p?.pad_role !== "shield" && p?.pad_role !== "mechanical" && !text(p?.number).startsWith("SH"),
  1468. );
  1469. const resolvedPins = rawPins.map((pin) => {
  1470. const logical = resolveLogicalPin(touchpoint, pin, ver);
  1471. return {
  1472. pin,
  1473. pinNumber: text(pin?.number),
  1474. net: text(pin?.net),
  1475. direction: text(pin?.direction),
  1476. level: text(pin?.external_level),
  1477. logical,
  1478. };
  1479. });
  1480. const tags = new Set([ref]);
  1481. for (const rp of resolvedPins) {
  1482. for (const t of rp.logical.tags ?? []) {
  1483. tags.add(t);
  1484. }
  1485. }
  1486. let peripheralType = "GPIO";
  1487. let isUsed = false;
  1488. let roleTitle = summary || ref;
  1489. if (ver === "v2_0") {
  1490. if (["CON147", "CON148", "CON149", "CON150"].includes(ref)) {
  1491. peripheralType = "PTC";
  1492. isUsed = true;
  1493. } else if (["CON154", "CON155"].includes(ref)) {
  1494. peripheralType = "FAN";
  1495. isUsed = true;
  1496. } else if (["CON156", "CON157", "CON158"].includes(ref)) {
  1497. peripheralType = "FAN";
  1498. isUsed = false;
  1499. } else if (["CON12", "CON134", "CON77"].includes(ref)) {
  1500. peripheralType = "UART";
  1501. isUsed = true;
  1502. } else if (["CON70", "CON103", "CON170"].includes(ref)) {
  1503. peripheralType = "UART";
  1504. isUsed = false;
  1505. } else if (["CON165", "CON164"].includes(ref)) {
  1506. peripheralType = "SSR";
  1507. isUsed = true;
  1508. } else if (/^CON(53|55|56|57|79|80|81|82)$/.test(ref)) {
  1509. peripheralType = "TEMP";
  1510. isUsed = true;
  1511. } else if (["CON159", "CON160", "CON161", "CON162", "CON163"].includes(ref)) {
  1512. peripheralType = "MOTOR";
  1513. isUsed = false;
  1514. } else if (["J3", "J19", "J25"].includes(ref)) {
  1515. peripheralType = "JTAG";
  1516. isUsed = ref !== "J25";
  1517. } else if (["CON168", "CON172", "J29", "J30"].includes(ref)) {
  1518. peripheralType = "MCU";
  1519. isUsed = false;
  1520. } else if (["CON173", "CON174"].includes(ref) || ref.startsWith("PL") || ref.startsWith("TP")) {
  1521. peripheralType = "POWER";
  1522. isUsed = true;
  1523. } else {
  1524. peripheralType = "GPIO";
  1525. isUsed = false;
  1526. }
  1527. } else if (ver === "v2_0_fan") {
  1528. if (["CON168", "CON8", "CON169", "CON170", "CON171"].includes(ref)) {
  1529. peripheralType = "FAN";
  1530. isUsed = true;
  1531. } else {
  1532. peripheralType = "POWER";
  1533. isUsed = true;
  1534. }
  1535. } else if (ver === "v1_2") {
  1536. if (["CON18", "CON27", "CON28", "CON29"].includes(ref)) {
  1537. peripheralType = "PTC";
  1538. isUsed = true;
  1539. } else if (["CON25", "CON8", "CON24", "CON26"].includes(ref)) {
  1540. peripheralType = "FAN";
  1541. isUsed = true;
  1542. } else if (["CON31", "CON32", "CON33", "CON34", "CON35", "CON68"].includes(ref)) {
  1543. peripheralType = "FAN";
  1544. isUsed = false;
  1545. } else if (["CON140", "CON78", "CON77"].includes(ref)) {
  1546. peripheralType = "UART";
  1547. isUsed = true;
  1548. } else if (["CON70", "CON102", "CON103", "CON134", "CON135"].includes(ref)) {
  1549. peripheralType = "UART";
  1550. isUsed = false;
  1551. } else if (ref in V12_MOTOR_MAP || ref === "CON120") {
  1552. peripheralType = "MOTOR";
  1553. isUsed = true;
  1554. } else if (["CON116", "CON117", "CON118", "CON119", "CON124", "CON125", "CON142"].includes(ref)) {
  1555. peripheralType = "MOTOR";
  1556. isUsed = false;
  1557. } else if (["CON12", "CON126", "CON127", "CON128", "CON129", "CON138"].includes(ref)) {
  1558. peripheralType = "LIMIT";
  1559. isUsed = true;
  1560. } else if (["J3", "J19", "J25"].includes(ref)) {
  1561. peripheralType = "JTAG";
  1562. isUsed = ref !== "J25";
  1563. } else if (/^CON(53|55|56|57|79|8[0-9]|9[0-8])$/.test(ref)) {
  1564. peripheralType = "TEMP";
  1565. isUsed = true;
  1566. } else if (["CON101", "CON130", "CON131", "CON132", "CON133", "J29", "J30"].includes(ref)) {
  1567. peripheralType = "MCU";
  1568. isUsed = false;
  1569. } else if (["CON2", "CON23"].includes(ref) || ref.startsWith("PL") || ref.startsWith("TP")) {
  1570. peripheralType = "POWER";
  1571. isUsed = true;
  1572. } else {
  1573. peripheralType = "GPIO";
  1574. isUsed = false;
  1575. }
  1576. } else {
  1577. // v1_1 (6400 V1.1)
  1578. if (["CON18", "CON27", "CON28", "CON29"].includes(ref)) {
  1579. peripheralType = "PTC";
  1580. isUsed = true;
  1581. } else if (["CON25", "CON8", "CON24", "CON26", "CON64", "CON66", "CON62", "CON60"].includes(ref)) {
  1582. peripheralType = "FAN";
  1583. isUsed = true;
  1584. } else if (["CON31", "CON32", "CON33", "CON34", "CON35", "CON68"].includes(ref)) {
  1585. peripheralType = "FAN";
  1586. isUsed = false;
  1587. } else if (["CON30", "CON78", "CON77"].includes(ref)) {
  1588. peripheralType = "UART";
  1589. isUsed = true;
  1590. } else if (["CON70"].includes(ref)) {
  1591. peripheralType = "UART";
  1592. isUsed = false;
  1593. } else if (["J3", "J19", "J25"].includes(ref)) {
  1594. peripheralType = "JTAG";
  1595. isUsed = ref !== "J25";
  1596. } else if (/^CON(53|55|56|57|79|8[0-9]|9[0-8])$/.test(ref)) {
  1597. peripheralType = "TEMP";
  1598. isUsed = true;
  1599. } else if (["CON2", "CON23"].includes(ref) || ref.startsWith("PL") || ref.startsWith("TP")) {
  1600. peripheralType = "POWER";
  1601. isUsed = true;
  1602. } else {
  1603. peripheralType = "GPIO";
  1604. isUsed = false;
  1605. }
  1606. }
  1607. // Mark mechanical mounting holes (J8, J9, J11..18, J20..22, J26..27) as non-used mechanical
  1608. if (/^J(8|9|11|12|13|14|17|18|20|21|22|26|27)$/.test(ref)) {
  1609. peripheralType = "POWER";
  1610. isUsed = false;
  1611. }
  1612. const isSpareConnector = !isUsed && (ref.startsWith("CON") || ref.startsWith("J"));
  1613. if (isSpareConnector) {
  1614. tags.add("SPARE");
  1615. tags.add("空余插座");
  1616. }
  1617. return {
  1618. reference: ref,
  1619. touchpoint,
  1620. peripheralType,
  1621. peripheralLabel: PERIPHERAL_TYPE_LABELS[peripheralType] ?? peripheralType,
  1622. isUsed,
  1623. isSpareConnector,
  1624. roleTitle,
  1625. tags: Array.from(tags),
  1626. pins: resolvedPins,
  1627. };
  1628. }
  1629. /**
  1630. * Build the complete peripheral & logical pin catalog for a board dataset.
  1631. */
  1632. export function buildPeripheralIndex(data, boardVersion = "v2_0") {
  1633. const ver = normalizeBoardVersion(boardVersion, data);
  1634. const items = (data?.touchpoints ?? [])
  1635. .map((tp) => classifyTouchpointPeripheral(tp, ver))
  1636. .sort((a, b) => {
  1637. if (a.isUsed !== b.isUsed) return a.isUsed ? -1 : 1;
  1638. const typeIdxA = PERIPHERAL_TYPE_ORDER.indexOf(a.peripheralType);
  1639. const typeIdxB = PERIPHERAL_TYPE_ORDER.indexOf(b.peripheralType);
  1640. if (typeIdxA !== typeIdxB) return typeIdxA - typeIdxB;
  1641. return naturalCompare(a.reference, b.reference);
  1642. });
  1643. const categoryCounts = {ALL: items.length};
  1644. for (const cat of PERIPHERAL_TYPE_ORDER) {
  1645. if (cat !== "ALL") categoryCounts[cat] = 0;
  1646. }
  1647. let usedCount = 0;
  1648. let spareCount = 0;
  1649. for (const item of items) {
  1650. categoryCounts[item.peripheralType] = (categoryCounts[item.peripheralType] ?? 0) + 1;
  1651. if (item.isUsed) usedCount += 1;
  1652. if (item.isSpareConnector) spareCount += 1;
  1653. }
  1654. // Build quick-jump chips tailored to the board
  1655. const quickChips = [];
  1656. if (ver === "v1_2") {
  1657. quickChips.push(
  1658. {label: "HOST TTL (CON140)", category: "UART", query: "CON140"},
  1659. {label: "HOST RS232 (CON78)", category: "UART", query: "CON78"},
  1660. {label: "BMC RS232 (CON77)", category: "UART", query: "CON77"},
  1661. {label: "JTAG (J3)", category: "JTAG", query: "J3"},
  1662. {label: "PTC1..6 (CON18)", category: "PTC", query: "CON18"},
  1663. {label: "PTC7..12 (CON27)", category: "PTC", query: "CON27"},
  1664. {label: "FAN1..4 (全部风扇)", category: "FAN", usage: "used", query: ""},
  1665. {label: "电机 M1..M12", category: "MOTOR", usage: "used", query: ""},
  1666. {label: "限位 LS1..12/HS1..12", category: "LIMIT", usage: "used", query: ""},
  1667. {label: "空余插座", category: "ALL", usage: "spare", query: ""},
  1668. );
  1669. } else if (ver === "v2_0") {
  1670. quickChips.push(
  1671. {label: "HOST TTL (CON12)", category: "UART", query: "CON12"},
  1672. {label: "HOST RS232 (CON134)", category: "UART", query: "CON134"},
  1673. {label: "BMC RS232 (CON77)", category: "UART", query: "CON77"},
  1674. {label: "JTAG (J3)", category: "JTAG", query: "J3"},
  1675. {label: "PTC1", category: "PTC", query: "PTC1"},
  1676. {label: "PTC 1#腔 (CON147)", category: "PTC", query: "CON147"},
  1677. {label: "PTC 2#腔 (CON148)", category: "PTC", query: "CON148"},
  1678. {label: "PTC 3#腔 (CON149)", category: "PTC", query: "CON149"},
  1679. {label: "PTC 4#腔 (CON150)", category: "PTC", query: "CON150"},
  1680. {label: "FAN 全部风扇", category: "FAN", usage: "used", query: ""},
  1681. {label: "SSR 继电器 (CON165)", category: "SSR", query: "CON165"},
  1682. {label: "JTAG / SWD", category: "JTAG", query: ""},
  1683. {label: "空余插座", category: "ALL", usage: "spare", query: ""},
  1684. );
  1685. } else if (ver === "v2_0_fan") {
  1686. quickChips.push(
  1687. {label: "控制输入 (CON168)", category: "FAN", query: "CON168"},
  1688. {label: "FAN1 (CON8)", category: "FAN", query: "CON8"},
  1689. {label: "FAN2 (CON169)", category: "FAN", query: "CON169"},
  1690. {label: "FAN3 (CON170)", category: "FAN", query: "CON170"},
  1691. {label: "FAN4 (CON171)", category: "FAN", query: "CON171"},
  1692. {label: "+12V 电源 (CON2/172)", category: "POWER", query: ""},
  1693. );
  1694. } else {
  1695. quickChips.push(
  1696. {label: "HOST TTL (CON30)", category: "UART", query: "CON30"},
  1697. {label: "HOST RS232 (CON78)", category: "UART", query: "CON78"},
  1698. {label: "BMC RS232 (CON77)", category: "UART", query: "CON77"},
  1699. {label: "JTAG (J3)", category: "JTAG", query: "J3"},
  1700. {label: "PTC1", category: "PTC", query: "PTC1"},
  1701. {label: "PTC1..24 全部", category: "PTC", usage: "used", query: ""},
  1702. {label: "FAN 前/后全部", category: "FAN", usage: "used", query: ""},
  1703. {label: "空余插座", category: "ALL", usage: "spare", query: ""},
  1704. );
  1705. }
  1706. return {
  1707. boardVersion: ver,
  1708. items,
  1709. categoryCounts,
  1710. usedCount,
  1711. spareCount,
  1712. quickChips,
  1713. };
  1714. }
  1715. /**
  1716. * Filter the peripheral catalog by category, usage ("all" | "used" | "spare"), and query string.
  1717. */
  1718. export function filterPeripheralCatalog(data, boardVersion = "v2_0", options = {}) {
  1719. const index = buildPeripheralIndex(data, boardVersion);
  1720. const category = text(options.category || "ALL").toUpperCase();
  1721. const usage = text(options.usage || "all").toLowerCase();
  1722. const rawQuery = text(options.query || "").trim();
  1723. const needle = normalized(rawQuery);
  1724. const exactTag = rawQuery.toUpperCase();
  1725. const filtered = index.items.filter((item) => {
  1726. if (category !== "ALL" && item.peripheralType !== category) return false;
  1727. if (usage === "used" && !item.isUsed) return false;
  1728. if (usage === "spare" && !item.isSpareConnector) return false;
  1729. if (!needle) return true;
  1730. // Check exact tag match first (e.g., "PTC1" should not match "PTC10" if exact tag exists)
  1731. if (/^(PTC|FAN|M|LS|HS|SSR|TEMP)\d+$/i.test(rawQuery)) {
  1732. const hasExactTag = item.tags.some((t) => t.toUpperCase() === exactTag) ||
  1733. item.pins.some((p) => (p.logical.tags ?? []).some((t) => t.toUpperCase() === exactTag));
  1734. if (hasExactTag) return true;
  1735. return false;
  1736. }
  1737. const haystack = normalized([
  1738. item.reference,
  1739. item.peripheralType,
  1740. item.peripheralLabel,
  1741. item.roleTitle,
  1742. ...item.tags,
  1743. ...item.pins.flatMap((p) => [
  1744. `${item.reference}.${p.pinNumber}`,
  1745. p.net,
  1746. p.logical.physicalPin,
  1747. p.logical.logicalSignal,
  1748. p.logical.boardSignal,
  1749. p.logical.peripheralRole,
  1750. p.logical.pinDefinition,
  1751. p.logical.displayBadge,
  1752. ]),
  1753. ].join(" "));
  1754. return haystack.includes(needle);
  1755. });
  1756. // If filtering by specific pin tag (like PTC1, M1, LS1), highlight matching pins
  1757. const matchingItems = filtered.map((item) => {
  1758. if (!needle) return {...item, matchedPinNumbers: []};
  1759. const matchedPinNumbers = item.pins
  1760. .filter((p) => {
  1761. if (/^(PTC|FAN|M|LS|HS|SSR|TEMP)\d+$/i.test(rawQuery)) {
  1762. return (p.logical.tags ?? []).some((t) => t.toUpperCase() === exactTag);
  1763. }
  1764. const pinHaystack = normalized([
  1765. `${item.reference}.${p.pinNumber}`,
  1766. p.net,
  1767. p.logical.physicalPin,
  1768. p.logical.logicalSignal,
  1769. p.logical.boardSignal,
  1770. p.logical.peripheralRole,
  1771. p.logical.pinDefinition,
  1772. p.logical.displayBadge,
  1773. ].join(" "));
  1774. return pinHaystack.includes(needle);
  1775. })
  1776. .map((p) => p.pinNumber);
  1777. return {...item, matchedPinNumbers};
  1778. });
  1779. return {
  1780. ...index,
  1781. filteredItems: matchingItems,
  1782. };
  1783. }