# Sector window

A sector window (secwin) is a buffer referencing one or more sectors and holding
one sector in memory. It manages the "dirty" status of the buffer and makes it
easy to seek within the window, automatically managing buffer status.

For example, one could open a secwin of 4 512b sectors, starting at sector $2a.
The buffer would begin holding sector $2a in its buf(. The user could seek
relative address $212 within it, which would make it select sector $2b and
return address $12 relative to buf(. If the dirty flag is set, the buffer
ensures that the contents is saved before loading a new one.

Fields:

drv     Target drive [doc/io/drive]
fsec    First sector of the window
cnt     Number of sectors in the window
sec     Sector currently in buffer
dirty   Whether the buffer is dirty

Words:

newsecwin ( drv -- secwin )
  Create a new buffer targeting drive "drv", on sector 0, cnt 1. Current buffer
  starts uninitialized.

buf( ( secwin -- a )
  Address of the buffer.

)buf ( secwin -- a )
  End of the buffer.

reframe ( fsec cnt secwin -- )
  Move window to "fsec" with "cnt" sectors. This doesn't change or flush the
  current buffer.

fulldrv ( secwin -- )
  Move the window so that it spans the whole drive.

load ( sec secwin -- )
  Load sector "sec" in the buffer. Aborts if outside the window.

flush ( secwin -- )
  If buffer is dirty, save it.

seek ( pos secwin -- a? n-or-0 )
  Load, if needed, the correct sector so that the position "pos", in bytes
  relative to the beginning of "fsec", is available in the buffer. Returns its
  address in "a" as well as the number of bytes that can be read before the end
  of the buffer is reaches, as "n". If "pos" is outside the window, "n" is 0 and
  "a" is absent.

nextsector ( secwin -- a? n-or-0 )
  Seek the first byte of the sector following the one currently in the buffer.

dirty! ( secwin -- )
  Make the buffer dirty.
