import { describe, expect, it } from 'vitest' import { nandCounts, stats } 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' 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) => { 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')) }) })