Skip to content

Commit

Permalink
Start VM Builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Jan 12, 2024
1 parent cd41fe1 commit a31a1bc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
21 changes: 21 additions & 0 deletions simulator/src/vm/builtins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { unwrap } from "@davidsouther/jiffies/lib/esm/result.js";
import { Vm } from "./vm.js";

describe("builtins", () => {
describe("Math", () => {
test("multiply", () => {
const vm = unwrap(
Vm.build([
{ op: "push", segment: "constant", offset: 7 },
{ op: "push", segment: "constant", offset: 8 },
{ op: "call", name: "Math.multiply", nArgs: 2 },
])
);

vm.step();
vm.step();
vm.step();
expect(vm.read([0, 256])).toEqual([257, 56]);
});
});
});
16 changes: 16 additions & 0 deletions simulator/src/vm/builtins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SCREEN_SIZE } from "../cpu/memory.js";
import { VmMemory } from "./memory.js";

export type VmBuiltin = (memory: VmMemory) => number;

const BLANK_SCREEN = new Array(SCREEN_SIZE).fill(0);
export const VM_BUILTINS: Record<string, VmBuiltin> = {
"Math.multiply": (memory) => {
const sp = memory.SP;
return (memory.get(sp - 2) * memory.get(sp - 1)) & 0xffff;
},
"Screen.clearScreen": (memory) => {
memory.screen.loadBytes(BLANK_SCREEN);
return 0;
},
};
33 changes: 20 additions & 13 deletions simulator/src/vm/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import { FunctionInstruction, VmInstruction } from "../languages/vm.js";
import { VmMemory } from "./memory.js";
import { MemoryAdapter, RAM } from "../cpu/memory.js";
import { VM_BUILTINS } from "./builtins.js";
import { op } from "../util/asm.js";

Check failure on line 12 in simulator/src/vm/vm.ts

View workflow job for this annotation

GitHub Actions / all

'op' is defined but never used

export type VmOperation =
| FunctionOperation
Expand Down Expand Up @@ -492,19 +494,24 @@ export class Vm {
}
case "call": {
const fnName = operation.name;
const fn = this.functionMap[fnName];
if (!fn) throw new Error(`Calling unknown function ${fnName}`);
const base = this.memory.pushFrame(
this.invocation.opPtr,
operation.nArgs,
fn.nVars
);
this.executionStack.push({
function: fnName,
opPtr: 0,
nArgs: operation.nArgs,
frameBase: base,
});
if (this.functionMap[fnName]) {
const base = this.memory.pushFrame(
this.invocation.opPtr,
operation.nArgs,
this.functionMap[fnName].nVars
);
this.executionStack.push({
function: fnName,
opPtr: 0,
nArgs: operation.nArgs,
frameBase: base,
});
} else if (VM_BUILTINS[fnName]) {
const ret = VM_BUILTINS[fnName](this.memory);
const sp = this.memory.SP - operation.nArgs;
this.memory.set(sp, ret);
this.memory.SP = sp + 1;
}
break;
}
case "return": {
Expand Down

0 comments on commit a31a1bc

Please sign in to comment.