;
.export _exit
- .import __hinit, initconio, zerobss, push0, doatexit
+ .import initlib, donelib
+ .import zerobss, push0
.import _main
.include "pet.inc"
; ------------------------------------------------------------------------
; Actual code
- ldy #zpspace-1
-L1: lda sp,y
- sta zpsave,y ; Save the zero page locations we need
- dey
+ ldx #zpspace-1
+L1: lda sp,x
+ sta zpsave,x ; Save the zero page locations we need
+ dex
bpl L1
; Close open files
lda MEMSIZE+1
sta sp+1 ; Set argument stack ptr
-; Initialize the heap
+; Call module constructors
- jsr __hinit
-
-; Initialize conio stuff
-
- jsr initconio
+ jsr initlib
; Pass an empty command line
ldy #4 ; Argument size
jsr _main ; call the users code
-; fall thru to exit...
+; Call module destructors. This is also the _exit entry.
+
+_exit: jsr donelib ; Run module destructors
-_exit: jsr doatexit ; call exit functions
+; Restore system stuff
ldx spsave
txs ; Restore stack pointer
; Copy back the zero page stuff
- ldy #zpspace-1
-L2: lda zpsave,y
- sta sp,y
- dey
+ ldx #zpspace-1
+L2: lda zpsave,x
+ sta sp,x
+ dex
bpl L2
; Back to basic