···11+INCLUDE "hardware.inc/hardware.inc"
22+33+SECTION "Input", ROM0
44+55+Input::
66+ ; Poll half the controller
77+ ld a, P1F_GET_BTN
88+ call .onenibble
99+ ld b, a ; B7-4 = 1; B3-0 = unpressed buttons
1010+1111+ ; Poll the other half
1212+ ld a, P1F_GET_DPAD
1313+ call .onenibble
1414+ swap a ; A3-0 = unpressed directions; A7-4 = 1
1515+ xor a, b ; A = pressed buttons + directions
1616+ ld b, a ; B = pressed buttons + directions
1717+1818+ ; And release the controller
1919+ ld a, P1F_GET_NONE
2020+ ldh [rP1], a
2121+2222+ ; Combine with previous wCurKeys to make wNewKeys
2323+ ld a, [wCurKeys]
2424+ xor a, b ; A = keys that changed state
2525+ and a, b ; A = keys that changed to presed
2626+ ld [wNewKeys], a
2727+ ld a, b
2828+ ld [wCurKeys], a
2929+ ret
3030+3131+.onenibble
3232+ ldh [rP1], a ; switch the key matrix
3333+ call .knownret ; burn 10 cycles calling a known ret
3434+ ldh a, [rP1] ; ignore value while waiting for the key matrix to settle
3535+ ldh a, [rP1]
3636+ ldh a, [rP1] ; this read counts
3737+ or a, $f0 ; A7-4 = 1; A3-0 = unpressed keys
3838+.knownret
3939+ ret
+57-14
gb/src/main/main.asm
···11INCLUDE "hardware.inc/hardware.inc"
22 rev_Check_hardware_inc 4.0
3344+SECTION "GameVariables", WRAM0
55+66+wLastKeys:: db
77+wCurKeys:: db
88+wNewKeys:: db
99+wGameState:: db
1010+411SECTION "Header", ROM0[$100]
512613 ; This is your ROM's entry point
···1926SECTION "Entry point", ROM0
20272128EntryPoint:
2222- ; Seed random number generator
2323- ld b, 0
2424- ld c, 10
2525- call srand
2929+ ; Shut down audio circuitry
3030+ xor a
3131+ ld [rNR52], a
3232+ ld [wGameState], a
3333+3434+ ; Wait for VBlank phase before initiating the library
3535+ call WaitForOneVBlank
3636+3737+ ; Turn the LCD off
3838+ xor a
3939+ ld [rLCDC], a
4040+4141+ ; Turn the LCD on
4242+ ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON
4343+ ld [rLCDC], a
4444+4545+ ; During the first (blank) frame, intialize display registers
4646+ ld a, %11100100
4747+ ld [rBGP], a
4848+ ld [rOBP0], a
4949+5050+NextGameState::
5151+ ; Do not turn the LCD off outside of VBlank
5252+ call WaitForOneVBlank
5353+ call ClearBackground
26542727- ; Initialize Dungeon
2828- ld b, 6 ; Starting Width
2929- ld c, 6 ; Starting Height
3030- ld d, 36 ; W * H
3131- ld e, 27 ; W * H * 0.75
3232- call InitDungeon
3333- call GenerateDungeon
5555+ ; Turn the LCD off
5656+ xor a
5757+ ld [rLCDC], a
34583535- jp Done
5959+ ld [rSCX], a
6060+ ld [rSCY], a
6161+ ld [rWX], a
6262+ ld [rWY], a
6363+ ; disable interrupts
6464+ ; call DisableInterrupts
6565+6666+ ; Clear all sprites
6767+ ; call ClearAllSprites
6868+6969+ ; Initiate the next state
7070+ ld a, [wGameState]
7171+ cp 1 ; 1 = Gameplay
7272+ ; call z, InitGameplayState
7373+ ld a, [wGameState]
7474+ and a ; 0 = Menu
7575+ call z, InitTitleScreenState
36763737-Done:
3838- jp Done7777+ ; Update the next state
7878+ ld a, [wGameState]
7979+ cp 1 ; 1 = Gameplay
8080+ ; jp z, UpdateGameplayState
8181+ jp UpdateTitleScreenState
+9
gb/src/main/states/gameplay/interrupts.asm
···11+INCLUDE "hardware.inc/hardware.inc"
22+33+SECTION "Interrupts", ROM0
44+55+DisableInterrupts::
66+ xor a
77+ ldh [rSTAT], a
88+ di
99+ ret
···11+INCLUDE "hardware.inc/hardware.inc"
22+33+SECTION "TitleScreenState", ROM0
44+55+titleScreenTileData: INCBIN "src/generated/backgrounds/title.2bpp"
66+titleScreenTileDataEnd:
77+88+titleScreenTileMap: INCBIN "src/generated/backgrounds/title.tilemap"
99+titleScreenTileMapEnd:
1010+1111+InitTitleScreenState::
1212+ call DrawTitleScreen
1313+1414+ ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON
1515+ ld [rLCDC], a
1616+ ret
1717+1818+DrawTitleScreen::
1919+ ld de, titleScreenTileData
2020+ ld hl, $9340
2121+ ld bc, titleScreenTileDataEnd - titleScreenTileData
2222+ call CopyDEIntoMemoryAtHL
2323+2424+ ld de, titleScreenTileMap
2525+ ld hl, $9800
2626+ ld bc, titleScreenTileMapEnd - titleScreenTileMap
2727+ jp CopyDEintoMemoryAtHL_With520Offset
2828+2929+UpdateTitleScreenState::
3030+ ld a, PADF_A
3131+ ld [mWaitKey], a
3232+3333+ call WaitForKeyFunction
3434+ ld a, 1
3535+ ld [wGameState], a
3636+ jp NextGameState
+27
gb/src/main/utils/background-utils.asm
···11+INCLUDE "hardware.inc/hardware.inc"
22+33+SECTION "Background", ROM0
44+55+ClearBackground::
66+ ; Turn the LCD off
77+ xor a
88+ ld [rLCDC], a
99+1010+ ld bc, 1024
1111+ ld hl, $9800
1212+1313+ClearBackgroundLoop:
1414+ xor a
1515+ ld [hli], a
1616+1717+ dec bc
1818+ ld a, b
1919+ or c
2020+2121+ jp nz, ClearBackgroundLoop
2222+2323+ ; Turn the LCD on
2424+ ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON
2525+ ld [rLCDC], a
2626+2727+ ret
+35
gb/src/main/utils/input-utils.asm
···11+SECTION "InputUtilsVariables", WRAM0
22+33+mWaitKey:: db
44+55+SECTION "InputUtils", ROM0
66+77+WaitForKeyFunction::
88+ push bc
99+1010+WaitForKeyFunction_Loop:
1111+ ld a, [wCurKeys]
1212+ ld [wLastKeys], a
1313+1414+ call Input
1515+1616+ ld a, [mWaitKey]
1717+ ld b, a
1818+ ld a, [wCurKeys]
1919+ and b
2020+ jp z, WaitForKeyFunction_NotPressed
2121+2222+ ld a, [wLastKeys]
2323+ and b
2424+ jp nz, WaitForKeyFunction_NotPressed
2525+2626+ pop bc
2727+ ret
2828+2929+WaitForKeyFunction_NotPressed:
3030+ ld a, 1
3131+ ld [wVBlankCount], a
3232+3333+ call WaitForVBlankFunction
3434+3535+ jp WaitForKeyFunction_Loop
+21
gb/src/main/utils/memory-utils.asm
···11+SECTION "MemoryUtilsSection", ROM0
22+33+CopyDEIntoMemoryAtHL::
44+ ld a, [de]
55+ ld [hli], a
66+ inc de
77+ dec bc
88+ ld a, b
99+ or c
1010+ jp nz, CopyDEIntoMemoryAtHL
1111+ ret
1212+1313+CopyDEintoMemoryAtHL_With520Offset::
1414+ ld a, [de]
1515+ add a, 52
1616+ ld [hli], a
1717+ inc de
1818+ dec bc
1919+ or c
2020+ jp nz, CopyDEintoMemoryAtHL_With520Offset
2121+ ret
+28
gb/src/main/utils/vblank-utils.asm
···11+INCLUDE "hardware.inc/hardware.inc"
22+33+SECTION "VBlankVariables", WRAM0
44+55+wVBlankCount:: db
66+77+SECTION "VBlankFunctions", ROM0
88+99+WaitForOneVBlank::
1010+ ld a, 1
1111+ ld [wVBlankCount], a
1212+1313+WaitForVBlankFunction::
1414+1515+WaitForVBlankFunction_Loop::
1616+ ld a, [rLY]
1717+ cp 144
1818+ jp c, WaitForVBlankFunction_Loop
1919+ ld a, [wVBlankCount]
2020+ sub 1
2121+ ld [wVBlankCount], a
2222+ ret z
2323+2424+WaitForKeyFunction_Loop2::
2525+ ld a, [rLY]
2626+ cp 144
2727+ jp nc, WaitForKeyFunction_Loop2
2828+ jp WaitForVBlankFunction_Loop