~vdupras/duskos

18d3f30bc4df7070cbd1aa68cce850255a11fb40 — Virgil Dupras a month ago 86f2afb
fs/boot: implement fat16close
2 files changed, 15 insertions(+), 10 deletions(-)

M fs/fs/boot.fs
M fs/tests/fs/boot.fs
M fs/fs/boot.fs => fs/fs/boot.fs +13 -9
@@ 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! ;

M fs/tests/fs/boot.fs => fs/tests/fs/boot.fs +2 -1
@@ 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