💾 Archived View for spam.works › mirrors › textfiles › apple › ANATOMY › cmdopen.txt captured on 2023-06-16 at 21:11:11.

View Raw

More Information

-=-=-=-=-=-=-







(A2A3)
CMDOPEN  LDA #0       ;0 = code for text file.
(A2A5)   JMP OPNCKTYP ;Go open the file & chk its type.
         ------------

(A3D5)
OPNCKTYP STA FILTYPFM ;Put code for file type in the
(A3D8)   PHA          ;Fm parameter list & save it on stk.
                      ;($00=Text, $01=Integer, $02=Applesoft,
                      ;$04=Binary, $08=S-type, $10=Relocatable,
                      ;$20=A-type and $40=B-type.)
(A3D9)   JSR HNDLCMD  ;Use FM cmd handler to open file.

                      * Common file manager command handler code.
                      (A2A8)
                      HNDLCMD  LDA #1       ;1 = open opcode.
                      HNDLCMD1 STA TEMPBYT  ;Store opcode in temporary location.
                               LDA LENPRSD  ;Get L-parameter from parsed table.
                               BNE SAVLENFM ;Was a non-zero L-parm issued with cmd?
                               LDA LENPRSD+1
                               BNE SAVLENFM
                               LDA #1       ;Length was 0 so make it 1 instead.
                               STA LENPRSD
                      SAVLENFM LDA LENPRSD  ;Put length in FM parm list.
                               STA RECLENFM ;(Note:  Record length = 1 for sequential files
                               LDA LENPRSD+1 ;else parsed length for random access files.)
                               STA RECLENFM+1
                      CLSLOCBF JSR CMDCLOSE ;Close file if it's already open.
                      (A2C8)

                                            (A2EA)
                                            CMDCLOSE .
                                                     .
                                            (See dis'mbly of CMDCLOSE given below.)
                                                     .
                                                     .
                                            - Note that execution flows thru CMDCLOSE twice if the
                                              file is already open.
                                            - The first time thru, the matching DOS filename buffer is
                                              located & then CLOSEONE is used to close the file.
                                            - Execution then jumps back to the start of CMDCLOSE.
                                            - On this second pass, a matching filename is not found
                                              because the DOS filename buffer was released on the
                                              first pass.  Therefore, A5L/H is left pointing at the
                                              highest numbered (lowest in memory) FREE DOS buffer
                                              when CMCLOSE is exited via EVENTXIT and CLOSERTS.
                                            - If the file is not already open on the first entry to
                                              CMDCLOSE, only one pass is made.  This single pass
                                              resembles the second pass mentioned above.
                                                     .
                                                     .
                                            - If necessary, the CLOSE FUNCTION updates the data
                                              sector, T/S list sector & the VTOC.  It also fixes
                                              up links in the directory sectors and updates the
                                              file size if needed.
                                                     .
                                                     .
                                                     (RTS)

                      (A2CB)   LDA A5L+1    ;Hi byte of A5L/H pointer which points at the highest
                                            ;numbered (lowest in memory) free DOS name buffer (in chain).
                      (A2CD)   BNE SAVFNPTR ;Branch if found a free buffer.
                      (A2CF)   JMP NOBUFERR ;Go issue an out-of-buffers message.
                               ------------ ;(See dis'mbly of errors.)

                      (A2D2)
                      SAVFNPTR STA A3L+1    ;Reset A3L/H to point at DOS buffer that we
                               LDA A5L      ;will use for file name field buffer (chain).
                               STA A3L
                      (A2D8)   JSR CPYPFN

                                            * NOTE:  This (re)assigns a DOS buffer to the
                                            * file we want to open.  The buffer may or may not
                                            * be the same one that was just released by the CLOSE
                                            * command above.  The highest numbered (lowest in
                                            * in memory) free DOS buffer is used.
                                            (A743)
                                            CPYPFN   LDY #29      ;30 bytes to copy (0 to 29).
                                            CPYPRIM  LDA PRIMFNBF,Y ;Copy the name of the file wanted from
                                                     STA (A3L),Y  ;the primary filename buffer into the
                                                     DEY          ;filename field buffer (in DOS chain).
                                                     BPL CPYRIM   ;More chars to get.
                                            (A74D)   RTS
                                            
                      (A2DB)   JSR BUFS2PRM

                                            * Get addresses of the various DOS buffers from the
                                            * chain buffer & put them in the FM parameter list.
                                            (A74E)
                                            BUFS2PRM LDY #30      ;Get addr of FM work buf, T/S list
                                            ADRINPRM LDA (A3L),Y  ;buf, data sector buf & next DOS
                                                     STA WRKBUFFM-30,Y ;filename buf from chain
                                                     INY          ;pointer buffer & put them in FM parm list.
                                                     CPY #38      ;(P.S.  Adr of next DOS file name buf is
                                                     BNE ADRINPRM ;not used by DOS.)
                                            (A75A)   RTS

                      (A2DE)   JSR CPY2PARM

                                            * Put volume, drive, & slot values plus the
                                            * address of the primary filename buffer
                                            * in the FM parameter list.
                                            (A71A)
                                            CPY2PARM LDA VOLPRSD  ;From parsed table.
                                                     STA VOLFM
                                                     LDA DRVPRSD  ;From parsed table.
                                                     STA DRVFM
                                                     LDA SLOTPRSD ;From parsed table.
                                                     STA SLOTFM
                                                     LDA ADRPFNBF ;Get the adr of the primary file
                                                     STA FNAMBUFM ;name buf from the constants tbl
                                                     LDA ADRPFNBF+1 ;and put it in the FM parm list.
                                                     STA FNAMBUFM+1
                                                     LDA A3L      ;Save adr of current DOS file name
                                                     STA CURFNADR ;buf in table of DOS variables.
                                                     LDA A3L+1
                                                     STA CURFNADR+1
                                            (A742)   RTS

                      (A2E1)   LDA TEMPBYT  ;Get open opcode back from temporary buffer
                               STA OPCODEFM ;and put it in the FM parameter list.
                      (A2E7)   JMP FMDRIVER ;Do function via the FM driver.
                               ------------

                      * Use the file manager driver
                      * to do the OPEN FUNCTION.
                      (A6A8)
                      FMDRIVER JSR FILEMGR  ;Call the file manager to do the function.

                                            * File manager proper.
                                            (AB06)
                                            FILEMGR  TSX          ;Save stk pointer so can later rtn to the
                                            (AB07)   STX STKSAV   ;caller of the file manager.  (That is, set it
                                                                  ;to return to the AFTRFUNC routine ($A6AB).)
                                            (AB0A)   JSR RSTRFMWA

                                                                  * Copy FM work buf (in DOS chain) to
                                                                  * FM work area (not in DOS chain).
                                                                  (AE6A)
                                                                  RSTRFMWA JSR SELWKBUF ;Point A4L/H at FM work buf.

                                                                                        * Get addr of FM work buff from
                                                                                        * the FM parm list & put it in
                                                                                        * the A4L/H pointer.
                                                                                        (AF08)
                                                                                        SELWKBUF LDX #0       ;Offset to select
                                                                                                              ;work buffer.
                                                                                        (AF0A)   BEQ PT2FMBUF ;ALWAYS.

                                                                                        (AF12)
                                                                                        PT2FMBUF LDA WRKBUFFM,X
                                                                                                 STA A4L
                                                                                                 LDA WRKBUFFM+1,X
                                                                                                 STA A4L+1
                                                                                        (AF1C)   RTS

                                                                  (AE6D)   LDY #0       ;Zero out return code in FM parm list to
                                                                           STY RTNCODFM ;signal no errors as default condition.
                                                                  STORFMWK LDA (A4L),Y  ;Copy FM work buf to FM work area.
                                                                           STA FMWKAREA,Y
                                                                           INY
                                                                           CPY #45      ;45 bytes to copy (0 to 44).
                                                                           BNE STORFMWK
                                                                           CLC          ;WHY?????
                                                                  (AE7D)   RTS

                                            (AB0D)   LDA OPCODEFM ;Check if opcode is legal.
                                                     CMP #13      ;(Must be less than 13.)
                                                     BCS TOERROP  ;Opcode too large so got range error.
                                                     ASL          ;Double val of opcode & put it in (x)
                                                     TAX          ;so it indexes tables of adrs.
                                                     LDA FMFUNCTB+1,X ;Stick adr of appropriate function
                                                     PHA          ;handler on stack (hi byte first).
                                                     LDA FMFUNCTB,X
                                                     PHA
                                            (AB1E)   RTS          ;DO STACK JUMP TO FUNCTION ENTRY POINT.

                                                     .
                                                     .
                                            (AB22)   .
                                            FNOPEN   .
                                                     .
                                            (See dis'mbly of OPEN function.)
                                                     .
                                                     .
                                            - uses part of COMNOPEN routine.
                                            - reads in VTOC to get link to 1rst directory.
                                            - reads directory secs in & looks for file
                                              description entry with matching filename.
                                            - if matching name found, reads in the
                                              1rst T/S list sector belonging to the file.
                                            - if no match found, starts a new file by:
                                              (1) creates new file description entry
                                                  - copies name to 1rst available spc
                                                    in direc sec (if can't find spc, then
                                                    issues disk full error message).
                                                  - allocates secs for file.
                                                  - writes updated VTOC to disk.
                                                  - puts link to first T/S list, file size, etc
                                                    in directory entry space.
                                                  - writes directory sector buffer to disk.
                                              (2) creates new T/S list & writes it to disk.
                                            - reads T/S list back into T/S list buf.
                                                     .
                                                     .
                                                     (RTS)
                                                     ============

                                            (AB1F)
                                            TOERROP  JMP RNGERROP ;Go handle range error.
                                                     ------------ ;(See dis'mbly of errors.)

                      * Return here after doing the OPEN FUNCTION.
                      * (Cause after @ function is done, use stack
                      * to get back to the original caller.)
                      (A6AB)
                      AFTRFUNC BCC FMDRVRTN ;(c) = 0 = no errors.
                               LDA RTNCODFM ;Get error code from FM parameter list.
                               CMP #$5      ;End-of-data error?
                      (A6B2)   BEQ TOAPPTCH ;Yes - encountered a zeroed-out T/S link or
                                            ;a zeroed-out data pair in T/S list.
                                            ;(Not applicable to the open function.)
                      (A6B4)   JMP OTHRERR  ;No - see dis'mbly of errors.
                               ------------

                      (A6C3)
                      FMDRVRTN RTS

(A3DC)   PLA          ;Get file type wanted off of stack.
(A3DD)   JMP CHKFTYPE ;Go check if type wanted equals type found.
         ------------


(A7C4)
CHKFTYPE EOR FILTYPFM ;Type found (via open function).
(A7C7)   BEQ CKTYPRTN ;Branch if type wanted = type found.


(A7C9)   AND #%01111111 ;Maybe matched-disregard lock bit.
(A7CB)   BEQ CKTYPRTN ;Branch if matched.


(A7CD)   JSR CMDCLOSE ;Wrong kind of file so go close it.

                      * Because the file is already open, execution flows
                      * thru the close cmd twice.  The first time thru, the matching
                      * DOS filename buffer is located & then CLOSEONE is used to
                      * close the file via the open FUNCTION.  The 2nd time thru, a
                      * matching filename buffer is not found because the DOS
                      * buffer was released on the first pass.  Therefore, A5L/H is
                      * left pointing at the highest numbered (lowest in memory)
                      * FREE DOS buffer when the close command is exited via EVENTXIT
                      * and CLOSERTS.
                      * (P.S.  NOT ALL RAMIFICATIONS OF THE CLOSE COMMAND ARE SHOWN
                      * BELOW.  (For more details, see the formatted disassembly
                      * dedicated solely to the close command.)

                      (A2EA)
                      CMDCLOSE LDA PRIMFNBF ;Get 1rst char from primary name buffer.
                               CMP #" "     ;Don't allow leading <spc> in name.
                      (A2EF)   BEQ CLOSEALL ;Leading <spc> = signal to close all files.
                                            ;(A close cmd was issued with no 
                                            ;accompanying file name.)
                      (A2F1)   JSR GETBUFF  ;Locate a DOS file buffer.

                                            * Locate buffer with same name.
                                            * If that fails, locate a free buffer.
                                            (A764)
                                            GETBUFF  LDA #0       ;Default hi-byte of pointer to 0
                                                     STA A5L+1    ;(ie. assume no free buff available).
                                            (A768)   JSR GETFNBF1 ;Get pointer to 1rst filename buffer in chain.

                                                                  (A792)
                                                                  GETFNBF1 LDA ADOSFNB1 ;First link to chain of DOS buffers
                                                                           LDX ADOSFNB1+1 ;(ie. pt 2 1rst name buf in chain).
                                                                  (A798)   BNE SETNXPTR ;ALWAYS.

                                                                  (A7A4)
                                                                  SETNXPTR STX A3L+1    ;Put addr of 1rst filename buffer in ptr
                                                                           STA A3L      ;(ie. highest name buffer in chain).
                                                                           TXA          ;Get hi-byte of addr in back in (a).
                                                                  GETNXRTN RTS
                                                                  (A7A9)

                                            (A76B)   JMP FNCHAR1  ;Get 1rst char from DOS name buf.
                                                     ------------
     
                                            (A76E)
                                            GETFNLNK JSR GETNXBUF

                                                                  * Get addr of next filename buffer in chain
                                                                  * from chain pointers buffer offset 37 & 36
                                                                  * bytes from 1rst char of present filename
                                                                  * buffer.
                                                                  (A79A)
                                                                  GETNXBUF LDY #37      ;Point the pointer at the chain buffer &
                                                                           LDA (A3L),Y  ;get addr of the next filename buffer.
                                                                           BEQ GETNXRTN ;If hi-byte is 0, then lnk zeroed out.
                                                                           TAX          ;Save hi-byte in (x).
                                                                           DEY          ;Pick up low-byte.
                                                                           LDA (A3L),Y
                                                                  SETNXPTR STX A3L+1    ;Stick addr of filename buffer in ptr.
                                                                           STA A3L
                                                                           TXA          ;Get hi-byte back in (a).
                                                                  GETNXRTN RTS
                                                                  (A7A9)
 
                                            (A771)   BEQ NOFNMTCH ;Link zeroed out, end of chain.
                                            FNCHAR1  JSR GETFNBY1 ;Get the 1rst char of filename from buf in chain.
                                            (A773)

                                                                  * Get first byte in DOS file name buf.
                                                                  (A7AA)
                                                                  GETFNBY1 LDY #0       ;Buffer is free if 1rst byte = $00.
                                                                           LDA (A3L),Y  ;If buf occuppied, the 1rst byte = 1rst
                                                                  (A7AE)   RTS          ;char of filename which owns buffer.
 
                                            (A776)   BNE NXFNBUF  ;Take branch if buffer wasn't free.
                                                     LDA A3L      ;Buffer was free, there4, point the A5L/H pointer
                                                     STA A5L      ;at the free buffer.
                                                     LDA A3L+1
                                                     STA A5L+1
                                            (A780)   BNE GETFNLNK ;ALWAYS.

                                            (A782)
                                            NXFNBUF  LDY #29      ;Buffer not free there4 compare name
                                            CMPFNCHR LDA (A3L),Y  ;of owner with name of file in primary
                                                     CMP PRIMFNBF,Y ;name buffer.  (Start with last char first.)
                                            (A789)   BNE GETFNLNK ;Char doesn't match, there4 look for another
                                                                  ;buffer that might have same name.
                                            (A78B)   DEY          ;That char matched, how bout rest of name?
                                                     BPL CMPFNCHR ;30 chars in name (ie. 0 to 29).
                                                     CLC          ;Clr carry to signal names match.
                                            (A78F)   RTS
                                                     ============

                                            (A790)
                                            NOFNMTCH SEC          ;Link zeroed out.
                                            (A791)   RTS
                                                     ============
 
                      (A2F4)
                      EVENTXIT BCS CLOSERTS ;EVENTUALLY exit via this route.
                      (A2F6)   JSR CLOSEONE ;Matching file name was found, so close that file.

                                            * Close a specific file.
                                            (A2FC)
                                            CLOSEONE JSR CKEXCBUF

                                                                  * Check if current filename buffer
                                                                  * belongs to an EXEC file.  After all,
                                                                  * don't want to close buffer if we are
                                                                  * using it to exec (ie. would be like
                                                                  * burying ourselves alive).
                                                                  (A7AF)
                                                                  CKEXCBUF LDA EXECFLAG ;Check to see if EXECing.
                                                                           BEQ NOTEXCBF ;No sweat - not running exec file.
                                                                           LDA EXECBUFF ;We are EXECing, there4 chk if addr of
                                                                           CMP A3L      ;current filename buffer same as that
                                                                           BNE CKEXCRTN ;for buffer belonging to EXEC.
                                                                           LDA EXECBUFF+1 ;Maybe - low-bytes matched,
                                                                           CMP A3L+1    ;so now check hi bytes of addr.
                                                                           BEQ CKEXCRTN ;Exec buffer = current buffer.
                                                                  NOTEXCBF DEX          ;Not EXECing, DEX to make sure z-flag off.
                                                                  (A7C2)                ;(P.S. (x) was orig set to large non-zero
                                                                                        ;val on entry to GETFNBF1.  There4, if now
                                                                  (A7C3)                ;DEX, can be sure z-flag will be off.)
                                                                  CKEXCRTN RTS          ;Exit with: z-flag = 1 if execing.
                                                                                        ;                  = 0 if not execing.

                                            (A2FF)   BNE FREEBUFF ;Not EXECing from this particular file.
                                                     LDA #0       ;Closing an exec file so shut
                                                     STA EXECFLAG ;off the exec flag.
                                            FREEBUFF LDY #0       ;Free up the DOS buffer by poking a $00 in
                                                     TYA          ;1rst byte of filename.
                                                     STA (A3L),Y
                                            (A30B)   JSR BUFS2PRM

                                                                  * GET addresses of the DOS buffers from chain
                                                                  * buffer & put them in FM parameter list.
                                                                  (A74E)
                                                                  BUFS2PRM LDY #30      ;Get addresses of FM Work buffer,
                                                                  ADRINPRM LDA (A3L),Y  ;T/S list buffer, Data sec buffer and 
                                                                           STA WRKBUFFM-30,Y ;NEXT filename buf from the chain ptr
                                                                           INY          ;buf & put them in the FM parameter list.
                                                                           CPY #38      ;(P.S. addr of NEXT filename buffer not
                                                                           BNE ADRINPRM ;used by DOS.)
                                                                  (A75A)   RTS

                                            (A30E)   LDA #2       ;Stick opcode for CLOSE function
                                                     STA OPCODEFM ;in the FM parameter list.
                                            (A313)   JMP FMDRIVER ;Get ready to do the function.
                                                     ------------

                                            (A6A8)
                                            FMDRIVER JSR FILEMGR  ;Call the file manager to do the function.
         
                                                                  (AB06)
                                                                  FILEMGR  TSX          ;Save stk ptr so we can rtn to caller
                                                                           STX STKSAV   ;(ie., set it to rtn to AFTRFUNC, $A6AB.)
                                                                  (AB0A)   JSR RSTRFMWA ;Copy contents of FM work buffer (in DOS
                                                                                        ;chain) to FM work area (not in chain).

                                                                                        (AE6A)
                                                                                        RSTRFMWA JSR SELWKBUF ;Find FM work buffer.

                                                                                                           * Get address of FM
                                                                                                           * work buf from FM
                                                                                                           * parm list & stick
                                                                                                           * it in the A4L/H
                                                                                                           * pointer.
                                                                                                           (AF08)
                                                                                                           SELWKBUF LDA #0
                                                                                                           (AF0A)   BEQ PT2FMBUF

                                                                                                           (AF12)
                                                                                                           PT2FMBUF LDA WRKBUFFM,X 
                                                                                                                    STA A4L
                                                                                                                    LDA WRKBUFFM+1,X
                                                                                                                    STA A4L+1
                                                                                                           (AF1C)   RTS

                                                                                        (AE6D)   LDY #0       ;Zero-out rtn code in
                                                                                                 STY RTNCODFM ;lst 2 signal no errs.
                                                                                        STORFMWK LDA (A4L),Y  ;Copy FM work buf
                                                                                                 STA FMWKAREA,Y ;(in chain) to FM
                                                                                                 INY          ;wrk area (not in
                                                                                                 CPY #45      ;DOS buf chain).
                                                                                                 BNE STORFMWK
                                                                                                 CLC          ;Why?????
                                                                                        (AE7D)   RTS
 
                                                                  (AB0D)   LDA OPCODEFM ;Chk if opcode is legal.
                                                                           CMP #13      ;(Must be less than 13.)
                                                                           BCS TOERROP  ;Opcode too large, got range error.
                                                                           ASL          ;Double val of opcode & get addr of
                                                                           TAX          ;appropriate function handler from tbl.
                                                                           LDA FMFUNCTB+1,X ;Put the adr on stack (hi byte first)
                                                                           PHA          ;& then do a "stack jump" to the appropriate
                                                                           LDA FMFUNCTB,X ;function handler.
                                                                           PHA
                                                                  (AB1E)   RTS

                                                                           .
                                                                           .
                                                                  (AC06)   .
                                                                  FNCLOSE  .
                                                                           .
                                                                  (See dis'mbly of CLOSE function.)
                                                                           .
                                                                           .
                                                                           - if necessary, free up any sectors that
                                                                             were previously allocated to the file
                                                                             but not needed.
                                                                           - also updates the file size, fixes up
                                                                             links & updates data, VTOC, T/S list
                                                                             & directory sectors if necessary.
                                                                           .
                                                                           .
                                                                           (RTS)
                                                                           ============

                                                                  (AB1F)
                                                                  TOERROP  JMP RNGERROP ;Go handle range error.
                                                                           ------------ ;(See dis'mbly of errors.)

                                            * Return here after doing the CLOSE FUNCTION.
                                            * (Cause after @ function is done, use stack
                                            * to get back to the original caller.)
                                            (A6AB)
                                            AFTRFUNC BCC FMDRVRTN ;(c) = 0 = no errors.
                                                     LDA RTNCODFM ;Get error code from FM parameter list.
                                                     CMP #$5      ;End-of-data error?
                                            (A6B2)   BEQ TOAPPTCH ;Yes - encountered a zeroed-out T/S link or
                                                                  ;a zeroed-out data pair in T/S list.
                                                                  ;(Not applicable to the close function.)
                                            (A6B4)   JMP OTHRERR  ;No - see dis'mbly of errors.
                                                     ------------

                                            (A6C3)
                                            FMDRVRTN RTS
         
                      (A2F9)   JMP CMDCLOSE ;Go back into CMDCLOSE to point A5L/H pointer
                               ------------ ;at the highest numbered (lowest in
                                            ;memory) free filename buffer in chain of DOS
                                            ;buffers & then exit CMDCLOSE via EVENTXIT & CLOSERTS.

                      (A330)
                      CLOSERTS RTS
                               ============

(A7D0)   JMP TYPMISM  ;See dis'mbly of errors.
         ------------ ;(Eventually goes into DOS's warm start routine.)

(A7D3)
CKTYPRTN RTS          ;Exit to caller of the open command.
         ============ ;Usually returns to AFTRCMD ($A17D) located
                      ;in the command parsing and processing routines.