-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
1323 lines (1010 loc) · 17.8 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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
INCLUDE "definitions/hardware.inc"
INCLUDE "definitions/memory.inc"
; TODO - organize function positions
; de may be changed by interruptions
SECTION "begin",ROM0[$0]
VBLANKF_rom:
jp VBLANK_Nothing ; It'll be copyed to wram
VBLANK_Nothing:
reti
SECTION "vblank",ROM0[$40]
VBLANKI:
jp VBLANKF_wram
SECTION "lcdc",ROM0[$48]
LCDI: ; TODO - at line L, enable this interrupt and read a vector directely
push af
push hl
; a = rLY + transition
ld a, [rLogoTransition]
ld l, a
ld a, [rLY]
add l
; hl = HVector + a
ld h, HIGH(HVector)
ld l, a
; Compare a and hl
cp [hl]
jr nc, .00
.ff: ; a < hl
ld hl, Screen2X
jr .setvalues
.00: ; a > hl
ld hl, Screen1X
.setvalues:
ld a, [hli]
ld b, a ; X
ld a, [hli]
ld c, a ; Y
ld a, [hli]
ld d, a ; S
ld hl, rSCY
.waitmode3:
ld a, [rSTAT]
cpl
and %11
jr z, .waitmode3
ld a, c
ld [hli], a
ld a, b
ld [hl], a
ld a, d
ld [rLCDC], a
; Return LCDI
pop hl
pop af
reti
;
SECTION "entry", ROM0[$100]
di
jr start
SECTION "main", ROM0[$150]
_LCDI_Test:
push af
push hl
; a = rLY - rLYC
ld hl, rLY
ld a, [hli]
sub [hl]
;; Do something
reti
SetSmallMem:
.loop:
ld [hli], a
dec b
jp nz, .loop
ret
;
ResetMem: ; hl: start, bc: size
.loop
xor a
ld [hli], a
dec bc
ld a, b
or c
jr nz, .loop
ret
start:
call WaitAndShutdownScreen
; Copy DMA function to HRAM
ld de, OAMF_rom
ld hl, OAMF_hram
ld b, 5
call CopySmallMem
; Copy VBlank jump to WRAM
ld de, VBLANKF_rom
ld hl, VBLANKF_wram
ld b, 3
call CopySmallMem
jr InitLogo
WaitAndShutdownScreen:
.waitvblank:
ld a, [rLY]
cp 144
jr c, .waitvblank
xor a
ld [rLCDC], a ; Turn off screen
ret
;
InitLogo:
; Set white screen
xor a
ld [rBGP], a
; Copy sd tiles ; TODO - create a matro for that (copyasset $8000, SD_tiles)
ld de, SD_tiles
ld hl, $8000
ld bc, SD_tiles_end - SD_tiles
call CopyMem
; Copy map
ld de, SD_map
ld hl, $9800
ld bc, SD_map_end - SD_map
call CopyMem
; Copy penguin tiles
ld de, Penguin_tiles
ld hl, $9000 - $400
ld bc, Penguin_tiles_end - Penguin_tiles
call CopyMem
; Copy penguin map
ld de, Penguin_map
ld hl, $9C00
ld bc, Penguin_map_end - Penguin_map
call CopyMemSub64 ; TODO - subtract 64 on the rom
; Change jump on VBLANK WRAM - TODO - make a macro (setvblank VBLANK_dec_e)
ld hl, VBLANKF_wram + 1
ld a, LOW(VBLANK_dec_e)
ld [hli], a
ld a, HIGH(VBLANK_dec_e)
ld [hli], a
; Start screen
ld a, LCDCF_BGON | LCDCF_ON | LCDCF_BG8800 | LCDCF_BG9C00
ld [rLCDC], a
; Clear interrupt flags (needed?)
xor a; (-) a is already 0
ld [rIF], a
; Turn on VBlank interrupt
ld a, IEF_VBLANK; (-) or waitvblank;
ld [rIE], a
ei
; Intro events
call FadeIn
ld e, 60
call WaitFrames
call FadeOut
;ld e, 30
;call WaitFrames
; Change screen
ld a, LCDCF_BGON | LCDCF_ON | LCDCF_BG8000 | LCDCF_BG9800
ld [rLCDC], a
call FadeIn
ld e, 60
call WaitFrames
call FadeOut
ld e, 10
call WaitFrames
di
call WaitAndShutdownScreen
jr Init_Title
;
FadeOut: ; VBlank must be "dec e"
ld e, 4
call WaitFrames
ld a, %10010000
ld [rBGP], a
ld e, 4
call WaitFrames
ld a, %01000000
ld [rBGP], a
ld e, 4
call WaitFrames
ld a, %00000000
ld [rBGP], a
ret
;
FadeIn:
ld e, 4
call WaitFrames
ld a, %01000000
ld [rBGP], a
ld e, 4
call WaitFrames
ld a, %10010000
ld [rBGP], a
ld e, 4
call WaitFrames
ld a, %11100100
ld [rBGP], a
ret
;
WaitFrames:
.stillwaiting
halt
xor a
cp e
jr nz, .stillwaiting
ret
;
VBLANK_dec_e:
dec e
reti
;
Init_Title: ; Call it on VBLANK
; Clear OAM ; TODO - make a macro?
; TODO - xor a ?
ld hl, _OAMRAM
ld b, 160
call SetSmallMem
; Clear OAM at WRAM
ld hl, OAM_Data_wram
ld b, 160
call SetSmallMem
; Copy stars tiles
ld de, Stars_tiles
ld hl, $9000
ld bc, Stars_tiles_end - Stars_tiles
call CopyMem
; Copy logo tiles
ld de, Logo_tiles
ld hl, $8000
ld bc, Logo_tiles_end - Logo_tiles
call CopyMem
; Copy logo map
ld de, Logo_map
ld hl, $9800
ld bc, Logo_map_end - Logo_map
call CopyMem
; Clear the rest of the tiles
ld bc, $9c00 - ($9800 + Logo_map_end - Logo_map)
call ResetMem
; Copy tiles for Screen 1
ld de, Stars_map
ld hl, $9c00
ld bc, Stars_map_end - Stars_map
call CopyMem
; Copy text tiles
ld de, Text_tiles
ld hl, $8800
ld bc, Text_tiles_end - Text_tiles
call CopyMem
; Copy object tiles
ld de, Objects_tiles
ld hl, $8b00
ld bc, Objects_tiles_end - Objects_tiles
call CopyMem
xor a
ld [Screen1X], a
ld [Screen1Y], a
ld [Screen2X], a
ld [Screen2Y], a
ld [Frame_Counter], a
ld [InGame], a
ld [rButtons], a
ld a, $90
ld [rLogoTransition], a
; Clear OAM
ld hl, _OAMRAM
ld b, 160
call SetSmallMem
; Screen1C <- BG at $9800
ld a, LCDCF_BGON | LCDCF_OBJON | LCDCF_OBJ16 | LCDCF_ON | LCDCF_BG8000
ld [Screen1C], a
; Screen2C <- BG at $9C00
ld a, LCDCF_BGON | LCDCF_OBJON | LCDCF_OBJ16 | LCDCF_ON | LCDCF_BG9C00
ld [Screen2C], a
;ld [rLCDC], a ; Turn on creen
; Set palette
ld a, %11100100
ld [rBGP], a
ld [rOBP0], a
ld [rOBP1], a
; Change vblank to title
setvblanki VBLANK_title_in
; Set MODE0 (hblank) as STAT interrupt
ld a, STATF_MODE10 ;STATF_MODE00
ld [rSTAT], a
; Turn on LCDC (STAT) and VBLANK interupts
ld a, IEF_LCDC | IEF_VBLANK
ld [rIE], a
xor a
ld [rIF], a ; Clear interrupt flags (needed?)
call VBLANK_title_in ; Turn on screen and interupts
.haltLoop: ; Keep here while not in game
nop;halt - TODO - halt
;call DS_Play
ld a, [InGame]
bit 0, a
jr z, .haltLoop
;;;;;;;; IN GAME
.loop
halt
ld a, [TickCount_old]
ld b, a
ld a, [TickCount]
ld [TickCount_old], a
cp b
jr z, .done
add 4
and %111
call z, ReadGasVector
.done:
; <>ba
ld a, [rButtons_High]
bit 0, a ; A
call nz, MoveShip2_Right
bit 1, a ; B
call nz, MoveShip2_Left
bit 2, a ; >
call nz, MoveShip1_Right
bit 3, a ; <
call nz, MoveShip1_Left
call UpdateShips
.return:
call DS_Play
call UpdateGas
jr .loop
;
TIME = 20
;%10000000
MoveShip2_Right:
push af
ld a, [Ship2_PosXTimer]
cp a, $0
jr nz, .return
ld a, TIME
ld [Ship2_PosXTimer], a
ld a, 0
ld [Ship2_Direction], a
ld a, [TickCount]
ld [Ship2_PosXTicks], a
.return:
pop af
ret
MoveShip1_Right:
push af
ld a, [Ship1_PosXTimer]
cp a, $0
jr nz, .return
ld a, TIME
ld [Ship1_PosXTimer], a
ld a, 0
ld [Ship1_Direction], a
ld a, [TickCount]
ld [Ship1_PosXTicks], a
.return:
pop af
ret
MoveShip2_Left:
push af
ld a, [Ship2_PosXTimer]
cp a, $0
jr nz, .return
ld a, TIME
ld [Ship2_PosXTimer], a
ld a, 1
ld [Ship2_Direction], a
ld a, [TickCount]
ld [Ship2_PosXTicks], a
.return:
pop af
ret
MoveShip1_Left:
push af
ld a, [Ship1_PosXTimer]
cp a, $0
jr nz, .return
ld a, TIME
ld [Ship1_PosXTimer], a
ld a, 1
ld [Ship1_Direction], a
ld a, [TickCount]
ld [Ship1_PosXTicks], a
.return:
pop af
ret
UpdateShip1:
ld a, [Ship1_PosXTimer]
cp 0
ret z
sub 2
ld [Ship1_PosXTimer], a
ld a, [Ship1_PosXTimer]
ret
UpdateShip2:
ld a, [Ship2_PosXTimer]
cp 0
ret z
sub 2
ld [Ship2_PosXTimer], a
ld a, [Ship2_PosXTimer]
ret
UpdateShips:
call UpdateShip1
call UpdateShip2
call MoveShip1
call MoveShip2
ret
MoveShip1:
ld a, [Ship1_PosXTimer]
ld c, a
ld a, [Ship1_Direction]
ld d, a
ld a, SHIP1X
dec d
jr z, .one
add c
jr .done
.one:
sub c
.done
ld hl, OAM_Data_wram + 1 ;SHIP1X
ld bc, 4
ld [hl], a
add hl, bc
add 8
ld [hl], a
add 8
add hl, bc
ld [hl], a
ret
MoveShip2:
ld a, [Ship2_PosXTimer]
ld c, a
ld a, [Ship2_Direction]
ld d, a
ld a, SHIP2X
dec d
jr z, .one
add c
jr .done
.one:
sub c
.done
ld hl, OAM_Data_wram + $0C + 1 ;SHIP2X
ld bc, 4
ld [hl], a
add hl, bc
add 8
ld [hl], a
add 8
add hl, bc
ld [hl], a
ret
ReadGasVector:
; Increment gas vector position
ld a, [GasVectorPosition]
ld hl, GasPositions
inc a
cp a, GasPositions_end - GasPositions
jr nz, .dont_reset
xor a
.dont_reset
ld [GasVectorPosition], a
ld c, a
ld b, 0
add hl, bc
ld hl, GasPositions
ld c, a
ld b, 0
add hl, bc
ld a, [hl]
ld e, a
bit 0, e
call nz, Spawn_Gas1
bit 1, e
call nz, Spawn_Gas2
bit 2, e
call nz, Spawn_Gas3
bit 3, e
call nz, Spawn_Gas4
ret
CopyMem: ; de: src, hl: dst, bc: size
ld a, [de]
ld [hli], a
inc de
dec bc
ld a, b
or a, c
jp nz, CopyMem
ret
;
CopyMemSub64: ; Copy memery subtracting 64 from it (it should never exist, change on ROM)
ld a, [de]
sub 64
ld [hli], a
inc de
dec bc
ld a, b
or a, c
jp nz, CopyMemSub64
ret
CopySmallMem: ; de: src, hl: dst, b: size
ld a, [de]
ld [hli], a
inc de
dec b
jp nz, CopySmallMem
ret
;
dmaCopy:
ld a, HIGH(OAM_Data_wram)
ld bc, $2846 ; B: wait time; C: LOW($FF46)
jp OAMF_hram
;
OAMF_rom:
ld [$ff00 + c], a
.wait
dec b
jr nz, .wait
ret
;
LoadPressStart_OAMS:
ld de, Text_OAMS
ld b, Text_OAMS_end - Text_OAMS
jr CopySmallMem ; de: src, hl: dst, b: size
LoadDirection_OAMS:
ld de, DIRECTION_OAMS
ld b, DIRECTION_OAMS_end - DIRECTION_OAMS
jr CopySmallMem ; de: src, hl: dst, b: size
LoadShip1_OAMS:
ld de, SHIP1_OAMS
ld b, SHIP1_OAMS_end - SHIP1_OAMS
jr CopySmallMem ; de: src, hl: dst, b: size
LoadShip2_OAMS:
ld de, SHIP2_OAMS
ld b, SHIP2_OAMS_end - SHIP2_OAMS
jr CopySmallMem ; de: src, hl: dst, b: size
LoadGas_OAMS:
ld de, GAS_OAMS
ld b, GAS_OAMS_end - GAS_OAMS
jr CopySmallMem ; de: src, hl: dst, b: size
SetScreen2XY: ; x = sin, y = -frame_counter
; Increment frame counter
push bc
ld hl, Frame_Counter
ld c, [hl]
inc [hl]
; a = SinVec + counter <- TODO: align SinVec
ld hl, SinVec
ld b, $00
add hl, bc
ld a, [hl]
; Screen2X = sin
ld [Screen2X], a
; Screen2Y = - frame counter
xor a
sub c
ld [Screen2Y], a
pop bc
ret
SetScreen2:
ld hl, Screen2X
ld a, [hli]
ld [rSCX], a
ld a, [hli]
ld [rSCY], a
ld a, [hli]
ld [rLCDC], a
ret
SetScreen1:
ld hl, Screen2X
ld a, [hli]
ld [rSCX], a
ld a, [hli]
ld [rSCY], a
ld a, [hli]
ld [rLCDC], a
ret
VBLANK_title_in:
call dmaCopy
push af
push hl
; Increment the transition until it reacher $00
ld hl, rLogoTransition
ld a, [hl]
cp $00
jr nz, .not_zero
setvblanki VBLANK_title_wait
ld hl, OAM_Data_wram
call LoadPressStart_OAMS
call LoadDirection_OAMS
call LoadShip1_OAMS
call LoadShip2_OAMS
ld a, %00011011
ld [rOBP0], a
ld a, %00011011;%11100100
ld [rOBP1], a
jr .return
.not_zero:
inc [hl]
add a
ld [Screen1Y], a
.return
call SetScreen2XY
call SetScreen1
; Return VBLANK_title
pop hl
pop af
reti
;
ClearOAMEntries: ; b - entries, hl - position
xor a
sla b
sla b
call SetSmallMem
ret
VBLANK_title_wait:
push af
push hl
call dmaCopy
call SetScreen2XY
call SetScreen2
ld a, [Frame_Counter]
bit 4, a
jr z, .pal2
ld a, %00011111
ld [rOBP0], a
jr .endpal
.pal2:
ld a, %00001010
ld [rOBP0], a
.endpal:
call ReadStartButton
jr nz, .no_start
setvblanki VBLANK_title_out
ld hl, OAM_Data_wram
call LoadShip1_OAMS
call LoadShip2_OAMS
;;ld b, 12
;;call ClearOAMEntries
ld c, 16
.loop:
call LoadGas_OAMS
dec c
jr nz, .loop
.no_start
pop hl
pop af
reti
;
VBLANK_title_out:
call dmaCopy
push af
push hl
; Increment the transition until it reacher $00
ld hl, rLogoTransition
ld a, [hl]
cp $50
jr nz, .not_zero
setvblanki VBLANK_in_game
call Init_in_game
jr .return
.not_zero:
inc [hl]
inc [hl]
srl a
ld [Screen1Y], a
.return:
call SetScreen2XY
call SetScreen2
; Return VBLANK_title
pop hl
pop af
reti
;
Init_in_game:
ld a, $01
ld [InGame], a
xor a
ld [GasCount], a
ld [TickCount_old], a
ld [GasVectorPosition], a
ld [Ship1_Direction], a
ld [Ship1_PosXTimer], a
ld [Ship2_Direction], a
ld [Ship2_PosXTimer], a
ld a, IEF_VBLANK
ld [rIE], a
ld a, 3 ; replace SongID with the ID of the song you want to load
call DS_Init
ret
;
VBLANK_in_game:
call dmaCopy
call ReadShipMoveButton
call SetScreen2XY
call SetScreen2
reti
ReadStartButton:
ld hl, rP1
ld [hl], P1F_GET_BTN
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld [hl], P1F_GET_NONE
bit PADB_START, a
ret
; <>ba
;00000000 <- a
ReadShipMoveButton:
ld hl, rP1
ld [hl], P1F_GET_BTN
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld a, [hl]
cpl
and %11
ld b, a
ld [hl], P1F_GET_DPAD
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld a, [hl]
ld [hl], P1F_GET_NONE
cpl
and %11
rla
rla
add b
ld b, a
; b <- current buttons
ld a, [rButtons]
ld [rButtons_Old], a
cpl
and b
ld [rButtons_High], a
ld a, b
ld [rButtons], a
ret
UpdateGas:
ld a, [GasCount]
ld hl, OAM_Data_wram + $18
ld bc, 4
.loop
ld a, [hl] ; Y position
cp 0
jr z, .next
;; update gas
inc a
inc a