update curlywas, use 'include "microw8-api.cwa"' in examples

This commit is contained in:
2022-02-26 23:41:15 +01:00
parent 47ad3b4f30
commit eb7c33d412
13 changed files with 137 additions and 78 deletions

27
Cargo.lock generated
View File

@@ -238,9 +238,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chumsky" name = "chumsky"
version = "0.5.0" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2d3efff85e8572b1c3fa0127706af58c4fff8458f8d9436d54b1e97573c7a3f" checksum = "8d02796e4586c6c41aeb68eae9bfb4558a522c35f1430c14b40136c3706e09e4"
dependencies = [ dependencies = [
"ahash", "ahash",
] ]
@@ -498,14 +498,14 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]] [[package]]
name = "curlywas" name = "curlywas"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/exoticorn/curlywas.git?rev=196719b#196719b35ef377cb7e001554b27ac5de013dcf2b" source = "git+https://github.com/exoticorn/curlywas.git?rev=cda3eb868bc1aaa837c4d36898d0e885ee8fce59#cda3eb868bc1aaa837c4d36898d0e885ee8fce59"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"ariadne", "ariadne",
"chumsky", "chumsky",
"pico-args", "pico-args",
"wasm-encoder", "wasm-encoder 0.10.0",
"wasmparser 0.81.0", "wasmparser 0.83.0",
] ]
[[package]] [[package]]
@@ -2338,7 +2338,7 @@ dependencies = [
"pico-args", "pico-args",
"upkr", "upkr",
"walrus", "walrus",
"wasm-encoder", "wasm-encoder 0.8.0",
"wasmparser 0.81.0", "wasmparser 0.81.0",
] ]
@@ -2506,6 +2506,15 @@ dependencies = [
"leb128", "leb128",
] ]
[[package]]
name = "wasm-encoder"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa9d9bf45fc46f71c407837c9b30b1e874197f2dc357588430b21e5017d290ab"
dependencies = [
"leb128",
]
[[package]] [[package]]
name = "wasmparser" name = "wasmparser"
version = "0.77.0" version = "0.77.0"
@@ -2524,6 +2533,12 @@ version = "0.81.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98930446519f63d00a836efdc22f67766ceae8dbcc1571379f2bcabc6b2b9abc" checksum = "98930446519f63d00a836efdc22f67766ceae8dbcc1571379f2bcabc6b2b9abc"
[[package]]
name = "wasmparser"
version = "0.83.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "718ed7c55c2add6548cca3ddd6383d738cd73b892df400e96b9aa876f0141d7a"
[[package]] [[package]]
name = "wasmtime" name = "wasmtime"
version = "0.30.0" version = "0.30.0"

View File

@@ -16,7 +16,7 @@ anyhow = "1"
minifb = { version = "0.20", default-features = false, features = ["x11"] } minifb = { version = "0.20", default-features = false, features = ["x11"] }
notify = "4" notify = "4"
pico-args = "0.4" pico-args = "0.4"
curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "196719b" } curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "cda3eb868bc1aaa837c4d36898d0e885ee8fce59" }
wat = "1" wat = "1"
uw8-tool = { path = "uw8-tool" } uw8-tool = { path = "uw8-tool" }
same-file = "1" same-file = "1"

View File

