crt0.s:
Code: Select all
;
; Startup code for cc65 (Vic20 version)
;
.export _exit
.export __STARTUP__ : absolute = 1 ; Mark as startup
.import initlib, donelib, callirq
.import zerobss, push0
.import _main
.import RESTOR, BSOUT, CLRCH
.import __INTERRUPTOR_COUNT__
.import __RAM_START__, __RAM_SIZE__ ; Linker generated
.import __DATA_LOAD__, __DATA_RUN__, __DATA_SIZE__
;.import _puts, _cgetc, _memcpy, pushax
.import _printscr, _getkey, _memcpy, pushax
.include "zeropage.inc"
.include "vic20.inc"
PLOT=$FFF0
; ------------------------------------------------------------------------
; Place the startup code in a special segment.
.segment "STARTUP"
.word $A000 ;Cartridge load address
.word Crt0_Init ;Address of start-up code
.word 0;Crt0_Start ;I don't know.
.byte $41,$30,$C3,$C2,$CD ;ROM present signature
;--------------------------------------------------------------------
;Startup code:
Crt0_Init:
sei
cld
ldx #$ff
txs
;Run kernal setup routines.
jsr $FD8D
jsr $FD52
jsr $FDF9
jsr $E518
cli
;Set up BASIC env.
jsr $E3A4
;Set up stack.
;Clear bss.
jsr zerobss
; Save system stuff and setup the stack
lda #<$400
sta sp
lda #>$400
sta sp+1 ; Set argument stack ptr
;Copy read/write data to RAM.
lda #<__DATA_RUN__
ldx #>__DATA_RUN__
jsr pushax
lda #<__DATA_LOAD__
ldx #>__DATA_LOAD__
jsr pushax
lda #<__DATA_SIZE__
ldx #>__DATA_SIZE__
;jsr pushax
jsr _memcpy
jsr CLRCH
; Switch to second charset
lda #14
jsr BSOUT
ldx #0
ldy #0
clc
jsr PLOT
;lda #65
;jsr $FFD2
; Clear the BSS data
jsr zerobss
;Call constructors.
jsr initlib
; If we have IRQ functions, chain our stub into the IRQ vector
lda #<__INTERRUPTOR_COUNT__
beq NoIRQ1
lda IRQVec
ldx IRQVec+1
sta IRQInd+1
stx IRQInd+2
lda #<IRQStub
ldx #>IRQStub
sei
sta IRQVec
stx IRQVec+1
cli
; Call module constructors
NoIRQ1: jsr initlib
; Push arguments and call main()
;jsr callmain
jsr push0
jsr push0
jsr _main
; Back from main (This is also the _exit entry). Run module destructors
_exit: jsr donelib
; Reset the IRQ vector if we chained it.
pha ; Save the return code on stack
lda #<__INTERRUPTOR_COUNT__
beq NoIRQ2
lda IRQInd+1
ldx IRQInd+2
sei
sta IRQVec
stx IRQVec+1
cli
NoIRQ2:
; Place the program return code into ST
pla
sta ST
;End and restart.
;Print end message.
lda #<EndMsg
ldx #>EndMsg
jsr _printscr
;Wait for key.
jsr _getkey
;Restart.
jmp ($FFFC) ;Kernal reset vector
; ------------------------------------------------------------------------
; The IRQ vector jumps here, if condes routines are defined with type 2.
IRQStub:
cld ; Just to be sure
jsr callirq ; Call the functions
jmp IRQInd ; Jump to the saved IRQ vector
; ------------------------------------------------------------------------
; Data
.data
IRQInd: jmp $0000
.rodata
;End message.
EndMsg:
.byt 147,"Your program ended.",13
.byt "Press any key to", 13
.byt "restart.",0
Code: Select all
SYMBOLS {
__STACKSIZE__: value = $200, type = weak; # 512-byte stack
}
MEMORY {
ZP: start = $0002, size = $001A, type = rw, define = yes;
#RAM: start = $0200, size = $0200, define = yes;
RAM: start = $1000, size = $0C00, define = yes;
ROM: start = $9FFE, size = $2002, define = yes, file = %O,
fill=yes;
}
SEGMENTS {
STARTUP: load = ROM, type = ro;
LOWCODE: load = ROM, type = ro, optional = yes;
INIT: load = ROM, type = ro, define = yes, optional = yes;
CODE: load = ROM, type = ro;
RODATA: load = ROM, type = ro;
DATA: load = ROM, run = RAM, type = rw, define = yes;
#ZPSAVE: load = RAM, type = bss;
BSS: load = RAM, type = bss, define = yes;
HEAP: load = RAM, type = bss, optional = yes; # must sit just below stack
ZEROPAGE: load = ZP, type = zp;
}
FEATURES {
CONDES: segment = INIT,
type = constructor,
label = __CONSTRUCTOR_TABLE__,
count = __CONSTRUCTOR_COUNT__;
CONDES: segment = RODATA,
type = destructor,
label = __DESTRUCTOR_TABLE__,
count = __DESTRUCTOR_COUNT__;
CONDES: segment = RODATA,
type = interruptor,
label = __INTERRUPTOR_TABLE__,
count = __INTERRUPTOR_COUNT__;
}