aboutsummaryrefslogtreecommitdiff
path: root/stub/pico8.wat
blob: 80295590493c528d0ec43010b1b8d8e16d5fc9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(module
  (import "env" "memory" (memory 4))
  (import "env" "_emscripten_set_main_loop"
    (func $emscripten_set_main_loop (param i32 i32 i32)))
  (import "env" "_SDL_SetVideoMode"
    (func $SDL_SetVideoMode (param i32 i32 i32 i32) (result i32)))
  (import "env" "_SDL_GetMouseState"
    (func $SDL_GetMouseState (param i32 i32) (result i32)))
  (import "env" "_SDL_LockSurface"
    (func $SDL_LockSurface (param i32) (result i32)))
  (import "env" "_SDL_PollEvent"
    (func $SDL_PollEvent (param i32) (result i32)))
  (import "env" "_SDL_UnlockSurface"
    (func $SDL_UnlockSurface (param i32)))

  (func (export "___emscripten_environ_constructor"))

  (global $ptr (mut i32) (i32.const 65536))
  (func $alloc (export "stackAlloc") (export "_malloc")
    (param $size i32) (result i32)
    (local $ret i32)
    (local.set $ret (global.get $ptr))
    (global.set $ptr
      (i32.and
        (i32.add
          (i32.add
            (global.get $ptr)
            (local.get $size))
          (i32.const 15))
        (i32.const -16)))
    (local.get $ret))

  (global $surface (mut i32) (i32.const 0))
  (global $event (mut i32) (i32.const 0))
  (global $mouse_x (mut i32) (i32.const 0))
  (global $mouse_y (mut i32) (i32.const 0))
  (global $key_z (mut i32) (i32.const 0))
  (global $key_x (mut i32) (i32.const 0))

  (func (export "_main") (param i32 i32) (result i32)
    (global.set $surface
      (call $SDL_SetVideoMode
        (i32.const 128)
        (i32.const 128)
        (i32.const 32)
        (i32.const 0)))
    (global.set $event
      (call $alloc (i32.const 36)))
    (call $emscripten_set_main_loop
      (i32.const 1)
      (i32.const 0)
      (i32.const 0))
    (i32.const 0))

  (func (export "dynCall_v") (param i32)
    (local $pixels i32)
    (local $w i32)
    (local $h i32)
    (local $pitch i32)
    (local $mx i32)
    (local $my i32)
    (local $buttons i32)
    (local $x i32)
    (local $y i32)
    (local $addr i32)
    (local $color i32)
    (local $bg i32)
    (local $type i32)
    (local $sym i32)

    (block $poll_done
      (loop $poll_loop
        (br_if $poll_done
          (i32.eqz (call $SDL_PollEvent (global.get $event))))
        (local.set $type (i32.load (global.get $event)))
        (if (i32.eq (local.get $type) (i32.const 0x400))
          (then
            (global.set $mouse_x
              (i32.load (i32.add (global.get $event) (i32.const 20))))
            (global.set $mouse_y
              (i32.load (i32.add (global.get $event) (i32.const 24))))))
        (if
          (i32.or
            (i32.eq (local.get $type) (i32.const 0x300))
            (i32.eq (local.get $type) (i32.const 0x301)))
          (then
            (local.set $sym
              (i32.load (i32.add (global.get $event) (i32.const 16))))
            (if (i32.eq (local.get $sym) (i32.const 122))
              (then
                (global.set $key_z
                  (i32.eq (local.get $type) (i32.const 0x300)))))
            (if (i32.eq (local.get $sym) (i32.const 120))
              (then
                (global.set $key_x
                  (i32.eq (local.get $type) (i32.const 0x300)))))
            (if
              (i32.and
                (i32.eq (local.get $sym) (i32.const 27))
                (i32.eq (local.get $type) (i32.const 0x300)))
              (then unreachable))))
        (br $poll_loop)))

    (local.set $buttons
      (call $SDL_GetMouseState (i32.const 0) (i32.const 0)))
    (local.set $mx (global.get $mouse_x))
    (local.set $my (global.get $mouse_y))

    (local.set $bg (i32.const 0xff532b1d))
    (if (global.get $key_z)
      (then
        (local.set $bg (i32.const 0xff53257e))))
    (if (global.get $key_x)
      (then
        (local.set $bg (i32.const 0xff518700))))
    (if (i32.and (global.get $key_z) (global.get $key_x))
      (then
        (local.set $bg (i32.const 0xff3652ab))))

    (drop
      (call $SDL_LockSurface (global.get $surface)))

    (local.set $w (i32.load (i32.add (global.get $surface) (i32.const 8))))
    (local.set $h (i32.load (i32.add (global.get $surface) (i32.const 12))))
    (local.set $pitch (i32.load (i32.add (global.get $surface) (i32.const 16))))
    (local.set $pixels (i32.load (i32.add (global.get $surface) (i32.const 20))))

    (local.set $y (i32.const 0))
    (block $y_done
      (loop $y_loop
        (br_if $y_done (i32.ge_s (local.get $y) (local.get $h)))
        (local.set $x (i32.const 0))
        (block $x_done
          (loop $x_loop
            (br_if $x_done (i32.ge_s (local.get $x) (local.get $w)))
            (local.set $addr
              (i32.add
                (local.get $pixels)
                (i32.add
                  (i32.mul (local.get $y) (local.get $pitch))
                  (i32.shl (local.get $x) (i32.const 2)))))
            (if
              (i32.or
                (i32.eq (local.get $x) (local.get $mx))
                (i32.eq (local.get $y) (local.get $my)))
              (then
                (local.set $color (i32.const 0xff000000))
                (if (i32.and (local.get $buttons) (i32.const 1))
                  (then
                    (local.set $color
                      (i32.or (local.get $color) (i32.const 0x000000ff)))))
                (if (i32.and (local.get $buttons) (i32.const 2))
                  (then
                    (local.set $color
                      (i32.or (local.get $color) (i32.const 0x0000ff00)))))
                (if (i32.and (local.get $buttons) (i32.const 4))
                  (then
                    (local.set $color
                      (i32.or (local.get $color) (i32.const 0x00ff0000)))))
              )
              (else
                (local.set $color (local.get $bg))))
            (i32.store (local.get $addr) (local.get $color))
            (local.set $x (i32.add (local.get $x) (i32.const 1)))
            (br $x_loop)))
        (local.set $y (i32.add (local.get $y) (i32.const 1)))
        (br $y_loop)))

    (call $SDL_UnlockSurface (global.get $surface)))

  (func (export "_mix_0") (param $a i32) (param $b i32) (result i32)
    (i32.mul
      (local.get $a)
      (local.get $b)))
)