2011-02-06 22:27:31 +00:00
|
|
|
;
|
|
|
|
; Oliver Schmidt, 2011-01-26
|
|
|
|
;
|
|
|
|
; int __fastcall__ exec (const char* progname, const char* cmdline);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _exec
|
|
|
|
.import pushname, popname
|
|
|
|
.import popax, done, _exit
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.include "zeropage.inc"
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "apple2.inc"
|
|
|
|
.include "mli.inc"
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Wrong file type
|
2013-05-09 11:56:54 +00:00
|
|
|
typerr: lda #$4A ; "Incompatible file format"
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Cleanup name
|
2013-05-09 11:56:54 +00:00
|
|
|
oserr: jsr popname ; Preserves A
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Set __oserror
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp __mappederrno
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
_exec:
|
2015-04-26 12:01:36 +00:00
|
|
|
; Save cmdline
|
|
|
|
sta ptr4
|
|
|
|
stx ptr4+1
|
|
|
|
|
2011-02-06 22:27:31 +00:00
|
|
|
; Get and push name
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax
|
|
|
|
jsr pushname
|
|
|
|
bne oserr
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2015-04-19 19:59:05 +00:00
|
|
|
; ProDOS TechRefMan, chapter 5.1.5.1:
|
|
|
|
; "The complete or partial pathname of the system program
|
|
|
|
; is stored at $280, starting with a length byte."
|
|
|
|
; In fact BASIC.SYSTEM does the same for BLOAD and BRUN of
|
|
|
|
; binary programs so we should do the same too in any case
|
2015-04-26 12:01:36 +00:00
|
|
|
; especially as _we_ rely on it in mainargs.s for argv[0]
|
2015-04-19 19:59:05 +00:00
|
|
|
ldy #$00
|
|
|
|
lda (sp),y
|
|
|
|
tay
|
|
|
|
: lda (sp),y
|
|
|
|
sta $0280,y
|
|
|
|
dey
|
|
|
|
bpl :-
|
|
|
|
|
2011-02-06 22:27:31 +00:00
|
|
|
; Set pushed name
|
2013-05-09 11:56:54 +00:00
|
|
|
lda sp
|
|
|
|
ldx sp+1
|
|
|
|
sta mliparam + MLI::INFO::PATHNAME
|
|
|
|
stx mliparam + MLI::INFO::PATHNAME+1
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Get file_type and aux_type
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #GET_INFO_CALL
|
|
|
|
ldx #GET_INFO_COUNT
|
|
|
|
jsr callmli
|
|
|
|
bcs oserr
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; If we get here the program file at least exists so we copy
|
|
|
|
; the loader stub right now and patch it later to set params
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #size - 1
|
|
|
|
: lda source,x
|
|
|
|
sta target,x
|
2011-02-06 22:27:31 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl :-
|
2011-02-11 23:22:26 +00:00
|
|
|
|
2011-02-06 22:27:31 +00:00
|
|
|
; Check program file type
|
2013-05-09 11:56:54 +00:00
|
|
|
lda mliparam + MLI::INFO::FILE_TYPE
|
|
|
|
cmp #$FF ; SYS file?
|
|
|
|
bne binary ; No, check for BIN file
|
2015-04-26 12:01:36 +00:00
|
|
|
sta file_type ; Save file type for cmdline handling
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; SYS programs replace BASIC.SYSTEM so set in the ProDOS system bit map
|
|
|
|
; protection for pages $80 - $BF just in case BASIC.SYSTEM is there now
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #$0F ; Start with protection for pages $B8 - $BF
|
|
|
|
lda #%00000001 ; Protect only system global page
|
|
|
|
: sta $BF60,x ; Set protection for 8 pages
|
2015-04-26 12:01:36 +00:00
|
|
|
lda #%00000000 ; Protect no page
|
2011-02-06 22:27:31 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl :-
|
|
|
|
bmi prodos ; Branch always
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
binary: cmp #$06 ; BIN file?
|
|
|
|
bne typerr ; No, wrong file type
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Set BIN program load addr
|
2013-05-09 11:56:54 +00:00
|
|
|
lda mliparam + MLI::INFO::AUX_TYPE
|
|
|
|
ldx mliparam + MLI::INFO::AUX_TYPE+1
|
|
|
|
sta data_buffer
|
|
|
|
stx data_buffer+1
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Check ProDOS system bit map for presence of BASIC.SYSTEM
|
2013-05-09 11:56:54 +00:00
|
|
|
lda $BF6F ; Protection for pages $B8 - $BF
|
|
|
|
cmp #%00000001 ; Exactly system global page is protected
|
|
|
|
beq setvec
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Get highest available mem addr from BASIC.SYSTEM
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx HIMEM+1 ; High byte
|
|
|
|
bne setbuf ; Branch always
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; BIN programs are supposed to quit through one of the two DOS
|
|
|
|
; vectors so we set up those to point to the ProDOS dispatcher
|
2013-05-09 11:56:54 +00:00
|
|
|
setvec: ldx #$03 - 1 ; Size of JMP opcode
|
|
|
|
: lda dosvec,x
|
|
|
|
sta DOSWARM,x ; DOS warm start
|
|
|
|
sta DOSWARM + 3,x ; DOS cold start
|
2011-02-06 22:27:31 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl :-
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; No BASIC.SYSTEM so use addr of ProDOS system global page
|
2013-05-09 11:56:54 +00:00
|
|
|
prodos: ldx #>$BF00 ; High byte
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; The I/O buffer needs to be page aligned
|
2013-05-09 11:56:54 +00:00
|
|
|
setbuf: lda #$00 ; Low byte
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; The I/O buffer needs four pages
|
|
|
|
dex
|
|
|
|
dex
|
|
|
|
dex
|
|
|
|
dex
|
2015-04-26 12:01:36 +00:00
|
|
|
|
2011-02-06 22:27:31 +00:00
|
|
|
; Set I/O buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
sta mliparam + MLI::OPEN::IO_BUFFER
|
|
|
|
stx mliparam + MLI::OPEN::IO_BUFFER+1
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; PATHNAME already set
|
|
|
|
.assert MLI::OPEN::PATHNAME = MLI::INFO::PATHNAME, error
|
|
|
|
|
|
|
|
; Lower file level to avoid program file
|
2022-02-21 20:44:31 +00:00
|
|
|
; being closed by C library shutdown code
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx LEVEL
|
|
|
|
stx level
|
|
|
|
beq :+
|
|
|
|
dec LEVEL
|
2015-04-26 12:01:36 +00:00
|
|
|
|
2011-02-06 22:27:31 +00:00
|
|
|
; Open file
|
2013-05-09 11:56:54 +00:00
|
|
|
: lda #OPEN_CALL
|
|
|
|
ldx #OPEN_COUNT
|
|
|
|
jsr callmli
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Restore file level
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx level
|
|
|
|
stx LEVEL
|
|
|
|
bcc :+
|
|
|
|
jmp oserr
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Get and save fd
|
2013-05-09 11:56:54 +00:00
|
|
|
: lda mliparam + MLI::OPEN::REF_NUM
|
|
|
|
sta read_ref
|
|
|
|
sta close_ref
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2011-02-11 23:22:26 +00:00
|
|
|
.ifdef __APPLE2ENH__
|
|
|
|
; Calling the 80 column firmware needs the ROM switched
|
|
|
|
; in, otherwise it copies the F8 ROM to the LC (@ $CEF4)
|
2013-05-09 11:56:54 +00:00
|
|
|
bit $C082
|
2011-02-11 23:22:26 +00:00
|
|
|
|
|
|
|
; ProDOS TechRefMan, chapter 5.3.1.3:
|
|
|
|
; "80-column text cards -- and other Apple IIe features -- can
|
|
|
|
; be turned off using the following sequence of instructions:"
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #$15
|
|
|
|
jsr $C300
|
2011-02-11 23:22:26 +00:00
|
|
|
|
|
|
|
; Switch in LC bank 2 for R/O
|
2013-05-09 11:56:54 +00:00
|
|
|
bit $C080
|
2011-02-11 23:22:26 +00:00
|
|
|
.endif
|
|
|
|
|
2015-04-27 07:39:50 +00:00
|
|
|
; Reset stack as we already passed
|
2015-04-26 12:01:36 +00:00
|
|
|
; the point of no return anyway
|
|
|
|
ldx #$FF
|
|
|
|
txs
|
|
|
|
|
|
|
|
; Store up to 127 chars of cmdline (if any)
|
|
|
|
; including terminating zero in stack page
|
|
|
|
ldy #$00
|
|
|
|
lda ptr4+1 ; NULL?
|
|
|
|
beq :++ ; Yes, store as '\0'
|
|
|
|
: lda (ptr4),y
|
|
|
|
: sta $0100,y
|
|
|
|
beq :+ ; '\0' stored, done
|
|
|
|
iny
|
|
|
|
cpy #$7E
|
|
|
|
bcc :--
|
|
|
|
lda #$00 ; '\0'
|
|
|
|
beq :- ; Branch always
|
|
|
|
|
2022-02-21 20:44:31 +00:00
|
|
|
; Call loader stub after C library shutdown
|
2015-04-26 12:01:36 +00:00
|
|
|
: lda #<target
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #>target
|
2014-01-26 17:10:01 +00:00
|
|
|
sta done+1
|
|
|
|
stx done+2
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2022-02-21 20:44:31 +00:00
|
|
|
; Initiate C library shutdown
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp _exit
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
.bss
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
level : .res 1
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
.rodata
|
|
|
|
|
|
|
|
; Read whole program file
|
2013-05-09 11:56:54 +00:00
|
|
|
source: jsr $BF00
|
|
|
|
.byte READ_CALL
|
|
|
|
.word read_param
|
2015-04-26 12:01:36 +00:00
|
|
|
bcs error
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Close program file
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr $BF00
|
|
|
|
.byte CLOSE_CALL
|
|
|
|
.word close_param
|
2015-04-26 12:01:36 +00:00
|
|
|
bcs error
|
|
|
|
|
|
|
|
; Check for cmdline handling
|
|
|
|
lda $0100 ; Valid cmdline?
|
|
|
|
beq jump ; No, jump to program right away
|
|
|
|
ldx file_type ; SYS file?
|
|
|
|
bne system ; Yes, check for startup filename
|
|
|
|
|
|
|
|
; Store REM and cmdline in BASIC input buffer
|
|
|
|
lda #$B2 ; REM token
|
|
|
|
bne :++ ; Branch always
|
|
|
|
: inx
|
|
|
|
lda a:$0100-1,x
|
|
|
|
: sta $0200,x
|
|
|
|
bne :--
|
|
|
|
beq jump ; Branch always
|
|
|
|
|
|
|
|
; Check for startup filename support
|
|
|
|
; ProDOS TechRefMan, chapter 5.1.5.1:
|
|
|
|
; "$2000 is a jump instruction. $2003 and $2004 are $EE."
|
2016-05-30 12:31:53 +00:00
|
|
|
system: lda #$4C
|
|
|
|
cmp $2000
|
2015-04-26 12:01:36 +00:00
|
|
|
bne jump
|
2016-05-30 12:16:37 +00:00
|
|
|
lda #$EE
|
|
|
|
cmp $2003
|
2015-04-26 12:01:36 +00:00
|
|
|
bne jump
|
2016-05-30 12:16:37 +00:00
|
|
|
cmp $2004
|
2015-04-26 12:01:36 +00:00
|
|
|
bne jump
|
|
|
|
|
|
|
|
; Store cmdline in startup filename buffer
|
|
|
|
ldx #$01
|
|
|
|
: lda a:$0100-1,x
|
|
|
|
beq :+
|
|
|
|
sta $2006,x
|
|
|
|
inx
|
|
|
|
cpx $2005 ; Buffer full?
|
|
|
|
bcc :- ; No, continue
|
|
|
|
: dex
|
|
|
|
stx $2006 ; Store cmdline length
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
; Go for it ...
|
2015-04-26 12:01:36 +00:00
|
|
|
jump: jmp (data_buffer)
|
|
|
|
|
|
|
|
file_type = * - source + target
|
|
|
|
.byte $00
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
read_param = * - source + target
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $04 ; PARAM_COUNT
|
2011-02-06 22:27:31 +00:00
|
|
|
read_ref = * - source + target
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $00 ; REF_NUM
|
2011-02-06 22:27:31 +00:00
|
|
|
data_buffer = * - source + target
|
2013-05-09 11:56:54 +00:00
|
|
|
.addr $2000 ; DATA_BUFFER
|
|
|
|
.word $FFFF ; REQUEST_COUNT
|
|
|
|
.word $0000 ; TRANS_COUNT
|
2011-02-06 22:27:31 +00:00
|
|
|
|
|
|
|
close_param = * - source + target
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $01 ; PARAM_COUNT
|
2011-02-06 22:27:31 +00:00
|
|
|
close_ref = * - source + target
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $00 ; REF_NUM
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2012-01-02 11:09:25 +00:00
|
|
|
; Quit to ProDOS dispatcher
|
|
|
|
quit = * - source + target
|
2015-04-26 12:01:36 +00:00
|
|
|
error: jsr $BF00
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $65 ; QUIT
|
|
|
|
.word quit_param
|
2012-01-02 11:09:25 +00:00
|
|
|
|
2011-02-06 22:27:31 +00:00
|
|
|
quit_param = * - source + target
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $04 ; PARAM_COUNT
|
|
|
|
.byte $00 ; QUIT_TYPE
|
|
|
|
.word $0000 ; RESERVED
|
|
|
|
.byte $00 ; RESERVED
|
|
|
|
.word $0000 ; RESERVED
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2012-01-02 11:09:25 +00:00
|
|
|
size = * - source
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2012-01-02 11:09:25 +00:00
|
|
|
target = DOSWARM - size
|
2011-02-06 22:27:31 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
dosvec: jmp quit
|