]> git.sur5r.net Git - cc65/blobdiff - libsrc/plus4/crt0.s
Renamed RAM to MAIN for all disk based targets.
[cc65] / libsrc / plus4 / crt0.s
index b33ff667a285bcb33d1288b8a5e58ac41ecb7f1c..2262b4c4245a26e446bc92533e06fa73fe1c7d6b 100644 (file)
 ;
 ; Startup code for cc65 (Plus/4 version)
 ;
-; This must be the *first* file on the linker command line
-;
 
-       .export         _exit
-       .import         __hinit, push0, doatexit, _main
-       .import         initconio, doneconio, zerobss
+        .export         _exit
+        .export         brk_jmp
+        .export         __STARTUP__ : absolute = 1      ; Mark as startup
+
+        .import         callirq_y, initlib, donelib
+        .import         callmain, zerobss
+        .import         __INTERRUPTOR_COUNT__
+        .import         __MAIN_START__, __MAIN_SIZE__   ; Linker generated
+        .import         __STACKSIZE__                   ; Linker generated
+        .importzp       ST
+
+        .include        "zeropage.inc"
+        .include        "plus4.inc"
 
-       .include        "plus4.inc"
-       .include        "../cbm/cbm.inc"
 
 ; ------------------------------------------------------------------------
-; Define and export the ZP variables for the C64 runtime
+; Constants
 
-       .exportzp       sp, sreg, regsave
-       .exportzp       ptr1, ptr2, ptr3, ptr4
-       .exportzp       tmp1, tmp2, tmp3, tmp4
-       .exportzp       regbank, zpspace
+IRQInd          = $500  ; JMP $0000 - used as indirect IRQ vector
 
-.zeropage
+; ------------------------------------------------------------------------
+; Startup code
 
-zpstart        = *
-sp:            .res    2       ; Stack pointer
-sreg:          .res    2       ; Secondary register/high 16 bit for longs
-regsave:       .res    2       ; slot to save/restore (E)AX into
-ptr1:          .res    2
-ptr2:          .res    2
-ptr3:          .res    2
-ptr4:          .res    2
-tmp1:          .res    1
-tmp2:          .res    1
-tmp3:          .res    1
-tmp4:          .res    1
-regbank:       .res    6       ; 6 byte register bank
+.segment        "STARTUP"
 
-zpspace        = * - zpstart           ; Zero page space allocated
+Start:
 
-.code
+; Save the zero-page locations that we need.
 
-; ------------------------------------------------------------------------
-; BASIC header with a SYS call
+        sei                     ; No interrupts since we're banking out the ROM
+        sta     ENABLE_RAM
+        ldx     #zpspace-1
+L1:     lda     sp,x
+        sta     zpsave,x
+        dex
+        bpl     L1
+        sta     ENABLE_ROM
+        cli
 
-       .org    $0FFF
-        .word   Head            ; Load address
-Head:   .word   @Next
-        .word   1000            ; Line number
-        .byte   $9E,"4109"     ; SYS 4109
-        .byte   $00             ; End of BASIC line
-@Next:  .word   0               ; BASIC end marker
-       .reloc
+; Switch to the second charset.
 
-; ------------------------------------------------------------------------
-; Actual code
+        lda     #14
+        jsr     $FFD2           ; BSOUT
 
-               ldx     #zpspace-1
-L1:    lda     sp,x
-       sta     zpsave,x        ; save the zero page locations we need
-       dex
-               bpl     L1
+; Save some system stuff; and, set up the stack. The stack starts at the top
+; of the usable RAM.
 
-; Close open files
+        tsx
+        stx     spsave          ; Save system stk ptr
 
-       jsr     CLRCH
+        lda     #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+        ldx     #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+        sta     sp
+        stx     sp+1
 
-; Switch to second charset
+; Set up the IRQ vector in the banked RAM; and, switch off the ROM.
 
-       lda     #14
-       jsr     BSOUT
+        ldx     #<IRQ
+        ldy     #>IRQ
+        sei                     ; No ints, handler not yet in place
+        sta     ENABLE_RAM
+        stx     $FFFE           ; Install interrupt handler
+        sty     $FFFF
+        cli                     ; Allow interrupts
 
-; Clear the BSS data
+; Clear the BSS data.
 
-       jsr     zerobss
+        jsr     zerobss
 
-; Save system stuff and setup the stack
+; Initialize irqcount, which means that, from now on, custom linked-in IRQ
+; handlers will be called (via condes).
 
-               tsx
-               stx     spsave          ; save system stk ptr
+        lda     #.lobyte(__INTERRUPTOR_COUNT__*2)
+        sta     irqcount
 
