97 lines
4.3 KiB
TypeScript
97 lines
4.3 KiB
TypeScript
|
|
import { describe, expect, it } from 'vitest'
|
||
|
|
import { nandCounts, TRANSISTORS_PER_NAND } from '../app/lib/logic/analyze'
|
||
|
|
import { arrangeNodes, duplicateNames, entryLabel, ioList, numberedNames, sharedFamily, transistorBreakdown } from '../app/lib/logic/bulk'
|
||
|
|
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: [],
|
||
|
|
}
|
||
|
|
|
||
|
|
it('lists pins first in pin order, then the parts that stay inside, with panel rows', () => {
|
||
|
|
const { inputs, outputs } = ioList(def)
|
||
|
|
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'])
|
||
|
|
expect(outputs.map(e => e.node.id)).toEqual(['q', 'l'])
|
||
|
|
expect(inputs.map(entryLabel)).toEqual(['A', 'B', '', 'EN', ''])
|
||
|
|
})
|
||
|
|
|
||
|
|
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)
|
||
|
|
})
|
||
|
|
})
|