forked from GuillianSeed/MetalGear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Banks0123.asm
13784 lines (10556 loc) · 354 KB
/
Banks0123.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
;----------------------------------------------------------------------------
;
; ROM header
;
;----------------------------------------------------------------------------
org #4000
db 41h, 42h
dw Start
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
db 43h, 44h ; RC id
db 7, 50h ; RC750
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
dw 0
; These pointers are not used by the game.
; Since they are related to the sound driver, probably were added to be used by a future cartridge (i.e.: Game Master 2) in order to play musics and SFXs from the game
dw MuteSoundFlag ; 1 = Mute
dw SoundWorkArea+2
dw SoundWorkAreaB+2
dw SoundWorkAreaC+2
dw SoundWorkArea
dw SoundWorkArea+1
dw SoundWorkAreaB
dw SoundWorkAreaB+1
dw SoundWorkAreaC
dw SoundWorkAreaC+1
dw idxSoundData
dw SoundDataSaved
dw MusicToSet ; New music to play (fade out current one)
dw SoundWorkAreaSfx+2
dw SoundWorkAreaSfx
dw SoundWorkAreaSfx+1
;----------------------------------------------------------------------------
;
; Functions entry points
;
;----------------------------------------------------------------------------
ADD_HL_A_:
call ADD_HL_A
jr SetBanks_1_2_X___
ADD_DE_A__:
call ADD_DE_A
jr SetBanks_1_2_X___
SetSoundEntry__:
call SetSoundEntryChk
jr SetBanks_1_2_X___
SetText_:
call SetText
jr SetBanks_1_2_X___
ChkControlPlayer_:
call ChkControlPlayer
jr SetBanks_1_2_X___
ControlPlayerV_:
call ControlPlayerV
jr SetBanks_1_2_X___
ChkPlayerColl_:
call ChkPlayerColl
jr SetBanks_1_2_X___
ChkExitScreen_:
call ChkExitRoom
jr SetBanks_1_2_X___
SetPlayerSpr_:
call AnimatePlayer
jr SetBanks_1_2_X___
DrawRadio_:
call DrawRadio
jr SetBanks_1_2_X___
RadioSignalUp_:
call RadioSignalUp
jr SetBanks_1_2_X___
ExitRadio_:
call ExitRadio
jr SetBanks_1_2_X___
Load1bppGFX_:
call Load1bppTiles
jr SetBanks_1_2_X___
Load2bppTile_:
call Load2bppTile
jr SetBanks_1_2_X___
FillRect_:
call FillRect
jr SetBanks_1_2_X___
ClearScreen_:
call ClearScreen
jr SetBanks_1_2_X___
DisableScreenBnk:
call DisableScreen
SetBanks_1_2_X___:
jr SetBanks_1_2_X____
VDP_Copy_Dot_:
call VDP_Copy_Dot
jr SetBanks_1_2_X____
EnableScreenBnk:
call EnableScreen
jr SetBanks_1_2_X____
PrintTextXY_:
call PrintTextXY
jr SetBanks_1_2_X____
DrawTileP1_:
call DrawTile_P1
jr SetBanks_1_2_X____
GetDirLeftRight_:
call GetDirLeftRight
jr SetBanks_1_2_X____
GetPointerDE2A_:
call GetPointerDE2A
jr SetBanks_1_2_X____
SetVRAMAddWR_:
call SetVramAddressWR
jr SetBanks_1_2_X____
RAMtoVRAM__:
call RAMtoVRAM
jr SetBanks_1_2_X____
SetPaletteColor_:
call SetPaletteColor
jr SetBanks_1_2_X____
GetItemInvAdd_:
call GetItemInvAdd
jr SetBanks_1_2_X____
IncClassLv_:
call IncClassLv
jr SetBanks_1_2_X____
EraseTextXY_:
call EraseTextXY
jr SetBanks_1_2_X____
InitGameArea:
call InitGame4
jr SetBanks_1_2_X____
DrawChar_:
call DrawChar
jr SetBanks_1_2_X____
ClearSprites_:
call ClearSprAttr
jr SetBanks_1_2_X____
DrawMetalGear_:
call DrawMetalGear
SetBanks_1_2_X____:
jr SetBanks_1_2_X__
ClearPage0_:
call ClearPage0
jr SetBanks_1_2_X__
LoadRoomTiles_:
call LoadRoomTiles
jr SetBanks_1_2_X__
ChkRoomPal_:
call SetRoomPal
jr SetBanks_1_2_X__
DrawTileBlock_:
call DrawTileBlkTimp
jr SetBanks_1_2_X__
EraseMetalGear_:
call EraseMetalGear
jr SetBanks_1_2_X__
StoreControls__:
call StoreControls_
jr SetBanks_1_2_X__
SetDefaultPal_:
call SetDefaultPal
jr SetBanks_1_2_X__
SetPalette_:
call SetPalette
jr SetBanks_1_2_X__
SetUpKonamiLogo_:
call SetUpKonamiLogo
jr SetBanks_1_2_X__
VdpCopyByteBnks:
call VDP_Copy_Byte
jr SetBanks_1_2_X__
SetTextUnskip_:
call SetTextUnskippable
jr SetBanks_1_2_X__
DrawDestrucTimer_:
call DrawDestrucTimer
jr SetBanks_1_2_X__
DrawLineVert_:
call DrawLineVert
jr SetBanks_1_2_X__
DrawLineHoriz_:
call DrawLineHoriz
jr SetBanks_1_2_X__
DismissActor2_:
call DismissActor
jr SetBanks_1_2_X__
SetSound_:
call SetSoundEntry
SetBanks_1_2_X__:
jr SetBanks_1_2_X_
SetRoomPal_:
call SetRoomPal2
jr SetBanks_1_2_X_
TextBoxLogic_:
call TextBoxLogic
jr SetBanks_1_2_X_
DrawRecv_:
call PrintRecv
jr SetBanks_1_2_X_
DrawSend_:
call PrintSend
jr SetBanks_1_2_X_
InitGame_:
call InitGame
jr SetBanks_1_2_X_
GameLogic_:
call GameLogic ; ;
jr SetBanks_1_2_X_
ClearPage_:
call ClearPage
jr SetBanks_1_2_X_
Load3pppTile_:
call Load3bppTiles
jr SetBanks_1_2_X_
SetMenuWeaponPal_:
call SetMenuWeaponPal
jr SetBanks_1_2_X_
LoadGameGfx_:
call LoadGameGfx
jr SetBanks_1_2_X_
CopyPalToRAM_:
call CopyPalToRAM
jr SetBanks_1_2_X_
FadeOutLogic_:
call FadeOutLogic
jr SetBanks_1_2_X_
DrawRadioFreq_:
call DrawRadioFreq
jr SetBanks_1_2_X_
SetSnakeSprAttr_:
call SetSnakeSprAtt
jr SetBanks_1_2_X_
SetSnakeSprPatt_:
call SetSnakeSprPatt
jr SetBanks_1_2_X_
EraseSprAttRAM_:
call EraseSprAttRAM
SetBanks_1_2_X_:
jr SetBanks_1_2_X
ClearGameVars_:
call ClearGameVars
;----------------------------------------------------------------------------
;
; Set banks 1, 2, (3)
;
; #6000-#7FFF: Bank 1
; #8000-#9FFF: Bank 2
; #A000-#BFFF: Bank 3 or (BankInA0Fixed)
;
; Bank 3 will be set to the value (BankInA0Fixed) when it is not 0
;----------------------------------------------------------------------------
SetBanks_1_2_X:
call SetBanks1_2_3
push af
ld a, (BankInA0Fixed)
and a
jr z, SetBanks_1_2_X2
di
ld (0A000h), a
ld (BankInA0), a
ei
SetBanks_1_2_X2:
pop af
ret
;----------------------------------------------------------------------------
;
; Interrupt handler
;
; Update the sound
; Execute a game iteration if the previous one has finished
;
;----------------------------------------------------------------------------
InterruptTick:
di
call RDVDP ; Read VDP status register to clear interrupt flag
ld a, 4
ld (6000h), a ; Mapper register: bank at #6000-#7FFF
inc a
ld (8000h), a ; Mapper register: bank at #8000-#9FFF
call UpdateSound ; Update sound: music and sfx
ld a, (BankIn60)
ld (6000h), a
ld a, (BankIn80)
ld (8000h), a ; Restore previous banks in #4000-#9FFF
ld hl, TickInProgress
bit 0, (hl)
jp nz, InterruptTick2 ; There was an game iteration in progess. Skip this iteration
inc (hl) ; Set "iteration in progress" flag
ei ; Enable interrupts
call GameStatusLogic ; Main logic
xor a
ld (TickInProgress), a ; Erase "iteration in progress flag"
InterruptTick2:
call RDVDP ; Read VDP status register to clear interrupt flag
ei
ret
;----------------------------------------------------------------------------
;
; HL = HL + (A - 1) * 2
;
;----------------------------------------------------------------------------
ADD_HL_2A_DEC:
dec a
;----------------------------------------------------------------------------
;
; HL = HL + (A * 2)
;
;----------------------------------------------------------------------------
ADD_HL_2A:
add a, a
;----------------------------------------------------------------------------
;
; ADD HL,A
; HL = HL + A
;
;----------------------------------------------------------------------------
ADD_HL_A:
add a, l
ld l, a
ret nc
inc h
ret
;----------------------------------------------------------------------------
;
; DE = DE + A
;
;----------------------------------------------------------------------------
ADD_DE_A:
add a, e
ld e, a
ret nc
inc d
ret
;----------------------------------------------------------------------------
;
; Jump index
;
; In:
; A = Pointer index
; (SP) = Pointer to jump addresses list
;
;----------------------------------------------------------------------------
JumpIndex:
pop hl ; Pointer to list
call ADD_HL_2A
ld e, (hl)
inc hl
ld d, (hl) ; DE = Address to jump
ex de, hl
jp (hl)
;----------------------------------------------------------------------------
;
;
; ROM entry point
;
;
;----------------------------------------------------------------------------
Start:
di
ld sp, Stack
call RSLREG ; Read primary slot register
rrca
rrca
and 3 ; Main slot of the cartridge
ld c, a
ld b, 0
ld hl, EXPTBL ; Expanded slot table
add hl, bc
ld a, (hl)
and 80h ; Expanded flag
or c
ld c, a
inc hl
inc hl
inc hl
inc hl
ld a, (hl)
and 1100b ; Subslot in page 1 #4000-#7FFF
or c
ld h, 80h
call ENASLT ; Set cartridge slot in page 2 (#8000-#BFFF)
ld hl, GameStatus
ld de, GameSubstatus
ld bc, 30EFh
ld (hl), l
ldir ; Clear RAM area used as variables
call SetBanks1_2_3
call RegionLock ; In the japanese version, this is the region lock check
xor a
ld hl, BankIn60Fixed
ld (hl), a
inc hl
ld (hl), a
inc hl
ld (hl), a
call InitHardware ; Initialize PSG, VDP, VRAM
di
ld a, 0C3h ; Z80 opcode: JP
ld (HTIMI), a
ld hl, InterruptTick
ld (HTIMI+1), hl ; Set interrupt hook
xor a
ld (CLIKSW), a ; Disable key click sound
ei
DummyLoop:
jr $ ; All the logic is handled in the interrupt routine
;----------------------------------------------------------------------------
;
; Set banks 1, 2, 3
;
; #6000-#7FFF: Bank 1
; #8000-#9FFF: Bank 2
; #A000-#BFFF: Bank 3
;
;----------------------------------------------------------------------------
SetBanks1_2_3:
di
push af
ld a, 1
jr SetBanks
;----------------------------------------------------------------------------
;
; Set banks 4,5,6
;
; #6000-#7FFF: Bank 4
; #8000-#9FFF: Bank 5
; #A000-#BFFF: Bank 6
;
;----------------------------------------------------------------------------
SetBanks_4_5_6:
di
push af
ld a, 4
;----------------------------------------------------------------------------
;
; Set banks
;
; In:
; A = Bank number
;
; #6000-#7FFF: Bank number
; #8000-#9FFF: Bank number + 1
; #A000-#BFFF: Bank number + 2
;
;----------------------------------------------------------------------------
SetBanks:
push hl
ld hl, BankIn60
ld (6000h), a
ld (hl), a
inc a
inc hl
ld (8000h), a
ld (hl), a
inc a
inc hl
ld (0A000h), a
ld (hl), a
pop hl
pop af
ei
ret
;----------------------------------------------------------------------------
;
; Set banks 7,8,9
;
; #6000-#7FFF: Bank 7
; #8000-#9FFF: Bank 8
; #A000-#BFFF: Bank 9
;
;----------------------------------------------------------------------------
SetBanks_7_8_9:
di
push af
ld a, 7
jr SetBanks
;----------------------------------------------------------------------------
;
; Set banks A,B,C
;
; #6000-#7FFF: Bank #A
; #8000-#9FFF: Bank #B
; #A000-#BFFF: Bank #C
;
;----------------------------------------------------------------------------
SetBanks_A_B_C:
di
push af
ld a, 0Ah
jr SetBanks
;----------------------------------------------------------------------------
;
; Set banks D,E,F
;
; #6000-#7FFF: Bank #D
; #8000-#9FFF: Bank #E
; #A000-#BFFF: Bank #F
;
;----------------------------------------------------------------------------
SetBanks_D_E_F:
di
push af
ld a, 0Dh
jr SetBanks
;----------------------------------------------------------------------------
;
; Set sound entry
;
; If sound is enabled, select sound driver banks and play a sound/music
;
; In:
; A = Sound ID
;
;----------------------------------------------------------------------------
SetSoundEntryChk:
push hl
ld hl, ControlConfig ; Bit6: 1=Enable music/Player control
bit 6, (hl)
pop hl
ret z ; Sound disabled
SetSoundEntry:
di
push hl
push de
push bc ; Save BC, DE, HL
ld hl, (BankIn60)
push hl ; Save current banks at #6000-#7FFF and #8000-#9FFF
push af
ld a, 4
ld (6000h), a
ld (BankIn60), a
inc a
ld (BankIn80), a
ld (8000h), a ; Set sound driver banks
pop af
call SetSound
pop hl
ld (BankIn60), hl
ld a, l
ld (6000h), a
ld a, h
ld (8000h), a ; Restore previous banks at #6000-#7FFF and #8000-#9FFF
pop bc
pop de
pop hl ; Restore BC, DE, HL
ei
ret
;----------------------------------------------------------------------------
;
; Get a pointer from an array of pointers depending on the room number
;
; In:
; DE = Array of pointers
;
; Out:
; DE = Pointer
;
;----------------------------------------------------------------------------
GetRoomPointer:
ld a, (Room)
GetPointerDE2A:
ld l, a
ld h, 0
add hl, hl
add hl, de
ld e, (hl)
inc hl
ld d, (hl)
ret
;----------------------------------------------------------------------------
;
; HL = (A-1) * 4
;
;----------------------------------------------------------------------------
DEC_A_HL_4xA:
dec a
HL_4xA:
ld l, a
ld h, 0
add hl, hl
add hl, hl
ret
;----------------------------------------------------------------------------
;
; Set the 8 colors numbers used by tilesets
; These colors are used when decoding 3 or 2 bits per pixel graphics
;
;----------------------------------------------------------------------------
SetTilesetColors:
ld hl, ColorsTileset
SetColorsIndexes:
ld c, 8
SetColorsIndexes2:
ld de, BufferColor ; Buffer used to store the colors' indexes for decoding 2/3bpp graphics
ld b, 0
ldir
ret
;----------------------------------------------------------------------------
;
; Get a nibble from HL array depending on the room number
;
; in:
; HL = Pointer to array of nibbles
;----------------------------------------------------------------------------
GetNibbleRoom:
ld a, (Room)
;----------------------------------------------------------------------------
;
; Get a nibble from HL array
;
; in:
; A = Position
; HL = Pointer to array of nibbles
;----------------------------------------------------------------------------
GetNibbleHL_A2:
ld c, a
srl a
call ADD_HL_A
bit 0, c
ld a, (hl)
jr nz, GetNibbleHL_A3
rra
rra
rra
rra
GetNibbleHL_A3:
and 0Fh
ret
;----------------------------------------------------------------------------
;
; Get next room number using room's connections
;
; In:
; (Room) = current room
; (NextRoomDirect) = Exit direction
;
; Out:
; A = Room number (#FF = undefined)
;----------------------------------------------------------------------------
GetNextRoomNum:
call SetBanks_4_5_6
ld c, 0FFh
ld a, (Room)
cp 126 ; First lorry room
jr c, GetNextRoomNum3 ; Rooms 0-125 have 1:1 exit table index relation
cp 208
jr c, GetNextRoomNum4 ; Rooms 126-207 (loories and isolated rooms) are not connected to other rooms (only using a door)
cp 228
jr c, GetNextRoomNum2 ; Rooms 208-227 use exit table (126-145)
; Probably room 227 was another ladder. Room connections seem right
; Rooms 227-239 are undefined
cp 241 ; Elevators rooms 241-250
; Room 240 (first elevator) does not use room connections
jr c, GetNextRoomNum4
sub 13
GetNextRoomNum2:
sub 82
GetNextRoomNum3:
ld de, RoomConnections
call HL_4xA
add hl, de
ld a, (NextRoomDirect) ; 4=Right, 3=Left, 2=Down, 1=Up
dec a
call ADD_HL_A
ld c, (hl)
GetNextRoomNum4:
ld a, c
jp SetBanks1_2_3
;----------------------------------------------------------------------------
;
; Get elevator and player Y coordinate to match the floor used to enter the elevator
;
; Out:
; C = Player Y
; B = Elevator Y
;----------------------------------------------------------------------------
GetElevatorPosY:
call GetElevatorRoomDat ; Get elevator data and set movement limits
call SetBanks_4_5_6
inc hl
ld a, (PreviousRoom) ; Get the room used to enter the elevator
GetElevatorPos2:
cp (hl) ; search the same room in the elevator data
jr z, GetElevPlayerY
inc hl
inc hl
inc hl
jr GetElevatorPos2
; Sets the player and elevator Y coordinates to match the floor used to enter
GetElevPlayerY:
inc hl
ld c, (hl) ; Player Y
inc hl
ld b, (hl) ; Elevator Y
jr SetBanks_1_2_3_
;----------------------------------------------------------------------------
;
; Get elevator data and set movement limits
;
; In:
; (Room) = Elevator room
; Out:
; HL = Elevator data + 1
;
; +0 = Elevator Limit up, limit down
; +2 = previous room (room used to enter the elevator)
; +3 = Y player, Y elevator
; (repeat +2, +3) depending on the number of exits
;----------------------------------------------------------------------------
GetElevatorRoomDat:
call SetBanks_4_5_6
ld a, (Room)
sub 0F0h ; First elevator room
ld de, idxElevatorRoom
call GetPointerDE2A
ex de, hl
ld a, (hl)
ld (ElevatorLimitUp), a ; Set elevator top limit
inc hl
ld a, (hl)
ld (ElevatorLimitDown), a ; Set elevator bottom limit
jr SetBanks_1_2_3_
;----------------------------------------------------------------------------
;