]> git.sur5r.net Git - cc65/blobdiff - libsrc/c128/crt0.s
Add handling of write requests
[cc65] / libsrc / c128 / crt0.s
index f10078d32cf672819a472d0bf06a5b9a8a92a9aa..cd104decd6d3b63e9184f491b0dfd87a326dbdf2 100644 (file)
 ;
 ; Startup code for cc65 (C128 version)
-;
-; This must be the *first* file on the linker command line
 ;
 
-       .export         _exit
-       .import         __hinit, initconio, doneconio, zerobss
-       .import         push0, doatexit, _main
+        .export         _exit
+        .export         __STARTUP__ : absolute = 1      ; Mark as startup
+        .import         initlib, donelib
+        .import         zerobss
+        .import         push0, callmain
+        .import         RESTOR, BSOUT, CLRCH
+        .import         __RAM_START__, __RAM_SIZE__, __STACKSIZE__
+        .importzp       ST
 
-       .include        "c128.inc"
-       .include        "../cbm/cbm.inc"
+        .include        "zeropage.inc"
+        .include        "c128.inc"
 
-; ------------------------------------------------------------------------
-; Define and export the ZP variables for the C64 runtime
-
-       .exportzp       sp, sreg, regsave
-       .exportzp       ptr1, ptr2, ptr3, ptr4
-       .exportzp       tmp1, tmp2, tmp3, tmp4
-       .exportzp       regbank, zpspace
-
-sp             =       $02             ; stack pointer
-sreg   =       $04             ; secondary register/high 16 bit for longs
-regsave        =       $06             ; slot to save/restore (E)AX into
-ptr1   =       $0A             ;
-ptr2   =       $0C
-ptr3   =       $0E
-ptr4   =       $10
-tmp1   =       $12
-tmp2   =       $13
-tmp3   =       $14
-tmp4   =       $15
-regbank        =       $16             ; 6 byte register bank
-zpspace        =       $1A             ; Zero page space allocated
 
 ; ------------------------------------------------------------------------
-; BASIC header with a SYS call
+; Startup code
 
-       .org    $1BFF
-        .word   Head            ; Load address
-Head:   .word   @Next
-        .word   1000            ; Line number
-        .byte   $9E,"7181"      ; SYS 7181
-        .byte   $00             ; End of BASIC line
-@Next:  .word   0               ; BASIC end marker
-       .reloc
+.segment        "STARTUP"
 
-; ------------------------------------------------------------------------
-; Actual code
+Start:
 
-       ldy     #zpspace-1
-L1:    lda     sp,y
-       sta     zpsave,y        ; save the zero page locations we need
-       dey
-               bpl     L1
+; Switch to the second charset
 
-; Close open files
+        lda     #14
+        jsr     BSOUT
 
-       jsr     CLRCH
+; Before doing anything else, we have to setup our banking configuration.
+; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
+; to the underlying RAM works, but it is bad style.
 
-; Switch to second charset
+        lda     MMU_CR          ; Get current memory configuration...
+        pha                     ; ...and save it for later
+        lda     #MMU_CFG_CC65   ; Bank0 with kernal ROM
+        sta     MMU_CR
 
-       lda     #14
-       jsr     BSOUT
+; Save the zero page locations we need
 
-; Get the current MMU setting and save it. Set new memory config.
-
-       lda     MMU_CR          ; Get current memory configuration...
-               pha                     ; ...and save it for later
-       lda     #$0E            ; Bank0 with kernal ROM
-       sta     MMU_CR
+        ldx     #zpspace-1
+L1:     lda     sp,x
+        sta     zpsave,x
+        dex
+        bpl     L1
 
 ; Clear the BSS data
 
-       jsr     zerobss
+        jsr     zerobss
 
 ; Save system stuff and setup the stack
 
-       pla                     ; Get MMU setting
-       sta     mmusave
+        pla                     ; Get MMU setting
+        sta     mmusave
 
-               tsx
-               stx     spsave          ; save system stk ptr
+        tsx
+        stx     spsave          ; Save the system stack pointer
 
-       lda     #<$C000
-       sta     sp
-       lda     #>$C000
-       sta     sp+1
+        lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+        sta     sp
+        lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+        sta     sp+1            ; Set argument stack ptr
 
-; Initialize the heap
+; Call module constructors
 
-       jsr     __hinit
+        jsr     initlib
 
-; Initialize conio stuff
+; Set the bank for the file name to our execution bank. We must do this,
+; *after* calling constructors, because some of them may depend on the
+; original value of this register.
 
-       jsr     initconio
+        lda     #0
+        sta     FNAM_BANK
 
-; Pass an empty command line
+; Push arguments and call main()
 
-       jsr     push0           ; argc
-       jsr     push0           ; argv
+        jsr     callmain
 
-       ldy     #4              ; Argument size
-               jsr     _main           ; call the users code
+; Back from main (this is also the _exit entry). Run module destructors
 
-; fall thru to exit...
+_exit:  pha                     ; Save the return code on stack
+        jsr     donelib
 
-_exit: jsr     doatexit        ; call exit functions
+; Copy back the zero page stuff
 
-; Reset the conio stuff
+        ldx     #zpspace-1
+L2:     lda     zpsave,x
+        sta     sp,x
+        dex
+        bpl     L2
 
-       jsr     doneconio
+; Place the program return code into ST
 
-; Reset stack and the MMU
+        pla
+        sta     ST
 
-       ldx     spsave          ; Patched at runtime
-       txs
-               lda     mmusave         ; Patched at runtime
-       sta     MMU_CR
+; Reset the stack and the memory configuration
 
-; Copy back the zero page stuff
+        ldx     spsave
+        txs
+        ldx     mmusave
+        stx     MMU_CR
 
-       ldy     #zpspace-1
-L2:    lda     zpsave,y
-       sta     sp,y
-       dey
-       bpl     L2
+; Done, return to BASIC
 
-; Done
+        rts
 
-       jmp     RESTOR
+; ------------------------------------------------------------------------
+; Data
 
-.data
-zpsave:        .res    zpspace
+.segment        "ZPSAVE"
 
-.bss
-spsave:        .res    1
-mmusave:.res   1
+zpsave: .res    zpspace
 
+; ------------------------------------------------------------------------
 
+.bss
 
+spsave: .res    1
+mmusave:.res    1