248 lines
11 KiB
Vue
248 lines
11 KiB
Vue
|
|
<script setup lang="ts">
|
||
|
|
import type { TestResult, TruthTable } from '~/lib/logic/analyze'
|
||
|
|
import { stats } from '~/lib/logic/analyze'
|
||
|
|
import { CHALLENGES } from '~/lib/logic/challenges'
|
||
|
|
import { compile } from '~/lib/logic/compile'
|
||
|
|
import { chipPins } from '~/lib/logic/geometry'
|
||
|
|
import { usages } from '~/lib/logic/ops'
|
||
|
|
import type { SymbolKind } from '~/lib/logic/types'
|
||
|
|
|
||
|
|
const ed = useEditor()
|
||
|
|
const def = ed.def
|
||
|
|
|
||
|
|
const KIND_NAMES: Record<string, string> = {
|
||
|
|
nand: 'NAND gate',
|
||
|
|
input: 'Input',
|
||
|
|
output: 'Output',
|
||
|
|
lamp: 'Lamp',
|
||
|
|
button: 'Push button',
|
||
|
|
clock: 'Clock',
|
||
|
|
high: 'Constant 1',
|
||
|
|
low: 'Ground (0)',
|
||
|
|
hex: 'Hex display',
|
||
|
|
note: 'Note',
|
||
|
|
}
|
||
|
|
|
||
|
|
const SYMBOLS: { value: SymbolKind; label: string }[] = [
|
||
|
|
{ value: 'box', label: 'Box with name' },
|
||
|
|
{ value: 'and', label: 'AND gate' },
|
||
|
|
{ value: 'or', label: 'OR gate' },
|
||
|
|
{ value: 'xor', label: 'XOR gate' },
|
||
|
|
{ value: 'not', label: 'NOT gate' },
|
||
|
|
{ value: 'buf', label: 'Buffer' },
|
||
|
|
{ value: 'nand', label: 'NAND gate' },
|
||
|
|
{ value: 'nor', label: 'NOR gate' },
|
||
|
|
{ value: 'xnor', label: 'XNOR gate' },
|
||
|
|
]
|
||
|
|
|
||
|
|
const single = computed(() => (ed.selection.value.length === 1 ? def.value.nodes.find(n => n.id === ed.selection.value[0]) : undefined))
|
||
|
|
const wire = computed(() => def.value.wires.find(w => w.id === ed.selectedWire.value))
|
||
|
|
const chipDef = computed(() => (single.value?.chip ? ed.getDef.value(single.value.chip) : undefined))
|
||
|
|
const chipStats = computed(() => (chipDef.value ? stats(compile(ed.getDef.value, chipDef.value.id)) : null))
|
||
|
|
const hasLabel = computed(() => !!single.value && ['input', 'output', 'lamp', 'button', 'note'].includes(single.value.kind))
|
||
|
|
|
||
|
|
const labelInput = ref<HTMLInputElement | HTMLTextAreaElement>()
|
||
|
|
const nameInput = ref<HTMLInputElement>()
|
||
|
|
watch(
|
||
|
|
() => ed.labelFocus.value,
|
||
|
|
() =>
|
||
|
|
nextTick(() => {
|
||
|
|
const el = labelInput.value ?? nameInput.value
|
||
|
|
el?.focus()
|
||
|
|
el?.select()
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
|
||
|
|
const pins = computed(() => chipPins(def.value))
|
||
|
|
const users = computed(() => usages(ed.project.value, def.value.id))
|
||
|
|
const groupNames = computed(() => [...new Set(ed.project.value.components.map(d => d.group).filter(Boolean))])
|
||
|
|
const challenge = computed(() => CHALLENGES.find(c => c.id === def.value.challenge))
|
||
|
|
const solved = computed(() => !!challenge.value && ed.project.value.solved.includes(challenge.value.id))
|
||
|
|
|
||
|
|
const packName = ref('')
|
||
|
|
function pack() {
|
||
|
|
ed.packageSelected(packName.value || 'Part')
|
||
|
|
packName.value = ''
|
||
|
|
}
|
||
|
|
|
||
|
|
const showTable = ref(false)
|
||
|
|
const table = ref<TruthTable | { error: string } | null>(null)
|
||
|
|
function refreshTable() {
|
||
|
|
table.value = showTable.value ? ed.table() : null
|
||
|
|
}
|
||
|
|
watch([showTable, () => ed.sim.value, () => def.value.id], refreshTable)
|
||
|
|
|
||
|
|
const check = ref<TestResult | null>(null)
|
||
|
|
watch(() => def.value.id, () => (check.value = null))
|
||
|
|
watch(() => ed.sim.value, () => (check.value = null))
|
||
|
|
function runCheck() {
|
||
|
|
check.value = ed.checkChallenge()
|
||
|
|
}
|
||
|
|
|
||
|
|
const resetRoute = () => {
|
||
|
|
if (!wire.value) return
|
||
|
|
delete wire.value.pts
|
||
|
|
ed.commit(false)
|
||
|
|
}
|
||
|
|
|
||
|
|
const confirmDelete = ref(false)
|
||
|
|
watch(() => def.value.id, () => (confirmDelete.value = false))
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<template>
|
||
|
|
<aside class="inspector">
|
||
|
|
<div class="scroll">
|
||
|
|
<!-- Selection -->
|
||
|
|
<section v-if="single">
|
||
|
|
<h3>{{ single.kind === 'chip' ? chipDef?.name ?? 'Missing component' : KIND_NAMES[single.kind] }}</h3>
|
||
|
|
<label v-if="hasLabel" class="field">
|
||
|
|
<span>{{ single.kind === 'note' ? 'Text' : 'Label' }}</span>
|
||
|
|
<textarea
|
||
|
|
v-if="single.kind === 'note'"
|
||
|
|
ref="labelInput"
|
||
|
|
v-model="single.label"
|
||
|
|
rows="3"
|
||
|
|
@change="ed.commit(false)"
|
||
|
|
/>
|
||
|
|
<input v-else ref="labelInput" v-model="single.label" @change="ed.commit(false)" @keydown.enter="($event.target as HTMLInputElement).blur()" />
|
||
|
|
</label>
|
||
|
|
<p v-if="single.kind === 'input'" class="hint">
|
||
|
|
Click the switch on the canvas to flip it. When this circuit is used as a component, this becomes one of its input pins (pins are ordered top to bottom).
|
||
|
|
</p>
|
||
|
|
<p v-else-if="single.kind === 'output'" class="hint">
|
||
|
|
When this circuit is used as a component, this becomes one of its output pins.
|
||
|
|
</p>
|
||
|
|
<p v-else-if="single.kind === 'nand'" class="hint">
|
||
|
|
Output is 0 only when both inputs are 1. Every other part can be built from this one.
|
||
|
|
</p>
|
||
|
|
<template v-else-if="single.kind === 'chip' && chipDef">
|
||
|
|
<p v-if="chipDef.description" class="hint">{{ chipDef.description }}</p>
|
||
|
|
<dl v-if="chipStats" class="stats">
|
||
|
|
<dt>NAND gates</dt>
|
||
|
|
<dd>{{ chipStats.nands.toLocaleString() }}</dd>
|
||
|
|
<dt>{{ chipStats.sequential ? 'Memory' : 'Delay' }}</dt>
|
||
|
|
<dd>{{ chipStats.sequential ? 'yes (feedback)' : `${chipStats.delay} gates` }}</dd>
|
||
|
|
</dl>
|
||
|
|
<button class="btn w-full" @click="ed.open(chipDef.id)"><Icon name="lucide:log-in" class="size-4" /> Open component</button>
|
||
|
|
</template>
|
||
|
|
<button class="btn btn-ghost mt-2 w-full" @click="ed.deleteSelected()"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
|
||
|
|
</section>
|
||
|
|
|
||
|
|
<section v-else-if="ed.selection.value.length > 1">
|
||
|
|
<h3>{{ ed.selection.value.length }} parts selected</h3>
|
||
|
|
<p class="hint">Turn them into a reusable component. Wires that cross the selection border become its pins.</p>
|
||
|
|
<label class="field">
|
||
|
|
<span>Component name</span>
|
||
|
|
<input v-model="packName" placeholder="e.g. Half Adder" @keydown.enter="pack" />
|
||
|
|
</label>
|
||
|
|
<button class="btn btn-primary w-full" @click="pack"><Icon name="lucide:package" class="size-4" /> Make component</button>
|
||
|
|
<button class="btn btn-ghost mt-2 w-full" @click="ed.deleteSelected()"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
|
||
|
|
</section>
|
||
|
|
|
||
|
|
<section v-else-if="wire">
|
||
|
|
<h3>Wire</h3>
|
||
|
|
<p class="hint">Drag a middle segment to reroute it.</p>
|
||
|
|
<button v-if="wire.pts" class="btn w-full" @click="resetRoute"><Icon name="lucide:undo-2" class="size-4" /> Automatic route</button>
|
||
|
|
<button class="btn btn-ghost mt-2 w-full" @click="ed.deleteSelected()"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
|
||
|
|
</section>
|
||
|
|
|
||
|
|
<!-- Challenge -->
|
||
|
|
<section v-if="challenge" class="challenge" :class="{ solved }">
|
||
|
|
<h3>
|
||
|
|
<Icon :name="solved ? 'lucide:badge-check' : 'lucide:flag'" class="size-4" />
|
||
|
|
Challenge: {{ challenge.name }}
|
||
|
|
</h3>
|
||
|
|
<p>{{ challenge.goal }}</p>
|
||
|
|
<p class="pins">Pins: {{ challenge.inputs.join(', ') }} → {{ challenge.outputs.join(', ') }}</p>
|
||
|
|
<button class="btn btn-primary w-full" @click="runCheck"><Icon name="lucide:play" class="size-4" /> Check</button>
|
||
|
|
<p v-if="check" class="result" :class="check.pass ? 'pass' : 'fail'">
|
||
|
|
<Icon :name="check.pass ? 'lucide:circle-check' : 'lucide:circle-x'" class="size-4 shrink-0" />
|
||
|
|
{{ check.message }}
|
||
|
|
</p>
|
||
|
|
</section>
|
||
|
|
|
||
|
|
<!-- Circuit -->
|
||
|
|
<section>
|
||
|
|
<h3>This circuit</h3>
|
||
|
|
<label class="field">
|
||
|
|
<span>Name</span>
|
||
|
|
<input ref="nameInput" v-model="def.name" @change="ed.commit(false)" @keydown.enter="($event.target as HTMLInputElement).blur()" />
|
||
|
|
</label>
|
||
|
|
<label class="field">
|
||
|
|
<span>Description</span>
|
||
|
|
<textarea v-model="def.description" rows="2" placeholder="What does it do?" @change="ed.commit(false)" />
|
||
|
|
</label>
|
||
|
|
<div class="grid grid-cols-2 gap-2">
|
||
|
|
<label class="field">
|
||
|
|
<span>Drawn as</span>
|
||
|
|
<select v-model="def.symbol" @change="ed.commit(false)">
|
||
|
|
<option v-for="s in SYMBOLS" :key="s.value" :value="s.value">{{ s.label }}</option>
|
||
|
|
</select>
|
||
|
|
</label>
|
||
|
|
<label class="field">
|
||
|
|
<span>Group</span>
|
||
|
|
<input v-model="def.group" list="groups" placeholder="My components" @change="ed.commit(false)" />
|
||
|
|
<datalist id="groups">
|
||
|
|
<option v-for="g in groupNames" :key="g" :value="g" />
|
||
|
|
</datalist>
|
||
|
|
</label>
|
||
|
|
</div>
|
||
|
|
<dl class="stats">
|
||
|
|
<dt>Pins</dt>
|
||
|
|
<dd>{{ pins.inputs.length }} in, {{ pins.outputs.length }} out</dd>
|
||
|
|
<dt>NAND gates</dt>
|
||
|
|
<dd>{{ ed.circuitStats.value.nands.toLocaleString() }}</dd>
|
||
|
|
<dt>{{ ed.circuitStats.value.sequential ? 'Memory' : 'Delay' }}</dt>
|
||
|
|
<dd>{{ ed.circuitStats.value.sequential ? 'yes (feedback loops)' : `${ed.circuitStats.value.delay} gates` }}</dd>
|
||
|
|
</dl>
|
||
|
|
<p v-for="err in ed.errors.value" :key="err" class="result fail">
|
||
|
|
<Icon name="lucide:triangle-alert" class="size-4 shrink-0" /> {{ err }}
|
||
|
|
</p>
|
||
|
|
<p v-if="users.length" class="hint">
|
||
|
|
Used in
|
||
|
|
<template v-for="(u, i) in users" :key="u.id">
|
||
|
|
<a href="#" @click.prevent="ed.open(u.id)">{{ u.name }}</a><template v-if="i < users.length - 1">, </template>
|
||
|
|
</template>
|
||
|
|
</p>
|
||
|
|
</section>
|
||
|
|
|
||
|
|
<section>
|
||
|
|
<button class="section-toggle" @click="showTable = !showTable">
|
||
|
|
<Icon :name="showTable ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
|
||
|
|
Truth table
|
||
|
|
</button>
|
||
|
|
<template v-if="showTable && table">
|
||
|
|
<p v-if="'error' in table" class="hint">{{ table.error }}</p>
|
||
|
|
<template v-else>
|
||
|
|
<p v-if="ed.circuitStats.value.sequential" class="hint">This circuit stores state, so the table depends on the order the rows are tried in.</p>
|
||
|
|
<div class="truth">
|
||
|
|
<table>
|
||
|
|
<thead>
|
||
|
|
<tr>
|
||
|
|
<th v-for="l in table.inputs" :key="`i${l}`">{{ l }}</th>
|
||
|
|
<th class="sep" />
|
||
|
|
<th v-for="l in table.outputs" :key="`o${l}`" class="out">{{ l }}</th>
|
||
|
|
</tr>
|
||
|
|
</thead>
|
||
|
|
<tbody>
|
||
|
|
<tr v-for="(r, i) in table.rows" :key="i" :class="{ unstable: !r.settled }">
|
||
|
|
<td v-for="(v, k) in r.ins" :key="`i${k}`" :class="{ one: v }">{{ v }}</td>
|
||
|
|
<td class="sep" />
|
||
|
|
<td v-for="(v, k) in r.outs" :key="`o${k}`" class="out" :class="{ one: v }">{{ r.settled ? v : '~' }}</td>
|
||
|
|
</tr>
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
</div>
|
||
|
|
</template>
|
||
|
|
</template>
|
||
|
|
</section>
|
||
|
|
|
||
|
|
<section class="actions">
|
||
|
|
<button class="btn btn-ghost" @click="ed.copyComponent(def.id)"><Icon name="lucide:copy" class="size-4" /> Duplicate</button>
|
||
|
|
<button v-if="!confirmDelete" class="btn btn-ghost" @click="confirmDelete = true"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
|
||
|
|
<button v-else class="btn btn-danger" @click="ed.removeComponent(def.id); confirmDelete = false">Really delete?</button>
|
||
|
|
</section>
|
||
|
|
</div>
|
||
|
|
</aside>
|
||
|
|
</template>
|