From 42595fbf134ab12c3ea2872397fd94888826bcf5 Mon Sep 17 00:00:00 2001 From: Greg King Date: Thu, 3 Apr 2014 08:23:28 -0400 Subject: [PATCH] Added command-line argument parsing to the CBM510 and CBM610 targets. --- asminc/cbm510.inc | 4 +- asminc/cbm610.inc | 4 +- doc/cbm510.sgml | 52 +++++++++----- doc/cbm610.sgml | 101 +++++++++++++++----------- libsrc/cbm510/mainargs.s | 150 ++++++++++++++++++++++++++++++++++++--- libsrc/cbm610/mainargs.s | 150 ++++++++++++++++++++++++++++++++++++--- 6 files changed, 381 insertions(+), 80 deletions(-) diff --git a/asminc/cbm510.inc b/asminc/cbm510.inc index abc363728..e1a86c487 100644 --- a/asminc/cbm510.inc +++ b/asminc/cbm510.inc @@ -4,7 +4,7 @@ ; Taken from a kernal disassembly done by myself in 2000/2001. ; ; 2001-09-13, Ullrich von Bassewitz -; 2013-08-26, Greg King +; 2014-04-02, Greg King ;----------------------------------------------------------------------------- @@ -14,6 +14,8 @@ ExecReg := $00 ; Controls execution memory bank IndReg := $01 ; Controls indirect indexed load-store bank TXTPTR := $85 ; Far pointer into BASIC source code +FNAM := $90 ; Far pointer to LOAD/SAVE file-name +FNAM_LEN := $9D ; Holds length of file-name ; --------------------------------------------------------------------------- ; Screen size diff --git a/asminc/cbm610.inc b/asminc/cbm610.inc index f442f5da8..b1b03eb1d 100644 --- a/asminc/cbm610.inc +++ b/asminc/cbm610.inc @@ -4,7 +4,7 @@ ; Taken from a kernal disassembly done by myself in 1987. ; ; 1998-09-28, Ullrich von Bassewitz -; 2013-08-26, Greg King +; 2014-04-02, Greg King ; --------------------------------------------------------------------------- @@ -14,6 +14,8 @@ ExecReg := $00 ; Controls execution memory bank IndReg := $01 ; Controls indirect indexed load-store bank TXTPTR := $85 ; Far pointer into BASIC source code +FNAM := $90 ; Far pointer to LOAD/SAVE file-name +FNAM_LEN := $9D ; Holds length of file-name ; --------------------------------------------------------------------------- ; Screen size diff --git a/doc/cbm510.sgml b/doc/cbm510.sgml index f0e03c28b..3389bd4e1 100644 --- a/doc/cbm510.sgml +++ b/doc/cbm510.sgml @@ -6,7 +6,7 @@ Ullrich von Bassewitz, Stefan A. Haubenthal, -2014-03-26 +2014-04-02 An overview over the Commodore 510 runtime system as it is implemented for the @@ -38,10 +38,10 @@ machines are supported by this cc65 target. Binary format

The standard binary output format generated by the linker for the Commodore -510 target is a machine language program with a one line BASIC stub, which -transfers control to the machine language running in bank 0. This means that a -program can be loaded as BASIC program and started with RUN. It is of course -possible to change this behaviour by using a modified startup file and linker +510 target is a machine language program with a one-line BASIC stub, which +transfers control to the machine language running in bank 0. That means that a +program can be loaded as a BASIC program, and started with RUN. It is, of course, +possible to change that behaviour by using a modified startup file and linker config. @@ -58,7 +58,7 @@ The default memory configuration for the CBM 510 allocates all memory between in low memory is lost, because a separate hardware stack is set up in page 1, and the kernal replacement functions need some more memory locations. A few more pages are lost in high memory, because the runtime sets up a copy of the -character ROM, a text screen and a CBM compatible jump table at $FF81. +character ROM, a text screen, and a CBM-compatible jump table at $FF81. The main startup code is located at $0400, so about 54K of the complete bank are actually usable for applications. @@ -66,10 +66,10 @@ Special locations:

@@ -79,7 +79,7 @@ Special locations: Programs containing CBM 510-specific code may use the CBM 510-specific functions

