166 lines
6.3 KiB
TypeScript
166 lines
6.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
import { runSteps, stats, truthTable } from '../app/lib/logic/analyze'
|
||
import { CHALLENGES, challengeSteps } from '../app/lib/logic/challenges'
|
||
import { compile } from '../app/lib/logic/compile'
|
||
import { chipPins } from '../app/lib/logic/geometry'
|
||
import { containsDef, createProject, lookup, packageSelection, placeable, pruneWires } from '../app/lib/logic/ops'
|
||
import { Simulator } from '../app/lib/logic/sim'
|
||
import type { Project } from '../app/lib/logic/types'
|
||
|
||
const project = (): Project => createProject({ examples: true })
|
||
|
||
const solvedBy: Record<string, string> = {
|
||
not: 'ex-not', and: 'ex-and', or: 'ex-or', nor: 'ex-nor', xor: 'ex-xor', xnor: 'ex-xnor',
|
||
mux: 'ex-mux', ha: 'ex-ha', fa: 'ex-fa', sr: 'ex-sr', dlatch: 'ex-dlatch', dff: 'ex-dff',
|
||
}
|
||
|
||
describe('example library built from NAND', () => {
|
||
const p = project()
|
||
const get = lookup(p)
|
||
|
||
for (const [challenge, defId] of Object.entries(solvedBy)) {
|
||
it(`${defId} passes the ${challenge} challenge`, () => {
|
||
const c = CHALLENGES.find(c => c.id === challenge)!
|
||
const res = runSteps(get, defId, challengeSteps(c))
|
||
expect(res.message).toMatch(/pass/)
|
||
expect(res.pass).toBe(true)
|
||
})
|
||
}
|
||
|
||
it('compiles every example without errors', () => {
|
||
for (const d of p.components) expect(compile(get, d.id).errors, d.name).toEqual([])
|
||
})
|
||
|
||
it('Adder 4 adds all 512 input combinations', () => {
|
||
const t = truthTable(get, 'ex-add4')
|
||
if ('error' in t) throw new Error(t.error)
|
||
expect(t.inputs).toEqual(['A0', 'A1', 'A2', 'A3', 'B0', 'B1', 'B2', 'B3', 'Cin'])
|
||
expect(t.outputs).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout'])
|
||
expect(t.rows).toHaveLength(512)
|
||
for (const r of t.rows) {
|
||
const num = (bits: number[]) => bits.reduce((n, b, i) => n + (b << i), 0)
|
||
const sum = num(r.ins.slice(0, 4)) + num(r.ins.slice(4, 8)) + r.ins[8]!
|
||
expect(num(r.outs)).toBe(sum)
|
||
}
|
||
})
|
||
|
||
it('Increment 4 adds one', () => {
|
||
const t = truthTable(get, 'ex-inc4')
|
||
if ('error' in t) throw new Error(t.error)
|
||
for (const r of t.rows) {
|
||
const num = (bits: number[]) => bits.reduce((n, b, i) => n + (b << i), 0)
|
||
expect(num(r.outs)).toBe(num(r.ins) + 1)
|
||
}
|
||
})
|
||
|
||
it('Counter 4 starts at 0, resets, counts clock edges and wraps at 16', () => {
|
||
const def = get('ex-cnt4')!
|
||
const pins = chipPins(def)
|
||
const sim = new Simulator(compile(get, 'ex-cnt4'))
|
||
sim.run()
|
||
const set = (label: string, v: number) => {
|
||
sim.setSource(pins.inputs.find(p => p.label === label)!.key, v === 1)
|
||
expect(sim.run()).toBe(true)
|
||
}
|
||
const value = () => pins.outputs.reduce((n, p, i) => n + (sim.pin(p.key, 'a') << i), 0)
|
||
const tick = () => {
|
||
set('CLK', 1)
|
||
set('CLK', 0)
|
||
}
|
||
expect(value()).toBe(0)
|
||
set('RST', 1)
|
||
tick()
|
||
expect(value()).toBe(0)
|
||
set('RST', 0)
|
||
for (let i = 1; i <= 20; i++) {
|
||
tick()
|
||
expect(value()).toBe(i % 16)
|
||
}
|
||
})
|
||
|
||
it('reports NAND count, delay and memory', () => {
|
||
expect(stats(compile(get, 'ex-not'))).toEqual({ nands: 1, sequential: false, delay: 1 })
|
||
expect(stats(compile(get, 'ex-xor'))).toEqual({ nands: 4, sequential: false, delay: 3 })
|
||
expect(stats(compile(get, 'ex-fa')).nands).toBe(15)
|
||
expect(stats(compile(get, 'ex-dff')).sequential).toBe(true)
|
||
})
|
||
})
|
||
|
||
describe('simulator', () => {
|
||
it('a NOT wired to itself oscillates and never settles', () => {
|
||
const p = project()
|
||
p.components.push({
|
||
id: 'ring', name: 'Ring', symbol: 'box',
|
||
nodes: [{ id: 'g', kind: 'chip', chip: 'ex-not', x: 0, y: 0 }],
|
||
wires: [],
|
||
})
|
||
const pin = chipPins(lookup(p)('ex-not')!)
|
||
p.components.at(-1)!.wires.push({ id: 'w', from: { node: 'g', pin: pin.outputs[0]!.key }, to: { node: 'g', pin: pin.inputs[0]!.key } })
|
||
const sim = new Simulator(compile(lookup(p), 'ring'))
|
||
expect(sim.run(10_000)).toBe(false)
|
||
})
|
||
|
||
it('keeps latch state across a recompile', () => {
|
||
const p = project()
|
||
const get = lookup(p)
|
||
const pins = chipPins(get('ex-sr')!)
|
||
const key = (l: string) => pins.inputs.find(p => p.label === l)!.key
|
||
const sim = new Simulator(compile(get, 'ex-sr'))
|
||
sim.run()
|
||
sim.setSource(key('R'), true)
|
||
sim.run()
|
||
sim.setSource(key('R'), false)
|
||
sim.setSource(key('S'), true)
|
||
sim.run()
|
||
sim.setSource(key('S'), false)
|
||
sim.run()
|
||
const q = pins.outputs[0]!.key
|
||
expect(sim.pin(q, 'a')).toBe(1)
|
||
const again = new Simulator(compile(get, 'ex-sr'), sim.carry())
|
||
again.run()
|
||
expect(again.pin(q, 'a')).toBe(1)
|
||
})
|
||
})
|
||
|
||
describe('project operations', () => {
|
||
it('packages a selection into a new component without changing behaviour', () => {
|
||
const p = project()
|
||
const xor = p.components.find(d => d.id === 'ex-xor')!
|
||
const nands = new Set(xor.nodes.filter(n => n.kind === 'nand').map(n => n.id))
|
||
const res = packageSelection(p, 'ex-xor', nands, 'Core')
|
||
if (!res.ok) throw new Error(res.error)
|
||
expect(xor.nodes.filter(n => n.kind === 'nand')).toHaveLength(0)
|
||
const pins = chipPins(res.def)
|
||
expect(pins.inputs.map(p => p.label)).toEqual(['A', 'B'])
|
||
expect(pins.outputs.map(p => p.label)).toEqual(['Q'])
|
||
const xorChallenge = CHALLENGES.find(c => c.id === 'xor')!
|
||
expect(runSteps(lookup(p), 'ex-xor', challengeSteps(xorChallenge)).pass).toBe(true)
|
||
expect(runSteps(lookup(p), res.def.id, challengeSteps(xorChallenge)).pass).toBe(true)
|
||
})
|
||
|
||
it('refuses to package the circuit’s own pins', () => {
|
||
const p = project()
|
||
const not = p.components.find(d => d.id === 'ex-not')!
|
||
const res = packageSelection(p, 'ex-not', new Set(not.nodes.map(n => n.id)), 'X')
|
||
expect(res.ok).toBe(false)
|
||
})
|
||
|
||
it('drops wires to pins that were removed from a component', () => {
|
||
const p = project()
|
||
const not = p.components.find(d => d.id === 'ex-not')!
|
||
const before = p.components.find(d => d.id === 'ex-and')!.wires.length
|
||
not.nodes = not.nodes.filter(n => n.kind !== 'output')
|
||
expect(pruneWires(p)).toBe(true)
|
||
expect(p.components.find(d => d.id === 'ex-and')!.wires.length).toBe(before - 1)
|
||
})
|
||
|
||
it('never lets a component contain itself', () => {
|
||
const p = project()
|
||
expect(containsDef(p, 'ex-and', 'ex-not')).toBe(true)
|
||
const ids = placeable(p, 'ex-not').map(d => d.id)
|
||
expect(ids).not.toContain('ex-not')
|
||
expect(ids).not.toContain('ex-and')
|
||
expect(ids).toContain('main')
|
||
})
|
||
})
|