2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Startup code for cc65 (Plus/4 version)
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _exit
|
2002-11-22 23:41:20 +00:00
|
|
|
.export brk_jmp
|
2009-07-31 12:05:42 +00:00
|
|
|
.export __STARTUP__ : absolute = 1 ; Mark as startup
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.import callirq_y, initlib, donelib
|
|
|
|
.import callmain, zerobss
|
|
|
|
.import __INTERRUPTOR_COUNT__
|
2016-03-07 00:28:55 +00:00
|
|
|
.import __MAIN_START__, __MAIN_SIZE__ ; Linker generated
|
2013-05-09 11:56:54 +00:00
|
|
|
.import __STACKSIZE__ ; Linker generated
|
|
|
|
.importzp ST
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-05-26 09:09:10 +00:00
|
|
|
.include "zeropage.inc"
|
2013-05-09 11:56:54 +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
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
IRQInd = $500 ; JMP $0000 - used as indirect IRQ vector
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
; ------------------------------------------------------------------------
|
2009-12-09 12:42:24 +00:00
|
|
|
; Startup code
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.segment "STARTUP"
|
2009-12-09 12:42:24 +00:00
|
|
|
|
|
|
|
Start:
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Save the zero-page locations that we need.
|
2009-12-09 12:42:24 +00:00
|
|
|
|
|
|
|
sei ; No interrupts since we're banking out the ROM
|
2002-11-22 22:16:20 +00:00
|
|
|
sta ENABLE_RAM
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #zpspace-1
|
|
|
|
L1: lda sp,x
|
|
|
|
sta zpsave,x
|
|
|
|
dex
|
|
|
|
bpl L1
|
2002-11-22 22:16:20 +00:00
|
|
|
sta ENABLE_ROM
|
|
|
|
cli
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Switch to the second charset.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #14
|
|
|
|
jsr $FFD2 ; BSOUT
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Save some system stuff; and, set up the stack. The stack starts at the top
|
|
|
|
; of the usable RAM.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
tsx
|
2016-03-07 00:28:55 +00:00
|
|
|
stx spsave ; Save system stk ptr
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2016-03-07 00:28:55 +00:00
|
|
|
lda #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
|
|
|
|
ldx #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
|
2002-11-22 22:16:20 +00:00
|
|
|
sta sp
|
2016-03-07 00:28:55 +00:00
|
|
|
stx sp+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Set up 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
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Clear the BSS data.
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr zerobss
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Initialize irqcount, which means that, from now on, custom linked-in IRQ
|
|
|
|
; handlers will be called (via condes).
|
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
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Call the module constructors.
|
2009-02-22 18:10:01 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr initlib
|
2009-02-22 18:10:01 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Push the command-line arguments; and, call main().
|
2003-12-14 17:40:57 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr callmain
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Back from main() [this is also the exit() entry]. Run the module destructors.
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
_exit: pha ; Save the return code
|
|
|
|
jsr donelib ; Run module destructors
|
2009-02-22 18:10:01 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Disable the chained IRQ handlers.
|
2009-02-22 18:10:01 +00:00
|
|
|
|
2013-05-09 11:56:54 +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
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Copy back the zero-page stuff.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #zpspace-1
|
|
|
|
L2: lda zpsave,x
|
|
|
|
sta sp,x
|
|
|
|
dex
|
|
|
|
bpl L2
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Place the program return code into BASIC's status variable.
|
2004-03-02 17:08:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
pla
|
|
|
|
sta ST
|
2004-03-02 17:08:07 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Restore the stack pointer.
|
2004-03-08 20:38:58 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx spsave
|
|
|
|
txs
|
2004-03-08 20:38:58 +00:00
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; Enable the ROM; and, return to BASIC.
|
2002-11-22 22:16:20 +00:00
|
|
|
|
|
|
|
sta ENABLE_ROM
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-11-22 22:16:20 +00:00
|
|
|
; ------------------------------------------------------------------------
|
2014-08-23 18:05:36 +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
|
2003-12-14 17:40:57 +00:00
|
|
|
; indirect vectors at $314/$316.
|
|
|
|
; To make our stub as fast as possible, we skip the whole part of the ROM
|
2014-08-23 18:05:36 +00:00
|
|
|
; 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"
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
IRQ: cld ; Just to be sure
|
|
|
|
pha
|
2002-11-22 22:16:20 +00:00
|
|
|
txa
|
|
|
|
pha
|
2013-05-09 11:56:54 +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
|
|
|
|
2014-08-23 18:05:36 +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
|
|
|
|
2013-05-09 11:56:54 +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
|
2014-08-23 18:05:36 +00:00
|
|
|
; on the stack, so that we get control of the CPU after the ROM handler,
|
|
|
|
; and can switch back to RAM.
|
2003-12-14 17:40:57 +00:00
|
|
|
|
|
|
|
@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
|
2013-05-09 11:56:54 +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
|
2014-08-23 18:05:36 +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
|
2013-05-09 11:56:54 +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
|
|
|
|
|
2014-08-23 18:05:36 +00:00
|
|
|
; No break handler installed, jump to ROM.
|
2002-11-22 23:41:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
spsave: .res 1
|
2002-11-22 23:41:20 +00:00
|
|
|
|
2009-02-22 18:04:25 +00:00
|
|
|
irqcount: .byte 0
|
|
|
|
|
2016-03-06 20:26:22 +00:00
|
|
|
.segment "INIT"
|
2008-07-03 19:39:14 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
zpsave: .res zpspace
|
2008-07-03 19:39:14 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|