Compare commits

..

80 Commits

Author SHA1 Message Date
aa9c18346d Add message for when chromium hard reload bug breaks things 2026-03-21 23:16:37 +00:00
a2f06c977e Wrap no JS message in noscript tag 2026-03-21 23:15:57 +00:00
58e2cf3e1f Fix SharedArrayBuffer name in README 2026-03-20 13:36:54 +00:00
efdae93d90 Add favicon to deploy manifest 2026-03-20 13:35:59 +00:00
804626ddad Add asm.js to deploy manifest 2026-03-20 13:15:49 +00:00
cbe5733fcd Bump version patch number 2026-03-20 13:04:36 +00:00
7961c68639 Reduce wait in e2e tests 2026-03-20 13:04:28 +00:00
d202157a58 Reduce dot interval to 25 ms 2026-03-20 13:04:17 +00:00
97fc43bf93 Add "Welcome to" to welcome message 2026-03-20 13:04:10 +00:00
2c13ad4e1f Handle prelude loading message in JS rather than Forth 2026-03-20 13:03:57 +00:00
36429bf8bc Rework tokenizer to avoid unecessary allocations 2026-03-20 13:02:40 +00:00
c20e7e181b Reduce poll interval to 5 ms 2026-03-20 13:02:29 +00:00
fe5c55cabf Increase TX and RX buffer size to 128 bytes 2026-03-20 13:02:07 +00:00
67fc1d8d7b Remove race condition between assemble and prelude load prints 2026-03-18 15:17:08 +00:00
4000522b3a Remove obsolete assembly driver script 2026-03-18 15:08:57 +00:00
19ef69958d Update README 2026-03-18 15:04:32 +00:00
0a52388030 Update deploy manifest 2026-03-18 14:25:11 +00:00
6e8439eeaf Bump version number 2026-03-18 14:24:41 +00:00
eaa3242cc0 Update e2e tests 2026-03-18 14:24:25 +00:00
f77adffbef Update MIME types in server.scm 2026-03-18 14:24:05 +00:00
c91f46be88 Assemble kernel on client 2026-03-18 14:23:37 +00:00
6ee4adfea5 Translate kernel to Wasmasm 2026-03-18 10:41:09 +00:00
5dc0a7a601 Add temporary driver script 2026-03-18 10:35:14 +00:00
896a1ca563 Implement (limited) forward reference handling 2026-03-18 10:32:49 +00:00
37d56988ef Make a couple of tweaks to the kernel in preparation for porting 2026-03-18 10:30:26 +00:00
6c643f8402 Don't silently ignore trailing characters in numbers 2026-03-18 10:29:46 +00:00
7828b0f112 Yield newline token at end of comment 2026-03-15 21:39:14 +00:00
e7affbf8b7 Add .zero directive 2026-03-15 21:28:25 +00:00
02ee4c3c88 Support symbols in .at address field 2026-03-15 21:27:48 +00:00
c21b3c79c7 Fix names of atomic load and store opcodes 2026-03-15 20:04:49 +00:00
1318c3cc4e Add i64.const, i32.div_s and i32.rem_s opcodes 2026-03-15 20:04:27 +00:00
74a8f21379 Encode indices as unsigned LEB128 instead of signed 2026-03-15 20:04:06 +00:00
6784cd02b4 Encode section lengths with unsigned LEB128 2026-03-15 20:03:47 +00:00
3a103c46d1 Don't require ; to have space after in comments 2026-03-15 17:32:14 +00:00
8d4c53ca92 Allow implicit zero-init for globals 2026-03-15 14:26:22 +00:00
5e39024f6d Use unsigned shift in uleb128() 2026-03-15 14:15:40 +00:00
b85a4e8bc9 Encode data values in assembler, not parser 2026-03-15 14:15:34 +00:00
401e8e1fad Use unsigned right shift in Assembler.le() 2026-03-15 14:07:26 +00:00
d4c837216a Add f32 type 2026-03-15 13:58:33 +00:00
c93e9009da LEB128-encode index in action_symbol 2026-03-15 13:58:25 +00:00
0056610238 Add missing semicolon 2026-03-15 13:58:24 +00:00
9b4ff3e8f6 Use array flattening instead of spread operator in a few places 2026-03-15 13:58:23 +00:00
e9beacba3a De-duplicate consecutive locals of same type in wasm_section_code() 2026-03-15 13:58:23 +00:00
acf5b6e284 Handle failed def lookup in action_data() 2026-03-15 13:58:22 +00:00
72c5f64312 Handle global init value encoding in Assembler 2026-03-15 13:58:20 +00:00
7135eeba74 Restructure uleb128 2026-03-15 13:41:39 +00:00
7099ca34a3 Fix .word size 2026-03-15 13:41:05 +00:00
3ebb74c73c Check for null explicitly in token_top() 2026-03-15 13:40:43 +00:00
0dd2a925d8 Allow table elems to be labelled 2026-03-15 12:34:41 +00:00
2155d17731 Implement type, table and func symbol resolution 2026-03-15 12:28:29 +00:00
1452ffe615 Implement .table and .elem 2026-03-15 12:14:35 +00:00
46a571be93 Add error message for unhandled states and actions 2026-03-15 11:09:16 +00:00
d35b13fed0 Add .type directive 2026-03-15 11:05:37 +00:00
a3cfd405a9 Add some threads opcodes 2026-03-14 19:30:44 +00:00
671e7f60d2 Add a bunch of opcodes 2026-03-14 19:30:43 +00:00
580d5d2a4a Implement function type de-duplication 2026-03-14 19:23:02 +00:00
1105daaad0 Add support for extended opcodes 2026-03-14 19:23:00 +00:00
347dd8f534 Make all sections optional 2026-03-14 18:36:21 +00:00
f4433ce3a3 LEB128-encode addresses in data section 2026-03-14 18:35:33 +00:00
714973f052 LEB128-encode values from defs 2026-03-14 18:29:55 +00:00
4f878fdbab Add suport for block/loop/if/else 2026-03-14 18:26:12 +00:00
9fb3910a16 Allow defs to reference other defs 2026-03-14 15:04:25 +00:00
22dc1fc0ca Add support for labels 2026-03-14 14:52:44 +00:00
cc51b2d7be Fix data word size 2026-03-14 14:50:40 +00:00
902404cb10 Fix string interpolation in error messages 2026-03-14 14:01:09 +00:00
d4718f1106 Allow using defs in code 2026-03-14 13:59:42 +00:00
33f5a4be06 Fix bug in local lookup 2026-03-14 13:59:41 +00:00
e2429b2b03 Enable using defs in .byte and .word directives 2026-03-14 13:52:00 +00:00
2972030d0a Add .def support 2026-03-14 13:48:37 +00:00
2c3e5f46da Implement .align directive 2026-03-14 13:48:36 +00:00
93f3dd1f41 Implement .utf directive 2026-03-14 13:48:35 +00:00
cfa4fa7d4f Add .word directive 2026-03-14 13:48:34 +00:00
94cee7d258 Fix string interpolation in error messages 2026-03-14 13:48:33 +00:00
092d870a9c Implement .byte directive 2026-03-14 13:48:33 +00:00
6db71ee382 Add .at directive 2026-03-14 11:56:30 +00:00
5369a0969e Restructure copy implementation to avoid type-indexed block 2026-03-10 21:25:37 +00:00
118e6af896 Add support for globals (mutable only)
I'm planning on adding symbolic constants to the assembler, so I won't
really have much use for immutable globals.
2026-03-10 19:56:49 +00:00
1c4b9f850a Add support for imports (memory only) 2026-03-10 19:54:16 +00:00
672a453f6c Add string support to tokenizer 2026-03-10 17:53:58 +00:00
5a3084dd16 Implement .mem directive 2026-03-10 15:33:41 +00:00
11 changed files with 3417 additions and 2224 deletions

