Advertisement
Guest User

Displaying a simple String on a Gameboy!

a guest
Oct 10th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Define hardware macros/shortcuts
  2. INCLUDE "hardware.inc"
  3.  
  4. ; Setup Header of memory type ROM0, start at 100h
  5. ; 100h is where execution of code begins.
  6. SECTION "Header", ROM0[$100]
  7.  
  8. EntryPoint:
  9.     di   ; Disable Interupts
  10.     jp Begin ; Begin our program!
  11.  
  12. REPT $150 - $104
  13.     db 0
  14. ENDR
  15.  
  16. ; The section of code that'll contain our actual game.
  17. ; Start location isn't specified, doesn't need to be.
  18. SECTION "Game", ROM0
  19.  
  20. Begin:
  21.     ; Turn off the LCD
  22. .waitVBlank
  23.     ld a, [rLY] ; Load LCD draw status
  24.     cp 144 ; Check if LCD is past VBlank time.
  25.     jr c, .waitVBlank
  26.  
  27.     ; Past VBlank, reset LCD mode
  28.     xor a       ; == ld a, 0
  29.     ld [rLCDC], a
  30.  
  31.     ; Now that the LCD is off so is the PPU
  32.     ; We can now access VRAM freely.
  33.     ld hl, _VRAM+$1000 ; Tile RAM
  34.     ; Get size of Font in bytes
  35.     ld de, tiFnt
  36.     ld bc, tiFntEd - tiFnt
  37. .copyFont
  38.     ld a, [de]  ; Grab byte from Font
  39.     ld [hli], a ; *HL = a; HL++;
  40.     inc de      ; Move to next byte in font
  41.         dec bc      ; Decrease bytes to load.
  42.         ld a, b     ; 16bit inc/dec don't update flags. Update flags using load.
  43.         or c        ; a or c, if b and c are both zero, our copy is done.
  44.         jr nz, .copyFont
  45.  
  46.         ; Copy string to print
  47.         ld hl, _SCRN0   ; Start print at X:0,Y:0 tiles
  48.         ld de, HelloStr ; Load 1st byte of string
  49. .copyString
  50.     ld a, [de]  ; a = (char) *de
  51.     ld [hli], a ; *hl = a, hl++
  52.     inc de      ; de++
  53.     or a        ; == cp 0
  54.     jr nz, .copyString ; If a != 0 then; get next char
  55.  
  56.     ; Init display regs
  57.     ld a, %11100100 ; Load palette, 11 10 01 00. Black,Gray,LightGray,White
  58.     ld [rBGP], a    ; Background Palette Data
  59.     ; Reset BG scroll X/Y
  60.     xor a           ; == ld a, 0
  61.     ld [rSCY], a
  62.     ld [rSCX], a
  63.  
  64.     ; Shut off APU (Sound)
  65.     ; bit7 is all that matters, although other bits don't do anything
  66.     ; and 7 ends up controlling all the other channels so, meh set it to 0.
  67.     ld [rNR52], a
  68.  
  69.     ; Turn screen on, display the background!
  70.     ld a, %10000001 ; LCDCF_ON | LCDCF_BGON
  71.     ld [rLCDC], a   ; Set display flags
  72.  
  73.     ; Lock the CPU for safety reasons.
  74. .lockcpu
  75.     jr .lockcpu
  76.  
  77.  
  78. ; Finally, actually making the font!
  79. SECTION "Font", ROM0
  80.  
  81. tiFnt:
  82. INCBIN "font.chr" ; Tells RGBDS to copy the contents of font.chr into this ROM section.
  83. tiFntEd:
  84.  
  85. Section "Text", ROM0
  86.  
  87. HelloStr:
  88.     db "Hello GBZ80 World!", 0 ; End with byte 0 as string return (\0 in C)
  89.     ; db for bytes, alternatively -
  90.     ; dw for words (16bits) and
  91.     ; dl for longs (32bits).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement