dmgtris/src/main.asm

147 lines
3.0 KiB
NASM
Raw Normal View History

2023-10-21 15:28:38 +00:00
; DMGTRIS
; Copyright (C) 2023 - Randy Thiemann <randy.thiemann@gmail.com>
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-10-16 05:47:11 +00:00
IF !DEF(MAIN_ASM)
DEF MAIN_ASM EQU 1
2023-10-10 08:32:24 +00:00
INCLUDE "globals.asm"
INCLUDE "res/tiles.inc"
INCLUDE "res/gameplay_map.inc"
2023-10-20 16:09:06 +00:00
INCLUDE "res/title_map.inc"
2023-10-10 08:32:24 +00:00
2023-10-16 05:47:11 +00:00
2023-10-21 18:49:52 +00:00
SECTION "High Globals", HRAM
2023-10-18 02:09:12 +00:00
hGameState:: ds 1
2023-10-23 10:16:16 +00:00
2023-10-16 05:47:11 +00:00
2023-10-23 04:17:34 +00:00
SECTION "Globals", WRAM0
2023-10-23 10:16:16 +00:00
wSwapABState:: ds 1
wRNGModeState:: ds 1
wRotModeState:: ds 1
wDropModeState:: ds 1
wSpeedCurveState:: ds 1
wAlways20GState:: ds 1
2023-10-23 04:17:34 +00:00
wInitialA:: ds 1
wInitialB:: ds 1
wInitialC:: ds 1
wInitialD:: ds 1
wInitialE:: ds 1
wInitialH:: ds 1
wInitialL:: ds 1
2023-10-16 05:47:11 +00:00
2023-10-18 01:02:46 +00:00
SECTION "Stack", WRAM0
2023-10-18 01:13:20 +00:00
wStack::
2023-10-18 09:59:22 +00:00
ds STACK_SIZE + 1
2023-10-18 01:13:20 +00:00
wStackEnd::
2023-10-18 01:02:46 +00:00
2023-10-10 08:32:24 +00:00
SECTION "Code Entry Point", ROM0
2023-10-13 06:38:10 +00:00
Main::
2023-10-23 04:17:34 +00:00
; Load the initial registers. For reasons.
ld [wInitialA], a
ld a, b
ld [wInitialB], a
ld a, c
ld [wInitialC], a
ld a, d
ld [wInitialD], a
ld a, e
ld [wInitialE], a
ld a, h
ld [wInitialH], a
ld a, l
ld [wInitialL], a
2023-10-21 22:34:55 +00:00
2023-10-10 08:32:24 +00:00
; Turn off LCD during initialization.
2023-10-13 06:38:10 +00:00
wait_vram
xor a, a
ldh [rLCDC], a
2023-10-11 06:18:12 +00:00
2023-10-18 01:13:20 +00:00
; Set up stack
2023-10-18 09:59:22 +00:00
ld sp, wStackEnd-1
2023-10-18 01:02:46 +00:00
2023-10-24 07:04:39 +00:00
; GBC? Double speed mode and set up palettes.
ld a, [wInitialA]
cp a, $11
jr nz, .notgbc
ld a, KEY1F_PREPARE
ldh [rKEY1], a
stop
.notgbc
2023-10-25 04:40:08 +00:00
; Initialize the mapper.
ld a, CART_SRAM_ENABLE
ld [rRAMG], a
xor a, a
ld [rRAMB], a
ld a, BANK("Static Data")
ld [rROMB0], a
2023-10-24 07:04:39 +00:00
2023-10-10 08:32:24 +00:00
; We use a single set of tiles for the entire game, so we copy it at the start.
ld de, Tiles
2023-10-11 06:18:12 +00:00
ld hl, _VRAM
2023-10-10 08:32:24 +00:00
ld bc, TilesEnd - Tiles
call UnsafeMemCopy
2023-10-11 06:18:12 +00:00
; Clear OAM.
call ClearOAM
2023-10-20 16:09:06 +00:00
call SetNumberSpritePositions
2023-10-11 06:18:12 +00:00
call CopyOAMHandler
2023-10-25 04:40:08 +00:00
; Other initialization.
call RestoreSRAM
2023-10-18 01:02:46 +00:00
call TimeInit
call IntrInit
call InputInit
call SFXInit
2023-10-11 06:18:12 +00:00
; Set up the interrupt handlers.
2023-10-10 08:32:24 +00:00
call InitializeLCDCInterrupt
2023-10-16 05:47:11 +00:00
; Switch to gameplay state.
2023-10-20 16:09:06 +00:00
call SwitchToTitle
2023-10-13 06:38:10 +00:00
2023-10-10 08:32:24 +00:00
2023-10-16 05:47:11 +00:00
EventLoop::
2023-10-16 11:27:08 +00:00
; Play the sound effect, if any.
call SFXPlay
2023-10-16 05:47:11 +00:00
; Wrangle inputs and timers at the start of every frame.
2023-10-10 08:32:24 +00:00
call GetInput
call HandleTimers
2023-10-16 05:47:11 +00:00
; Call the current state's event handler.
2023-10-18 02:45:58 +00:00
ld b, 0
2023-10-18 02:09:12 +00:00
ldh a, [hGameState]
2023-10-18 02:45:58 +00:00
cp a, b
2023-10-18 02:09:12 +00:00
jp nz, GamePlayEventLoopHandler
2023-10-20 16:09:06 +00:00
jp TitleEventLoopHandler
2023-10-16 05:47:11 +00:00
EventLoopPostHandler::
2023-10-10 08:32:24 +00:00
2023-10-18 01:11:05 +00:00
; Wait for vblank.
2023-10-18 02:01:35 +00:00
wait_vblank
2023-10-18 01:11:05 +00:00
; Do OAM DMA.
2023-10-18 02:01:35 +00:00
; This will chain jump into the vblank handler.
jp hOAMDMA
2023-10-16 05:47:11 +00:00
2023-10-21 19:03:50 +00:00
; The VBlank Handler is expected to end with jr EventLoop.
2023-10-10 08:32:24 +00:00
2023-10-16 05:47:11 +00:00
ENDC