View File

@@ -1,31 +1,24 @@
# Wipforth
Wipforth is a simple Forth implementation that runs in the WebAssembly
virtual machine. It does I/O via memory-mapped peripherals, which are
emulated in JavaScript.
Wipforth is a Forth implementation that runs in the WebAssembly
virtual machine. The system is bootstrapped from source on page load:
the only non-text file is the favicon :)
- For the Forth kernel, see [wipforth.wat](./wipforth.wat)
- For the JavaScript emulator, see [emu.js](./emu.js)
- For the Forth prelude, which is loaded at start-up, see
[prelude.f](./prelude.f)
I/O is done via memory-mapped peripherals, which are emulated in
JavaScript.
- 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
[Peripherals](#peripherals) section below.
## Building and Running Locally
You'll need:
- [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:
There's a [Guile](https://www.gnu.org/software/guile/) script in the
repo you can use for this:
```
guile server.scm
@@ -34,14 +27,20 @@ guile server.scm
You should then be able to open <http://localhost:8080> in a browser
and use the system from there.
**NOTE**: The server is very simple and just serves the files with the
cross-origin isolation headers required for `SharedMemoryBuffer` use.
You could use any HTTP server that sets these headers.
However, since everything is bootstrapped on the client, basically any
HTTP server will do as long as it sets the appropriate response
headers for `SharedArrayBuffer` use:
You should **definitely not** use the development server to serve the
application on the open internet; I just hacked it together for
testing on localhost during development and it's probably hilariously
insecure.
- `Cross-Origin-Opener-Policy: same-origin`
- `Cross-Origin-Embedder-Policy: require-corp`
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
@@ -62,36 +61,34 @@ Given that's all sorted, you should be able to run:
guile tests.scm
```
It will print a JUnit XML report to standard out, you can pretty-print
it with:
It will print a JUnit XML report to standard out. You can
pretty-print it with `xmllint` if you have it installed:
```
guile tests.scm | xmllint --format -
```
Though, of course, this will require that you have `xmllint` on your
system.
## Peripherals
### Terminal
| Name | Address | Size / B | Access |
|--------|---------|----------|--------------|
| TXBUF | 00h | 32 | write |
| RXBUF | 20h | 32 | read |
| TXHEAD | 40h | 4 | atomic read |
| TXTAIL | 44h | 4 | atomic write |
| RXHEAD | 48h | 4 | atomic write |
| RXTAIL | 4Ch | 4 | atomic read |
| TXBUF | 000h | 32 | write |
| RXBUF | 080h | 32 | read |
| TXHEAD | 100h | 4 | atomic read |
| TXTAIL | 104h | 4 | atomic write |
| RXHEAD | 108h | 4 | atomic write |
| RXTAIL | 10Ch | 4 | atomic read |
For both sending (`TX`) and receiving (`RX`), there are three
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
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
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 |
|----------|---------|----------|--------------|
| 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
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.

1196
asm.js

File diff suppressed because it is too large Load Diff

27
boot.js
View File

@@ -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) => {
const exports = { emu: { mem: e.data } };
const mod = await WebAssembly.instantiateStreaming(
fetch('wipforth.wasm'), exports)
mod.instance.exports.reset();
switch (e.data.type) {
case "load":
const exports = { emu: { mem: e.data.mem } };
const wasm = await assemble;
self.mod = await WebAssembly.instantiate(wasm, exports);
await self.postMessage('ready');
break;
case "boot":
self.mod.instance.exports.reset();
console.log('System halt');
break;
}
};

View File

@@ -1,6 +1,8 @@
asm.js
boot.js
emu.js
favicon.png
index.html
prelude.f
styles.css
wipforth.wasm
wipforth.ws

70
emu.js
View File

@@ -1,16 +1,17 @@
const TXBUF = 0x00;
const RXBUF = 0x20;
const TXHEAD = 0x40;
const TXTAIL = 0x44;
const RXHEAD = 0x48;
const RXTAIL = 0x4c;
const SYSREADY = 0x50;
const TXBUF = 0x000;
const RXBUF = 0x080;
const TXHEAD = 0x100;
const TXTAIL = 0x104;
const RXHEAD = 0x108;
const RXTAIL = 0x10c;
const TXBUF_SIZE = 32;
const RXBUF_SIZE = 32;
const PERIPHS_SIZE = 81;
const SYSREADY = 0x110;
const SYSINTER = 0x114;
const POLL_INTERVAL_MS = 20;
const PERIPHS_SIZE = 0x200;
const POLL_INTERVAL_MS = 5;
const DOT_INTERVAL_MS = 25;
const COLS = 80;
const TAB_WIDTH = 8;
@@ -50,8 +51,19 @@ class Emulator {
document.addEventListener('keydown', (e) => this.handle_keydown(e));
window.addEventListener('resize', () => this.handle_resize());
this.worker = new Worker('boot.js');
this.worker.postMessage(this.mem);
this.forth = new Worker('boot.js', { type: 'module' });
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')
.then(res => res.text())
@@ -77,15 +89,21 @@ class Emulator {
if (!this.input_enable) {
const sysready = Atomics.load(this.mem_u8, SYSREADY);
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.blink = true;
this.flush_output();
document.getElementById('cursor').classList.add('blinking');
}
}
}
fifo_next(idx) {
return (idx + 1) & 0x1f;
return (idx + 1) & 0x7f;
}
handle_tx_data(head, tail) {
@@ -173,12 +191,14 @@ class Emulator {
}
e.preventDefault();
this.blink = false;
this.flush_output();
document.getElementById('cursor').classList.remove('blinking');
if (this.idle_timer)
clearTimeout(this.idle_timer);
this.idle_timer = setTimeout(() => {
this.blink = true;
document.getElementById('cursor').classList.add('blinking');
}, CURSOR_IDLE_TIME_MS);
}
@@ -265,10 +285,12 @@ class Emulator {
return row.map((c, x) => {
const ec = this.html_escape(c);
if (this.input_enable
&& x == this.cursor.x && y == this.cursor.y)
return '<span id="cursor">' + ec + '</span>';
else
&& x == this.cursor.x && y == this.cursor.y) {
const cl = this.blink ? 'class="blinking"' : '';
return `<span id="cursor" ${cl}>` + ec + '</span>';
} else {
return ec;
}
}).join('').trimEnd();
}).join('\n');
this.output.innerHTML = html;
@@ -325,6 +347,16 @@ class Emulator {
}
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('output').innerText = '';
const output = document.getElementById('output');
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();
}
});

