-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.asm
612 lines (524 loc) · 12.7 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
; =======================
; Rebound GBC main source
; =======================
; If set to 1, enable debugging features.
DebugMode = 1
; Defines
include "Defines.asm"
; =============
; Reset vectors
; =============
section "Reset $00",rom0[$00]
Bankswitch:: jp _Bankswitch
section "Reset $08",rom0[$08]
FillRAM:: jp _FillRAM
section "Reset $10",rom0[$10]
WaitVBlank:: jp _WaitVBlank
section "Reset $18",rom0[$18]
WaitTimer:: jp _WaitTimer
section "Reset $20",rom0[$20]
WaitLCDC:: jp _WaitLCDC
section "Reset $30",rom0[$30]
DoOAMDMA:: jp OAM_DMA
section "Reset $38",rom0[$38]
Trap:: jr @
; ==================
; Interrupt handlers
; ==================
section "VBlank IRQ",rom0[$40]
IRQ_VBlank:: jp DoVBlank
section "STAT IRQ",rom0[$48]
IRQ_Stat:: jp DoStat
section "Timer IRQ",rom0[$50]
IRQ_Timer:: jp DoTimer
section "Serial IRQ",rom0[$58]
IRQ_Serial:: jp DoSerial
section "Joypad IRQ",rom0[$60]
IRQ_Joypad:: jp DoJoypad
; ===============
; System routines
; ===============
; ================================================================
; Call HL
; ================================================================
_CallHL:
ld a,h
bit 7,a
jr nz,@ ; trap
.skip
jp hl
; ==========
; ROM header
; ==========
section "ROM header",rom0[$100]
EntryPoint::
nop
jp ProgramStart
if !def(BUILD_POCKET)
NintendoLogo: ds 48,0 ; Nintendo logo bitmap (handled by post-linking tool)
else
AnalogueLogo:
db $01,$10,$CE,$EF,$00,$00,$44,$AA,$00,$74,$00,$18,$11,$95,$00,$34
db $00,$1A,$00,$D5,$00,$22,$00,$69,$6F,$F6,$F7,$73,$09,$90,$E1,$10
db $44,$40,$9A,$90,$D5,$D0,$44,$30,$A9,$21,$5D,$48,$22,$E0,$F8,$60
endc
ROMTitle: dbp "TURBOCHARGED",15,0 ; ROM title (15 bytes)
GBCSupport: db $00 ; GBC support (0 = DMG only, $80 = DMG/GBC, $C0 = GBC only)
NewLicenseCode: db "56" ; new license code (2 bytes)
SGBSupport: db 0 ; SGB support
CartType: db $19
ROMSize: db 0 ; ROM size (handled by post-linking tool)
RAMSize: db 0
DestCode: db 1 ; Destination code (0 = Japan, 1 = All others)
OldLicenseCode: db $33 ; Old license code (if $33, check new license code)
ROMVersion: db 0 ; ROM version
HeaderChecksum: db 0 ; Header checksum (handled by post-linking tool)
ROMChecksum: dw 0 ; ROM checksum (2 bytes) (handled by post-linking tool)
; =====================
; Start of program code
; =====================
section fragment "Program code",rom0[$150]
ProgramStart::
di
ld sp,$c800
; preserve A and B for later to determine console type
push bc
push af
; clear HRAM
ld bc,$7f80
xor a
.loop
ld [c],a
inc c
dec b
jr nz,.loop
call CopyDMARoutine
; check GB type
; sets sys_GBType to 0 if DMG/SGB/GBP/GBL/SGB2, 1 if GBC, 2 if GBA/GBA SP/GB Player
pop af
pop bc
and 1 ; a = 0 on DMG, 1 on GBC
jr z,:+
add b
: ld [sys_GBType],a
ld a,LCDCF_ON | LCDCF_BG8000 | LCDCF_BGON
ldh [rLCDC],a
ld a,IEF_VBLANK | IEF_TIMER | IEF_STAT
ldh [rIE],a
:
; clear vars
xor a
ld [sys_CurrentFrame],a
ld [sys_btnPress],a
ld [sys_btnHold],a
ld [sys_VBlankFlag],a
ld [sys_TimerFlag],a
ld [sys_LCDCFlag],a
jp GM_Intro
; ================================
include "Engine/GameModes/Intro.asm"
; ================================
; ==================
; Interrupt handlers
; ==================
section "Interrupt handlers",rom0
DoVBlank::
push af
push bc
push de
push hl
ld a,1
ld [sys_VBlankFlag],a ; set VBlank flag
ld a,[sys_CurrentFrame]
inc a
ld [sys_CurrentFrame],a ; tick frame counter
call Intro_RunCheckerboard
:
; check input
ld a,[sys_btnHold]
ld c,a
ld a,P1F_5
ldh [rP1],a
ldh a,[rP1]
ldh a,[rP1]
cpl
and $f
swap a
ld b,a
ld a,P1F_4
ldh [rP1],a
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1]
cpl
and $f
or b
ld b,a
ld a,[sys_btnHold]
xor b
and b
ld [sys_btnPress],a ; store buttons pressed this frame
ld e,a
ld a,b
ld [sys_btnHold],a ; store held buttons
xor c
xor e
ld [sys_btnRelease],a ; store buttons released this frame
ld a,P1F_5|P1F_4
ldh [rP1],a
rst DoOAMDMA
xor a
ldh [rLYC],a
pop hl
pop de
pop bc
pop af
reti ; done!
DoStat::
push af
ld a,1
ld [sys_LCDCFlag],a
call Intro_RunCheckerboardLCD
pop af
reti
DoTimer::
ei ; natt wants scanline interrupts for the checkerboard :V
push af
ld a,[GBM_EnableTimer]
and a
jr z,:+
push bc
push de
push hl
call GBM_Update
pop hl
pop de
pop bc
: ld a,1
ld [sys_TimerFlag],a
pop af
ret
; Currently not used
DoSerial::
push af
ld a,1
ld [sys_SerialFlag],a
pop af
reti
; Currently not used
DoJoypad::
push af
ld a,1
ld [sys_JoypadFlag],a
pop af
reti
; =======================
; Interrupt wait routines
; =======================
; Wait for vertical blank.
_WaitVBlank::
push af
ldh a,[rIE]
bit 0,a
jr z,.done
.wait
halt
ld a,[sys_VBlankFlag]
and a
jr z,.wait
xor a
ld [sys_VBlankFlag],a
.done
pop af
ret
; Wait for LCD status interrupt.
_WaitLCDC::
push af
ldh a,[rIE]
bit 1,a
jr z,.done
.wait
halt
ld a,[sys_LCDCFlag]
and a
jr z,.wait
xor a
ld [sys_LCDCFlag],a
.done
pop af
ret
; Wait for timer interrupt. (Currently unused)
_WaitTimer::
push af
ldh a,[rIE]
bit 2,a
jr z,.done
.wait
halt
ld a,[sys_TimerFlag]
and a
jr z,.wait
xor a
ld [sys_VBlankFlag],a
.done
pop af
ret
; Wait for serial transfer interrupt. (Currently unused)
_WaitSerial::
push af
ldh a,[rIE]
bit 3,a
jr z,.done
.wait
halt
ld a,[sys_SerialFlag]
and a
jr z,.wait
xor a
ld [sys_SerialFlag],a
.done
pop af
ret
; Wait for joypad interrupt. (Currently unused)
_WaitJoypad:
push af
ldh a,[rIE]
bit 4,a
jr z,.done
.wait
halt
ld a,[sys_JoypadFlag]
and a
jr z,.wait
xor a
ld [sys_JoypadFlag],a
.done
pop af
ret
; =================
; Graphics routines
; =================
; =========
; Clear the screen.
; TRASHES: a, bc, hl
; RESTRICTIONS: Requires the LCD to be disabled, otherwise screen will not be properly cleared.
ClearScreen:
; clear VRAM bank 0
xor a
ldh [rVBK],a
ld hl,_VRAM ; clear from start of VRAM...
ld bc,_SRAM-_VRAM ; ...to end of VRAM.
rst FillRAM
xor a
ld hl,_VRAM ; clear from start of VRAM...
ld bc,_SRAM-_VRAM ; ...to end of VRAM.
rst FillRAM
; clear OAM
ld hl,OAMBuffer
ld b,OAMBuffer.end-OAMBuffer
xor a
call _FillRAMSmall
; reset scrolling
xor a
ldh [rSCX],a
ldh [rSCY],a
ret
; Same as ClearScreen, but preserves loaded graphics.
ClearScreen2:
; clear VRAM bank 0
xor a
ldh [rVBK],a
ld hl,_SCRN0 ; clear from start of VRAM...
ld bc,_SRAM-_SCRN0 ; ...to end of VRAM.
rst FillRAM
; clear VRAM bank 1
xor a
ld hl,_SCRN0 ; clear from start of VRAM...
ld bc,_SRAM-_SCRN0 ; ...to end of VRAM.
rst FillRAM
; clear OAM
ld hl,OAMBuffer
ld b,OAMBuffer.end-OAMBuffer
xor a
call _FillRAMSmall
; reset scrolling
xor a
ldh [rSCX],a
ldh [rSCY],a
ret
; Loads a 20x18 tilemap to VRAM.
; INPUT: hl = source
; TRASHES: a, bc, de, hl
; RESTRICTIONS: Must run during VBlank or while VRAM is accessible, otherwise written data will be corrupted
LoadTilemapScreen:
ld de,_SCRN0
lb bc,$12,$14
.loop
ld a,[hl+]
ld [de],a
inc de
dec c
jr nz,.loop
ld c,$14
ld a,e
add $c
jr nc,.continue
inc d
.continue
ld e,a
dec b
jr nz,.loop
ret
; Same as LoadTilemapScreen, but performs ASCII conversion.
; INPUT: hl = source
; TRASHES: a, bc, de, hl
; RESTRICTIONS: Must run during VBlank or while VRAM is accessible, otherwise written data will be corrupted
LoadTilemapText:
ld de,_SCRN0
lb bc,$12,$14
.loop
ld a,[hl+]
sub " "
ld [de],a
inc de
dec c
jr nz,.loop
ld c,$14
ld a,e
add $C
jr nc,.continue
inc d
.continue
ld e,a
dec b
jr nz,.loop
ret
; Prints a null-terminated string.
; INPUT: hl = source
; de = destination
PrintString:
WaitForVRAM
ld a,[hl+]
and a ; terminator byte reached?
ret z ; if yes, return
sub " "
ld [de],a
inc de
jr PrintString
; ============
; Sprite stuff
; ============
; Copies OAM DMA routine to HRAM.
; Called once during startup sequence.
; TRASHES: a, bc, hl
CopyDMARoutine::
ld bc,low(OAM_DMA) + ((_OAM_DMA_End-_OAM_DMA) << 8)
ld hl,_OAM_DMA
.loop
ld a,[hl+]
ld [c],a
inc c
dec b
jr nz,.loop
ret
; OAM DMA routine.
; This is copied to HRAM by CopyDMARoutine and run from there.
; TRASHES: a
_OAM_DMA::
ld a,high(OAMBuffer)
ldh [rDMA],a
ld a,$28
.wait ; wait for OAM DMA to finish
dec a
jr nz,.wait
ret
_OAM_DMA_End:
; =============
; Misc routines
; =============
; include "Engine/GBPrinter.asm"
; ================
; Performs a bankswitch to bank B, preserving previous ROM bank.
; INPUT: b = bank
_Bankswitch:
push af
ldh a,[sys_CurrentBank]
ldh [sys_LastBank],a ; preserve old ROM bank
ld a,b
ldh [sys_CurrentBank],a ; set new ROM bank
ld [rROMB0],a ; perform bankswitch
pop af
ret
; Fill RAM with a value.
; INPUT: a = value
; hl = address
; bc = size
; TRASHES: a, bc, e, hl
_FillRAM::
ld e,a
.loop
ld [hl],e
inc hl
dec bc
ld a,b
or c
jr nz,.loop
ret
; Fill up to 256 bytes of RAM with a value.
; INPUT: a = value
; hl = address
; b = size
; TRASHES: a, b, e, hl
_FillRAMSmall::
ld e,a
.loop
ld [hl],e
inc hl
dec b
jr nz,.loop
ret
; ================
; Copy up to 65536 bytes to RAM.
; INPUT: hl = source
; de = destination
; bc = size
; TRASHES: a, bc, de, hl
_CopyRAM::
ld a,[hl+]
ld [de],a
inc de
dec bc
ld a,b
or c
jr nz,_CopyRAM
ret
; Copy up to 256 bytes to RAM.
; INPUT: hl = source
; de = destination
; b = size
; TRASHES: a, b, de, hl
_CopyRAMSmall::
ld a,[hl+]
ld [de],a
inc de
dec b
jr nz,_CopyRAMSmall
ret
; ================
; ==============
; Sound routines
; ==============
include "Audio/GBMod_Player.asm"
; =============
; Graphics data
; =============
section "Font",rom0
Font::
incbin "GFX/font.1bpp"
.end
section "GBMod module - Turbo Racing Challenge Menu",romx[$4000]
Module_Turbo:
incbin "Audio/Modules/Turbo.bin"