!
! Bart's Custom Sega Saturn Start-Up Code
! Bart Trzynadlowski, 2001
! Modified by Anders Montonen, 2006
! Public domain
!
! For use with the GNU C Compiler. This code has been tested with gcc version
! 4.0-GNUSH_v0601.
!
! Make sure this is the first file linked into your project, so that it is at
! the very beginning. Use the BART.LINK linker script.

.section .text

!
! Entry point
!
.global start
start:
    !
    ! Clear BSS
    !
    mov.l   bss_start,r0
    mov.l   bss_end,r1
    mov     #0,r2
lbss:
    cmp/ge  r0,r1
    bt      lbss_end
    add     #1,r0
    mov.b   r2,@r0
    bra     lbss
lbss_end:

    !
    ! Jump to _main()
    !
    mov.l   main_ptr,r0
    jsr @r0
    nop

    !
    ! Once _main() has terminated, disable interrupts and loop infinitely
    !
    mov     #0xf,r0
    shll2   r0
    shll2   r0
    ldc     r0,sr
end:
    bra     end
    nop

.balign 4
main_ptr:   .long _main
bss_start:  .long __bss_start
bss_end:    .long __bss_end
