From 97c650a2b87aa345cf89b01e686a6e73712953dd Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Fri, 23 Oct 2026 12:27:12 +0200 Subject: [PATCH] BF inverted inputs: lamps and outputs show the inverted value, and checks read it A bubble on a lamp or output pin now shows on it. The truth table, Check, puzzle scores and gate detection read what an output puts out after its bubble (they read the raw wire, so a bubble on an output was ignored), and rows of an output panel count as outputs there (they always read 0). Right-click on an input pin offers to invert it, for desktops that take Alt+click for themselves. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/components/editor/Canvas.vue | 7 ++++ app/components/editor/NodeView.vue | 3 +- app/components/editor/PinRow.vue | 2 +- app/lib/logic/analyze.ts | 4 +-- app/lib/logic/compile.ts | 7 ++-- app/lib/logic/puzzles.ts | 4 +-- app/lib/logic/sim.ts | 6 ++++ app/lib/logic/wide.ts | 2 +- tests/signals.test.ts | 52 +++++++++++++++++++++++++++++- 9 files changed, 77 insertions(+), 10 deletions(-) diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue index 172619b..d3314d8 100644 --- a/app/components/editor/Canvas.vue +++ b/app/components/editor/Canvas.vue @@ -1289,6 +1289,13 @@ function openMenuAt(target: Element, at: Pt, x: number, y: number) { } const onThing = !!(nodeId || wireId || junction) const entries = build(onThing ? { kind: 'selection' } : { kind: 'canvas', at }, canvasOps) + // On an input pin: its bubble first (the same as Alt + click, which some desktops take for themselves). + const pin = target.closest('[data-pin][data-dir="in"]')?.getAttribute('data-pin') + const n = nodeId ? nodeMap.value.get(nodeId) : undefined + if (pin && n && INVERTIBLE.has(n.kind)) { + const on = n.invert?.includes(pin) + entries.unshift({ label: on ? 'Take the bubble off this input' : 'Invert this input', icon: 'lucide:circle-dot', keys: 'Alt+click', run: () => ed.toggleInvert(n, pin) }, 'sep') + } menu.value = entries.length ? { x: x + 2, y: y + 2, entries } : null } diff --git a/app/components/editor/NodeView.vue b/app/components/editor/NodeView.vue index c0b3d26..22b91b7 100644 --- a/app/components/editor/NodeView.vue +++ b/app/components/editor/NodeView.vue @@ -134,7 +134,8 @@ const faceHex = (inner: string) => sevenSegment([0, 1, 2, 3].reduce((n, i) => n const FACE_SCALE = 0.6 const hexValue = computed(() => [0, 1, 2, 3].reduce((n, i) => n + (v(`b${i}`) << i), 0)) const segments = computed(() => sevenSegment(hexValue.value)) -const lit = computed(() => v(props.node.kind === 'input' || props.node.kind === 'button' || props.node.kind === 'clock' ? 'q' : 'a') === 1) +// Lamps and outputs show what they pass on (q): with a bubble, that is the inverted wire. +const lit = computed(() => v(props.node.kind === 'pixel' ? 'a' : 'q') === 1) const title = computed(() => { if (props.node.kind === 'chip') return chipDef.value ? `${chipDef.value.name}${chipDef.value.description ? `: ${chipDef.value.description}` : ''}\nDouble-click to open` : 'Missing component' return undefined diff --git a/app/components/editor/PinRow.vue b/app/components/editor/PinRow.vue index e094d43..4bf8182 100644 --- a/app/components/editor/PinRow.vue +++ b/app/components/editor/PinRow.vue @@ -16,7 +16,7 @@ const value = computed(() => { const id = n.value.id if (props.e.row) return String(ed.val(id, props.e.row.id)) if (n.value.kind === 'hex') return [0, 1, 2, 3].reduce((v, i) => v + (ed.val(id, `b${i}`) << i), 0).toString(16).toUpperCase() - return String(ed.val(id, ['output', 'lamp'].includes(n.value.kind) ? 'a' : 'q')) + return String(ed.val(id, 'q')) }) function flip() { diff --git a/app/lib/logic/analyze.ts b/app/lib/logic/analyze.ts index 7904006..4a9c11d 100644 --- a/app/lib/logic/analyze.ts +++ b/app/lib/logic/analyze.ts @@ -37,7 +37,7 @@ export function truthTable(getDef: DefLookup, defId: string): TruthTable | { err const ins = pins.inputs.map((_, k) => (i >> (n - 1 - k)) & 1) pins.inputs.forEach((p, k) => sim.setSource(p.key, ins[k] === 1)) const settled = sim.run(SETTLE_BUDGET) - rows.push({ ins, outs: pins.outputs.map(p => sim.pin(p.key, 'a')), settled }) + rows.push({ ins, outs: pins.outputs.map(p => sim.output(p.key)), settled }) } return { inputs: pins.inputs.map(p => p.label), outputs: pins.outputs.map(p => p.label), rows } } @@ -176,7 +176,7 @@ export function runSteps(getDef: DefLookup, defId: string, steps: TestStep[]): T for (const [l, v] of Object.entries(step.set)) sim.setSource(find(pins.inputs, l)!.key, v === 1) if (!sim.run(SETTLE_BUDGET)) return { pass: false, message: 'The circuit oscillates and never settles', failedStep: i } for (const [l, want] of Object.entries(step.expect)) { - const got = sim.pin(find(pins.outputs, l)!.key, 'a') + const got = sim.output(find(pins.outputs, l)!.key) if (got !== want) { const inputs = Object.entries(step.set).map(([k, v]) => `${k}=${v}`).join(' ') return { pass: false, message: `With ${inputs}: ${l} is ${got}, expected ${want}`, failedStep: i } diff --git a/app/lib/logic/compile.ts b/app/lib/logic/compile.ts index 545e980..0bff2b5 100644 --- a/app/lib/logic/compile.ts +++ b/app/lib/logic/compile.ts @@ -24,6 +24,8 @@ export interface Netlist { tracePinNet?: Map /** Inputs of lamps and displays inside the root's chips, keyed "chipNode/nodeId:pin", for drawing them on the chip. */ facePinNet: Map + /** What each of the root circuit's output pins puts out (keyed like ChipPins outputs), after any bubble on it. */ + outputs: Map /** User-driven sources in the root circuit (switches, buttons): nodeId → net. */ sources: Map clocks: Int32Array @@ -357,8 +359,8 @@ export function compile(getDef: DefLookup, rootId: string, opts: { trace?: boole } const root = getDef(rootId) - if (root) expand(root, null, '', []) - else errors.push('Circuit not found') + const rootOut = root ? expand(root, null, '', []) : new Map() + if (!root) errors.push('Circuit not found') // Renumber union-find roots to 0..netCount-1, constants first. const idx = new Int32Array(parent.length).fill(-1) @@ -420,6 +422,7 @@ export function compile(getDef: DefLookup, rootId: string, opts: { trace?: boole pinNet, tracePinNet, facePinNet, + outputs: mapNets(rootOut), sources: srcs, clocks: clockNets, clockHz: Float64Array.from(clockList.map(c => c.hz)), diff --git a/app/lib/logic/puzzles.ts b/app/lib/logic/puzzles.ts index 37c0704..f4b132f 100644 --- a/app/lib/logic/puzzles.ts +++ b/app/lib/logic/puzzles.ts @@ -71,7 +71,7 @@ export function describeCircuit(get: DefLookup, defId: string): { inputs: string if (!sim.run(STEP_BUDGET)) throw new Error('The circuit keeps oscillating, so it has no clear answer') steps.push({ set: Object.fromEntries(pins.inputs.map((p, k) => [p.label, ins[k]!])), - expect: Object.fromEntries(pins.outputs.map(p => [p.label, sim.pin(p.key, 'a')])), + expect: Object.fromEntries(pins.outputs.map(p => [p.label, sim.output(p.key)])), }) } return { inputs: pins.inputs.map(p => p.label), outputs: pins.outputs.map(p => p.label), steps, sequential } @@ -162,7 +162,7 @@ export function scoreSolution(components: ComponentDef[], defId: string, spec: P for (const [l, v] of Object.entries(step.set)) sim.setSource(find(pins.inputs, l)!.key, v === 1) if (!settle()) return out(false, left <= 0 ? 'The circuit takes too long to settle' : 'The circuit oscillates and never settles', delay) for (const [l, want] of Object.entries(step.expect)) { - const got = sim.pin(find(pins.outputs, l)!.key, 'a') + const got = sim.output(find(pins.outputs, l)!.key) if (got !== want) { const ins = Object.entries(step.set).map(([k, v]) => `${k}=${v}`).join(' ') return out(false, `${spec.steps.length > 1 << spec.inputs.length ? `Step ${i + 1}: with` : 'With'} ${ins}: ${l} is ${got}, expected ${want}`, delay) diff --git a/app/lib/logic/sim.ts b/app/lib/logic/sim.ts index 46935aa..5eedb3a 100644 --- a/app/lib/logic/sim.ts +++ b/app/lib/logic/sim.ts @@ -112,6 +112,12 @@ export class Simulator { return net === undefined ? 0 : this.values[net]! } + /** What an output pin of the root circuit puts out (a ChipPins output key), after any bubble on it. */ + output(key: string): number { + const net = this.nl.outputs.get(key) + return net === undefined ? 0 : this.values[net]! + } + /** Value at a lamp or display input inside one of the root circuit's chips. */ face(chipNode: string, node: string, pin: string): number { const net = this.nl.facePinNet.get(`${chipNode}/${node}:${pin}`) diff --git a/app/lib/logic/wide.ts b/app/lib/logic/wide.ts index 53803a8..daf9945 100644 --- a/app/lib/logic/wide.ts +++ b/app/lib/logic/wide.ts @@ -36,7 +36,7 @@ function table(get: DefLookup, base: string): number[] | null { sim.setSource(pins.inputs[0]!.key, (i & 2) !== 0) sim.setSource(pins.inputs[1]!.key, (i & 1) !== 0) if (!sim.run(200_000)) return null - out.push(sim.pin(pins.outputs[0]!.key, 'a')) + out.push(sim.output(pins.outputs[0]!.key)) } return out } diff --git a/tests/signals.test.ts b/tests/signals.test.ts index d49fd38..ea73693 100644 --- a/tests/signals.test.ts +++ b/tests/signals.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { nandCounts, stats } from '../app/lib/logic/analyze' +import { nandCounts, runSteps, stats, truthTable } from '../app/lib/logic/analyze' import { transistorBreakdown } from '../app/lib/logic/bulk' import { compile } from '../app/lib/logic/compile' import { chipPins } from '../app/lib/logic/geometry' @@ -130,3 +130,53 @@ describe('inverted inputs', () => { expect(stats(compile(lookup(p), 'outer')).nands).toBe(counts.get('outer')) }) }) + +describe('outputs as the checks read them', () => { + it('a bubble on an output pin counts in the truth table and the challenge check', () => { + // NOT: one NAND fed twice gives !A; a bubble on Q inverts it back to A. + const { p, def } = circuit( + [ + { id: 'a', kind: 'input', x: 0, y: 0, label: 'A' }, + { id: 'g', kind: 'nand', x: 6, y: 0 }, + { id: 'q', kind: 'output', x: 14, y: 0, label: 'Q', invert: ['a'] }, + ], + [ + { id: 'w1', from: { node: 'a', pin: 'q' }, to: { node: 'g', pin: 'a' } }, + { id: 'w2', from: { node: 'a', pin: 'q' }, to: { node: 'g', pin: 'b' } }, + { id: 'w3', from: { node: 'g', pin: 'q' }, to: { node: 'q', pin: 'a' } }, + ], + ) + const t = truthTable(lookup(p), def.id) + expect('rows' in t && t.rows.map(r => r.outs[0])).toEqual([0, 1]) + const steps = [ + { set: { A: 0 }, expect: { Q: 0 } }, + { set: { A: 1 }, expect: { Q: 1 } }, + ] + expect(runSteps(lookup(p), def.id, steps).pass).toBe(true) + // The output pin shows what leaves the circuit; its input pin shows the wire. + const s = run(p, def, { a: true }) + expect([s.pin('q', 'a'), s.pin('q', 'q'), s.output('q')]).toEqual([0, 1, 1]) + }) + + it('rows of an output panel are outputs in the truth table', () => { + const { p, def } = circuit( + [ + { id: 'a', kind: 'input', x: 0, y: 0, label: 'A' }, + { id: 'g', kind: 'nand', x: 6, y: 0 }, + { id: 'op', kind: 'outpanel', x: 14, y: 0, rows: [{ id: 'r0', type: 'switch', label: 'Y' }, { id: 'r1', type: 'switch', label: 'Z' }] }, + ], + [ + { id: 'w1', from: { node: 'a', pin: 'q' }, to: { node: 'g', pin: 'a' } }, + { id: 'w2', from: { node: 'a', pin: 'q' }, to: { node: 'g', pin: 'b' } }, + { id: 'w3', from: { node: 'g', pin: 'q' }, to: { node: 'op', pin: 'r0' } }, + { id: 'w4', from: { node: 'a', pin: 'q' }, to: { node: 'op', pin: 'r1' } }, + ], + ) + const t = truthTable(lookup(p), def.id) + expect('rows' in t && t.outputs).toEqual(['Y', 'Z']) + expect('rows' in t && t.rows.map(r => r.outs)).toEqual([ + [1, 0], + [0, 1], + ]) + }) +})