f_inc_next_cluster increments the next free cluster link _on disc_ in FSInfo. According to MS, this is in the second reserved sector in the reserved area at the beginning of the partition.
While I'm writing this, I don't want to write to the disc in case either (a) the disc write doesn't work properly or (b) my update code doesn't work properly, and in either case I end up trashing the disc. I suspect I'm going to be formatting and rewriting a lot of CF cards today...
Code: Select all
;-----------------------------------------------------------------------
; f_inc_next_cluster
;
; Increment the next free cluster link in FSInfo.
; Loads the FSInfo sector, reads the free cluster link, increments it,
; saves it to next_free_cluster, and writes the sector back to disc
; NOTE: does not yet check that the new cluster does not exceed the
; cluster count
; NOTE: saves and restores LBA
f_inc_next_cluster:
PUSHLBA ; save current directory
LYA partition_begin_lba
jsr u32_tolba
LYA one
jsr u32_add ; point to the FSInfo
; could check for valid signature here, but
; we'll trust it...
jsr cf_set_lba
jsr cf_read ; FSInfo is now in transient
; the cluster is at offset $01ec in transient
LYA transient+$1ec
jsr u32_tolba
; FIXME check cluster limit/disc size
LYA one
jsr u32_add ; increment it
LYA next_free_cluster
jsr u32_fromlba ; save it
LYA transient+$1ec
jsr u32_fromlba
LYA partition_begin_lba
jsr u32_tolba
LYA one
jsr u32_add ; point to the FSInfo again
;;;;;;;;;;;;;;;;;;;;
;jsr cf_write ; write it to the disc
;;;;;;;;;;;;;;;;;;;;
LYA next_free_cluster
jsr hexuint32
jsr crlf
POPLBA ; restore lba
rts
Code: Select all
;-----------------------------------------------------------------------
; f_create
;
; If the file does not exist in the current directory, create it as a
; zero length file.
; The file name is a zero terminated string point to by Y:A
; The current directory sector is in lba on entry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; FIXME disc writes disabled
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
f_create:
; step one: check the file doesn't exist
pha ; FIXME debug
phy
jsr puts ; show the string
ply
pla
jsr crlf
jsr str_to_83 ; get the 8.3 version
jsr fs_find_first ; seek it
f_cr_01:
lda (fs_dir_ptr) ; check the first character of name
beq f_cr_03 ; quit if it's zero
jsr fs_match_83
bne f_cr_02
; the filename exists: quit with zero flag set
rts ; FIXME one entry point one exit...
f_cr_02:
jsr fs_find_next ; not found yet
bra f_cr_01 ; until no more records
f_cr_03:
; if we get here, the file does not exist so we can progress
; step two: increment the pointer in FSInfo sector
jsr f_inc_next_cluster ; also sets next_free_cluster
SHOWTRANS
; step three: get the appropriate FAT sector into transient
jsr clus_to_fat_rec ; preloads lba and fat_record
jsr cf_set_lba
jsr cf_read
SHOWTRANS
; step four: allocate the new cluster with end-of-cluster marker
ldy #0
lda #$ff
sta (fat_record),y ; (fat_record) is record in transient
iny
sta (fat_record),y
iny
sta (fat_record),y
iny
lda #$0f
sta (fat_record),y ; that's $0FFFFFFF written
SHOWTRANS
; step five: save transient to the first FAT (sector still in lba)
;;;;;;;;;;;;;;;;;;;;
;jsr cf_write ; write it to the disc
;;;;;;;;;;;;;;;;;;;;
; step six: save transient to second FAT to keep synchronised
LYA sectors_per_fat
jsr u32_add ; by adding sectors per fat
jsr cf_set_lba
;;;;;;;;;;;;;;;;;;;;
;jsr cf_write ; write it to the disc
;;;;;;;;;;;;;;;;;;;;
; step seven: find a place for the new directory entry
jsr crlf
lda #1
rts
Neil