aboutsummaryrefslogtreecommitdiff
path: root/example.asm
blob: 468db3af8ebadcf3d43d78233685efea70184a2d (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
;
; example.asm for TR8
;
.org 0

; address of the frame interrupt vector
.equ INT_VECT 0xff00

    ; setup an int handler so we
    ; can use the frame interrupt to sync
    ; and play sounds in background
    ld a, >INT_VECT
    ld x, <INT_VECT
    ld b, <int_handler
    ld [a : x], b
    inc x
    ld b, >int_handler
    ld [a : x], b

    ; enable interrupts
    cif

    ld b, 0
loop:
    call fill_screen
    push b

    ; update sprite position

    ld a, >incx
    ld x, <incx
    ld y, [a : x]
    ld a, >px
    ld x, <px
    ld b, [a : x]
    ; px + incx
    add b, y
    ; 128 - 16
    cmp b, 112
    bnc
    jmp turn_x

    ; new x
    ld [a : x], b
    jmp update_y

turn_x:
    ld a, >incx
    ld x, <incx
    call turn

update_y:
    ld a, >incy
    ld x, <incy
    ld y, [a : x]
    ld a, >py
    ld x, <py
    ld b, [a : x]
    ; py + incy
    add b, y
    ; 128 - 16
    cmp b, 112
    bnc
    jmp turn_y

    ; new y
    ld [a : x], b
    jmp update_done

turn_y:
    ld a, >incy
    ld x, <incy
    call turn

update_done:

    call draw_sprite

    ; wait 1 frame
    halt

    pop b
    jmp loop

;
; Make the sprite change direction and queue a sound
;
; in: [a : x] inc*
turn:
    ;  1 -> -1
    ; -1 -> 1
    ld y, [a : x]
    ld b, 254
    xor y, b
    ld [a : x], y

    ; change color
    ld b, [sp + 2]
    inc b
    and b, 15
    ld [sp + 2], b

    ; play an effect when we turn
    ; alternating two sounds

    ld x, <effect
    ld a, >effect
    ld y, [a : x]
    xor y, 3
    ld [a : x], y

    call sound_queue_effect
    ret

;
; fill frame-buffer
;
; in: reg b color
fill_screen:
    ld a, 0xbf
    ld x, 0
    ld y, 0x40
fill_loop:
    ld [a : x], b
    inc x
    bno
    jmp fill_loop
    inc a
    dec y
    bnz
    jmp fill_loop
    ret

;
; Draw the sprite
;
; in: coordinates in px, py
draw_sprite:
    ; blitter in settings mode
    ld x, 128
    ld a, 0xb0
    port a, x

    ; setup
    inc a
    ; source
    ld x, <sprite
    port a, x
    ld x, >sprite
    port a, x
    ; destination
    ld y, <px
    ld b, >px
    ld x, [b : y]
    port a, x
    ld y, <py
    ld b, >py
    ld x, [b : y]
    port a, x
    ; size 16x16
    ld x, 16
    port a, x
    port a, x

    ; now blit
    dec a
    ld x, 3
    ; 3: write with transparent color support
    port a, x
    ret

sprite:
    .incpng "assets/icon.png"

incx:
    .db 1
px:
    .db 18
incy:
    .db 1
py:
    .db 64

effect:
    .db 1

;
; Our int handler plays / stops sounds
;
int_handler:
    push a
    push b
    push x
    push y
    call sound_play
    pop y
    pop x
    pop b
    pop a
    iret

;
; Queue a sound effect to be played
;
; in: y sound index (1 based)
sound_queue_effect:
    ; can't be interrupted
    sif

    ld a, >sound_efx
    ld x, <sound_efx
    dec y
    ld b, y
    add y, y
    add y, b
    add x, y
    bo
    inc a

    ; effect addr
    ld b, >sound_queue
    ld y, <sound_queue
    push x
    ld x, [a : x]
    ld [b : y], x
    pop x
    inc x
    bo
    inc a
    inc y
    bo
    inc b
    push x
    ld x, [a : x]
    ld [b : y], x
    pop x

    inc x
    bo
    inc a
    ; x has length
    ld x, [a : x]

    ; current is 0
    inc y
    bo
    inc b
    ld a, 0
    ld [b : y], a

    ; length
    inc y
    bo
    inc b
    ld [b : y], x

    cif
    ret

;
; Plays sound in our queue
;
; To be run from the interrupt handler. Can't play effects which data is at 0x0000.
; It is very simple and uses only channel 0.
;
sound_play:
    ld b, >sound_queue
    ld y, <sound_queue

    ; check we have a valid addr in the queue (not 0x0000)
    ld x, [b : y]
    inc y
    bo
    inc b
    ld a, [b : y]
    cmp a, 0
    bnz
    jmp sound_play_has_addr
    cmp x, 0
    bz
    ret

sound_play_has_addr:
    push a

    inc y
    bo
    inc b

    ; is already playing?
    ld a, [b : y]
    cmp a, 0
    bz
    jmp sound_play_start

    ; the sound is active
    pop a
    ld a, [b : y]

    ; another frame
    inc a
    ld [b : y], a

    inc y
    bo
    inc b

    ; is the end?
    ld x, [b : y]
    cmp x, a
    bnc
    ret

    ; no active sound now: set addr to 0x0000
    ld b, >sound_queue
    ld y, <sound_queue
    ld x, 0
    ld [b : y], x
    inc y
    bo
    inc b
    ld [b : y], x

    ; stop the sound
    ld b, 0xc0
    ld y, 0xb0
    port b, y

    ld b, 0xc1
    ld y, 0x11
    port b, y
    ret

sound_play_start:
    ; we start playing
    inc a
    ld [b : y], a

    pop a
    ; a : x has the effect addr

sound_loop:
    ld y, [a : x]
    cmp y, 0xff
    bz
    ret

    ld b, 0xc0
    port b, y

    inc x
    bo
    inc a

    ld y, [a : x]

    ld b, 0xc1
    port b, y

    inc x
    bo
    inc a

    jmp sound_loop

sound_queue:
    ; effect addr, current frame count, length
    .ds 4, 0

sound_efx:
    ; addr to the instrument data
    .dw sound_efx_data0
    ; length in frames
    .db 2
    .dw sound_efx_data1
    .db 2

sound_efx_data0:
    ; register, value
    .db 0x20, 0x01
    .db 0x40, 0x10
    .db 0x60, 0xF0
    .db 0x80, 0x77
    .db 0xA0, 0x81
    .db 0x23, 0x01
    .db 0x43, 0x00
    .db 0x63, 0xF0
    .db 0x83, 0x77
    .db 0xe0, 0x03
    .db 0xB0, 0x31
    ; end of sequence marker
    .db 0xff

sound_efx_data1:
    ; register, value
    .db 0x20, 0x01
    .db 0x40, 0x10
    .db 0x60, 0xF0
    .db 0x80, 0x77
    .db 0xA0, 0xb0
    .db 0x23, 0x01
    .db 0x43, 0x00
    .db 0x63, 0xF0
    .db 0x83, 0x77
    .db 0xe0, 0x03
    .db 0xB0, 0x31
    ; end of sequence marker
    .db 0xff