Advertisement
Redxone

[RGBDS-GBZ80] WIP Gameboy ROM. (Backup)

Oct 16th, 2018
207
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. INCLUDE "hardware_ext.inc"
  4.  
  5. ; Interrupt Requests (IRQs)
  6. ; V-Blank  Interrupt Enable  (INT 40h)  (1=Enable)
  7. ; LCD STAT Interrupt Enable  (INT 48h)  (1=Enable)
  8. ; Timer    Interrupt Enable  (INT 50h)  (1=Enable)
  9. ; Serial   Interrupt Enable  (INT 58h)  (1=Enable)
  10. ; Joypad   Interrupt Enable  (INT 60h)  (1=Enable)
  11.  
  12.  
  13. SECTION "IRQ_VBLANK", ROM0[$0040]
  14.     reti
  15. SECTION "IRQ_LCDC", ROM0[$0048]
  16.     reti
  17. SECTION "IRQ_TIMER", ROM0[$0050]
  18.     reti
  19. SECTION "IRQ_SERIAL", ROM0[$0058]
  20.     reti
  21. SECTION "IRQ_JOYPAD", ROM0[$0060]
  22.     reti
  23.  
  24. ; Extra definitions
  25.  
  26. PADS_DPAD equ $20
  27. PADS_BTNS equ $10
  28. MOVE_TILES equ 8
  29. MOVE_DELAY equ 30
  30. TILE_BYTES equ 16
  31.  
  32. ; Setup Header of memory type ROM0 (Either ROM0 or ROMX), start at 100h
  33. ; 100h is where execution of code begins.
  34. SECTION "Header", ROM0[$100]
  35.  
  36. ; Actual code starts at 104, so we have 4 bytes to start our cart.
  37. EntryPoint:
  38.     di   ; Enable Interupts
  39.     jp Begin ; Begin our program!
  40.  
  41. ; Pad out remaining header bytes.
  42. REPT $150 - $104
  43.     db 0
  44. ENDR
  45.  
  46.  
  47. ; The section of code that'll contain our actual game.
  48. ; Start location isn't specified, doesn't need to be.
  49. SECTION "Game", ROM0
  50.  
  51.  
  52. ; Useful functions
  53.  
  54. ; Multiplies A by C
  55. ; @param A - Value
  56. ; @param C - Multiple
  57. ; @return Z set, A = result
  58. Multiply:
  59.     ld d, a
  60. .multiply_loop
  61.     dec c
  62.     ret z
  63.     add d
  64.     jr Multiply
  65.     ret
  66.  
  67. ; Multiplies BC by C
  68. ; @param BC - Value
  69. ; @param D - Multiple
  70. ; @return Z set, BC = result
  71. Multiply16:
  72.     ld e, a
  73. .multiply16_loop
  74.     dec d
  75.     ret z
  76.     ld a, c
  77.     add e
  78.     ld c, a
  79.     ld a, b
  80.     adc a
  81.     ld b, a
  82.     jr Multiply16
  83.     ret
  84.  
  85. ; Divides A by C
  86. ; @param A - Divisor
  87. ; @param C - Dividend
  88. ; @return C is reset, A = result
  89. Divide:
  90.     ld d, 0
  91. .divide_loop:
  92.     sub c
  93.     inc d
  94.     jr nc, .divide_loop
  95.     dec d
  96.     ld a, d
  97.     ret
  98.  
  99.  
  100. ; Delay Frame/Frams
  101. ; @param A - Amount of frames to delay for
  102. DelayFrame:
  103.     call WaitForVBlank
  104.     ret
  105. DelayFrames:
  106.     call DelayFrame
  107.     dec c
  108.     ret z
  109.     jr DelayFrames
  110.  
  111. ; Get joypad buttons
  112. ; @return A - High Nibble: Directional; Low Nibble: Buttons
  113. getPadButtons:
  114.     ld hl, OLDPAD   ; Update old buttons
  115.     ld a, [PADBTNS] ; ^
  116.     ld [hl], a      ; ^
  117.     ld a, PADS_DPAD ; Setup joypad to read D-PAD
  118.     ld [rP1], a     ; ^
  119.     ld a, [rP1]     ; Get results from joypad twice to eliminate noise.
  120.     ld a, [rP1]     ; Results will be 1101 1111 If no keys are pressed
  121.     cpl         ; Complement A (rP1 almost behaves like active low)
  122.     and $0F         ; As the joypad behaves active low we need to filter On/Off bits.
  123.     swap a          ; Swap DPAD inputs to MSB, prepare to load buttons in LSB
  124.     ld b, a         ; Load DPAD inputs into B
  125.     ld a, PADS_BTNS ; Setup joypad to load buttons
  126.     ld [rP1], a     ; ^
  127.     ld a, [rP1]     ; Get joypad result
  128.     ld a, [rP1]     ; ^
  129.     cpl             ; Filter bits, don't swap
  130.     and $0F         ; ^
  131.     or b            ; DPAD now MSB, BTNS now LSB
  132.     ld [PADBTNS], a ; Save new buttons
  133.     ret
  134.  
  135.  
  136. ; Sets sprite to access by drawSprite
  137. ; @params A - Sprite index
  138. ; @returns sprCount = A*4
  139. setSpriteIndex:
  140.       sla a        ; *sprCount = A*4
  141.       sla a            ; ^
  142.       ld [sprCount], a ; ^
  143.       sra a
  144.       sra a
  145.       ret
  146.  
  147.  
  148. ; Retrieves Map Tile at Coords
  149. ; @params HL - Map source ( _SCRN0; _SCRN1 )
  150. ; @params BC - X,Y
  151. ; @return A - Tile
  152.  
  153. GetMapTile:
  154.     push hl
  155.     ld e, SCRN_VX_B-1  ; (BC * SCRN_X_B-1) + Old B
  156.     ld d, b     ;
  157.     ld b, 0     ;
  158.     ld l, c
  159. .AdjustY
  160.     ld a, c     ;
  161.     add l           ;
  162.     ld c, a         ;
  163.     ld a, b         ;
  164.     adc 0           ;
  165.     ld b, a         ;
  166.     dec e           ;
  167.     jr nz, .AdjustY ;
  168.     ld a, c         ;
  169.     add d           ;
  170.     ld c, a         ;
  171.     ld a, b         ;
  172.     adc 0           ;
  173.     ld b, a         ;
  174.     pop hl
  175.     add hl, bc      ; Load tile ID into A
  176.     ld a, [hl]      ;
  177.     ret
  178.  
  179. ; Get OAM sprite address bsed on sprCount
  180. ; @param  C  = HL offset by n'bytes
  181. ; @return HL = _OAMRAM + *sprCount
  182. getSpriteAddr:
  183.     ld a, [sprCount] ; hl = _OAMRAM + *sprCount
  184.     ld hl, _OAMRAM   ; ^
  185.     add l            ; ^
  186.     add c            ; HL += C
  187.     ld l, a
  188.     ret
  189.  
  190. ; Sets sprite tile based on *sprCount
  191. ; @params C - Tile #
  192. ; @return HL = _OAMRAM + *sprCount + 2
  193. setSpriteTile:
  194.     ld a, [sprCount] ; hl = _OAMRAM + *sprCount
  195.     ld hl, _OAMRAM   ; ^
  196.     add l            ; ^
  197.     add 2        ; HL += 2; Move to tile addr
  198.     ld l, a          ; ^
  199.     ld [hl], c   ; Set sprite tile
  200.     ret
  201.  
  202. ; Gets tile coords at sprite in *sprCount
  203. ; @return BC = Tile coords of sprite.
  204. getSpriteTileCoords:
  205.     ld c, 0
  206.     call getSpriteAddr ; Entry data for spr
  207.     ld a, [hli] ; C = YPos
  208.     srl a       ; Adjust as a tile.
  209.     srl a       ; ^
  210.     srl a           ; ^
  211.     dec a       ; Zero indexed tiles.
  212.     dec a           ;
  213.     ld c, a         ; ^
  214.     ld a, [hli]     ; B = XPos
  215.     srl a       ; Adjust as a tile.
  216.     srl a       ; ^
  217.     srl a       ; ^
  218.     dec a           ; Zero indexed tiles.
  219.     ld b, a         ; ^
  220.     ret
  221.  
  222.  
  223. ; Flips X sprite in *sprCount
  224. FlipSpriteX:
  225.     ld c, 3
  226.     call getSpriteAddr
  227.     ld a, [hl]
  228.     xor OAMF_XFLIP
  229.     ld [hl], a
  230.     ret
  231.  
  232. ; Flips Y sprite in *sprCount1
  233. FlipSpriteY:
  234.     ld c, 3
  235.     call getSpriteAddr
  236.     ld a, [hl]
  237.     xor OAMF_YFLIP
  238.     ld [hl], a
  239.     ret
  240.  
  241. ; Puts sprite data into _OAMRAM + *sprCount
  242. ; @params BC - B = XPos, C = YPos
  243. ; @params DE - D = Sprite Index, E = Sprite attrib
  244. AddNewSprite:
  245.     ld a, [sprCount] ; hl = _OAMRAM + *sprCount
  246.     ld hl, _OAMRAM   ; ^
  247.     add l            ; ^
  248.     ld l, a          ; ^
  249.     ld a, c            
  250.     ld [hli], a      ; *YPos = C
  251.     ld a, b          
  252.     ld [hli], a      ; *XPos = B
  253.     ld a, d
  254.     ld [hli], a      ; *Tile = D
  255.     ld a, e
  256.     ld [hli], a      ; *Attr = E
  257.     ld a, [sprCount] ; *sprCount += 4
  258.     add $04          ; ^
  259.     ld [sprCount], a ; ^
  260.     ret
  261.  
  262. ; Move sprite XY from _OAMRAM + *sprCount
  263. ; @params BC - B = add XPos, C = add YPos
  264. ; @return HL at _OAMRAM+*sprCount + 2 (HL at sprite Tile)
  265. moveSprite:
  266.     call WaitForVBlank
  267.     ld a, [sprCount] ; hl = _OAMRAM + *sprCount
  268.     ld hl, _OAMRAM   ; ^
  269.     add l            ; ^
  270.     ld l, a          ; ^
  271.     ld a, [hl]       ; *HL += C
  272.     add c            ; ^
  273.     ld [hli], a      ; ^
  274.     ld a, [hl]       ; *(HL+1) += B      
  275.     add b            ; ^
  276.     ld [hli], a      ; ^
  277.     ret
  278.  
  279. ; Turns LCD off for accessing VRAM
  280. ; @return sets A to 0
  281. LCD_Off:
  282.     call WaitForVBlank ; Must only be done during VBlank
  283.     xor a         ; == ld a, 0
  284.     ld [rLCDC], a ; Clear LCD flags. Results in LCD turning off.
  285.     ret
  286.  
  287. ; Turns LCD on for displaying VRAM
  288. ; @param B - Specify more flags
  289. ; @return sets A to 128
  290. LCD_On:
  291.     ld a, %10000000 ; LCDCF_ON
  292.     or b            ; Turn on specified flags
  293.     ld [rLCDC], a   ; Set display flags
  294.  
  295.  
  296. ; Waits for vertical blank period to begin.
  297. ; @return rLY >= 144
  298. WaitForVBlank:
  299.     ld a, [rLY] ; Load LCD draw status
  300.     cp 144 ; Check if LCD is in VBlank time.
  301.     ret nc ; Return if VBlank-ing
  302.     jr WaitForVBlank
  303.  
  304.  
  305. ; Waits for vertical blank period to end.
  306. ; @return rLY < 144
  307. WaitVBlankFinished:
  308.     ld a, [rLY] ; Load LCD draw status
  309.     cp 144      ;
  310.     ret c       ; Return if VBlank period is over.
  311.     jr WaitVBlankFinished
  312.  
  313.  
  314.  
  315. ; Copy n'bytes to location
  316. ; @param DE - Source location
  317. ; @param HL - Destination
  318. ; @param BC - Number of bytes to copy
  319. ; @return B and C equal 0
  320. Memcpy:
  321.     ld a, [de]  ; Grab byte from base
  322.     ld [hli], a ; *HL = a; HL++;
  323.     inc de      ; Move to next byte in base
  324.         dec bc      ; Decrease bytes to load.
  325.         ld a, b     ; 16bit inc/dec don't update flags. Update flags using load.
  326.         or c        ; a or c, if b and c are both zero, our copy is done.
  327.         jr nz, Memcpy
  328.         ret         ; Return control
  329.  
  330. ; Set n'bytes to a value
  331. ; @param A  - Value to set bytes to
  332. ; @param HL - Destination
  333. ; @param BC - Number of bytes to set
  334. ; @returns B and C equal 0
  335. Memset:
  336.     ld [hli], a ; *HL = a
  337.     dec bc      ; Decrease byte set count
  338.     ld e, a     ; Backup A
  339.     ld a, b     ; Load b into a so we can compare b and c
  340.     or c        ; if b or c equals 0; all bytes are set.
  341.     ld a, e     ; Restore A
  342.     jr nz, Memset
  343.     ret
  344.  
  345.  
  346. ; Copy string to location
  347. ; @param DE - Location of String to copy
  348. ; @param HL - Destination, where to copy string to
  349. ; @return A equals 0 (Character in string is \0)
  350. ; @return flags Z set, C reset
  351. Strcpy:
  352.     ld a, [de]  ; a = (char) *de
  353.     or a        ; == cp 0
  354.     ret z       ; if(a==0) return;
  355.     ld [hli], a ; *hl = a, hl++
  356.     inc de     ; de++
  357.     jr Strcpy  ; Get next char
  358.  
  359. ; Clear/Initialize _OAMRAM
  360. ClearOAM:
  361.     ; Clear object RAM
  362.     ld hl, _OAMRAM
  363.     ld bc, $A0
  364. .clearOAM_loop
  365.     xor a       ; ld a, 0
  366.     ld [hl], a  ; *HL = a
  367.     inc l       ; Only do an 8bit inc
  368.     dec bc      ; Decrease byte set count
  369.     ld a, b     ; Load b into a so we can compare b and c
  370.     or c        ; if b or c equals 0; all bytes are set.
  371.     ret z      
  372.     jr .clearOAM_loop
  373.  
  374. ; Updates background sprite priority depending on if there's a HUD above them.
  375. UpdateBGSpritesForHUD:
  376.     xor a                ; (ld a, 0) Starting with sprite 0
  377.     call setSpriteIndex  ; *sprCount = a*4
  378. .processBGSprite
  379.     ld hl, rLCDC             ; Clear all priorties if we're not in the HUD.
  380.     bit LCDCB_WINONOFF, [hl] ; ^
  381.     jp z, .notPriority       ; ^
  382.     call getSpriteTileCoords ; BC = Tile XY (BC;XY)
  383.     ld hl, _SCRN0        ; Get map tile from HL
  384.     call GetMapTile          ; Get background tile under sprite in _SCRN0
  385.     ld d, a                  ; Store BG tile in D
  386.     ld hl, _SCRN1        ; Get window tile under sprite in _SCRN1
  387.     add hl, bc       ; ^
  388.     ld a, [hl]               ; Store WN tile in A
  389.     cp d             ; Compare tiles
  390.     jr z, .notPriority   ; If tiles are equal, don't prioritize sprite.
  391.     ld c, 3          ; Get sprite attribute byte
  392.     call getSpriteAddr   ; ^
  393.     set OAMB_PRI, [hl]   ; Set sprite priority
  394.     jr .processNextSprite    ; Get next sprite to check.
  395. .notPriority
  396.     ld c, 3          ; Get sprite attribute byte
  397.     call getSpriteAddr   ; ^
  398.     res OAMB_PRI, [hl]   ; Clear sprite priority
  399. .processNextSprite
  400.     ld a, [sprCount]     ; Grab current sprite offset
  401.     srl a            ; Convert sprite offset to index
  402.     srl a            ; ^
  403.     inc a            ; Next sprite
  404.     cp 41            ; If we've just processed our last sprite...
  405.     ret z                    ; Exit.
  406.     call setSpriteIndex  ; Set sprite to next.
  407.     jr .processBGSprite      ; Proceed to process next sprite.
  408.  
  409. ; Macros
  410.  
  411. waitjoypad: MACRO
  412. .wjploop\@: ; The \@ generates a unique label because macro's are "inline"
  413.     call getPadButtons
  414.     ld hl, OLDPAD
  415.     bit \1, [hl]
  416.     jr nz, .wjploop\@
  417.     bit \1, a
  418.     jr z, .wjploop\@
  419.     ENDM
  420.  
  421. addHLn16: MACRO
  422.     ld a, l
  423.     add \2
  424.     ld l, a
  425.     ld a, h
  426.     adc \1
  427.     ld h, a
  428.     ENDM
  429. addDEn16: MACRO
  430.     ld a, e
  431.     add \2
  432.     ld e, a
  433.     ld a, d
  434.     adc \1
  435.     ld d, a
  436.     ENDM
  437.  
  438. ; Takes /2 and /3 as X,Y screen coords
  439. ; Converts X,Y into screen offset, which is put in HL.
  440. ;
  441. ;   X + Y *= SCRN_VX_B
  442. ;
  443. coords: MACRO
  444.     ld e, SCRN_VX_B-1 ; (BC * SCRN_X_B-1) + Old B
  445.     ld d, \1        ; Store X into D
  446.     ld c, \2    ; Store Y into C
  447.     ld l, c     ; Backup original value of Y (C)
  448.     ld b, 0     ; B is where all addition that carries over is going to go into.
  449. .AdjustY\@
  450.     ld a, c     ; C *= SCRN_X_B-1
  451.     add l           ; ^
  452.     ld c, a         ; ^
  453.     ld a, b         ; ^
  454.     adc 0           ; Adjust multiplication for 16bits
  455.     ld b, a         ; ^
  456.     dec e           ; Continue to multiply
  457.     jr nz, .AdjustY\@ ; ^
  458.     ld a, c         ; Add X coord
  459.     add d           ; ^
  460.     ld c, a         ; ^
  461.     ld a, b         ; ^
  462.     adc 0           ; Adjust for 16 bits.
  463.     ld b, a         ; ^
  464.     ld h, b     ; Load result into HL
  465.     ld l, c         ; ^
  466.     ld bc, _SCRN1   ; Convert to tiles coords.
  467.     addHLn16 b, c
  468.     ENDM
  469.  
  470. show_window: MACRO
  471.     ld hl, rLCDC
  472.     set LCDCB_WINONOFF, [hl]
  473.     ENDM
  474. hide_window: MACRO
  475.     ld hl, rLCDC
  476.     res LCDCB_WINONOFF, [hl]
  477.     ENDM
  478.  
  479.  
  480. LoadHUDTiles: MACRO
  481.     ; Loads into 9000, tile ram starts at 8800.
  482.     ; We load the border tiles last because we need the padding for ASCII
  483.     ld hl, _VRAM+$1000 ; Tile RAM
  484.     ld de, tiFnt
  485.     ld bc, tiFntEd - tiFnt ; Get size of Font in bytes
  486.     call Memcpy ; Copy from DE, to HL. BC amount of bytes.
  487.     ld hl, _VRAM+$1000+TILE_BYTES
  488.     ld de, ti_hud
  489.     ld bc, ti_hudEd - ti_hud
  490.     call Memcpy
  491.     ENDM
  492.  
  493. ; Copy visible _SCRN0 to _SCRN1
  494. CloneScrn:
  495.     ld a, 18 ; Tile lines
  496.     ld hl, _SCRN1 ; Dest
  497.     ld de, _SCRN0 ; Source
  498. .CloneScrnLines
  499.     push af
  500.     ld bc, SCRN_X_B  ; Copy n'bytes
  501.     call Memcpy
  502.     addHLn16 0, 12
  503.     addDEn16 0, 12
  504.     pop af
  505.     dec a            ; Loop for every SCRN line.
  506.     ret z        ; ^
  507.     jr .CloneScrnLines
  508.  
  509. Begin:
  510.     ; Turn off the LCD
  511.     call LCD_Off  ; LCD_Off waits for VBlank already
  512.     call ClearOAM ; Clear sprite memory (It starts filled with garbage)
  513.  
  514.     ; Clear beginning tile logo.
  515.     ld hl, $9900
  516.     ld a, 0
  517.     ld bc, $33
  518.     call Memset
  519.  
  520.     ; Now that the LCD is off so is the PPU
  521.     ; We can now access VRAM freely.
  522.     LoadHUDTiles
  523.  
  524.     ; Setup sprites. Load all sprites into VRAM
  525.     ld hl, _VRAM  
  526.     ld de, ti_sprites
  527.     ld bc, ti_spritesEd - ti_sprites
  528.     call Memcpy
  529.  
  530.     ; Sprite 0
  531.     xor a      
  532.     call setSpriteIndex
  533.     ld bc, $4030 ; X, Y
  534.     ld de, $0000 ; Tile: 00, Flags: 00
  535.     call AddNewSprite
  536.  
  537.     ; Sprite 1
  538.     ld a, 1    
  539.     call setSpriteIndex
  540.     ld bc, $1616 ; X, Y
  541.     ld de, $0000 ; Tile: 00, Flags: 00
  542.     call AddNewSprite
  543.    
  544.         ; Copy "Press Start" to Window.
  545.         coords 4, 7
  546.         ld de, PStartStr   ; Load 1st byte of string
  547.         call Strcpy        ; Copy from DE to HL. Until char == 0.
  548.  
  549.     ; Init display regs
  550.     ld a, %11100100 ; Load palette, 11 10 01 00. Black,Gray,LightGray,White
  551.     ld [rBGP], a    ; Background Palette Data
  552.     ; Sprite PAL regs
  553.     ld [rOBP0], a
  554.  
  555.     ; Reset BG scroll X/Y
  556.     xor a           ; == ld a, 0\
  557.     ld [rSCY], a
  558.     ld [rSCX], a
  559.  
  560.     ; Shut off APU (Sound)
  561.     ; bit7 is all that matters, although other bits don't do anything
  562.     ; and 7 ends up controlling all the other channels so, meh set it to 0.
  563.     ld [rNR52], a
  564.  
  565.     ld a, 7
  566.     ld [rWX], a
  567.     xor a
  568.     ld [rWY], a
  569.  
  570.  
  571.     ; Turn screen on, display the background!
  572.     ld b, LCDCF_BGON | LCDCF_WIN9C00
  573.     call LCD_On
  574.     show_window
  575.  
  576.     waitjoypad PADB_START
  577.     hide_window
  578.  
  579.     ld hl, rLCDC             ; Draw sprites after pressed.
  580.     set LCDCB_OBJONOFF, [hl] ; ^
  581.  
  582.     call WaitForVBlank
  583.     ld hl, _SCRN0+$61  ; Start print at X:0,Y:0 tiles
  584.         ld de, HelloStr   ; Load 1st byte of string
  585.         call Strcpy      
  586.    
  587. .mainLoop
  588.     ; Select the "player" sprite.
  589.     xor a
  590.     call setSpriteIndex
  591.     call getPadButtons
  592.     ld d, a
  593.     or a               ; cp 0
  594.     call nz, WaitForVBlank ; Wait for VBlank before moving sprite.
  595.     ld a, d
  596.     bit PADB_DOWN, a
  597.     jr nz, .moveDown
  598.     bit PADB_UP, a
  599.     jr nz, .moveUp
  600.     bit PADB_LEFT, a
  601.     jp nz, .moveLeft
  602.     bit PADB_RIGHT, a
  603.     jp nz, .moveRight
  604.     ld hl, OLDPAD        ; Make sure we are not holding start.
  605.     bit PADB_START, [hl] ; ^
  606.     jr nz, .mainLoop     ; ^
  607.     bit PADB_START, a  
  608.     jr nz, .debugBtn
  609.     jr .mainLoop
  610.  
  611. ; Toggle "hud"
  612. .debugBtn:
  613.     ld hl, rLCDC
  614.     ld a, [hl]
  615.     xor LCDCF_WINON
  616.     ld [hl], a
  617.     push af
  618.     call LCD_Off
  619.     call CloneScrn
  620.     coords 4, 7
  621.         ld de, PStartStr   ; Load 1st byte of string
  622.         call Strcpy        ; Copy from DE to HL. Until char == 0.
  623.     pop af
  624.     ld b, a
  625.     call LCD_On
  626.     call WaitForVBlank
  627.     call UpdateBGSpritesForHUD
  628.         jr .mainLoop
  629.  
  630. ; Down movement
  631. .moveDown
  632.     ld d, MOVE_TILES
  633.     ld b, 0
  634.     ; Set walking tile
  635.     ld c, 1
  636.     call setSpriteTile
  637.     call FlipSpriteX
  638. .moveDownCont
  639.     ld c, MOVE_DELAY
  640.     call DelayFrames
  641.     ld c, 1 ; 0, 1
  642.     call moveSprite
  643.     ; Move again
  644.     dec d
  645.     jr nz, .moveDownCont
  646.     ; Set player sprite to down-facing sprite.
  647.     ld c, 0
  648.     call setSpriteTile
  649.     jp .mainLoop
  650.  
  651. ; Up movement
  652. .moveUp
  653.     ld d, MOVE_TILES
  654.     ld b, 0
  655.     ; Set walking up tile
  656.     ld c, 5
  657.     call setSpriteTile
  658.     call FlipSpriteX
  659. .moveUpCont
  660.     ld c, MOVE_DELAY
  661.     call DelayFrames
  662.     ld c, $FF ; 0, -1
  663.     call moveSprite
  664.     ; Move again
  665.     dec d
  666.     jr nz, .moveUpCont
  667.     ; Set player sprite to up-facing sprite.
  668.     ld c, 4
  669.     call setSpriteTile
  670.     jp .mainLoop
  671.  
  672. ; Left movement
  673. .moveLeft
  674.     ; Load right-facing sprite, flip X
  675.     ld c, 3
  676.     call setSpriteTile
  677.     call FlipSpriteX
  678.     ld d, MOVE_TILES
  679.     ld b, $FF ; -1, 0
  680. .moveLeftCont
  681.     ld c, MOVE_DELAY
  682.     call DelayFrames
  683.     ld c, 0
  684.     call moveSprite
  685.     dec d
  686.     jr nz, .moveLeftCont
  687.     ld c, 2
  688.     call setSpriteTile
  689.     jp .mainLoop
  690.  
  691. ; Right movement
  692. .moveRight
  693.     ; Load right-facing sprite, do not flip X
  694.     ld c, 3
  695.     call setSpriteTile
  696.     ld d, MOVE_TILES
  697.     ld b, $01 ; 1, 0
  698.     call FlipSpriteX
  699. .moveRightCont
  700.     ld c, MOVE_DELAY
  701.     call DelayFrames
  702.     ld c, 0
  703.     call moveSprite
  704.     ; Move again
  705.     dec d
  706.     jr nz, .moveRightCont
  707.     ld c, 2
  708.     call setSpriteTile
  709.     jp .mainLoop
  710.  
  711.  
  712. ; RAM Variables
  713. Section "RAM Variables", WRAM0
  714.  
  715. sprCount: ; Allocate sprCount ($C000)
  716.     ds 1
  717. PADBTNS:
  718.     ds 1
  719. OLDPAD:
  720.     ds 1
  721.  
  722.  
  723. ; Sprites
  724. SECTION "Sprites", ROM0
  725.  
  726. ti_sprites:
  727. INCBIN "player.2bpp"
  728. INCBIN "sprite.2bpp"
  729. ti_spritesEd:
  730.  
  731.  
  732. ; Finally, actually making the font!
  733. SECTION "Font", ROM0
  734.  
  735. ti_hud:
  736. INCBIN "textbox_border.2bpp"
  737. ti_hudEd:
  738.  
  739. tiFnt:
  740. INCBIN "font.2bpp" ; Tells RGBDS to copy the contents of font.chr into this ROM section.
  741. tiFntEd:
  742.  
  743. Section "Text", ROM0
  744.  
  745. WinStr:
  746.     db "String on Window :O", 0
  747. PStartStr:
  748.     db "PRESS START", 0
  749. HelloStr:
  750.     db "Hello GBZ80 World!", 0 ; End with byte 0 as string return (\0 in C)
  751.     ; db for bytes, alternatively -
  752.     ; dw for words (16bits) and
  753.     ; dl for longs (32bits).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement