document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ; Super Game Boy development (step by step)
  2. ; Made by Imanol Barriuso (Imanolea) for Games aside
  3.  
  4. INCLUDE "dev/gbhw.inc"              ; File with the system addresses defined
  5.  
  6. ; Constants
  7. ; System
  8. LCDCF_DEFAULT       EQU LCDCF_ON|LCDCF_BG8800|LCDCF_BG9800|LCDCF_BGON|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_WIN9C00|LCDCF_WINON
  9. ; Routines
  10. DMA_TRANSFER        EQU $FF80       ; HRAM address available during the DMA Transfer
  11. ; Banks
  12. DEFAULT_BNK         EQU 1
  13. SGB_BNK             EQU 2
  14.  
  15. ; Variables
  16. ; Hardware state
  17. _CUR_BNK            EQU _RAM + 0    ; Indicates the currently used memory bank
  18. _VBLANK_F           EQU _CUR_BNK + 1; Raises to indicate that we are waiting for the VBlank interruption
  19. _SGB_F              EQU _VBLANK_F + 1; Raises to indicate that the hardware in which the software it is running is the Super Game Boy
  20. ; Auxiliary variables
  21. _AUXVAR             EQU _SGB_F + 1
  22.  
  23. ; Virtual OAM
  24. _OAM                EQU _RAM + 512  ; This is the start address of the sprite memory that will be copied into the real OAM RAM during the VBlank interruption
  25.  
  26. ; VBlank interruption
  27. SECTION "vblank",HOME[$0040]
  28.     call    vblank
  29.     reti
  30.  
  31. ; Program start
  32. SECTION "start", HOME[$0100]
  33.     nop
  34.     jp      start
  35.  
  36.     ROM_HEADER "SGB development", $01, ROM_MBC1, ROM_SIZE_128KBYTE, RAM_SIZE_0KBYTE
  37.  
  38. start:
  39.     nop
  40.     ; Base system initialization
  41.     di
  42.     call    display_off
  43.     ; Sytem registers reset
  44.     xor     a
  45.     ld      [rIF]a
  46.     ld      [rSCX], a
  47.     ld      [rSCY], a
  48.     ld      [rSB]a
  49.     ld      [rSC]a
  50.     ld      [rTMA], a
  51.     ld      [rTAC], a
  52.     ld      [rBGP], a
  53.     ld      [rOBP0],    a
  54.     ld      [rOBP1],    a
  55.     ; We position the window out of the visible screen
  56.     ld      hl, rWX
  57.     ld      [hl],   7
  58.     ld      hl, rWY
  59.     ld      [hl],   144
  60.     ; Interruptions
  61.     ld      a,  IEF_VBLANK
  62.     ld      [rIE]a               ; VBlank interruption enabled
  63.     ; RAM reset
  64.     ld      de, _RAM
  65.     ld      bc, $2000
  66.     ld      l$0
  67.     call    fillmem
  68.     ld      sp, $E000               ; Stack pointer points to the RAM end
  69.     ; VRAM reset
  70.     ld      de, _VRAM
  71.     ld      bc, $2000
  72.     ld      l$0
  73.     call    fillmem
  74.     ; Background state reset
  75.     ld      de, _SCRN0
  76.     ld      bc, $2000
  77.     ld      l$0
  78.     call    fillmem
  79.     ; Loads the DMA Transfer routine. It will be in charge of updating the OAM RAM during the VBlank interruption
  80.     ld      hl, DMATransferRoutine
  81.     ld      de, DMA_TRANSFER
  82.     ld      bc, DMATransferRoutineEnd - DMATransferRoutine
  83.     call    copymem
  84.     call    sound_off
  85.     ld      a,  LCDCF_DEFAULT
  86.     ld      [rLCDC],    a           ; LCD turn on
  87.     ld      a1
  88.     ld      [_VBLANK_F],    a       ; We wait for the VBlank interruption
  89.     ei
  90.     halt                            ; We wait for the first VBlank interruption, it will reset the OAM RAM
  91.     nop
  92.     call    init_sgb
  93.     call    init_titlescreen
  94.     ei
  95. main_loop:
  96. .main_loop_0:
  97.     ld      a1
  98.     ld      [_VBLANK_F],    a       ; We wait for the VBlank interruption
  99. .main_loop_1:
  100.     halt
  101.     nop
  102.     ld      a[_VBLANK_F]
  103.     and     a
  104.     jr      nz, .main_loop_1        ; If the VBlank interruption didn\'t occur, we jump
  105.     jr      .main_loop_0
  106.  
  107. ; Vblank implementation
  108. vblank:
  109.     push    af
  110.     push    bc
  111.     push    de
  112.     push    hl
  113.     ld      a[_VBLANK_F]
  114.     and     a
  115.     jr      z,  vblank_0            ; If we are not waiting for the interruption, we return
  116.     call    vram_upd
  117.     xor     a
  118.     ld      [_VBLANK_F],    a
  119. vblank_0:
  120.     pop     hl
  121.     pop     de
  122.     pop     bc
  123.     pop     af
  124.     ret
  125.  
  126. ; VRAM update
  127. vram_upd:
  128.     call    DMA_TRANSFER            ; It is charge of writing in the OAM RAM the data of the sprites stored in _OAM
  129.     ret
  130.  
  131. ; Super Game Boy initialization
  132. init_sgb:
  133.     ; Empty
  134.     ret
  135.  
  136. ; Initializes the title screen
  137. init_titlescreen:
  138.     di
  139.     call    display_off
  140.     ; Palette initialization
  141.     ld      a%11100100
  142.     ld      [rBGP], a
  143.     ld      [rOBP0],    a
  144.     ld      hl, rOBP1
  145.     ld      [hl],   %00011110
  146.     ; Title screen bank
  147.     ld      a,  DEFAULT_BNK
  148.     ld      [_CUR_BNK], a
  149.     ld      [$2000],    a
  150.     ; VRAM tile load
  151.     ld      hl, TitleTiles
  152.     ld      de, _VRAM + 4096
  153.     ld      bc, EndTitleTiles - TitleTiles
  154.     call    copymem
  155.     ; Title screen load
  156.     ld      de, TitleScreen
  157.     ld      hl, _SCRN0
  158.     call    loadscreen
  159.     ; LCD configuration
  160.     ld      a,  LCDCF_DEFAULT
  161.     ld      [rLCDC],    a
  162.     ret
  163.  
  164. ; Additional routines
  165. ; Basic hardware routines
  166. INCLUDE "dev/base/utils.asm"
  167.  
  168. ; Additional memory banks
  169. ; Title screen
  170. SECTION "default", DATA[$4000], BANK[DEFAULT_BNK]
  171. TitleTiles::
  172. INCLUDE "dev/data/tiles/title_tileset.z80"
  173. EndTitleTiles::
  174. TitleScreen::
  175. INCLUDE "dev/data/maps/titlescreen.z80"
  176. EndTitleScreen::
  177. ; Super Game Boy
  178. SECTION "supergameboy", DATA[$4000], BANK[SGB_BNK]
  179. ; Empty
');