Skip to content

Commit

Permalink
Update to new APIs. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw authored Dec 6, 2024
1 parent f649402 commit 97680e4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
32 changes: 30 additions & 2 deletions assembly/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ export function resetGenericWithMemory(
interpreter = int;
}

export function nextStep(steps: number = 1): boolean {
export function nextStep(): boolean {
if (interpreter !== null) {
const int = <Interpreter>interpreter;
return int.nextStep();
}
return false;
}

export function run(steps: u32): boolean {
if (interpreter !== null) {
const int = <Interpreter>interpreter;
let isOk = true;
for (let i = 0; i < steps; i++) {
for (let i: u32 = 0; i < steps; i++) {
isOk = int.nextStep();
if (!isOk) {
return false;
Expand Down Expand Up @@ -119,6 +127,14 @@ export function getRegisters(): Uint8Array {
return flat;
}

export function setRegisters(flatRegisters: u8[]): void {
if (interpreter === null) {
return;
}
const int = <Interpreter>interpreter;
fillRegisters(int.registers, flatRegisters);
}

export function getPageDump(index: u32): Uint8Array {
if (interpreter === null) {
return new Uint8Array(PAGE_SIZE).fill(0);
Expand All @@ -132,6 +148,18 @@ export function getPageDump(index: u32): Uint8Array {
return page;
}

export function setMemory(address: u32, data: Uint8Array): void {
if (interpreter === null) {
return;
}
const int = <Interpreter>interpreter;
const end = address + data.length;
for (let i = address; i < end; i++) {
// TODO [ToDr] handle page fault?
int.memory.setU8(i, data[i]);
}
}

function fillRegisters(registers: Registers, flat: u8[]): void {
const len = registers.length * REG_SIZE_BYTES;
if (len !== flat.length) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function disassemble(input: u8[], kind: InputKind): string {
return `Unknown kind: ${kind}`;
}

export function run(input: u8[], kind: InputKind): void {
export function runProgram(input: u8[], kind: InputKind): void {
if (kind === InputKind.Generic) {
const vmInput = new VmInput();
vmInput.registers[7] = 9;
Expand Down
4 changes: 2 additions & 2 deletions web/conway.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
</style>
<script type="module" defer>
import { resetGenericWithMemory, getPageDump, nextStep } from "./build/release.js";
import { resetGenericWithMemory, getPageDump, run as runSteps } from "./build/release.js";
const $out = document.querySelector('#output');
const $run = document.querySelector('#run');
const $stop = document.querySelector('#stop');
Expand Down Expand Up @@ -110,7 +110,7 @@
}
steps.count += stepsAtOnce;
steps.lastTime = performance.now();
isRunning = nextStep(stepsAtOnce);
isRunning = runSteps(stepsAtOnce);
if (!isRunning) {
$stop.click();
}
Expand Down
4 changes: 2 additions & 2 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<link type="text/css" rel="stylesheet" href="./styles.css" />
<script type="module" defer>
import { disassemble, run, InputKind } from "./build/release.js";
import { disassemble, runProgram, InputKind } from "./build/release.js";
const $upload = document.querySelector('#upload');
const $code = document.querySelector('#code');
const $file = document.querySelector('#file');
Expand Down Expand Up @@ -89,7 +89,7 @@
}

try {
run(program, kind);
runProgram(program, kind);
$out.innerHTML += `\n\n Run OK!`;
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 97680e4

Please sign in to comment.