-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd41fe1
commit a31a1bc
Showing
3 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters