From 18d3f30bc4df7070cbd1aa68cce850255a11fb40 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Fri, 1 Jul 2022 13:59:59 -0400 Subject: [PATCH] fs/boot: implement fat16close --- fs/fs/boot.fs | 22 +++++++++++++--------- fs/tests/fs/boot.fs | 3 ++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/fs/fs/boot.fs b/fs/fs/boot.fs index 9c0adf0e..738f2ac8 100644 --- a/fs/fs/boot.fs +++ b/fs/fs/boot.fs @@ -86,7 +86,7 @@ create fnbuf FNAMESZ allot 2drop ; \ File cursor -\ 2b first cluster +\ 2b first cluster 0=free cursor \ 2b current cluster in buf \ 4b cur pos (offset from beginning of file) \ 4b file size @@ -102,18 +102,20 @@ create fnbuf FNAMESZ allot : FCUR_size ( fcur -- n ) 8 + @ ; : FCUR_buf( ( fcur -- a ) 12 + ; -create fcursors( FCursorSize FCURSORCNT * allot -here value )fcursor -fcursors( value nextfcursor +create fcursors( FCursorSize FCURSORCNT * allot0 + +: findfreecursor ( -- fcursor ) + FCURSORCNT >r fcursors( begin ( a ) + dup FCUR_cluster0 not if ( found! ) r~ exit then FCursorSize + next + abort" out of file cursors!" ; \ Open the specified "direntry" into one of the free cursors and return that \ cursor. : openfile ( direntry -- fcursor ) - nextfcursor )fcursor = if abort" out of file cursors!" then - dup DIR_Cluster ( dirent cluster ) dup nextfcursor FCUR_buf( readcluster - ( dirent cluster ) dup nextfcursor w! nextfcursor FCUR_cluster! ( dirent ) - 0 nextfcursor 4 + ! DIR_FileSize nextfcursor 8 + ! ( ) - nextfcursor FCursorSize to+ nextfcursor ( fcursor ) ; + findfreecursor >r + dup DIR_Cluster ( dirent cluster ) dup r@ FCUR_buf( readcluster + ( dirent cluster ) dup r@ w! r@ FCUR_cluster! ( dirent ) + 0 r@ 4 + ! DIR_FileSize r@ 8 + ! ( ) r> ; : fat16getc ( fcursor -- c-or-0 ) dup FCUR_pos over FCUR_size = if drop 0 exit then @@ -125,3 +127,5 @@ fcursors( value nextfcursor rot 2dup FCUR_cluster! ( c cluster fc ) tuck FCUR_buf( readcluster ( c fc ) swap then then ( fc c ) nip ; + +: fat16close ( fcursor ) 0 swap w! ; diff --git a/fs/tests/fs/boot.fs b/fs/tests/fs/boot.fs index ade09af0..a8d8fb9f 100644 --- a/fs/tests/fs/boot.fs +++ b/fs/tests/fs/boot.fs @@ -19,5 +19,6 @@ dup $fd readN ( fcursor ) dup fat16getc 'b' #eq dup $dfc readN ( fcursor ) dup fat16getc 'E' #eq dup fat16getc 'O' #eq dup fat16getc 'F' #eq -fat16getc 0 #eq +dup fat16getc 0 #eq +fat16close testend -- 2.45.2