192 lines
10 KiB
Vue
192 lines
10 KiB
Vue
<script setup lang="ts">
|
|
// A test bench's columns and rows. Drive columns are its output pins (it
|
|
// sets them), check columns its input pins (it reads and checks them). Each
|
|
// row sets the drive columns, gives the circuit ticks, then checks.
|
|
import { checkCols, combinationRows, driveCols, recordExpected } from '~/lib/logic/bench'
|
|
import { uid } from '~/lib/logic/ops'
|
|
import type { BenchCol, BenchRow } from '~/lib/logic/types'
|
|
|
|
const ed = useEditor()
|
|
const close = () => (ed.benchOpen.value = null)
|
|
const node = computed(() => ed.def.value.nodes.find(n => n.id === ed.benchOpen.value && n.kind === 'bench'))
|
|
watch(node, n => !n && close())
|
|
const cols = computed(() => node.value?.bench?.cols ?? [])
|
|
const drives = computed(() => driveCols(node.value?.bench))
|
|
const checks = computed(() => checkCols(node.value?.bench))
|
|
const rows = computed(() => node.value?.bench?.rows ?? [])
|
|
const result = computed(() => (node.value ? ed.benchResults.value.get(node.value.id) : undefined))
|
|
|
|
function onKey(e: KeyboardEvent) {
|
|
if (e.key === 'Escape') close()
|
|
}
|
|
onMounted(() => {
|
|
window.addEventListener('keydown', onKey)
|
|
if (node.value && !result.value) ed.runBenchNow(node.value.id)
|
|
})
|
|
onBeforeUnmount(() => window.removeEventListener('keydown', onKey))
|
|
|
|
const id = () => node.value!.id
|
|
/** Columns change the bench's pins: a structural change. */
|
|
const setCols = (f: (c: BenchCol[]) => void) => ed.setBench(id(), b => f((b.cols ??= [])), true)
|
|
function addCol(dir: BenchCol['dir']) {
|
|
const used = new Set(cols.value.map(c => c.name))
|
|
let k = cols.value.filter(c => c.dir === dir).length
|
|
const base = dir === 'drive' ? 'IN' : 'OUT'
|
|
while (used.has(`${base}${k}`)) k++
|
|
setCols(c => c.push({ id: uid('c'), name: `${base}${k}`, bits: 1, dir }))
|
|
}
|
|
const renameCol = (i: number, name: string) => name.trim() && setCols(c => (c[i]!.name = name.trim().slice(0, 40)))
|
|
function setBits(i: number, v: string) {
|
|
const n = Math.round(Number(v))
|
|
if (n >= 1 && n <= 32) setCols(c => (c[i]!.bits = n))
|
|
}
|
|
const removeCol = (i: number) => setCols(c => c.splice(i, 1))
|
|
function moveCol(i: number, by: number) {
|
|
setCols(c => {
|
|
const j = i + by
|
|
if (j < 0 || j >= c.length) return
|
|
;[c[i], c[j]] = [c[j]!, c[i]!]
|
|
})
|
|
}
|
|
|
|
function setCell(i: number, side: 'set' | 'expect', colId: string, v: string) {
|
|
ed.setBench(id(), b => {
|
|
const row = b.rows[i]
|
|
if (!row) return
|
|
const cells = { ...(row[side] ?? {}) }
|
|
if (v.trim()) cells[colId] = v.trim()
|
|
else delete cells[colId]
|
|
if (Object.keys(cells).length) row[side] = cells
|
|
else delete row[side]
|
|
})
|
|
}
|
|
function setTicks(i: number, v: string) {
|
|
ed.setBench(id(), b => {
|
|
const row = b.rows[i]
|
|
if (!row) return
|
|
if (v.trim()) row.ticks = v.trim()
|
|
else delete row.ticks
|
|
})
|
|
}
|
|
function addRow(at = rows.value.length) {
|
|
// A new row starts as a copy of the one above, without its checks.
|
|
const above = rows.value[at - 1]
|
|
const row: BenchRow = above?.set ? { set: { ...above.set } } : {}
|
|
ed.setBench(id(), b => b.rows.splice(at, 0, row))
|
|
}
|
|
const removeRow = (i: number) => ed.setBench(id(), b => b.rows.splice(i, 1))
|
|
function combinations() {
|
|
const b = node.value?.bench
|
|
if (!b) return
|
|
const r = combinationRows(b)
|
|
if ('error' in r) return ed.notify(r.error, 'error')
|
|
ed.setBench(id(), x => (x.rows = r))
|
|
ed.notify(`${r.length} rows, one for every combination. Fill in what it should read, or take what it does.`)
|
|
}
|
|
function takeExpected() {
|
|
ed.runBenchNow(id(), true)
|
|
const r = result.value
|
|
if (!r) return
|
|
ed.setBench(id(), b => (b.rows = recordExpected(r, b.rows)))
|
|
ed.notify('Empty checks set to what it reads now: the tests keep it that way')
|
|
}
|
|
|
|
/** What a failing check cell read, when it differs. */
|
|
function gotNote(i: number, colId: string): string {
|
|
const r = result.value?.rows[i]
|
|
const want = rows.value[i]?.expect?.[colId] ?? ''
|
|
const got = r?.got[colId]
|
|
if (!r || r.ok !== false || got === undefined || !want) return ''
|
|
return Number(want) === got ? '' : `is ${got}`
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="modal-backdrop" @click.self="close">
|
|
<div class="modal bench" role="dialog" aria-labelledby="bench-title">
|
|
<div class="flex items-start justify-between gap-4">
|
|
<h2 id="bench-title">{{ node?.label?.trim() || 'Test bench' }}</h2>
|
|
<button class="tb-btn" title="Close (Esc)" @click="close"><Icon name="lucide:x" class="size-4" /></button>
|
|
</div>
|
|
<p class="hint">
|
|
Wire its pins to what you test: <strong>drive</strong> columns are its outputs (on the right of its box), <strong>check</strong> columns its inputs (on the left).
|
|
Each row sets the drive columns, gives ticks, then checks. A run takes this circuit on its own from all zeros; what it holds carries to the next row.
|
|
Drive: a number (12, 0x0C, 0b1100), <code>P</code> for a clock pulse, empty to leave it. Check: what it should read, empty for any.
|
|
Ticks: <code>5</code> pulses (P columns and the circuit's clocks), or <code>≤1000</code> to wait until the checks hold. Click a row on the box to put it on the running circuit.
|
|
</p>
|
|
|
|
<h3>Columns</h3>
|
|
<div class="bench-cols">
|
|
<div v-for="(c, i) in cols" :key="c.id" class="bench-col" :class="c.dir">
|
|
<span class="bench-col-dir">{{ c.dir === 'drive' ? 'Drive' : 'Check' }}</span>
|
|
<input class="field-input" :value="c.name" spellcheck="false" @change="renameCol(i, ($event.target as HTMLInputElement).value)" />
|
|
<label title="Bits: one pin each">
|
|
<input class="field-input bench-bits" type="number" min="1" max="32" :value="c.bits" @change="setBits(i, ($event.target as HTMLInputElement).value)" />
|
|
bits
|
|
</label>
|
|
<button class="icon-btn" title="Further left" :disabled="i === 0" @click="moveCol(i, -1)"><Icon name="lucide:chevron-up" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Further right" :disabled="i === cols.length - 1" @click="moveCol(i, 1)"><Icon name="lucide:chevron-down" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Remove the column (and its pins)" @click="removeCol(i)"><Icon name="lucide:trash-2" class="size-3.5" /></button>
|
|
</div>
|
|
<div class="bench-bar">
|
|
<button class="btn-sm" @click="addCol('drive')"><Icon name="lucide:plus" class="size-3.5" /> Drive column</button>
|
|
<button class="btn-sm" @click="addCol('check')"><Icon name="lucide:plus" class="size-3.5" /> Check column</button>
|
|
</div>
|
|
</div>
|
|
|
|
<h3>Rows</h3>
|
|
<div class="bench-bar">
|
|
<p v-if="result" class="bench-summary" :class="result.fail || result.error ? 'fail' : result.pass ? 'pass' : ''">
|
|
<template v-if="result.error">{{ result.error }}. </template>
|
|
<template v-if="result.fail">{{ result.fail }} failing, {{ result.pass }} pass</template>
|
|
<template v-else-if="result.pass">All {{ result.pass }} pass</template>
|
|
</p>
|
|
<span class="grow" />
|
|
<button class="btn-sm" :disabled="!drives.length" title="A row for every combination of the drive columns" @click="combinations"><Icon name="lucide:grid-3x3" class="size-3.5" /> Every combination</button>
|
|
<button class="btn-sm" :disabled="!rows.length || !checks.length" title="Empty checks: what it reads now" @click="takeExpected"><Icon name="lucide:camera" class="size-3.5" /> Take what it does</button>
|
|
<button class="btn-sm" :disabled="!rows.length" title="Run again, longer runs allowed" @click="node && ed.runBenchNow(node.id, true)"><Icon name="lucide:play" class="size-3.5" /> Run</button>
|
|
</div>
|
|
<div v-if="cols.length" class="bench-table-wrap">
|
|
<table class="bench-table">
|
|
<thead>
|
|
<tr>
|
|
<th class="bench-num">#</th>
|
|
<th v-for="c in drives" :key="c.id" :title="`${c.bits} bit${c.bits === 1 ? '' : 's'}`">{{ c.name }}</th>
|
|
<th class="bench-arrow">→</th>
|
|
<th v-for="c in checks" :key="c.id" class="out" :title="`${c.bits} bit${c.bits === 1 ? '' : 's'}`">{{ c.name }}</th>
|
|
<th>Ticks</th>
|
|
<th />
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="(row, i) in rows" :key="i" :class="result?.rows[i]?.ok === false ? 'fail' : result?.rows[i]?.ok ? 'pass' : ''">
|
|
<td class="bench-num tabular-nums">{{ i + 1 }}</td>
|
|
<td v-for="c in drives" :key="c.id">
|
|
<input :value="row.set?.[c.id] ?? ''" class="bench-cell" spellcheck="false" @change="setCell(i, 'set', c.id, ($event.target as HTMLInputElement).value)" />
|
|
</td>
|
|
<td class="bench-arrow" :title="result?.rows[i]?.error ?? ''">
|
|
<Icon v-if="result?.rows[i]?.ok === false" name="lucide:x" class="size-3.5 text-(--danger)" />
|
|
<Icon v-else-if="result?.rows[i]?.ok" name="lucide:check" class="size-3.5 text-(--on)" />
|
|
</td>
|
|
<td v-for="c in checks" :key="c.id" class="out">
|
|
<input :value="row.expect?.[c.id] ?? ''" class="bench-cell" spellcheck="false" :placeholder="String(result?.rows[i]?.got[c.id] ?? '')" @change="setCell(i, 'expect', c.id, ($event.target as HTMLInputElement).value)" />
|
|
<span v-if="gotNote(i, c.id)" class="bench-got">{{ gotNote(i, c.id) }}</span>
|
|
</td>
|
|
<td>
|
|
<input :value="row.ticks ?? ''" class="bench-cell" spellcheck="false" :placeholder="result?.rows[i]?.ticks ? String(result.rows[i]!.ticks) : ''" @change="setTicks(i, ($event.target as HTMLInputElement).value)" />
|
|
</td>
|
|
<td class="bench-actions">
|
|
<span v-if="result?.rows[i]?.error" class="bench-err" :title="result.rows[i]!.error">{{ result.rows[i]!.error }}</span>
|
|
<button class="icon-btn" title="New row below" @click="addRow(i + 1)"><Icon name="lucide:plus" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Remove this row" @click="removeRow(i)"><Icon name="lucide:trash-2" class="size-3.5" /></button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<p v-else class="hint">Add columns first, or drop the bench on a component to get one for each of its pins.</p>
|
|
<button v-if="cols.length" class="btn-sm bench-add" @click="addRow()"><Icon name="lucide:plus" class="size-3.5" /> Row</button>
|
|
</div>
|
|
</div>
|
|
</template>
|