Skip to content

Commit

Permalink
Fix examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw committed Dec 29, 2024
1 parent e490eee commit 64e904f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
4 changes: 2 additions & 2 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions bin/fuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions web/conway.html
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
Expand Down Expand Up @@ -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];
</script>
</head>
<body>
Expand Down
54 changes: 43 additions & 11 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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}`;
Expand All @@ -113,11 +142,14 @@ <h1>🍍 Anan-AS</h1>
<label>
<input type="checkbox" id="spi"> JAM SPI
</label>
<textarea id="code" rows="10">
[
0, 0, 33, 4, 8, 1, 4, 9, 1, 5, 3, 0, 2, 119, 255, 7, 7, 12, 82, 138, 8, 152, 8, 82, 169, 5, 243, 82, 135, 4, 8, 4, 9, 17, 19, 0, 73, 147, 82, 213, 0
]
</textarea>
<textarea id="code" rows="10">[
0, 0, 33, 51, 8, 1, 51, 9, 1, 40, 3, 0, 121, 119, 255, 81, 7, 12, 100, 138, 170, 152, 8, 100,
169, 40, 243, 100, 135, 51, 8, 51, 9, 1, 50, 0, 73, 147, 82, 213, 0
]</textarea>
<details>
<summary>Initial Registers</summary>
<textarea id="registers" rows="3">[0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0]</textarea>
</details>
<div class="actions">
<button id="upload">📂 Upload PVM file</button>
<input id="file" type="file" placeholder="Upload a PVM file" />
Expand Down
1 change: 1 addition & 0 deletions web/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ button {
cursor: pointer;
}
textarea {
width: 100%;
padding: 1rem;
}
textarea.error {
Expand Down

0 comments on commit 64e904f

Please sign in to comment.