-;--------------------------------------------------------------------------
-; fnparse: Parse a filename passed in in a/x. Will set the following
+.proc fnparsename
+
+ lda #0
+ sta tmp1 ; Remember length of name
+
+nameloop:
+ lda (ptr1),y ; Get next char from filename
+ beq namedone ; Jump if end of name reached
+
+; Check for valid chars in the file name. We allow letters, digits, plus some
+; additional chars from a table.
+
+ ldx #fncharcount-1
+namecheck:
+ cmp fnchars,x
+ beq nameok
+ dex
+ bpl namecheck
+ tax
+ lda __ctype,x
+ and #CT_ALNUM
+ beq invalidname
+
+; Check the maximum length, store the character
+
+nameok: ldx tmp1
+ cpx #16 ; Maximum length reached?
+ bcs invalidname
+ lda (ptr1),y ; Reload char
+ jsr fnadd ; Add character to name
+ iny ; Next char from name
+ inc tmp1 ; Increment length of name
+ bne nameloop ; Branch always
+
+; Invalid file name
+
+invalidname:
+ lda #33 ; Invalid file name
+
+; Done, we've successfully parsed the name.
+
+namedone:
+ rts
+
+.endproc
+
+
+;------------------------------------------------------------------------------
+; fnparse: Parse a full filename passed in in a/x. Will set the following