-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
executable file
·537 lines (461 loc) · 13.1 KB
/
main.asm
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
INCLUDE "gbhw.inc" ; standard hardware definitions from devrs.com
INCLUDE "ibmpc1.inc" ; ASCII character set from devrs.com
INCLUDE "sprite.inc" ; specific defs
; constants
PLXStart EQU $0a * 8 ; 80, $50
PLXEnd EQU $0f * 8 ; 120, $78
PLXLength EQU PLXEnd - PLXStart ; 40
PLXOffset EQU 75
ScreenHeight EQU $12 * 8
MusicRAMLength EQU $80
; RAM variables
; music RAM section must not move from this offset
SECTION "Music RAM", WRAM0[$c000]
MusicRAM:
GBCFlag: DS 1 ;c000
GBAFlag: DS 1
SndEnabled: DS 1
ROMBank: DS 1
Unk04: DS 1 ;c004
Unk05: DS 1
Unk06: DS 1
Unk07: DS 1
Unk08: DS 1 ;c008
Unk09: DS 1
MusicRAMEnd:
DS MusicRAMLength-(MusicRAMEnd-MusicRAM) ; pad to size
SECTION "Regular RAM", WRAM0
VBLANKED: DS 1
scrollX: DS 2
SECTION "Parallax Table", WRAM0, ALIGN[8]
plxTable: DS PLXLength ; $100-aligned
; WRAMX variables. first $a0 bytes are Fake OAM
SECTION "RAM1", WRAM0[$d000]
SpriteAttr Sprite0
OAMData: DS $a0 ; shitty macro protection
; RST jump vectors
SECTION "Org $0000", ROM0[$0000]
RST_00: ld b, b
SECTION "Org $0008", ROM0[$0008]
RST_08: ld b, b
SECTION "Org $0010", ROM0[$0010]
RST_10: ld b, b
SECTION "Org $0018", ROM0[$0018]
RST_18: ld b, b
SECTION "Org $0020", ROM0[$0020]
RST_20: ld b, b
SECTION "Org $0028", ROM0[$0028]
RST_28: ld b, b
SECTION "Org $0030", ROM0[$0030]
RST_30: ld b, b
SECTION "Org $0038", ROM0[$0038]
RST_38: ld b, b
; Interrupts
SECTION "VBlank", ROM0[$0040]
; copy OAM, though it's a frame out of date
; this also sets VBLANKED so that when the DMA is RETI'd
; the game logic after MainLoop's HALT will execute
jp DMACODELOC
SECTION "LCDC", ROM0[$0048]
jp LCDC_STAT
SECTION "Timer", ROM0[$0050]
reti
SECTION "Serial", ROM0[$0058]
reti
SECTION "p1thru4", ROM0[$0060]
reti
; boot loader jumps to here.
SECTION "start", ROM0[$0100]
nop
jp entry_point
; *****************************************************************************
; header and and hardcoded data
; *****************************************************************************
SECTION "header", ROM0[$0104]
; $0104-$0133 (Nintendo logo - do _not_ modify the logo data here or the GB
; will not run the program)
NINTENDO_LOGO
; $0134-$013E (Game title - up to 11 upper case ASCII characters; pad with $00)
; 0123456789ABCDE
db "AvgDayCowboyV01"
; $0143 (Game Boy Color compatibility code)
db $00 ; $00 - DMG
; $80 - DMG/GBC
; $C0 - GBC Only cartridge
; $0144 (High-nibble of license code - normally $00 if $014B != $33)
db 0
; $0145 (Low-nibble of license code - normally $00 if $014B != $33)
db 0
; $0146 (Game Boy/Super Game Boy indicator)
db 0
; $0147 (Cartridge type - all Game Boy Color cartridges are at least $19)
db $19 ; $19 - MBC5
; $0148 (ROM size)
db $4 ; $4 = 512Kb (32 banks)
; $0149 (RAM size)
db 0 ; $00 - None
; $014A (Destination code)
db 1 ; $01 - All others
; $00 - Japan
; $014B (Licensee code - this _must_ be $33)
db $33 ; $33 - Check $0144/$0145 for Licensee code.
; $014C (Mask ROM version)
db 0
; $014D (Complement check - handled by post-linking tool)
db 0
; $014E-$014F (Cartridge checksum - handled by post-linking tool)
dw 0
SECTION "Music in ROM0", ROM0[$0500]
MusicLoad EQU $0500 ; deflemask-generated .GBS have procedures
MusicInit EQU $05ec ; at these locations
MusicPlay EQU $0544 ; see also: https://github.com/DevEd2/Deflemask2GB
; music code
; darkman.gbs
;
; size $1eaf (7855_10)
; offset $0070
; includedSize $1e3f
;incbin "darkman.gbs",$70,$1e3f
; industrial.gbs
;
; size $15633 (87603_10)
; | offset | bank size | bytes left
; -------+--------+-----------+------------
; bank 0 | $00070 | $3b00* | $11ac3 = $15633-$3b00-$70
; bank 1 | $03b70 | $4000 | $0dac3 = $11ac3-$4000
; bank 2 | $07b70 | $4000 | $09ac3 = $0dac3-$4000
; bank 3 | $0bb70 | $4000 | $05ac3 = $09ac3-$4000
; bank 4 | $0fb70 | $4000 | $01ac3 = $05ac3-$4000
; bank 5 | $13b70 | $1ac3 | 0
; total | $15633 (size ok)
;
; *$3b00 == bankSize-musicStartAddr == $4000-$0500
incbin "industrial.gbs",$00070, $3b00
SECTION "Music in ROM1", ROMX[$4000], BANK[1]
ROM1_Music:
incbin "industrial.gbs",$03b70, $4000
SECTION "Music in ROM2", ROMX[$4000], BANK[2]
ROM2_Music:
incbin "industrial.gbs",$07b70, $4000
SECTION "Music in ROM3", ROMX[$4000], BANK[3]
ROM3_Music:
incbin "industrial.gbs",$0bb70, $4000
SECTION "Music in ROM4", ROMX[$4000], BANK[4]
ROM4_Music:
incbin "industrial.gbs",$0fb70, $4000
SECTION "Music in ROM5", ROMX[$4000], BANK[5]
ROM5_Music:
incbin "industrial.gbs",$13b70, $1ac3
SECTION "ROM data", ROMX, BANK[10]
INCLUDE "memory.asm"
TileData:
PUSHO
OPT b.X
DB %.XXXXXX.
DB %X......X
DB %X.X..X.X
DB %X......X
DB %X.XXXX.X
DB %X..XX..X
DB %X......X
DB %.XXXXXX.
POPO
TileDataEnd:
Title:
; [ ] 20tiles
DB $85,$86,$87,$88,$85,$86,$87,$88,$85,$86,$87,$88,$85,$86,$87,$88,$85,$86,$87,$88,$85,$86,$87,$88,$85,$86,$87,$88,$85,$86,$87,$88
DB $89,$8a,$8b,$8c,$89,$8a,$8b,$8c,$89,$8a,$8b,$8c,$89,$8a,$8b,$8c,$89,$8a,$8b,$8c,$89,$8a,$8b,$8c,$89,$8a,$8b,$8c,$89,$8a,$8b,$8c
DB $8d,$8e,$8f,$90,$8d,$8e,$8f,$90,$8d,$8e,$8f,$90,$8d,$8e,$8f,$90,$8d,$8e,$8f,$90,$8d,$8e,$8f,$90,$8d,$8e,$8f,$90,$8d,$8e,$8f,$90
DB $82,$81,$82,$80,$80,$81,$82,$81,$80,$82,$80,$81,$80,$80,$80,$82,$82,$80,$81,$82,$82,$80,$82,$82,$82,$82,$81,$80,$82,$80,$80,$81
DB $81,$80,$81,$80,$82,$82,$82,$81,$80,$82,$81,$82,$80,$81,$80,$81,$81,$81,$80,$81,$81,$80,$81,$80,$82,$82,$82,$81,$82,$82,$82,$82
DB $82,$80,$82,$81,$82,$82,$80,$81,$82,$81,$80,$81,$81,$80,$80,$81,$81,$82,$80,$82,$80,$82,$80,$81,$82,$80,$80,$80,$80,$80,$81,$80
DB $81,$82,$82,$81,$82,$81,$82,$80,$80,$82,$82,$82,$80,$82,$80,$82,$81,$81,$82,$81,$80,$80,$81,$80,$80,$80,$80,$80,$80,$81,$80,$80
DB $80,$80,$80,$82,$81,$80,$82,$81,$81,$81,$80,$80,$81,$81,$80,$82,$82,$82,$80,$80,$80,$81,$82,$82,$82,$80,$81,$81,$80,$80,$82,$81
DB $83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83,$83
DB $84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84,$84
DB $81,$82,$80,$82,$82,$81,$82,$81,$82,$81,$82,$80,$81,$82,$80,$81,$80,$80,$82,$80,$80,$82,$80,$80,$81,$81,$81,$81,$80,$81,$80,$82
TitleEnd:
GameTile:
GameTile_Dirt0: incbin "tile_dirt0.png.2bp"
GameTile_Dirt1: incbin "tile_dirt1.png.2bp"
GameTile_Dirt2: incbin "tile_dirt2.png.2bp"
GameTile_TrackHi: incbin "tile_trackhi.png.2bp"
GameTile_TrackLow: incbin "tile_tracklow.png.2bp"
GameTileMnt: incbin "tile_mnt1.png.2bp"
GameTileMntEnd:
GameTileEnd:
; *****************************************************************************
; Initialization
; *****************************************************************************
BankSelect EQU $2000
SECTION "Init bootstrap code", ROM0
entry_point:
nop
ld a, BANK("Init code")
ld [BankSelect], a
jp init
SECTION "Music init bootstrap", ROM0
music_init_bootstrap:
nop
ld a, 1
ld [BankSelect], a
; the init funcs don't set any registers before using them. undefined
; behavior? zeroing out seems to work.
xor a
ld b,a
ld c,a
ld d,a
ld e,a
ld h,a
ld l,a
call MusicLoad
call MusicInit
ld a, 10
ld [BankSelect], a
ret
SECTION "Init code", ROMX, BANK[10]
init:
nop
di
ld sp, $ffff ; set the stack pointer to highest mem location + 1
call initdma ; move routine to HRAM
ld a, %11100100 ; Window palette colors, from darkest to lightest
ld [rBGP], a ; set background and window pallette
ldh [rOBP0], a ; set sprite pallette 0
ldh [rOBP1], a ; 1 (choose palette 0 or 1 when describing the sprite)
ld a, 0 ; SET SCREEN TO TO UPPER RIGHT HAND CORNER
ld [rSCX], a
ld [rSCY], a
call StopLCD ; YOU CAN NOT LOAD $8000 WITH LCD ON
ld hl, TileData
ld de, _VRAM ; $8000
ld bc, TileDataEnd - TileData
call mem_CopyMono ; load tile data
ld hl, GameTile
ld de, _VRAM + $800
ld bc, GameTileEnd - GameTile
call mem_CopyVRAM
; clear out OAM (in RAM)
ld a, 0
ld hl, OAMDATALOC
ld bc, OAMDATALENGTH
call mem_Set
; DMA OAM
call DMACODELOC
; clear tiles
ld a, 32 ; ascii for space
ld hl, _SCRN0
ld bc, SCRN_VX_B * SCRN_VY_B
call mem_SetVRAM
; write those tiles from ROM!
ld hl,Title
ld de, _SCRN0+(SCRN_VX_B * 7) ; first 7 rows are blank
ld bc, TitleEnd-Title
call mem_CopyVRAM
; re-disable interrupts
di ; before we set IE, lots of the above macros/funcs re-enabled
ld a, [rIF] ; IME with reti or ei.
res IEF_VBLANK, a ; we don't want vblank before we're done with initialization.
res IEF_LCDC, a ; while we're at it, clear stale LCD interrupt flags
ld [rIF], a
; enable vblank and LCDC interrupts
ld a, IEF_LCDC | IEF_VBLANK
ld [rIE], a
; enable y-coincidence interrupt for LCDC
ld a, $ff
ld [rLYC], a ; LY never reaches above 153 ($99) so === disabled
ld a, STATF_LYC
ld [rSTAT], a
; start screen again
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BGON|LCDCF_OBJ8|LCDCF_OBJON
ld [rLCDC], a
; zero out WRAM things
xor a
ld [VBLANKED], a
ld [scrollX], a
ld [scrollX+1], a
ld hl, plxTable
ld bc, PLXLength
call mem_Set
; sprite metadata
PutSpriteYAddr Sprite0, 0 ; necessary because X=Y=$00 is offscreen
PutSpriteXAddr Sprite0, 0
ld a, 0 ; happy face :-)
ld [Sprite0TileNum], a
; music RAM and code init
xor a
ld hl, MusicRAM
ld bc, MusicRAMLength
call mem_Set
; GBCFlag = 0, GBAFlag = 0
ld a, 1
ld [SndEnabled], a
call music_init_bootstrap
; all done with setup, begin interrupts
; (even though the music funcs already did, most likely)
; also, because I DMA'd earlier, need to reset this flag
; because we're likely halfway down the screen instead
xor a
ld [VBLANKED], a
; also also, we don't use the timer for anything,
; but the music init funcs enable it, for some reason.
; it fucks with our interrupts, so turn it off
ld hl, rIE
res 2, [hl]
ld hl, rTAC
res 2, [hl]
; begin interrupts, then move on to mainloop
ei
jp MainLoop
Section "Loop code", ROM0
MainLoop:
halt
nop ; always put NOP after HALT
ld a, [VBLANKED]
; TODO: vblank can execute right here and cause a skipped frame...
or a
jr z, MainLoop ; halt-loop until vblank
xor a
ld [VBLANKED], a
call MusicPlay ; every frame, jump into the .gbs file to play music
; 16-bit scroller variable increment
ld a, [scrollX]
ld c, a
ld a, [scrollX+1]
ld b, a
inc bc
ld a, b
ld [scrollX+1], a
ld a, c
ld [scrollX], a
srl b
rr c
srl b
rr c
ld a, c
ld [rSCX], a ; mountains move at 1/4 pixels per second
call PLXTable ; generate parallax table
ld a, PLXStart
ld [rLYC], a ; interrupt when rLY is at the first parallax line
call GetKeys
push af
and PADF_RIGHT
call nz,right
pop af
push af
and PADF_LEFT
call nz,left
pop af
push af
and PADF_UP
call nz,up
pop af
push af
and PADF_DOWN
call nz,down
pop af
push af
and PADF_START
call nz,Yflip
pop af
jr MainLoop
right:
GetSpriteXAddr Sprite0
cp SCRN_X - 8 ; already on RHS of screen?
ret z
inc a
PutSpriteXAddr Sprite0, a
ret
left:
GetSpriteXAddr Sprite0
cp 0 ; already on LHS of screen?
ret z
dec a
PutSpriteXAddr Sprite0, a
ret
up:
GetSpriteYAddr Sprite0
cp 0 ; already at top of screen?
ret z
dec a
PutSpriteYAddr Sprite0, a
ret
down:
GetSpriteYAddr Sprite0
cp SCRN_Y - 8 ; already at bottom of screen?
ret z
inc a
PutSpriteYAddr Sprite0, a
ret
Yflip:
ld a, [Sprite0Flags]
xor OAMF_YFLIP ; toggle flip of sprite vertically
ld [Sprite0Flags], a
ret
; *****************************************************************************
; PLXTable - compute each scanline's scroll value
; *****************************************************************************
PLXTable::
xor a ; a = first scanline
ld hl, plxTable
.loop:
push af ; save relative scanline
add PLXStart ; make it absolute scanline
push hl
sub PLXOffset
ld h, a ; h = scanline - 75
ld a, [scrollX]
ld e, a
ld a, [scrollX+1]
ld d, a ; de = scrollX 16bit
ld a, h ; a = (scanline - 75)
sra a ; a = (scanline - 75) >> 1
ld c, a ; c = ^^
ld b, 0
call Mul16 ; hl = (scrollX * (scanline - 75) >> 1)
srl h
rr l
srl h
rr l
srl h
rr l ; hl = (scrollX * (scanline - 75) >> 1) >> 3
ld a, l ; a = LSB of that function
pop hl
ld [hl+], a ; save to ram at index, increment hl
ld [hl+], a ; each scroll layer is 2px
ld [hl+], a ; layer is 3px... comment me out if you can spare cycles
ld [hl+], a ; layer is 4px... comment me out if you can spare cycles
pop af ; restore relative scanline
inc a
inc a
inc a ; 3px comment me out if you can spare cycles
inc a ; 4px comment me out if you can spare cycles
cp a, PLXLength
jr nz, .loop
ret
LCDC_STAT:
push af
push hl
.wait_hblank: ; spinloop until hblank
ld a, [rSTAT]
and %00000011 ; if LCD mode bits are set (ie not in mode 0)
jp nz, .wait_hblank
ld a, [rLY] ; read scanline
ld h, PLXEnd
cp h ; if scanline >= LayerTrackStart, go to the end
jp NC, endLCDC ; rLYC isn't updated, so this ends parallax til vblank
; todo: any optimizations from $100-alignment?
sub PLXStart ; a = index
ld hl, plxTable ; hl = scroll table start
add a, l ; hl += index
ld l, a ; (which is low enough that this add doesn't overflow)
ld a, [hl] ; load from scroll table
ld [rSCX], a ; scroll the window
ld a, [rLYC]
add a, 4
ld [rLYC], a ; see you again in 4 lines
endLCDC:
pop hl
pop af
reti
INCLUDE "algorithms.asm"