A right panel in tabs, Pins list, bulk editing, transistor breakdown, Settings dialog
- Right panel tabs: Selection, Circuit, Pins, Test. Display and sound
preferences move to a Settings dialog (gear in the top bar).
- Pins tab: every input and output in pin order with live values; rename in
place (Enter goes to the next), number a whole side from a pattern, flag
duplicate names, centre the canvas on a part.
- Several selected parts: narrow by type, swap all, rename all from a
pattern, set switches to 0/1 or flip, shared colour/frequency/size, align
and spread.
- Transistors on every part (NAND 4, switches and lamps 0), NAND 4T in the
palette, and a per-part breakdown of the circuit. Test: the count equals
4 x the gates the simulator builds, for every example and wide gate.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 07:29:55 +00:00
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
|
import { nandCounts, TRANSISTORS_PER_NAND } from '../app/lib/logic/analyze'
|
2026-10-21 08:09:43 +00:00
|
|
|
import { arrangeNodes, duplicateNames, entryLabel, ioList, numberedNames, seriesLinks, sharedFamily, transistorBreakdown } from '../app/lib/logic/bulk'
|
A right panel in tabs, Pins list, bulk editing, transistor breakdown, Settings dialog
- Right panel tabs: Selection, Circuit, Pins, Test. Display and sound
preferences move to a Settings dialog (gear in the top bar).
- Pins tab: every input and output in pin order with live values; rename in
place (Enter goes to the next), number a whole side from a pattern, flag
duplicate names, centre the canvas on a part.
- Several selected parts: narrow by type, swap all, rename all from a
pattern, set switches to 0/1 or flip, shared colour/frequency/size, align
and spread.
- Transistors on every part (NAND 4, switches and lamps 0), NAND 4T in the
palette, and a per-part breakdown of the circuit. Test: the count equals
4 x the gates the simulator builds, for every example and wide gate.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 07:29:55 +00:00
|
|
|
import { compile } from '../app/lib/logic/compile'
|
|
|
|
|
import { createProject, lookup } from '../app/lib/logic/ops'
|
|
|
|
|
import type { CircuitNode, ComponentDef } from '../app/lib/logic/types'
|
|
|
|
|
import { NAND_BASE, wideGate } from '../app/lib/logic/wide'
|
|
|
|
|
|
|
|
|
|
const node = (id: string, kind: CircuitNode['kind'], x: number, y: number, extra: Partial<CircuitNode> = {}): CircuitNode => ({ id, kind, x, y, ...extra })
|
|
|
|
|
|
|
|
|
|
describe('numbered names', () => {
|
|
|
|
|
it('replaces # runs with a padded counter', () => {
|
|
|
|
|
expect(numberedNames('D#', 3)).toEqual(['D0', 'D1', 'D2'])
|
|
|
|
|
expect(numberedNames('Q##', 2, 9)).toEqual(['Q09', 'Q10'])
|
|
|
|
|
expect(numberedNames('Bit # of #', 2, 1)).toEqual(['Bit 1 of 1', 'Bit 2 of 2'])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('appends the number when there is no #, unless it is a single part', () => {
|
|
|
|
|
expect(numberedNames('IN', 3, 1)).toEqual(['IN1', 'IN2', 'IN3'])
|
|
|
|
|
expect(numberedNames('Reset', 1)).toEqual(['Reset'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('the list of inputs and outputs', () => {
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 'd',
|
|
|
|
|
name: 'D',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
node('b', 'input', 0, 4, { label: 'B' }),
|
|
|
|
|
node('a', 'input', 0, 0, { label: 'A' }),
|
|
|
|
|
node('clk', 'clock', 0, 10),
|
|
|
|
|
node('p', 'panel', 0, 14, { rows: [{ id: 'r1', type: 'switch', label: 'EN' }, { id: 'r2', type: 'button' }] }),
|
|
|
|
|
node('l', 'lamp', 10, 4, { label: 'A' }),
|
|
|
|
|
node('q', 'output', 10, 0, { label: 'Q' }),
|
|
|
|
|
node('g', 'nand', 5, 0),
|
|
|
|
|
],
|
|
|
|
|
wires: [],
|
|
|
|
|
}
|
|
|
|
|
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
it('lists pins first in pin order (panel rows are pins too), then the parts that stay inside', () => {
|
A right panel in tabs, Pins list, bulk editing, transistor breakdown, Settings dialog
- Right panel tabs: Selection, Circuit, Pins, Test. Display and sound
preferences move to a Settings dialog (gear in the top bar).
- Pins tab: every input and output in pin order with live values; rename in
place (Enter goes to the next), number a whole side from a pattern, flag
duplicate names, centre the canvas on a part.
- Several selected parts: narrow by type, swap all, rename all from a
pattern, set switches to 0/1 or flip, shared colour/frequency/size, align
and spread.
- Transistors on every part (NAND 4, switches and lamps 0), NAND 4T in the
palette, and a per-part breakdown of the circuit. Test: the count equals
4 x the gates the simulator builds, for every example and wide gate.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 07:29:55 +00:00
|
|
|
const { inputs, outputs } = ioList(def)
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
expect(inputs.map(e => `${e.node.id}${e.row ? `/${e.row.id}` : ''}:${e.pin}`)).toEqual(['a:true', 'b:true', 'p/r1:true', 'p/r2:true', 'clk:false'])
|
A right panel in tabs, Pins list, bulk editing, transistor breakdown, Settings dialog
- Right panel tabs: Selection, Circuit, Pins, Test. Display and sound
preferences move to a Settings dialog (gear in the top bar).
- Pins tab: every input and output in pin order with live values; rename in
place (Enter goes to the next), number a whole side from a pattern, flag
duplicate names, centre the canvas on a part.
- Several selected parts: narrow by type, swap all, rename all from a
pattern, set switches to 0/1 or flip, shared colour/frequency/size, align
and spread.
- Transistors on every part (NAND 4, switches and lamps 0), NAND 4T in the
palette, and a per-part breakdown of the circuit. Test: the count equals
4 x the gates the simulator builds, for every example and wide gate.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 07:29:55 +00:00
|
|
|
expect(outputs.map(e => e.node.id)).toEqual(['q', 'l'])
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
expect(inputs.map(entryLabel)).toEqual(['A', 'B', 'EN', '', ''])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a panel kept inside lists its rows after the pins', () => {
|
|
|
|
|
const inside = { ...def, nodes: def.nodes.map(n => (n.kind === 'panel' ? { ...n, inner: true } : n)) }
|
|
|
|
|
const { inputs } = ioList(inside)
|
|
|
|
|
expect(inputs.map(e => `${e.node.id}${e.row ? `/${e.row.id}` : ''}:${e.pin}`)).toEqual(['a:true', 'b:true', 'clk:false', 'p/r1:false', 'p/r2:false'])
|
A right panel in tabs, Pins list, bulk editing, transistor breakdown, Settings dialog
- Right panel tabs: Selection, Circuit, Pins, Test. Display and sound
preferences move to a Settings dialog (gear in the top bar).
- Pins tab: every input and output in pin order with live values; rename in
place (Enter goes to the next), number a whole side from a pattern, flag
duplicate names, centre the canvas on a part.
- Several selected parts: narrow by type, swap all, rename all from a
pattern, set switches to 0/1 or flip, shared colour/frequency/size, align
and spread.
- Transistors on every part (NAND 4, switches and lamps 0), NAND 4T in the
palette, and a per-part breakdown of the circuit. Test: the count equals
4 x the gates the simulator builds, for every example and wide gate.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 07:29:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('flags names used twice on the same side only', () => {
|
|
|
|
|
const { inputs, outputs } = ioList(def)
|
|
|
|
|
expect([...duplicateNames(inputs)]).toEqual([])
|
|
|
|
|
expect([...duplicateNames([...inputs, ...outputs])]).toEqual(['a'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('selections', () => {
|
|
|
|
|
it('knows when parts can all be swapped to the same thing', () => {
|
|
|
|
|
expect(sharedFamily([node('1', 'input', 0, 0), node('2', 'clock', 0, 0)])).toBe('source')
|
|
|
|
|
expect(sharedFamily([node('1', 'nand', 0, 0), node('2', 'chip', 0, 0, { chip: 'x' })])).toBe('logic')
|
|
|
|
|
expect(sharedFamily([node('1', 'lamp', 0, 0), node('2', 'output', 0, 0)])).toBe('sink')
|
|
|
|
|
expect(sharedFamily([node('1', 'lamp', 0, 0), node('2', 'input', 0, 0)])).toBeNull()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('lines parts up and spreads them evenly', () => {
|
|
|
|
|
const ns = [node('1', 'input', 3, 0), node('2', 'input', 1, 7), node('3', 'input', 2, 3)]
|
|
|
|
|
arrangeNodes(ns, 'left')
|
|
|
|
|
expect(ns.map(n => n.x)).toEqual([1, 1, 1])
|
|
|
|
|
arrangeNodes(ns, 'spread-y')
|
|
|
|
|
expect(ns.map(n => n.y).sort((a, b) => a - b)).toEqual([0, 4, 7])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('transistors', () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
for (const base of [NAND_BASE, 'ex-and', 'ex-or', 'ex-xor', 'ex-nor']) for (const n of [3, 8, 16]) wideGate(p, base, n)
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const counts = nandCounts(p.components)
|
|
|
|
|
|
|
|
|
|
it('count 4 per NAND gate the simulator actually builds, in every component', () => {
|
|
|
|
|
for (const d of p.components) expect(counts.get(d.id) ?? 0, d.name).toBe(compile(get, d.id).gateOut.length)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('match the textbook NAND builds', () => {
|
|
|
|
|
const t = (id: string) => (counts.get(id) ?? 0) * TRANSISTORS_PER_NAND
|
|
|
|
|
expect([t('ex-not'), t('ex-and'), t('ex-or'), t('ex-xor'), t('ex-ha'), t('ex-fa')]).toEqual([4, 8, 12, 16, 24, 60])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('break a circuit down into its parts, adding up to the total', () => {
|
|
|
|
|
const fa = get('ex-fa')!
|
|
|
|
|
const lines = transistorBreakdown(fa, counts, get)
|
|
|
|
|
expect(lines.map(l => [l.name, l.count, l.each, l.total])).toEqual([
|
|
|
|
|
['Half Adder', 2, 24, 48],
|
|
|
|
|
['OR', 1, 12, 12],
|
|
|
|
|
])
|
|
|
|
|
expect(lines.reduce((s, l) => s + l.total, 0)).toBe((counts.get('ex-fa') ?? 0) * TRANSISTORS_PER_NAND)
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-10-21 08:09:43 +00:00
|
|
|
|
|
|
|
|
describe('wiring several parts at once', () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const add4 = get('ex-add4')!
|
|
|
|
|
const outsOfAdder = (n: CircuitNode) => {
|
|
|
|
|
const d = get(n.chip!)!
|
|
|
|
|
return d.nodes.filter(x => x.kind === 'output').sort((a, b) => a.y - b.y).map(x => x.id)
|
|
|
|
|
}
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 't',
|
|
|
|
|
name: 'T',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
node('add', 'chip', 0, 0, { chip: add4.id }),
|
|
|
|
|
...['Y0', 'Y1', 'Y2', 'Y3', 'Carry'].map((l, i) => node(`y${i}`, 'output', 20, i * 2, { label: l })),
|
|
|
|
|
],
|
|
|
|
|
wires: [],
|
|
|
|
|
}
|
|
|
|
|
const sel = ['y0', 'y1', 'y2', 'y3', 'y4']
|
|
|
|
|
const S = outsOfAdder(def.nodes[0]!)
|
|
|
|
|
|
|
|
|
|
it('drags one wire and connects every selected output to the next pin in turn', () => {
|
|
|
|
|
const links = seriesLinks(def, get, sel, { node: 'y0', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! })
|
|
|
|
|
expect(links.map(l => `${l.from.pin}>${l.to.node}`)).toEqual(S.map((s, i) => `${s}>y${i}`))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('counts from the dragged part, both ways, and stops at the last pin', () => {
|
|
|
|
|
const links = seriesLinks(def, get, sel, { node: 'y2', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! })
|
|
|
|
|
expect(links.map(l => `${l.from.pin}>${l.to.node}`)).toEqual([`${S[0]}>y2`, `${S[1]}>y3`, `${S[2]}>y4`])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('falls back to a single wire when the dragged part is not selected', () => {
|
|
|
|
|
expect(seriesLinks(def, get, ['y1'], { node: 'y0', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! })).toHaveLength(1)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('wiring several parts at once, mixed selection', () => {
|
|
|
|
|
it('skips selected parts without a matching pin instead of using up a target pin', () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 't',
|
|
|
|
|
name: 'T',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
node('add', 'chip', 0, 0, { chip: 'ex-add4' }),
|
|
|
|
|
node('y0', 'output', 20, 0),
|
|
|
|
|
node('note', 'note', 0, 1, { label: 'hi' }),
|
|
|
|
|
node('y1', 'output', 20, 2),
|
|
|
|
|
],
|
|
|
|
|
wires: [],
|
|
|
|
|
}
|
|
|
|
|
const S = get('ex-add4')!.nodes.filter(x => x.kind === 'output').sort((a, b) => a.y - b.y).map(x => x.id)
|
|
|
|
|
const links = seriesLinks(def, get, ['y0', 'note', 'y1'], { node: 'y0', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! })
|
|
|
|
|
expect(links.map(l => `${l.from.pin}>${l.to.node}`)).toEqual([`${S[0]}>y0`, `${S[1]}>y1`])
|
|
|
|
|
})
|
|
|
|
|
})
|