Skip to content

Instantly share code, notes, and snippets.

@oktomus
Created December 14, 2018 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oktomus/22f014b5e6740a9d71dadc184987bfeb to your computer and use it in GitHub Desktop.
Save oktomus/22f014b5e6740a9d71dadc184987bfeb to your computer and use it in GitHub Desktop.
GameBoy Hello World
; See https://eldred.fr/gb-asm-tutorial/hello-world.html
; https://github.com/ISSOtm/gb-asm-tutorial
INCLUDE "hardware.inc"
SECTION "Header", ROM0[$100]
EntryPoint: ; Where the code begins.
di ; Disable interrupts.
jp Start
; Fill the header.
REPT $150 - $104
db 0
ENDR
; END SECTION Header.
SECTION "Game code", ROM0
Start: ; Main program.
; Turn off LCD.
.waitVBlank
ld a, [rLY] ; Really ?
cp 144 ; The screen is 144 px tall. After the LCD is past VBlank.
jr c, .waitVBlank
xor a ; The same as ld a, 0 ; We actually need to only reset the 7th bit.
ld [rLCDC], a
; Copy the font to VRAM.
ld hl, $9000
ld de, FontTiles
ld bc, FontTilesEnd - FontTiles
.copyFont ; For loop FontTiles <= de <= FontilesEnd.
ld a, [de] ; Grab the 1st byte.
ld [hli], a ; Place it. Actual copy.
inc de ; Increment (will copy the next tile in the next iteration).
dec bc ; Decrement count.
ld a, b ; Check if count is 0 because dec does not update flags.
or c ; If bc == 0 then Z will be set.
jr nz, .copyFont
; Copy the string we want to display.
ld hl, $9800 ; Print it at the top left.
ld de, HelloWorldStr
.copyString
ld a, [de]
ld [hli], a
inc de
and a ; Check if the char is \0.
jr nz, .copyString
; To turn the screen on, we need to set bit 7 of LCDC
; Initialize display registers.
ld a, %11100100 ; Palette loading.
ld [rBGP], a
xor a ; ld a, 0
ld [rSCY], A
ld [rSCX], A
; Disable sound.
ld [rNR52], a
; Turn the screen on.
ld a, %10000001
ld [rLCDC], a
; Trap it in an infinite loop.
.lockup
jr .lockup
; END SECTION Game code.
SECTION "Font", ROM0
FontTiles:
INCBIN "font.chr"
FontTilesEnd:
SECTION "Hello World string", ROM0
HelloWorldStr:
db "Hello Prout!", 0
; END SECTION Font
GB_NAME := hello_world
all: $(GB_NAME).gb
$(GB_NAME).gb: $(GB_NAME).o
rgblink -o $@ $<
rgbfix -v -p 0 $@
$(GB_NAME).o: $(GB_NAME).ds
rgbasm -o $@ $<
clean:
rm -f *.o *.gb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment