Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TIME for when the runtime started #365

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/assets/templates/assemblyscript/src/wasm4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const MOUSE_X: usize = 0x1a;
export const MOUSE_Y: usize = 0x1c;
export const MOUSE_BUTTONS: usize = 0x1e;
export const SYSTEM_FLAGS: usize = 0x1f;
export const TIME: usize = 0x20;
export const FRAMEBUFFER: usize = 0xa0;

export const BUTTON_1: u8 = 1;
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/c/src/wasm4.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ WASM_EXPORT("update") void update ();
#define MOUSE_Y ((const int16_t*)0x1c)
#define MOUSE_BUTTONS ((const uint8_t*)0x1e)
#define SYSTEM_FLAGS ((uint8_t*)0x1f)
#define TIME ((uint64_t*)0x20)
#define FRAMEBUFFER ((uint8_t*)0xa0)

#define BUTTON_1 1
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/go/w4/wasm4.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var MOUSE_X = (*int16)(unsafe.Pointer(uintptr(0x1a)))
var MOUSE_Y = (*int16)(unsafe.Pointer(uintptr(0x1c)))
var MOUSE_BUTTONS = (*uint8)(unsafe.Pointer(uintptr(0x1e)))
var SYSTEM_FLAGS = (*uint8)(unsafe.Pointer(uintptr(0x1f)));
var TIME = (*uint64)(unsafe.Pointer(uintptr(0x20)));
var FRAMEBUFFER = (*[6400]uint8)(unsafe.Pointer(uintptr(0xa0)))

const BUTTON_1 byte = 1
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/nelua/src/wasm4.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ global MOUSE_X <comptime> = (@*int16)(0x1a)
global MOUSE_Y <comptime> = (@*int16)(0x1c)
global MOUSE_BUTTONS <comptime> = (@*uint8)(0x1e)
global SYSTEM_FLAGS <comptime> = (@*uint8)(0x1f)
global TIME <comptime> = (@*uint64)(0x20)
global FRAMEBUFFER <comptime> = (@*[6400]uint8)(0xa0)

global BUTTON_1 <comptime> = 1
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/odin/src/w4/wasm4_wasm32.odin
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ MOUSE_X := (^i16)(uintptr(0x1a))
MOUSE_Y := (^i16)(uintptr(0x1c))
MOUSE_BUTTONS := (^Mouse_Buttons)(uintptr(0x1e))
SYSTEM_FLAGS := (^System_Flags)(uintptr(0x1f))
TIME := (^u64)(uintptr(0x20))
FRAMEBUFFER := (^[6400]u8)(uintptr(0xa0)) // 4 bits * (160*160)

Palette :: distinct [4]u32
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/rust/src/wasm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub const MOUSE_X: *const i16 = 0x1a as *const i16;
pub const MOUSE_Y: *const i16 = 0x1c as *const i16;
pub const MOUSE_BUTTONS: *const u8 = 0x1e as *const u8;
pub const SYSTEM_FLAGS: *mut u8 = 0x1f as *mut u8;
pub const TIME: *mut u64 = 0x20 as *mut u64;
pub const FRAMEBUFFER: *mut [u8; 6400] = 0xa0 as *mut [u8; 6400];

pub const BUTTON_1: u8 = 1;
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/wat/main.wat
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
(global $MOUSE_Y i32 (i32.const 0x1c))
(global $MOUSE_BUTTONS i32 (i32.const 0x1e))
(global $SYSTEM_FLAGS i32 (i32.const 0x1f))
(global $TIME u64 (u64.const 0x20))
(global $FRAMEBUFFER i32 (i32.const 0xa0))

(global $BUTTON_1 i32 (i32.const 1))
Expand Down
1 change: 1 addition & 0 deletions cli/assets/templates/zig/src/wasm4.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const MOUSE_X: *const i16 = @intToPtr(*const i16, 0x1a);
pub const MOUSE_Y: *const i16 = @intToPtr(*const i16, 0x1c);
pub const MOUSE_BUTTONS: *const u8 = @intToPtr(*const u8, 0x1e);
pub const SYSTEM_FLAGS: *u8 = @intToPtr(*u8, 0x1f);
pub const TIME: *u64 = @intToPtr(*u64, 0x20);
pub const FRAMEBUFFER: *[6400]u8 = @intToPtr(*[6400]u8, 0xA0);

pub const BUTTON_1: u8 = 1;
Expand Down
7 changes: 6 additions & 1 deletion devtools/web/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const ADDR_MOUSE_X = 0x1a;
export const ADDR_MOUSE_Y = 0x1c;
export const ADDR_MOUSE_BUTTONS = 0x1e;
export const ADDR_SYSTEM_FLAGS = 0x1f;
export const ADDR_TIME = 0x20;
export const ADDR_FRAMEBUFFER = 0xa0;

