logigate/tests/asm.test.ts

104 lines
4.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { assemble, EXAMPLE, loadIntoPanels, memoryPanels, parseTable } from '../app/lib/asm'
const TABLE = `
LDA 0001 addr:4
ADD 0010 addr:4
LDI 0101 value:4
JMP 0110 addr:4
OUT 1110
HLT 1111
LONG 00000001 value:8 ; two words at 8 bits
`
const asm = (src: string, width = 8, table = TABLE) => assemble(src, table, width)
describe('assembler', () => {
it('the example assembles without errors', () => {
const r = assemble(EXAMPLE.source, EXAMPLE.table, EXAMPLE.width)
expect(r.errors).toEqual([])
// LDI 0, ADD three(6), OUT, JC done(5), JMP loop(1), HLT, 3
expect(r.image).toEqual([0x50, 0x26, 0xe0, 0x75, 0x61, 0xf0, 3])
})
it('opcode then operands from the top bit; bits left over are 0', () => {
const r = asm('LDA 3\nOUT\nHLT')
expect(r.errors).toEqual([])
expect(r.image).toEqual([0x13, 0xe0, 0xf0])
})
it('labels before they are defined, constants, .org, .word with strings and characters, .fill', () => {
const r = asm(`
N = 2
.org 1
start: LDA data+N ; forward label plus a constant
JMP start
.org 0x8
data: .word 1, 'A', "Hi", -1
.fill 2, $FF`)
expect(r.errors).toEqual([])
expect(r.labels.get('start')).toBe(1)
expect(r.labels.get('data')).toBe(8)
expect(r.image).toEqual([0, 0x1a, 0x61, 0, 0, 0, 0, 0, 1, 65, 72, 105, 0xff, 0xff, 0xff])
})
it('an instruction longer than a word takes several words', () => {
const r = asm('LONG 200\nHLT')
expect(r.errors).toEqual([])
expect(r.image).toEqual([1, 200, 0xf0])
})
it('wider words: 12 bits', () => {
const r = asm('LDA 15', 12)
expect(r.image).toEqual([0x1f0])
})
it('says what is wrong and on which line', () => {
const r = asm('LDA 16\nFOO 1\nADD\nLDA nowhere\nx: OUT\nx: HLT\n.org 0\nOUT')
const at = (line: number) => r.errors.filter(e => e.where === 'source' && e.line === line).map(e => e.text)
expect(at(1)[0]).toMatch(/does not fit in 4 bits/)
expect(at(2)[0]).toMatch(/not in the instruction set/)
expect(at(3)[0]).toMatch(/takes addr/)
expect(at(4)[0]).toMatch(/not a label/)
expect(at(6)[0]).toMatch(/defined twice/)
expect(at(8)[0]).toMatch(/also written by line 1/)
})
it('a broken table says so', () => {
const t = parseTable('LDA 0102 addr:4\nADD 0010 addr\nADD 0011\nADD 0100')
expect(t.errors.map(e => e.line)).toEqual([1, 2, 4])
expect(parseTable('X 0xA').ops.get('X')!.bits).toBe('1010')
})
it('comments do not cut quoted text', () => {
const r = asm('.word ";", \';\' ; comment')
expect(r.errors).toEqual([])
expect(r.image).toEqual([59, 59])
})
})
describe('loading words into panels of switches', () => {
const panel = (id: string, x: number, y: number, rows = 4) => ({ id, kind: 'panel' as const, x, y, rows: Array.from({ length: rows }, (_, i) => ({ id: `r${i}`, type: 'switch' as const })) })
const def = (nodes: ReturnType<typeof panel>[]) => ({ id: 'rom', name: 'ROM', symbol: 'box' as const, nodes, wires: [] })
const bits = (p: ReturnType<typeof panel>) => p.rows.map(r => ((r as { on?: boolean }).on ? 1 : 0)).join('')
it('in reading order, row i is bit i; panels left over go to 0', () => {
// Two rows of panels, the second slightly out of line.
const ps = [panel('c', 10, 0), panel('a', 0, 1), panel('d', 0, 8), panel('e', 5, 9), panel('b', 5, 0)]
const d = def(ps)
expect(memoryPanels(d).map(n => n.id)).toEqual(['a', 'b', 'c', 'd', 'e'])
ps[3]!.rows[0]!.on = true
const r = loadIntoPanels(d, [1, 2, 0xc, 5], 4)
expect(r).toEqual({ ok: true, panels: 4, cleared: 1 })
const by = (id: string) => bits(ps.find(p => p.id === id)!)
expect([by('a'), by('b'), by('c'), by('d'), by('e')]).toEqual(['1000', '0100', '0011', '1010', '0000'])
loadIntoPanels(d, [1], 4, true)
expect(by('a')).toBe('0001')
})
it('refuses what does not fit', () => {
expect(loadIntoPanels(def([panel('a', 0, 0)]), [1, 2], 4).ok).toBe(false)
expect(loadIntoPanels(def([panel('a', 0, 0, 3)]), [1], 4).ok).toBe(false)
expect(loadIntoPanels(def([]), [1], 4).ok).toBe(false)
})
})