A Loxone-style working aids: named signals, inverted inputs, a project check, values on cables, search, printable sheets; wire ends move by dragging
- Send signal / Use signal: name a signal (CLK, RESET) once and use it anywhere in the circuit without a wire; selecting one lights up all of them, a use with no sender shows dashed
- Inverted inputs: Alt + click an input (or the menu/settings) puts a bubble on it; it reads the signal inverted through one NAND, counted in every transistor total
- Check (Circuit tab): open inputs, parts that drive nothing, duplicate pin names, unsent or doubly sent signals, loops that never settle; click one to show it. Open inputs get an orange marker on the canvas. Components no other circuit uses are listed with a delete
- Cables show the number they carry (= 13 · 0x0D); hovering a wire tells where it runs and its value
- Ctrl+F finds parts, signals, panel rows, notes and components in every circuit, with where each component is used
- Ctrl+P prints the circuit as a sheet with a title block on white paper (or save as PDF); also as an SVG file
- + and − under a selected gate change its number of inputs; swaps keep bubbles in place
- Drag a wire by its end to move that end to another pin; drop a dragged wire on another wire to tap its signal or feed what it fed
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:26:59 +00:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2026-10-23 10:24:35 +00:00
|
|
|
import { nandCounts, runSteps, stats, truthTable } from '../app/lib/logic/analyze'
|
A Loxone-style working aids: named signals, inverted inputs, a project check, values on cables, search, printable sheets; wire ends move by dragging
- Send signal / Use signal: name a signal (CLK, RESET) once and use it anywhere in the circuit without a wire; selecting one lights up all of them, a use with no sender shows dashed
- Inverted inputs: Alt + click an input (or the menu/settings) puts a bubble on it; it reads the signal inverted through one NAND, counted in every transistor total
- Check (Circuit tab): open inputs, parts that drive nothing, duplicate pin names, unsent or doubly sent signals, loops that never settle; click one to show it. Open inputs get an orange marker on the canvas. Components no other circuit uses are listed with a delete
- Cables show the number they carry (= 13 · 0x0D); hovering a wire tells where it runs and its value
- Ctrl+F finds parts, signals, panel rows, notes and components in every circuit, with where each component is used
- Ctrl+P prints the circuit as a sheet with a title block on white paper (or save as PDF); also as an SVG file
- + and − under a selected gate change its number of inputs; swaps keep bubbles in place
- Drag a wire by its end to move that end to another pin; drop a dragged wire on another wire to tap its signal or feed what it fed
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:26:59 +00:00
|
|
|
import { transistorBreakdown } from '../app/lib/logic/bulk'
|
|
|
|
|
import { compile } from '../app/lib/logic/compile'
|
|
|
|
|
import { chipPins } from '../app/lib/logic/geometry'
|
|
|
|
|
import { createProject, lookup, swapNode } from '../app/lib/logic/ops'
|
|
|
|
|
import { Simulator } from '../app/lib/logic/sim'
|
|
|
|
|
import type { ComponentDef, Project } from '../app/lib/logic/types'
|
|
|
|
|
|
|
|
|
|
function circuit(nodes: ComponentDef['nodes'], wires: ComponentDef['wires']): { p: Project; def: ComponentDef } {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const def: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes, wires }
|
|
|
|
|
p.components.push(def)
|
|
|
|
|
return { p, def }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const run = (p: Project, def: ComponentDef, on: Record<string, boolean>) => {
|
|
|
|
|
const s = new Simulator(compile(lookup(p), def.id))
|
|
|
|
|
for (const [k, v] of Object.entries(on)) s.setSource(k, v)
|
|
|
|
|
s.run()
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('named signals', () => {
|
|
|
|
|
it('a reference carries what is wired into the tag of its name, without a wire', () => {
|
|
|
|
|
const { p, def } = circuit(
|
|
|
|
|
[
|
|
|
|
|
{ id: 'clk', kind: 'input', x: 0, y: 0 },
|
|
|
|
|
{ id: 't', kind: 'tag', x: 4, y: 0, label: 'CLK' },
|
|
|
|
|
{ id: 'r1', kind: 'ref', x: 20, y: 10, label: 'CLK' },
|
|
|
|
|
{ id: 'r2', kind: 'ref', x: 20, y: 20, label: 'CLK' },
|
|
|
|
|
{ id: 'l1', kind: 'lamp', x: 30, y: 10 },
|
|
|
|
|
{ id: 'l2', kind: 'lamp', x: 30, y: 20 },
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
{ id: 'a', from: { node: 'clk', pin: 'q' }, to: { node: 't', pin: 'a' } },
|
|
|
|
|
{ id: 'b', from: { node: 'r1', pin: 'q' }, to: { node: 'l1', pin: 'a' } },
|
|
|
|
|
{ id: 'c', from: { node: 'r2', pin: 'q' }, to: { node: 'l2', pin: 'a' } },
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
expect(run(p, def, { clk: false }).pin('l1', 'a')).toBe(0)
|
|
|
|
|
const s = run(p, def, { clk: true })
|
|
|
|
|
expect([s.pin('l1', 'a'), s.pin('l2', 'a')]).toEqual([1, 1])
|
|
|
|
|
// Another name: nothing sends it, it reads 0.
|
|
|
|
|
def.nodes.find(n => n.id === 'r2')!.label = 'RESET'
|
|
|
|
|
expect(run(p, def, { clk: true }).pin('l2', 'a')).toBe(0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('names stay inside their circuit', () => {
|
|
|
|
|
const { p, def } = circuit(
|
|
|
|
|
[
|
|
|
|
|
{ id: 'sw', kind: 'input', x: 0, y: 0 },
|
|
|
|
|
{ id: 't', kind: 'tag', x: 4, y: 0, label: 'X' },
|
|
|
|
|
],
|
|
|
|
|
[{ id: 'a', from: { node: 'sw', pin: 'q' }, to: { node: 't', pin: 'a' } }],
|
|
|
|
|
)
|
|
|
|
|
const inner: ComponentDef = {
|
|
|
|
|
id: 'inner',
|
|
|
|
|
name: 'Inner',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'r', kind: 'ref', x: 0, y: 0, label: 'X' },
|
|
|
|
|
{ id: 'o', kind: 'output', x: 6, y: 0, label: 'Q' },
|
|
|
|
|
],
|
|
|
|
|
wires: [{ id: 'w', from: { node: 'r', pin: 'q' }, to: { node: 'o', pin: 'a' } }],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(inner)
|
|
|
|
|
def.nodes.push({ id: 'c', kind: 'chip', chip: 'inner', x: 10, y: 0 }, { id: 'l', kind: 'lamp', x: 20, y: 0 })
|
|
|
|
|
def.wires.push({ id: 'b', from: { node: 'c', pin: 'o' }, to: { node: 'l', pin: 'a' } })
|
|
|
|
|
expect(run(p, def, { sw: true }).pin('l', 'a')).toBe(0)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('inverted inputs', () => {
|
|
|
|
|
it('a bubble inverts the input through one NAND, and the counts agree', () => {
|
|
|
|
|
const { p, def } = circuit(
|
|
|
|
|
[
|
|
|
|
|
{ id: 'a', kind: 'input', x: 0, y: 0 },
|
|
|
|
|
{ id: 'b', kind: 'input', x: 0, y: 3 },
|
|
|
|
|
{ id: 'g', kind: 'nand', x: 6, y: 0, invert: ['a'] },
|
|
|
|
|
{ id: 'l', kind: 'lamp', x: 14, y: 1, invert: ['a'] },
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
{ id: 'w1', from: { node: 'a', pin: 'q' }, to: { node: 'g', pin: 'a' } },
|
|
|
|
|
{ id: 'w2', from: { node: 'b', pin: 'q' }, to: { node: 'g', pin: 'b' } },
|
|
|
|
|
{ id: 'w3', from: { node: 'g', pin: 'q' }, to: { node: 'l', pin: 'a' } },
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
// g = NAND(!a, b); the lamp shows its inverse: AND(!a, b).
|
|
|
|
|
const table = [
|
|
|
|
|
[false, false],
|
|
|
|
|
[false, true],
|
|
|
|
|
[true, false],
|
|
|
|
|
[true, true],
|
|
|
|
|
].map(([a, b]) => run(p, def, { a: a!, b: b! }).pin('l', 'q'))
|
|
|
|
|
expect(table).toEqual([0, 1, 0, 0])
|
|
|
|
|
// The wire's own value shows on the pin, before the bubble.
|
|
|
|
|
expect(run(p, def, { a: true, b: false }).pin('g', 'a')).toBe(1)
|
|
|
|
|
const nl = compile(lookup(p), def.id)
|
|
|
|
|
expect(stats(nl).nands).toBe(3)
|
|
|
|
|
expect(nandCounts(p.components).get('root')).toBe(3)
|
|
|
|
|
const lines = transistorBreakdown(def, nandCounts(p.components), lookup(p))
|
|
|
|
|
expect(lines.find(l => l.key === 'invert')).toMatchObject({ count: 2, total: 8 })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('swapping a part keeps its bubbles on the inputs in the same position', () => {
|
|
|
|
|
const { p, def } = circuit([{ id: 'g', kind: 'nand', x: 0, y: 0, invert: ['b'] }], [])
|
|
|
|
|
const or = p.components.find(d => d.id === 'ex-or')!
|
|
|
|
|
swapNode(p, def, 'g', { kind: 'chip', chip: or.id })
|
|
|
|
|
const g = def.nodes[0]!
|
|
|
|
|
const ins = chipPins(or).inputs.map(x => x.key)
|
|
|
|
|
expect(g.invert).toEqual([ins[1]])
|
|
|
|
|
swapNode(p, def, 'g', { kind: 'lamp' })
|
|
|
|
|
expect(g.invert).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('bubbles on component inputs count inside every circuit that places it', () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const not = p.components.find(d => d.id === 'ex-not')!
|
|
|
|
|
const outer: ComponentDef = {
|
|
|
|
|
id: 'outer',
|
|
|
|
|
name: 'Outer',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [{ id: 'c', kind: 'chip', chip: not.id, x: 0, y: 0, invert: [...new Set(['x', ...not.nodes.filter(n => n.kind === 'input').map(n => n.id)])] }],
|
|
|
|
|
wires: [],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(outer)
|
|
|
|
|
const counts = nandCounts(p.components)
|
|
|
|
|
expect(counts.get('outer')).toBe(counts.get(not.id)! + 1)
|
|
|
|
|
expect(stats(compile(lookup(p), 'outer')).nands).toBe(counts.get('outer'))
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-10-23 10:24:35 +00:00
|
|
|
|
|
|
|
|
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],
|
|
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
})
|