Compare commits
33 Commits
3a103c46d1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
aa9c18346d
|
|||
|
a2f06c977e
|
|||
|
58e2cf3e1f
|
|||
|
efdae93d90
|
|||
|
804626ddad
|
|||
|
cbe5733fcd
|
|||
|
7961c68639
|
|||
|
d202157a58
|
|||
|
97fc43bf93
|
|||
|
2c13ad4e1f
|
|||
|
36429bf8bc
|
|||
|
c20e7e181b
|
|||
|
fe5c55cabf
|
|||
|
67fc1d8d7b
|
|||
|
4000522b3a
|
|||
|
19ef69958d
|
|||
|
0a52388030
|
|||
|
6e8439eeaf
|
|||
|
eaa3242cc0
|
|||
|
f77adffbef
|
|||
|
c91f46be88
|
|||
|
6ee4adfea5
|
|||
|
5dc0a7a601
|
|||
|
896a1ca563
|
|||
|
37d56988ef
|
|||
|
6c643f8402
|
|||
|
7828b0f112
|
|||
|
e7affbf8b7
|
|||
|
02ee4c3c88
|
|||
|
c21b3c79c7
|
|||
|
1318c3cc4e
|
|||
|
74a8f21379
|
|||
|
6784cd02b4
|
84
README.md
84
README.md
@@ -1,31 +1,24 @@
|
|||||||
# Wipforth
|
# Wipforth
|
||||||
|
|
||||||
Wipforth is a simple Forth implementation that runs in the WebAssembly
|
Wipforth is a Forth implementation that runs in the WebAssembly
|
||||||
virtual machine. It does I/O via memory-mapped peripherals, which are
|
virtual machine. The system is bootstrapped from source on page load:
|
||||||
emulated in JavaScript.
|
the only non-text file is the favicon :)
|
||||||
|
|
||||||
- For the Forth kernel, see [wipforth.wat](./wipforth.wat)
|
I/O is done via memory-mapped peripherals, which are emulated in
|
||||||
- For the JavaScript emulator, see [emu.js](./emu.js)
|
JavaScript.
|
||||||
- For the Forth prelude, which is loaded at start-up, see
|
|
||||||
[prelude.f](./prelude.f)
|
- For the Forth kernel, see [wipforth.ws](./wipforth.ws)
|
||||||
|
- For the emulator, see [emu.js](./emu.js)
|
||||||
|
- For the assembler, see [asm.js](./asm.js)
|
||||||
|
- For the prelude (Forth code loaded right after the kernel boots),
|
||||||
|
see [prelude.f](./prelude.f)
|
||||||
- For a description of the peripherals, see the
|
- For a description of the peripherals, see the
|
||||||
[Peripherals](#peripherals) section below.
|
[Peripherals](#peripherals) section below.
|
||||||
|
|
||||||
## Building and Running Locally
|
## Building and Running Locally
|
||||||
|
|
||||||
You'll need:
|
There's a [Guile](https://www.gnu.org/software/guile/) script in the
|
||||||
|
repo you can use for this:
|
||||||
- [WABT](https://github.com/WebAssembly/wabt) (not for long mwahaha)
|
|
||||||
- [Guile](https://www.gnu.org/software/guile/) (or bring your own HTTP
|
|
||||||
server -- see note below)
|
|
||||||
|
|
||||||
To run, first compile the WebAssembly module:
|
|
||||||
|
|
||||||
```
|
|
||||||
wat2wasm --enable-threads wipforth.wat
|
|
||||||
```
|
|
||||||
|
|
||||||
Then run the development server:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
guile server.scm
|
guile server.scm
|
||||||
@@ -34,14 +27,20 @@ guile server.scm
|
|||||||
You should then be able to open <http://localhost:8080> in a browser
|
You should then be able to open <http://localhost:8080> in a browser
|
||||||
and use the system from there.
|
and use the system from there.
|
||||||
|
|
||||||
**NOTE**: The server is very simple and just serves the files with the
|
However, since everything is bootstrapped on the client, basically any
|
||||||
cross-origin isolation headers required for `SharedMemoryBuffer` use.
|
HTTP server will do as long as it sets the appropriate response
|
||||||
You could use any HTTP server that sets these headers.
|
headers for `SharedArrayBuffer` use:
|
||||||
|
|
||||||
You should **definitely not** use the development server to serve the
|
- `Cross-Origin-Opener-Policy: same-origin`
|
||||||
application on the open internet; I just hacked it together for
|
- `Cross-Origin-Embedder-Policy: require-corp`
|
||||||
testing on localhost during development and it's probably hilariously
|
|
||||||
insecure.
|
So, if you don't have Guile on your system you can use something else
|
||||||
|
like Python's `http.server`.
|
||||||
|
|
||||||
|
**NOTE**: You should **definitely not** use `server.scm` to serve the
|
||||||
|
application on the open internet or anything like that; I just hacked
|
||||||
|
it together for testing on localhost during development and it's
|
||||||
|
probably hilariously insecure.
|
||||||
|
|
||||||
## End-to-End Tests
|
## End-to-End Tests
|
||||||
|
|
||||||
@@ -62,36 +61,34 @@ Given that's all sorted, you should be able to run:
|
|||||||
guile tests.scm
|
guile tests.scm
|
||||||
```
|
```
|
||||||
|
|
||||||
It will print a JUnit XML report to standard out, you can pretty-print
|
It will print a JUnit XML report to standard out. You can
|
||||||
it with:
|
pretty-print it with `xmllint` if you have it installed:
|
||||||
|
|
||||||
```
|
```
|
||||||
guile tests.scm | xmllint --format -
|
guile tests.scm | xmllint --format -
|
||||||
```
|
```
|
||||||
|
|
||||||
Though, of course, this will require that you have `xmllint` on your
|
|
||||||
system.
|
|
||||||
|
|
||||||
## Peripherals
|
## Peripherals
|
||||||
|
|
||||||
### Terminal
|
### Terminal
|
||||||
|
|
||||||
| Name | Address | Size / B | Access |
|
| Name | Address | Size / B | Access |
|
||||||
|--------|---------|----------|--------------|
|
|--------|---------|----------|--------------|
|
||||||
| TXBUF | 00h | 32 | write |
|
| TXBUF | 000h | 32 | write |
|
||||||
| RXBUF | 20h | 32 | read |
|
| RXBUF | 080h | 32 | read |
|
||||||
| TXHEAD | 40h | 4 | atomic read |
|
| TXHEAD | 100h | 4 | atomic read |
|
||||||
| TXTAIL | 44h | 4 | atomic write |
|
| TXTAIL | 104h | 4 | atomic write |
|
||||||
| RXHEAD | 48h | 4 | atomic write |
|
| RXHEAD | 108h | 4 | atomic write |
|
||||||
| RXTAIL | 4Ch | 4 | atomic read |
|
| RXTAIL | 10Ch | 4 | atomic read |
|
||||||
|
|
||||||
For both sending (`TX`) and receiving (`RX`), there are three
|
For both sending (`TX`) and receiving (`RX`), there are three
|
||||||
registers: `xBUF`, `xHEAD` and `xTAIL`:
|
registers: `xBUF`, `xHEAD` and `xTAIL`:
|
||||||
|
|
||||||
- `xBUF` registers are 32-byte FIFO ring buffers used for data
|
- `xBUF` registers are 128-byte FIFO ring buffers used for data
|
||||||
- The `xHEAD` and `xTAIL` registers specify the start and end of data
|
- The `xHEAD` and `xTAIL` registers specify the start and end of data
|
||||||
in the ring buffer, `xHEAD` being the offset of the first byte of
|
in the ring buffer, `xHEAD` being the offset of the first byte of
|
||||||
data, and `xTAIL` being the offset of the first byte *after* the data.
|
data, and `xTAIL` being the offset of the first byte *after* the
|
||||||
|
data.
|
||||||
|
|
||||||
In order to be distinguishable from the empty state, the ring buffers
|
In order to be distinguishable from the empty state, the ring buffers
|
||||||
must never be completely full -- there must always be *at least one*
|
must never be completely full -- there must always be *at least one*
|
||||||
@@ -101,7 +98,10 @@ unoccupied byte between the tail and the head.
|
|||||||
|
|
||||||
| Name | Address | Size / B | Access |
|
| Name | Address | Size / B | Access |
|
||||||
|----------|---------|----------|--------------|
|
|----------|---------|----------|--------------|
|
||||||
| SYSREADY | 50h | 1 | atomic write |
|
| SYSREADY | 110h | 4 | atomic write |
|
||||||
|
| SYSINTER | 114h | 4 | atomic read |
|
||||||
|
|
||||||
The `SYSREADY` register is used to indicate when the system has booted
|
The `SYSREADY` register is used to indicate when the system has booted
|
||||||
up and is ready for user input.
|
up and is ready for user input. `SYSINTER` is set (and notified on)
|
||||||
|
once the emulator has enabled user input and the system is
|
||||||
|
interactive.
|
||||||
|
|||||||
163
asm.js
163
asm.js
@@ -14,20 +14,29 @@ class Tokenizer {
|
|||||||
this.buffer = [];
|
this.buffer = [];
|
||||||
this.comment = false;
|
this.comment = false;
|
||||||
this.string = false;
|
this.string = false;
|
||||||
|
this.cursor = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
find(pred) {
|
||||||
|
for (let i = this.cursor; i < this.buffer.length; ++i) {
|
||||||
|
if (pred(this.buffer[i]))
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
skip() {
|
skip() {
|
||||||
const idx = this.buffer.findIndex((cp) => !this.skips.has(cp));
|
const idx = this.find((cp) => !this.skips.has(cp));
|
||||||
this.buffer = idx == -1 ? [] : this.buffer.slice(idx);
|
this.cursor = idx == null ? this.buffer.length : idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
next_string() {
|
next_string() {
|
||||||
const idx = this.buffer.findIndex((cp) => cp == this.string_quote);
|
const idx = this.find((cp) => cp == this.string_quote);
|
||||||
if (idx == -1) {
|
if (idx == null) {
|
||||||
this.string = true;
|
this.string = true;
|
||||||
} else {
|
} else {
|
||||||
const string = this.buffer.slice(0, idx).join("");
|
const string = this.buffer.slice(this.cursor, idx).join("");
|
||||||
this.buffer = this.buffer.slice(idx + 1);
|
this.cursor = idx + 1;
|
||||||
this.string = false;
|
this.string = false;
|
||||||
return { string: string };
|
return { string: string };
|
||||||
}
|
}
|
||||||
@@ -38,18 +47,20 @@ class Tokenizer {
|
|||||||
return this.next_string();
|
return this.next_string();
|
||||||
|
|
||||||
this.skip();
|
this.skip();
|
||||||
if (this.buffer[0] == LINE_END)
|
if (this.buffer[this.cursor] == LINE_END) {
|
||||||
return this.buffer.shift();
|
++this.cursor;
|
||||||
|
return LINE_END;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.buffer[0] == this.string_quote) {
|
if (this.buffer[this.cursor] == this.string_quote) {
|
||||||
this.buffer.shift();
|
++this.cursor;
|
||||||
return this.next_string();
|
return this.next_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
const idx = this.buffer.findIndex((cp) => this.delims.has(cp));
|
const idx = this.find((cp) => this.delims.has(cp));
|
||||||
if (idx != -1) {
|
if (idx != null) {
|
||||||
const token = this.buffer.slice(0, idx).join("");
|
const token = this.buffer.slice(this.cursor, idx).join("");
|
||||||
this.buffer = this.buffer.slice(idx);
|
this.cursor = idx;
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,12 +70,14 @@ class Tokenizer {
|
|||||||
let token;
|
let token;
|
||||||
while (token = this.next()) {
|
while (token = this.next()) {
|
||||||
if (token.string == undefined
|
if (token.string == undefined
|
||||||
&& token.startsWith(this.comment_start))
|
&& token.startsWith(this.comment_start)) {
|
||||||
this.comment = true;
|
this.comment = true;
|
||||||
else if (this.comment && token == LINE_END)
|
} else if (this.comment && token == LINE_END) {
|
||||||
this.comment = false;
|
this.comment = false;
|
||||||
else if (!this.comment)
|
yield token;
|
||||||
|
} else if (!this.comment) {
|
||||||
yield token;
|
yield token;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,6 +119,7 @@ const State = Object.freeze({
|
|||||||
ELEM_TABLE: 33,
|
ELEM_TABLE: 33,
|
||||||
ELEM_ELEM: 34,
|
ELEM_ELEM: 34,
|
||||||
ELEM_LABEL: 35,
|
ELEM_LABEL: 35,
|
||||||
|
ZERO: 36,
|
||||||
});
|
});
|
||||||
|
|
||||||
const Action = Object.freeze({
|
const Action = Object.freeze({
|
||||||
@@ -161,6 +175,7 @@ const opcodes = {
|
|||||||
"i32.store": 0x36,
|
"i32.store": 0x36,
|
||||||
"i32.store8": 0x3a,
|
"i32.store8": 0x3a,
|
||||||
"i32.const": 0x41,
|
"i32.const": 0x41,
|
||||||
|
"i64.const": 0x42,
|
||||||
"i32.eqz": 0x45,
|
"i32.eqz": 0x45,
|
||||||
"i32.eq": 0x46,
|
"i32.eq": 0x46,
|
||||||
"i32.ne": 0x47,
|
"i32.ne": 0x47,
|
||||||
@@ -175,6 +190,8 @@ const opcodes = {
|
|||||||
"i32.add": 0x6a,
|
"i32.add": 0x6a,
|
||||||
"i32.sub": 0x6b,
|
"i32.sub": 0x6b,
|
||||||
"i32.mul": 0x6c,
|
"i32.mul": 0x6c,
|
||||||
|
"i32.div_s": 0x6d,
|
||||||
|
"i32.rem_s": 0x6f,
|
||||||
"i32.and": 0x71,
|
"i32.and": 0x71,
|
||||||
"i32.or": 0x72,
|
"i32.or": 0x72,
|
||||||
"i32.xor": 0x73,
|
"i32.xor": 0x73,
|
||||||
@@ -183,12 +200,12 @@ const opcodes = {
|
|||||||
"i32.shr_u": 0x76,
|
"i32.shr_u": 0x76,
|
||||||
|
|
||||||
// Threads instructions
|
// Threads instructions
|
||||||
"memory.atomic.notify": [ 0xfe, 0x00 ],
|
"memory.atomic.notify": [ 0xfe, 0x00 ],
|
||||||
"memory.atomic.wait32": [ 0xfe, 0x01 ],
|
"memory.atomic.wait32": [ 0xfe, 0x01 ],
|
||||||
"memory.atomic.load": [ 0xfe, 0x10 ],
|
"i32.atomic.load": [ 0xfe, 0x10 ],
|
||||||
"memory.atomic.load8_u": [ 0xfe, 0x12 ],
|
"i32.atomic.load8_u": [ 0xfe, 0x12 ],
|
||||||
"memory.atomic.store": [ 0xfe, 0x17 ],
|
"i32.atomic.store": [ 0xfe, 0x17 ],
|
||||||
"memory.atomic.store8": [ 0xfe, 0x19 ],
|
"i32.atomic.store8": [ 0xfe, 0x19 ],
|
||||||
};
|
};
|
||||||
|
|
||||||
const mem_flags = {
|
const mem_flags = {
|
||||||
@@ -225,6 +242,7 @@ class Parser {
|
|||||||
".type": State.TYPE_NAME,
|
".type": State.TYPE_NAME,
|
||||||
".table": State.TABLE_NAME,
|
".table": State.TABLE_NAME,
|
||||||
".elem": State.ELEM_TABLE,
|
".elem": State.ELEM_TABLE,
|
||||||
|
".zero": State.ZERO,
|
||||||
};
|
};
|
||||||
this.blocks = new Set(["block", "loop", "if"]);
|
this.blocks = new Set(["block", "loop", "if"]);
|
||||||
this.handlers = {
|
this.handlers = {
|
||||||
@@ -264,6 +282,7 @@ class Parser {
|
|||||||
[State.ELEM_TABLE]: (token) => this.token_elem_table(token),
|
[State.ELEM_TABLE]: (token) => this.token_elem_table(token),
|
||||||
[State.ELEM_ELEM]: (token) => this.token_elem_elem(token),
|
[State.ELEM_ELEM]: (token) => this.token_elem_elem(token),
|
||||||
[State.ELEM_LABEL]: (token) => this.token_elem_label(token),
|
[State.ELEM_LABEL]: (token) => this.token_elem_label(token),
|
||||||
|
[State.ZERO]: (token) => this.token_zero(token),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.results = [];
|
this.results = [];
|
||||||
@@ -272,15 +291,14 @@ class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
integer(token) {
|
integer(token) {
|
||||||
let base;
|
let base, regex;
|
||||||
switch (token.slice(-1)) {
|
switch (token.slice(-1)) {
|
||||||
case "b": base = 2; break;
|
case "b": base = 2; regex = /^-?[01]+b$/; break;
|
||||||
case "o": base = 8; break;
|
case "o": base = 8; regex = /^-?[0-7]+o$/; break;
|
||||||
case "h": base = 16; break;
|
case "h": base = 16; regex = /^-?[0-9A-F]+h$/; break;
|
||||||
default: base = 10; break;
|
default: base = 10; regex = /^-?[0-9]+d?$/; break;
|
||||||
}
|
}
|
||||||
const x = parseInt(token, base);
|
return regex.test(token) ? parseInt(token, base) : null;
|
||||||
return Number.isNaN(x) ? null : x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
translate_code(token) {
|
translate_code(token) {
|
||||||
@@ -547,15 +565,10 @@ class Parser {
|
|||||||
|
|
||||||
token_at_addr(token) {
|
token_at_addr(token) {
|
||||||
const value = this.integer(token);
|
const value = this.integer(token);
|
||||||
if (value == null) {
|
if (value != null)
|
||||||
console.error(
|
this.at.addr = value;
|
||||||
`ERROR: Unexpected token ${token} in .mem: `
|
else
|
||||||
+ "expected address");
|
this.at.addr_symbol = token;
|
||||||
this.at = undefined;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.at.addr = value;
|
|
||||||
const action = { type: Action.AT, at: this.at };
|
const action = { type: Action.AT, at: this.at };
|
||||||
this.at = undefined;
|
this.at = undefined;
|
||||||
this.state = State.TOP;
|
this.state = State.TOP;
|
||||||
@@ -823,6 +836,26 @@ class Parser {
|
|||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token_zero(token) {
|
||||||
|
if (token == LINE_END) {
|
||||||
|
console.error(
|
||||||
|
"ERROR: Unexpected newline in .zero, expected count")
|
||||||
|
this.state = State.TOP;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const count = this.integer(token);
|
||||||
|
if (count == null) {
|
||||||
|
console.error(
|
||||||
|
`ERROR: Unexpected token ${token} in .zero, expected count`);
|
||||||
|
this.state = State.TOP;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.state = State.TOP;
|
||||||
|
return { type: Action.DATA, size: count, value: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
mem_action() {
|
mem_action() {
|
||||||
const action = {
|
const action = {
|
||||||
type: Action.MEM,
|
type: Action.MEM,
|
||||||
@@ -909,6 +942,7 @@ export class Assembler {
|
|||||||
this.types = [];
|
this.types = [];
|
||||||
this.type_bindings = {};
|
this.type_bindings = {};
|
||||||
this.tables = {};
|
this.tables = {};
|
||||||
|
this.unresolved = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
action_append(action) {
|
action_append(action) {
|
||||||
@@ -963,7 +997,7 @@ export class Assembler {
|
|||||||
}
|
}
|
||||||
func.body.push(...this.leb128(def_value));
|
func.body.push(...this.leb128(def_value));
|
||||||
} else {
|
} else {
|
||||||
func.body.push(...this.leb128(value));
|
func.body.push(...this.uleb128(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -995,7 +1029,8 @@ export class Assembler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.pos.mem = mem;
|
this.pos.mem = mem;
|
||||||
this.pos.addr = action.at.addr;
|
this.pos.addr = action.at.addr
|
||||||
|
?? this.lookup_def(action.at.addr_symbol);
|
||||||
this.data.push({ loc: { ...this.pos }, data: [] })
|
this.data.push({ loc: { ...this.pos }, data: [] })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1013,9 +1048,14 @@ export class Assembler {
|
|||||||
}
|
}
|
||||||
value = this.lookup_def(action.symbol);
|
value = this.lookup_def(action.symbol);
|
||||||
if (value == undefined) {
|
if (value == undefined) {
|
||||||
console.error(
|
this.unresolved.push({
|
||||||
`ERROR: Unable to resolve symbol ${action.symbol}`);
|
type: "data",
|
||||||
return;
|
size: action.size,
|
||||||
|
symbol: action.symbol,
|
||||||
|
target: data,
|
||||||
|
offset: data.length,
|
||||||
|
});
|
||||||
|
value = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bytes = this.le(value, action.size);
|
bytes = this.le(value, action.size);
|
||||||
@@ -1303,7 +1343,7 @@ export class Assembler {
|
|||||||
opcodes["i32.const"],
|
opcodes["i32.const"],
|
||||||
...this.leb128(loc.addr),
|
...this.leb128(loc.addr),
|
||||||
opcodes["end"],
|
opcodes["end"],
|
||||||
data.length,
|
...this.uleb128(data.length),
|
||||||
...data,
|
...data,
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
@@ -1311,6 +1351,7 @@ export class Assembler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wasm() {
|
wasm() {
|
||||||
|
this.resolve_refs();
|
||||||
this.resolve_func_types();
|
this.resolve_func_types();
|
||||||
|
|
||||||
const template = [
|
const template = [
|
||||||
@@ -1327,7 +1368,9 @@ export class Assembler {
|
|||||||
];
|
];
|
||||||
const sections = template.map(([ code, generator ]) => {
|
const sections = template.map(([ code, generator ]) => {
|
||||||
const body = generator();
|
const body = generator();
|
||||||
return body == null ? [] : [ code, body.length, body ];
|
if (body == null)
|
||||||
|
return [];
|
||||||
|
return [ code, this.uleb128(body.length), body ];
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Uint8Array([ HEADER, sections ].flat(Infinity));
|
return new Uint8Array([ HEADER, sections ].flat(Infinity));
|
||||||
@@ -1377,4 +1420,32 @@ export class Assembler {
|
|||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolve_refs() {
|
||||||
|
const failed = [];
|
||||||
|
for (const ref of this.unresolved) {
|
||||||
|
if (ref.type != "data") {
|
||||||
|
console.error(
|
||||||
|
`ERROR: Unsupported ref type ${ref.type} for `
|
||||||
|
+ `symbol ${ref.symbol}`
|
||||||
|
);
|
||||||
|
failed.push(ref.symbol);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = this.defs[ref.symbol];
|
||||||
|
if (value == undefined) {
|
||||||
|
failed.push(ref.symbol);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bytes = this.le(value, ref.size);
|
||||||
|
ref.target.splice(ref.offset, ref.size, ...bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failed.length != 0) {
|
||||||
|
const failed_str = failed.join(" ");
|
||||||
|
console.error(`ERROR: Unable to resolve refs: ${failed_str}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29
boot.js
29
boot.js
@@ -1,7 +1,26 @@
|
|||||||
|
import { Assembler } from './asm.js';
|
||||||
|
|
||||||
|
const assemble = (async () => {
|
||||||
|
const asm = new Assembler();
|
||||||
|
const resp = await fetch('wipforth.ws');
|
||||||
|
for await (const chunk of resp.body) {
|
||||||
|
asm.push(chunk);
|
||||||
|
}
|
||||||
|
return asm.wasm();
|
||||||
|
})();
|
||||||
|
|
||||||
self.onmessage = async (e) => {
|
self.onmessage = async (e) => {
|
||||||
const exports = { emu: { mem: e.data } };
|
switch (e.data.type) {
|
||||||
const mod = await WebAssembly.instantiateStreaming(
|
case "load":
|
||||||
fetch('wipforth.wasm'), exports)
|
const exports = { emu: { mem: e.data.mem } };
|
||||||
mod.instance.exports.reset();
|
const wasm = await assemble;
|
||||||
console.log('System halt');
|
self.mod = await WebAssembly.instantiate(wasm, exports);
|
||||||
|
await self.postMessage('ready');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "boot":
|
||||||
|
self.mod.instance.exports.reset();
|
||||||
|
console.log('System halt');
|
||||||
|
break;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
asm.js
|
||||||
boot.js
|
boot.js
|
||||||
emu.js
|
emu.js
|
||||||
|
favicon.png
|
||||||
index.html
|
index.html
|
||||||
prelude.f
|
prelude.f
|
||||||
styles.css
|
styles.css
|
||||||
wipforth.wasm
|
wipforth.ws
|
||||||
|
|||||||
72
emu.js
72
emu.js
@@ -1,16 +1,17 @@
|
|||||||
const TXBUF = 0x00;
|
const TXBUF = 0x000;
|
||||||
const RXBUF = 0x20;
|
const RXBUF = 0x080;
|
||||||
const TXHEAD = 0x40;
|
const TXHEAD = 0x100;
|
||||||
const TXTAIL = 0x44;
|
const TXTAIL = 0x104;
|
||||||
const RXHEAD = 0x48;
|
const RXHEAD = 0x108;
|
||||||
const RXTAIL = 0x4c;
|
const RXTAIL = 0x10c;
|
||||||
const SYSREADY = 0x50;
|
|
||||||
|
|
||||||
const TXBUF_SIZE = 32;
|
const SYSREADY = 0x110;
|
||||||
const RXBUF_SIZE = 32;
|
const SYSINTER = 0x114;
|
||||||
const PERIPHS_SIZE = 81;
|
|
||||||
|
|
||||||
const POLL_INTERVAL_MS = 20;
|
const PERIPHS_SIZE = 0x200;
|
||||||
|
|
||||||
|
const POLL_INTERVAL_MS = 5;
|
||||||
|
const DOT_INTERVAL_MS = 25;
|
||||||
|
|
||||||
const COLS = 80;
|
const COLS = 80;
|
||||||
const TAB_WIDTH = 8;
|
const TAB_WIDTH = 8;
|
||||||
@@ -50,8 +51,19 @@ class Emulator {
|
|||||||
document.addEventListener('keydown', (e) => this.handle_keydown(e));
|
document.addEventListener('keydown', (e) => this.handle_keydown(e));
|
||||||
window.addEventListener('resize', () => this.handle_resize());
|
window.addEventListener('resize', () => this.handle_resize());
|
||||||
|
|
||||||
this.worker = new Worker('boot.js');
|
this.forth = new Worker('boot.js', { type: 'module' });
|
||||||
this.worker.postMessage(this.mem);
|
|
||||||
|
this.print("Assembling kernel ");
|
||||||
|
this.dots = setInterval(() => this.print("."), DOT_INTERVAL_MS);
|
||||||
|
this.forth.postMessage({ type: "load", mem: this.mem });
|
||||||
|
this.forth.onmessage = (e) => {
|
||||||
|
clearInterval(this.dots);
|
||||||
|
this.print(" done\n");
|
||||||
|
|
||||||
|
this.print("Loading prelude ");
|
||||||
|
this.forth.postMessage({ type: "boot" });
|
||||||
|
this.dots = setInterval(() => this.print("."), DOT_INTERVAL_MS);
|
||||||
|
};
|
||||||
|
|
||||||
fetch('prelude.f')
|
fetch('prelude.f')
|
||||||
.then(res => res.text())
|
.then(res => res.text())
|
||||||
@@ -77,15 +89,21 @@ class Emulator {
|
|||||||
if (!this.input_enable) {
|
if (!this.input_enable) {
|
||||||
const sysready = Atomics.load(this.mem_u8, SYSREADY);
|
const sysready = Atomics.load(this.mem_u8, SYSREADY);
|
||||||
if (sysready != 0) {
|
if (sysready != 0) {
|
||||||
|
clearInterval(this.dots);
|
||||||
|
this.print(" done\n");
|
||||||
|
|
||||||
|
Atomics.store(this.mem_u8, SYSINTER, 1);
|
||||||
|
Atomics.notify(this.mem_i32, SYSINTER / 4);
|
||||||
|
|
||||||
this.input_enable = true;
|
this.input_enable = true;
|
||||||
|
this.blink = true;
|
||||||
this.flush_output();
|
this.flush_output();
|
||||||
document.getElementById('cursor').classList.add('blinking');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fifo_next(idx) {
|
fifo_next(idx) {
|
||||||
return (idx + 1) & 0x1f;
|
return (idx + 1) & 0x7f;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_tx_data(head, tail) {
|
handle_tx_data(head, tail) {
|
||||||
@@ -173,12 +191,14 @@ class Emulator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
this.blink = false;
|
||||||
this.flush_output();
|
this.flush_output();
|
||||||
|
|
||||||
document.getElementById('cursor').classList.remove('blinking');
|
document.getElementById('cursor').classList.remove('blinking');
|
||||||
if (this.idle_timer)
|
if (this.idle_timer)
|
||||||
clearTimeout(this.idle_timer);
|
clearTimeout(this.idle_timer);
|
||||||
this.idle_timer = setTimeout(() => {
|
this.idle_timer = setTimeout(() => {
|
||||||
|
this.blink = true;
|
||||||
document.getElementById('cursor').classList.add('blinking');
|
document.getElementById('cursor').classList.add('blinking');
|
||||||
}, CURSOR_IDLE_TIME_MS);
|
}, CURSOR_IDLE_TIME_MS);
|
||||||
}
|
}
|
||||||
@@ -265,10 +285,12 @@ class Emulator {
|
|||||||
return row.map((c, x) => {
|
return row.map((c, x) => {
|
||||||
const ec = this.html_escape(c);
|
const ec = this.html_escape(c);
|
||||||
if (this.input_enable
|
if (this.input_enable
|
||||||
&& x == this.cursor.x && y == this.cursor.y)
|
&& x == this.cursor.x && y == this.cursor.y) {
|
||||||
return '<span id="cursor">' + ec + '</span>';
|
const cl = this.blink ? 'class="blinking"' : '';
|
||||||
else
|
return `<span id="cursor" ${cl}>` + ec + '</span>';
|
||||||
|
} else {
|
||||||
return ec;
|
return ec;
|
||||||
|
}
|
||||||
}).join('').trimEnd();
|
}).join('').trimEnd();
|
||||||
}).join('\n');
|
}).join('\n');
|
||||||
this.output.innerHTML = html;
|
this.output.innerHTML = html;
|
||||||
@@ -325,6 +347,16 @@ class Emulator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener('DOMContentLoaded', () => {
|
window.addEventListener('DOMContentLoaded', () => {
|
||||||
document.getElementById('output').innerText = '';
|
const output = document.getElementById('output');
|
||||||
window.emu = new Emulator();
|
output.innerText = '';
|
||||||
|
|
||||||
|
if (!self.crossOriginIsolated) {
|
||||||
|
output.innerText = "Yeah so there's this thing where Chromium ends "
|
||||||
|
+ "up ignoring COOP/COEP\nheaders after a hard reload "
|
||||||
|
+ "sometimes, and I haven't been able to\nfigure out how to "
|
||||||
|
+ "work around it yet. If you just wait a little while\nand "
|
||||||
|
+ "then reload normally then hopefully it should work haha";
|
||||||
|
} else {
|
||||||
|
window.emu = new Emulator();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="text/javascript" src="emu.js"></script>
|
<script type="text/javascript" src="emu.js"></script>
|
||||||
<div id="output">I'm afraid you need javascript enabled for this to work :(
|
<div id="output"><noscript>I'm afraid you need javascript enabled for this to work :(
|
||||||
|
|
||||||
cringe, I know... but I promise there are no frameworks™</div>
|
cringe, I know... but I promise there are no frameworks™</noscript></div></body>
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
60
prelude.f
60
prelude.f
@@ -1,12 +1,5 @@
|
|||||||
76 EMIT 111 EMIT 97 EMIT 100 EMIT 105 EMIT 110 EMIT 103 EMIT 32 EMIT
|
|
||||||
112 EMIT 114 EMIT 101 EMIT 108 EMIT 117 EMIT 100 EMIT 101 EMIT 32 EMIT
|
|
||||||
|
|
||||||
: \ KEY 10 = 0BRANCH [ -20 , ] ; IMMEDIATE \ Now we have line comments :)
|
: \ KEY 10 = 0BRANCH [ -20 , ] ; IMMEDIATE \ Now we have line comments :)
|
||||||
|
|
||||||
\ We'll periodically sprinkle these in so that it's clear to the user
|
|
||||||
\ that things are happening.
|
|
||||||
46 EMIT
|
|
||||||
|
|
||||||
\ Conditionals
|
\ Conditionals
|
||||||
|
|
||||||
: IF
|
: IF
|
||||||
@@ -27,8 +20,6 @@
|
|||||||
SWAP !
|
SWAP !
|
||||||
; IMMEDIATE
|
; IMMEDIATE
|
||||||
|
|
||||||
46 EMIT
|
|
||||||
|
|
||||||
\ Loops
|
\ Loops
|
||||||
|
|
||||||
: BEGIN HERE @ ; IMMEDIATE
|
: BEGIN HERE @ ; IMMEDIATE
|
||||||
@@ -43,8 +34,6 @@
|
|||||||
HERE @ - ,
|
HERE @ - ,
|
||||||
; IMMEDIATE
|
; IMMEDIATE
|
||||||
|
|
||||||
46 EMIT
|
|
||||||
|
|
||||||
\ Recursive calls
|
\ Recursive calls
|
||||||
|
|
||||||
: RECURSE LATEST @ >CFA , ; IMMEDIATE
|
: RECURSE LATEST @ >CFA , ; IMMEDIATE
|
||||||
@@ -61,8 +50,6 @@
|
|||||||
|
|
||||||
( ( Take that, C ) )
|
( ( Take that, C ) )
|
||||||
|
|
||||||
46 EMIT
|
|
||||||
|
|
||||||
\ Printing utilities
|
\ Printing utilities
|
||||||
|
|
||||||
: CR 10 EMIT ;
|
: CR 10 EMIT ;
|
||||||
@@ -80,8 +67,6 @@
|
|||||||
+ EMIT
|
+ EMIT
|
||||||
;
|
;
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
: .
|
: .
|
||||||
\ Handle negatives
|
\ Handle negatives
|
||||||
DUP 0< IF CHAR - EMIT NEGATE THEN
|
DUP 0< IF CHAR - EMIT NEGATE THEN
|
||||||
@@ -110,8 +95,6 @@ CHAR . EMIT
|
|||||||
2DROP
|
2DROP
|
||||||
;
|
;
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
: TYPE ( addr len -- )
|
: TYPE ( addr len -- )
|
||||||
BEGIN
|
BEGIN
|
||||||
DUP 0= IF 2DROP EXIT THEN
|
DUP 0= IF 2DROP EXIT THEN
|
||||||
@@ -120,8 +103,6 @@ CHAR . EMIT
|
|||||||
AGAIN
|
AGAIN
|
||||||
;
|
;
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
: C, HERE @ C! 1 HERE +! ;
|
: C, HERE @ C! 1 HERE +! ;
|
||||||
|
|
||||||
: ."
|
: ."
|
||||||
@@ -151,8 +132,6 @@ CHAR . EMIT
|
|||||||
THEN
|
THEN
|
||||||
; IMMEDIATE
|
; IMMEDIATE
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
\ Misc utilities
|
\ Misc utilities
|
||||||
|
|
||||||
: NIP SWAP DROP ;
|
: NIP SWAP DROP ;
|
||||||
@@ -171,8 +150,6 @@ CHAR . EMIT
|
|||||||
|
|
||||||
: [COMPILE] ' , ; IMMEDIATE
|
: [COMPILE] ' , ; IMMEDIATE
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
\ Constants, variables and values
|
\ Constants, variables and values
|
||||||
|
|
||||||
: CONSTANT
|
: CONSTANT
|
||||||
@@ -204,24 +181,21 @@ CHAR . EMIT
|
|||||||
THEN
|
THEN
|
||||||
; IMMEDIATE
|
; IMMEDIATE
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
\ Peripheral register addresses
|
\ Peripheral register addresses
|
||||||
|
|
||||||
HEX
|
HEX
|
||||||
|
|
||||||
00 CONSTANT TXBUF
|
000 CONSTANT TXBUF
|
||||||
20 CONSTANT RXBUF
|
080 CONSTANT RXBUF
|
||||||
40 CONSTANT TXHEAD
|
100 CONSTANT TXHEAD
|
||||||
44 CONSTANT TXTAIL
|
104 CONSTANT TXTAIL
|
||||||
48 CONSTANT RXHEAD
|
108 CONSTANT RXHEAD
|
||||||
4C CONSTANT RXTAIL
|
10C CONSTANT RXTAIL
|
||||||
50 CONSTANT SYSREADY
|
110 CONSTANT SYSREADY
|
||||||
|
114 CONSTANT SYSINTER
|
||||||
|
|
||||||
DECIMAL
|
DECIMAL
|
||||||
|
|
||||||
46 EMIT
|
|
||||||
|
|
||||||
\ A better word-not-found handler
|
\ A better word-not-found handler
|
||||||
|
|
||||||
: ANY-RX? RXHEAD AC@ RXTAIL AC@ <> ;
|
: ANY-RX? RXHEAD AC@ RXTAIL AC@ <> ;
|
||||||
@@ -244,13 +218,11 @@ DECIMAL
|
|||||||
|
|
||||||
' WNF-HANDLER TO WNFHOOK
|
' WNF-HANDLER TO WNFHOOK
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
\ Version number
|
\ Version number
|
||||||
|
|
||||||
0 CONSTANT VERSION-MAJOR
|
0 CONSTANT VERSION-MAJOR
|
||||||
1 CONSTANT VERSION-MINOR
|
2 CONSTANT VERSION-MINOR
|
||||||
0 CONSTANT VERSION-PATCH
|
2 CONSTANT VERSION-PATCH
|
||||||
|
|
||||||
: PRINT-VERSION
|
: PRINT-VERSION
|
||||||
CHAR v EMIT VERSION-MAJOR .
|
CHAR v EMIT VERSION-MAJOR .
|
||||||
@@ -258,8 +230,6 @@ CHAR . EMIT
|
|||||||
CHAR . EMIT VERSION-PATCH .
|
CHAR . EMIT VERSION-PATCH .
|
||||||
;
|
;
|
||||||
|
|
||||||
CHAR . EMIT
|
|
||||||
|
|
||||||
\ Welcome banner
|
\ Welcome banner
|
||||||
|
|
||||||
: BANNER
|
: BANNER
|
||||||
@@ -269,7 +239,7 @@ CHAR . EMIT
|
|||||||
." |__,__/_/ .__(_)___/_//_/" CR
|
." |__,__/_/ .__(_)___/_//_/" CR
|
||||||
." /_/ " CR
|
." /_/ " CR
|
||||||
CR
|
CR
|
||||||
." Wipforth " PRINT-VERSION CR
|
." Welcome to Wipforth " PRINT-VERSION ." !" CR
|
||||||
." Copyright (c) Camden Dixie O'Brien" CR
|
." Copyright (c) Camden Dixie O'Brien" CR
|
||||||
CR
|
CR
|
||||||
." Wipforth is freely available to use, modify and distribute for personal use" CR
|
." Wipforth is freely available to use, modify and distribute for personal use" CR
|
||||||
@@ -277,8 +247,8 @@ CHAR . EMIT
|
|||||||
CR
|
CR
|
||||||
;
|
;
|
||||||
|
|
||||||
." done" CR
|
\ Set SYSREADY high and wait until interactive
|
||||||
BANNER
|
|
||||||
|
|
||||||
\ Set SYSREADY high to enable user input
|
|
||||||
1 SYSREADY AC!
|
1 SYSREADY AC!
|
||||||
|
SYSINTER WAIT DROP
|
||||||
|
|
||||||
|
BANNER
|
||||||
|
|||||||
@@ -16,9 +16,8 @@
|
|||||||
'(("html" . (text/html))
|
'(("html" . (text/html))
|
||||||
("css" . (text/css))
|
("css" . (text/css))
|
||||||
("js" . (application/javascript))
|
("js" . (application/javascript))
|
||||||
("wasm" . (application/wasm))
|
|
||||||
("f" . (text/plain))
|
("f" . (text/plain))
|
||||||
("wat" . (text/plain))
|
("ws" . (text/plain))
|
||||||
("png" . (image/png))))
|
("png" . (image/png))))
|
||||||
|
|
||||||
(define (mime-type path)
|
(define (mime-type path)
|
||||||
|
|||||||
14
tests.scm
14
tests.scm
@@ -19,13 +19,19 @@
|
|||||||
(define client (client-setup))
|
(define client (client-setup))
|
||||||
|
|
||||||
(navigate client "http://localhost:8080")
|
(navigate client "http://localhost:8080")
|
||||||
(sleep 5)
|
(sleep 1)
|
||||||
|
|
||||||
|
(define-test kernel-assembles-successfully
|
||||||
|
(let* ((display (get-display client))
|
||||||
|
(line (first (lines display))))
|
||||||
|
(assert (string-match "Assembling kernel \\.+ done" line)
|
||||||
|
(format #f "Kernel assemble line: ~s" line))))
|
||||||
|
|
||||||
(define-test prelude-loads-successfully
|
(define-test prelude-loads-successfully
|
||||||
(let* ((display (get-display client))
|
(let* ((display (get-display client))
|
||||||
(first-line (first (lines display))))
|
(line (second (lines display))))
|
||||||
(assert (string-match "Loading prelude \\.+ done" first-line)
|
(assert (string-match "Loading prelude \\.+ done" line)
|
||||||
(format #f "Prelude load line: ~s" first-line))))
|
(format #f "Prelude load line: ~s" line))))
|
||||||
|
|
||||||
(define-test six-seven-times-dot-cr-yields-42
|
(define-test six-seven-times-dot-cr-yields-42
|
||||||
(input-line client "6 7 * . CR")
|
(input-line client "6 7 * . CR")
|
||||||
|
|||||||
2023
wipforth.wat
2023
wipforth.wat
File diff suppressed because it is too large
Load Diff
2106
wipforth.ws
Normal file
2106
wipforth.ws
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user