This repository was archived by the owner on Sep 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.asm
More file actions
853 lines (751 loc) · 17.9 KB
/
Main.asm
File metadata and controls
853 lines (751 loc) · 17.9 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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
; =============================================================================
; POTION PANIC
; Copyright (C) 2025 DevEd
;
; This project is licensed under the terms of the MIT License. See the LICENSE
; file in the root directory of this repository for the full license terms.
; =============================================================================
def percent equs "* $ff / 100"
def seconds equs "* 60"
def second equs "* 60"
charmap "©",$a9
include "hardware.inc/hardware.inc"
def BUILD_DEBUG = 0
def STACK_TOP = $d000
def BIT_A equ 0
def BIT_B equ 1
def BIT_SELECT equ 2
def BIT_START equ 3
def BIT_RIGHT equ 4
def BIT_LEFT equ 5
def BIT_UP equ 6
def BIT_DOWN equ 7
def BTN_A equ 1 << BIT_A
def BTN_B equ 1 << BIT_B
def BTN_START equ 1 << BIT_START
def BTN_SELECT equ 1 << BIT_SELECT
def BTN_RIGHT equ 1 << BIT_RIGHT
def BTN_LEFT equ 1 << BIT_LEFT
def BTN_UP equ 1 << BIT_UP
def BTN_DOWN equ 1 << BIT_DOWN
; =============================================================================
macro bankswitch_to_pointer
assert warn, \1 > $3fff, "Bankswitchingh is unnecessary for calls to ROM0"
assert \1 < $7fff, "Bankswitch target outside ROM!"
ld a,bank(\1)
bankswitch_to_a
endm
macro bankswitch_to_a
ldh [hROMB0],a
ld [rROMB0],a
endm
macro farcall
bankswitch_to_pointer \1
call \1
endm
macro farload
bankswitch_to_pointer \2
ld \1,\2
endm
macro pushbank
ldh a,[hROMB0]
push af
endm
macro popbank
pop af
ldh [hROMB0],a
ld [rROMB0],a
endm
macro lb
ld \1,(\2 * 256) + \3
endm
macro wait_for_vram
: ldh a,[rSTAT]
and STATF_BUSY
jr nz,:-
endm
macro dwbank
db bank(\1)
dw \1
endm
macro dbw
db \1
dw \2
endm
macro dbbw
db \1
db \2
dw \3
endm
macro rgb
dw \1 | \2 << 5 | \3 << 10
endm
macro rgb8
dw (\1 >> 3) | (\2 >> 3) << 5 | (\3 >> 3) << 10
endm
; y, x, tile, attributes
macro oam_entry
db \1+16
db \2+8
db \3
db \4
endm
; =============================================================================
section "Reset $00",rom0[$00]
WaitForVBlank: jp _WaitForVBlank
section "Reset $08",rom0[$08]
WaitForSTAT: jp _WaitForSTAT
section "Reset $10",rom0[$10]
CallHL:
bit 7,h
jr nz,CallHL_Error
jp hl
section "Reset $18",rom0[$18]
Reset18: ret
section "Reset $20",rom0[$20]
Reset20: ret
section "Reset $28",rom0[$28]
Reset28: ret
section "Reset $30",rom0[$30]
Reset30: ret
section "Reset $38",rom0[$38]
Reset38:
jr _Reset38
; =============================================================================
section "VBlank interrupt vector",rom0[$40]
IRQ_VBlank: jp DoVBlank
section "LCD status interrupt vector",rom0[$48]
IRQ_STAT: jp DoSTAT
section "Timer interrupt vector",rom0[$50]
IRQ_Timer: jp DoTimer
section "Serial interrupt vector",rom0[$58]
IRQ_Serial: reti
section "Joypad interrupt vector",rom0[$60]
IRQ_Joypad: reti
; =============================================================================
CallHL_Error:
push af
ld a,ERR_JP_HL_OUTSIDE_ROM
ldh [hErrType],a
pop af
jp ErrorScreen
_Reset38:
push af
ld a,ERR_TRAP
ldh [hErrType],a
pop af
jp ErrorScreen
; =============================================================================
section "ROM header",rom0[$100]
Header_EntryPoint:
jr ProgramStart ;
ds 2,0 ; padding
Header_NintendoLogo: ds 48,0 ; handled by rgbfix
Header_Title: db "POTION PANIC" ; must be 15 chars or less!
ds (Header_Title + 15) - @,0 ; padding
Header_GBCSupport: db CART_COMPATIBLE_GBC ;
Header_NewLicenseCode: dw 0 ; not needed
Header_SGBSupport: db $00 ; $03 = enable SGB features (requires old license code to be set to $33)
Header_CartridgeType: db CART_ROM_MBC5_RUMBLE ;
Header_ROMSize: ds 1 ; handled by rgbfix
Header_RAMSize: db 0 ; not used
Header_DestinationCode: db 1 ; 0 = Japan, 1 = not Japan
Header_OldLicenseCode: db $33 ; must be $33 for SGB support
Header_Revision: db -1 ; revision (-1 for prerelease builds)
Header_Checksum: db 0 ; handled by rgbfix
Header_ROMChecksum: dw 0 ; handled by rgbfix
; =============================================================================
section fragment "Program code",rom0[$150]
ProgramStart:
di
ld sp,STACK_TOP
ldh [hIsGBC],a
cp $11
jr z,.gbc
ld a,c
sub $13
jr :+
.gbc
xor a
: ldh [hIsSGB],a
ld a,b
ld [hIsGBA],a
; initialize interrupt pointers
ld a,low(Int_Dummy)
ldh [hVBlankPointer],a
ldh [hSTATPointer],a
ldh [hTimerPointer],a
ld a,high(Int_Dummy)
ldh [hVBlankPointer+1],a
ldh [hSTATPointer+1],a
ldh [hTimerPointer+1],a
; clear OAM
ld hl,OAMBuffer
ld b,40*4
xor a
ldh [hOAMPos],a
call MemFillSmall
; copy OAM DMA routine
ld hl,_OAMDMA
lb bc,SIZEOF_OAMDMA,low(hOAMDMA)
: ld a,[hl+]
ldh [c],a
inc c
dec b
jr nz,:-
call hOAMDMA
; GBC check
ldh a,[hIsGBC]
cp $11
jp nz,GM_Lockout
; Very Bad Amulator™ check
ld a,5
add a
daa
push af
pop hl
bit 5,l
jp nz,GM_Lockout
; enable double speed mode
ldh a,[rKEY1]
bit SPDB_DBLSPEED,a
jr nz,:+
xor a
ldh [rIE],a
ld a,$30
ldh [rP1],a
ld a,1
ldh [rKEY1],a
stop
: ld a,1
bankswitch_to_a
call GBM_Stop
call DSFX_Init
xor a
ldh [rSCX],a
ldh [rSCY],a
ldh [hPressedButtons],a
ldh [hHeldButtons],a
ldh [hReleasedButtons],a
ldh [hGlobalTick],a
ld [Level_ID],a
ld [Level_CameraX],a
ld [Level_CameraY],a
call Math_InitRandSeed
ld a,3
ld [Player_Lives],a
xor a
ld [Player_Score],a
ld [Player_Score+1],a
ld [Player_Score+2],a
ld a,low(IntV_Default)
ldh [hVBlankPointer],a
ld a,high(IntV_Default)
ldh [hVBlankPointer+1],a
if BUILD_DEBUG
ld a,bank(GM_Debug)
bankswitch_to_a
jp GM_Debug
else
ld a,bank(GM_Copyright)
bankswitch_to_a
jp GM_Copyright
endc
jr @
; =============================================================================
; Support routines
; =============================================================================
; print a null-terminated string to DE
; INPUT: hl = pointer
; de = destination
PrintString:
ld a,[hl+]
and a
ret z
sub " "
ld [de],a
inc de
jr PrintString
; print an FF-terminated string to DE
; INPUT: hl = pointer
; de = destination
PrintString2:
ld a,[hl+]
cp -1
ret z
sub " "
ld [de],a
inc de
jr PrintString2
; print a null-terminated color inverted string to DE
; INPUT: hl = pointer
; de = destination
PrintStringInverted:
ld a,[hl+]
and a
ret z
sub " "
set 7,a
ld [de],a
inc de
jr PrintStringInverted
; Print hexadecimal number B at DE
; INPUT: a = number
; de = destination
PrintHex:
ld b,a
swap a
and $f
cp $a
jr nc,:+
add "0"-" "
jr :++
: add "A"-" "-$a
: ld [de],a
inc e
ld a,b
and $f
cp $a
jr nc,:+
add "0"-" "
jr :++
: add "A"-" "-$a
: ld [de],a
inc e
ret
; INPUT: hl = source
; de = destination
; bc = size
CopyTiles1BPP:
ld a,[hl+]
ld [de],a
inc de
ld [de],a
inc de
dec bc
ld a,b
or c
jr nz,CopyTiles1BPP
ret
;CopyTiles1BPPLight:
; ld a,[hl+]
; ld [de],a
; inc de
; inc de
; dec bc
; ld a,b
; or c
; jr nz,CopyTiles1BPPLight
; ret
;CopyTiles1BPPDark:
; ld a,[hl+]
; inc de
; ld [de],a
; inc de
; dec bc
; ld a,b
; or c
; jr nz,CopyTiles1BPPDark
; ret
CopyTiles1BPPInverted:
ld a,[hl+]
cpl
ld [de],a
inc de
ld [de],a
inc de
dec bc
ld a,b
or c
jr nz,CopyTiles1BPPInverted
ret
; INPUT: hl = source
; de = destination
; bc = dimensions (b = x, c = y)
LoadTilemap:
push bc
.loop
ld a,[hl+]
ld [de],a
inc de
dec b
jr nz,.loop
pop bc
dec c
ret z
push hl
ld a,e
and %11100000
ld e,a
ld hl,$20
add hl,de
ld d,h
ld e,l
pop hl
push bc
jr .loop
; INPUT: hl = source
; de = destination
; bc = dimensions (b = x, c = y)
LoadTilemapAttr:
push bc
.loop
xor a
ldh [rVBK],a
ld a,[hl+]
ld [de],a
ld a,1
ldh [rVBK],a
ld a,[hl+]
ld [de],a
inc de
dec b
jr nz,.loop
pop bc
dec c
jr z,.done
push hl
ld a,e
and %11100000
ld e,a
ld hl,$20
add hl,de
ld d,h
ld e,l
pop hl
push bc
jr .loop
.done
xor a
ldh [rVBK],a
ret
; INPUT: hl = source
; de = destination
; bc = dimensions (b = x, c = y)
LoadBackgroundMap:
push bc
push de
xor a
ldh [rVBK],a
push bc
.loop
: ldh a,[rSTAT]
and STATF_BUSY
jr nz,:-
ld a,[hl+]
add $80
ld [de],a
inc de
dec b
jr nz,.loop
pop bc
dec c
jr z,.attr
push bc
jr .loop
.attr
pop de
pop bc
ld a,1
ldh [rVBK],a
push bc
.loop2
: ldh a,[rSTAT]
and STATF_BUSY
jr nz,:-
ld a,[hl+]
inc a ; HACK
ld [de],a
inc de
dec b
jr nz,.loop2
pop bc
dec c
ret z
push bc
jr .loop2
ret
; INPUT: hl = destination
; e = fill byte
; bc = size
MemFill:
ld [hl],e
inc hl
dec bc
ld a,b
or c
jr nz,MemFill
ret
; INPUT: hl = destination
; a = fill byte
; b = size
MemFillSmall:
ld [hl+],a
dec b
jr nz,MemFillSmall
ret
; INPUT: hl = source
; de = destination
; bc = size
MemCopy:
ld a,[hl+]
ld [de],a
inc de
dec bc
ld a,b
or c
jr nz,MemCopy
ret
MemCopySafe:
ldh a,[rSTAT]
and STATF_BUSY
jr nz,MemCopySafe
: ld a,[hl+]
ld [de],a
inc de
dec bc
ld a,b
or c
jr nz,MemCopy
ret
; INPUT: hl = source
; de = destination
; b = size
MemCopySmall:
ld a,[hl+]
ld [de],a
inc de
dec b
jr nz,MemCopySmall
ret
MemCopySmallSafe:
ldh a,[rSTAT]
and STATF_BUSY
jr nz,MemCopySmallSafe
: ld a,[hl+]
ld [de],a
inc de
dec b
jr nz,MemCopySmall
ret
LCDOff:
ldh a,[rLCDC]
bit 7,a
ret z
: ldh a,[rSTAT]
and STATF_VBL
jr z,:-
xor a
ldh [rLCDC],a
ret
; WARNING: Assumes LCD is off!
ClearScreen:
ld a,1
ldh [rVBK],a
ld hl,_VRAM
ld bc,_SRAM-_VRAM
push hl
push bc
ld e,0
call MemFill
xor a
ldh [rVBK],a
pop bc
pop hl
call MemFill
ld hl,OAMBuffer
ld b,OAMBuffer.end-OAMBuffer
call MemFillSmall
; clear palettes
ld hl,sys_BGPalettes
ld b,(sys_ObjPalettes-sys_BGPalettes)*2
dec a
call MemFillSmall
call CopyPalettes
jp UpdatePalettes
_OAMDMA: ; copied to HRAM on boot
ld a,high(OAMBuffer)
ldh [rDMA],a
; wait 160 cycles for transfer to complete
ld a,160/4
: dec a
jr nz,:-
ret
def SIZEOF_OAMDMA = @-_OAMDMA
; ================================================================
_WaitForVBlank:
push af
: halt
ldh a,[hVBlankFlag]
and a
jr z,:-
xor a
ldh [hVBlankFlag],a
pop af
ret
_WaitForSTAT:
push af
: halt
ldh a,[hSTATFlag]
and a
jr z,:-
xor a
ldh [hSTATFlag],a
pop af
ret
_WaitForTimer:
push af
: halt
ldh a,[hTimerFlag]
and a
jr nz,:-
xor a
ldh [hTimerFlag],a
pop af
ret
DoVBlank:
push af
push hl
ld hl,hVBlankPointer
ld a,[hl+]
ld h,[hl]
ld l,a
rst CallHL
ld hl,hVBlankFlag
inc [hl]
pop hl
pop af
reti
DoSTAT:
push af
push hl
ld hl,hSTATPointer
ld a,[hl+]
ld h,[hl]
ld l,a
rst CallHL
ld a,1
ldh [hSTATFlag],a
pop hl
pop af
reti
DoTimer:
push af
push hl
ld hl,hTimerPointer
ld a,[hl+]
ld h,[hl]
ld l,a
rst CallHL
ld a,1
ldh [hTimerFlag],a
pop hl
pop af
reti
; Actual interrupt routines.
; Use IntV for vblank, IntS for STAT, and IntT for timer.
; BC and DE need to be preserved.
IntV_Dummy:
IntS_Dummy:
IntT_Dummy:
Int_Dummy:
ret
IntV_Default:
push bc
push de
; do OAM DMA
call hOAMDMA
; get input
ld a,[hHeldButtons]
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,[hHeldButtons]
xor b
and b
ld [hPressedButtons],a ; store buttons pressed this frame
ld e,a
ld a,b
ld [hHeldButtons],a ; store held buttons
xor c
xor e
ld [hReleasedButtons],a ; store buttons released this frame
ld a,P1F_5|P1F_4
ld [rP1],a
; clear OAM
ld hl,OAMBuffer
ld b,40*4
xor a
call MemFillSmall
ld hl,hGlobalTick
inc [hl]
pop de
pop bc
ret
; =============================================================================
include "Engine/Math.asm"
include "Engine/Canvas.asm"
include "Engine/PerFade.asm"
include "Engine/Metatile.asm"
include "Engine/WLE_Decode.asm"
include "Engine/Object.asm"
; =============================================================================
include "GameModes/DebugMenu.asm"
include "GameModes/CanvasTest.asm"
include "GameModes/Level.asm"
include "GameModes/TitleScreen.asm"
include "GameModes/Credits.asm"
include "GameModes/GBCompo25Splash.asm"
include "GameModes/Copyright.asm"
include "GameModes/Lockout.asm"
include "GameModes/DevEdPresentsSplash.asm"
; =============================================================================
include "Engine/ErrorHandler.asm"
; =============================================================================
include "Audio/Audio.asm"
; =============================================================================
section "OAM buffer",wram0,align[8]
OAMBuffer: ds 40*4
.end
section "System variables",hram[$ff80]
hOAMDMA: ds SIZEOF_OAMDMA
def hOAMPage equ hOAMDMA + 1
hOAMPos: db
hROMB0: db
hHeldButtons: db
hPressedButtons: db
hReleasedButtons: db
hGlobalTick: db
hVBlankFlag: db
hSTATFlag: db
hTimerFlag: db
hVBlankPointer: dw
hSTATPointer: dw
hTimerPointer: dw
hTempPtr1: dw
hTempPtr2: dw
hTemp1: db
hTemp2: db
hTemp3: db
hTemp4: db
hIsGBC: db ; $01 if on GBC or GBA, $00 otherwise
hIsGBA: db ; $01 if on GBC, $FF if on DMG0, otherwise $00
hIsSGB: db ; $01 if on SGB, otherwise $00