Skip to content

Commit 6fb5e66

Browse files
committed
Merge branch 'main' into rc-1.0
2 parents ffb0071 + 2b85ca9 commit 6fb5e66

File tree

9 files changed

+186
-26
lines changed

9 files changed

+186
-26
lines changed

src/api/wasm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ M3Result linkTicAPI(IM3Module module)
966966
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "font", "i(*iiiiiii)", &wasmtic_font)));
967967
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "key", "i(i)", &wasmtic_key)));
968968
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "keyp", "i(iii)", &wasmtic_keyp)));
969-
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "line", "v(iiiii)", &wasmtic_line)));
969+
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "line", "v(ffffi)", &wasmtic_line)));
970970
// TODO: needs a lot of help for all the optional arguments
971971
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "map", "v(iiiiiiiiii)", &wasmtic_map)));
972972
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "memcpy", "v(iii)", &wasmtic_memcpy)));
@@ -994,9 +994,9 @@ M3Result linkTicAPI(IM3Module module)
994994
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "time", "f()", &wasmtic_time)));
995995
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "tstamp", "i()", &wasmtic_tstamp)));
996996
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "trace", "v(*i)", &wasmtic_trace)));
997-
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "tri", "v(iiiiiii)", &wasmtic_tri)));
998-
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "trib", "v(iiiiiii)", &wasmtic_trib)));
999-
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "ttri", "v(iiiiiiiiiiiiiiiiiii)", &wasmtic_ttri)));
997+
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "tri", "v(ffffffi)", &wasmtic_tri)));
998+
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "trib", "v(ffffffi)", &wasmtic_trib)));
999+
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "ttri", "v(ffffffffffffiiifffi)", &wasmtic_ttri)));
10001000
_ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "vbank", "i(i)", &wasmtic_vbank)));
10011001

10021002
_catch:

templates/d/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DUB_FLAGS = --quiet --build release --compiler ldc2 --arch wasm32-unknown-unknown-wasm
2+
ifneq ($(origin WASI_SDK_PATH), undefined)
3+
override DUB_FLAGS += --config wasi
4+
endif
5+
6+
build:
7+
dub build ${DUB_FLAGS}
8+
9+
clean:
10+
rm -rf cart.wasm .dub

templates/d/README.md

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,69 @@
11
# D Starter Project Template
22

