-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmain.asm
More file actions
428 lines (400 loc) · 8.61 KB
/
main.asm
File metadata and controls
428 lines (400 loc) · 8.61 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
INCLUDE "hardware.inc"
SECTION "Header", ROM0[$100]
jp EntryPoint
ds $150 - @, 0 ; Make room for the header
EntryPoint:
; Do not turn the LCD off outside of VBlank
WaitVBlank:
ld a, [rLY]
cp 144
jp c, WaitVBlank
; Turn the LCD off
ld a, 0
ld [rLCDC], a
; ANCHOR: copy_tiles
; Copy the tile data
ld de, Tiles
ld hl, $9000
ld bc, TilesEnd - Tiles
call MemCopy
; ANCHOR_END: copy_tiles
; ANCHOR: copy_map
; Copy the tilemap
ld de, Tilemap
ld hl, $9800
ld bc, TilemapEnd - Tilemap
call MemCopy
; ANCHOR_END: copy_map
; ANCHOR: copy_paddle
; Copy the paddle tile
ld de, Paddle
ld hl, $8000
ld bc, PaddleEnd - Paddle
call MemCopy
; ANCHOR_END: copy_paddle
xor a, a
ld b, 160
ld hl, STARTOF(OAM)
ClearOam:
ld [hli], a
dec b
jp nz, ClearOam
ld hl, STARTOF(OAM)
ld a, 128 + 16
ld [hli], a
ld a, 16 + 8
ld [hli], a
ld a, 0
ld [hli], a
ld [hl], a
; Turn the LCD on
ld a, LCDC_ON | LCDC_BG_ON | LCDC_OBJ_ON
ld [rLCDC], a
; During the first (blank) frame, initialize display registers
ld a, %11100100
ld [rBGP], a
ld a, %11100100
ld [rOBP0], a
; ANCHOR: initialize-vars
; Initialize global variables
ld a, 0
ld [wFrameCounter], a
ld [wCurKeys], a
ld [wNewKeys], a
; ANCHOR_END: initialize-vars
; ANCHOR: main
Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
; Check the current keys every frame and move left or right.
call UpdateKeys
; First, check if the left button is pressed.
CheckLeft:
ld a, [wCurKeys]
and a, PAD_LEFT
jp z, CheckRight
Left:
; Move the paddle one pixel to the left.
ld a, [STARTOF(OAM) + 1]
dec a
; If we've already hit the edge of the playfield, don't move.
cp a, 15
jp z, Main
ld [STARTOF(OAM) + 1], a
jp Main
; Then check the right button.
CheckRight:
ld a, [wCurKeys]
and a, PAD_RIGHT
jp z, Main
Right:
; Move the paddle one pixel to the right.
ld a, [STARTOF(OAM) + 1]
inc a
; If we've already hit the edge of the playfield, don't move.
cp a, 105
jp z, Main
ld [STARTOF(OAM) + 1], a
jp Main
; ANCHOR_END: main
; ANCHOR: input-routine
UpdateKeys:
; Poll half the controller
ld a, JOYP_GET_BUTTONS
call .onenibble
ld b, a ; B7-4 = 1; B3-0 = unpressed buttons
; Poll the other half
ld a, JOYP_GET_CTRL_PAD
call .onenibble
swap a ; A7-4 = unpressed directions; A3-0 = 1
xor a, b ; A = pressed buttons + directions
ld b, a ; B = pressed buttons + directions
; And release the controller
ld a, JOYP_GET_NONE
ldh [rJOYP], a
; Combine with previous wCurKeys to make wNewKeys
ld a, [wCurKeys]
xor a, b ; A = keys that changed state
and a, b ; A = keys that changed to pressed
ld [wNewKeys], a
ld a, b
ld [wCurKeys], a
ret
.onenibble
ldh [rJOYP], a ; switch the key matrix
call .knownret ; burn 10 cycles calling a known ret
ldh a, [rJOYP] ; ignore value while waiting for the key matrix to settle
ldh a, [rJOYP]
ldh a, [rJOYP] ; this read counts
or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys
.knownret
ret
; ANCHOR_END: input-routine
; ANCHOR: memcpy
; Copy bytes from one area to another.
; @param de: Source
; @param hl: Destination
; @param bc: Length
MemCopy:
ld a, [de]
ld [hli], a
inc de
dec bc
ld a, b
or a, c
jp nz, MemCopy
ret
; ANCHOR_END: memcpy
Tiles:
dw `33333333
dw `33333333
dw `33333333
dw `33322222
dw `33322222
dw `33322222
dw `33322211
dw `33322211
dw `33333333
dw `33333333
dw `33333333
dw `22222222
dw `22222222
dw `22222222
dw `11111111
dw `11111111
dw `33333333
dw `33333333
dw `33333333
dw `22222333
dw `22222333
dw `22222333
dw `11222333
dw `11222333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33322211
dw `33322211
dw `33322211
dw `33322211
dw `33322211
dw `33322211
dw `33322211
dw `33322211
dw `22222222
dw `20000000
dw `20111111
dw `20111111
dw `20111111
dw `20111111
dw `22222222
dw `33333333
dw `22222223
dw `00000023
dw `11111123
dw `11111123
dw `11111123
dw `11111123
dw `22222223
dw `33333333
dw `11222333
dw `11222333
dw `11222333
dw `11222333
dw `11222333
dw `11222333
dw `11222333
dw `11222333
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `11001100
dw `11111111
dw `11111111
dw `21212121
dw `22222222
dw `22322232
dw `23232323
dw `33333333
; My custom logo (tail)
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33302333
dw `33333133
dw `33300313
dw `33300303
dw `33013330
dw `30333333
dw `03333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `03333333
dw `30333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333330
dw `33333320
dw `33333013
dw `33330333
dw `33100333
dw `31001333
dw `20001333
dw `00000333
dw `00000033
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33330333
dw `33300333
dw `33333333
dw `33033333
dw `33133333
dw `33303333
dw `33303333
dw `33303333
dw `33332333
dw `33332333
dw `33333330
dw `33333300
dw `33333300
dw `33333100
dw `33333000
dw `33333000
dw `33333100
dw `33333300
dw `00000001
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `10000333
dw `00000033
dw `00000003
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `33332333
dw `33302333
dw `32003333
dw `00003333
dw `00003333
dw `00013333
dw `00033333
dw `00033333
dw `33333300
dw `33333310
dw `33333330
dw `33333332
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `30000000
dw `33000000
dw `33333000
dw `33333333
dw `00000000
dw `00000000
dw `00000000
dw `00000003
dw `00000033
dw `00003333
dw `02333333
dw `33333333
dw `00333333
dw `03333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
dw `33333333
TilesEnd:
Tilemap:
db $00, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $02, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $0A, $0B, $0C, $0D, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $0E, $0F, $10, $11, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $12, $13, $14, $15, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $16, $17, $18, $19, $03, 0,0,0,0,0,0,0,0,0,0,0,0
db $04, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
TilemapEnd:
Paddle:
dw `33333333
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
dw `00000000
PaddleEnd:
SECTION "Counter", WRAM0
wFrameCounter: db
; ANCHOR: vars
SECTION "Input Variables", WRAM0
wCurKeys: db
wNewKeys: db
; ANCHOR_END: vars