@@ -133,11 +133,11 @@ declaration and usage. The following pseudo variables declared in the Note: All I/O chips are located in the system bank (bank 15) and can +Note: All I/O chips are located in the system bank (bank 15); and can therefore not be accessed like on other platforms. Please use one of the -not work! @@ -164,7 +164,7 @@ will not work! declaration of the structure. - The two 6525 triport chips may be accessed by using this variable. See the + The two 6525 triport chips may be accessed by using these variables. See the - Supports up to two standard joysticks connected to the joysticks port of + Supports up to two standard joysticks connected to the joysticks ports of the Commodore 510.

@@ -247,17 +247,17 @@ Since the program runs in bank 0, and the kernal and all I/O chips are located in bank 15, calling ROM routines or accessing hardware needs special code. The cc65 runtime implements wrappers for all functions in the kernal jump table. While this simplifies things, it should be noted that the wrappers do have -quite an impact on performance: A cross bank call has an extra 300µs +quite an impact on performance: A cross-bank call has an extra 300µs penalty added by the wrapper. Interrupts

Compiled programs contain an interrupt handler that runs in the program bank. -This has several advantages, one of them being performance (see cross bank +This has several advantages, one of them being performance (see cross-bank call overhead mentioned above). However, this introduces one problem: Interrupts are lost while the CPU executes code in the kernal bank. As a -result, the clock may go wrong and (worse) serial interrupts may get lost. +result, the clock may go wrong; and (worse), serial interrupts may get lost. Since the cc65 runtime does only call the kernal for disk I/O, this means that a program should not do file I/O while it depends on interrupts. @@ -269,8 +269,22 @@ a program should not do file I/O while it depends on interrupts. Passing arguments to the program

-Command line argument passing is currently not supported for the Commodore -510. +Command-line arguments can be passed to + RUN:REM ARG1 " ARG2 IS QUOTED" ARG3 "" ARG5 + + + +Arguments are separated by spaces. +Arguments may be quoted. +Leading and trailing spaces around an argument are ignored. Spaces within + a quoted argument are allowed. +The first argument passed to A maximum number of 10 arguments (including the program name) are + supported. + Program return code

diff --git a/doc/cbm610.sgml b/doc/cbm610.sgml index a6ec4650b..df44f1d76 100644 --- a/doc/cbm610.sgml +++ b/doc/cbm610.sgml @@ -2,9 +2,10 @@

-Commodore 610 specific information for cc65 -<author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org"> -<date>2003-12-16 +<title>Commodore 610-specific information for cc65 +<author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org"><newline> +<url url="mailto:greg.king5@verizon.net" name="Greg King"> +<date>2004-04-02 <abstract> An overview over the Commodore 610 runtime system as it is implemented for the @@ -19,11 +20,11 @@ cc65 C compiler. <sect>Overview<p> This file contains an overview of the CBM 610 runtime system as it comes with -the cc65 C compiler. It describes the memory layout, CBM 610 specific header +the cc65 C compiler. It describes the memory layout, CBM 610-specific header files, available drivers, and any pitfalls specific to that platform. -Please note that CBM 610 specific functions are just mentioned here, they are -described in detail in the separate <htmlurl url="funcref.html" name="function +Please note that CBM 610-specific functions are just mentioned here, they are +described in detail in the separate <url url="funcref.html" name="function reference">. Even functions marked as "platform dependent" may be available on more than one platform. Please see the function reference for more information. @@ -31,21 +32,22 @@ information. In addition to the Commodore 610 (named B40 in the U.S.), several other machines are supported by this cc65 target, since they have identical hardware: The Commodore 620 and 630 (more memory, additional coprocessor -card), and the Commodore 710, 720 and 730 (same hardware in another case with -a builtin monitor). +card), and the Commodore 710, 720, and 730 (same hardware in another case with +a built-in monitor). <sect>Binary format<p> The standard binary output format generated by the linker for the Commodore -610 target is a machine language program with a one line BASIC stub, which -transfers control to the machine language running in bank 1. This means that a -program can be loaded as BASIC program and started with RUN. It is of course -possible to change this behaviour by using a modified startup file and linker +610 target is a machine language program with a one-line BASIC stub, which +transfers control to the machine language running in bank 1. That means that a +program can be loaded as a BASIC program, and started with RUN. It is, of course, +possible to change that behaviour by using a modified startup file and linker config. + <sect>Memory layout<p> cc65 generated programs for the Commodore 610 run in bank 1, the memory bank @@ -57,8 +59,8 @@ The default memory configuration for the CBM 610 allocates all memory between $0002 and $FFF0 in bank 1 for the compiled program. Some space in low memory is lost, because a separate hardware stack is set up in page 1, and the kernal replacement functions need some more memory locations. A few -more bytes are lost in high memory, because the runtime sets up a CBM -compatible jump table at $FF81. The main startup code is located at +more bytes are lost in high memory, because the runtime sets up a CBM-compatible +jump table at $FF81. The main startup code is located at $0400, so about 63K of the complete bank are actually usable for applications. @@ -66,25 +68,26 @@ Special locations: <descrip> <tag/Stack/ - The C runtime stack is located at $FF81 and growing downwards. + The C runtime stack is located at $FF81, and grows downwards. <tag/Heap/ - The C heap is located at the end of the program and grows towards the C + The C heap is located at the end of the program, and grows towards the C runtime stack. </descrip><p> -<sect>Platform specific header files<p> +<sect>Platform-specific header files<p> -Programs containing CBM 610 specific code may use the <tt/cbm610.h/ or +Programs containing CBM 610-specific code may use the <tt/cbm610.h/ or <tt/cbm.h/ header files. Using the later may be an option when writing code -for more than one CBM platform, since it includes <tt/cbm610.h/ and declares +for more than one CBM platform, since it includes <tt/cbm610.h/, and declares several functions common to all CBM platforms. -<sect1>CBM 610 specific functions<p> -The functions listed below are special for the CBM 610. See the <htmlurl +<sect1>CBM 610-specific functions<p> + +The functions listed below are special for the CBM 610. See the <url url="funcref.html" name="function reference"> for declaration and usage. <itemize> @@ -95,10 +98,10 @@ url="funcref.html" name="function reference"> for declaration and usage. </itemize> -<sect1>CBM specific functions<p> +<sect1>CBM-specific functions<p> Some functions are available for all (or at least most) of the Commodore -machines. See the <htmlurl url="funcref.html" name="function reference"> for +machines. See the <url url="funcref.html" name="function reference"> for declaration and usage. @@ -128,16 +131,15 @@ declaration and usage. </itemize> - <sect1>Hardware access<p> The following pseudo variables declared in the <tt/cbm610.h/ header file do allow access to hardware located in the address space. Some variables are -structures, accessing the struct fields will access the chip registers. +structures; accessing the struct fields will access the chip registers. -<bf>Note:</bf> All I/O chips are located in the system bank (bank 15) and can +<bf>Note:</bf> All I/O chips are located in the system bank (bank 15); and can therefore not be accessed like on other platforms. Please use one of the -<tt/peekbsys/, <tt/peekwsys/, <tt/pokebsys/ and <tt/pokewsys/ functions to +<tt/peekbsys/, <tt/peekwsys/, <tt/pokebsys/, and <tt/pokewsys/ functions to access the I/O chips. Direct reads and writes to the structures named below will <em>not</em> work! @@ -163,7 +165,7 @@ will <em>not</em> work! declaration of the structure. <tag><tt/TPI1, TPI2/</tag> - The two 6525 triport chips may be accessed by using this variable. See the + The two 6525 triport chips may be accessed by using these variables. See the <tt/_6525.h/ header file located in the include directory for the declaration of the structure. @@ -180,7 +182,7 @@ The names in the parentheses denote the symbols to be used for static linking of No graphics drivers are currently available for the Commodore 610 (and since the machine has no graphics capabilities, chances for a graphics driver aren't -really good:-). +really good :-). <sect1>Extended memory drivers<p> @@ -194,9 +196,8 @@ really good:-). <sect1>Joystick drivers<p> -The Commodore 610 is a business machine and doesn't have joystick ports. There -are no drivers for the non existing ports available. - +The Commodore 610 is a business machine, and doesn't have joystick ports. There +are no drivers for the non-existing ports available. <sect1>Mouse drivers<p> @@ -210,14 +211,15 @@ No mouse drivers are currently available for the Commodore 610. <tag><tt/cbm610-std.ser (cbm610_std_ser)/</tag> Driver for the 6551 ACIA chip built into the Commodore 610. Supports up to - 19200 baud, hardware flow control (RTS/CTS) and interrupt driven receives. - Note that because of the peculiarities of the 6551 chip transmits are not - interrupt driven, and the transceiver blocks if the receiver asserts flow + 19200 BPS, hardware flow control (RTS/CTS), and interrupt-driven receives. + Note that, because of the peculiarities of the 6551 chip, transmits are not + interrupt driven; and, the transceiver blocks if the receiver asserts flow control because of a full buffer. </descrip><p> + <sect>Limitations<label id="limitations"><p> @@ -227,27 +229,44 @@ Since the program runs in bank 1, and the kernal and all I/O chips are located in bank 15, calling ROM routines or accessing hardware needs special code. The cc65 runtime implements wrappers for all functions in the kernal jump table. While this simplifies things, it should be noted that the wrappers do have -quite an impact on performance: A cross bank call has an extra 300µs +quite an impact on performance: A cross-bank call has an extra 300µs penalty added by the wrapper. + <sect1>Interrupts<p> Compiled programs contain an interrupt handler that runs in the program bank. -This has several advantages, one of them being performance (see cross bank +This has several advantages, one of them being performance (see cross-bank call overhead mentioned above). However, this introduces one problem: Interrupts are lost while the CPU executes code in the kernal bank. As a -result, the clock may go wrong and (worse) serial interrupts may get lost. +result, the clock may go wrong; and (worse), serial interrupts may get lost. Since the cc65 runtime does only call the kernal for disk I/O, this means that a program should not do file I/O while it depends on interrupts. + <sect>Other hints<p> + <sect1>Passing arguments to the program<p> -Command line argument passing is currently not supported for the Commodore -610. +Command-line arguments can be passed to <tt/main()/. Since that is not +supported directly by BASIC, the following syntax was chosen: + +<tscreen><verb> + RUN:REM ARG1 " ARG2 IS QUOTED" ARG3 "" ARG5 +</verb></tscreen> + +<enum> +<item>Arguments are separated by spaces. +<item>Arguments may be quoted. +<item>Leading and trailing spaces around an argument are ignored. Spaces within + a quoted argument are allowed. +<item>The first argument passed to <tt/main()/ is the program name. +<item>A maximum number of 10 arguments (including the program name) are + supported. +</enum> <sect1>Program return code<p> @@ -262,7 +281,7 @@ The runtime for the Commodore 610 uses routines marked as <tt/.INTERRUPTOR/ for interrupt handlers. Such routines must be written as simple machine language subroutines and will be called automatically by the interrupt handler code when they are linked into a program. See the discussion of the -<tt/.CONDES/ feature in the <htmlurl url="ca65.html" name="assembler manual">. +<tt/.CONDES/ feature in the <url url="ca65.html" name="assembler manual">. diff --git a/libsrc/cbm510/mainargs.s b/libsrc/cbm510/mainargs.s index 7ed8d46f4..542b1ec65 100644 --- a/libsrc/cbm510/mainargs.s +++ b/libsrc/cbm510/mainargs.s @@ -1,24 +1,156 @@ +; mainargs.s ; -; Ullrich von Bassewitz, 2003-03-07 +; 2003-03-07, Ullrich von Bassewitz, +; based on code from Stefan A. Haubenthal, <polluks@web.de> +; 2005-02-26, Ullrich von Bassewitz +; 2014-04-02, Greg King ; -; Setup arguments for main +; Scan a group of arguments that are in BASIC's input-buffer. +; Build an array that points to the beginning of each argument. +; Send, to main(), that array and the count of the arguments. ; - +; Command-lines look like these lines: +; +; run +; run : rem +; run:rem arg1 " arg 2 is quoted " arg3 "" arg5 +; +; "run" and "rem" are entokenned; the args. are not. Leading and trailing +; spaces outside of quotes are ignored. +; +; TO-DO: +; - The "file-name" might be a path-name; don't copy the directory-components. +; - Add a control-character quoting mechanism. .constructor initmainargs, 24 .import __argc, __argv + .import sys_bank, restore_bank + .import sysp0:zp, ptr1:zp + + .include "cbm510.inc" + .macpack generic -;--------------------------------------------------------------------------- + +MAXARGS = 10 ; Maximum number of arguments allowed +REM = $8f ; BASIC token-code +NAME_LEN = 16 ; maximum length of command-name + ; Get possible command-line arguments. Goes into the special INIT segment, -; which may be reused after the startup code is run - +; which may be reused after the startup code is run. +; .segment "INIT" -.proc initmainargs +initmainargs: +; Assume that the program was loaded, a moment ago, by the traditional LOAD +; statement. Save the "most-recent filename" as argument #0. +; Because the buffer, that we're copying into, was zeroed out, +; we don't need to add a NUL character. +; + jsr sys_bank + ldy #FNAM + lda (sysp0),y ; Get file-name pointer from system bank + sta ptr1 + iny + lda (sysp0),y + sta ptr1+1 + iny ; FNAM_BANK + lda (sysp0),y + tax + ldy #FNAM_LEN + lda (sysp0),y + tay + stx IndReg ; Look for name in correct bank + cpy #NAME_LEN + 1 + blt L1 + ldy #NAME_LEN - 1 ; limit the length +L0: lda (ptr1),y + sta name,y +L1: dey + bpl L0 + jsr restore_bank + inc __argc ; argc always is equal to at least 1 + +; Find a "rem" token. +; + ldx #0 +L2: lda BASIC_BUF,x + bze done ; no "rem," no args. + inx + cmp #REM + bne L2 + ldy #1 * 2 + +; Find the next argument. +; +next: lda BASIC_BUF,x + bze done ; End of line reached + inx + cmp #' ' ; Skip leading spaces + beq next ; + +; Found start of next argument. We've incremented the pointer in X already, so +; it points to the second character of the argument. That is useful because we +; will check now for a quoted argument; in which case, we will have to skip that +; first character. +; +found: cmp #'"' ; Is the argument quoted? + beq setterm ; Jump if so + dex ; Reset pointer to first argument character + lda #' ' ; A space ends the argument +setterm:sta term ; Set end-of-argument marker + +; Now, store a pointer to the argument into the next slot. +; + txa ; Get low byte + add #<BASIC_BUF + sta argv,y ; argv[y]= &arg + lda #>0 + adc #>BASIC_BUF + sta argv+1,y + iny + iny + inc __argc ; Found another arg + +; Search for the end of the argument. +; +argloop:lda BASIC_BUF,x + bze done + inx + cmp term + bne argloop + +; We've found the end of the argument. X points one character behind it, and +; A contains the terminating character. To make the argument a valid C string, +; replace the terminating character by a zero. +; + lda #0 + sta BASIC_BUF-1,x + +; Check if the maximum number of command-line arguments is reached. If not, +; parse the next one. +; + lda __argc ; Get low byte of argument count + cmp #MAXARGS ; Maximum number of arguments reached? + blt next ; Parse next one if not + +; (The last vector in argv[] already is NULL.) +; +done: lda #<argv + ldx #>argv + sta __argv + stx __argv + 1 rts -.endproc - +; These arrays are zeroed before initmainargs is called. +; char name[16+1]; +; char* argv[MAXARGS+1]={name}; +; +.bss +term: .res 1 +name: .res NAME_LEN + 1 +.data +argv: .addr name + .res MAXARGS * 2, 0 diff --git a/libsrc/cbm610/mainargs.s b/libsrc/cbm610/mainargs.s index 7ed8d46f4..9b9aea650 100644 --- a/libsrc/cbm610/mainargs.s +++ b/libsrc/cbm610/mainargs.s @@ -1,24 +1,156 @@ +; mainargs.s ; -; Ullrich von Bassewitz, 2003-03-07 +; 2003-03-07, Ullrich von Bassewitz, +; based on code from Stefan A. Haubenthal, <polluks@web.de> +; 2005-02-26, Ullrich von Bassewitz +; 2014-04-02, Greg King ; -; Setup arguments for main +; Scan a group of arguments that are in BASIC's input-buffer. +; Build an array that points to the beginning of each argument. +; Send, to main(), that array and the count of the arguments. ; - +; Command-lines look like these lines: +; +; run +; run : rem +; run:rem arg1 " arg 2 is quoted " arg3 "" arg5 +; +; "run" and "rem" are entokenned; the args. are not. Leading and trailing +; spaces outside of quotes are ignored. +; +; TO-DO: +; - The "file-name" might be a path-name; don't copy the directory-components. +; - Add a control-character quoting mechanism. .constructor initmainargs, 24 .import __argc, __argv + .import sys_bank, restore_bank + .import sysp0:zp, ptr1:zp + + .include "cbm610.inc" + .macpack generic -;--------------------------------------------------------------------------- + +MAXARGS = 10 ; Maximum number of arguments allowed +REM = $8f ; BASIC token-code +NAME_LEN = 16 ; maximum length of command-name + ; Get possible command-line arguments. Goes into the special INIT segment, -; which may be reused after the startup code is run - +; which may be reused after the startup code is run. +; .segment "INIT" -.proc initmainargs +initmainargs: +; Assume that the program was loaded, a moment ago, by the traditional LOAD +; statement. Save the "most-recent filename" as argument #0. +; Because the buffer, that we're copying into, was zeroed out, +; we don't need to add a NUL character. +; + jsr sys_bank + ldy #FNAM + lda (sysp0),y ; Get file-name pointer from system bank + sta ptr1 + iny + lda (sysp0),y + sta ptr1+1 + iny ; FNAM_BANK + lda (sysp0),y + tax + ldy #FNAM_LEN + lda (sysp0),y + tay + stx IndReg ; Look for name in correct bank + cpy #NAME_LEN + 1 + blt L1 + ldy #NAME_LEN - 1 ; limit the length +L0: lda (ptr1),y + sta name,y +L1: dey + bpl L0 + jsr restore_bank + inc __argc ; argc always is equal to at least 1 + +; Find a "rem" token. +; + ldx #0 +L2: lda BASIC_BUF,x + bze done ; no "rem," no args. + inx + cmp #REM + bne L2 + ldy #1 * 2 + +; Find the next argument. +; +next: lda BASIC_BUF,x + bze done ; End of line reached + inx + cmp #' ' ; Skip leading spaces + beq next ; + +; Found start of next argument. We've incremented the pointer in X already, so +; it points to the second character of the argument. That is useful because we +; will check now for a quoted argument; in which case, we will have to skip that +; first character. +; +found: cmp #'"' ; Is the argument quoted? + beq setterm ; Jump if so + dex ; Reset pointer to first argument character + lda #' ' ; A space ends the argument +setterm:sta term ; Set end-of-argument marker + +; Now, store a pointer to the argument into the next slot. +; + txa ; Get low byte + add #<BASIC_BUF + sta argv,y ; argv[y]= &arg + lda #>0 + adc #>BASIC_BUF + sta argv+1,y + iny + iny + inc __argc ; Found another arg + +; Search for the end of the argument. +; +argloop:lda BASIC_BUF,x + bze done + inx + cmp term + bne argloop + +; We've found the end of the argument. X points one character behind it, and +; A contains the terminating character. To make the argument a valid C string, +; replace the terminating character by a zero. +; + lda #0 + sta BASIC_BUF-1,x + +; Check if the maximum number of command-line arguments is reached. If not, +; parse the next one. +; + lda __argc ; Get low byte of argument count + cmp #MAXARGS ; Maximum number of arguments reached? + blt next ; Parse next one if not + +; (The last vector in argv[] already is NULL.) +; +done: lda #<argv + ldx #>argv + sta __argv + stx __argv + 1 rts -.endproc - +; These arrays are zeroed before initmainargs is called. +; char name[16+1]; +; char* argv[MAXARGS+1]={name}; +; +.bss +term: .res 1 +name: .res NAME_LEN + 1 +.data +argv: .addr name + .res MAXARGS * 2, 0