From 937ccf60c9c9f05b10cbfb244364cbe94ec9271c Mon Sep 17 00:00:00 2001 From: BjoernSchilberg Date: Sun, 12 Feb 2023 22:24:16 +0100 Subject: [PATCH] Added a tinygo example. --- examples/tinygo/build.sh | 6 ++++++ examples/tinygo/main.go | 38 +++++++++++++++++++++++++++++++++++++ examples/tinygo/target.json | 23 ++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100755 examples/tinygo/build.sh create mode 100644 examples/tinygo/main.go create mode 100644 examples/tinygo/target.json diff --git a/examples/tinygo/build.sh b/examples/tinygo/build.sh new file mode 100755 index 0000000..174e179 --- /dev/null +++ b/examples/tinygo/build.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +tinygo build -o cart.wasm -target target.json ./main.go +uw8 filter-exports cart.wasm cart.wasm && \ +#wasm-opt -Oz --fast-math --strip-producers -o cart.wasm cart.wasm && \ +uw8 pack -l 9 cart.wasm cart.uw8 diff --git a/examples/tinygo/main.go b/examples/tinygo/main.go new file mode 100644 index 0000000..f0f2c25 --- /dev/null +++ b/examples/tinygo/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "math" + "unsafe" +) + +//go:wasm-module env +//export atan2 +func atan2(x, y float32) float32 + +//go:wasm-module env +//export time +func time() float32 + +func sqrt(v float32) float32 { + return float32(math.Sqrt(float64(v))) +} + +var FRAMEBUFFER = (*[320 * 240]byte)(unsafe.Pointer(uintptr(120))) + +//export upd +func upd() { + var i int + for i < 320*240 { + t := time() * 63.0 + x := float32(i%320 - 160) + y := float32(i/320 - 120) + d := float32(40000.0) / sqrt(x*x+y*y) + u := atan2(x, y) * 512.0 / 3.141 + c := uint8((int(d+t*2.0) ^ int(u+t)) >> 4) + FRAMEBUFFER[i] = c + i++ + } +} + +func main() { +} diff --git a/examples/tinygo/target.json b/examples/tinygo/target.json new file mode 100644 index 0000000..e26bb37 --- /dev/null +++ b/examples/tinygo/target.json @@ -0,0 +1,23 @@ +{ + "llvm-target": "wasm32--wasi", + "build-tags": [ "tinygo.wasm" ], + "goos": "js", + "goarch": "wasm", + "linker": "wasm-ld", + "libc": "wasi-libc", + "cflags": [ + "--target=wasm32--wasi", + "--sysroot={root}/lib/wasi-libc/sysroot", + "-Oz" + ], + "ldflags": [ + "--no-entry", + "--export-all", + "--import-memory", + "--initial-memory=262144", + "--global-base=81920", + "-zstack-size=4096", + "--strip-all" + ], + "wasm-abi": "js" +}