-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.asm
901 lines (787 loc) · 17.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
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
; ================================================================
; POCKET MANS
; Main source code file
; (C) 2023 DevEd
;
; Permission is hereby granted, free of charge, to any person obtaining
; a copy of this software and associated documentation files (the
; "Software"), to deal in the Software without restriction, including
; without limitation the rights to use, copy, modify, merge, publish,
; distribute, sublicense, and/or sell copies of the Software, and to
; permit persons to whom the Software is furnished to do so, subject to
; the following conditions:
;
; The above copyright notice and this permission notice shall be included
; in all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
; ================================================================
; set to 1 to enable debugging features
DebugMode = 1
include "hardware.inc/hardware.inc"
; ================================================================
; Defines
; ================================================================
btnA equ 0
btnB equ 1
btnSelect equ 2
btnStart equ 3
btnRight equ 4
btnLeft equ 5
btnUp equ 6
btnDown equ 7
_A equ 1 << btnA
_B equ 1 << btnB
_Select equ 1 << btnSelect
_Start equ 1 << btnStart
_Right equ 1 << btnRight
_Left equ 1 << btnLeft
_Up equ 1 << btnUp
_Down equ 1 << btnDown
; --------
; global struct definitions
macro MansStruct
MansData_\@:
.start
ds 12 ; name
ds 1 ; mans type
ds 8 ; owner name
ds 2 ; owner ID
ds 1 ; held item
ds 1 ; level
ds 2 ; experience
ds 1 ; exp growth rate
ds 1 ; attack
ds 1 ; defense
ds 1 ; speed
ds 1 ; luck
ds 1 ; special attack
ds 1 ; special defense
ds 2 ; move 1 + uses left
ds 2 ; move 2 + uses left
ds 2 ; move 3 + uses left
ds 2 ; move 4 + uses left
.end
SIZEOF_MANS_STRUCT = .end-.start
endm
; --------
; sound playback macros
macro play_sfx
ld de,\1
call Sound_PlaySFX
endm
; Script command definitions
macro script_text
db $0
db bank(\1)
dw \1
endm
; script_disp_pic id,x,y,flags,vram
macro script_disp_pic
db $1
db \1
db (\3 & $0f) << 4 | (\2 & $0f)
db \4
dw \5
endm
; script_create_window x,y,w,h
macro script_create_window
db $2
db \1
db \2
db \3
db \4
endm
; script_prompt varaiable,type
macro script_prompt
db $3
dw \1
db \2
endm
; script_jump_if_zero var,script
macro script_jump_if_zero
db $4
dw \1
dw \2
endm
; script_jump_if_equal num,var,script
macro script_jump_if_equal
db $5
db \1
dw \2
dw \3
endm
; script_jump_if_greater num,var,script
macro script_jump_if_greater
db $6
db \1
dw \2
dw \3
endm
; script_play_song ptr
macro script_play_song
db $7
db \1
endm
; script_play_sfx id
macro script_play_sfx
db $8
db \1
endm
; script_actor_move num,x,y
macro script_actor_move
db $9
db \1
db \2
db \3
endm
; script_actor_walk num,dir,dist
macro script_actor_walk
db $a
db \1
db ((\2 & 3) << 6) | (\3 & 64)
endm
; script_wait_frames num
macro script_wait_frames
db $b
db \1
endm
macro script_resume_text
db $c
endm
macro script_stop_music
db $d
endm
; script_asm_call addr
macro script_asm_call
db $e
db bank(\1)
dw \1
endm
macro script_end
db $ff
endm
; --------
macro dstr
db \1
db 0
endm
macro ldfar
ld b,bank(\2)
rst Bankswitch
ld \1,\2
endm
; Loads appropriate ROM bank for a routine and executes it.
; Trashes B.
macro farjp
ld b,bank(\1)
rst Bankswitch
jp \1
endm
; Loads appropriate ROM bank for a routine and executes it.
; Trashes B.
macro farcall
ld b,bank(\1)
rst Bankswitch
call \1
endm
macro resbank
ldh a,[sys_LastBank]
ldh [sys_CurrentBank],a
ld [rROMB0],a
endm
macro tmcoord
ld hl,((\2*20) | \1)
endm
macro WaitForVRAM
ldh a,[rSTAT]
and STATF_BUSY
jr nz,@-4
endm
macro djnz
dec b
jr nz,\1
endm
macro lb
ld \1,\2<<8 | \3
endm
macro dbp
.str\@
db \1
.str\@_end
rept \2-(.str\@_end-.str\@)
db \3
endr
endm
macro dbw
db \1
dw \2
endm
macro dwfar
db bank(\1)
dw \1
endm
; --------
section "OAM buffer",wram0,align[8]
OAMBuffer: ds 40 * 4
OAMBuffer_End:
section "Stack",wram0[$cf00]
sys_Stack: ds 256
section "System RAM",hram
sys_GBType: db
sys_CurrentFrame: db
sys_btnPress: db
sys_btnHold: db
sys_btnRelease: db
sys_RNG: ds 4
sys_VBlankFlag: db
sys_STATFlag: db
sys_TimerFlag: db
sys_SerialFlag: db
sys_JoypadFlag: db
sys_CurrentBank: db
sys_LastBank: db
sys_TempSP: dw
section "OAM DMA routine",hram[$fff0]
sys_OAMDMA: ds 15
section fragment "WRAM defines",wram0
sys_StringBuffer: ds 20
; ================================================================
; Reset vectors
; ================================================================
section "Reset $00",rom0[$00]
Reset00: jp EntryPoint
section "Reset $08",rom0[$08]
DoOAMDMA: jr sys_OAMDMA
section "Reset $10",rom0[$10]
WaitVBlank: jp _WaitVBlank
section "Reset $18",rom0[$18]
WaitSTAT: jp _WaitSTAT
section "Reset $20",rom0[$20]
WaitTimer: jp _WaitTimer
section "Reset $28",rom0[$28]
Bankswitch: jp _Bankswitch
ret
section "Reset $30",rom0[$30]
Reset30: ret
section "Reset $38",rom0[$38]
ErrTrap: jr ErrTrap
; ================================================================
; Interrupt vectors
; ================================================================
section "VBlank IRQ",rom0[$40]
_VBlank: jp DoVBlank
section "STAT IRQ",rom0[$48]
_STAT: jp DoSTAT
section "Timer IRQ",rom0[$50]
_Timer: jp DoTimer
section "Serial IRQ",rom0[$58]
_Serial: jp DoSerial
section "Joypad IRQ",rom0[$60]
_Joypad: jp DoJoypad
; ================================================================
; ROM header
; ================================================================
section "ROM header",rom0[$100]
EntryPoint:
jr Start
nop
nop
NintendoLogo: ds 48,0 ; handled by post-linking tool
ROMTitle: dbp "POCKET MANS",15,0
GBCSupport: db CART_COMPATIBLE_DMG
NewLicenseCode: db "56"
SGBFlag: db CART_INDICATOR_GB
CartType: db CART_ROM_MBC5_RAM_BAT
ROMSize: db ; handled by post-linking tool
RAMSize: db CART_SRAM_128KB
DestCode: db CART_DEST_NON_JAPANESE
OldLicenseCode: db $33
ROMVersion: db -1
HeaderChecksum: db 0 ; handled by post-linking tool
ROMChecksum: dw 0 ; handled by post-linking tool
; ================================================================
; Start of program code
; ================================================================
section fragment "Program code",rom0[$150]
Start:
di
ld sp,sys_Stack
push af
; wait for vblank and disable lcd
ld hl,rLY
ld a,SCRN_Y
: cp [hl]
jr nz,:-
xor a
ldh [rLCDC],a
pop af
ld c,a
ld a,b
and 1
add c
ld [sys_GBType],a ; $02 on DMG0 (unlikely), $01 on DMG/SGB, $FF on MGB/MGL/SGB2, $11 on CGB, $12 on AGB/GBP
; clear HRAM
ld bc,low(_HRAM) | (low(rIE) - low(_HRAM)) << 8
xor a
: ld [c],a
inc c
dec b
jr nz,:-
; clear VRAM
xor a
ld hl,_VRAM
ld bc,_SRAM-_VRAM
call _FillRAM
; clear OAM
xor a
ld hl,OAMBuffer
ld b,OAMBuffer_End - OAMBuffer
call _FillRAMSmall
; copy OAM DMA routine
ld hl,OAMDMA
ld bc,low(sys_OAMDMA) | ((OAMDMA_End - OAMDMA) << 8)
: ld a,[hl+]
ld [c],a
inc c
dec b
jr nz,:-
ld a,%11100100
ldh [rBGP],a
ldh [rOBP1],a
ld a,%11100000
ldh [rOBP0],a
farcall Sound_Init
if DebugMode
jp GM_Debug
endc
; ================================================================
; Game mode includes
; ================================================================
include "Engine/GameModes/Debug.asm"
include "Engine/GameModes/SpriteView.asm"
include "Engine/GameModes/SoundTest.asm"
include "Engine/GameModes/TilesetViewer.asm"
include "Engine/GameModes/Overworld.asm"
include "Engine/GameModes/Battle.asm"
; ================================================================
; Support routines
; ================================================================
section fragment "Program code",rom0
; 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
OAMDMA: ; copied to HRAM during startup
ld a,high(OAMBuffer)
ldh [rDMA],a
; wait 160 cycles for transfer to complete
ld a,40
: dec a
jr nz,:-
ret
OAMDMA_End:
; 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
; 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
; 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 " " + $80
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 " " + $80
ld [de],a
inc de
jr PrintString
PrintMansName:
ld b,12
: WaitForVRAM
ld a,[hl+]
sub " " + $80
ld [de],a
inc de
djnz :-
ret
; INPUT: a = hexadecimal number
; OUTPUT: sys_StringBuffer = converted number
; TRASHES: bc, hl
; Adapted from https://wikiti.brandonw.net/index.php?title=Z80_Routines:Other:DispA
Hex2Dec8:
inc a
ld hl,sys_StringBuffer
ld c,-100
call :+ ; hundreds place
ld c,-10
call :+ ; tens place
ld c,-1
call :+
ld [hl],0
ret
: ld b,-1
: inc b
add c
jr c,:-
sub c
push af
ld a,b
add "0"
ld [hl+],a
pop af
ret
; Prints hexadecimal number A at HL.
; INPUT: a = number to print
; hl = screen address
PrintHex:
push af
swap a
call :+
pop af
: and $f
cp $a
jr c,:+
add 7
: add $10
push af
ldh a,[rSTAT]
and 2
jr nz,@-4
pop af
add $80
ld [hl+],a
ret
; INPUT: de = (x,y)
; OUTPUT: de = VRAM address
GetScreenCoordinates:
push hl
push bc
ld a,d
and $1f
ld c,a
ld b,0
ld l,e
ld h,0
add hl,hl ; x2
add hl,hl ; x4
add hl,hl ; x8
add hl,hl ; x16
add hl,hl ; x32
add hl,bc
ld de,_SCRN0
add hl,de
ld d,h
ld e,l
pop bc
pop hl
ret
; Clear the screen.
; TRASHES: a, bc, hl
; RESTRICTIONS: Requires the LCD to be disabled, otherwise screen will not be properly cleared.
ClearScreen:
ld hl,_VRAM ; clear from start of VRAM...
ld bc,_SRAM-_VRAM ; ...to end of VRAM.
call _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
ld hl,_SCRN0 ; clear from start of VRAM...
ld bc,_SRAM-_SCRN0 ; ...to end of VRAM.
call _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
include "Engine/WLE_Decode.asm"
include "Engine/Math.asm"
include "Engine/Pic.asm"
include "Engine/Text.asm"
include "Engine/Mans.asm"
include "Engine/Metatile.asm"
include "Engine/TileAnimation.asm"
; ================================================================
; Interrupt handlers
; ================================================================
section fragment "Program code",rom0
DoVBlank:
push af
push bc
push de
push hl
ld a,1
ldh [sys_VBlankFlag],a
ld hl,sys_CurrentFrame
inc [hl]
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
ld a,[sys_CurrentBank]
push af
call Sound_Update
pop bc
rst Bankswitch
WaitForVRAM
pop hl
pop de
pop bc
pop af
reti
DoSTAT:
push af
push bc
push de
push hl
ld a,1
ldh [sys_STATFlag],a
pop hl
pop de
pop bc
pop af
reti
DoTimer:
push af
push bc
push de
push hl
ld a,1
ldh [sys_TimerFlag],a
pop hl
pop de
pop bc
pop af
reti
DoSerial:
push af
push bc
push de
push hl
ld a,1
ldh [sys_SerialFlag],a
pop hl
pop de
pop bc
pop af
reti
DoJoypad:
push af
push bc
push de
push hl
ld a,1
ldh [sys_JoypadFlag],a
pop hl
pop de
pop bc
pop af
reti
_WaitVBlank:
halt
ldh a,[sys_VBlankFlag]
and a
jr z,_WaitVBlank
xor a
ldh [sys_VBlankFlag],a
ret
_WaitSTAT:
halt
ldh a,[sys_STATFlag]
and a
jr z,_WaitSTAT
xor a
ldh [sys_STATFlag],a
ret
_WaitTimer:
halt
ldh a,[sys_TimerFlag]
and a
jr z,_WaitTimer
xor a
ldh [sys_TimerFlag],a
ret
_WaitSerial:
halt
ldh a,[sys_SerialFlag]
and a
jr z,_WaitSerial
xor a
ldh [sys_SerialFlag],a
ret
_WaitJoypad:
halt
ldh a,[sys_JoypadFlag]
and a
jr z,_WaitJoypad
xor a
ldh [sys_JoypadFlag],a
ret
; ================================================================
; GFX data
; ================================================================
Font: incbin "GFX/Font.2bpp.wle"
include "Data/MansNames.asm"
include "Data/MansPics.asm"
include "Data/MansDexEntries.asm"
include "Data/Tilesets.asm"
; ================================================================
; Sound data
; ================================================================
include "Audio/Driver.asm"