2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Startup code for cc65 (Plus/4 version)
|
|
|
|
;
|
|
|
|
; This must be the *first* file on the linker command line
|
|
|
|
;
|
|
|
|
|
2000-11-23 19:21:05 +00:00
|
|
|
.export _exit
|
2002-11-22 23:41:20 +00:00
|
|
|
.export brk_jmp
|
|
|
|
|
2004-04-04 14:28:57 +00:00
|
|
|
.import callirq_y, initlib, donelib
|
2009-02-22 18:14:54 +00:00
|
|
|
.import callmain, zerobss
|
2004-09-20 10:24:59 +00:00
|
|
|
.import __INTERRUPTOR_COUNT__
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-05-26 09:09:10 +00:00
|
|
|
.include "zeropage.inc"
|
2000-05-28 13:40:48 +00:00
|
|
|
.include "plus4.inc"
|
2002-11-19 23:02:47 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-11-22 23:41:20 +00:00
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Constants
|
|
|
|
|
|
|
|
IRQInd = $500 ; JMP $0000 - used as indirect IRQ vector
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
; ------------------------------------------------------------------------
|
2002-11-22 22:16:20 +00:00
|
|
|
; Place the startup code in a special segment to cope with the quirks of
|
|
|
|
; plus/4 banking.
|
2000-09-07 21:49:13 +00:00
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
.segment "STARTUP"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
.word Head ; Load address
|
|
|
|
Head: .word @Next
|
2004-02-02 13:34:12 +00:00
|
|
|
.word .version ; Line number
|
2000-05-28 13:40:48 +00:00
|
|
|
.byte $9E,"4109" ; SYS 4109
|
|
|
|
.byte $00 ; End of BASIC line
|
|
|
|
@Next: .word 0 ; BASIC end marker
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Actual code
|
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
sei ; No interrupts since we're banking out the ROM
|
|
|
|
sta ENABLE_RAM
|
2000-10-30 21:02:49 +00:00
|
|
|
ldx #zpspace-1
|
|
|
|
L1: lda sp,x
|
|
|
|
sta zpsave,x ; save the zero page locations we need
|
2003-12-14 17:40:57 +00:00
|
|
|
dex
|
2000-05-28 13:40:48 +00:00
|
|
|
bpl L1
|
2002-11-22 22:16:20 +00:00
|
|
|
sta ENABLE_ROM
|
|
|
|
cli
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Close open files
|
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
jsr $FFCC ; CLRCH
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Switch to second charset
|
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
lda #14
|
|
|
|
jsr $FFD2 ; BSOUT
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
; Save system stuff and setup the stack. The stack starts at the top of the
|
|
|
|
; usable RAM.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
tsx
|
2004-04-04 14:28:57 +00:00
|
|
|
stx spsave ; save system stk ptr
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
lda #<$FD00
|
|
|
|
sta sp
|
|
|
|
lda #>$FD00
|
|
|
|
sta sp+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
; Setup the IRQ vector in the banked RAM and switch off the ROM
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
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
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
; Clear the BSS data
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
jsr zerobss
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
; Initialize irqcount, which means that from now own custom linked in IRQ
|
|
|
|
; handlers (via condes) will be called.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-09-20 10:24:59 +00:00
|
|
|
lda #.lobyte(__INTERRUPTOR_COUNT__*2)
|
2003-12-14 17:40:57 +00:00
|
|
|
sta irqcount
|
2000-11-22 22:19:09 +00:00
|
|
|
|
2009-02-22 18:10:01 +00:00
|
|
|
; Call module constructors
|
|
|
|
|
|
|
|
jsr initlib
|
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
; Push arguments and call main()
|
|
|
|
|
|
|
|
jsr callmain
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
; Back from main (this is also the _exit entry). Run module destructors.
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2004-04-04 14:28:57 +00:00
|
|
|
_exit: pha ; Save the return code
|
2009-02-22 18:10:01 +00:00
|
|
|
jsr donelib ; Run module destructors
|
|
|
|
|
|
|
|
; Disable chained IRQ handlers
|
|
|
|
|
2004-03-02 17:08:07 +00:00
|
|
|
lda #0
|
2003-12-14 17:40:57 +00:00
|
|
|
sta irqcount ; Disable custom IRQ handlers
|
2000-11-22 22:19:09 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
; Copy back the zero page stuff
|
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
ldx #zpspace-1
|
2000-10-30 21:02:49 +00:00
|
|
|
L2: lda zpsave,x
|
2003-12-14 17:40:57 +00:00
|
|
|
sta sp,x
|
|
|
|
dex
|
2000-05-28 13:40:48 +00:00
|
|
|
bpl L2
|
|
|
|
|
2004-03-02 17:08:07 +00:00
|
|
|
; Place the program return code into ST
|
|
|
|
|
|
|
|
pla
|
|
|
|
sta ST
|
|
|
|
|
2004-03-08 20:38:58 +00:00
|
|
|
; Restore the stack pointer
|
|
|
|
|
|
|
|
ldx spsave
|
|
|
|
txs
|
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
; Enable the ROM, reset changed vectors and return to BASIC
|
|
|
|
|
|
|
|
sta ENABLE_ROM
|
2003-12-14 17:40:57 +00:00
|
|
|
jmp $FF8A ; RESTOR
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
; ------------------------------------------------------------------------
|
2003-12-14 17:40:57 +00:00
|
|
|
; 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.
|
2002-11-22 22:16:20 +00:00
|
|
|
|
|
|
|
.segment "LOWCODE"
|
|
|
|
|
2003-12-14 17:40:57 +00:00
|
|
|
IRQ: cld ; Just to be sure
|
|
|
|
pha
|
2002-11-22 22:16:20 +00:00
|
|
|
txa
|
|
|
|
pha
|
2003-12-14 17:40:57 +00:00
|
|
|
tya
|
|
|
|
pha
|
2002-11-22 22:16:20 +00:00
|
|
|
tsx ; Get the stack pointer
|
2003-12-14 17:40:57 +00:00
|
|
|
lda $0104,x ; Get the saved status register
|
2002-11-22 23:41:20 +00:00
|
|
|
and #$10 ; Test for BRK bit
|
|
|
|
bne dobreak
|
2003-12-14 17:40:57 +00:00
|
|
|
|
|
|
|
; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
|
2008-07-03 19:39:14 +00:00
|
|
|
; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
|
|
|
|
; condes function is not reentrant. The irqcount flag will be set/reset from
|
2004-09-20 10:24:59 +00:00
|
|
|
; the main code, to avoid races.
|
2003-12-14 17:40:57 +00:00
|
|
|
|
|
|
|
ldy irqcount
|
|
|
|
beq @L1
|
2004-04-04 14:28:57 +00:00
|
|
|
jsr callirq_y ; Call the IRQ functions
|
2003-12-14 17:40:57 +00:00
|
|
|
|
|
|
|
; Since the ROM handler will end with an RTI, we have to fake an IRQ return
|
|
|
|
; on stack, so we get control of the CPU after the ROM handler and can switch
|
|
|
|
; back to RAM.
|
|
|
|
|
|
|
|
@L1: lda #>irq_ret ; Push new return address
|
2002-11-22 22:16:20 +00:00
|
|
|
pha
|
2002-11-22 23:41:20 +00:00
|
|
|
lda #<irq_ret
|
2002-11-22 22:16:20 +00:00
|
|
|
pha
|
2004-04-04 14:28:57 +00:00
|
|
|
php ; Push faked IRQ frame on stack
|
|
|
|
pha ; Push faked A register
|
|
|
|
pha ; Push faked X register
|
|
|
|
pha ; Push faked Y register
|
2002-11-22 22:16:20 +00:00
|
|
|
sta ENABLE_ROM ; Switch to ROM
|
2003-12-14 17:40:57 +00:00
|
|
|
jmp (IRQVec) ; Jump indirect to kernal irq handler
|
2002-11-22 22:16:20 +00:00
|
|
|
|
2002-11-22 23:41:20 +00:00
|
|
|
irq_ret:
|
|
|
|
sta ENABLE_RAM ; Switch back to RAM
|
2003-12-14 17:40:57 +00:00
|
|
|
pla
|
|
|
|
tay
|
2002-11-22 22:16:20 +00:00
|
|
|
pla
|
|
|
|
tax
|
|
|
|
pla
|
|
|
|
rti
|
|
|
|
|
2002-11-22 23:41:20 +00:00
|
|
|
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
|
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
.data
|
2002-11-22 23:41:20 +00:00
|
|
|
|
|
|
|
; BRK handling
|
|
|
|
brk_jmp: jmp $0000
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-11-22 23:41:20 +00:00
|
|
|
spsave: .res 1
|
|
|
|
|
2009-02-22 18:04:25 +00:00
|
|
|
irqcount: .byte 0
|
|
|
|
|
2008-07-03 19:39:14 +00:00
|
|
|
.segment "ZPSAVE"
|
|
|
|
|
|
|
|
zpsave: .res zpspace
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|