ΞΌEforth
Graphics and Sound
on the Web
π
FORTH DAY!
November 18, 2022
ΞΌEforth
β¨ β¨ β¨
β’ Multi-platform eForth based Forth
β’ One codebase targets:
- Windows 32-bit and 64-bit
- Linux & WSL
- ESP32 / ESP32-S2/S3 / ESP32-C3 / ESP-CAM
(known as ESP32forth)
- Web!
Why the Web?
πΈ π πΈ
β’ Portable, powerful,
relatively simple I/O APIs.
β’ Asm.js, WebAssembly, and JITs
mean it can go fast!
β’ It's easy to show people.
β’ It let's people run my code
without trusting me. π
Β΅EForth Approach
β
β’ Each opcodes in 1-2 lines of C code
β’ Define "system variables" in a C structure
β’ Define 5 "core" opcodes
in larger C functions
β₯ These could be built from opcodes
β₯ But avoid needing to "assemble" loops
β’ Boot from Forth code in a string
Design Choices
π
β’ Indirect Threaded
β₯ Single cell VM opcodes
β₯ Simpler SEE / DOES>
β₯ Computed goto in C avoids assembly
β₯ Other models might be faster?
β’ Unlimited stacks
β’ 32-bit floats only
β’ No counted strings
How to bring it to the Web?
β΅ π πΈ
β’ Convert to Asm.js
β₯ Hand convert 5 "core" words
β₯ Automate converting opcodes
.. with Regexes π ..
Asm.js
β΅π±
β’ Best hack ever
β’ Embed C in JavaScript
β’ Converted by Chrome
to WebAssembly
Asm.js
β΅π±
a = a | 0; // a is an int
a = fround(a); // a is a float32
a = (a + 1) | 0; // ++a;
a = (a + b) | 0; // a = a + b;
a = imul32(a, b); // a = a * b;
a = i32[p>>2] | 0; // a = *p; (32-bit)
a = u8[p] | 0; // a = *p; (8-bit)
return a | 0; // function returns an int
Bindings to JavaScript
βββ
jseval! ( a n slot -- )
call ( slot -- )
function(sp) {
var tos = i32[sp>>2]; sp -= 4;
// do something with tos
return sp;
}
Bindings to JavaScript
βββ
jseval ( a n -- )
r~
// Run a bunch of JavaScript!
~ jseval
Bindings to JavaScript
βββ
JSWORD: myword { }
// Do something in JS
~
Arguments to JavaScript
π π
JSWORD: my+ { a b -- n }
return a + b;
~
Multiple Return
πΆπΆπΆ
JSWORD: myswap { a b -- n n }
return [b, a];
~
Graphics
π·π·
gr ( -- ) Enter graphics mode
text ( -- ) Text mode
show-text ( f -- ) Show/hide text
viewport@ ( -- w h )
window ( w h -- )
Input
π
mouse ( -- x y )
button ( -- b )
Color
π π‘ π’ π£
color! ( rgb -- )
$ff0000 constant red
$ffff00 constant yellow
$0000ff constant blue
Drawing
π¨ π¨
box ( x y w h -- )
line ( x1 y1 x2 y2 -- )
font ( a n -- )
fillText ( str str# x y -- )
Paths
π§ π§
lineWidth ( w -- )
beginPath ( -- )
moveTo ( x y -- )
lineTo ( x y -- )
quarticCurveTo ( cx cy x y -- )
stroke ( -- )
fill ( -- )
Transforms
π€π€π€
translate ( x y -- )
scale ( sx sy div -- )
rotate ( angle div -- )
gpush ( -- ) Push transform stack
gpop ( -- ) Push transform stack
Sound
π π
tone ( note[midi] volume[0-100] voice[0-7] )
silence ( -- )
DEMO
eforth.appspot.com
QUESTIONS?
π΅
Thank you!