        CASE ON         ; Enable case sensitivity for labels

;   Imported addresses
        IMPORT  main

;   Forward addresses and entries
        ;ENTRY   G3958_
        ;ENTRY   G4048_
        ;ENTRY   _ownerid


;        EXPORT	~globals
        SEG '~globals'
;        SEGATTR RELOAD                          ; Sets bit 10 in KIND Should be removed for G14ED
~globals RECORD ENTRY                             ; ENTRY sets bit 14 in KIND. Should be removed for G14ED
        EXPORT	_ownerid
_ownerid
        DS.B 2
        EXPORT	_toolErr
_toolErr
        DS.B 2
        EXPORT	G3958_
G3958_
        DS.B 2
        EXPORT	G4048_
G4048_
        DS.B 2
        ENDR

        EXPORT	Loader
        SEG 'main'
Loader  PROC
; When the GS/OS operating system loader gives control to this program entry point, these registers contain:
; A: userID assigned to the program (assigned by the operating system loader)
; D: address of the first (lowest-address) byte in the direct-page / stack space (assigned by the operating system loader)
; S: address of the Iast (highest-address) byte in the direet-page / stack space (assigned by the operating system loader)
        PHA                                     ; Save A to stack (contains the ownerid value assigned by the operating system loader)
; Set data bank register to the address of the data segment
        SEP        #$30
        LONGA OFF
        LONGI OFF
        LDA        #^_ownerid                   ; Most significant byte of address of the first variable in the data segment = Address of the data segment
        PHA                                     ; Push Accumulator
        PLB                                     ; Pull data bank register
        REP        #$30
        LONGA ON
        LONGI ON
; Store the register values assigned by the operating system loader in the data segment
        PLA                                     ; Restore A from stack (restore ownerid value assigned by the operating system loader)
        STA        |_ownerid                    ; Backup Accumulator in data bank at offset 0
        TDC                                     ; Transfer Direct Page register to 16-bit Accumulator
        STA        |G3958_                      ; Backup Direct Page register value in data bank at offset 4
        TSC                                     ; Transfer Stack Pointer to 16-bit Accumulator
        STA        |G4048_                      ; Backup Stack Pointer value in data bank at offset 6
        JMP        >main                        ; Jump to main
        ENDP

        END