-       sec
-       jsr     MEMTOP          ; Get top memory
-       cpy     #$80            ; We can only use the low 32K :-(
-       bcc     MemOk
-       ldy     #$80
-       ldx     #$00
-MemOk: stx     sp
-       sty     sp+1            ; set argument stack ptr
+; Call the module constructors.
 
-; Initialize the heap
+        jsr     initlib
 
-       jsr     __hinit
+; Push the command-line arguments; and, call main().
 
-; Initialize conio stuff
+        jsr     callmain
 
-       jsr     initconio
+; Back from main() [this is also the exit() entry]. Run the module destructors.
 
-; Pass an empty command line
+_exit:  pha                     ; Save the return code
+        jsr     donelib         ; Run module destructors
 
-               jsr     push0           ; argc
-       jsr     push0           ; argv
+; Disable the chained IRQ handlers.
 
-       ldy     #4              ; Argument size
-               jsr     _main           ; call the users code
+        lda     #0
+        sta     irqcount        ; Disable custom IRQ handlers
 
-; fall thru to exit...
+; Copy back the zero-page stuff.
 
-_exit: jsr     doatexit        ; call exit functions
-       ldx     spsave
-       txs
+        ldx     #zpspace-1
+L2:     lda     zpsave,x
+        sta     sp,x
+        dex
+        bpl     L2
 
-; Reset the conio stuff
+; Place the program return code into BASIC's status variable.
 
-       jsr     doneconio
+        pla
+        sta     ST
 
-; Copy back the zero page stuff
+; Restore the stack pointer.
 
-       ldx     #zpspace-1
-L2:    lda     zpsave,x
-       sta     sp,x
-       dex
-               bpl     L2
+        ldx     spsave
+        txs
 
-; Reset changed vectors
+; Enable the ROM; and, return to BASIC.
 
-       jmp     RESTOR
+        sta     ENABLE_ROM
+        rts
 
+; ------------------------------------------------------------------------
+; IRQ handler. The handler in the ROM enables the Kernal, and jumps to
+; $CE00, where the ROM code checks for a BRK or IRQ, and branches via the
+; indirect vectors at $314/$316.
+; To make our stub as fast as possible, we skip the whole part of the ROM
+; handler, and jump to the indirect vectors directly. We do also call our
+; own interrupt handlers if we have any; so, they need not use $314.
+
+.segment        "LOWCODE"
+
+IRQ:    cld                     ; Just to be sure
+        pha
+        txa
+        pha
+        tya
+        pha
+        tsx                     ; Get the stack pointer
+        lda     $0104,x         ; Get the saved status register
+        and     #$10            ; Test for BRK bit
+        bne     dobreak
+
+; It's an IRQ; and, RAM is enabled. If we have handlers, call them. We will use
+; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
+; condes function is not reentrant. The irqcount flag will be set/reset from
+; the main code, to avoid races.
+
+        ldy     irqcount
+        beq     @L1
+        jsr     callirq_y       ; Call the IRQ functions
+
+; Since the ROM handler will end with an RTI, we have to fake an IRQ return
+; on the stack, so that we get control of the CPU after the ROM handler,
+; and can switch back to RAM.
+
+@L1:    lda     #>irq_ret       ; Push new return address
+        pha
+        lda     #<irq_ret
+        pha
+        php                     ; Push faked IRQ frame on stack
+        pha                     ; Push faked A register
+        pha                     ; Push faked X register
+        pha                     ; Push faked Y register
+        sta     ENABLE_ROM      ; Switch to ROM
+        jmp     (IRQVec)        ; Jump indirect to Kernal IRQ handler
+
+irq_ret:
+        sta     ENABLE_RAM      ; Switch back to RAM
+        pla
+        tay
+        pla
+        tax
+        pla
+        rti
+
+dobreak:
+        lda     brk_jmp+2       ; Check high byte of address
+        beq     nohandler
+        jmp     brk_jmp         ; Jump to the handler
+
+; No break handler installed, jump to ROM.
+
+nohandler:
+        sta     ENABLE_ROM
+        jmp     (BRKVec)        ; Jump indirect to the break vector
+
+; ------------------------------------------------------------------------
+; Data
 
 .data
-zpsave:        .res    zpspace
 
-.bss
-spsave:        .res    1
+; BRK handling
+brk_jmp:        jmp     $0000
+
+spsave:         .res    1
+
+irqcount:       .byte   0
+
+.segment        "INIT"
+
+zpsave:         .res    zpspace