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) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-23 12:27:12 +02:00
parent 24bc66f798
commit 97c650a2b8
9 changed files with 77 additions and 10 deletions

View File

@ -1289,6 +1289,13 @@ function openMenuAt(target: Element, at: Pt, x: number, y: number) {
} }
const onThing = !!(nodeId || wireId || junction) const onThing = !!(nodeId || wireId || junction)
const entries = build(onThing ? { kind: 'selection' } : { kind: 'canvas', at }, canvasOps) 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 menu.value = entries.length ? { x: x + 2, y: y + 2, entries } : null
} }

View File

@ -134,7 +134,8 @@ const faceHex = (inner: string) => sevenSegment([0, 1, 2, 3].reduce((n, i) => n
const FACE_SCALE = 0.6 const FACE_SCALE = 0.6
const hexValue = computed(() => [0, 1, 2, 3].reduce((n, i) => n + (v(`b${i}`) << i), 0)) const hexValue = computed(() => [0, 1, 2, 3].reduce((n, i) => n + (v(`b${i}`) << i), 0))
const segments = computed(() => sevenSegment(hexValue.value)) 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(() => { 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' 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 return undefined

View File

@ -16,7 +16,7 @@ const value = computed(() => {
const id = n.value.id const id = n.value.id
if (props.e.row) return String(ed.val(id, props.e.row.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() 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() { function flip() {

View File

@ -37,7 +37,7 @@ export function truthTable(getDef: DefLookup, defId: string): TruthTable | { err
const ins = pins.inputs.map((_, k) => (i >> (n - 1 - k)) & 1) const ins = pins.inputs.map((_, k) => (i >> (n - 1 - k)) & 1)
pins.inputs.forEach((p, k) => sim.setSource(p.key, ins[k] === 1)) pins.inputs.forEach((p, k) => sim.setSource(p.key, ins[k] === 1))
const settled = sim.run(SETTLE_BUDGET) 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 } 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) 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 } 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)) { 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) { if (got !== want) {
const inputs = Object.entries(step.set).map(([k, v]) => `${k}=${v}`).join(' ') 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 } return { pass: false, message: `With ${inputs}: ${l} is ${got}, expected ${want}`, failedStep: i }

View File

@ -24,6 +24,8 @@ export interface Netlist {
tracePinNet?: Map<string, number> tracePinNet?: Map<string, number>
/** Inputs of lamps and displays inside the root's chips, keyed "chipNode/nodeId:pin", for drawing them on the chip. */ /** Inputs of lamps and displays inside the root's chips, keyed "chipNode/nodeId:pin", for drawing them on the chip. */
facePinNet: Map<string, number> facePinNet: Map<string, number>
/** What each of the root circuit's output pins puts out (keyed like ChipPins outputs), after any bubble on it. */
outputs: Map<string, number>
/** User-driven sources in the root circuit (switches, buttons): nodeId → net. */ /** User-driven sources in the root circuit (switches, buttons): nodeId → net. */
sources: Map<string, number> sources: Map<string, number>
clocks: Int32Array clocks: Int32Array
@ -357,8 +359,8 @@ export function compile(getDef: DefLookup, rootId: string, opts: { trace?: boole
} }
const root = getDef(rootId) const root = getDef(rootId)
if (root) expand(root, null, '', []) const rootOut = root ? expand(root, null, '', []) : new Map<string, number>()
else errors.push('Circuit not found') if (!root) errors.push('Circuit not found')
// Renumber union-find roots to 0..netCount-1, constants first. // Renumber union-find roots to 0..netCount-1, constants first.
const idx = new Int32Array(parent.length).fill(-1) const idx = new Int32Array(parent.length).fill(-1)
@ -420,6 +422,7 @@ export function compile(getDef: DefLookup, rootId: string, opts: { trace?: boole
pinNet, pinNet,
tracePinNet, tracePinNet,
facePinNet, facePinNet,
outputs: mapNets(rootOut),
sources: srcs, sources: srcs,
clocks: clockNets, clocks: clockNets,
clockHz: Float64Array.from(clockList.map(c => c.hz)), clockHz: Float64Array.from(clockList.map(c => c.hz)),

View File

@ -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') if (!sim.run(STEP_BUDGET)) throw new Error('The circuit keeps oscillating, so it has no clear answer')
steps.push({ steps.push({
set: Object.fromEntries(pins.inputs.map((p, k) => [p.label, ins[k]!])), 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 } 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) 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) 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)) { 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) { if (got !== want) {
const ins = Object.entries(step.set).map(([k, v]) => `${k}=${v}`).join(' ') 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) return out(false, `${spec.steps.length > 1 << spec.inputs.length ? `Step ${i + 1}: with` : 'With'} ${ins}: ${l} is ${got}, expected ${want}`, delay)

View File

@ -112,6 +112,12 @@ export class Simulator {
return net === undefined ? 0 : this.values[net]! 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. */ /** Value at a lamp or display input inside one of the root circuit's chips. */
face(chipNode: string, node: string, pin: string): number { face(chipNode: string, node: string, pin: string): number {
const net = this.nl.facePinNet.get(`${chipNode}/${node}:${pin}`) const net = this.nl.facePinNet.get(`${chipNode}/${node}:${pin}`)

View File

@ -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[0]!.key, (i & 2) !== 0)
sim.setSource(pins.inputs[1]!.key, (i & 1) !== 0) sim.setSource(pins.inputs[1]!.key, (i & 1) !== 0)
if (!sim.run(200_000)) return null 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 return out
} }

View File

@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest' 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 { transistorBreakdown } from '../app/lib/logic/bulk'
import { compile } from '../app/lib/logic/compile' import { compile } from '../app/lib/logic/compile'
import { chipPins } from '../app/lib/logic/geometry' 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')) 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],
])
})
})