	.TITLE "DOS VERSION NUMBER MODIFICATION"
	*= $1A7C
; *** ORG = $1DOO FOR OS/A+ ***
;
; *** DOS ENTRY POINTS ***
XDEL0	= $0C53 	;DELETE FILE
SFDIR	= $0F21 	;SEARCH DIR
CSFDIR	= $0F31 	;SEARCH CONTINUE
RDDIR	= $106E 	;READ DIRECTORY
WRTDIR	= $1071 	;WRITE DIRECTORY
WRVTOC	= $1095 	;WRITE VTOC
;
; *** DOS STORAGE LOCATIONS ***
CDIRD	= $1305 	;CUR DIR SECTOR
CDIRS	= $1306 	;CUR DIR DISPL.
SFNUM	= $1307 	;FILE NUMBER
FNAME	= $1359 	;FILE NAME
FCBOTC	= $1382 	;FCB OPEN TYPE
FILDIR	= $1401 	;DIR BUFFER
;
; *** MY STORAGE AREAS ***
;     TABEL FOR CURRENT FILES.
;      LAYOUT:
;BYTE 1 - VERSION NUMBER (BCD)
;BYTE 2 - DIRECTORY SECTOR
;BYTE 3 - DIRECTORY DISPLACEMENT
;BYTE 4 - FILE NUMBER
TABLE	.BYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
MAXVER	.BYTE 3 	;MAXIMUM NUMBER
;   OF VERSIONS (2 - 8)
CTABD	= $1372 	;CURRENT TABLE
;   DISPLACEMENT
NUMNAME	= $1373 	;NUMBER OF FILES
TEMPX	= $1374 	;X HOLDING PLACE
TMPCNT	= $1375 	;TEMP COUNTER
CMDD8	= $1376 	;OPEN TYPE/8
;
;*** MISCELLANEOUS EQUATES ***
OPOUT	= $08 	;OPEN FOR OUTPUT
OPIN	= $04 	;OPEN FOR INPUT
;
;START OF CODE	;!!!!!!!! INVERSE CHARS !!!!!!!!
;
;INITIALIZE, THEN CHECK FOR
;THE PROPER FILE NAME. THE
;X REGISTER CONTAINS THE FCB
;NUMBER, AND MUST BE PRESERVED
;THROUGH THIS ROUTINE
;
	LDA #$00 	;INITIALIZE:
	STA CTABD 	;DISPLACEMENT
	STA NUMNAME 	;NUMBER OF FILES
	LDY #31
CLRTABLP
	STA TABLE,Y 	;AND TABLE (8
	DEY  	;4-BYTE ENTRIES)
	BPL CLRTABLP
;
	LDA FNAME+8 	;IF EXTENDER NOT
	CMP #'V 	; = 'V  '
	BNE NOTAPP 	;THEN LEAVE
	LDA FNAME+9
	CMP #$20
	BNE NOTAPP
	LDA FNAME+10
	CMP #$20
	BNE NOTAPP
;
	LDA FCBOTC,X
	CMP #OPIN 	;IF OPEN FOR
	BEQ IOCOMND 	;INPUT OR OUTPUT
	CMP #OPOUT 	;THEN CONTINUE
	BEQ IOCOMND
NOTAPP
	JMP SREXIT 	;ELSE LEAVE
;
;PROPER FILE TO DO THIS ROUTINE
;
IOCOMND
	STX TEMPX 	;SAVE X REGISTER
	LDA #'? 	;PUT WILDCARDS
	STA FNAME+9 	;OVER SPACES
	STA FNAME+10
	JSR SFDIR 	;SEARCH FOR FILE
	BCS NOMORE 	;NO SUCH FILE
;
;ENTER ALL FILES OF THE TYPE
; 'NAME.V??' INTO THE TABLE
;
SRCHLP
	LDY CDIRD 	;GET THE TWO
	LDX CTABD 	;DISPLACEMENTS
	LDA FILDIR+14,Y 	;FETCH NAME
	ASL A 	;EXTENSION AND
	ASL A 	;CHANGE LAST TWO
	ASL A 	;DIGITS TO BCD
	ASL A 	;FORMAT
	STA TABLE,X 	;HIGH DIGIT
	LDA FILDIR+15,Y
	AND #$0F 	;AND LOW DIGIT
	ORA TABLE,X
	STA TABLE,X 	;PUT IN TABLE
	LDA CDIRS 	;SAVE DIR SECTOR
	STA TABLE+1,X 	;AND
	TYA  	;AND DISPLACEMENT
	STA TABLE+2,X
	LDA SFNUM 	; & FILE NUMBER
	STA TABLE+3,X 	;FOR TABLE
	CLC  	;
	LDA CTABD 	;ADD FOUR TO
	ADC #$04 	;THE TABLE
	STA CTABD 	;DISPLACEMENT.
	INC NUMNAME 	;
SKIPENTRY
	JSR CSFDIR 	;IF MORE FILES
	BCC SRCHLP 	; THEN GO BACK
