logigate/app/components/editor/AsmDialog.vue

129 lines
6.8 KiB
Vue
Raw Normal View History

<script setup lang="ts">
// The assembler: a program in your own instruction set becomes words, which
// copy out as hex or binary or go straight into the switch panels of the
// memory you built.
import { assemble, binWord, hexWord, MAX_WIDTH, memoryPanels } from '~/lib/asm'
const ed = useEditor()
const close = () => (ed.dialog.value = null)
const doc = ed.asmDoc
const result = computed(() => assemble(doc.value.source, doc.value.table, doc.value.width))
const sourceErrors = computed(() => result.value.errors.filter(e => e.where === 'source'))
const tableErrors = computed(() => result.value.errors.filter(e => e.where === 'table'))
const showTable = ref(!ed.project.value.asm)
/** Circuits with panels of switches to hold words. */
const targets = computed(() =>
ed.project.value.components
.map(d => {
const panels = memoryPanels(d)
const rows = Math.min(...panels.map(p => p.rows!.length))
return { d, panels, rows, outside: d.id === ed.def.value.id ? 0 : panels.filter(p => !p.inner).length }
})
.filter(t => t.panels.length),
)
const target = computed(() => targets.value.find(t => t.d.id === doc.value.target) ?? targets.value.find(t => t.d.id === ed.def.value.id) ?? targets.value[0])
function setWidth(e: Event) {
const v = Math.round(Number((e.target as HTMLInputElement).value))
if (v >= 1 && v <= MAX_WIDTH) ed.saveAsm({ width: v })
}
const copyAll = (fmt: 'hex' | 'bin') => ed.copyText(result.value.image.map(v => (fmt === 'hex' ? hexWord(v, doc.value.width) : binWord(v, doc.value.width))).join(' '), `${result.value.image.length} words copied`)
function load() {
const t = target.value
if (t) ed.loadProgram(t.d.id, result.value.image, doc.value.width, !!doc.value.msbTop)
}
</script>
<template>
<div class="modal-backdrop" @click.self="close">
<div class="modal asm" role="dialog" aria-labelledby="asm-title">
<div class="flex items-start justify-between gap-4">
<h2 id="asm-title">Assembler</h2>
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
</div>
<p class="hint">
Your own instruction set turns a program into words for the memory you built. Labels end with <code>:</code>, comments start with <code>;</code>.
<code>.org 16</code> moves on to an address, <code>.word 1, 'A', "Hi"</code> puts words as they are, <code>.fill 4, 0</code> repeats one, <code>N = 5</code> names a value.
</p>
<div class="asm-grid">
<div class="asm-col">
<div class="asm-bar">
<span class="font-semibold">Program</span>
<span class="grow" />
<label class="asm-width" title="Bits per memory word">
Word
<input type="number" min="1" :max="MAX_WIDTH" :value="doc.width" class="field-input" @change="setWidth" />
bits
</label>
</div>
<textarea class="field-input asm-text" spellcheck="false" rows="14" :value="doc.source" @input="ed.saveAsm({ source: ($event.target as HTMLTextAreaElement).value })" />
<ul v-if="sourceErrors.length" class="asm-errors">
<li v-for="(e, i) in sourceErrors.slice(0, 20)" :key="i"><span class="tabular-nums">Line {{ e.line }}</span> {{ e.text }}</li>
</ul>
<button class="asm-fold" @click="showTable = !showTable">
<Icon :name="showTable ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-3.5" />
Instruction set
<span class="hint">{{ tableErrors.length ? `${tableErrors.length} to fix` : 'name, opcode bits, operands as name:bits' }}</span>
</button>
<template v-if="showTable">
<textarea class="field-input asm-text" spellcheck="false" rows="10" :value="doc.table" @input="ed.saveAsm({ table: ($event.target as HTMLTextAreaElement).value })" />
<ul v-if="tableErrors.length" class="asm-errors">
<li v-for="(e, i) in tableErrors.slice(0, 20)" :key="i"><span class="tabular-nums">Line {{ e.line }}</span> {{ e.text }}</li>
</ul>
</template>
</div>
<div class="asm-col">
<div class="asm-bar">
<span class="font-semibold">Words</span>
<span class="hint">{{ result.image.length }} words</span>
<span class="grow" />
<button class="btn-sm" :disabled="!result.image.length" @click="copyAll('hex')"><Icon name="lucide:copy" class="size-3.5" /> Hex</button>
<button class="btn-sm" :disabled="!result.image.length" @click="copyAll('bin')"><Icon name="lucide:copy" class="size-3.5" /> Binary</button>
</div>
<div class="asm-listing">
<table class="convert-table tabular-nums">
<thead>
<tr><th>Address</th><th>Hex</th><th>Binary</th><th>Line</th></tr>
</thead>
<tbody>
<template v-for="l in result.lines.slice(0, 512)" :key="l.line">
<tr v-for="(w, k) in l.words.slice(0, 64)" :key="k">
<td>{{ hexWord(l.addr + k, 8) }}</td>
<td>{{ hexWord(w, doc.width) }}</td>
<td>{{ binWord(w, doc.width) }}</td>
<td class="asm-src">{{ k ? '' : l.text }}</td>
</tr>
</template>
</tbody>
</table>
</div>
<div class="asm-load">
<span class="font-semibold">Into memory</span>
<p v-if="!targets.length" class="hint">
Build memory from input panels of switches, one panel per word (in a component, set them to stay inside). Words then go in here, in reading order.
</p>
<template v-else>
<select class="tb-select" :value="target?.d.id" @change="ed.saveAsm({ target: ($event.target as HTMLSelectElement).value })">
<option v-for="t in targets" :key="t.d.id" :value="t.d.id">{{ t.d.name }} · {{ t.panels.length }} panels of {{ t.rows }}</option>
</select>
<div class="seg" title="Which switch of a panel is bit 0">
<button :class="{ active: !doc.msbTop }" @click="ed.saveAsm({ msbTop: false })">Top row is bit 0</button>
<button :class="{ active: doc.msbTop }" @click="ed.saveAsm({ msbTop: true })">Top row is the highest bit</button>
</div>
<p v-if="target?.outside" class="hint">{{ target.outside }} of its panels are inputs when it is placed: set them to stay inside to make them its memory.</p>
<button class="btn btn-primary" :disabled="!result.image.length || result.errors.length > 0" @click="load">
<Icon name="lucide:download" class="size-3.5" /> Load {{ result.image.length }} words into {{ target?.d.name }}
</button>
</template>
</div>
</div>
</div>
</div>
</div>
</template>