git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/targets/z80/sms/crt0.s

      1         .include        "global.s"
      2 
      3         .title  "Runtime"
      4         .module Runtime
      5 
      6         .ez80
      7 
      8         .area   _HEADER (ABS)
      9 
     10         .globl  .OUTI16
     11 
     12         .org    0x00            ; Reset 00h
     13         di                      ; disable interrupt
     14         im 1                    ; interrupt mode 1 (this won't change)
     15         jp .init
     16 
     17 ;        .org    0x08            ; --profile handler
     18 
     19         .org    0x10            ; RST 0x10: VDP_WRITE_CMD
     20 
     21 _WRITE_VDP_CMD::
     22         VDP_WRITE_CMD h, l
     23         ret
     24 
     25 ;        .org    0x18            ; unusable
     26 
     27         .org    0x20            ; RST 0x20: VDP_WRITE_DATA
     28 
     29 _WRITE_VDP_DATA::
     30         VDP_WRITE_DATA h, l
     31         ret
     32 
     33 ;       .org    0x28             ; unusable
     34 
     35         .org    0x30            ; RST 0x30: call HL
     36 .call_hl::
     37         jp      (HL)
     38 
     39         .org    0x38            ; handle IRQ
     40         jp _INT_ISR
     41 
     42         .org    0x66            ; handle NMI
     43         jp _NMI_ISR
     44 
     45         .org    0x80
     46 
     47 .init::
     48         ld sp, #.STACK          ; set stack pointer at end of RAM
     49 
     50         ld a, (#.BIOS)
     51         push af
     52 
     53         xor a
     54         ld bc, #l__DATA
     55         ld hl, #s__DATA
     56         call .memset_simple     ; initialize veriables in RAM with zero
     57 
     58         pop af
     59         ld (#__BIOS), a         ; save BIOS value
     60 
     61         ld hl, #_shadow_OAM
     62         ld de, #(_shadow_OAM + 1)
     63         ld bc, #64
     64         ld (hl), #0xc0
     65         ldir
     66         ld (hl), #0
     67         ld bc, #(128 - 1)
     68         ldir
     69 
     70         ld hl,#0x0000           ; initialize mappers
     71         ld (#.RAM_CONTROL),hl   ; [.RAM_CONTROL]=$00, [.MAP_FRAME0]=$00
     72         ld hl,#0x0201
     73         ld (#.MAP_FRAME1),hl    ; [.MAP_FRAME1]=$01, [.MAP_FRAME2]=$02
     74 
     75         ; Initialise global variables
     76         call .gsinit
     77 
     78         ; Clear VRAM and Initialize VDP
     79 
     80         ld hl, #((.VDP_R1 << 8) | (.R1_DEFAULT | .R1_DISP_OFF))
     81         ld c, #.VDP_CMD
     82         out (c), l
     83         out (c), h
     84 
     85         ; clear VRAM
     86         call .clear_VRAM
     87 
     88         ; set default palette
     89         call .set_default_palette
     90 
     91         ; set VDP registers
     92         ld c, #.VDP_CMD
     93         ld b, #(.shadow_VDP_end - .shadow_VDP)
     94         ld hl,#(.shadow_VDP_end - 1)
     95 1$:
     96         outd
     97 
     98         ld a, b
     99         or #.VDP_REG_MASK
    100         out (c), a
    101 
    102         ld a, b
    103         or a
    104         jr nz, 1$
    105 
    106         ; detect PAL/NTSC
    107         ld c, #.VDP_VCOUNTER
    108 2$:     in a, (c)
    109         cp #0x80
    110         jr nz, 2$
    111 3$:     ld b, a
    112         in a, (c)
    113         cp b
    114         jr nc, 3$
    115 
    116         ld a, b
    117         cp #0xE8
    118         ld a, #.SYSTEM_NTSC
    119         jr c, 4$
    120         ld a, #.SYSTEM_PAL
    121 4$:
    122         ld (#__SYSTEM), a
    123 
    124         VDP_CANCEL_INT
    125 
    126         ei                      ; re-enable interrupts before going to main()
    127 
    128         call _main
    129 10$:
    130         halt
    131         jr 10$
    132 
    133         ; Ordering of segments for the linker.
    134         .area   _HOME
    135         .area   _BASE
    136         .area   _CODE
    137         .area   _CODE_0
    138         .area   _LIT
    139         .area   _INITIALIZER
    140         .area   _GSINIT
    141         .area   _GSFINAL
    142 
    143         .area   _DATA
    144         .area   _INITIALIZED
    145         .area   _BSEG
    146         .area   _BSS
    147         .area   _HEAP
    148         .area   _HEAP_END
    149 
    150         .area   _CODE
    151         .area   _GSINIT
    152 .gsinit::
    153         ; initialize static storage variables
    154         ld bc, #l__INITIALIZER
    155         ld hl, #s__INITIALIZER
    156         ld de, #s__INITIALIZED
    157         call .memcpy_simple
    158 
    159         .area   _GSFINAL
    160         ret
    161 
    162         .area   _HOME
    163 
    164 .clear_VRAM:
    165         ld hl, #.VDP_VRAM
    166         ld c, #.VDP_CMD
    167         out (c), l
    168         out (c), h
    169         xor a
    170         ld bc, #0x4101
    171         jr 6$
    172 5$:
    173         out (.VDP_DATA), a
    174 6$:
    175         dec c
    176         jr nz, 5$
    177         dec b
    178         jr nz, 5$
    179         ret
    180 
    181 .set_default_palette:
    182         ld hl, #.VDP_CRAM
    183         ld c, #.VDP_CMD
    184         out (c), l
    185         out (c), h
    186         ld c, #.VDP_DATA
    187         ld hl, #.CRT_DEFAULT_PALETTE
    188         call .OUTI16
    189         ld hl, #.CRT_DEFAULT_PALETTE
    190         jp .OUTI16
    191 
    192 .CRT_DEFAULT_PALETTE::
    193         .db 0b00111111
    194         .db 0b00101010
    195         .db 0b00010101
    196         .db 0b00000000
    197         .db 0b00000010
    198         .db 0b00001000
    199         .db 0b00100000
    200         .db 0b00001010
    201         .db 0b00101000
    202         .db 0b00100010
    203         .db 0b00000011
    204         .db 0b00001100
    205         .db 0b00110000
    206         .db 0b00001111
    207         .db 0b00111100
    208         .db 0b00110011
    209 
    210         ; fills memory at HL of length BC with A, clobbers DE
    211 .memset_simple::
    212         ld e, a
    213         ld a, c
    214         or b
    215         ret z
    216         ld (hl), e
    217         dec bc
    218         ld d, h
    219         ld e, l
    220         inc de
    221 
    222         ; copies BC bytes from HL into DE
    223 .memcpy_simple::
    224         ld a, c
    225         or b
    226         ret z
    227         ldir
    228         ret
    229 
    230         ; Wait for VBL interrupt to be finished
    231 .wait_vbl_done::
    232 _wait_vbl_done::
    233 _vsync::
    234         ld  a, (_shadow_VDP_R1)
    235         and #.R1_DISP_ON
    236         ret z
    237 
    238         xor a
    239         ld (.vbl_done), a
    240 1$:
    241         halt
    242         ld a, (.vbl_done)
    243         or a
    244         jr z, 1$
    245         ret
    246 
    247         .area   _DATA
    248 
    249 .start_crt_globals:
    250 __BIOS::
    251         .ds     0x01            ; GB type (GB, PGB, CGB)
    252 __SYSTEM::
    253         .ds     0x01            ; PAL/NTSC
    254 .end_crt_globals:
    255 
    256         .area _INITIALIZED
    257 .shadow_VDP:
    258 _shadow_VDP_R0::
    259         .ds     0x01
    260 _shadow_VDP_R1::
    261         .ds     0x01
    262 _shadow_VDP_R2::
    263         .ds     0x01
    264 _shadow_VDP_R3::
    265         .ds     0x01
    266 _shadow_VDP_R4::
    267         .ds     0x01
    268 _shadow_VDP_R5::
    269         .ds     0x01
    270 _shadow_VDP_R6::
    271         .ds     0x01
    272 _shadow_VDP_R7::
    273 _shadow_VDP_RBORDER::
    274         .ds     0x01
    275 _shadow_VDP_R8::
    276 _shadow_VDP_RSCX::
    277         .ds     0x01
    278 _shadow_VDP_R9::
    279 _shadow_VDP_RSCY::
    280         .ds     0x01
    281 _shadow_VDP_R10::
    282         .ds     0x01
    283 .shadow_VDP_end::
    284 
    285 .sys_time::
    286 _sys_time::
    287         .ds     0x02
    288 .vbl_done::
    289 __vbl_done::
    290         .ds     0x01
    291 _VDP_ATTR_SHIFT::
    292 .vdp_shift::
    293         .ds     0x01
    294 __shadow_OAM_base::
    295         .ds     0x01
    296 __shadow_OAM_OFF::
    297         .ds     0x01
    298 __sprites_OFF::
    299         .ds     0x01
    300 .mode::
    301         .ds     0x01            ; Current mode
    302 
    303         .area _INITIALIZER
    304 
    305         .db .R0_DEFAULT
    306         .db #(.R1_DEFAULT | .R1_DISP_ON | .R1_IE)       ; VBLANK
    307         .db .R2_MAP_0x1800                              ; .R2_MAP_0x3800
    308         .db 0xFF
    309         .db 0xFF
    310         .db .R5_SAT_0x1F00                              ; .R5_SAT_0x3F00
    311         .db .R6_DATA_0x2000
    312         .db #(0 | .R7_COLOR_MASK)
    313         .db 0                   ; SCX
    314         .db 0                   ; SCY
    315         .db .R10_INT_OFF
    316         .dw 0x0000              ; .sys_time
    317         .db 0                   ; .vbl_done
    318         .db 0                   ; _VDP_ATTR_SHIFT
    319         .db #>_shadow_OAM       ; __shadow_OAM_base
    320         .db 0                   ; __shadow_OAM_OFF
    321         .db 0                   ; __sprites_OFF
    322         .db .T_MODE_INOUT       ; .mode

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