1
0
mirror of https://github.com/cc65/cc65.git synced 2024-07-17 09:29:10 +00:00
cc65/libsrc/c64/crt0.s
Oliver Schmidt 3d08abcfa8 Load INITBSS segment from disk.
Conceptually the INITBSS segment is not initialized in any way. Therefore it makes sense to not load it from disk. However the INIT segment has to be loaded from disk and therefore moved to its run location above the INITBSS segment. The necessary move routine increases runtime RAM usage :-(

Therefore we now "unnecessarily" load the INITBSS segment from disk too meaning that the INIT segment is loaded at its run location. Therefore there's no need for the move routine anymore.

After all we trade disk space for (runtime) RAM space - an easy decision ;-)

Notes:

- The code allowing to re-run a program without re-load present so far could not have worked as far as I can see as it only avoided to re-run the move routine but still tried to re-run the code in the INIT segment that was clobbered by zeroing the BSS. Therefore I removed the code in question altogether. I'm personally not into this "dirty re-run" but if someone wants to add an actually working solution I won't block that.

- INITBSS is intentionally not just merged with the DATA segment as ROM-based targets can't reuse the INIT segment for the BSS and therefore have no reason to place the INIT segment above INITBSS.

- Because ROM-based targets don't copy INITBSS from the ROM (like it is done with the DATA segment) all users of INITBSS _MUST_NOT_ presume INITBSS to be initialized with zeros!
2016-02-28 19:45:36 +01:00

119 lines
2.6 KiB
ArmAsm

;
; Startup code for cc65 (C64 version)
;
.export _exit
.export __STARTUP__ : absolute = 1 ; Mark as startup
.import initlib, donelib
.import zerobss, callmain
.import BSOUT
.import __MAIN_START__, __MAIN_SIZE__ ; Linker generated
.import __STACKSIZE__ ; from configure file
.importzp ST
.include "zeropage.inc"
.include "c64.inc"
; ------------------------------------------------------------------------
; Startup code
.segment "STARTUP"
Start:
; Switch off the BASIC ROM.
lda $01
sta mmusave ; Save the memory configuration
and #$F8
ora #$06 ; Enable Kernal+I/O, disable BASIC
sta $01
tsx
stx spsave ; Save the system stack ptr
; Save space by putting some of the start-up code in the INIT segment,
; which can be re-used by the BSS segment, the heap and the C stack.
jsr init
; Clear the BSS data.
jsr zerobss
; Push the command-line arguments; and, call main().
jsr callmain
; Back from main() [this is also the exit() entry]. Run the module destructors.
_exit: pha ; Save the return code on stack
jsr donelib
; Copy back the zero-page stuff.
ldx #zpspace-1
L2: lda zpsave,x
sta sp,x
dex
bpl L2
; Place the program return code into BASIC's status variable.
pla
sta ST
; Restore the system stuff.
ldx spsave
txs ; Restore stack pointer
ldx mmusave
stx $01 ; Restore memory configuration
; Back to BASIC.
rts
; ------------------------------------------------------------------------
.segment "INIT"
init:
; Save the zero-page locations that we need.
ldx #zpspace-1
L1: lda sp,x
sta zpsave,x
dex
bpl L1
; Set up the stack.
lda #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
ldx #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
sta sp
stx sp+1 ; Set argument stack ptr
; Switch to the second charset.
lda #14
jsr BSOUT
; Call the module constructors.
jmp initlib
; ------------------------------------------------------------------------
; Data
.segment "INITBSS"
mmusave:.res 1
spsave: .res 1
zpsave: .res zpspace