@@ -1,5 +1,4 @@
import "env.memory" memory(4); include "microw8-api.cwa"
import "env.printString" fn printString(i32);
export fn upd() { export fn upd() {
printString(0x20000); printString(0x20000);

View File

@@ -1,11 +1,4 @@
import "env.time" fn time() -> f32; include "microw8-api.cwa"
import "env.circle" fn circle(f32, f32, f32, i32);
import "env.cls" fn cls(i32);
import "env.randomSeed" fn seed(i32);
import "env.randomf" fn randomf() -> f32;
import "env.sin" fn sin(f32) -> f32;
import "env.cos" fn cos(f32) -> f32;
import "env.fmod" fn fmod(f32, f32) -> f32;
export fn upd() { export fn upd() {
cls(0); cls(0);
@@ -15,10 +8,10 @@ export fn upd() {
let inline rocket = i #>> 9; let inline rocket = i #>> 9;
let lazy local_time = fmod(time() + rocket as f32 / 5 as f32, 2 as f32); let lazy local_time = fmod(time() + rocket as f32 / 5 as f32, 2 as f32);
let lazy rocket = rocket + nearest(time() - local_time) as i32 * 10; let lazy rocket = rocket + nearest(time() - local_time) as i32 * 10;
seed(rocket); randomSeed(rocket);
let inline x = randomf() * 645 as f32; let inline x = randomf() * 645 as f32;
let y = randomf() * 133 as f32; let y = randomf() * 133 as f32;
let lazy angle = { seed(i); randomf() } * 44 as f32; let lazy angle = { randomSeed(i); randomf() } * 44 as f32;
let inline dx = sin(angle); let inline dx = sin(angle);
let inline dy = cos(angle); let inline dy = cos(angle);
let lazy dist = local_time * (randomf() * 44 as f32); let lazy dist = local_time * (randomf() * 44 as f32);

View File

@@ -1,38 +1,30 @@
import "env.memory" memory(4); include "microw8-api.cwa"
import "env.cls" fn cls(i32);
import "env.printString" fn printString(i32);
import "env.printChar" fn printChar(i32);
import "env.setCursorPosition" fn setCursor(i32, i32);
import "env.setTextColor" fn setTextColor(i32);
import "env.line" fn line(f32, f32, f32, f32, i32);
import "env.isButtonTriggered" fn triggered(i32) -> i32;
global mut mode: i32 = 0; global mut mode: i32 = 0;
export fn upd() { export fn upd() {
cls(0); cls(0);
if triggered(4) { if isButtonTriggered(BUTTON_A) {
mode = !mode; mode = !mode;
} }
setTextColor(15); setTextColor(15);
printString(mode * 0x20000); printString(mode * USER_MEM);
let y: i32; let y: i32;
loop y { loop y {
line(0 as f32, (y * 9 + 39) as f32, (14+16*9) as f32, (y * 9 + 39) as f32, 1); line(0 as f32, (y * 9 + 39) as f32, (14+16*9) as f32, (y * 9 + 39) as f32, 1);
line((y * 9 + 15) as f32, 24 as f32, (y * 9 + 15) as f32, (38+16*9) as f32, 1); line((y * 9 + 15) as f32, 24 as f32, (y * 9 + 15) as f32, (38+16*9) as f32, 1);
setTextColor(15); setTextColor(15);
setCursor(y * 9 + 16, 24); setCursorPosition(y * 9 + 16, 24);
let lazy hexChar = select(y < 10, y + 48, y + 87); let lazy hexChar = select(y < 10, y + 48, y + 87);
printChar(hexChar); printChar(hexChar);
setCursor(0, y * 9 + 24+16); setCursorPosition(0, y * 9 + 24+16);
printChar(hexChar); printChar(hexChar);
let x = 0; let x = 0;
loop x { loop x {
setCursor(x * 9 + 16, y * 9 + 24+16); setCursorPosition(x * 9 + 16, y * 9 + 24+16);
setTextColor(select(mode, x + y * 16, -9)); setTextColor(select(mode, x + y * 16, -9));
if y >= 2 | mode { if y >= 2 | mode {
printChar(select(mode, 0xa4, x + y * 16)); printChar(select(mode, 0xa4, x + y * 16));
@@ -47,6 +39,6 @@ data 0 {
"Default font: (press " i8(0xcc) " for palette)" i8(5, 0) "Default font: (press " i8(0xcc) " for palette)" i8(5, 0)
} }
data 0x20000 { data USER_MEM {
"Default palette: (press " i8(0xcc) " for font)" i8(5, 0) "Default palette: (press " i8(0xcc) " for font)" i8(5, 0)
} }

View File

@@ -1,10 +1,4 @@
import "env.memory" memory(4); include "microw8-api.cwa"
import "env.cls" fn cls(i32);
import "env.time" fn time() -> f32;
import "env.line" fn line(f32, f32, f32, f32, i32);
import "env.sin" fn sin(f32) -> f32;
import "env.cos" fn cos(f32) -> f32;
export fn upd() { export fn upd() {
cls(0); cls(0);

View File

@@ -0,0 +1,50 @@
// MicroW8 APIs, to be `include`d in CurlyWas sources
import "env.memory" memory(4);
import "env.sin" fn sin(f32) -> f32;
import "env.cos" fn cos(f32) -> f32;
import "env.tan" fn tan(f32) -> f32;
import "env.asin" fn asin(f32) -> f32;
import "env.acos" fn acos(f32) -> f32;
import "env.atan" fn atan(f32) -> f32;
import "env.atan2" fn atan2(f32, f32) -> f32;
import "env.pow" fn pow(f32, f32) -> f32;
import "env.log" fn log(f32) -> f32;
import "env.fmod" fn fmod(f32, f32) -> f32;
import "env.random" fn random() -> i32;
import "env.randomf" fn randomf() -> f32;
import "env.randomSeed" fn randomSeed(i32);
import "env.cls" fn cls(i32);
import "env.setPixel" fn setPixel(i32, i32, i32);
import "env.getPixel" fn getPixel(i32, i32) -> i32;
import "env.hline" fn hline(i32, i32, i32, i32);
import "env.rectangle" fn rectangle(f32, f32, f32, f32, i32);
import "env.circle" fn circle(f32, f32, f32, i32);
import "env.line" fn line(f32, f32, f32, f32, i32);
import "env.time" fn time() -> f32;
import "env.isButtonPressed" fn isButtonPressed(i32) -> i32;
import "env.isButtonTriggered" fn isButtonTriggered(i32) -> i32;
import "env.printChar" fn printChar(i32);
import "env.printString" fn printString(i32);
import "env.printInt" fn printInt(i32);
import "env.setTextColor" fn setTextColor(i32);
import "env.setBackgroundColor" fn setBackgroundColor(i32);
import "env.setCursorPosition" fn setCursorPosition(i32, i32);
import "env.rectangle_outline" fn rectangle_outline(f32, f32, f32, f32, i32);
import "env.circle_outline" fn circle_outline(f32, f32, f32, i32);
import "env.exp" fn exp(f32) -> f32;
const TIME_MS = 0x40;
const GAMEPAD = 0x44;
const FRAMEBUFFER = 0x78;
const PALETTE = 0x13000;
const FONT = 0x13400;
const USER_MEM = 0x14000;
const BUTTON_UP = 0x0;
const BUTTON_DOWN = 0x1;
const BUTTON_LEFT = 0x2;
const BUTTON_RIGHT = 0x3;
const BUTTON_A = 0x4;
const BUTTON_B = 0x5;
const BUTTON_X = 0x6;
const BUTTON_Y = 0x7;

View File

@@ -1,12 +1,4 @@
import "env.memory" memory(4); include "microw8-api.cwa"
import "env.rectangle" fn rect(f32, f32, f32, f32, i32);
import "env.circle" fn circle(f32, f32, f32, i32);
import "env.isButtonPressed" fn btn(i32) -> i32;
import "env.random" fn random() -> i32;
import "env.randomSeed" fn randomSeed(i32);
import "env.cls" fn cls(i32);
import "env.printInt" fn printInt(i32);
global mut pz: i32 = 4; global mut pz: i32 = 4;
global mut px: f32 = 2.0; global mut px: f32 = 2.0;
@@ -19,7 +11,7 @@ export fn upd() {
let inline zero = 0.0; let inline zero = 0.0;
let lazy control_speed = 0.03125; let lazy control_speed = 0.03125;
s = s + 0.1875 - (f + control_speed) * btn(4 <| cls(4)) as f32; s = s + 0.1875 - (f + control_speed) * isButtonPressed(4 <| cls(4)) as f32;
f = f * 0.5625; f = f * 0.5625;
printInt(pz); printInt(pz);
@@ -33,8 +25,8 @@ export fn upd() {
let inline c = (z & 1) * -2; let inline c = (z & 1) * -2;
let inline yf = y as f32; let inline yf = y as f32;
rect(rx, yf, rw, yf / 6 as f32, c + 1); rectangle(rx, yf, rw, yf / 6 as f32, c + 1);
rect(rx, yf, rw, 1 as f32, c - 4); rectangle(rx, yf, rw, 1 as f32, c - 4);
if y == 180 & py > zero { if y == 180 & py > zero {
if x > w | x < zero { if x > w | x < zero {
@@ -51,7 +43,7 @@ export fn upd() {
circle(160 as f32, 160 as f32 + py, 22 as f32, -28); circle(160 as f32, 160 as f32 + py, 22 as f32, -28);
circle((160 - 6) as f32, (160 - 6) as f32 + py, 6 as f32, -26); circle((160 - 6) as f32, (160 - 6) as f32 + py, 6 as f32, -26);
px = px + (btn(3) - btn(2)) as f32 * control_speed; px = px + (isButtonPressed(3) - isButtonPressed(2)) as f32 * control_speed;
py = py + s; py = py + s;
pz = pz + 1; pz = pz + 1;
} }

View File

@@ -1,7 +1,4 @@
import "env.memory" memory(4); include "microw8-api.cwa"
import "env.sin" fn sin(f32) -> f32;
import "env.time" fn time() -> f32;
import "env.setPixel" fn setPixel(i32, i32, i32);
export fn upd() { export fn upd() {
let x: i32; let x: i32;

View File

@@ -1,7 +1,4 @@
import "env.memory" memory(2); include "microw8-api.cwa"
import "env.fmod" fn fmod(f32, f32) -> f32;
import "env.time" fn time() -> f32;
export fn upd() { export fn upd() {
let i: i32; let i: i32;

View File

@@ -1,7 +1,4 @@
import "env.memory" memory(4); include "microw8-api.cwa"
import "env.atan2" fn atan2(f32, f32) -> f32;
import "env.time" fn time() -> f32;
export fn upd() { export fn upd() {
let i: i32; let i: i32;

View File

@@ -135,17 +135,15 @@ struct Config {
} }
fn load_cart(filename: &Path, config: &Config) -> Result<Vec<u8>> { fn load_cart(filename: &Path, config: &Config) -> Result<Vec<u8>> {
let mut cart = vec![]; let mut cart = match SourceType::of_file(filename)? {
File::open(filename)?.read_to_end(&mut cart)?; SourceType::Binary => {
let mut cart = vec![];
if cart[0] >= 10 { File::open(filename)?.read_to_end(&mut cart)?;
let src = String::from_utf8(cart)?; cart
cart = if src.chars().find(|c| !c.is_whitespace()) == Some('(') { }
wat::parse_str(src)? SourceType::Wat => wat::parse_file(filename)?,
} else { SourceType::CurlyWas => curlywas::compile_file(filename, curlywas::Options::default())?,
curlywas::compile_str(&src, filename, curlywas::Options::default())? };
};
}
if let Some(ref pack_config) = config.pack { if let Some(ref pack_config) = config.pack {
cart = uw8_tool::pack(&cart, pack_config)?; cart = uw8_tool::pack(&cart, pack_config)?;
@@ -159,6 +157,41 @@ fn load_cart(filename: &Path, config: &Config) -> Result<Vec<u8>> {
Ok(cart) Ok(cart)
} }
enum SourceType {
Binary,
Wat,
CurlyWas,
}
impl SourceType {
fn of_file(filename: &Path) -> Result<SourceType> {
if let Some(extension) = filename.extension() {
if extension == "uw8" || extension == "wasm" {
return Ok(SourceType::Binary);
} else if extension == "wat" || extension == "wast" {
return Ok(SourceType::Wat);
} else if extension == "cwa" {
return Ok(SourceType::CurlyWas);
}
}
let mut cart = vec![];
File::open(filename)?.read_to_end(&mut cart)?;
let ty = if cart[0] < 10 {
SourceType::Binary
} else {
let src = String::from_utf8(cart)?;
if src.chars().find(|&c| !c.is_whitespace() && c != ';') == Some('(') {
SourceType::Wat
} else {
SourceType::CurlyWas
}
};
Ok(ty)
}
}
#[cfg(any(feature = "native", feature = "browser"))] #[cfg(any(feature = "native", feature = "browser"))]
fn start_cart(filename: &Path, runtime: &mut dyn Runtime, config: &Config) -> Result<()> { fn start_cart(filename: &Path, runtime: &mut dyn Runtime, config: &Config) -> Result<()> {
let cart = load_cart(filename, config)?; let cart = load_cart(filename, config)?;

View File

@@ -291,7 +291,7 @@ impl BaseModule {
pub fn write_as_cwa<P: AsRef<Path>>(&self, path: P) -> Result<()> { pub fn write_as_cwa<P: AsRef<Path>>(&self, path: P) -> Result<()> {
fn inner(mut file: File, base: &BaseModule) -> Result<()> { fn inner(mut file: File, base: &BaseModule) -> Result<()> {
writeln!(file, ";; MicroW8 APIs, to be `include`d in CurlyWas sources")?; writeln!(file, "// MicroW8 APIs, to be `include`d in CurlyWas sources")?;
writeln!(file, "import \"env.memory\" memory({});", base.memory)?; writeln!(file, "import \"env.memory\" memory({});", base.memory)?;
writeln!(file)?; writeln!(file)?;
for &(module, ref name, type_id) in &base.function_imports { for &(module, ref name, type_id) in &base.function_imports {