export const BUTTON_X = 1;
Expand Down Expand Up @@ -83,9 +84,13 @@ export const memoryMap: Readonly<Record<string, Range>> = {
offset: ADDR_SYSTEM_FLAGS,
len: 1,
},
TIME: {
offset: ADDR_TIME,
len: 8,
},
RESERVED: {
offset: 0x0020,
len: 128,
len: 120,
},
FRAMEBUFFER: {
offset: ADDR_FRAMEBUFFER,
Expand Down
2 changes: 2 additions & 0 deletions devtools/web/src/models/MemoryView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class MemoryView implements MemoryViewComputedProperties {
readonly pointerPos: Point;
readonly mouseBtnByte: number;
readonly systemFlags: number;
readonly startTime: number;
RobLoach marked this conversation as resolved.
Show resolved Hide resolved
readonly gamepads: number[];
readonly palette: [number, number, number, number];
readonly drawColors: number;
Expand All @@ -58,6 +59,7 @@ export class MemoryView implements MemoryViewComputedProperties {
this.palette = extractPalette(dataView);
this.gamepads = extractGamepads(dataView, bufferedData);
this.systemFlags = dataView.getUint8(constants.ADDR_SYSTEM_FLAGS);
this.startTime = dataView.getUint32(constants.ADDR_TIME); // TODO: DataView doesn't support uint64?
RobLoach marked this conversation as resolved.
Show resolved Hide resolved
this.mouseBtnByte =
dataView.getUint8(constants.ADDR_MOUSE_BUTTONS) |
bufferedData.mouseButtons;
Expand Down
3 changes: 3 additions & 0 deletions examples/clock/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build
/package-lock.json
/node_modules
32 changes: 32 additions & 0 deletions examples/clock/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"entries": [
"./src/main.ts"
],
"options": {
"binaryFile": "build/cart.wasm",
"runtime": "incremental",
"importMemory": true,
"initialMemory": 1,
"maximumMemory": 1,
"noExportMemory": true,
"zeroFilledMemory": true,
"memoryBase": 6560,
"use": [
"seed=src/wasm4/seedHandler",
"trace="
]
},
"targets": {
"release": {
"optimizeLevel": 3,
"shrinkLevel": 0,
"noAssert": true,
"use": "abort="
},
"debug": {
"debug": true,
"sourceMap": "http://localhost:4444/cart.wasm.map",
"use": "abort=src/wasm4/abortHandler"
}
}
}
15 changes: 15 additions & 0 deletions examples/clock/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "wasm4-clock",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "asc --target release",
"build:debug": "asc --target debug"
},
"dependencies": {
"as-date": "^0.1.3"
},
"devDependencies": {
"assemblyscript": "^0.19.11"
}
}
82 changes: 82 additions & 0 deletions examples/clock/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as w4 from "./wasm4";
import { Date } from "as-date";

let time: f64;
let timezone: i32;
let previousGamepad: u8;

export function start(): void {
// Get the time the runtime was started.
time = load<u64>(w4.TIME) as f64;

// Load the timezone.
const ptr = memory.data(sizeof<i32>());
w4.diskr(ptr, sizeof<i32>());
timezone = load<i32>(ptr);

// Palette.
store<u32>(w4.PALETTE, Number.parseInt("000000", 16) as u32);
store<u32>(w4.PALETTE, Number.parseInt("ffffff", 16) as u32, 4);
store<u32>(w4.PALETTE, Number.parseInt("ff0000", 16) as u32, 8);
store<u32>(w4.PALETTE, Number.parseInt("00ff00", 16) as u32, 12);
}

function setTimezone(newTimezone:i32): void {
const ptr = memory.data(sizeof<i32>());
store<i32>(ptr, newTimezone);
w4.diskw(ptr, sizeof<i32>());
timezone = newTimezone;
}

export function update(): void {
// Increase the seconds.
time += 1 / 60; // 60 frames per second

// Clear the screen.
store<u16>(w4.DRAW_COLORS, 0x2);
w4.rect(0, 0, w4.SCREEN_SIZE, w4.SCREEN_SIZE);

// Interpret the time into a valid Date.
const date = new Date((time as i64) * 1000); // Date() expects milliseconds
date.setTimezoneOffset(timezone * 60);

// Draw the clock.
let center:i32 = w4.SCREEN_SIZE / 2;
let clockRadius = w4.SCREEN_SIZE / 2 * 0.95;
store<u16>(w4.DRAW_COLORS, 0x12);
w4.oval(center - clockRadius as u32, center - clockRadius as u32, clockRadius * 2 as u32, clockRadius * 2 as u32);

// Draw the numbers on the clock.
store<u16>(w4.DRAW_COLORS, 0x1);
let textRadius = clockRadius * 0.85
for (let i = 0; i < 12; i++) {
let clockPosition = i * 5;
let text = i == 0 ? "12" : i.toString()
let textWidth = text.length * 8;
let textHeight = 8;
w4.text(text, center + textRadius * Math.sin(clockPosition * (2 * Math.PI / 60)) - textWidth / 2 as i32 , center - textRadius * Math.cos(clockPosition * (2 * Math.PI / 60)) - textHeight / 2 as i32);
}

// Draw the hands.
let sradius = clockRadius * 0.8;
let mradius = clockRadius * 0.9;
let hradius = clockRadius * 0.6;
store<u16>(w4.DRAW_COLORS, 0x3);
w4.line(center, center, center + sradius * Math.sin(date.getSeconds() * (2 * Math.PI / 60)) as i32, center - sradius * Math.cos(date.getSeconds() * (2 * Math.PI / 60)) as i32)
store<u16>(w4.DRAW_COLORS, 0x1);
w4.line(center, center, center + mradius * Math.sin(date.getMinutes() * (2 * Math.PI / 60)) as i32, center - mradius * Math.cos(date.getMinutes() * (2 * Math.PI / 60)) as i32);
store<u16>(w4.DRAW_COLORS, 0x1);
let h = date.getHours() % 12 + date.getMinutes() / 60.0;
w4.line(center, center, center + hradius * Math.sin(h * (2 * Math.PI / 60)) as i32, center - hradius * Math.cos(h * (2 * Math.PI / 60)) as i32);

// Allow changing the timezone.
const gamepad = load<u8>(w4.GAMEPAD1);
const pressedThisFrame = gamepad & (gamepad ^ previousGamepad);
previousGamepad = gamepad;
if (pressedThisFrame & w4.BUTTON_LEFT) {
setTimezone(timezone - 1);
}
if (pressedThisFrame & w4.BUTTON_RIGHT) {
setTimezone(timezone + 1);
}
}
Loading