View File

@@ -7,8 +7,7 @@
</head>
<body>
<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&#8482;</div>
</body>
cringe, I know... but I promise there are no frameworks&#8482;</noscript></div></body>
</html>

View File

@@ -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 :)
\ We'll periodically sprinkle these in so that it's clear to the user
\ that things are happening.
46 EMIT
\ Conditionals
: IF
@@ -27,8 +20,6 @@
SWAP !
; IMMEDIATE
46 EMIT
\ Loops
: BEGIN HERE @ ; IMMEDIATE
@@ -43,8 +34,6 @@
HERE @ - ,
; IMMEDIATE
46 EMIT
\ Recursive calls
: RECURSE LATEST @ >CFA , ; IMMEDIATE
@@ -61,8 +50,6 @@
( ( Take that, C ) )
46 EMIT
\ Printing utilities
: CR 10 EMIT ;
@@ -80,8 +67,6 @@
+ EMIT
;
CHAR . EMIT
: .
\ Handle negatives
DUP 0< IF CHAR - EMIT NEGATE THEN
@@ -110,8 +95,6 @@ CHAR . EMIT
2DROP
;
CHAR . EMIT
: TYPE ( addr len -- )
BEGIN
DUP 0= IF 2DROP EXIT THEN
@@ -120,8 +103,6 @@ CHAR . EMIT
AGAIN
;
CHAR . EMIT
: C, HERE @ C! 1 HERE +! ;
: ."
@@ -151,8 +132,6 @@ CHAR . EMIT
THEN
; IMMEDIATE
CHAR . EMIT
\ Misc utilities
: NIP SWAP DROP ;
@@ -171,8 +150,6 @@ CHAR . EMIT
: [COMPILE] ' , ; IMMEDIATE
CHAR . EMIT
\ Constants, variables and values
: CONSTANT
@@ -204,24 +181,21 @@ CHAR . EMIT
THEN
; IMMEDIATE
CHAR . EMIT
\ Peripheral register addresses
HEX
00 CONSTANT TXBUF
20 CONSTANT RXBUF
40 CONSTANT TXHEAD
44 CONSTANT TXTAIL
48 CONSTANT RXHEAD
4C CONSTANT RXTAIL
50 CONSTANT SYSREADY
000 CONSTANT TXBUF
080 CONSTANT RXBUF
100 CONSTANT TXHEAD
104 CONSTANT TXTAIL
108 CONSTANT RXHEAD
10C CONSTANT RXTAIL
110 CONSTANT SYSREADY
114 CONSTANT SYSINTER
DECIMAL
46 EMIT
\ A better word-not-found handler
: ANY-RX? RXHEAD AC@ RXTAIL AC@ <> ;
@@ -244,13 +218,11 @@ DECIMAL
' WNF-HANDLER TO WNFHOOK
CHAR . EMIT
\ Version number
0 CONSTANT VERSION-MAJOR
1 CONSTANT VERSION-MINOR
0 CONSTANT VERSION-PATCH
2 CONSTANT VERSION-MINOR
2 CONSTANT VERSION-PATCH
: PRINT-VERSION
CHAR v EMIT VERSION-MAJOR .
@@ -258,8 +230,6 @@ CHAR . EMIT
CHAR . EMIT VERSION-PATCH .
;
CHAR . EMIT
\ Welcome banner
: BANNER
@@ -269,7 +239,7 @@ CHAR . EMIT
." |__,__/_/ .__(_)___/_//_/" CR
." /_/ " CR
CR
." Wipforth " PRINT-VERSION CR
." Welcome to Wipforth " PRINT-VERSION ." !" CR
." Copyright (c) Camden Dixie O'Brien" CR
CR
." Wipforth is freely available to use, modify and distribute for personal use" CR
@@ -277,8 +247,8 @@ CHAR . EMIT
CR
;
." done" CR
BANNER
\ Set SYSREADY high to enable user input
\ Set SYSREADY high and wait until interactive
1 SYSREADY AC!
SYSINTER WAIT DROP
BANNER

View File

@@ -16,9 +16,8 @@
'(("html" . (text/html))
("css" . (text/css))
("js" . (application/javascript))
("wasm" . (application/wasm))
("f" . (text/plain))
("wat" . (text/plain))
("ws" . (text/plain))
("png" . (image/png))))
(define (mime-type path)

View File

@@ -19,13 +19,19 @@
(define client (client-setup))
(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
(let* ((display (get-display client))
(first-line (first (lines display))))
(assert (string-match "Loading prelude \\.+ done" first-line)
(format #f "Prelude load line: ~s" first-line))))
(line (second (lines display))))
(assert (string-match "Loading prelude \\.+ done" line)
(format #f "Prelude load line: ~s" line))))
(define-test six-seven-times-dot-cr-yields-42
(input-line client "6 7 * . CR")

File diff suppressed because it is too large Load Diff

2106
wipforth.ws Normal file

File diff suppressed because it is too large Load Diff