' Config File-System:

' User Settings ===============================================================

Const cFileHandles = 3                  ' Count of file handles
Const cSepFATHandle = 0                 ' separate FAT Handle buffer

' End of User Setting ==========================================================



' Variables for AVR-DOS =======================================================

' FileSystem Basis Informationen
Dim glDriveSectors as Long
Dim gbDOSError as Byte

' Master Boot Record
Dim gbFileSystem as Byte

' Partition Boot Record
Dim glFATFirstSector as Long
Dim gbNumberOfFATs as Byte
Dim gwSectorsPerFat as Word
Dim glRootFirstSector as Long
Dim gwRootEntries as Word
Dim glDataFirstSector as Long
Dim gbSectorsPerCluster as Byte
Dim gwMaxClusterNumber as Word

' Additional info
Dim gwLastSearchedCluster as Word
Dim gwFreeDirEntry as Word
Dim glFS_Temp1 as Long                  ' for temporary purpose
Dim gsTempFileName as String * 11
' Block für Directory Handling
Dim gwDirRootEntry as Word
Dim glDirSectorNumber as Long
Dim gbDirBufferStatus as Byte
Dim gbDirBuffer(512) as Byte
Const c_FileSystemSRAMSize1 = 561
#IF cSepFATHandle = 1
Dim glFATSectorNumber as Long
Dim gbFATBufferStatus as Byte
Dim gbFATBuffer(512) as Byte
Const c_FileSystemSRAMSize2 = 517
#ELSE
Const c_FileSystemSRAMSize2 = 0
#ENDIF

' File Handle Block
Const co_FileNumber = 0
Const co_FileMode = 1
Const co_FileRootEntry = 2 : Const co_FileRootEntry_2 = 3
Const co_FileFirstCluster = 4 : Const co_FileFirstCluster_2 = 5
Const co_FATCluster = 6 : Const co_FATCluster_2 = 7
Const co_FileSize = 8 : Const co_FileSize_2 = 9 : Const co_FileSize_3 = 10
       Const co_FileSize_4 = 11 : Const co_File_Size_Next = 12
Const co_FilePosition = 12 : Const co_FilePosition_2 = 13 : Const co_FilePosition_3 = 14
       Const co_FilePosition_4 = 15 : Const co_FilePosition_next = 16
Const co_FileSectorNumber = 16 : Const co_FileSectorNumber_2 = 17 : Const co_FileSectorNumber_3 = 18
       Const co_FileSectorNumber_4 = 19
Const co_FileBufferStatus = 20
Const co_FileBuffer = 21
Const c_FileHandleSize = co_FileBuffer + 513       ' incl. one Additional Byte for 00 as string terminator
                                                   ' for direct text reading from File-buffer
Const c_FileHandleSize_m = 65536 - c_FileHandleSize       ' for use with add immediate word with subi, sbci
                                                          ' minus c_FileHandleSize in Word-Format

Const c_FileHandlesSize = c_FileHandleSize * cFileHandles
Dim abFileHandles(c_FileHandlesSize) as Byte
Const c_FileSystemSRAMSize = c_FileSystemSRAMSize1 + c_FileSystemSRAMSize2 + c_FileHandlesSize

' End of variables for AVR-DOS ================================================

' Definitions of Constants ====================================================

' Bit Definitions for BufferStatus (FAT, DIR, File)

dBOF alias 0 : Const dBOF = 0 : Const dmBOF =(2 ^ dBOF)
dEOF alias 1 : Const dEOF = 1 : Const dmEOF =(2 ^ dEOF)
dEOFinSector alias 2 : Const dEOFinSector = 2 : Const dmEOFinSector =(2 ^ dEOFInSector)
dWritePending alias 3 : Const dWritePending = 3 : Const dmWritePending =(2 ^ dWritePending)
dFATSector alias 4 : Const dFATSector = 4       ' For Writing Sector back (FATNumber times)
dFileEmpty alias 5 : Const dFileEmpty = 5 : Const dmFileEmpty =(2 ^ dFileEmpty)

Const dmEOFAll =(2 ^ dEOF + 2 ^ dEOFinSector)
Const dmEOF_Empty =(2 ^ dEOF + 2 ^ dEOFinSector + 2 ^ dFileEmpty)

Const cp_FATBufferInitStatus =(2 ^ dFatSector)
Const cp_DirBufferInitStatus = 0
Const cp_FileBufferInitStatus = 0


' Bit definitions for FileMode (Similar to DOS File Attribut)
dReadOnly alias 0 : Const dReadOnly = 0
'Const cpFileReadOnly = &H21             ' Archiv and read-only Bit set
Const cpFileWrite = &H20                ' Archiv Bit set


' Error Codes

' Group Number is upper nibble of Error-Code
' Group 0 (0-15): No Error or File End Information
Const cpNoError = 0
Const cpEndOfFile = 1

' Group 1 (17-31): File System Init
Const cpNoMBR = 17
Const cpNoPBR = 18
Const cpFileSystemNotSupported = 19
Const cpSectorSizeNotSupported = 20
Const cpSectorsPerClusterNotSupported = 21

' Group 2 (32-47): FAT - Error
Const cpNoNextCluster = 33
Const cpNoFreeCluster = 34
Const cpClusterError = 35
' Group 3 (49-63): Directory Error
Const cpNoFreeDirEntry = 49
Const cpFileExists = 50
' Group 4 (65-79): File Handle
Const cpNoFreeFileNumber = 65
Const cpFileNotFound = 66
Const cpFileNumberNotFound = 67
Const cpFileOpenNoHandle = 68
Const cpFileOpenHandleInUse = 69
Const cpFileOpenShareConflict = 70
Const cpFileInUse = 71
Const cpFileReadOnly = 72
Const cpFileNoWildCardAllowed = 73
Const cpFileNumberInValid = 74          ' Zero is not allowed

' Group 7 (97-127): other errors
Const cpFilePositionError = 97
Const cpFileAccessError = 98
Const cpInvalidFilePosition = 99
Const cpFileSizeToGreat = 100


' Range 224 to 255 is reserved for Driver

' Other Constants
' File Open Mode /  stored in File-handle return-value of Fileattr(FN#, [1])
Const cpFileOpenInput = 1               ' Read
Const cpFileOpenOutput = 2              ' Write sequential
'Const cpFileOpenRandom = 4              ' not in use yet
Const cpFileOpenAppend = 8              ' Write sequential; first set Pointer to end
Const cpFileOpenBinary = 32             ' Read and Write; Pointer can be changed by user


' permission Masks for file access routine regarding to the file open mode
Const cFileWrite_Mode = &B00101010      ' Binary, Append, Output
Const cFileRead_Mode = &B00100001       ' Binary, Input
Const cFileSeekSet_Mode = &B00100000    ' Binary
Const cFileInputLine = &B00100001       ' Binary, Input
Const cFilePut_Mode = &B00100000        ' Binary
Const cFileGet_Mode = &B00100000        ' Binary

' Directory attributs in FAT16
Const cpFileOpenAllowed = &B00100001    ' Read Only and Archiv may be set
' Bit 0 = Read Only
' Bit 1 = Hidden
' Bit 2 = System
' Bit 3 = Volume ID
' Bit 4 = Directory
' Bit 5 = Archiv
' Long File name has Bit 0+1+2+3 set

$LIB "AVR-DOS.LBX"