Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

yosys: the pane's server and readers to full coverage

deed3a191310ff3
diff --git a/store/agents/yosys/README.md b/store/agents/yosys/README.md
index acc4ac652..fa384fa59 100644
--- a/store/agents/yosys/README.md
+++ b/store/agents/yosys/README.md
@@ -91,5 +91,6 @@ upstream, bugs in the wrapper belong here, and a newer toolchain is a `brew upgr
harness dsh check . # conformance
harness dsh install "$PWD" --link # this checkout as the installed agent
harness dsh doctor autonomous/yosys # what this machine is missing
-npm test # the pane's readers, the judge and the VCD reader, without a toolchain
+npm test # the pane's server and readers, the judge, the VCD reader and the scripts, on stub tools
+ # (plus one real flow on the starter when the FPGA tools are installed)
```
diff --git a/store/agents/yosys/viewer/lib/chip.mjs b/store/agents/yosys/viewer/lib/chip.mjs
index 69e8e31a3..da9267665 100644
--- a/store/agents/yosys/viewer/lib/chip.mjs
+++ b/store/agents/yosys/viewer/lib/chip.mjs
@@ -151,7 +151,7 @@ export function routeSegments(routing) {
const at = [Number(pm[1]), Number(pm[2])]
drivenAt.set(wire, at)
const [src] = pm[3].split('.->.')
- const sm = PIP_WIRE.exec(src ?? '')
+ const sm = PIP_WIRE.exec(src)
if (sm) edges.push([`X${sm[1]}/Y${sm[2]}/${sm[3]}`, at, [Number(sm[1]), Number(sm[2])]])
}
const segs = []
diff --git a/store/agents/yosys/viewer/lib/schematic-worker.mjs b/store/agents/yosys/viewer/lib/schematic-worker.mjs
index 18f712cb4..d2ff10baf 100644
--- a/store/agents/yosys/viewer/lib/schematic-worker.mjs
+++ b/store/agents/yosys/viewer/lib/schematic-worker.mjs
@@ -11,6 +11,6 @@ parentPort.on('message', async ({ id, skin, netlist }) => {
const svg = await lib.render(skin, netlist)
parentPort.postMessage({ id, svg })
} catch (error) {
- parentPort.postMessage({ id, error: String(error?.message ?? error) })
+ parentPort.postMessage({ id, error: error.message })
}
})
diff --git a/store/agents/yosys/viewer/lib/vcd.mjs b/store/agents/yosys/viewer/lib/vcd.mjs
index 8679d092d..cd1c6f169 100644
--- a/store/agents/yosys/viewer/lib/vcd.mjs
+++ b/store/agents/yosys/viewer/lib/vcd.mjs
@@ -33,9 +33,10 @@ const MAX_CHANGES = 40_000_000 // across all signals; past this the dump is trun
/**
* @param {string} text the VCD
+ * @param {{maxChanges?: number}} [options] the cap on changes kept across all signals
* @returns {{timescale:string, tickFs:number, end:number, truncated:boolean, scopes:object[], vars:object[], signals:Map<string,object>}}
*/
-export function parseVcd(text) {
+export function parseVcd(text, { maxChanges = MAX_CHANGES } = {}) {
const n = text.length
let i = 0
@@ -131,7 +132,7 @@ export function parseVcd(text) {
return
}
}
- if (total >= MAX_CHANGES) { truncated = true; return }
+ if (total >= maxChanges) { truncated = true; return }
times.push(time)
values.push(value)
total++
diff --git a/store/agents/yosys/viewer/lib/workspace.mjs b/store/agents/yosys/viewer/lib/workspace.mjs
index ccb73105e..39a563591 100644
--- a/store/agents/yosys/viewer/lib/workspace.mjs
+++ b/store/agents/yosys/viewer/lib/workspace.mjs
@@ -41,7 +41,6 @@ export function fileInfo(ws, rel) {
}
function alive(pid) {
- if (!pid) return false
try { process.kill(pid, 0); return true } catch (e) { return e.code === 'EPERM' }
}
diff --git a/store/agents/yosys/viewer/test/chipdb-probe.mjs b/store/agents/yosys/viewer/test/chipdb-probe.mjs
new file mode 100644
index 000000000..3fb8991c7
--- /dev/null
+++ b/store/agents/yosys/viewer/test/chipdb-probe.mjs
@@ -0,0 +1,27 @@
+// Run by viewer.test.mjs in a child process, because chipdbDir() looks once per process: stands in
+// for the machine — `command -v icepack` answers $PROBE_ICEPACK, or fails when it is empty, in which
+// case no chipdb exists anywhere either — then prints what chip.mjs found.
+import { createRequire, syncBuiltinESMExports } from 'node:module'
+
+const require = createRequire(import.meta.url)
+const fs = require('node:fs')
+const childProcess = require('node:child_process')
+const icepack = process.env.PROBE_ICEPACK
+let lookupPath = null
+childProcess.execFileSync = (file, args, options) => {
+ lookupPath = options.env.PATH
+ if (!icepack) throw new Error('command -v icepack: exit 1')
+ return `${icepack}\n`
+}
+if (!icepack) {
+ const exists = fs.existsSync
+ fs.existsSync = (path) => !String(path).endsWith('chipdb-5k.txt') && exists(path)
+}
+if (process.env.PROBE_NO_PATH) delete process.env.PATH
+syncBuiltinESMExports()
+
+const { chipdbDir, packagePins } = await import('../lib/chip.mjs')
+const dir = chipdbDir()
+const again = chipdbDir()
+const pins = JSON.parse(process.env.PROBE_PINS).map(([arch, pkg]) => packagePins(arch, pkg))
+process.stdout.write(JSON.stringify({ dir, cached: again === dir, lookupPath, pins, pinsCached: packagePins('up5k', 'sg48') === pins[0] }))
diff --git a/store/agents/yosys/viewer/test/server.test.mjs b/store/agents/yosys/viewer/test/server.test.mjs
new file mode 100644
index 000000000..caff0dcfc
--- /dev/null
+++ b/store/agents/yosys/viewer/test/server.test.mjs
@@ -0,0 +1,401 @@
+// viewer.mjs as Harness runs it: a child process on a loopback port over a workspace laid out as
+// flow.sh leaves it. `node --test viewer/test/` — the schematic is drawn for real when this
+// checkout's node_modules has netlistsvg (toolchain/setup.sh), and says it cannot be otherwise.
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { once } from 'node:events'
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { request } from 'node:http'
+import { createServer } from 'node:net'
+import { tmpdir } from 'node:os'
+import { dirname, join } from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { setTimeout as sleep } from 'node:timers/promises'
+
+const PKG = fileURLToPath(new URL('../..', import.meta.url))
+const PRELOAD = fileURLToPath(new URL('./sigterm-exit.mjs', import.meta.url))
+const HAS_NETLISTSVG = existsSync(join(PKG, 'node_modules', 'netlistsvg', 'lib', 'default.svg'))
+
+const VCD = `$timescale 1ps $end
+$scope module blink_tb $end
+$var reg 1 ! clk $end
+$var reg 4 " count [3:0] $end
+$upscope $end
+$enddefinitions $end
+#0
+0!
+b0 "
+#5
+1!
+b1 "
+`
+
+const DIVIDER = "$paramod\\divider\\N=s32'00000000000000000000000000001000"
+const SCHEMATIC = {
+ modules: {
+ blink: {
+ attributes: { top: '00000000000000000000000000000001', src: 'rtl/blink.v:1.1-20.10' },
+ ports: { clk: { direction: 'input', bits: [2] }, led: { direction: 'output', bits: [3] } },
+ cells: {
+ u_div: { type: DIVIDER, port_directions: { clk: 'input', q: 'output' }, connections: { clk: [2], q: [4] }, attributes: { src: 'rtl/blink.v:5.3-5.30' } },
+ '$not$rtl/blink.v:9$1': { type: '$not', port_directions: { A: 'input', Y: 'output' }, connections: { A: [4], Y: [3] }, attributes: {} },
+ },
+ netnames: {
+ clk: { hide_name: 0, bits: [2], attributes: {} },
+ led: { hide_name: 0, bits: [3], attributes: {} },
+ tick: { hide_name: 0, bits: [4], attributes: { src: 'rtl/blink.v:3' } },
+ '$auto$tick': { hide_name: 1, bits: [4], attributes: {} },
+ },
+ },
+ [DIVIDER]: {
+ attributes: { src: 'rtl/blink.v:22.1-30.10' },
+ parameter_default_values: { N: '00000000000000000000000000001000' },
+ ports: { clk: { direction: 'input', bits: [2] }, q: { direction: 'output', bits: [3] } },
+ cells: {},
+ netnames: { clk: { hide_name: 0, bits: [2] }, q: { hide_name: 0, bits: [3] }, count: { hide_name: 0, bits: [4, 5] } },
+ },
+ // netlistsvg cannot draw a port whose bits are not a list
+ broken: { attributes: {}, ports: { a: { direction: 'input', bits: 'zz' } }, cells: {}, netnames: {} },
+ },
+}
+
+const ROUTED = {
+ modules: {
+ blink: {
+ settings: { 'arch.type': 'up5k', 'arch.package': 'sg48' },
+ cells: {
+ 'led$sb_io': { type: 'SB_IO', parameters: {}, attributes: { NEXTPNR_BEL: 'X13/Y31/io1' }, port_directions: {}, connections: {} },
+ 'tick_LC': { type: 'ICESTORM_LC', parameters: { DFF_ENABLE: '1' }, attributes: { NEXTPNR_BEL: 'X12/Y30/lc0' }, port_directions: {}, connections: {} },
+ },
+ netnames: {},
+ },
+ },
+}
+
+function write(root, rel, content) {
+ const full = join(root, rel)
+ mkdirSync(dirname(full), { recursive: true })
+ writeFileSync(full, typeof content === 'string' || Buffer.isBuffer(content) ? content : JSON.stringify(content))
+}
+
+function workspace(t) {
+ const ws = mkdtempSync(join(tmpdir(), 'yosys-viewer-'))
+ t.after(() => rmSync(ws, { recursive: true, force: true }))
+ return ws
+}
+
+/** A workspace after a whole flow run. */
+function built(ws) {
+ write(ws, 'rtl/blink.v', 'module blink(input clk, output led); endmodule\n')
+ write(ws, 'tb/blink_tb.v', 'module blink_tb; endmodule\n')
+ write(ws, 'constraints/blink.pcf', 'set_io -nowarn clk 35\nset_io led 39\nset_frequency clk 12\n')
+ write(ws, 'notes.xyz', 'odd\n')
+ write(ws, 'out/.top', 'blink\n')
+ write(ws, 'out/sim.vcd', VCD)
+ write(ws, 'out/blink_schematic.json', SCHEMATIC)
+ write(ws, 'out/blink.asc', '.device 5k\n.logic_tile 12 30\n.io_tile 13 31\n')
+ write(ws, 'out/blink_routed.json', ROUTED)
+ write(ws, 'out/blink_pnr.json', { utilization: { ICESTORM_LC: { used: 1, available: 5280 } }, fmax: {}, critical_paths: [] })
+ write(ws, 'out/blink.report.json', { top: 'blink', ready: true })
+ write(ws, 'out/blink.svg', '<svg/>')
+ write(ws, 'out/blink.bin', Buffer.from([0, 1, 2, 3]))
+ write(ws, 'out/logs/run.json', { top: 'blink', pid: 1, startedAt: 1, finishedAt: 2, device: '--up5k', package: 'sg48' })
+ write(ws, 'out/logs/sim.exit', '0\n')
+ write(ws, 'out/logs/sim.log', 'PASS blink\n')
+ return ws
+}
+
+async function freePort() {
+ const server = createServer()
+ server.listen(0, '127.0.0.1')
+ await once(server, 'listening')
+ const { port } = server.address()
+ server.close()
+ await once(server, 'close')
+ return port
+}
+
+async function startViewer(t, ws, { cwd } = {}) {
+ const port = await freePort()
+ const env = { ...process.env, HARNESS_VIEWER_PORT: String(port) }
+ if (ws) env.HARNESS_WORKSPACE = ws
+ else delete env.HARNESS_WORKSPACE
+ const child = spawn(process.execPath, ['--import', PRELOAD, join(PKG, 'viewer.mjs')], { cwd, env, stdio: ['ignore', 'pipe', 'pipe'] })
+ let output = ''
+ child.stdout.on('data', (d) => { output += d })
+ child.stderr.on('data', (d) => { output += d })
+ const stop = async () => {
+ if (child.exitCode === null && child.signalCode === null) {
+ child.kill('SIGTERM')
+ await once(child, 'exit')
+ }
+ }
+ t.after(stop)
+ for (let k = 0; k < 200 && !output.includes('listening on'); k++) await sleep(25)
+ assert.match(output, /\[yosys\] listening on http:\/\/127\.0\.0\.1:\d+\//)
+ return { port, output: () => output, stop }
+}
+
+function get(port, path, method = 'GET') {
+ return new Promise((resolve, reject) => {
+ const req = request({ host: '127.0.0.1', port, path, method }, (res) => {
+ const chunks = []
+ res.on('data', (c) => chunks.push(c))
+ res.on('end', () => {
+ const body = Buffer.concat(chunks).toString('utf8')
+ let json = null
+ try { json = JSON.parse(body) } catch { /* not JSON */ }
+ resolve({ status: res.statusCode, type: res.headers['content-type'], headers: res.headers, body, json })
+ })
+ })
+ req.on('error', reject)
+ req.end()
+ })
+}
+
+test('the page, its files, and workspace files — never anything outside either', async (t) => {
+ const ws = built(workspace(t))
+ const { port } = await startViewer(t, ws)
+
+ for (const path of ['/', '/index.html']) {
+ const r = await get(port, path)
+ assert.equal(r.status, 200)
+ assert.equal(r.type, 'text/html; charset=utf-8')
+ assert.match(r.body, /<html/i)
+ }
+ const head = await get(port, '/', 'HEAD')
+ assert.deepEqual([head.status, head.body], [200, ''])
+ assert.ok(Number(head.headers['content-length']) > 0)
+
+ assert.equal((await get(port, '/app/app.js')).type, 'text/javascript; charset=utf-8')
+ assert.equal((await get(port, '/app/app.css')).type, 'text/css; charset=utf-8')
+ assert.equal((await get(port, '/app/nope.js')).status, 404)
+ assert.equal((await get(port, '/app/..%2F..%2Fviewer.mjs')).status, 404)
+
+ const v = await get(port, '/ws/rtl/blink.v')
+ assert.deepEqual([v.status, v.type, v.body], [200, 'text/plain; charset=utf-8', 'module blink(input clk, output led); endmodule\n'])
+ assert.equal((await get(port, '/ws/out/blink.bin')).type, 'application/octet-stream')
+ assert.equal((await get(port, '/ws/notes.xyz')).type, 'application/octet-stream')
+ assert.equal((await get(port, '/ws/')).status, 404) // the workspace itself is not a file
+ assert.equal((await get(port, '/ws/..%2F..%2F..%2Fetc%2Fhosts')).status, 404)
+ // old links: a workspace path at the root
+ const svg = await get(port, '/out/blink.svg')
+ assert.deepEqual([svg.status, svg.type], [200, 'image/svg+xml'])
+ assert.equal((await get(port, '/out/missing.svg')).status, 404)
+})
+
+test('a malformed path or an unreadable file is a 500, and the server keeps serving', async (t) => {
+ const ws = built(workspace(t))
+ write(ws, 'rtl/locked.v', 'secret\n')
+ chmodSync(join(ws, 'rtl/locked.v'), 0o000) // a regular file in the temp workspace; removed with it
+ const { port, output } = await startViewer(t, ws)
+ const bad = await get(port, '/%E0%A4%A')
+ assert.equal(bad.status, 500)
+ assert.match(bad.json.error, /URI malformed/)
+ const locked = await get(port, '/ws/rtl/locked.v')
+ assert.equal(locked.status, 500)
+ assert.match(locked.json.error, /EACCES/)
+ assert.equal((await get(port, '/ws/rtl/blink.v')).status, 200)
+ assert.match(output(), /\[yosys\] URIError/)
+})
+
+test('/api/state: the top, the flow, the report and which files exist', async (t) => {
+ const ws = workspace(t)
+ const { port } = await startViewer(t, ws)
+ let s = (await get(port, '/api/state')).json
+ assert.equal(s.top, null)
+ assert.deepEqual([s.files, s.sources, s.report], [{}, [], null])
+ assert.equal(s.workspace, ws.split('/').pop())
+
+ built(ws)
+ s = (await get(port, '/api/state?file=out%2Fblink.report.json')).json
+ assert.equal(s.top, 'blink')
+ assert.deepEqual(s.report, { top: 'blink', ready: true })
+ assert.equal(s.files.vcd.path, 'out/sim.vcd')
+ assert.equal(s.files.waves, null)
+ assert.equal(s.files.bin.size, 4)
+ assert.equal(s.flow.steps[0].state, 'done')
+ assert.deepEqual(s.sources.map((f) => f.path), ['rtl/blink.v', 'tb/blink_tb.v', 'constraints/blink.pcf'])
+})
+
+test('/api/vcd: the hierarchy up front, the changes on demand, re-read when the dump changes', async (t) => {
+ const ws = workspace(t)
+ const { port } = await startViewer(t, ws)
+ assert.deepEqual((await get(port, '/api/vcd/meta')).json, { error: 'no out/sim.vcd yet' })
+ assert.equal((await get(port, '/api/vcd/data?ids=!')).status, 404)
+
+ write(ws, 'out/sim.vcd', VCD)
+ const meta = (await get(port, '/api/vcd/meta')).json
+ assert.equal(meta.path, 'out/sim.vcd')
+ assert.equal(meta.end, 5)
+ assert.deepEqual(meta.scopes[0].vars.map((v) => v.name), ['clk', 'count'])
+ assert.equal((await get(port, '/api/vcd/meta')).json.mtime, meta.mtime) // cached
+
+ const data = (await get(port, `/api/vcd/data?ids=${encodeURIComponent('! " nope')}`)).json
+ assert.equal(data.mtime, meta.mtime)
+ assert.deepEqual(data.signals.map((s) => [s.id, s.v]), [['!', '01'], ['"', ['0000', '0001']]])
+ assert.deepEqual((await get(port, '/api/vcd/data')).json.signals, [])
+
+ write(ws, 'out/sim.vcd', VCD + '#9\n0!\n#12\n')
+ assert.equal((await get(port, '/api/vcd/meta')).json.end, 12)
+})
+
+test('/api/netlist: the instance tree, one module\'s names, and every name for search', async (t) => {
+ const ws = workspace(t)
+ const { port } = await startViewer(t, ws)
+ for (const path of ['/api/netlist', '/api/netlist/module?module=blink', '/api/netlist/names']) {
+ assert.equal((await get(port, path)).status, 404, path) // no top module yet
+ }
+ write(ws, 'out/.top', 'blink\n')
+ assert.deepEqual((await get(port, '/api/netlist')).json, { error: 'no schematic netlist yet' })
+ assert.equal((await get(port, '/api/netlist/names')).status, 404)
+
+ write(ws, 'out/blink_schematic.json', SCHEMATIC)
+ const tree = (await get(port, '/api/netlist')).json
+ assert.equal(tree.top, 'blink')
+ assert.match(tree.stamp, /^\d+:\d+$/)
+ assert.deepEqual(tree.tree.children.map((c) => [c.name, c.label, c.params]), [['u_div', 'divider', { N: '8' }]])
+
+ const mod = (await get(port, `/api/netlist/module?module=${encodeURIComponent(DIVIDER)}`)).json
+ assert.equal(mod.label, 'divider')
+ assert.deepEqual(Object.keys(mod.ports), ['clk', 'q'])
+ assert.equal((await get(port, '/api/netlist/module?module=nope')).status, 404)
+ assert.equal((await get(port, '/api/netlist/module')).status, 404)
+ // ?top= wins over the workspace's own
+ assert.equal((await get(port, '/api/netlist?top=other')).status, 404)
+
+ const { names } = (await get(port, '/api/netlist/names')).json
+ assert.deepEqual(names, [
+ { kind: 'port', name: 'clk', path: '' }, { kind: 'port', name: 'led', path: '' },
+ { kind: 'net', name: 'tick', path: '' },
+ { kind: 'inst', name: 'u_div', path: '' },
+ { kind: 'port', name: 'clk', path: 'u_div' }, { kind: 'port', name: 'q', path: 'u_div' },
+ { kind: 'net', name: 'count', path: 'u_div' },
+ ])
+
+ // a netlist with no modules has no tree to search; a top that is not a module has no names
+ write(ws, 'out/blink_schematic.json', '{}')
+ assert.deepEqual((await get(port, '/api/netlist/names')).json, { names: [] })
+ assert.equal((await get(port, '/api/schematic.svg?module=blink')).status, 404)
+ write(ws, 'out/blink_schematic.json', '{"modules": {"blink": null}}')
+ assert.deepEqual((await get(port, '/api/netlist/names')).json, { names: [] })
+ // a netlist that is not JSON (yosys still writing it) is not there yet
+ write(ws, 'out/blink_schematic.json', '{"modules": ')
+ assert.equal((await get(port, '/api/netlist')).status, 404)
+})
+
+test('/api/schematic.svg: one module drawn by netlistsvg, once per netlist', async (t) => {
+ const ws = built(workspace(t))
+ const { port } = await startViewer(t, ws)
+ assert.equal((await get(port, '/api/schematic.svg?module=nope')).status, 404)
+ assert.equal((await get(port, '/api/schematic.svg')).status, 404)
+ const first = await get(port, '/api/schematic.svg?module=blink')
+ if (HAS_NETLISTSVG) {
+ assert.deepEqual([first.status, first.type], [200, 'image/svg+xml'])
+ assert.match(first.body, /^<svg/)
+ assert.equal((await get(port, '/api/schematic.svg?module=blink')).body, first.body) // the cached job
+ const broken = await get(port, '/api/schematic.svg?module=broken')
+ assert.deepEqual([broken.status, broken.type], [500, 'text/plain'])
+ assert.match(broken.body, /is not a function/)
+ // a failed drawing is forgotten, so the next request tries again
+ assert.equal((await get(port, '/api/schematic.svg?module=broken')).status, 500)
+ } else {
+ assert.deepEqual([first.status, first.body], [500, 'netlistsvg is not installed here — run toolchain/setup.sh'])
+ }
+ write(ws, 'out/.top', 'nobody\n')
+ assert.equal((await get(port, '/api/schematic.svg?module=blink')).status, 404)
+})
+
+test('/api/chip and /api/board: the floorplan, the pins, the constraints', async (t) => {
+ const ws = workspace(t)
+ const { port } = await startViewer(t, ws)
+ const nothing = (await get(port, '/api/board')).json // no top module at all
+ assert.deepEqual([nothing.top, nothing.pcf, nothing.ports, nothing.arch], [null, null, null, 'up5k'])
+ write(ws, 'out/.top', 'blink\n')
+ assert.deepEqual((await get(port, '/api/chip')).json, { error: 'no place-and-route output yet' })
+
+ // before place and route: the board from the run record, no ports yet
+ write(ws, 'out/logs/run.json', { device: '--hx8k', package: 'ct256' })
+ let board = (await get(port, '/api/board')).json
+ assert.deepEqual([board.top, board.pcf, board.ports, board.arch, board.package, board.placed], ['blink', null, null, 'hx8k', 'ct256', []])
+ rmSync(join(ws, 'out/logs/run.json'))
+ board = (await get(port, '/api/board')).json
+ assert.deepEqual([board.arch, board.package], ['up5k', 'sg48'])
+ assert.ok(board.pins.some((p) => p.pin === '35'))
+
+ built(ws)
+ const chip = (await get(port, '/api/chip')).json
+ assert.equal(chip.arch, 'up5k')
+ assert.deepEqual(chip.cells.map((c) => [c.n, c.t, c.x, c.y]), [['led$sb_io', 'SB_IO', 13, 31], ['tick_LC', 'ICESTORM_LC', 12, 30]])
+ assert.equal(chip.grid.width, 14)
+ assert.equal(chip.files.length, 3)
+ assert.equal((await get(port, '/api/chip')).json.stamp, chip.stamp) // cached
+
+ board = (await get(port, '/api/board')).json
+ assert.deepEqual(board.pcf.ios.map((io) => [io.port, io.pin]), [['clk', '35'], ['led', '39']])
+ assert.equal(board.pcf.path, 'constraints/blink.pcf')
+ assert.deepEqual(board.placed, [{ cell: 'led$sb_io', x: 13, y: 31, z: 1 }])
+ // the ports come from the schematic netlist until synthesis writes its own
+ assert.deepEqual(board.ports, { from: 'out/blink_schematic.json', ports: { clk: { direction: 'input', width: 1 }, led: { direction: 'output', width: 1 } } })
+ write(ws, 'out/blink.json', { modules: { blink: { ports: { clk: { direction: 'input', bits: [2] } } } } })
+ assert.deepEqual((await get(port, '/api/board')).json.ports, { from: 'out/blink.json', ports: { clk: { direction: 'input', width: 1 } } })
+})
+
+test('/api/log and unknown endpoints', async (t) => {
+ const ws = built(workspace(t))
+ const { port } = await startViewer(t, ws)
+ const log = await get(port, '/api/log?step=sim')
+ assert.deepEqual([log.status, log.type, log.body], [200, 'text/plain; charset=utf-8', 'PASS blink\n'])
+ assert.equal((await get(port, '/api/log?step=..%2F..%2Fsim')).body, 'PASS blink\n') // only letters survive
+ assert.equal((await get(port, '/api/log?step=pack')).status, 404)
+ assert.equal((await get(port, '/api/log')).status, 404)
+ assert.deepEqual((await get(port, '/api/nope')).json, { error: 'unknown endpoint' })
+})
+
+test('/events: batched change events naming the files, not the noise', async (t) => {
+ const ws = built(workspace(t))
+ const { port } = await startViewer(t, ws)
+ const events = []
+ const res = await new Promise((resolve, reject) => {
+ const req = request({ host: '127.0.0.1', port, path: '/events' }, resolve)
+ req.on('error', reject)
+ req.end()
+ })
+ assert.equal(res.headers['content-type'], 'text/event-stream')
+ let buffer = ''
+ res.setEncoding('utf8')
+ res.on('data', (chunk) => {
+ buffer += chunk
+ for (let at = buffer.indexOf('\n\n'); at >= 0; at = buffer.indexOf('\n\n')) {
+ events.push(buffer.slice(0, at))
+ buffer = buffer.slice(at + 2)
+ }
+ })
+ for (let k = 0; k < 100 && !events.length; k++) await sleep(20)
+ assert.equal(events[0], 'retry: 1000')
+ await sleep(300) // let the watcher settle past the files built() wrote
+
+ events.length = 0
+ write(ws, 'node_modules/x.js', '')
+ write(ws, '.git/HEAD', '')
+ write(ws, '.claude/settings.json', '{}')
+ write(ws, 'out/sim.vvp', '')
+ write(ws, 'rtl/blink.v', 'module blink; endmodule\n')
+ write(ws, 'out/logs/synth.log', 'reading\n')
+ for (let k = 0; k < 100 && !events.some((e) => e.includes('synth.log')); k++) await sleep(20)
+ const paths = new Set(events.filter((e) => e.startsWith('event: change')).flatMap((e) => JSON.parse(e.split('data: ')[1]).paths))
+ assert.ok(paths.has('rtl/blink.v'), [...paths].join(' '))
+ assert.ok(paths.has('out/logs/synth.log'), [...paths].join(' '))
+ for (const p of paths) assert.doesNotMatch(p, /node_modules|^\.git\/|\.vvp$|^\.claude/)
+ res.destroy()
+})
+
+test('with no HARNESS_WORKSPACE it serves the directory it was started in; a workspace that is not there is not watched', async (t) => {
+ const ws = built(workspace(t))
+ const here = await startViewer(t, null, { cwd: ws })
+ assert.equal((await get(here.port, '/api/state')).json.top, 'blink')
+ const gone = await startViewer(t, join(ws, 'not-there'))
+ assert.match(gone.output(), /\[yosys\] watch failed: /)
+ assert.equal((await get(gone.port, '/api/state')).json.top, null)
+})
diff --git a/store/agents/yosys/viewer/test/sigterm-exit.mjs b/store/agents/yosys/viewer/test/sigterm-exit.mjs
new file mode 100644
index 000000000..1adac5aa3
--- /dev/null
+++ b/store/agents/yosys/viewer/test/sigterm-exit.mjs
@@ -0,0 +1,3 @@
+// Preloaded (node --import) into the viewer the server tests spawn: exit normally on SIGTERM, so the
+// process runs its exit hooks — V8 writes its coverage there — instead of dying on the signal.
+process.on('SIGTERM', () => process.exit(0))
diff --git a/store/agents/yosys/viewer/test/viewer.test.mjs b/store/agents/yosys/viewer/test/viewer.test.mjs
index 648592027..2300e73e7 100644
--- a/store/agents/yosys/viewer/test/viewer.test.mjs
+++ b/store/agents/yosys/viewer/test/viewer.test.mjs
@@ -1,13 +1,15 @@
// The pane's readers, without a browser or a toolchain: `npm test` (or node --test viewer/test/*.test.mjs).
import { test } from 'node:test'
import assert from 'node:assert/strict'
-import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
+import { spawnSync } from 'node:child_process'
+import { mkdtempSync, mkdirSync, realpathSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
+import { fileURLToPath } from 'node:url'
import { extendBits, parseVcd, tickFs, vcdMeta, vcdSignal } from '../lib/vcd.mjs'
import { buildChip, moduleOf, routeSegments, tilesFromText } from '../lib/chip.mjs'
-import { cleanModuleName, hierarchy, moduleForRender, moduleIndex, paramsOf } from '../lib/netlist.mjs'
-import { errorLine, findTop, flowState, parsePcf } from '../lib/workspace.mjs'
+import { cleanModuleName, hierarchy, moduleForRender, moduleIndex, paramsOf, topModule } from '../lib/netlist.mjs'
+import { errorLine, fileInfo, findTop, flowState, parsePcf, readText, sources, topPorts } from '../lib/workspace.mjs'
import { formatValue, uartDecode, uartTiming } from '../public/wavecore.js'
// Trimmed from Icarus: two $dumpvars make the testbench scope appear twice, a task scope, a
@@ -272,3 +274,296 @@ test('flow state: running, done, failed with its error line, skipped once the ru
}
assert.equal(errorLine('fine\n ok no error here\nError: it broke\n'), 'Error: it broke')
})
+
+// ------------------------------------------------------------------------------ the edges
+
+test('VCD edges: odd headers, same-tick rewrites, reals, strings, scalar levels, the change cap', () => {
+ assert.equal(tickFs(undefined), 1e3)
+ assert.equal(extendBits('10101', 3), '101')
+ assert.equal(extendBits('', 4), '0000')
+ assert.equal(parseVcd('$date never ends').signals.size, 0)
+
+ const p = parseVcd(`$timescale 10 ns $end
+$var wire 1 ! top_level $end
+$var wire 1 $end
+$var reg w # oddwidth $end
+$scope module m $end
+$var reg 8 @ bus [7:0] $end
+$var wire 1 ^ bit [3] $end
+$var real 64 % temp $end
+$var string 1 & msg $end
+$upscope $end
+$scope module m $end
+$var reg 8 @ bus [7:0] $end
+$upscope $end
+$upscope $end
+$enddefinitions $end
+#0
+0!
+1!
+b101 %
+x@
+#5
+1!
+$comment 0! not a change $end
+r2.5 %
+R2.5 %
+r9 ?
+sHello &
+Sbye ?
+b1 ?
+l^
+#6
+h^
+#7
+u^
+#8
+Z^
+#9
+X^
+0?
+`)
+ assert.equal(p.tickFs, 1e7)
+ assert.deepEqual(p.vars.map((v) => [v.path, v.width, v.msb, v.lsb]),
+ [['top_level', 1, 0, 0], ['oddwidth', 1, 0, 0], ['m.bus', 8, 7, 0], ['m.bit', 1, 3, 3], ['m.temp', 64, 63, 0], ['m.msg', 1, 0, 0]])
+ const sig = (id) => p.signals.get(id)
+ // 0 then 1 in the first tick: the later write wins; 1 again at #5 is no edge
+ assert.deepEqual([sig('!').times, sig('!').values], [[0], ['1']])
+ assert.deepEqual(sig('@').values, ['xxxxxxxx']) // a scalar x on a bus fills it
+ assert.deepEqual([sig('%').real, sig('%').values], [true, ['101', '2.5']])
+ assert.deepEqual(sig('&').values, ['Hello'])
+ assert.deepEqual(sig('^').values, ['0', '1', 'x', 'z', 'x'])
+ assert.equal(p.truncated, false)
+
+ const capped = parseVcd('$var wire 1 ! a $end $enddefinitions $end #0 0! #1 1! #2 0! #3 1!', { maxChanges: 2 })
+ assert.deepEqual([capped.truncated, capped.signals.get('!').values], [true, ['0', '1']])
+})
+
+test('chipdb: found beside icepack and read from its head, or the built-in UP5K pin table', () => {
+ const probe = (env, pins) => {
+ const r = spawnSync(process.execPath, [fileURLToPath(new URL('./chipdb-probe.mjs', import.meta.url))], {
+ env: { ...process.env, ...env, PROBE_PINS: JSON.stringify(pins) }, encoding: 'utf8',
+ })
+ assert.equal(r.status, 0, r.stderr)
+ return JSON.parse(r.stdout)
+ }
+ const root = mkdtempSync(join(tmpdir(), 'yosys-chipdb-'))
+ try {
+ mkdirSync(join(root, 'bin'))
+ writeFileSync(join(root, 'bin', 'icepack'), '')
+ mkdirSync(join(root, 'share', 'icestorm', 'chipdb'), { recursive: true })
+ writeFileSync(join(root, 'share', 'icestorm', 'chipdb', 'chipdb-5k.txt'),
+ '.device 5k\n.pins sg48\n2 8 0 0\n3 9 0 1\n\n.pins uwg30\nA1 1 2 3\n.io_tile 1 0\n')
+ const found = probe({ PROBE_ICEPACK: join(root, 'bin', 'icepack') },
+ [['up5k', 'sg48'], ['up5k', 'uwg30'], ['up5k', 'nope'], ['hx8k', 'ct256'], ['ecp5', 'x']])
+ assert.equal(realpathSync(found.dir), realpathSync(join(root, 'share', 'icestorm', 'chipdb')))
+ assert.equal(found.cached, true)
+ assert.ok(found.lookupPath.endsWith(':/opt/homebrew/bin:/usr/local/bin:/usr/bin'))
+ assert.deepEqual(found.pins, [
+ [{ pin: '2', x: 8, y: 0, z: 0 }, { pin: '3', x: 9, y: 0, z: 1 }],
+ [{ pin: 'A1', x: 1, y: 2, z: 3 }],
+ null, // no such package in the chipdb, and not the one the table knows
+ null, // chipdb-8k.txt is not there
+ null, // not an iCE40
+ ])
+ assert.equal(found.pinsCached, true)
+
+ const none = probe({ PROBE_ICEPACK: '', PROBE_NO_PATH: '1' }, [['up5k', 'sg48'], ['hx8k', 'ct256']])
+ assert.equal(none.dir, null)
+ assert.equal(none.lookupPath, ':/opt/homebrew/bin:/usr/local/bin:/usr/bin')
+ assert.equal(none.pins[0].length, 39)
+ assert.deepEqual(none.pins[0].find((pin) => pin.pin === '35'), { pin: '35', x: 12, y: 31, z: 1 })
+ assert.equal(none.pins[1], null)
+ } finally {
+ rmSync(root, { recursive: true, force: true })
+ }
+})
+
+test('floorplan edges: unknown tiles, odd routes, bare cells and nets, critical paths', () => {
+ assert.deepEqual(tilesFromText('.device 5k\n.weird_tile 1 0\n').rows, ['.?'])
+ assert.equal(tilesFromText('.device 5k\nno tiles\n'), null)
+ assert.equal(moduleOf('9lives.cell_LC'), '')
+ // a pip outside any tile, a source wire not in X.Y.name form, and the same segment twice
+ const r = routeSegments('X1/Y1/a;;1;X2/Y2/b;junk;1;X3/Y3/c;X3/Y3/nodots.->.x;1;X5/Y5/d;X5/Y5/1.1.a.->.5.5.d;1;X5/Y5/e;X5/Y5/1.1.a.->.5.5.e;1')
+ assert.deepEqual(r, { segs: [1, 1, 5, 5], global: false })
+
+ const empty = buildChip({})
+ assert.deepEqual([empty.arch, empty.package, empty.grid, empty.cells, empty.nets, empty.utilization, empty.fmax, empty.criticalPaths],
+ ['up5k', 'sg48', null, [], [], null, null, []])
+ assert.deepEqual(buildChip({ routed: {} }).cells, [])
+ assert.equal(buildChip({ asc: '.device 1k\n.logic_tile 0 0\n' }).arch, 'hx1k')
+ assert.equal(buildChip({ asc: '.device 2x\n.logic_tile 0 0\n' }).arch, '2x')
+
+ const chip = buildChip({
+ asc: '.device 8k\n.logic_tile 1 1\n',
+ routed: {
+ modules: {
+ top: {
+ cells: {
+ bare: { type: 'ICESTORM_LC' },
+ lc: { type: 'ICESTORM_LC', parameters: { DFF_ENABLE: 1, CARRY_ENABLE: '1', LUT_INIT: '1' }, attributes: { NEXTPNR_BEL: 'X1/Y1/lc1' }, port_directions: { I0: 'input', O: 'output' }, connections: { I0: [5], I1: ['0'], O: [6] } },
+ carry: { type: 'ICESTORM_LC', parameters: { DFF_ENABLE: '0', CARRY_ENABLE: 1 }, attributes: {}, connections: {} },
+ io: { type: 'SB_IO', attributes: { NEXTPNR_BEL: 'X0/Y1/io0' }, port_directions: { D_IN_0: 'output' }, connections: { D_IN_0: [5] } },
+ },
+ netnames: {
+ clk: { bits: [5], attributes: { ROUTING: 'X0/Y1/io_0:D_IN_0;;1;X1/Y1/lutff_1:in_0;X1/Y1/0.1.glb_netwk_1.->.1.1.lutff_1:in_0;1' } },
+ clk_alias: { bits: [5], attributes: { ROUTING: 'X9/Y9/x;;1' } },
+ unrouted: { bits: [6], attributes: {} },
+ constant: { bits: ['x'] },
+ floating: { bits: [8], attributes: { ROUTING: 'X2/Y2/w;;1' } },
+ },
+ },
+ },
+ },
+ report: {
+ critical_paths: [
+ { from: 'a', to: 'b', path: [
+ { type: 'clk-to-q', delay: 0.5, net: 'clk', sources: ['rtl/blink.v:3'], from: { cell: 'x', port: 'Q', loc: [1, 1] }, to: { cell: 'y', port: 'I0', loc: [2, 2] } },
+ { type: 'setup', delay: 0.1 },
+ ] },
+ { from: 'c', to: 'd' },
+ ],
+ },
+ })
+ assert.deepEqual([chip.arch, chip.package], ['hx8k', 'sg48'])
+ const [bare, lc, carry, io] = chip.cells
+ assert.deepEqual([bare.x, bare.y, bare.b, bare.lut, bare.ff, bare.carry, bare.k], [-1, -1, '', 0, 0, 0, undefined])
+ assert.deepEqual([lc.lut, lc.ff, lc.carry, lc.k, lc.init], [1, 1, 1, 2, undefined])
+ assert.deepEqual([carry.ff, carry.carry], [0, 1])
+ assert.equal(io.t, 'SB_IO')
+ assert.deepEqual(chip.nets, [
+ { n: 'clk', g: 1, s: [0, 1, 1, 1], d: 3, k: [1] },
+ { n: 'floating', g: 0, s: [], d: -1, k: [] },
+ ])
+ assert.deepEqual(chip.criticalPaths, [
+ { from: 'a', to: 'b', path: [
+ { type: 'clk-to-q', delay: 0.5, net: 'clk', sources: ['rtl/blink.v:3'], from: { cell: 'x', port: 'Q', loc: [1, 1] }, to: { cell: 'y', port: 'I0', loc: [2, 2] } },
+ { type: 'setup', delay: 0.1, net: null, sources: [], from: null, to: null },
+ ] },
+ { from: 'c', to: 'd', path: [] },
+ ])
+ assert.deepEqual([chip.utilization, chip.fmax], [null, null])
+})
+
+test('netlist edges: names, parameters, deep and bare modules', () => {
+ assert.equal(cleanModuleName(''), '')
+ assert.equal(cleanModuleName(undefined), undefined)
+ assert.equal(cleanModuleName('$paramod'), '$paramod')
+ assert.equal(cleanModuleName('\\top'), 'top')
+ assert.deepEqual(paramsOf({ parameter_default_values: { TEXT: 'abc', WIDE: '1'.repeat(60), N: '101' } }),
+ { TEXT: 'abc', WIDE: '1'.repeat(60), N: '5' })
+ assert.deepEqual(paramsOf(undefined), {})
+ assert.equal(topModule(undefined), null)
+ assert.equal(hierarchy({}), null)
+
+ const nl = {
+ modules: {
+ top: { cells: { m1: { type: 'mid' } } },
+ mid: { cells: { l1: { type: 'leaf' }, again: { type: 'mid' } } },
+ leaf: {},
+ },
+ }
+ const tree = hierarchy(nl)
+ const leaf = tree.children[0].children.find((c) => c.name === 'l1')
+ assert.deepEqual([leaf.path, leaf.cells, leaf.children], ['m1.l1', 0, []])
+ // a module that instantiates itself stops at the depth limit instead of recursing forever
+ let depth = 0
+ for (let node = tree.children[0]; node; node = node.children.find((c) => c.name === 'again')) depth++
+ assert.equal(depth, 33)
+
+ assert.equal(moduleForRender({}, 'top'), null)
+ assert.equal(moduleForRender(nl, 'nope'), null)
+ assert.deepEqual(moduleForRender(nl, 'leaf'), { modules: { leaf: { attributes: { top: 1 }, cells: {} } } })
+
+ assert.equal(moduleIndex(undefined, 'top'), null)
+ const bare = moduleIndex(nl, 'leaf')
+ assert.deepEqual([bare.nets, bare.cells, bare.ports, bare.src], [{}, {}, {}, ''])
+ const idx = moduleIndex({
+ modules: {
+ top: {
+ attributes: { src: 'rtl/top.v:1' },
+ ports: { nobits: { direction: 'input' } },
+ netnames: {
+ a: { bits: [2, '0', 'x'] }, b: { bits: [2] }, c: { bits: [2], attributes: { src: 'rtl/top.v:4' } }, d: { bits: [2] },
+ e: {},
+ '$auto$only': { hide_name: 1, bits: [9] },
+ },
+ },
+ },
+ }, 'top')
+ assert.deepEqual(idx.bitNames['2'], ['a[0]', 'b', 'c']) // at most three names per bit
+ assert.equal(idx.nets[''].width, 0)
+ assert.deepEqual([idx.nets['2'].names, idx.nets['2'].src, idx.nets['2'].hidden], [['b', 'c', 'd'], '', 0])
+ assert.deepEqual([idx.nets['9'].names, idx.nets['9'].hidden], [['$auto$only'], 1]) // a net only yosys named
+ assert.equal(idx.ports.nobits.width, 0)
+ assert.equal(idx.src, 'rtl/top.v:1')
+})
+
+test('workspace edges: long logs, the top from other files, odd steps, PCF flags, ports', () => {
+ const ws = mkdtempSync(join(tmpdir(), 'yosys-ws-'))
+ try {
+ const put = (rel, text) => { mkdirSync(join(ws, rel, '..'), { recursive: true }); writeFileSync(join(ws, rel), text) }
+ put('big.log', 'head\nmiddle\ntail')
+ assert.equal(readText(join(ws, 'big.log'), 4), 'tail')
+ assert.equal(readText(join(ws, 'big.log'), 100), 'head\nmiddle\ntail')
+ mkdirSync(join(ws, 'out'))
+ assert.equal(fileInfo(ws, 'out'), null)
+
+ // no out/.top: a file the pane was opened on, then the newest report, then the testbench
+ assert.equal(findTop(ws, 'out/uart.svg'), 'uart')
+ assert.equal(findTop(ws, 'out/waves.json'), null)
+ put('tb/notes.txt', '')
+ assert.equal(findTop(ws, 'out/sim.json'), null)
+ put('tb/spi_tb.v', '')
+ assert.equal(findTop(ws, null), 'spi')
+ put('out/old.report.json', '{}')
+ put('out/new.report.json', '{}')
+ utimesSync(join(ws, 'out/old.report.json'), new Date(1000), new Date(1000))
+ assert.equal(findTop(ws, 'out/waves.json'), 'new')
+
+ // steps: a running step with an empty log, a failed one whose log says nothing, a run with no pid
+ const logs = join(ws, 'out', 'logs')
+ put('out/logs/run.json', JSON.stringify({ startedAt: 1 }))
+ put('out/logs/sim.start', 'soon\n')
+ put('out/logs/sim.log', '')
+ put('out/logs/synth.exit', '2\n')
+ put('out/logs/synth.log', '\n\n')
+ put('out/logs/pnr.exit', '1\n')
+ put('out/logs/pnr.log', 'placing\nstill placing\n')
+ put('out/logs/pack.time', 'not times\n')
+ let st = flowState(ws)
+ assert.deepEqual([st.abandoned, st.running], [false, true])
+ const [sim, , synth, , , pnr, pack] = st.steps
+ assert.deepEqual([sim.state, sim.last, sim.startedAt], ['running', '', undefined])
+ assert.deepEqual([synth.state, synth.exit, synth.last, synth.error], ['failed', 2, '', ''])
+ assert.deepEqual([pnr.last, pnr.error], ['still placing', 'still placing'])
+ assert.deepEqual([pack.state, pack.ms], ['pending', undefined])
+ assert.ok(logs)
+ // a pid that exists but is not ours (EPERM) is still alive
+ put('out/logs/run.json', JSON.stringify({ pid: 1, startedAt: 1 }))
+ st = flowState(ws)
+ assert.deepEqual([st.abandoned, st.running], [false, true])
+ assert.equal(errorLine(''), '')
+
+ const pcf = parsePcf('set_io -x -nowarn a 1\nset_io b 2 -pullup\nset_io lonely\n# set_frequency clk 99\n')
+ assert.deepEqual(pcf.ios, [
+ { port: 'a', pin: '1', line: 1, commented: false, x: true, nowarn: true },
+ { port: 'b', pin: '2', line: 2, commented: false, pullup: true },
+ ])
+ assert.deepEqual(pcf.frequencies, {})
+ assert.deepEqual(parsePcf(undefined), { ios: [], frequencies: {} })
+
+ // ports: a synthesised netlist that does not name the top, then a schematic that marks it
+ put('out/t.json', JSON.stringify({ modules: { other: { attributes: { top: 0 } } } }))
+ put('out/t_schematic.json', JSON.stringify({ modules: { renamed: { attributes: { top: '00000000000000000000000000000001' }, ports: { a: { direction: 'input' } } } } }))
+ assert.deepEqual(topPorts(ws, 't'), { from: 'out/t_schematic.json', ports: { a: { direction: 'input', width: 0 } } })
+ put('out/u.json', JSON.stringify({ modules: { u: {} } }))
+ assert.deepEqual(topPorts(ws, 'u'), { from: 'out/u.json', ports: {} })
+ assert.equal(topPorts(ws, 'nobody'), null)
+ put('rtl/README.md', '')
+ put('rtl/top.sv', '')
+ assert.deepEqual(sources(ws).map((f) => f.path), ['rtl/top.sv', 'tb/spi_tb.v'])
+ } finally {
+ rmSync(ws, { recursive: true, force: true })
+ }
+})