git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

BootROMs/dmg_boot.asm

      1 ; SameBoy DMG bootstrap ROM
      2 
      3 include "sameboot.inc"
      4 
      5 SECTION "BootCode", ROM0[$0000]
      6 Start:
      7 ; Init stack pointer
      8     ld sp, $FFFE
      9 
     10 ; Clear memory VRAM
     11     ld hl, _VRAM
     12     xor a
     13 
     14 .clearVRAMLoop
     15     ldi [hl], a
     16     bit 5, h
     17     jr z, .clearVRAMLoop
     18 
     19 ; Init Audio
     20     ld a, AUDENA_ON
     21     ldh [rNR52], a
     22     assert AUDENA_ON == AUDLEN_DUTY_50
     23     ldh [rNR11], a
     24     ld a, $F3
     25     ldh [rNR12], a ; Envelope $F, decreasing, sweep $3
     26     ldh [rNR51], a ; Channels 1+2+3+4 left, channels 1+2 right
     27     ld a, $77
     28     ldh [rNR50], a ; Volume $7, left and right
     29 
     30 ; Init BG palette
     31     ld a, %01_01_01_00
     32     ldh [rBGP], a
     33 
     34 ; Load logo from ROM.
     35 ; A nibble represents a 4-pixels line, 2 bytes represent a 4x4 tile, scaled to 8x8.
     36 ; Tiles are ordered left to right, top to bottom.
     37     ld de, NintendoLogo
     38     ld hl, _VRAM + $10 ; This is where we load the tiles in VRAM
     39 
     40 .loadLogoLoop
     41     ld a, [de] ; Read 2 rows
     42     ld b, a
     43     call DoubleBitsAndWriteRow
     44     call DoubleBitsAndWriteRow
     45     inc de
     46     ld a, e
     47     xor LOW(NintendoLogoEnd)
     48     jr nz, .loadLogoLoop
     49 
     50 ; Load trademark symbol
     51     ld de, TrademarkSymbol
     52     ld c, TrademarkSymbolEnd - TrademarkSymbol
     53 .loadTrademarkSymbolLoop:
     54     ld a, [de]
     55     inc de
     56     ldi [hl], a
     57     inc hl
     58     dec c
     59     jr nz, .loadTrademarkSymbolLoop
     60 
     61 ; Set up tilemap
     62     ld a, $19                           ; Trademark symbol tile ID
     63     ld [_SCRN0 + 8 * SCRN_VX_B + 16], a ; ... put in the superscript position
     64     ld hl, _SCRN0 + 9 * SCRN_VX_B + 15  ; Bottom right corner of the logo
     65     ld c, 12                            ; Tiles in a logo row
     66 .tilemapLoop
     67     dec a
     68     jr z, .tilemapDone
     69     ldd [hl], a
     70     dec c
     71     jr nz, .tilemapLoop
     72     ld l, $0F ; Jump to top row
     73     jr .tilemapLoop
     74 .tilemapDone
     75 
     76     ld a, 30
     77     ldh [rSCY], a
     78 
     79     ; Turn on LCD
     80     ld a, LCDCF_ON | LCDCF_BLK01 | LCDCF_BGON
     81     ldh [rLCDC], a
     82 
     83     ld d, LOW(-119)
     84     ld c, 15
     85 
     86 .animate
     87     call WaitFrame
     88     ld a, d
     89     sra a
     90     sra a
     91     ldh [rSCY], a
     92     ld a, d
     93     add c
     94     ld d, a
     95     ld a, c
     96     cp 8
     97     jr nz, .noPaletteChange
     98     ld a, %10_10_10_00
     99     ldh [rBGP], a
    100 .noPaletteChange
    101     dec c
    102     jr nz, .animate
    103     ld a, %11_11_11_00
    104     ldh [rBGP], a
    105 
    106     ; Play first sound
    107     ld a, $83
    108     call PlaySound
    109     ld b, 5
    110     call WaitBFrames
    111     ; Play second sound
    112     ld a, $C1
    113     call PlaySound
    114 
    115 
    116 
    117 ; Wait ~1 second
    118     ld b, 60
    119     call WaitBFrames
    120 
    121 ; Set registers to match the original DMG boot
    122 IF DEF(MGB)
    123     lb hl, BOOTUP_A_MGB, %10110000
    124 ELSE
    125     lb hl, BOOTUP_A_DMG, %10110000
    126 ENDC
    127     push hl
    128     pop af
    129     ld hl, HeaderChecksum
    130     lb bc, 0, LOW(rNR13) ; $0013
    131     lb de, 0, $D8        ; $00D8
    132 
    133 ; Boot the game
    134     jp BootGame
    135 
    136 
    137 DoubleBitsAndWriteRow:
    138 ; Double the most significant 4 bits, b is shifted by 4
    139     ld a, 4
    140     ld c, 0
    141 .doubleCurrentBit
    142     sla b
    143     push af
    144     rl c
    145     pop af
    146     rl c
    147     dec a
    148     jr nz, .doubleCurrentBit
    149     ld a, c
    150 ; Write as two rows
    151     ldi [hl], a
    152     inc hl
    153     ldi [hl], a
    154     inc hl
    155     ret
    156 
    157 WaitFrame:
    158     push hl
    159     ld hl, rIF
    160     res 0, [hl]
    161 .wait
    162     bit 0, [hl]
    163     jr z, .wait
    164     pop hl
    165     ret
    166 
    167 WaitBFrames:
    168     call WaitFrame
    169     dec b
    170     jr nz, WaitBFrames
    171     ret
    172 
    173 PlaySound:
    174     ldh [rNR13], a
    175     ld a, AUDHIGH_RESTART | $7
    176     ldh [rNR14], a
    177     ret
    178 
    179 
    180 TrademarkSymbol:
    181     pusho
    182     opt b.X
    183     db %..XXXX..
    184     db %.X....X.
    185     db %X.XXX..X
    186     db %X.X..X.X
    187     db %X.XXX..X
    188     db %X.X..X.X
    189     db %.X....X.
    190     db %..XXXX..
    191     popo
    192 TrademarkSymbolEnd:
    193 
    194 SECTION "BootGame", ROM0[$00FE]
    195 BootGame:
    196     ldh [rBANK], a ; unmap boot ROM

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.