146 lines
6.6 KiB
Vue
146 lines
6.6 KiB
Vue
<script setup lang="ts">
|
||
// The logic analyzer: the watched signals of the open circuit over time, a
|
||
// lane each. A wire is a square wave, a cable a bus with its value. One
|
||
// step per clock half-cycle while the clock runs, and one for every other
|
||
// change. Point at the waves to read every value at that moment; the wheel
|
||
// scrolls back through time.
|
||
import { probeBits, probeName } from '~/lib/logic/probes'
|
||
|
||
const ed = useEditor()
|
||
const def = ed.def
|
||
const probes = computed(() => def.value.probes ?? [])
|
||
|
||
const CELL = 10 // pixels per sample
|
||
const NAME_W = 150
|
||
const LANE_H = 26
|
||
const wave = ref<HTMLElement>()
|
||
const width = ref(600)
|
||
let ro: ResizeObserver | undefined
|
||
onMounted(() => {
|
||
ro = new ResizeObserver(() => (width.value = wave.value?.clientWidth ?? 600))
|
||
if (wave.value) ro.observe(wave.value)
|
||
})
|
||
onBeforeUnmount(() => ro?.disconnect())
|
||
|
||
// The trace grows in place: a new object each time, so what reads it follows.
|
||
const samples = computed(() => ({ v: ed.traceVersion.value, list: ed.trace.value }))
|
||
const total = computed(() => samples.value.list.length)
|
||
/** How far back from the newest sample the view is (0: following the newest). */
|
||
const back = ref(0)
|
||
const count = computed(() => Math.max(1, Math.floor(width.value / CELL)))
|
||
const view = computed(() => {
|
||
const all = samples.value.list
|
||
const end = Math.max(0, all.length - back.value)
|
||
const start = Math.max(0, end - count.value)
|
||
return { start, list: all.slice(start, end) }
|
||
})
|
||
function onWheel(e: WheelEvent) {
|
||
const step = Math.round((Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY) / 20) || Math.sign(e.deltaY)
|
||
back.value = Math.max(0, Math.min(Math.max(0, total.value - count.value), back.value + step))
|
||
}
|
||
|
||
/** The sample pointed at (in the view), else the newest. */
|
||
const at = ref<number | null>(null)
|
||
function onMove(e: PointerEvent) {
|
||
const r = wave.value!.getBoundingClientRect()
|
||
const i = Math.floor((e.clientX - r.left) / CELL)
|
||
at.value = i >= 0 && i < view.value.list.length ? i : null
|
||
}
|
||
const shown = computed(() => view.value.list[at.value ?? view.value.list.length - 1])
|
||
|
||
const bits = (i: number) => probeBits(def.value, probes.value[i]!)
|
||
const hexOf = (v: number, b: number) => (b === 1 ? String(v) : `0x${v.toString(16).toUpperCase().padStart(Math.ceil(b / 4), '0')}`)
|
||
const valueText = (i: number) => {
|
||
const s = shown.value
|
||
if (!s) return '–'
|
||
const v = s.vals[i] ?? 0
|
||
const b = bits(i)
|
||
return b === 1 ? String(v) : `${v} · ${hexOf(v, b)}`
|
||
}
|
||
|
||
/** A wire's square wave. */
|
||
function square(i: number): string {
|
||
const top = 5
|
||
const low = LANE_H - 5
|
||
let d = ''
|
||
view.value.list.forEach((s, k) => {
|
||
const y = s.vals[i] ? top : low
|
||
d += k ? `L${k * CELL} ${y}H${(k + 1) * CELL}` : `M0 ${y}H${CELL}`
|
||
})
|
||
return d
|
||
}
|
||
|
||
/** A cable's runs of one value: a long hexagon each, named by its value when there is room. */
|
||
function runs(i: number): { x: number; w: number; v: number }[] {
|
||
const out: { x: number; w: number; v: number }[] = []
|
||
view.value.list.forEach((s, k) => {
|
||
const v = s.vals[i] ?? 0
|
||
const last = out[out.length - 1]
|
||
if (last && last.v === v) last.w += CELL
|
||
else out.push({ x: k * CELL, w: CELL, v })
|
||
})
|
||
return out
|
||
}
|
||
const hexagon = (r: { x: number; w: number }) => {
|
||
const m = Math.min(3, r.w / 3)
|
||
const t = 5
|
||
const b = LANE_H - 5
|
||
const c = LANE_H / 2
|
||
return `M${r.x} ${c}L${r.x + m} ${t}H${r.x + r.w - m}L${r.x + r.w} ${c}L${r.x + r.w - m} ${b}H${r.x + m}Z`
|
||
}
|
||
|
||
/** Clock steps: where a new clock cycle began, numbered now and then. */
|
||
const marks = computed(() => {
|
||
const out: { x: number; n?: number }[] = []
|
||
view.value.list.forEach((s, k) => {
|
||
const prev = view.value.list[k - 1]
|
||
if (prev && s.cycle !== prev.cycle) out.push({ x: k * CELL, ...(s.cycle % 5 === 0 ? { n: s.cycle } : {}) })
|
||
})
|
||
return out
|
||
})
|
||
|
||
const name = (i: number) => probeName(def.value, probes.value[i]!, ed.getDef.value)
|
||
</script>
|
||
|
||
<template>
|
||
<section class="analyzer" aria-label="Logic analyzer">
|
||
<header class="analyzer-head">
|
||
<Icon name="lucide:activity" class="size-4 text-(--accent)" />
|
||
<span class="font-semibold">Logic analyzer</span>
|
||
<span class="hint">{{ total }} {{ total === 1 ? 'step' : 'steps' }}<template v-if="back"> · {{ back }} back</template></span>
|
||
<button v-if="back" class="btn-sm" title="Follow the newest again" @click="back = 0"><Icon name="lucide:chevrons-right" class="size-3.5" /> Now</button>
|
||
<span class="grow" />
|
||
<button class="icon-btn" title="Start over" :disabled="!total" @click="ed.clearTrace()"><Icon name="lucide:eraser" class="size-4" /></button>
|
||
<button class="icon-btn" title="Close (the signals stay watched)" @click="ed.analyzerOpen.value = false"><Icon name="lucide:x" class="size-4" /></button>
|
||
</header>
|
||
<p v-if="!probes.length" class="hint analyzer-empty">
|
||
Right-click a wire, a cable or a pin and choose <strong>Watch in analyzer</strong>. Then run the clock, step it, or flip switches.
|
||
</p>
|
||
<div v-else class="analyzer-body">
|
||
<ul class="analyzer-names" :style="{ width: `${NAME_W}px` }">
|
||
<li v-for="(p, i) in probes" :key="`${p.node}:${p.pin}`" :style="{ height: `${LANE_H}px` }">
|
||
<span class="truncate" :title="name(i)">{{ name(i) }}</span>
|
||
<span class="analyzer-val tabular-nums">{{ valueText(i) }}</span>
|
||
<button class="icon-btn" title="Stop watching" @click="ed.unwatch(i)"><Icon name="lucide:x" class="size-3" /></button>
|
||
</li>
|
||
</ul>
|
||
<div ref="wave" class="analyzer-wave" @wheel.prevent="onWheel" @pointermove="onMove" @pointerleave="at = null">
|
||
<svg :width="view.list.length * CELL" :height="probes.length * LANE_H">
|
||
<line v-for="(m, k) in marks" :key="`m${k}`" :x1="m.x" :x2="m.x" y1="0" :y2="probes.length * LANE_H" class="analyzer-mark" />
|
||
<g v-for="(p, i) in probes" :key="`${p.node}:${p.pin}`" :transform="`translate(0 ${i * LANE_H})`">
|
||
<path v-if="bits(i) === 1" :d="square(i)" class="analyzer-bit" />
|
||
<template v-else>
|
||
<template v-for="(r, k) in runs(i)" :key="k">
|
||
<path :d="hexagon(r)" class="analyzer-bus" />
|
||
<text v-if="r.w >= 26" :x="r.x + r.w / 2" :y="LANE_H / 2 + 3.5" text-anchor="middle" class="analyzer-bus-text">{{ hexOf(r.v, bits(i)) }}</text>
|
||
</template>
|
||
</template>
|
||
</g>
|
||
<line v-if="at !== null" :x1="at * CELL + CELL / 2" :x2="at * CELL + CELL / 2" y1="0" :y2="probes.length * LANE_H" class="analyzer-cursor" />
|
||
</svg>
|
||
<span v-if="shown" class="analyzer-cycle tabular-nums">tick {{ shown.cycle }}</span>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</template>
|