;
;WHEN NO MORE FILES, SEE IF WE
;NEED TO DELETE ANY
;
NOMORE
	LDX TEMPX 	;IF OPEN TYPE =
	LDA FCBOTC,X 	; INPUT THEN
	CMP #OPIN 	;  SKIP OVER
	BEQ GETHINAME 	;SEARCH
TOOMANY?
	LDA NUMNAME 	;IF NUMNAME <
	CMP MAXVER 	;  MAXVER THEN
	BCC GETHINAME 	;DON'T DELETE
	STA TMPCNT 	;SAVE NUMNAME
;
;SEARCH FOR EARLIEST VERSION
;
;NUMBER IN TABLE
	LDY #$04 	;CURRENT ENTRY
	LDX #$00 	;SMALLEST ENTRY
SEARCHLOW
	DEC TMPCNT 	;IF NO MORE
	BEQ OUTSRCHL 	;THEN OUT
	LDA TABLE,X 	;ELSE IF CURRENT
	CMP TABLE,Y 	; > SMALLEST
	BCC NOTLOW 	; THEN SKIP
	TYA  	;ELSE MOVE
	TAX  	;CURRENT TO MIN
NOTLOW
	INY  	;POINT TO NEXT
	INY  	;TABLE ENTRY
	INY 
	INY  	;GOTO SEARCHLOW
	JMP SEARCHLOW
OUTSRCHL
;
;POINTER TO EARLIEST VERSION IS
;IN X REGISTER, SO DELETE IT
;
	JSR PTABFNM 	;RETRIEVE NAME
	JSR PULLDOWN 	;PATCH TABLE
	LDX TEMPX 	;GET FCB #
	JSR RDDIR 	;READ DIRECTORY
	JSR XDEL0 	;FREE SECTORS
	JSR WRTDIR 	;WRITE DIRECTORY
	JSR WRVTOC 	;WRITE VTOC
	DEC NUMNAME 	;ONE LESS NAME
	JMP TOOMANY? 	;CHECK MORE
;
;FIND LATEST CURRENT FILE
;
GETHINAME
	LDY #$00 	;MAXIMUM ENTRY
	LDX #$00 	;CURRENT ENTRY
	LDA NUMNAME 	;GET NUMBER OF
	STA TMPCNT 	;ENTRIES
SERCHHI
	LDA TABLE,X 	;IF CURRENT <
	CMP TABLE,Y 	;     MAXIMUM
	BCC NOTHI 	; THEN BRANCH
	TXA  	;ELSE MOVE X TO
	TAY  	;Y BY WAY OF A
NOTHI
	INX  	;POINT TO NEXT
	INX  	;ENTRY AND
	INX  	;DECREMENT THE
	INX  	;COUNTER
	DEC TMPCNT
	LDA TMPCNT 	;IF COUNTER >= 0
	BPL SERCHHI 	;  THEN GO BACK
;
;GET OPEN TYPE (4 OR 8) AND
;DIVIDE BY 8 TO GET ZERO OR 1
;
	LDX TEMPX 	;CURRENT FCB
	LDA FCBOTC,X
	LSR A 	;SHIFT RIGHT
	LSR A 	;THREE TIMES
	LSR A
	CLC  	;ADD TO VERSION
	SED  	;NUMBER IN BCD
	ADC TABLE,Y 	;MODE. IF OPEN
	STA TABLE,Y 	;FOR INPUT, ZERO
	CLD  	;IS ADDED, ONE
	TYA  	;FOR OUTPUT
	TAX  	;MOVE TO X REG
	JSR PTABFNM 	;GET INFORMATION
	LDX TEMPX 	;RESTORE X REG
SREXIT
	JSR SFDIR 	;SEARCH FOR FILE
	RTS  	;TO PLEASE DOS
;
;SET VERSION NUMBER IN FNAME
;AND SET OTHER DOS VARIABLES
;ACCORDING TO OUR TABLE. X
;HAS OFFSET TO TABLE ENTRY
;
PTABFNM
	LDA TABLE,X 	;GET BCD VERSION
	AND #$0F 	;NUMBER AND
	ORA #$30 	;CHANGE BACK T0
	STA FNAME+10 	;TWO DIGITS
	LDA TABLE,X
	LSR A
	LSR A
	LSR A
	LSR A
	ORA #$30 	;STORE IT IN
	STA FNAME+9 	;FNAME BUFFER
	LDA TABLE+1,X
	STA CDIRS 	;RESTORE SECTOR,
	LDA TABLE+2,X
	STA CDIRD 	; DISPLACEMENT,
	LDA TABLE+3,X
	STA SFNUM 	; AND NUMBER
	RTS 
;
;CONDENSE TABLE TO ELIMINATE
;THE DELETED FILE. X REGISTER
;CONTAINS OFFSET TO TABLE
;
PULLDOWN
	LDA #$00 	;STORE ZERO IF
	CPX #28 	;LAST ENTRY ELSE
	BCS SKIPNEXT
	LDA TABLE+4,X 	;GET NEXT
SKIPNEXT
	STA TABLE,X 	;INTO CURRENT
	INX  	;ENTRY
	CPX #32 	;UNTIL END OF
	BNE PULLDOWN 	;THE TABLE
	RTS 
;
	.END 
