From 27d0f8bab1110e5067fb01c6a5d6207258c948ca Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Fri, 16 Jan 2015 13:36:30 +0000 Subject: [PATCH] 6502: Usermode execution start up code --- Library/libs/crt0_6502.s | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Library/libs/crt0_6502.s diff --git a/Library/libs/crt0_6502.s b/Library/libs/crt0_6502.s new file mode 100644 index 00000000..94db7cc2 --- /dev/null +++ b/Library/libs/crt0_6502.s @@ -0,0 +1,72 @@ +; +; Startup code for cc65 on FUZIX +; +; Based on code by Debrune Jérôme +; and Ullrich von Bassewitz 2014-08-22, Greg King +; + + .export _exit + .import initlib, donelib + .import __CODE_SIZE__, __RODATA_SIZE__ + .import __DATA_SIZE__, __BSS_SIZE__ + .import __exit + .import _environ + + .include "zeropage.inc" + +; Place the startup code in a special segment. + +.segment "STARTUP" + jmp start + + .byte 'F' + .byte 'Z' + .byte 'X' + .byte '1' + .byte $20 + .word 0 + .word __CODE_SIZE__ + __RODATA_SIZE__ + .word __DATA_SIZE__ + .word __BSS_SIZE__ + .word 0 + +; +; On entry sp/sp+1 have been set up by the kernel (this has to be +; done in kernel as we might take a signal very early on). Above the +; sp are the argument vectors, environment etc. In other words we +; are basically a straight function call from the kernel stub. +; +; Our image has been loaded into RAM, any spare memory has been zeroed +; for us and we are ready to roll. +; +start: +; Call the module constructors. + jsr initlib + +; Push the command-line arguments; and, call main(). +; +; Need to save the environment ptr. The rest of the stack should be +; fine. +; + lda _sp + ldx _sp+1 + clc + adc #4 + bcc l1 + inx +l1: sta _environ + stx _environ+1 + ldy #4 + jsr callmain + +; Call the module destructors. This is also the exit() entry. + +_exit: pha + txa + pha + jsr donelib ; Run module destructors + pla + tax + pla + jmp __exit ; exit syscall, AX holds our return code + ; for a fastcall return to nowhere. \ No newline at end of file -- 2.34.1