Skip to content

Commit

Permalink
补充 wasm4 文件
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Aug 24, 2024
1 parent 3e1078c commit beffe37
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 21 deletions.
9 changes: 9 additions & 0 deletions waroot/examples/w4-hello/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 版权 @2024 P5-hello 作者。保留所有权利。

dev:
go run ../../../main.go build -target=wasm4 .

run:
go run ../../../main.go run -target=wasm4 .

clean:
4 changes: 4 additions & 0 deletions waroot/examples/w4-hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# WASM4 例子

https://wasm4.org/docs/

28 changes: 28 additions & 0 deletions waroot/examples/w4-hello/src/main.wa
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 版权 @2024 W4-hello 作者。保留所有权利。

import "wasm4"

global smiley = []byte{
0b11000011,
0b10000001,
0b00100100,
0b00100100,
0b00000000,
0b00100100,
0b10011001,
0b11000011,
}

func init {
wasm4.Update = update
}

func Update() {
wasm4.DrawColors.SetPrimary(wasm4.Primary)
wasm4.DrawText("Hello from Wa!", wasm4.Point{X: 10, Y: 10})
if wasm4.Gamepad.X() {
wasm4.DrawColors.SetPrimary(wasm4.Dark)
}
wasm4.Blit(smiley, wasm4.Point{X: 76, Y: 76}, wasm4.Size{Width: 8, Height: 8}, 0)
wasm4.DrawText("Press X to blink", wasm4.Point{X: 16, Y: 90})
}
4 changes: 4 additions & 0 deletions waroot/examples/w4-hello/wa.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 版权 @2024 magnifying-glass 作者。保留所有权利。

name = "w4app"
pkgpath = "w4app"
52 changes: 52 additions & 0 deletions waroot/src/runtime/print_wasm4.wat.ws
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
;; Copyright 2023 The Wa Authors. All rights reserved.

;; 打印字符串
(func $$runtime.waPuts (param $str i32) (param $len i32)
;; {{$$runtime.waPuts/body/begin}}

(local $sp i32)
(local $p_iov i32)
(local $p_nwritten i32)
(local $stdout i32)

;; 保存栈指针状态
global.get $__stack_ptr
local.set $sp

;; 分配 iov 结构体
i32.const 8
call $runtime.stackAlloc
local.set $p_iov

;; 返回地址
i32.const 4
call $runtime.stackAlloc
local.set $p_nwritten

;; 设置字符串指针和长度
local.get $p_iov
local.get $str
i32.store offset=0 align=1

local.get $p_iov
local.get $len
i32.store offset=4 align=1

;; 标准输出
i32.const 1
local.set $stdout

;; 输出字符串
local.get $stdout
local.get $p_iov
i32.const 1
local.get $p_nwritten
call $$runtime.fdWrite

;; 重置栈指针
local.get $sp
global.set $__stack_ptr
drop

;; {{$$runtime.waPuts/body/end}}
)
5 changes: 5 additions & 0 deletions waroot/src/runtime/runtime_wasm4.wa
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 版权 @2022 凹语言 作者。保留所有权利。

import "syscall/wasm4"

const WAOS = "wasm4"
2 changes: 1 addition & 1 deletion waroot/src/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ var stdPkgs = []string{
"syscall", // API 完整
"syscall/js", //
"syscall/wasi", //
"syscall/wasm4", // WASM4 游戏
"syscall/unknown", //
"text/template", // 无
"unicode", // API 部分
"unicode/ctypes", // API 完整, 测试已覆盖, wat2wasm ok
"unicode/utf8", // API 完整, 测试已覆盖, wat2wasm ok
"wasm4",
}

var wzStdPkgs = []string{
Expand Down
File renamed without changes.
38 changes: 18 additions & 20 deletions waroot/src/wasm4/wasm.wa → waroot/src/syscall/wasm4/wasm.wa
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const SCREEN_SIZE: i32 = 160

// 内存地址
const (
PALETTE :u32 = 0x04 // [4]u32
DRAW_COLORS :u32 = 0x14 // u16
GAMEPAD1 :u32 = 0x16 // u8
GAMEPAD2 :u32 = 0x17 // u8
GAMEPAD3 :u32 = 0x18 // u8
GAMEPAD4 :u32 = 0x19 // u8
MOUSE_X :u32 = 0x1a // u16
MOUSE_Y :u32 = 0x1c // u16
MOUSE_BUTTONS :u32 = 0x1e // u8
SYSTEM_FLAGS :u32 = 0x1f // u8
NETPLAY :u32 = 0x20 // u8
FRAMEBUFFER :u32 = 0xa0 // [6400]u8, 帧缓存
PALETTE :i32 = 0x04 // [4]i32
DRAW_COLORS :i32 = 0x14 // u16
GAMEPAD1 :i32 = 0x16 // u8
GAMEPAD2 :i32 = 0x17 // u8
GAMEPAD3 :i32 = 0x18 // u8
GAMEPAD4 :i32 = 0x19 // u8
MOUSE_X :i32 = 0x1a // u16
MOUSE_Y :i32 = 0x1c // u16
MOUSE_BUTTONS :i32 = 0x1e // u8
SYSTEM_FLAGS :i32 = 0x1f // u8
NETPLAY :i32 = 0x20 // u8
FRAMEBUFFER :i32 = 0xa0 // [6400]u8, 帧缓存
)

// 鼠标键盘
Expand All @@ -41,11 +41,11 @@ const (

// 复制数据到帧缓存
#wa:import env blit
func Blit(sprite: *byte, x, y: i32, width, height, flags: u32)
func Blit(sprite: i32, x, y: i32, width, height, flags: i32)

// 复制数据到帧缓存(增强版)
#wa:import env blitSub
func BlitSub(sprite: *byte, x, y: i32, width, height: u32, srcX, srcY: u32, stride: i32, flags: u32)
func BlitSub(sprite: i32, x, y: i32, width, height: i32, srcX, srcY: i32, stride: i32, flags: i32)

// Flags位信息
const (
Expand Down Expand Up @@ -82,7 +82,7 @@ func Text(text: string, x, y: i32)

// 播发声音
#wa:import env tone
func Tone(frequency, duration, volume, flags: u32)
func Tone(frequency, duration, volume, flags: i32)

const (
TONE_PULSE1 = 0
Expand All @@ -98,16 +98,14 @@ const (
TONE_NOTE_MODE = 64
)

// 存储函数

// 读磁盘数据
#wa:import env diskr
func DiskR(ptr, count: u32) => u32
func DiskR(ptr, count: i32) => i32

// 写磁盘数据
#wa:import env diskw
func DiskW(src, count: u32) => u32
func DiskW(src, count: i32) => i32

// 打印调试信息
#wa:import env traceUtf8
func Trace(str: string)
func Trace(str: i32)
1 change: 1 addition & 0 deletions waroot/src/wasm4/keep.wa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// 版权 @2024 凹语言 作者。保留所有权利。

0 comments on commit beffe37

Please sign in to comment.