-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygame.asm
More file actions
329 lines (303 loc) · 5.29 KB
/
mygame.asm
File metadata and controls
329 lines (303 loc) · 5.29 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
INCLUDE "gbhw.inc" ; hardware defs from devrs.com
;sprite constants
_SPR0_Y EQU _OAMRAM
_SPR0_X EQU _OAMRAM+1
_SPR0_NUM EQU _OAMRAM+2
_SPR0_ATT EQU _OAMRAM+3
_SPR1_Y EQU _OAMRAM+4
_SPR1_X EQU _OAMRAM+5
_SPR1_NUM EQU _OAMRAM+6
_SPR1_ATT EQU _OAMRAM+7
;when to move the sprite
_MOVX EQU _RAM
_MOVY EQU _RAM+1
;Start program
SECTION "start", ROM0[$0100]
nop
jp begin ;jump over rom header to beginning
;ROM HEADER
NINTENDO_LOGO ; macro from gbhw
DB "MYGAME",0,0,0,0,0,0,0,0,0 ; cart name
DB 0
DB 0,0 ; licensee code
DB 0 ; SGB support indicator
DB 0 ; cart type
DB 0 ; ROM Size
DB 1 ; Destination Code
DB $33 ; Old Licensee code
DB 0 ; Mask ROM version
DB 0 ; Complement check
DW 0 ; Checksum
;END ROM HEADER
begin:
nop
di ; disable interupts
ld sp, $ffff ; set stack pointer to highest mem loc + 1
init:
ld a, %11100100 ; color pallet: 11, 10, 01, 00
ld [rBGP], a ; load pallet from a to rBGP
ld [rOBP0], a ;
ld a, 0
ld [rSCX], a ; set scroll x to 0
ld [rSCY], a ; set scroll y to 0
call StopLCD ; turn off LCD
ld hl, Tiles
ld de, _VRAM
ld b, 48 ; bytes to copy(3 tiles)
.load_loop:
ld a, [hl]
ld [de], a
dec b
jr z, .end_load_loop
inc hl
inc de
jr .load_loop
.end_load_loop:
ld hl, _SCRN0
ld de, 32*32
.clear_screen:
ld a, 0
ld [hl], a
dec de
ld a, d
or e
jp z, .end_clear_screen
inc hl
jp z, .clear_screen
.end_clear_screen
ld a, 30
ld [_SPR0_Y], a
ld a, 30
ld [_SPR0_X], a
ld a, 1
ld [_SPR0_NUM], a
ld a, 0
ld [_SPR0_ATT], a
ld a, 50
ld [_SPR1_Y], a
ld a, 50
ld [_SPR1_X], a
ld a, 2
ld [_SPR1_NUM], a
ld a, 0
ld [_SPR1_ATT], a
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_BGON|LCDCF_OBJ8|LCDCF_OBJON
ld [rLCDC], a
ld a, 1
ld [_MOVX], a
ld [_MOVY], a
animation:
.wait:
ld a, [rLY]
cp 145
jr nz, .wait
call ReadJoy
ld bc, $0fff
call Delay
jr animation
ReadJoy:
push bc ;save bc to stack
ld a, P1F_5 ;get joypad, macro from devrs
ld [rP1], a
ld a, [rP1] ;get keypress multple times to account for hardware
ld a, [rP1]
ld a, [rP1]
ld a, [rP1]
cpl ;invert
ld b, a
;test Right Key
and $01
cp $01
jr z, MoveRight
MoveRightRet:
;test Left key
ld a, b
and $02
cp $02
jp z, MoveLeft
MoveLeftRet:
;test down key
ld a, b
and $04
cp $04
jp z, MoveDown
MoveDownRet:
;test up key
ld a, b
and $08
cp $08
jp z, MoveUp
MoveUpRet:
pop bc ;return bc from stack
ret
MoveRight:
ld a, [_SPR0_X]
cp 160 ;compare with right side of screen
jr z, MoveRightRet ;don't move if on edge
ld a, [_SPR1_X]
sub 8
ld [hl], a
ld a, [_SPR0_X]
cp [hl]
jr nz, TestRightColRet
ld a, [_SPR0_Y]
ld c, a
ld a, [_SPR1_Y]
call TestCol
ld a, b
cp 0
jr z, PushRight
TestRightColRet:
ld a, [_SPR0_X]
inc a
ld [_SPR0_X], a
jr MoveRightRet
PushRight:
ld a, [_SPR1_X]
cp 160
jp z, MoveRightRet
inc a
ld [_SPR1_X], a
jp TestRightColRet
;collision testing
;pre condition:
; c: first sprite's coord (x or y)
; a: second sprite's coord (x or y)
;return:
; b: 0 if colliding
TestCol:
add 8
ld d, a
ld b, 14
.TestColLoop:
dec d
ld a, d
cp c
jr z, TestColTrue
dec b
jr z, TestColFalse
jr .TestColLoop
TestColTrue:
ld b, 0
ret
TestColFalse:
ld b, 1
ret
MoveLeft:
ld a, [_SPR0_X]
cp 8 ;compare with left side of screen
jp z, MoveLeftRet ;don't move if on edge
ld a, [_SPR1_X]
add 8
ld [hl], a
ld a, [_SPR0_X]
cp [hl]
jp nz, TestLeftColRet
ld a, [_SPR0_Y]
ld c, a
ld a, [_SPR1_Y]
call TestCol
ld a, b
cp 0
jr z, PushLeft
TestLeftColRet:
ld a, [_SPR0_X]
dec a
ld [_SPR0_X],a
jp MoveLeftRet
PushLeft:
ld a, [_SPR1_X]
cp 7
jp z, MoveLeftRet
dec a
ld [_SPR1_X], a
jr TestLeftColRet
MoveDown:
ld a, [_SPR0_Y]
cp 16 ;compare with bottom of screen
jp z, MoveDownRet ;don't move if on edge
ld a, [_SPR1_Y]
add 7
ld [hl], a
ld a, [_SPR0_Y]
cp [hl]
jr nz, TestDownColRet
ld a, [_SPR0_X]
ld c, a
ld a, [_SPR1_X]
call TestCol
ld a, b
cp 0
jr z, PushDown
TestDownColRet:
ld a, [_SPR0_Y]
dec a
ld [_SPR0_Y],a
jp MoveDownRet
PushDown:
ld a, [_SPR1_Y]
cp 15
jp z, MoveDownRet
dec a
ld [_SPR1_Y], a
jp TestDownColRet
MoveUp:
ld a, [_SPR0_Y]
cp 152 ;compare with top of screen
jp z, MoveUpRet ;don't move if on edge
ld a, [_SPR1_Y]
sub 7
ld [hl], a
ld a, [_SPR0_Y]
cp [hl]
jr nz, TestUpColRet
ld a, [_SPR0_X]
ld c, a
ld a, [_SPR1_X]
call TestCol
ld a, b
cp 0
jp z, PushUp
TestUpColRet:
ld a, [_SPR0_Y]
inc a
ld [_SPR0_Y],a
jp MoveUpRet
PushUp:
ld a, [_SPR1_Y]
cp 152
jp z, MoveUpRet
inc a
ld [_SPR1_Y], a
jp TestUpColRet
;delay, parameter in bc
Delay:
dec bc
ld a,b
or c
jr nz, Delay
ret
StopLCD:
ld a, [rLCDC]
rlca ; rotate 1 bit left, high bit into carry flag
ret nc ;return if carry flag = 0, meaning LCD off
.wait_Vblank:
ld a, [rLY]
cp 145 ; compare with 145, set flag bits
jr nz, .wait_Vblank ;if flag bits not zero, go back
ld a, [rLCDC]
res 7, a ;reset bit 7
ld [rLCDC], a ;load a back into rLCDC
ret ;return
Tiles:
;bg tile
DB $00, $00, $00, $00, $00, $00, $00, $00
DB $00, $00, $00, $00, $00, $00, $00, $00
;DB $AA, $14, $55, $28, $AA, $51, $55, $A2, $AA, $45, $55, $8A, $55, $8A, $AA, $14
;DB $00, $00, $80, $01, $80, $01, $80, $01, $80, $01, $80, $01, $80, $01, $00, $FF
;sprite
DB $00, $00, $42, $42, $42, $42, $00, $00
DB $18, $18, $99, $99, $66, $66, $00, $00
DB $FF, $FF, $BF, $C1, $9F, $E1, $8F, $F1
DB $87, $F9, $83, $FD, $81, $FF, $FF, $FF
EndTiles: