Added cmdline support to exec().

The starting point is the CALL2051:REM <cmdline> approach. It uses the BASIC input buffer at $200. ProDOS stores the name of the loaded program at $280 (which we want for argv[0]) leaving us with 128 char buffer. If we run the program via exec() we don't need the CALL2051 but only the REM token (which is just one char). So have a maximum cmdline length of 126 (plus a terminating zero).

There's no specification for ProDOS BIN file cmdline parameters so exec() just supports the CALL2051:REM <cmdline> approach. In contrast ProDOS SYS files allow for a 'startup filename'. A ProDOS filename is short than 126 chars so having exec() general cut the cmdline after 126 chars seems reasonable. If the SYS file we exec() allows for less we cut the cmdline further.

Our 'loader.system' SYS file however allows for an unusually 126 char long "startup filename" as it is targeted towards cc65 BIN porgrams with their 126 cmdline length.
This commit is contained in:
Oliver Schmidt 2015-04-26 14:01:36 +02:00
parent 88fe6b615b
commit 3c1cd0d867
3 changed files with 108 additions and 44 deletions

View file

@ -23,6 +23,10 @@ oserr: jsr popname ; Preserves A
jmp __mappederrno jmp __mappederrno
_exec: _exec:
; Save cmdline
sta ptr4
stx ptr4+1
; Get and push name ; Get and push name
jsr popax jsr popax
jsr pushname jsr pushname
@ -33,7 +37,7 @@ _exec:
; is stored at $280, starting with a length byte." ; is stored at $280, starting with a length byte."
; In fact BASIC.SYSTEM does the same for BLOAD and BRUN of ; In fact BASIC.SYSTEM does the same for BLOAD and BRUN of
; binary programs so we should do the same too in any case ; binary programs so we should do the same too in any case
; especially as _we_ rely on it in mainargs.s for argv[0]. ; especially as _we_ rely on it in mainargs.s for argv[0]
ldy #$00 ldy #$00
lda (sp),y lda (sp),y
tay tay
@ -66,13 +70,14 @@ _exec:
lda mliparam + MLI::INFO::FILE_TYPE lda mliparam + MLI::INFO::FILE_TYPE
cmp #$FF ; SYS file? cmp #$FF ; SYS file?
bne binary ; No, check for BIN file bne binary ; No, check for BIN file
sta file_type ; Save file type for cmdline handling
; SYS programs replace BASIC.SYSTEM so set in the ProDOS system bit map ; 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 ; protection for pages $80 - $BF just in case BASIC.SYSTEM is there now
ldx #$0F ; Start with protection for pages $B8 - $BF ldx #$0F ; Start with protection for pages $B8 - $BF
lda #%00000001 ; Protect only system global page lda #%00000001 ; Protect only system global page
: sta $BF60,x ; Set protection for 8 pages : sta $BF60,x ; Set protection for 8 pages
lda #$00 ; Protect no page lda #%00000000 ; Protect no page
dex dex
bpl :- bpl :-
bmi prodos ; Branch always bmi prodos ; Branch always
@ -161,8 +166,27 @@ setbuf: lda #$00 ; Low byte
bit $C080 bit $C080
.endif .endif
; Reset stack we already passed
; 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
; Call loader stub after C libary shutdown ; Call loader stub after C libary shutdown
lda #<target : lda #<target
ldx #>target ldx #>target
sta done+1 sta done+1
stx done+2 stx done+2
@ -180,16 +204,58 @@ level : .res 1
source: jsr $BF00 source: jsr $BF00
.byte READ_CALL .byte READ_CALL
.word read_param .word read_param
bcs :+ bcs error
; Close program file ; Close program file
jsr $BF00 jsr $BF00
.byte CLOSE_CALL .byte CLOSE_CALL
.word close_param .word close_param
bcs :+ 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."
system: lda $2000
cmp #$4C
bne jump
lda $2003
cmp #$EE
bne jump
lda $2004
cmp #$EE
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
; Go for it ... ; Go for it ...
jmp (data_buffer) jump: jmp (data_buffer)
file_type = * - source + target
.byte $00
read_param = * - source + target read_param = * - source + target
.byte $04 ; PARAM_COUNT .byte $04 ; PARAM_COUNT
@ -207,7 +273,7 @@ close_ref = * - source + target
; Quit to ProDOS dispatcher ; Quit to ProDOS dispatcher
quit = * - source + target quit = * - source + target
: jsr $BF00 error: jsr $BF00
.byte $65 ; QUIT .byte $65 ; QUIT
.word quit_param .word quit_param

View file

@ -34,12 +34,12 @@
MAXARGS = 10 MAXARGS = 10
; ProDOS stores the filename in the second half of BASIC's input buffer, so ; ProDOS stores the filename in the second half of BASIC's input buffer, so
; there are 128 characters left. At least 1 characters is necessary for the ; there are 128 characters left. At least 1 character is necessary for the
; REM so 127 characters may be used before overwriting the ProDOS filename. ; REM so 127 characters (including the terminating zero) may be used before
; As we don't want to put further restrictions on the command-line length ; overwriting the ProDOS filename. As we don't want to further restrict the
; we reserve those 127 characters terminated by a zero. ; command-line length we reserve those 127 characters.
BUF_LEN = 128 BUF_LEN = 127
BASIC_BUF = $200 BASIC_BUF = $200
FNAM_LEN = $280 FNAM_LEN = $280

View file

@ -22,14 +22,14 @@ READ_CALL = $CA
CLOSE_CALL = $CC CLOSE_CALL = $CC
FILE_NOT_FOUND_ERR = $46 FILE_NOT_FOUND_ERR = $46
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ------------------------------------------------------------------------
.import __CODE_0300_SIZE__, __DATA_0300_SIZE__ .import __CODE_0300_SIZE__, __DATA_0300_SIZE__
.import __CODE_0300_LOAD__, __CODE_0300_RUN__ .import __CODE_0300_LOAD__, __CODE_0300_RUN__
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ------------------------------------------------------------------------
.segment "DATA_2000" .segment "DATA_2000"
GET_FILE_INFO_PARAM: GET_FILE_INFO_PARAM:
.byte $0A ;PARAM_COUNT .byte $0A ;PARAM_COUNT
@ -57,9 +57,9 @@ LOADING:
ELLIPSES: ELLIPSES:
.byte " ...", $0D, $0D, $00 .byte " ...", $0D, $0D, $00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ------------------------------------------------------------------------
.segment "DATA_0300" .segment "DATA_0300"
READ_PARAM: READ_PARAM:
.byte $04 ;PARAM_COUNT .byte $04 ;PARAM_COUNT
@ -88,15 +88,15 @@ ERROR_NUMBER:
PRESS_ANY_KEY: PRESS_ANY_KEY:
.asciiz " - Press Any Key " .asciiz " - Press Any Key "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ------------------------------------------------------------------------
.segment "CODE_2000" .segment "CODE_2000"
jmp :+ jmp :+
.byte $EE .byte $EE
.byte $EE .byte $EE
.byte $80 .byte $7F
STARTUP:.res $80 STARTUP:.res $7F
; Reset stack ; Reset stack
: ldx #$FF : ldx #$FF
@ -104,8 +104,8 @@ STARTUP:.res $80
; Relocate CODE_0300 and DATA_0300 ; Relocate CODE_0300 and DATA_0300
ldx #<(__CODE_0300_SIZE__ + __DATA_0300_SIZE__) ldx #<(__CODE_0300_SIZE__ + __DATA_0300_SIZE__)
: lda __CODE_0300_LOAD__ - 1,x : lda __CODE_0300_LOAD__-1,x
sta __CODE_0300_RUN__ - 1,x sta __CODE_0300_RUN__-1,x
dex dex
bne :- bne :-
@ -118,13 +118,13 @@ STARTUP:.res $80
; Add trailing '\0' to pathname ; Add trailing '\0' to pathname
tax tax
lda #$00 lda #$00
sta PATHNAME + 1,x sta PATHNAME+1,x
; Copy ProDOS startup filename and trailing '\0' to stack ; Copy ProDOS startup filename and trailing '\0' to stack
ldx STARTUP ldx STARTUP
lda #$00 lda #$00
beq :++ ; bra beq :++ ; Branch always
: lda STARTUP + 1,x : lda STARTUP+1,x
: sta STACK,x : sta STACK,x
dex dex
bpl :-- bpl :--
@ -133,8 +133,8 @@ STARTUP:.res $80
lda #<LOADING lda #<LOADING
ldx #>LOADING ldx #>LOADING
jsr PRINT jsr PRINT
lda #<(PATHNAME + 1) lda #<(PATHNAME+1)
ldx #>(PATHNAME + 1) ldx #>(PATHNAME+1)
jsr PRINT jsr PRINT
lda #<ELLIPSES lda #<ELLIPSES
ldx #>ELLIPSES ldx #>ELLIPSES
@ -159,16 +159,16 @@ STARTUP:.res $80
; Get load address from aux-type ; Get load address from aux-type
lda FILE_INFO_ADDR lda FILE_INFO_ADDR
ldx FILE_INFO_ADDR + 1 ldx FILE_INFO_ADDR+1
sta READ_ADDR sta READ_ADDR
stx READ_ADDR + 1 stx READ_ADDR+1
; It's high time to leave this place ; It's high time to leave this place
jmp __CODE_0300_RUN__ jmp __CODE_0300_RUN__
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ------------------------------------------------------------------------
.segment "CODE_0300" .segment "CODE_0300"
jsr MLI jsr MLI
.byte READ_CALL .byte READ_CALL
@ -180,12 +180,12 @@ STARTUP:.res $80
.word CLOSE_PARAM .word CLOSE_PARAM
bcs ERROR bcs ERROR
; Copy REM token and startup filename to BASIC input buffer ; Copy REM and startup filename to BASIC input buffer
ldx #$00 ldx #$00
lda #$B2 lda #$B2 ; REM token
bne :++ ; bra bne :++ ; Branch always
: inx : inx
lda a:STACK - 1,x lda a:STACK-1,x
: sta BUF,x : sta BUF,x
bne :-- bne :--
@ -207,7 +207,7 @@ PRINT:
: ora #$80 : ora #$80
jsr COUT jsr COUT
iny iny
bne :-- ; bra bne :-- ; Branch always
: rts : rts
ERROR: ERROR:
@ -216,7 +216,7 @@ ERROR:
lda #<FILE_NOT_FOUND lda #<FILE_NOT_FOUND
ldx #>FILE_NOT_FOUND ldx #>FILE_NOT_FOUND
jsr PRINT jsr PRINT
beq :++ ; bra beq :++ ; Branch always
: pha : pha
lda #<ERROR_NUMBER lda #<ERROR_NUMBER
ldx #>ERROR_NUMBER ldx #>ERROR_NUMBER
@ -230,5 +230,3 @@ ERROR:
jsr MLI jsr MLI
.byte QUIT_CALL .byte QUIT_CALL
.word QUIT_PARAM .word QUIT_PARAM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;