git.y1.nz

SameBoy

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

BootROMs/sgb_boot.asm

      1 ; SameBoy SGB 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 to white
     31     ld a, %00_00_00_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     ; Turn on LCD
     77     ld a, LCDCF_ON | LCDCF_BLK01 | LCDCF_BGON
     78     ldh [rLCDC], a
     79 
     80     ld a, $F1 ; Packet magic, increases by 2 for every packet
     81     ldh [hCommand], a
     82     ld hl, NintendoLogo ; Header start
     83 
     84     xor a
     85     ld c, a ; JOYP
     86 
     87 .sendCommand
     88     xor a
     89     ldh [c], a
     90     ld a, $30
     91     ldh [c], a
     92 
     93     ldh a, [hCommand]
     94     call SendByte
     95     push hl
     96 
     97     ld b, 14
     98     ld d, 0
     99 .checksumLoop
    100     call ReadHeaderByte
    101     add d
    102     ld d, a
    103     dec b
    104     jr nz, .checksumLoop
    105 
    106     ; Send checksum
    107     call SendByte
    108     pop hl
    109 
    110     ld b, 14
    111 .sendLoop
    112     call ReadHeaderByte
    113     call SendByte
    114     dec b
    115     jr nz, .sendLoop
    116 
    117     ; Done bit
    118     ld a, $20
    119     ldh [c], a
    120     ld a, $30
    121     ldh [c], a
    122 
    123     ; Wait 4 frames
    124     ld e, 4
    125     ld a, 1
    126     ldh [rIE], a
    127     xor a
    128 .waitLoop
    129     ldh [rIF], a
    130     halt
    131     nop
    132     dec e
    133     jr nz, .waitLoop
    134     ldh [rIE], a
    135 
    136     ; Update command
    137     ldh a, [hCommand]
    138     add 2
    139     ldh [hCommand], a
    140 
    141     ld a, $58
    142     cp l
    143     jr nz, .sendCommand
    144 
    145     ; Write to sound registers for DMG compatibility
    146     ld c, LOW(rNR13)
    147     ld a, $C1
    148     ldh [c], a
    149     inc c
    150     ld a, $7
    151     ldh [c], a
    152 
    153     ; Init BG palette
    154     ld a, %11_11_11_00
    155     ldh [rBGP], a
    156 
    157 ; Set registers to match the original SGB boot
    158 IF DEF(SGB2)
    159     ld a, BOOTUP_A_MGB
    160 ELSE
    161     ld a, BOOTUP_A_DMG
    162 ENDC
    163     ld hl, $C060
    164 
    165 ; Boot the game
    166     jp BootGame
    167 
    168 ReadHeaderByte:
    169     ld a, $4F
    170     cp l
    171     jr c, .zero
    172     ld a, [hli]
    173     ret
    174 .zero:
    175     inc hl
    176     xor a
    177     ret
    178 
    179 SendByte:
    180     ld e, a
    181     ld d, 8
    182 .loop
    183     ld a, $10
    184     rr e
    185     jr c, .zeroBit
    186     add a ; 10 -> 20
    187 .zeroBit
    188     ldh [c], a
    189     ld a, $30
    190     ldh [c], a
    191     dec d
    192     ret z
    193     jr .loop
    194 
    195 DoubleBitsAndWriteRow:
    196 ; Double the most significant 4 bits, b is shifted by 4
    197     ld a, 4
    198     ld c, 0
    199 .doubleCurrentBit
    200     sla b
    201     push af
    202     rl c
    203     pop af
    204     rl c
    205     dec a
    206     jr nz, .doubleCurrentBit
    207     ld a, c
    208 ; Write as two rows
    209     ldi [hl], a
    210     inc hl
    211     ldi [hl], a
    212     inc hl
    213     ret
    214 
    215 TrademarkSymbol:
    216     pusho
    217     opt b.X
    218     db %..XXXX..
    219     db %.X....X.
    220     db %X.XXX..X
    221     db %X.X..X.X
    222     db %X.XXX..X
    223     db %X.X..X.X
    224     db %.X....X.
    225     db %..XXXX..
    226     popo
    227 TrademarkSymbolEnd:
    228 
    229 SECTION "BootGame", ROM0[$00FE]
    230 BootGame:
    231     ldh [rBANK], a
    232 
    233 SECTION "HRAM", HRAM[_HRAM]
    234 hCommand:
    235     ds 1

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