I'm making a "Flapping Bird" game for the "Original Nintendo Game Boy" (DMG) in Assembly using RGBDS tools (RGBASM, RGBLINK and RGBFIX are all version 0.6.1) and BGB (version 1.6.6).
When I run the game normally, it seems broken and it doesn't even work the same if I exit the emulator and run it again!
If I run the game, enter debug mode and run the game instruction by instruction, everything is working as intended! Why is this happening?
My code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DEF OBJECT_TILES_SIZE EQU (96)
DEF BACKGROUND_AND_WINDOW_TILES_SIZE EQU (448)
;--------------------------------------------------------------------
DEF NUMBER_OF_LEVELS EQU (12)
;--------------------------------------------------------------------
DEF VIDEO_RAM EQU ($8000)
;--------------------------------------------------------------------
DEF WORKING_RAM EQU ($C000)
;--------------------------------------------------------------------
DEF OBJECT_ATTRIBUTE_MEMORY EQU ($FE00)
;--------------------------------------------------------------------
DEF JOYPAD_CONTROL_REGISTER EQU ($FF00)
;--------------------------------------------------------------------
DEF LCD_CONTROL_REGISTER EQU ($FF40)
DEF LCD_CONTROL_VERTICAL_SCANLINE_POSITION EQU ($FF44)
;--------------------------------------------------------------------
DEF OAM_DMA_SOURCE_ADDRESS_REGISTER EQU ($FF46)
;--------------------------------------------------------------------
DEF BACKGROUND_PALETTE EQU ($FF47)
DEF OBJECT_PALETTE_NUMBER_0 EQU ($FF48)
DEF OBJECT_PALETTE_NUMBER_1 EQU ($FF49)
;--------------------------------------------------------------------
DEF HIGH_RAM EQU ($FF80)
;--------------------------------------------------------------------
DEF SCORE_POSITION EQU ($9E07)
DEF SCORE_FRAME_COUNTER EQU (HIGH_RAM)
DEF SCORE_ONES_DIGIT EQU (HIGH_RAM + 1)
DEF SCORE_TENS_DIGIT EQU (HIGH_RAM + 2)
DEF SCORE_HUNDREDS_DIGIT EQU (HIGH_RAM + 3)
DEF SCORE_THOUSANDS_DIGIT EQU (HIGH_RAM + 4)
;--------------------------------------------------------------------
DEF BIRD_Y_COORDINATE EQU (HIGH_RAM + 5)
;--------------------------------------------------------------------
DEF OBSTACLE_NUMBER_IN_CURRENT_LEVEL EQU (HIGH_RAM + 6)
;--------------------------------------------------------------------
DEF OBJECT_FRAME_COUNTER EQU (HIGH_RAM + 7)
;--------------------------------------------------------------------
DEF CURRENT_LEVEL_NUMBER EQU (HIGH_RAM + 8)
;--------------------------------------------------------------------
DEF NEXT_ADDRESS_TO_LOAD_FROM_RAM_TO_OAM EQU (HIGH_RAM + 9)
DEF OAM_DMA_TRANSFER_IN_HIGH_RAM EQU (HIGH_RAM + 10)
;--------------------------------------------------------------------
DEF INTERRUPT_ENABLE_REGISTER EQU ($FFFF)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Macros.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MACRO MACRO_INITILIZE_INTERRUPT_ENABLE_REGISTER
; Enable interrupts and only allow VBLANK interrupts in the
; future.
Initialize_Interrupt_Enable_Register:
LD A, %10000001
LDH [INTERRUPT_ENABLE_REGISTER], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_INITILIZE_JOYPAD_CONTROL_REGISTER
; Set the "Joypad (Control) Register" to read button inputs
; ("A", "B", "Start" and "Select").
Initialize_Joypad_Control_Register:
LD A, %00100000
LDH [JOYPAD_CONTROL_REGISTER], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_WAIT_FOR_VBLANK
Wait_For_VBLANK:
LD A, [LCD_CONTROL_VERTICAL_SCANLINE_POSITION]
CP A, 144
JR C, Wait_For_VBLANK
ENDM
;--------------------------------------------------------------------
MACRO MACRO_INITILIZE_LCD
; Trun off the LCD, set "Window" tile map area to $9800-9BFF,
; hide "Window", set both "Background" and "Window" tile set area
; to $8800-$97FF, set "Background" tile map area to $9C00-$9FFF,
; set "Object" size to 8x16, enable "Object" and enable both the
; "Background" and the "Window".
Initialize_LCD:
LD A, %00001111
LDH [LCD_CONTROL_REGISTER], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_INITILIZE_BACKGROUND_AND_OBJECT_COLOR_PALETTES
; Set the "Background" palette and the 0th "Object" palette to
; "Dark Green/Green/Light Green/White" and the 1st "Object"
; palette to "White/Light Green/Green/Dark Green".
Initialize_Background_And_Object_Color_Palettes:
LD A, %11100100
LD [BACKGROUND_PALETTE], A
LD [OBJECT_PALETTE_NUMBER_0], A
CPL
LD [OBJECT_PALETTE_NUMBER_1], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_INITILIZE_VARIABLES
Initialize_Variables:
XOR A, A
LDH [SCORE_FRAME_COUNTER], A
LDH [SCORE_ONES_DIGIT], A
LDH [SCORE_TENS_DIGIT], A
LDH [SCORE_HUNDREDS_DIGIT], A
LDH [SCORE_THOUSANDS_DIGIT], A
LDH [OBJECT_FRAME_COUNTER], A
LDH [CURRENT_LEVEL_NUMBER], A
LD A, 18
LDH [BIRD_Y_COORDINATE], A
LD A, $C0
LDH [NEXT_ADDRESS_TO_LOAD_FROM_RAM_TO_OAM], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_INITILIZE_VIDEO_RAM
; Load "Object" tiles to addresses $8000-$87FF.
Initialize_Video_RAM:
LD DE, Object_Tiles
LD HL, VIDEO_RAM
LD BC, OBJECT_TILES_SIZE
.Load_Object_Tiles_From_ROM_Into_VRAM:
LD A, [DE]
INC DE
LD [HLI], A
DEC BC
LD A, B
OR A, C
JR NZ, .Load_Object_Tiles_From_ROM_Into_VRAM
; Fill the unused tiles with Chessboard-Pattern unusable tiles.
; Since "Object" and "Background and Window" tiles have nothing
; in common, fill the addresses $8800-$8FFF with
; Chessboard-Pattern unusable tiles too.
LD BC, $1000 - OBJECT_TILES_SIZE
.Fill_Unused_Object_And_Shared_Tiles:
LD A, $AA
LD [HLI], A
DEC BC
LD [HLI], A
DEC BC
CPL
LD [HLI], A
DEC BC
LD [HLI], A
DEC BC
LD A, B
OR A, C
JR NZ, .Fill_Unused_Object_And_Shared_Tiles
; Fill addresses $9000-$97FF with "Background and Window" tiles.
LD DE, Background_And_Window_Tiles
LD BC, BACKGROUND_AND_WINDOW_TILES_SIZE
.Load_Background_And_Window_Tiles_From_ROM_Into_VRAM:
LD A, [DE]
INC DE
LD [HLI], A
DEC BC
LD A, B
OR A, C
JR NZ, .Load_Background_And_Window_Tiles_From_ROM_Into_VRAM
; Fill the unused tiles with Chessboard-Pattern unusable tiles.
LD BC, $800 - BACKGROUND_AND_WINDOW_TILES_SIZE
.Fill_Unused_Background_And_Window_Tiles:
LD A, $AA
LD [HLI], A
DEC BC
LD [HLI], A
DEC BC
CPL
LD [HLI], A
DEC BC
LD [HLI], A
DEC BC
LD A, B
OR A, C
JR NZ, .Fill_Unused_Background_And_Window_Tiles
; Since the "Window" isn't used, fill its "Tile Map"
; (addresses $9800-$9BFF) Chessboard-Pattern unusable tile IDs.
LD BC, $400
.Fill_Unused_Window_Tile_Map:
LD A, $FF
LD [HLI], A
DEC BC
LD A, B
OR A, C
JR NZ, .Fill_Unused_Window_Tile_Map
; Populate the "Background Tile Map" (addresses $9C00-$9FFF).
LD DE, Background_Tilemap
LD BC, $400
.Load_Background_Tile_Map_From_ROM_Into_VRAM:
LD A, [DE]
INC DE
LD [HLI], A
DEC BC
LD A, B
OR A, C
JR NZ, .Load_Background_Tile_Map_From_ROM_Into_VRAM
ENDM
;--------------------------------------------------------------------
MACRO MACRO_INITILIZE_OBJECT_ATTRIBUTE_MEMORY_WITH_LEVEL_ZERO
; Each object entry in the "OAM" is 4 bytes and the "OAM" is only
; 160 bytes, so a maximum of 40 objects can reside in the "OAM".
; The first byte in each object entry is the object's
; "Y-Coordinate". The second byte is the "X-Coordinate". The
; third byte is the object's Tile-ID. the fourth and final byte
; is the object's "Attributes". The first and second entry in the
; "OAM" is always the first and second half of the bird.
Initialize_Object_Attribute_Memory_With_Level_Zero:
LD HL, OBJECT_ATTRIBUTE_MEMORY
LD DE, Level_Zero
LD C, 160
.Load_level_Zero_From_ROM_Into_OAM:
LD A, [DE]
INC DE
LD [HLI], A
DEC C
JR NZ, .Load_level_Zero_From_ROM_Into_OAM
ENDM
;--------------------------------------------------------------------
MACRO MACRO_TURN_OFF_LCD
;Turn_Off_LCD:
LDH A, [LCD_CONTROL_REGISTER]
RES 7, A
LDH [LCD_CONTROL_REGISTER], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_TURN_ON_LCD
Turn_On_LCD:
LDH A, [LCD_CONTROL_REGISTER]
SET 7, A
LDH [LCD_CONTROL_REGISTER], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_UPDATE_VARIABLES_PER_FRAME
Update_Variables_Per_Frame:
; Increment the "SCORE_FRAME_COUNTER".
LDH A, [SCORE_FRAME_COUNTER]
INC A
LDH [SCORE_FRAME_COUNTER], A
; Increment the "OBJECT_FRAME_COUNTER".
LDH A, [OBJECT_FRAME_COUNTER]
INC A
LDH [OBJECT_FRAME_COUNTER], A
ENDM
;--------------------------------------------------------------------
MACRO MACRO_UPDATE_AND_PRINT_SCORE
Update_And_Print_Score:
; Don't update the score if 1 second has not yet passed.
LDH A, [SCORE_FRAME_COUNTER]
CP A, 59
JR C, .Finished_Updating_And_Printing_The_Score
; 1 second has passed, so reset the "SCORE_FRAME_COUNTER" and
; increment the "SCORE_ONES_DIGIT". If "SCORE_ONES_DIGIT" isn't
; greater than 9, then skip to ".Start_Printing_The_Score".
XOR A, A
LDH [SCORE_FRAME_COUNTER], A
LDH A, [SCORE_ONES_DIGIT]
INC A
CP A, 10
JR NC, .Ones_Digit_Greater_Than_9
LDH [SCORE_ONES_DIGIT], A
JR .Start_Printing_The_Score
; "SCORE_ONES_DIGIT" is greater than 9, so we reset it and
; increment "SCORE_TENS_DIGIT". If "SCORE_TENS_DIGIT" isn't
; greater than 9, then skip to ".Start_Printing_The_Score".
.Ones_Digit_Greater_Than_9:
XOR A, A
LDH [SCORE_ONES_DIGIT], A
LDH A, [SCORE_TENS_DIGIT]
INC A
CP A, 10
JR NC, .Tens_Digit_Greater_Than_9
LDH [SCORE_TENS_DIGIT], A
JR .Start_Printing_The_Score
; "SCORE_TENS_DIGIT" is greater than 9, so we reset it and
; increment "SCORE_HUNDREDS_DIGIT". If "SCORE_HUNDREDS_DIGIT"
; isn't greater than 9, then skip to ".Start_Printing_The_Score".
.Tens_Digit_Greater_Than_9:
XOR A, A
LDH [SCORE_TENS_DIGIT], A
LDH A, [SCORE_HUNDREDS_DIGIT]
INC A
CP A, 10
JR NC, .Hundreds_Digit_Greater_Than_9
LDH [SCORE_HUNDREDS_DIGIT], A
JR .Start_Printing_The_Score
; "SCORE_HUNDREDS_DIGIT" is greater than 9, so we reset it and
; increment "SCORE_THOUSANDS_DIGIT". If "SCORE_THOUSANDS_DIGIT"
; isn't greater than 9, then skip to ".Start_Printing_The_Score".
.Hundreds_Digit_Greater_Than_9:
XOR A, A
LDH [SCORE_HUNDREDS_DIGIT], A
LDH A, [SCORE_THOUSANDS_DIGIT]
INC A
CP A, 10
JR NC, .Thousands_Digit_Greater_Than_9
LDH [SCORE_THOUSANDS_DIGIT], A
JR .Start_Printing_The_Score
; "SCORE_THOUSANDS_DIGIT" is greater than 9, so we reset it.
.Thousands_Digit_Greater_Than_9:
XOR A, A
LDH [SCORE_THOUSANDS_DIGIT], A
.Start_Printing_The_Score:
LD HL, SCORE_POSITION
; Print all 4 digits if the score's 1000s digit isn't 0.
LDH A, [SCORE_THOUSANDS_DIGIT]
AND A, A
JR Z, .Print_3_Digits_Score
.Print_4_Digits_Score:
LD [HLI], A
LDH A, [SCORE_HUNDREDS_DIGIT]
LD [HLI], A
LDH A, [SCORE_TENS_DIGIT]
LD [HLI], A
LDH A, [SCORE_ONES_DIGIT]
LD [HL], A
JR .Finished_Updating_And_Printing_The_Score
; Print 3 digits if the score's 100s digit isn't 0.
.Print_3_Digits_Score:
LDH A, [SCORE_HUNDREDS_DIGIT]
AND A, A
JR Z, .Print_2_Digits_Score
LD [HLI], A
LDH A, [SCORE_TENS_DIGIT]
LD [HLI], A
LDH A, [SCORE_ONES_DIGIT]
LD [HL], A
JR .Finished_Updating_And_Printing_The_Score
; Print 2 digits if the score's 10s digit isn't 0.
.Print_2_Digits_Score:
LDH A, [SCORE_TENS_DIGIT]
AND A, A
JR Z, .Print_1_Digit_Score
LD [HLI], A
LDH A, [SCORE_ONES_DIGIT]
LD [HL], A
JR .Finished_Updating_And_Printing_The_Score
; Print the score's 1s digit.
.Print_1_Digit_Score:
LDH A, [SCORE_ONES_DIGIT]
LD [HL], A
.Finished_Updating_And_Printing_The_Score:
ENDM
;--------------------------------------------------------------------
MACRO MACRO_MOVE_OBJECTS
; The bird and obstacles can move every 2 frames. If 2 frames
; have not yet passed, then skip moving any objects. If 2 frames
; have passed, then reset "OBJECT_FRAME_COUNTER" to 0.
Move_Objects:
LDH A, [OBJECT_FRAME_COUNTER]
CP A, 2
JR C, .Finished_Moving_All_Objects
XOR A, A
LDH [OBJECT_FRAME_COUNTER], A
; The obstacles are the first to be moved. the total number of
; obstacles is stored in "OBSTACLE_NUMBER_IN_CURRENT_LEVEL".
LDH A, [OBSTACLE_NUMBER_IN_CURRENT_LEVEL]
LD C, A
; The first obstacle's "X-Coordinate" is 9 bytes away from the
; start of the "OAM".
LD HL, OBJECT_ATTRIBUTE_MEMORY + 9
; There are 4 bytes between one object's "X-Coordinate" with the
; next one's, so 4 will be added to the pointer stored in HL for
; each iteration of the loop below that moves all obstacles to
; the left by 1 pixel.
LD DE, 4
.Move_All_Obstacles_1_Pixel_To_The_Left:
LD A, [HL]
DEC A
LD [HL], A
ADD HL, DE
DEC C
JR NZ, .Move_All_Obstacles_1_Pixel_To_The_Left
; The bird is the last object to be moved. It moves 4 pixels up
; if the user has pressed the "A" button. Due to button bounces,
; read from the register a few times to make sure the user input
; has been captured.
LD A, [JOYPAD_CONTROL_REGISTER]
LD A, [JOYPAD_CONTROL_REGISTER]
; If the user has pressed the "A" button, bit #0 of the
; "Joypad (Control) Register" becomes 0.
AND A, %00000001
JR NZ, .User_Did_Not_Press_The_A_Button
.User_Pressed_The_A_Button:
LD A, [OBJECT_ATTRIBUTE_MEMORY]
SUB A, 2
LD [OBJECT_ATTRIBUTE_MEMORY], A
LD [OBJECT_ATTRIBUTE_MEMORY + 4], A
JR .Finished_Moving_All_Objects
;
.User_Did_Not_Press_The_A_Button:
LD A, [OBJECT_ATTRIBUTE_MEMORY]
ADD A, 2
LD [OBJECT_ATTRIBUTE_MEMORY], A
LD [OBJECT_ATTRIBUTE_MEMORY + 4], A
.Finished_Moving_All_Objects:
ENDM
;--------------------------------------------------------------------
MACRO MACRO_LOAD_LEVELS_FROM_ROM_TO_WORKING_RAM
; Load (a maximum of 51) levels from the ROM to the "Working RAM"
; or "RAM". The number of available levels is stored in
; "NUMBER_OF_LEVELS" and each level has 160 bytes of data.
Load_Levels_From_ROM_To_Working_RAM:
LD HL, WORKING_RAM
LD DE, Levels
LD B, NUMBER_OF_LEVELS
.Load_New_Level:
LD C, 160
.Load_Level_Data:
LD A, [DE]
INC DE
LD [HLI], A
DEC C
JR NZ, .Load_Level_Data
INC H
XOR A, A
LD L, A
DEC B
JR NZ, .Load_New_Level
ENDM
;--------------------------------------------------------------------
MACRO MACRO_LOAD_OAM_DMA_TRANSFER_FUNCTION_FROM_ROM_INTO_HIGH_RAM
Load_OAM_DMA_Transfer_Function_From_ROM_Into_High_RAM:
LD HL, OAM_DMA_TRANSFER_IN_HIGH_RAM
LD DE, OAM_DMA_Transfer_In_ROM
LD C, End_Of_OAM_DMA_Transfer_In_ROM - OAM_DMA_Transfer_In_ROM
.Load_Next_Byte_Of_OAM_DMA_Transfer_Function:
LD A, [DE]
INC DE
LD [HLI], A
DEC C
JR NZ, .Load_Next_Byte_Of_OAM_DMA_Transfer_Function
ENDM
;--------------------------------------------------------------------
MACRO MACRO_LOAD_NEW_LEVEL_FROM_RAM_INTO_OAM_IF_NEEDED
Load_New_Level_From_RAM_Into_OAM_If_Needed:
LD HL, OBJECT_ATTRIBUTE_MEMORY
LDH A, [OBSTACLE_NUMBER_IN_CURRENT_LEVEL]
INC A
SLA A
SLA A
LD C, A
XOR A, A
LD B, A
ADD HL, BC
; The address of the last obstacle has been calculated. Check if
; the 2nd byte which is the obstacle's "X-Coordinate" is 0. If
; it was not zero, go to the end (of this macro).
INC HL
LD A, [HL]
AND A, A
JR NZ, .Done_Checking_If_A_New_Level_Should_be_Loaded
; Save the "Y-Coordinate" of the bird.
LD A, [OBJECT_ATTRIBUTE_MEMORY]
LDH [BIRD_Y_COORDINATE], A
LD HL, .OAM_DMA_Transfer_Is_Done
PUSH HL
.Start_OAM_DMA_Transfer:
LD HL, OAM_DMA_TRANSFER_IN_HIGH_RAM
PUSH HL
RET
.OAM_DMA_Transfer_Is_Done:
LDH A, [NEXT_ADDRESS_TO_LOAD_FROM_RAM_TO_OAM]
INC A
LDH [NEXT_ADDRESS_TO_LOAD_FROM_RAM_TO_OAM], A
LD A, [OBJECT_ATTRIBUTE_MEMORY]
LDH [OBSTACLE_NUMBER_IN_CURRENT_LEVEL], A
LDH A, [BIRD_Y_COORDINATE]
LD [OBJECT_ATTRIBUTE_MEMORY], A
LD [OBJECT_ATTRIBUTE_MEMORY + 4], A
LDH A, [CURRENT_LEVEL_NUMBER]
INC A
LDH [CURRENT_LEVEL_NUMBER], A
.Done_Checking_If_A_New_Level_Should_be_Loaded:
ENDM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Error Handlers.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION "Error Handlers", ROM0[$0000]
; Addresses $0000 to $0007 (8 bytes) are executed when invoking
; "RST 0".
Trap:
DI
; Trap the CPU in an infinite loop.
JR @
; We will fill the remaining 5 bytes with "RST 0", just in case.
RST 0
RST 0
RST 0
RST 0
RST 0
;--------------------------------------------------------------------
; We don't have any other error handlers at the moment, so we
; will fill addresses $0008 to $003F (56 bytes) with "RST 0".
REPT 56
RST 0
ENDR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interrupt Handlers.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION "Interrupt Handlers", ROM0[$0040]
; When a VBLANK interrupt occures (if interrupts are enabled),
; the CPU starts executing from address $0040.
VBLANK_Interrupt_Handler:
; The VBLANK interrupt serves only to show us that a VBLANK has
; occured. We will simply acknowledge it, return from its
; interrupt handler and later on enable it.
RETI
; We will fill the remaining 7 bytes with "RST 0", just in case.
RST 0
RST 0
RST 0
RST 0
RST 0
RST 0
RST 0
;--------------------------------------------------------------------
; We don't have any other Interrupt handlers at the moment, so we
; will fill addresses $0048 to $0067 (32 bytes) with "RST 0".
REPT 32
RST 0
ENDR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Free Memory Region.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION "Free Memory Region", ROM0[$0068]
; Addresses $0068-$00FF (152 bytes) is a free memory region. The
; "OAM DMA Transfer" procedure (9 bytes) will be stored in this
; region.
OAM_DMA_Transfer_In_ROM:
LDH A, [NEXT_ADDRESS_TO_LOAD_FROM_RAM_TO_OAM]
; Start DMA transfer.
LDH [OAM_DMA_SOURCE_ADDRESS_REGISTER], A
LD A, 40
.Wait_Untill_OAM_DMA_Transfer_Is_Done:
DEC A
JR NZ, .Wait_Untill_OAM_DMA_Transfer_Is_Done
RET
End_Of_OAM_DMA_Transfer_In_ROM:
;--------------------------------------------------------------------
; The remaining 143 bytes aren't used, so it will be filled with
; the "RST 0" instruction to make sure that the CPU is traped if
; it tries to execute and instruction from this region.
REPT 142
RST 0
ENDR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The Cartridge Header.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION "Cartridge Header", ROM0[$0100]
; After the "Boot ROM" has finished executing and unmaps itself
; from addresses $0000 to $00FF (256 bytes), the CPU starts
; executing instructions from address $0100 to $0103 (4 bytes).
; Disable interrupt handling.
DI
; Jump to where the code begins (the "Entry Point" of the code).
JP Entry_Point
; The rest of the header will be filled by "RGBFIX". We will fill
; addresses $0104 to $014F (76 bytes) with 0.
DS 76, $00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Entry Point.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION "Entry Point", ROM0[$0150]
Entry_Point:
; Wait for a VBLANK to arrive to safely turn off the LCD and
; initialze everything.
MACRO_WAIT_FOR_VBLANK
; Initilize the LCD.
MACRO_INITILIZE_LCD
; Initialize the "Stack Pointer".
LD SP, $DFFF
; Initilize the "Interrupt Enable Register" or "IE".
MACRO_INITILIZE_INTERRUPT_ENABLE_REGISTER
; Initilize the "Joypad (Control) Register".
MACRO_INITILIZE_JOYPAD_CONTROL_REGISTER
; Copy "OAM DMA Transfer" function from ROM to "High RAM".
MACRO_LOAD_OAM_DMA_TRANSFER_FUNCTION_FROM_ROM_INTO_HIGH_RAM
; Initilize the "Background & Window" and the "Object" palettes.
MACRO_INITILIZE_BACKGROUND_AND_OBJECT_COLOR_PALETTES
; Initilize the game variables.
MACRO_INITILIZE_VARIABLES
; Initilize the "Video RAM" or "VRAM".
MACRO_INITILIZE_VIDEO_RAM
; Initilize the "Object Attribute Memory" or "OAM" with level #0.
MACRO_INITILIZE_OBJECT_ATTRIBUTE_MEMORY_WITH_LEVEL_ZERO
; Load game levels from ROM to "Working RAM" or just "RAM".
MACRO_LOAD_LEVELS_FROM_ROM_TO_WORKING_RAM
; Turn on the LCD.
MACRO_TURN_ON_LCD
EI
Game_Loop:
HALT
MACRO_LOAD_NEW_LEVEL_FROM_RAM_INTO_OAM_IF_NEEDED
MACRO_UPDATE_VARIABLES_PER_FRAME
MACRO_UPDATE_AND_PRINT_SCORE
MACRO_MOVE_OBJECTS
JP Game_Loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Assets.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;