From 64e904f16be391913f0f375c39754106150c5c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Sun, 29 Dec 2024 16:21:40 +0100 Subject: [PATCH] Fix examples. --- assembly/index.ts | 4 ++-- bin/fuzz.js | 4 ++-- web/conway.html | 17 +++++++++++++-- web/index.html | 54 +++++++++++++++++++++++++++++++++++++---------- web/styles.css | 1 + 5 files changed, 63 insertions(+), 17 deletions(-) diff --git a/assembly/index.ts b/assembly/index.ts index 9b98509..949ba01 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -25,10 +25,10 @@ export function disassemble(input: u8[], kind: InputKind): string { return `Unknown kind: ${kind}`; } -export function runProgram(input: u8[], kind: InputKind): VmOutput { +export function runProgram(input: u8[], registers: u64[], kind: InputKind): VmOutput { if (kind === InputKind.Generic) { const vmInput = new VmInput(); - vmInput.registers[7] = 9; + vmInput.registers = registers; vmInput.gas = 10_000; vmInput.program = input; diff --git a/bin/fuzz.js b/bin/fuzz.js index 1dd6587..2846ca1 100755 --- a/bin/fuzz.js +++ b/bin/fuzz.js @@ -6,7 +6,7 @@ import { Pvm } from "@typeberry/pvm-debugger-adapter"; import { wrapAsProgram, runVm, disassemble, InputKind } from "../build/release.js"; export function fuzz(data) { - const gas = 20_000_000_000n; + const gas = 200n; const pc = 0; const pvm = new Pvm(); const program = wrapAsProgram(new Uint8Array(data)); @@ -80,7 +80,7 @@ function assert(tb, an, comment = '') { function writeTestCase(program, initial, expected) { const hex = programHex(program); - fs.writeFileSync(`../tests/${hex}.json`, JSON.stringify({ + fs.writeFileSync(`../tests/length_${hex.length}/${hex}.json`, JSON.stringify({ name: linkTo(hex), "initial-regs": initial.registers, "initial-pc": initial.pc, diff --git a/web/conway.html b/web/conway.html index e585110..6b22e5c 100644 --- a/web/conway.html +++ b/web/conway.html @@ -52,7 +52,7 @@ }); function pvmInit() { - const registers = Array(13 * 4).fill(0); + const registers = Array(13 * 8).fill(0); const pageMap = new Uint8Array( [...u32_le_bytes(0), ...u32_le_bytes(4096), 1] ); @@ -130,7 +130,20 @@ return out; } -const CODE = [0, 0, 129, 23, 62, 1, 3, 255, 0, 62, 1, 11, 255, 0, 62, 1, 19, 255, 0, 62, 1, 18, 255, 0, 62, 1, 9, 255, 0, 5, 233, 0, 4, 1, 255, 17, 2, 17, 1, 7, 17, 8, 223, 0, 4, 2, 255, 17, 2, 34, 1, 7, 18, 8, 241, 35, 19, 8, 8, 35, 3, 5, 47, 2, 51, 128, 0, 11, 52, 18, 68, 1, 15, 20, 1, 14, 44, 21, 2, 25, 50, 21, 3, 21, 5, 8, 7, 21, 3, 6, 5, 11, 2, 51, 128, 26, 3, 255, 0, 5, 205, 2, 51, 128, 26, 3, 5, 198, 4, 5, 82, 52, 4, 8, 64, 2, 68, 255, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 2, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 247, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 16, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 1, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 254, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 240, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 2, 68, 2, 73, 132, 7, 2, 119, 128, 0, 11, 118, 18, 102, 1, 8, 101, 5, 5, 60, 255, 4, 1, 17, 2, 19, 128, 0, 1, 18, 3, 50, 2, 17, 4, 7, 17, 64, 12, 255, 5, 240, 33, 132, 16, 146, 9, 153, 72, 138, 18, 17, 69, 137, 82, 149, 36, 74, 146, 40, 73, 162, 36, 137, 146, 36, 74, 146, 40, 73, 162, 36, 137, 146, 52, 42, 33]; +const CODE = [0, 0, 129, 23, 30, 1, 3, 255, 0, 30, 1, 11, 255, 0, 30, 1, 19, 255, 0, 30, 1, 18, +255, 0, 30, 1, 9, 255, 0, 40, 233, 0, 51, 1, 255, 1, 139, 17, 1, 81, 17, 8, 223, 0, 51, 2, 255, 1, +139, 34, 1, 81, 18, 8, 241, 140, 19, 8, 180, 35, 3, 40, 47, 139, 51, 128, 0, 114, 52, 122, 68, 1, +82, 20, 1, 14, 83, 21, 2, 25, 86, 21, 3, 21, 40, 8, 81, 21, 3, 6, 40, 11, 139, 51, 128, 70, 3, +255, 0, 40, 205, 139, 51, 128, 70, 3, 40, 198, 51, 5, 100, 52, 51, 8, 64, 139, 68, 255, 185, 132, +7, 139, 119, 128, 0, 114, 118, 122, 102, 1, 180, 101, 5, 139, 68, 2, 185, 132, 7, 139, 119, 128, +0, 114, 118, 122, 102, 1, 180, 101, 5, 139, 68, 247, 185, 132, 7, 139, 119, 128, 0, 114, 118, 122, +102, 1, 180, 101, 5, 139, 68, 16, 185, 132, 7, 139, 119, 128, 0, 114, 118, 122, 102, 1, 180, 101, +5, 139, 68, 1, 185, 132, 7, 139, 119, 128, 0, 114, 118, 122, 102, 1, 180, 101, 5, 139, 68, 254, +185, 132, 7, 139, 119, 128, 0, 114, 118, 122, 102, 1, 180, 101, 5, 139, 68, 240, 185, 132, 7, +139, 119, 128, 0, 114, 118, 122, 102, 1, 180, 101, 5, 139, 68, 2, 185, 132, 7, 139, 119, 128, 0, +114, 118, 122, 102, 1, 180, 101, 5, 40, 60, 255, 51, 1, 1, 139, 19, 128, 0, 118, 18, 112, 50, 139, +17, 4, 81, 17, 64, 12, 255, 40, 240, 33, 132, 16, 146, 9, 153, 72, 138, 18, 17, 69, 137, 82, 149, +36, 74, 146, 40, 73, 162, 36, 137, 146, 36, 74, 146, 40, 73, 162, 36, 137, 146, 52, 42, 33]; diff --git a/web/index.html b/web/index.html index 39bcf04..1ef6f2d 100644 --- a/web/index.html +++ b/web/index.html @@ -6,6 +6,7 @@ import { disassemble, runProgram, InputKind } from "./build/release.js"; const $upload = document.querySelector('#upload'); const $code = document.querySelector('#code'); +const $regs = document.querySelector('#registers'); const $file = document.querySelector('#file'); const $run = document.querySelector('#run'); const $dis = document.querySelector('#disassemble'); @@ -38,6 +39,19 @@ }); $run.click(); +function parseRegisters(input) { + const registers = eval(input); + if (!Array.isArray(registers)) { + throw new Error('Registers should be an array!'); + } + + if (registers.length !== 13) { + throw new Error(`Expected 13 elements, got: ${registers.length}`); + } + + return registers.map(x => BigInt(x)); +} + function parseCode(code) { if (code.startsWith('0x')) { if (code.length % 2 !== 0) { @@ -60,8 +74,13 @@ function execute(shouldRun = false) { $code.classList.remove('error'); - const markError = () => { - $code.classList.add('error'); + $regs.classList.remove('error'); + const markError = (isCode) => { + if (isCode) { + $code.classList.add('error'); + } else { + $regs.classList.add('error'); + } }; const isSpi = $spi.checked; const kind = isSpi ? InputKind.SPI : InputKind.Generic; @@ -71,16 +90,26 @@ } catch (e) { console.error(e); $out.innerHTML = `Not a valid JSON or HEX: ${e}`; - markError(); + markError(true); return; } + let registers; + try { + registers = parseRegisters($regs.value); + } catch (e) { + console.error(e); + $out.innerHTML = `Invalid registers: ${e}`; + markError(false); + return; + } + try { const asm = disassemble(program, kind); $out.innerHTML = asm; } catch (e) { console.error(e); $out.innerHTML = `Invalid program: ${e}`; - markError(); + markError(true); return; } @@ -89,8 +118,8 @@ } try { - const out = runProgram(program, kind); - $out.innerHTML += `\n\n Run OK! Status: ${out.status}, PC: ${out.pc}`; + const out = runProgram(program, registers, kind); + $out.innerHTML += `\n\nRun OK!\nStatus: ${out.status}\nPC: ${out.pc}\nGAS: ${out.gas}\nRegisters: ${out.registers}`; } catch (e) { console.error(e); $out.innerHTML += `\n\n Run error: ${e}`; @@ -113,11 +142,14 @@

🍍 Anan-AS

- + +
+ Initial Registers + +
diff --git a/web/styles.css b/web/styles.css index 25e0e4d..ef4cdcf 100644 --- a/web/styles.css +++ b/web/styles.css @@ -28,6 +28,7 @@ button { cursor: pointer; } textarea { + width: 100%; padding: 1rem; } textarea.error {