const $ = (id) => document.getElementById(id); const terminal = $('terminal'); const buttons = [...document.querySelectorAll('button')]; let traces = null, running = false, booted = false, runId = 0; const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); function lock(value) { running = value; buttons.forEach(button => { button.disabled = value || (button.id !== 'boot' && !booted); }); } function log(text, replace = false) { if (replace) terminal.textContent = ''; terminal.textContent += text; terminal.scrollTop = terminal.scrollHeight; } async function play(name, label) { if (running || !traces || !traces[name]) return; const id = ++runId, record = traces[name]; lock(true); $('screen-status').textContent = 'REPLAYING TRACE'; if (name === 'boot') log('', true); else log(`> ${label}\n`); const samples = []; for (let i = 0; i < record.trace.length; i++) { if (id !== runId) return; const frame = record.trace[i]; $('pc').textContent = String(frame.pc).padStart(3, '0'); $('acc').textContent = String(frame.acc).padStart(2, '0'); $('token').textContent = frame.token.toLocaleString('en-US'); $('writes').textContent = `${frame.writes} / 128`; $('step').textContent = `${i + 1} / ${record.trace.length}`; samples.push(frame.pc); const visible = samples.slice(-36); $('signal').setAttribute('d', visible.map((p, n) => `${n ? 'L' : 'M'}${(n / Math.max(visible.length - 1, 1) * 320).toFixed(1)} ${(42 - p / 255 * 36).toFixed(1)}`).join(' ')); if (i % 3 === 0) await sleep(24); } log(record.response + (record.response.endsWith('\n') ? '' : '\n')); if (name === 'boot') booted = true; $('screen-status').textContent = 'TRACE COMPLETE'; lock(false); } $('boot').addEventListener('click', () => play('boot', 'boot')); $('help').addEventListener('click', () => play('help', 'help')); $('version').addEventListener('click', () => play('version', 'version')); document.querySelectorAll('[data-trace]').forEach(button => button.addEventListener('click', () => play(button.dataset.trace, 'add ' + button.textContent.trim().replace('↗','')))); fetch('traces.json').then(response => { if (!response.ok) throw new Error('Trace file unavailable'); return response.json(); }).then(data => { traces = data; }).catch(() => { terminal.textContent = 'Trace data could not be loaded. Refresh the page to retry.'; $('boot').disabled = true; });