3-
This is a D TIC-80 starter template. To build the D source files:
3+
## Pre-requisites
4+
5+
- [LDC](https://wiki.dlang.org/LDC)
6+
- [WASI libc](https://github.com/WebAssembly/wasi-libc)
7+
8+
### WASI libc
9+
10+
WASI libc's [README](https://github.com/WebAssembly/wasi-libc#usage) states
11+
that "the easiest way to get started with this is to use wask-sdk, which
12+
includes a build of WASI libc in its sysroot."
13+
14+
Just [building WASI libc from source](https://github.com/WebAssembly/wasi-libc#building-from-source)
15+
works too, for the purpose of programming TIC-80 games in D. Let's say you
16+
install into ```$HOME/wasi-sdk/share/wasi-sysroot```, which then look like this:
417

518
```
6-
% ./build.sh
19+
% ls -l
20+
total 12
21+
drwxr-xr-x 10 pierce pierce 4096 Apr 24 16:19 include/
22+
drwxr-xr-x 3 pierce pierce 4096 Apr 24 16:19 lib/
23+
drwxr-xr-x 3 pierce pierce 4096 Apr 24 16:19 share/
724
```
825

9-
To import the resulting WASM file into a cartridge:
26+
## Files in this template
27+
28+
- ```buildcart.sh``` - convenience script to build and run the game cartridge
29+
- ```buildwasm.sh``` - convenience script to build and run the Wasm program
30+
- ```dub.json``` - D's dub package description file
31+
- ```Makefile``` - convenience Makefile that invokes ```dub```
32+
- ```wasmdemo.wasmp``` - TIC-80 Wasm 'script' file. Note the embedded game assets data at the end of the file.
33+
34+
## Building your game
35+
36+
Define the environment variable WASI_SDK_PATH; e.g., if you installed WASI
37+
libc into ```$HOME/wasi-sdk/share/wasi-sysroot```, then ```export WASI_SDK_PATH=$HOME/wasi-sdk```.
38+
39+
Edit ```src/main.d``` to implement your game. You are of course free to
40+
organize your code in more than one D source file.
41+
42+
If you create sprites, map, music, etc., for your game, remember to
43+
replace the game asset data at the end of ```wasmdemo.wasmp``` with
44+
your creations.
45+
46+
To build the Wasm file, execute ```make```. This generates ```cart.wasm```
47+
in the current directory. To run:
1048

1149
```
12-
% tic80 --fs .
13-
tic80 prompt> load wasmdemo.wasmp
14-
tic80 prompt> import binary cart.wasm
15-
tic80 prompt> save game.tic
16-
tic80 prompt> exit
50+
% tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & run & exit'
1751
```
1852

19-
Or, on the command line:
53+
The script ```buildwasm.sh``` contains above steps as a convenience.
54+
55+
To build a TIC-80 cartridge, first build the Wasm file, then build the
56+
cartridge file:
2057

2158
```
2259
% tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & save game.tic & exit'
2360
```
2461

25-
Now, run ```game.tic```:
62+
You can then run your cartridge as follows:
2663

2764
```
2865
% tic80 --fs . --cmd 'load game.tic & run & exit'
2966
```
3067

68+
The script ```buildcart.sh``` does the above steps as a convenience.
69+

templates/d/build.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.

templates/d/buildcart.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
rm -f game.tic
3+
make clean
4+
make
5+
tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & save game.tic & exit'
6+
tic80 --fs . --cmd 'load game.tic & run & exit'

templates/d/buildwasm.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
make clean
3+
make
4+
tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & run & exit'

templates/d/dub.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "cart",
3+
"targetType": "executable",
4+
"configurations": [
5+
{
6+
"name": "raw",
7+
"dflags": [
8+
"-betterC"
9+
],
10+
"lflags": [
11+
"--strip-all",
12+
"--allow-undefined",
13+
"--stack-first",
14+
"--no-entry"
15+
]
16+
},
17+
{
18+
"name": "wasi",
19+
"dflags": [
20+
"-betterC",
21+
"-Xcc",
22+
"$WASI_SDK_PATH/share/wasi-sysroot"
23+
],
24+
"lflags": [
25+
"--strip-all",
26+
"--allow-undefined",
27+
"--stack-first",
28+
"--no-entry",
29+
"$WASI_SDK_PATH/share/wasi-sysroot/lib/wasm32-wasi/libc.a"
30+
]
31+
}
32+
],
33+
"toolchainRequirements": {
34+
"dmd": "no",
35+
"gdc": "no",
36+
"ldc": ">=1.11.0"
37+
}
38+
}

templates/d/src/main.d

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import std.algorithm: min, max;
12
import tic80;
23

34
extern(C):
45

6+
// From WASI libc:
7+
int snprintf(scope char* s, size_t n, scope const char* format, scope const ...);
8+
59
int t, x, y;
610
const char* m = "HELLO WORLD FROM D!";
11+
int r = 0;
12+
MouseData md;
713

814
void BOOT() {
915
t = 1;
@@ -12,13 +18,30 @@ void BOOT() {
1218
}
1319

1420
void TIC() {
21+
cls(13);
22+
23+
// The standard demo.
1524
if (btn(0) > 0) { y--; }
1625
if (btn(1) > 0) { y++; }
1726
if (btn(2) > 0) { x--; }
1827
if (btn(3) > 0) { x++; }
1928

20-
cls(13);
2129
spr(1+t%60/30*2, x, y, null, 0, 3, 0, 0, 2, 2);
2230
print(m, 60, 84, 15, 1, 1, 0);
2331
t++;
32+
33+
// Mouse example demonstrating use of libc function.
34+
mouse(&md);
35+
if (md.left) { r = r + 2; }
36+
r--;
37+
r = max(0, min(32, r));
38+
line(md.x, 0, md.x, 136, 11);
39+
line(0, md.y, 240, md.y, 11);
40+
circ(md.x, md.y, r, 11);
41+
42+
const BUFSIZ = 10;
43+
char[BUFSIZ] buf;
44+
char* bufptr = cast(char*)buf;
45+
snprintf(bufptr, BUFSIZ, "(%03d,%03d)", md.x, md.y);
46+
print(bufptr, 3, 3, 15, 0, 1, 1);
2447
}

templates/d/src/tic80.d

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,55 @@ extern(C):
44

55
struct MouseData {
66
short x; short y;
7-
short scrollx; short scrolly;
7+
byte scrollx; byte scrolly;
88
bool left; bool middle; bool right;
99
}
1010

1111
const int WIDTH = 240;
1212
const int HEIGHT = 136;
1313

14+
// These are pointers.
15+
const FRAMEBUFFER_PTR = cast(ubyte*)0;
16+
const TILES_PTR = cast(ubyte*)0x4000;
17+
const SPRITES_PTR = cast(ubyte*)0x6000;
18+
const MAP_PTR = cast(ubyte*)0x8000;
19+
const GAMEPADS_PTR = cast(ubyte*)0xFF80;
20+
const MOUSE_PTR = cast(ubyte*)0xFF84;
21+
const KEYBOARD_PTR = cast(ubyte*)0xFF88;
22+
const SFX_STATE_PTR = cast(ubyte*)0xFF8C;
23+
const SOUND_REGISTERS_PTR = cast(ubyte*)0xFF9C;
24+
const WAVEFORMS_PTR = cast(ubyte*)0xFFE4;
25+
const SFX_PTR = cast(ubyte*)0x100E4;
26+
const MUSIC_PATTERNS_PTR = cast(ubyte*)0x11164;
27+
const MUSIC_TRACKS_PTR = cast(ubyte*)0x13E64;
28+
const SOUND_STATE_PTR = cast(ubyte*)0x13FFC;
29+
const STEREO_VOLUME_PTR = cast(ubyte*)0x14000;
30+
const PERSISTENT_MEMORY_PTR = cast(ubyte*)0x14004;
31+
const SPRITE_FLAGS_PTR = cast(ubyte*)0x14404;
32+
const SYSTEM_FONT_PTR = cast(ubyte*)0x14604;
33+
const WASM_FREE_RAM_PTR = cast(ubyte*)0x18000;
34+
35+
// These are bounded arrays.
36+
ubyte[] FRAMEBUFFER() { return (cast(ubyte*)0)[0..16319]; } // VRAM bank 0 screen area
37+
ubyte[] TILES() { return (cast(ubyte*)0x4000)[0..8191]; }
38+
ubyte[] SPRITES() { return (cast(ubyte*)0x4000)[0..8191]; }
39+
ubyte[] MAP() { return (cast(ubyte*)0x8000)[0..32639]; }
40+
ubyte[] GAMEPADS() { return (cast(ubyte*)0xFF80)[0..3]; }
41+
ubyte[] MOUSE() { return (cast(ubyte*)0xFF84)[0..3]; }
42+
ubyte[] KEYBOARD() { return (cast(ubyte*)0xFF88)[0..3]; }
43+
ubyte[] SFX_STATE() { return (cast(ubyte*)0xFF8C)[0..15]; }
44+
ubyte[] SOUND_REGISTERS() { return (cast(ubyte*)0xFF9C)[0..71]; }
45+
ubyte[] WAVEFORMS() { return (cast(ubyte*)0xFFE4)[0..255]; }
46+
ubyte[] SFX() { return (cast(ubyte*)0x100E4)[0..4223]; }
47+
ubyte[] MUSIC_PATTERNS() { return (cast(ubyte*)0x11164)[0..11519]; }
48+
ubyte[] MUSIC_TRACKS() { return (cast(ubyte*)0x13E64)[0..407]; }
49+
ubyte[] SOUND_STATE() { return (cast(ubyte*)0x13FFC)[0..3]; }
50+
ubyte[] STEREO_VOLUME() { return (cast(ubyte*)0x14000)[0..3]; }
51+
ubyte[] PERSISTENT_MEMORY() { return (cast(ubyte*)0x14004)[0..1023]; }
52+
ubyte[] SPRITE_FLAGS() { return (cast(ubyte*)0x14404)[0..511]; }
53+
ubyte[] SYSTEM_FONT() { return (cast(ubyte*)0x14604)[0..2047]; }
54+
ubyte[] WASM_FREE_RAM() { return (cast(ubyte*)0x18000)[0..163839]; } // 160kb
55+
1456
int btn(int id);
1557
bool btnp(int id, int hold, int period);
1658
void circ(int x, int y, int radius, int color);
@@ -25,7 +67,7 @@ int font(char* text, int x, int y, ubyte transcolors, int colorcount, int width,
2567
bool fset(int id, ubyte flag, bool value);
2668
bool key(int keycode);
2769
bool keyp(int keycode, int hold, int period);
28-
void line(int x0, int y0, int x1, int y1, int color);
70+
void line(float x0, float y0, float x1, float y1, byte color);
2971
void map(int x, int y, int w, int h, int sx, int sy, ubyte transcolors, int colorcount, int scale, int remap);
3072
void memcpy(uint copyto, uint copyfrom, uint length);
3173
void memset(uint addr, ubyte value, uint length);
@@ -34,27 +76,27 @@ void mouse(MouseData* data);
3476
void mset(int x, int y, bool value);
3577
void music(int track, int frame, int row, bool loop, bool sustain, int tempo, int speed);
3678
ubyte peek(int addr, int bits);
37-
int print(const char* txt, int x, int y, int color, int fixed, int scale, int alt);
3879
ubyte peek4(uint addr4);
3980
ubyte peek2(uint addr2);
4081
ubyte peek1(uint bitaddr);
4182
void pix(int x, int y, int color);
4283
uint pmem(uint index, uint value);
43-
void poke(uint addr, ubyte value, int bits);
44-
void poke4(uint addr4, ubyte value);
45-
void poke2(uint addr2, ubyte value);
46-
void poke1(uint bitaddr, ubyte value);
84+
void poke(int addr, byte value, byte bits);
85+
void poke4(int addr4, byte value);
86+
void poke2(int addr2, byte value);
87+
void poke1(int bitaddr, byte value);
88+
int print(const char* txt, int x, int y, int color, int fixed, int scale, int alt);
4789
void rect(int x, int y, int w, int h, int color);
4890
void rectb(int x, int y, int w, int h, int color);
4991
void reset();
5092
void sfx(int id, int note, int octave, int duration, int channel, int volumeLeft, int volumeRight, int speed);
5193
void spr(int id, int x, int y, uint* transcolors, uint colorcount, int scale, int flip, int rotate, int w, int h);
5294
void sync(int mask, int bank, bool tocart);
5395
void trace(const char* txt, int color);
54-
void textri(float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, int texsrc, ubyte transcolors, int colorcount, float z1, float z2, float z3, bool persp);
96+
void ttri(float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, int texsrc, ubyte transcolors, int colorcount, float z1, float z2, float z3, bool persp);
5597
void tri(float x1, float y1, float x2, float y2, float x3, float y3, int color);
5698
void trib(float x1, float y1, float x2, float y2, float x3, float y3, int color);
57-
int time();
99+
float time();
58100
int tstamp();
59101
int vbank(int bank);
60102

0 commit comments

Comments
 (0)