dmgtris/src/main.asm

234 lines
4.8 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-23 06:06:19 +00:00
SECTION "Persistent Globals", SRAM
2023-10-23 10:16:16 +00:00
rMagic:: ds 4
rSwapABState:: ds 1
rRNGModeState:: ds 1
rRotModeState:: ds 1
rDropModeState:: ds 1
rSpeedCurveState:: ds 1
rAlways20GState:: ds 1
2023-10-24 20:21:48 +00:00
rSelectedStartLevel:: ds 2
2023-10-23 06:06:19 +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-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-23 06:06:19 +00:00
; Enable RAM. (Not actually needed since we don't ACTUALLY use an MBC, but without this emulators shit the bed.)
ld hl, rRAMG
ld a, CART_SRAM_ENABLE
ld [hl], a
; Check for save data.
ld a, [rMagic]
2023-10-23 10:16:16 +00:00
cp a, "D"
2023-10-23 06:06:19 +00:00
jr nz, .nosavedata
ld a, [rMagic+1]
2023-10-23 10:16:16 +00:00
cp a, "M"
jr nz, .nosavedata
ld a, [rMagic+2]
2023-10-23 06:06:19 +00:00
cp a, "G"
jr nz, .nosavedata
2023-10-23 10:16:16 +00:00
ld a, [rMagic+3]
2023-10-24 20:21:48 +00:00
cp a, "1"
2023-10-23 06:06:19 +00:00
jr nz, .nosavedata
.savedata
2023-10-23 10:16:16 +00:00
ld a, [rSwapABState]
ld [wSwapABState], a
ld a, [rRNGModeState]
ld [wRNGModeState], a
ld a, [rRotModeState]
ld [wRotModeState], a
ld a, [rDropModeState]
ld [wDropModeState], a
ld a, [rSpeedCurveState]
ld [wSpeedCurveState], a
ld a, [rAlways20GState]
ld [wAlways20GState], a
2023-10-24 20:21:48 +00:00
ld a, [rSelectedStartLevel]
ldh [hStartSpeed], a
ld a, [rSelectedStartLevel+1]
ldh [hStartSpeed+1], a
2023-10-23 06:06:19 +00:00
jr .otherinit
.nosavedata
2023-10-23 10:16:16 +00:00
ld a, "D"
2023-10-23 06:06:19 +00:00
ld [rMagic], a
ld a, "M"
2023-10-23 10:16:16 +00:00
ld [rMagic+1], a
ld a, "G"
2023-10-23 06:06:19 +00:00
ld [rMagic+2], a
2023-10-24 20:21:48 +00:00
ld a, "1"
2023-10-23 10:16:16 +00:00
ld [rMagic+3], a
ld a, BUTTON_MODE_NORM
ld [rSwapABState], a
ld [wSwapABState], a
ld a, RNG_MODE_TGM3
ld [rRNGModeState], a
ld [wRNGModeState], a
ld a, RNG_MODE_TGM3
ld [rRNGModeState], a
ld [wRNGModeState], a
ld a, ROT_MODE_ARSTI
ld [rRotModeState], a
ld [wRotModeState], a
ld a, DROP_MODE_SONIC
ld [rDropModeState], a
ld [wDropModeState], a
ld a, SCURVE_DMGT
ld [rSpeedCurveState], a
ld [wSpeedCurveState], a
ld a, HIG_MODE_OFF
ld [rAlways20GState], a
ld [wAlways20GState], a
2023-10-23 06:06:19 +00:00
2023-10-21 14:31:26 +00:00
ld hl, sSpeedCurve
ld a, l
ldh [hStartSpeed], a
2023-10-24 20:21:48 +00:00
ld [rSelectedStartLevel], a
2023-10-21 14:31:26 +00:00
ld a, h
ldh [hStartSpeed+1], a
2023-10-24 20:21:48 +00:00
ld [rSelectedStartLevel+1], a
.otherinit
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