-
Notifications
You must be signed in to change notification settings - Fork 8
/
2048.asm
1531 lines (1378 loc) · 24.9 KB
/
2048.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 "constants.asm"
; rst vectors go unused
SECTION "rst00",HOME[0]
ret
SECTION "rst08",HOME[8]
ret
SECTION "rst10",HOME[$10]
ret
SECTION "rst18",HOME[$18]
ret
SECTION "rst20",HOME[$20]
ret
SECTION "rst30",HOME[$30]
ret
SECTION "rst38",HOME[$38]
ret
SECTION "vblank",HOME[$40]
jp VBlankHandler
SECTION "lcdc",HOME[$48]
reti
SECTION "timer",HOME[$50]
reti
SECTION "serial",HOME[$58]
reti
SECTION "joypad",HOME[$60]
reti
SECTION "bank0",HOME[$61]
SECTION "romheader",HOME[$100]
nop
jp Start150
Section "start",HOME[$150]
Start150:
jp Start
VBlankHandler:
push af
push bc
push de
push hl
ld a, [H_FAST_VCOPY]
and a
jr z, .regular
call FastVblank
xor a
ld [H_FAST_VCOPY], a
jr .copied
.regular
; load the bottom row (contains score)
call VblankCopyBottomRow
.skip
.copied
call $FF80
call ReadJoypadRegister
ld hl, H_TIMER
inc [hl]
call GetRNG
pop hl
pop de
pop bc
pop af
reti
FastVblank:
ld [W_SPTMP], sp
; ld a, [H_FAST_PART]
; and a
; jr nz, .bottom
;.top
ld sp, W_TILEMAP+22
ld hl, $9800 + $22
; jr .set
;.bottom
; ld sp, W_TILEMAP+82
; ld hl, $9800 + $82
;.set
ld b, 6
.loop
rept 2
popdetohli 8
ld de, $10
add hl, de
add sp, $4
endr
dec b
jr nz, .loop
; squeeze in one last row
popdetohli 8
ld de, $10
add hl, de
add sp, $4
;...and a bit
popdetohli 2
; and now wait patiently for blank
waithblank
; go!
popdetohli 6
ld de, $10
add hl, de
add sp, $4
waithblank
popdetohli 6
waithblank
popdetohli 2
ld de, $10
add hl, de
add sp, $4
popdetohli 4
waithblank
popdetohli 4
ld a, [W_SPTMP]
ld l, a
ld a, [W_SPTMP+1]
ld h, a
ld sp, hl
ret
VblankCopyBottomRow:
ld hl, W_TILEMAP+(20*17)
ld de, $9a20
rept 19
ld a, [hli]
ld [de], a
inc e
endr
ld a, [hl]
ld [de], a
ret
DisableLCD: ; $0061
xor a
ld [$ff0f],a
ld a,[$ffff]
ld b,a
res 0,a
ld [$ffff],a
.waitVBlank
ld a,[$ff44]
cp a,$91
jr nz,.waitVBlank
ld a,[$ff40]
and a,$7f ; res 7,a
ld [$ff40],a
ld a,b
ld [$ffff],a
ret
EnableLCD:
ld a,[$ff40]
set 7,a
ld [$ff40],a
ret
FadeToWhite:
lda [rBGP], %11100100
call Delay8Frames
FateToWhite_:
lda [rBGP], %10100100
call Delay8Frames
lda [rBGP], %01010100
call Delay8Frames
lda [rBGP], %00000000
jp Delay8Frames
FadeFromWhite:
lda [rBGP], %00000000
call Delay8Frames
lda [rBGP], %01010100
call Delay8Frames
lda [rBGP], %10100100
call Delay8Frames
lda [rBGP], %11100100
jp Delay8Frames
Delay8Frames:
ld b, 4 ; i lied
jp DelayFrames
DelayFrames:
.loop
halt
djnz .loop
ret
CopyData:
; copy bc bytes of data from hl to de
ld a,[hli]
ld [de],a
inc de
dec bc
ld a,c
or b
jr nz,CopyData
ret
CopyDataFF:
; copy data from hl to de ending with $ff (inclusive)
ld a,[hli]
ld [de],a
inc de
inc a
ret z
jr CopyDataFF
WriteDataInc:
; write data in hl increasing a until b.
.loop
ld [hli], a
inc a
cp a, b
jr nz, .loop
ret
FillMemory:
; write a in hl b times
.loop
ld [hli], a
dec b
jr nz, .loop
ret
ModuloC: ; modulo c
.loop
cp a, c
ret c
sub a, c
jr .loop
WriteSpriteRow:
; a = tile id
; b = amount
; de = xy
; hl = target
.loop
ld [hl], d
inc hl
ld [hl], e
ld c, a
ld a, e
add 8
ld e, a
ld a, c
inc hl
ld [hli], a
inc a
inc a
ld [hl], 0
inc hl
dec b
jr nz, .loop
ret
ClearOAM:
xor a
ld hl, W_OAM
ld b, 4*$28
call FillMemory
ret
ClearTilemap:
ld hl, W_TILEMAP
ld bc, 20*18
.loop
xor a
ld [hli], a
dec bc
ld a, b
or c
jr nz, .loop
ret
; a standard function:
; this function directly reads the joypad I/O register
; it reads many times in order to give the joypad a chance to stabilize
; it saves a result in [H_JOY] in the following format
; (set bit indicates pressed button)
; bit 0 - A button
; bit 1 - B button
; bit 2 - Select button
; bit 3 - Start button
; bit 4 - Right
; bit 5 - Left
; bit 6 - Up
; bit 7 - Down
ReadJoypadRegister: ; 15F
ld a, [H_JOY]
ld [H_JOYOLD], a
ld a,%00100000 ; select direction keys
ld c,$00
ld [rJOYP],a
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
cpl ; complement the result so that a set bit indicates a pressed key
and a,%00001111
swap a ; put direction keys in upper nibble
ld b,a
ld a,%00010000 ; select button keys
ld [rJOYP],a
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
ld a,[rJOYP]
cpl ; complement the result so that a set bit indicates a pressed key
and a,%00001111
or b ; put button keys in lower nibble
ld [$fff8],a ; save joypad state
ld a,%00110000 ; unselect all keys
ld [rJOYP],a
ld a, [H_JOY]
ld b, a
ld a, [H_JOYOLD]
xor $ff
and b
ld [H_JOYNEW], a
ld a, [H_JOY]
cp %00001111
ret nz
jp Start150
ret
GetRNG:
ld a, [rDIV]
ld b, a
ld a, [H_RNG1]
xor b
ld [H_RNG1], a
ret
WaitForKey:
.loop
halt
ld a, [H_JOYNEW]
and a, %00001001 ; A or START
jr z, .loop
ret
; copies DMA routine to HRAM. By GB specifications, all DMA needs to be done in HRAM (no other memory section is available during DMA)
WriteDMACodeToHRAM:
ld c, $80
ld b, $a
ld hl, DMARoutine
.copyLoop
ld a, [hli]
ld [$ff00+c], a
inc c
dec b
jr nz, .copyLoop
ret
; this routine is copied to HRAM and executed there on every VBlank
DMARoutine:
ld a, W_OAM >> 8
ld [$ff00+$46], a ; start DMA
ld a, $28
.waitLoop ; wait for DMA to finish
dec a
jr nz, .waitLoop
ret
OpenSRAM:
ld a, SRAM_ENABLE
ld [MBC1SRamEnable], a
xor a
ld [MBC1SRamBankingMode], a
ld [MBC1SRamBank], a
ret
CloseSRAM:
xor a
ld [MBC1SRamBankingMode], a
ld [MBC1SRamEnable], a
ret
LoadHighScoreFromSRAM:
call OpenSRAM
ld hl, $a000
ld a, [hli]
cp $20
jr nz, CloseSRAM
ld a, [hli]
cp $48
jr nz, CloseSRAM
lda [H_HIGHSCORE], [hli]
lda [H_HIGHSCORE+1], [hli]
lda [H_HIGHSCORE+2], [hli]
lda [H_GAMESBEATEN], [hli]
lda [H_GAMESBEATEN+1], [hli]
jp CloseSRAM
WriteHighScoreToSRAM:
call OpenSRAM
ld hl, $a000
lda [hli], $20
lda [hli], $48
lda [hli], [H_HIGHSCORE]
lda [hli], [H_HIGHSCORE+1]
lda [hli], [H_HIGHSCORE+2]
lda [hli], [H_GAMESBEATEN]
lda [hli], [H_GAMESBEATEN+1]
jp CloseSRAM
Start:
di
; palettes
ld a, %11100100
ld [rBGP], a
ld a, %11010000
ld [rOBP0], a
xor a
ld [rSCX], a
ld [rSCY], a
ld a, %11000111
ld [rLCDC], a
ei
call DisableLCD
; seed the RNG
ld hl, $C000
ld l, [hl]
ld a, [hl]
push af
; fill the memory with zeroes
ld hl, $C000
.loop
ld a, 0
ld [hli], a
ld a, h
cp $e0
jr nz, .loop
pop af
; set up the stack pointer
ld sp, $dffe
push af
ld hl, $ff80
.loop2
ld a, 0
ld [hli], a
ld a, h
cp $00
jr nz, .loop2
pop af
ld [H_RNG1], a
call WriteDMACodeToHRAM
; set up vblank copy offsets
lda [H_FIRSTVBLANK], 1
; set up ingame graphics
ld hl, Title
ld de, $9000
ld bc, $800
call CopyData
ld hl, Title+$800
ld de, $8800
ld bc, $100
call CopyData
ld hl, TitleTilemap
ld de, W_TILEMAP
ld bc, TitleTilemapEnd-TitleTilemap
call CopyData
call FastVblank
call LoadHighScoreFromSRAM
call EnableLCD
ld [$ffff], a
ld a, %00000001
ld [$ffff], a
ei
halt
xor a
ld [H_FIRSTVBLANK], a
call WaitForKey
call FadeToWhite
call ClearTilemap
call DisableLCD
; set up ingame graphics
ld hl, Tiles
ld de, $9000
ld bc, $800
call CopyData
ld hl, Tiles+$800
ld de, $8800
ld bc, $800
call CopyData
ld hl, Sprites
ld de, $8000
ld bc, $800
call CopyData
lda [H_FAST_VCOPY], 1
call EnableLCD
jp InitGame
TitleTilemap:
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $0f, $01, $02, $03, $04, $05, $06, $07, $08, $09, $0a, $0b, $0c, $0d, $0e, $00, $00, $00
db $00, $00, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $1a, $1b, $1c, $1d, $1e, $1f, $00, $00
db $00, $00, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $2a, $2b, $2c, $2d, $2e, $2f, $00, $00
db $00, $00, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $3a, $3b, $3c, $3d, $3e, $3f, $00, $00
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $7e, $7f, $00, $00, $00, $00, $00
db $00, $00, $40, $41, $42, $43, $44, $45, $46, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $47, $48, $49, $4a, $4b, $4c, $4d, $4e, $4f, $50, $51, $52, $53, $54, $00, $00
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $55, $56, $57, $58, $59, $5a, $5b, $5c, $5d, $86, $87, $88, $00, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $5e, $5f, $60, $61, $62, $63, $64, $65, $66, $67, $68, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $69, $6a, $6b, $6c, $6d, $6e, $6f, $70, $71, $72, $73, $74, $00, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $75, $76, $77, $78, $79, $7a, $7b, $7c, $7d, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
db $00, $00, $00, $00, $00, $00, $00, $80, $81, $82, $83, $84, $85, $00, $00, $00, $00, $00, $00, $00
TitleTilemapEnd
ModuloB:
.loop
cp a, b
ret c
sub a, b
jr .loop
DivB:
ld c, 0
.loop
cp a, b
jr c, .ret
sub a, b
inc c
jr .loop
.ret
ld a, c
ret
Powers:
;dw 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
dw $0001, $0002, $0004, $0008, $0016, $0032
dw $0064, $0128, $0256, $0512, $1024, $2048
dw $4096, $8192
AddScore:
push hl
push de
ld hl, Powers
ld e, a
ld d, 0
add hl, de
add hl, de
ld a, [hli]
ld h, [hl]
ld l, a
ld [H_PLUSSCORE+1], a
ld a, h
ld [H_PLUSSCORE], a
push hl
pop de
ld hl, H_SCORE+2
ld a, [hl]
add e
daa
ld [hld], a
ld a, [hl]
adc d
daa
ld [hld], a
jr nc, .nc
inc [hl]
.nc
ld hl, H_SCORE
ld de, H_HIGHSCORE
ld a, [de]
cp [hl]
jr z, .maybe1
jr nc, .nothi
jr c, .new
.maybe1
inc de
inc hl
ld a, [de]
cp [hl]
jr z, .maybe2
jr nc, .nothi
jr c, .new
.maybe2
inc de
inc hl
ld a, [de]
cp [hl]
jr z, .nothi
jr nc, .nothi
jr c, .new
.new
ld de, H_HIGHSCORE+2
ld hl, H_SCORE+2
lda [de], [hld]
dec de
lda [de], [hld]
dec de
lda [de], [hl]
call WriteHighScoreToSRAM
.nothi
pop de
pop hl
ret
Modulo10: ; bc %= 10
.loop
ld a, b
and a
jr nz, .nz
ld a, c
cp 10
ret c
.nz
ld a, c
sub 10
ld c, a
jr nc, .loop
dec b
jr .loop
Div10: ; bc /= 10
push de
ld de, 0
.loop
ld a, b
and a
jr nz, .nz
ld a, c
cp 10
jr c, .ret
.nz
ld a, c
sub 10
ld c, a
jr nc, .ok
dec b
.ok
inc de
jr .loop
.ret
push de
pop bc
pop de
ret
WriteBCDByte:
ld a, [de]
swap a
and $0f
add $e8
ld [hli], a
ld a, [de]
and $0f
add $e8
ld [hli], a
inc de
ret
WriteNumber:
; writes number at de to hl
call WriteBCDByte
call WriteBCDByte
call WriteBCDByte
ret
SprWriteNumAndCarry:
push bc
call Modulo10
ld a, c
add a
add $60
ld [hld], a
pop bc
call Div10
ret
SprWriteNumber:
ld a, [de]
ld c, a
inc de
ld a, [de]
ld b, a
call SprWriteNumAndCarry
call SprWriteNumAndCarry
call SprWriteNumAndCarry
call SprWriteNumAndCarry
ret
UpdateTilemapScore:
; draw score
hlcoord $11, 0
ld a, $e0
ld [hli], a
inc a
ld [hli], a
inc a
ld [hli], a
hlcoord $11, $9
ld a, $e3
ld [hli], a
rept 4
inc a
ld [hli], a
endr
hlcoord $11, 3
ld de, H_SCORE
call WriteNumber
hlcoord $11, $e
ld de, H_HIGHSCORE
call WriteNumber
ret
GridTilemap:
db $00, $00, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $00, $00
db $00, $00, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $00, $00
db $00, $00, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $00, $00
db $00, $00, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $00, $00
db $00, $00, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $00, $00
db $00, $00, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $00, $00
db $00, $00, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $00, $00
db $00, $00, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $00, $00
db $00, $00, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $00, $00
db $00, $00, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $00, $00
db $00, $00, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $00, $00
db $00, $00, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $00, $00
db $00, $00, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $10, $11, $12, $13, $00, $00
db $00, $00, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $14, $15, $16, $17, $00, $00
db $00, $00, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $18, $19, $1A, $1B, $00, $00
db $00, $00, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $1C, $1D, $1E, $1F, $00, $00
DetermineFastVcopyPart: ; XXX unused, remove
ld hl, W_ANIMFRAMES
ld a, [H_ANIMFRAME]
swap a
add 12
ld l, a
; if anything in the bottom row is moving, we need to update it.
rept 4
ld a, [hli]
bit 7, a
jr nz, .bottom
endr
; if we're moving down and anything in the third row is moving, we need
; to update it.
ld a, [H_ANIMDIR]
and a
jr nz, .top
ld hl, W_ANIMFRAMES
ld a, [H_ANIMFRAME]
swap a
add 8
ld l, a
rept 4
ld a, [hli]
bit 7, a
jr nz, .bottom
endr
.top
xor a
;ld [H_FAST_PART], a
ret
.bottom
ld a, 1
;ld [H_FAST_PART], a
ret
TilemapAddValues:
; down up left right
dw 20, -20, -1, 1
UpdateTilemap:
ld a, [H_ANIMDIR]
add a
ld hl, TilemapAddValues
ld d, 0
ld e, a
add hl, de
ld a, [hli]
ld [H_CURTMAPADD], a
ld a, [hl]
ld [H_CURTMAPADD+1], a
; draw 2048 tiles
ld hl, GridTilemap
decoord 1, 0
ld bc, 320
di
call CopyData
ld bc, $0000
ld a, [H_ANIMATE]
and a
jr z, .regular
ld de, W_ANIMFRAMES
ld a, [H_ANIMFRAME]
swap a
ld e, a
jr .picked
.regular
ld de, W_2048GRID
.picked
hlcoord 1, 2
dec de
.loop
inc de
ld a, [de]
res 7, a
and a
jr z, .blank
inc a
sla a
sla a
sla a
sla a
push af
push bc
push de
ld a, [de]
bit 7, a
jr z, .noanim
; anim
ld a, [H_ANIMSUB]
and a
jr z, .noanim
ld b, a
ld a, [H_CURTMAPADD]
ld e, a
ld a, [H_CURTMAPADD+1]
ld d, a
.shiftloop
add hl, de
dec b
jr nz, .shiftloop
.noanim
pop de
pop bc
pop af
ld c, 4
.tileloop
ld [hli], a
inc a
ld [hli], a
inc a
ld [hli], a
inc a
ld [hli], a
inc a
push bc
ld bc, 16
add hl, bc
pop bc
dec c
jr nz, .tileloop
push bc
ld bc,0 -((20*4)-4)
add hl, bc
pop bc
push af
push bc
push de
ld a, [de]
bit 7, a
jr z, .noanim2
; anim
ld a, [H_ANIMSUB]
and a
jr z, .noanim2
ld b, a
ld a, [H_CURTMAPADD]
cpl
ld e, a
ld a, [H_CURTMAPADD+1]
cpl
ld d, a
.shiftloop2
add hl, de
inc hl
dec b
jr nz, .shiftloop2
.noanim2
pop de
pop bc
pop af
.next
inc b
ld a, b
cp 16
jr z, .ret
and %00000011
jr nz, .loop
push bc
ld bc, 20*3+4
add hl, bc
pop bc
jr .loop
.blank
inc hl
inc hl
inc hl
inc hl
jr .next
.ret
ei
ret
GameOver:
ld a, 1
ld [H_GAMEOVER], a
ld a, %10010100
ld [rBGP], a
xor a
ld b, 10
ld de, $4034
ld hl, W_OAM
call WriteSpriteRow
ld a, $30
ld b, 6
ld de, $6444
ld hl, W_OAM+4*10
call WriteSpriteRow
ret
YouWin:
ld a, 2
ld [H_GAMEOVER], a
ld hl, H_GAMESBEATEN
lda d, [hli]
lda e, [hl]
inc de
lda [hld], e
lda [hl], d
call WriteHighScoreToSRAM
ld a, %10010100
ld [rBGP], a
ld a, $14
ld b, 9
ld de, $4038
ld hl, W_OAM
call WriteSpriteRow
ld a, $30
ld b, 6
ld de, $6444
ld hl, W_OAM+4*9
call WriteSpriteRow
ret
Has2048Tile:
ld a, [H_CONTINUING]