diff --git a/app/assets/css/main.css b/app/assets/css/main.css
index e2503c4..b8afe5f 100644
--- a/app/assets/css/main.css
+++ b/app/assets/css/main.css
@@ -878,6 +878,19 @@ kbd {
fill: var(--muted);
}
+.cable-choices .cable-row {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 4px;
+ margin-bottom: 4px;
+}
+
+.cable-choices .cable-row .hint {
+ width: 84px;
+ margin: 0;
+}
+
.invert-pins {
display: flex;
flex-wrap: wrap;
diff --git a/app/components/editor/Help.vue b/app/components/editor/Help.vue
index ee99466..a2eb93a 100644
--- a/app/components/editor/Help.vue
+++ b/app/components/editor/Help.vue
@@ -23,6 +23,7 @@ const SHORTCUTS: [string, string][] = [
['Mouse wheel', 'Zoom'],
['N, I, O, L, B, C, H, T, P, X', 'Place NAND, input, output, lamp, button, clock, hex display, note, input panel, pixel'],
['Select wires, Ctrl+G', 'Bundle them into one cable; an end whose wires all meet one part plugs straight into it'],
+ ['Right click a component, Cable from outputs', 'A cable plugged onto S0–S3 (or any group); drop its free end on the next part\'s pins'],
['Drag a plugged cable end away', 'Pull it out; drop its pins on a part\'s pins to plug it in again'],
['Drag a part onto a pin', 'Connects there when dropped (a green ring shows where); hold Alt to just place it'],
['Drag a wire by its end', 'Moves that end to another pin (grab it near the pin, or by the small circle)'],
diff --git a/app/components/editor/NodeSettings.vue b/app/components/editor/NodeSettings.vue
index b967c66..bc461bf 100644
--- a/app/components/editor/NodeSettings.vue
+++ b/app/components/editor/NodeSettings.vue
@@ -148,6 +148,9 @@ const widens = computed(() => {
})
const inputs = computed(() => inputCount(n.value, ed.getDef.value))
+/** Groups of pins a new cable can plug onto. */
+const cables = computed(() => ed.cableChoices(n.value))
+
/** Inputs that can carry a bubble, with their names. */
const invertible = computed(() => {
if (!INVERTIBLE.has(n.value.kind)) return []
@@ -407,6 +410,18 @@ function setRowHz(row: PanelRow, e: Event) {
Kept inside: in a component the switches stay as set here, buttons read 0.
+
diff --git a/app/composables/useEditor.ts b/app/composables/useEditor.ts
index 1cf0669..c1f0e06 100644
--- a/app/composables/useEditor.ts
+++ b/app/composables/useEditor.ts
@@ -3,7 +3,18 @@
// One shared instance for the whole page.
import { nandCounts, runSteps, stats, tableCsv, TRANSISTORS_PER_NAND, truthTable, type Stats, type TestResult, type TruthTable } from '~/lib/logic/analyze'
-import { bundleWires, cablePanel, dissolveCable, pluggedInto, plugInto, syncCables } from '~/lib/logic/cables'
+import {
+ bundleWires,
+ cableFromPins,
+ cableIntoPins,
+ cablePanel,
+ dissolveCable,
+ pinGroups,
+ pluggedInto,
+ plugInto,
+ syncCables,
+ type PinGroup,
+} from '~/lib/logic/cables'
import { fitLinks, fitSpots } from '~/lib/logic/fit'
import { checkCircuit } from '~/lib/logic/check'
import { loadSoundSettings, saveSoundSettings, sfx, type Sound, type SoundSettings } from '~/lib/audio'
@@ -830,6 +841,41 @@ function create() {
commit()
}
+ /**
+ * Pins of a part a new cable can plug onto: groups of its outputs (those
+ * not already in a plugged cable) and of its free inputs.
+ */
+ function cableChoices(n: CircuitNode): { out: PinGroup[]; in: PinGroup[] } {
+ const none = { out: [], in: [] }
+ if (n.kind !== 'chip' && n.kind !== 'panel' && n.kind !== 'hex') return none
+ const s = shapeOf(n, getDef.value)
+ const d = def.value
+ const kinds = new Map(d.nodes.map(x => [x.id, x]))
+ const cabled = new Set(d.wires.filter(w => w.from.node === n.id && kinds.get(w.to.node)?.plug === n.id).map(w => w.from.pin))
+ const fed = new Set(d.wires.filter(w => w.to.node === n.id).map(w => w.to.pin))
+ const outs = s.outputs.filter(p => !p.bus).sort((a, b) => a.dy - b.dy)
+ const ins = n.kind === 'panel' ? [] : s.inputs.filter(p => !p.bus).sort((a, b) => a.dy - b.dy)
+ return { out: pinGroups(outs, k => !cabled.has(k)), in: pinGroups(ins, k => !fed.has(k)) }
+ }
+
+ function cableOut(nodeId: string, pins: string[]) {
+ const res = cableFromPins(def.value, nodeId, pins, getDef.value)
+ if (!res.ok) return notify(res.error, 'error')
+ selection.value = [res.split.id]
+ wireSel.value = []
+ commit()
+ notify(`Cable with ${res.split.bits} wires: drag its free end onto the pins where they go`)
+ }
+
+ function cableIn(nodeId: string, pins: string[]) {
+ const res = cableIntoPins(def.value, nodeId, pins, getDef.value)
+ if (!res.ok) return notify(res.error, 'error')
+ selection.value = [res.bundle.id]
+ wireSel.value = []
+ commit()
+ notify(`Cable with ${res.bundle.bits} wires: drag its free end next to what drives them, or attach an input panel`)
+ }
+
function addCablePanel(bundleId: string) {
const panel = cablePanel(def.value, bundleId, getDef.value)
if (!panel) return notify('Every wire of this cable is already driven', 'error')
@@ -1929,6 +1975,9 @@ function create() {
unplugEnd,
setPitch,
addCablePanel,
+ cableChoices,
+ cableOut,
+ cableIn,
linking,
startLinking,
linkTo,
diff --git a/app/composables/useMenu.ts b/app/composables/useMenu.ts
index d03377b..fc3d178 100644
--- a/app/composables/useMenu.ts
+++ b/app/composables/useMenu.ts
@@ -220,6 +220,22 @@ export function useMenu() {
{ label: 'Make independent copy', icon: 'lucide:copy-plus', run: () => ed.makeIndependent(n.id) },
)
}
+ const cables = ed.cableChoices(n)
+ if (cables.out.length || cables.in.length) out.push('sep')
+ if (cables.out.length) {
+ out.push({
+ label: 'Cable from outputs',
+ icon: 'lucide:merge',
+ children: cables.out.map(g => ({ label: g.label, note: `${g.pins.length} wires`, run: () => ed.cableOut(n.id, g.pins) })),
+ })
+ }
+ if (cables.in.length) {
+ out.push({
+ label: 'Cable into inputs',
+ icon: 'lucide:split',
+ children: cables.in.map(g => ({ label: g.label, note: `${g.pins.length} wires`, run: () => ed.cableIn(n.id, g.pins) })),
+ })
+ }
if (n.kind === 'panel') {
out.push('sep')
if (!n.attach) out.push({ label: 'Add row', icon: 'lucide:plus', run: () => ed.addRow(n) })
diff --git a/app/lib/logic/cables.ts b/app/lib/logic/cables.ts
index b086595..ba82e3e 100644
--- a/app/lib/logic/cables.ts
+++ b/app/lib/logic/cables.ts
@@ -436,3 +436,106 @@ export const RIBBON_MAX = 16
export function ribbon(pts: Pt[], bits: number): Pt[][] {
return Array.from({ length: bits }, (_, i) => offsetPath(pts, (i - (bits - 1) / 2) * LANE))
}
+
+/** A choice of pins to run a cable from (or to): a numbered group like A0–A3, or all of them. */
+export interface PinGroup {
+ label: string
+ pins: string[]
+}
+
+/**
+ * Pins worth a cable, in pin order: each run of names counting up with the
+ * same stem (A0–A3, B0–B3, S0–S3), then all of them together. Pins that
+ * can't be used (`usable` false) break runs and are left out.
+ */
+export function pinGroups(pins: { key: string; label: string }[], usable: (key: string) => boolean = () => true): PinGroup[] {
+ const out: PinGroup[] = []
+ const name = (p: { label: string }) => /^(.*?)(\d+)$/.exec(p.label.trim())
+ let run: { key: string; label: string }[] = []
+ const flush = () => {
+ if (run.length >= 2) out.push({ label: `${run[0]!.label}–${run[run.length - 1]!.label}`, pins: run.map(p => p.key) })
+ run = []
+ }
+ for (const p of pins) {
+ const m = name(p)
+ const prev = run[run.length - 1]
+ const pm = prev && name(prev)
+ if (!usable(p.key) || !m) {
+ flush()
+ continue
+ }
+ if (pm && pm[1] === m[1] && Number(m[2]) === Number(pm[2]) + 1) run.push(p)
+ else {
+ flush()
+ run = [p]
+ }
+ }
+ flush()
+ const all = pins.filter(p => usable(p.key))
+ if (all.length >= 2 && !out.some(g => g.pins.length === all.length)) {
+ out.push({ label: `all ${all.length}${all.length <= 6 ? ` (${all[0]!.label}–${all[all.length - 1]!.label})` : ''}`, pins: all.map(p => p.key) })
+ }
+ return out.filter(g => g.pins.length <= MAX_BITS)
+}
+
+/** The first spot along the row from `x` (by `step`) where a cable end of `h` rows fits. */
+function clearSpot(def: ComponentDef, get: DefLookup, x: number, y: number, h: number, step: 1 | -1): number {
+ const boxes = def.nodes.map(n => {
+ const b = shapeOf(n, get).body
+ return { x: n.x + b.x, y: n.y + b.y, w: b.w, h: b.h }
+ })
+ for (let i = 0; i < 20; i++) {
+ const cx = x + i * step
+ if (!boxes.some(b => cx - 0.5 < b.x + b.w && cx + 1.5 > b.x && y - 1 < b.y + b.h && y + h > b.y)) return cx
+ }
+ return x
+}
+
+/**
+ * A new cable plugged onto some of a part's outputs (wire 0 the top one):
+ * its bundle sits on the pins, the cable runs a few squares on to a free
+ * split carrying their names, ready to be dragged to where they go.
+ */
+export function cableFromPins(def: ComponentDef, partId: string, pins: string[], get: DefLookup): { ok: true; bundle: CircuitNode; split: CircuitNode } | { ok: false; error: string } {
+ const part = def.nodes.find(n => n.id === partId)
+ if (!part) return { ok: false, error: 'Part not found' }
+ const outs = shapeOf(part, get).outputs.filter(p => !p.bus && pins.includes(p.key)).sort((a, b) => a.dy - b.dy)
+ if (outs.length < 2) return { ok: false, error: 'A cable needs at least two pins' }
+ if (outs.length > MAX_BITS) return { ok: false, error: `A cable holds at most ${MAX_BITS} wires` }
+ const bundle: CircuitNode = { id: uid('n'), kind: 'bundle', bits: outs.length, x: part.x + outs[0]!.dx, y: part.y + outs[0]!.dy }
+ def.nodes.push(bundle)
+ outs.forEach((p, i) => def.wires.push({ id: uid('w'), from: { node: part.id, pin: p.key }, to: { node: bundle.id, pin: `b${i}` } }))
+ plugInto(def, bundle, part.id, get)
+ const mid = Math.floor((outs.length - 1) / 2)
+ const busY = bundle.y + cableOffsets(bundle)[mid]!
+ const split: CircuitNode = { id: uid('n'), kind: 'split', bits: outs.length, x: 0, y: busY - mid }
+ split.x = clearSpot(def, get, bundle.x + 7, split.y, outs.length, 1)
+ def.nodes.push(split)
+ def.wires.push({ id: uid('w'), from: { node: bundle.id, pin: 'q' }, to: { node: split.id, pin: 'a' } })
+ return { ok: true, bundle, split }
+}
+
+/**
+ * A new cable plugged into some of a part's free inputs: its split sits on
+ * the pins, the cable comes from a free bundle a few squares back, ready
+ * to be fed (drag it next to what drives them, or attach a panel to it).
+ */
+export function cableIntoPins(def: ComponentDef, partId: string, pins: string[], get: DefLookup): { ok: true; bundle: CircuitNode; split: CircuitNode } | { ok: false; error: string } {
+ const part = def.nodes.find(n => n.id === partId)
+ if (!part) return { ok: false, error: 'Part not found' }
+ const fed = new Set(def.wires.filter(w => w.to.node === part.id).map(w => w.to.pin))
+ const ins = shapeOf(part, get).inputs.filter(p => !p.bus && pins.includes(p.key) && !fed.has(p.key)).sort((a, b) => a.dy - b.dy)
+ if (ins.length < 2) return { ok: false, error: 'A cable needs at least two free inputs' }
+ if (ins.length > MAX_BITS) return { ok: false, error: `A cable holds at most ${MAX_BITS} wires` }
+ const split: CircuitNode = { id: uid('n'), kind: 'split', bits: ins.length, x: part.x + ins[0]!.dx - 1, y: part.y + ins[0]!.dy }
+ def.nodes.push(split)
+ ins.forEach((p, i) => def.wires.push({ id: uid('w'), from: { node: split.id, pin: `b${i}` }, to: { node: part.id, pin: p.key } }))
+ plugInto(def, split, part.id, get)
+ const mid = Math.floor((ins.length - 1) / 2)
+ const busY = split.y + cableOffsets(split)[mid]!
+ const bundle: CircuitNode = { id: uid('n'), kind: 'bundle', bits: ins.length, x: 0, y: busY - mid }
+ bundle.x = clearSpot(def, get, split.x - 8, bundle.y, ins.length, -1)
+ def.nodes.push(bundle)
+ def.wires.push({ id: uid('w'), from: { node: bundle.id, pin: 'q' }, to: { node: split.id, pin: 'a' } })
+ return { ok: true, bundle, split }
+}
diff --git a/tests/plugs.test.ts b/tests/plugs.test.ts
index 3ec1190..be28b38 100644
--- a/tests/plugs.test.ts
+++ b/tests/plugs.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'
-import { bundleWires, cablePanel, nameTags, plugInto, plugLayout, syncCables } from '../app/lib/logic/cables'
+import { bundleWires, cableFromPins, cableIntoPins, cablePanel, nameTags, pinGroups, plugInto, plugLayout, syncCables } from '../app/lib/logic/cables'
+import { dropLinks, dropPins } from '../app/lib/logic/fit'
import { compile, rowSource } from '../app/lib/logic/compile'
import { chipPins, panelOffsets, pinPos, shapeOf } from '../app/lib/logic/geometry'
import { copySelection, createProject, lookup, pasteClip } from '../app/lib/logic/ops'
@@ -293,3 +294,62 @@ describe('plugging in more often', () => {
expect(sp.plug).toBe('add')
})
})
+
+describe('a cable straight off a part', () => {
+ it('offers numbered groups of pins and all of them', () => {
+ const groups = pinGroups(['A0', 'A1', 'A2', 'A3', 'B0', 'B1', 'B2', 'B3', 'Cin'].map(l => ({ key: l, label: l })))
+ expect(groups.map(g => g.label)).toEqual(['A0–A3', 'B0–B3', 'all 9'])
+ const outs = pinGroups(['S0', 'S1', 'S2', 'S3', 'Cout'].map(l => ({ key: l, label: l })), k => k !== 'S2')
+ expect(outs.map(g => g.label)).toEqual(['S0–S1', 'all 4 (S0–Cout)'])
+ })
+
+ it('from outputs: a bundle plugged on the pins, a free split with their names, then dropped onto the next part', () => {
+ const { p, def, outs } = adder()
+ const get = lookup(p)
+ // Take the lamps away: the outputs are free.
+ def.wires = def.wires.filter(w => w.from.node !== 'add')
+ const res = cableFromPins(def, 'add', outs.slice(0, 4), get)
+ if (!res.ok) throw new Error(res.error)
+ expect(res.bundle.plug).toBe('add')
+ expect(touches(p, def, res.bundle)).toBe(true)
+ syncCables(p)
+ expect(res.split.names).toEqual(['S0', 'S1', 'S2', 'S3'])
+ expect(res.split.x).toBeGreaterThan(res.bundle.x)
+ // A second adder: drop the split right onto its A inputs.
+ def.nodes.push({ id: 'add2', kind: 'chip', chip: 'ex-add4', x: 60, y: 0 })
+ const lay = (() => {
+ const s = shapeOf(node(def, 'add2'), get).inputs[0]!
+ return { x: 60 + s.dx - 1, y: s.dy }
+ })()
+ res.split.x = lay.x
+ res.split.y = lay.y
+ const moved = dropPins(def, [res.split], n => shapeOf(n, get))
+ const still = dropPins(def, def.nodes.filter(n => n !== res.split), n => shapeOf(n, get))
+ const drop = dropLinks(moved, still, 0)!
+ expect(drop.links).toHaveLength(4)
+ def.wires.push(...drop.links.map((l, i) => ({ id: `d${i}`, ...l })))
+ syncCables(p)
+ expect(res.split.plug).toBe('add2')
+ // A0 of the first adder set: S0 = 1 arrives at A0 of the second, whose S0 shows it.
+ def.nodes.push({ id: 'out', kind: 'lamp', x: 80, y: 0 })
+ def.wires.push({ id: 'o', from: { node: 'add2', pin: outs[0]! }, to: { node: 'out', pin: 'a' } })
+ const s = new Simulator(compile(get, def.id))
+ s.setSource('s0', true)
+ s.run()
+ expect(s.pin('out', 'a')).toBe(1)
+ })
+
+ it('into inputs: a split plugged on the free pins, a free bundle behind it', () => {
+ const { p, def, ins } = adder()
+ def.wires = def.wires.filter(w => w.to.node !== 'add')
+ const res = cableIntoPins(def, 'add', ins.slice(4, 8), lookup(p))
+ if (!res.ok) throw new Error(res.error)
+ expect(res.split.plug).toBe('add')
+ expect(touches(p, def, res.split)).toBe(true)
+ expect(res.bundle.x).toBeLessThan(res.split.x)
+ syncCables(p)
+ expect(res.bundle.names).toEqual(['B0', 'B1', 'B2', 'B3'])
+ // Those inputs are taken now: no second cable into them.
+ expect(cableIntoPins(def, 'add', ins.slice(4, 8), lookup(p)).ok).toBe(false)
+ })
+})