Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created October 10, 2024 09:06
Show Gist options
  • Save peterhellberg/8e05f61c35c61a36231b31602e3105a3 to your computer and use it in GitHub Desktop.
Save peterhellberg/8e05f61c35c61a36231b31602e3105a3 to your computer and use it in GitHub Desktop.
MicroW8 example https://github.com/exoticorn/microw8/tree/master/examples/zig updated to current versions of Zig ⚡
const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "cart",
.root_source_file = b.path("main.zig"),
.target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
}),
.optimize = .ReleaseSmall,
});
exe.entry = .disabled;
exe.rdynamic = true;
exe.import_memory = true;
exe.initial_memory = 262144;
exe.max_memory = 262144;
exe.global_base = 81920;
exe.stack_size = 8192;
b.installArtifact(exe);
const run_filter_exports = b.addSystemCommand(&[_][]const u8{
"uw8",
"filter-exports",
"zig-out/bin/cart.wasm",
"zig-out/bin/cart-filtered.wasm",
});
run_filter_exports.step.dependOn(b.getInstallStep());
const run_wasm_opt = b.addSystemCommand(&[_][]const u8{
"wasm-opt",
"-Oz",
"-o",
"zig-out/cart.wasm",
"zig-out/bin/cart-filtered.wasm",
});
run_wasm_opt.step.dependOn(&run_filter_exports.step);
const run_uw8_pack = b.addSystemCommand(&[_][]const u8{
"uw8",
"pack",
"-l",
"9",
"zig-out/cart.wasm",
"zig-out/cart.uw8",
});
run_uw8_pack.step.dependOn(&run_wasm_opt.step);
const make_opt = b.step("make_opt", "make size optimized cart");
make_opt.dependOn(&run_uw8_pack.step);
b.default_step = make_opt;
}
extern fn atan2(x: f32, y: f32) f32;
extern fn time() f32;
pub const FRAMEBUFFER: *[320 * 240]u8 = @ptrFromInt(0x78);
export fn upd() void {
var i: u32 = 0;
while (true) {
const t = time() * 63.0;
const x: f32 = @floatFromInt((@as(i32, @intCast(i % 320)) - 160));
const y: f32 = @floatFromInt((@as(i32, @intCast(i / 320)) - 120));
const d = 20000.0 / @sqrt(x * x + y * y);
const u = atan2(x, y) * 512.0 / 3.141;
const c: u8 = @as(u8, @intCast(
@as(i32, @intFromFloat(d + t * 2.0)) ^ @as(i32, @intFromFloat(u + t)) & 255,
)) >> 4;
FRAMEBUFFER[@as(usize, i)] = c;
i += 1;
if (i >= 320 * 240) {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment