Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Game Loop [Code documentation]

Lim Ngian Xin Terry edited this page May 28, 2021 · 6 revisions

//TODO:: talk about interrupts, initialisation, main and rst here?

initialisation.asm

initialisation.asm will initialise everything needed before the main game loop, such as setting the address of the stack pointer register, copying the DMA function from RAM to HRAM, copying tile and sprite data to VRAM, setting up interrupts and the necessary flags for the game.

Sections in initialisation.asm:


  • Contains the codes and logic needed to initialise the game.
  • Located in ROM0.
  • Functions:

Initialise Function:

  • Called at the start of the program by the instruction at $0100. ($0100 is the entry point for the codes on the Gameboy.)
  • Anything that needs to be initialised pre-game loop should be done here
; ---Initialise Logic---
Initialise:
    ld sp, $E000 ; initialise stack pointer to the end of WRAM 
         ; copy DMA handling routine from RAM to HRAM
         ; wait for VBLANK to shut off LCD and update VRAM
         ; copy tiles and map data needed to VRAM
         ; Initialise data for entities like player eg.
         ; set more flags in the registers for interrupts and visuals
         ; turn back the screen
      ei ; enable interupts

    jp MainGameLoop ; go to main game loop

update_loop.asm

update_loop.asm contains the logic for the main game loop and the logic to handle Vblank interrupt. Both the logic for the main game loop and Vblank interrupt is placed in ROM0.

Sections in game_loop.asm:


  • Contains the logic for the main game loop.
  • Located in ROM0.
  • Functions:

MainGameLoop Function:

  • It is called after intialisation.
  • Currently handles the main game logic and calls the correct functions in this loop, such as calling functions to Update Player or inputs.
; ---UpdateLoop Logic---
UpdateLoop:
         ; Handle inputs 
         ; Game logic
         ; Clean shadowOAM data
         ; Update latest sprite data to shadowOAM
    halt ; save power, wait for vBlank interrupt

    jr UpdateLoop ; repeat loop

  • Contains the logic for handling things during VBlank.
  • VBlank handler should do logic that can only be done during VBlank, such as accessing OAM and VRAM.
  • Anything related to VRAM such as rendering, graphics should be here.
  • Located in ROM0.
  • Functions:

VBlankHandler Function:

  • It is called whenever there's a Vblank interrupt.
  • Handles and update OAM, along with anything related to VRAM's registers such as graphics, rendering, screen eg.
  • Huge warning: Updating the registers for screen scrolling has to be here, or else it might cause screen tearing.

; ---VBlankHandler Logic---
VBlankHandler:
         ; Clean data in OAM
         ; Update latest sprite data from shadowOAM in RAM to OAM
         ; Update screen scrolling.

    reti ; return to the next instruction before the interrupt is called