ΞΌ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!