Subversion Repositories NedoOS

Rev

Rev 8 | Rev 119 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. ; Эдесь лежат макросы FatFs.
  2. ; Все аргументы передаются через стек, в порядке их перечисления.
  3. ; Длинна аргумента кратна двум, то есть, если аргумент типа BYTE,
  4. ; то он занимает на стеке два байта.
  5. ; Если аргумент типа DWORD, то на стек сначала кладем старшие два байта, затем младшие.
  6. ; После выполнения функции все аргументы остаются на стеке,
  7. ; не забывайте снимать ненужные (за исключением f_voltopart).
  8. ;
  9. ; При попадании в функцию на стеке должен быть адрес возврата, затем список переменных.
  10. ;
  11. ; Строковая переменная должна заканчиватся 0x0 (нулём).
  12. ; Можно использовать как абсолютный, так и относительный путь:
  13. ;       "file.txt"              A file in the current directory of the current drive
  14. ;       "/file.txt"             A file in the root directory of the current drive
  15. ;       ""                              The current directory of the current drive
  16. ;       "/"                             The root directory of the current drive
  17. ;       "2:"                    The current directory of the drive 2
  18. ;       "2:/"                   The root directory of the drive 2
  19. ;       "2:file.txt"    A file in the current directory of the drive 2
  20. ;       "../file.txt"   A file in the parent directory
  21. ;       "."                             This directory
  22. ;       ".."                    Parent directory of the current directory
  23. ;       "dir1/.."               The current directory
  24. ;       "/.."                   The root directory (sticks the top level)
  25.  
  26. ; Возвращаемый параметр лежит в HL, если DWORD то DEHL.
  27. ;
  28. ; Левые порты должны быть скрыты (MEM_HIDE)
  29. ; Стек должен быть в текущем адресном пространстве (юзается очень сильно байт 100-200
  30. ; свободно может заюзать). И переменные(если на них есть указатель в
  31. ; аргументах, а также глобальные переменные типа FATFS), тоже должны быть доступны.
  32.  
  33. ;------------------------СТРУКТУРЫ FATFS --------------------------------------
  34.  
  35. FA_READ=0x01                    ;Specifies read access to the object. Data can be read from the file.
  36.                                         ;Combine with FA_WRITE for read-write access.
  37. FA_WRITE=0x02                   ;Specifies write access to the object. Data can be written to the file.
  38.                                         ;Combine with FA_READ for read-write access.
  39. FA_OPEN_EXISTING=0x00   ;Opens the file. The function fails if the file is not existing. (Default)
  40. FA_OPEN_ALWAYS=0x10     ;Opens the file if it is existing. If not, a new file is created.
  41.                                         ;To append data to the file, use f_lseek function after file open in this method.
  42. FA_CREATE_NEW=0x04              ;Creates a new file. The function fails with FR_EXIST if the file is existing.
  43. FA_CREATE_ALWAYS=0x08   ;Creates a new file. If the file is existing, it is truncated and overwritten.
  44.  
  45.  
  46.  
  47. /* File system object structure (FATFS) */
  48.  
  49.         STRUCT FATFS
  50. fs_type         BYTE;           /* FAT sub-type (0:Not mounted) */
  51. drv             BYTE 0;         /* Physical drive number */
  52. part            BYTE 0;         /* Partition # (0-4) 0-auto*/
  53. csize           BYTE;           /* Sectors per cluster (1,2,4...128) */
  54. n_fats          BYTE;           /* Number of FAT copies (1,2) */
  55. wflag           BYTE;           /* win[] dirty flag (1:must be written back) */
  56. fsi_flag        BYTE;           /* fsinfo dirty flag (1:must be written back) */
  57. id              WORD;           /* File system mount ID */
  58. n_rootdir       WORD;           /* Number of root directory entries (FAT12/16) */
  59. last_clust      DWORD;          /* Last allocated cluster */
  60. free_clust      DWORD;          /* Number of free clusters */
  61. fsi_sector      DWORD;          /* fsinfo sector (FAT32) */
  62. cdir            DWORD;          /* Current directory start cluster (0:root) */ ;TODO use
  63. n_fatent        DWORD;          /* Number of FAT entries (= number of clusters + 2) */
  64. fsize           DWORD;          /* Sectors per FAT */
  65. fatbase         DWORD;          /* FAT start sector */
  66. dirbase         DWORD;          /* Root directory start sector (FAT32:Cluster#) */
  67. database        DWORD;          /* Data start sector */
  68. winsect         DWORD;          /* Current sector appearing in the win[] */
  69. win             BLOCK 512;      /* Disk access window for Directory, FAT (and Data on tiny cfg) */
  70.         ENDS
  71. FATFS_sz=51+512
  72.  
  73. ;/* Directory object structure (DIR) */
  74.         STRUCT DIR
  75. FS              WORD    ;/* POINTER TO THE OWNER FILE SYSTEM OBJECT */
  76. ID              WORD    ;/* OWNER FILE SYSTEM MOUNT ID */
  77. INDEX           WORD    ;/* CURRENT READ/WRITE INDEX NUMBER */
  78. SCLUST          DWORD   ;/* TABLE START CLUSTER (0:ROOT DIR) */ ;видимо, в том же формате, что FATFS.cdir
  79. CLUST           DWORD   ;/* CURRENT CLUSTER */
  80. SECT            DWORD   ;/* CURRENT SECTOR */
  81. DIR             WORD    ;/* POINTER TO THE CURRENT SFN ENTRY IN THE WIN[] */
  82. FN              WORD    ;/* POINTER TO THE SFN (IN/OUT) {FILE[8],EXT[3],STATUS[1]} */
  83.         ENDS
  84. DIR_sz=22
  85.  
  86. /* FILE STATUS STRUCTURE (FILINFO) */
  87.         STRUCT FILINFO
  88. FSIZE           DWORD           ;/* FILE SIZE */
  89. FDATE           WORD            ;/* LAST MODIFIED DATE */
  90. FTIME           WORD            ;/* LAST MODIFIED TIME */
  91. FATTRIB         BYTE            ;/* ATTRIBUTE */
  92. FNAME           BLOCK 13,0      ;/* SHORT FILE NAME (8.3 FORMAT) */
  93. ;MARK           byte            ;marking
  94. ;NEXT           WORD
  95. ;NEXTP          BYTE
  96. ;PREV           WORD
  97. ;PREVP          BYTE
  98. ;RESERV         dw
  99.         ENDS
  100.  
  101. /* File object structure (FIL) */
  102.  
  103.         struct FIL
  104. FS              WORD    ;/* Pointer to the owner file system object */
  105. ID              WORD    ;/* Owner file system mount ID */
  106. FLAG            BYTE    ;/* File status flags */
  107. PAD1            BYTE    ;;TODO добавить тут id приложения-хозяина, чтобы можно было автоматически удалить
  108. FPTR            DWORD   ;/* File read/write pointer (0 on file open) */
  109. FSIZE           DWORD   ;/* File size */
  110. FCLUST          DWORD   ;/* File start cluster (0 when fsize==0) */
  111. CLUST           DWORD   ;/* Current cluster */
  112. DSECT           DWORD   ;/* Current data sector */
  113. DIR_SECT        DWORD   ;/* Sector containing the directory entry */
  114. DIR_PTR         WORD    ;/* Ponter to the directory entry in the window */
  115. BUF             BLOCK 512       ;/* File data read/write buffer */
  116.         ENDS
  117. FIL_sz=32+512
  118.  
  119.  
  120. ;---------------------------------МАКРОСЫ--------------------------------------
  121.  
  122.         MACRO F_VOLTOPART _VOL,_DRIVE,_PART     ; Это типа виртуального предмонтирования, что ли.
  123.         ; BYTE VOL - под каким номером монтировать (0-3).
  124.         ; BYTE DRIVE - физическое устройство(0-ZSD,1-NEMO master,2-NEMO slave).
  125.         ; BYTE PART - номер раздела(0-3).
  126.         LD HL,_VOL              ;Кидаем на стек аргументы
  127.         PUSH HL
  128.         LD L,_DRIVE
  129.         PUSH HL
  130.         LD L,_PART
  131.         PUSH HL
  132.         LD A,4                  ; номер функции, кстати она исключение из правил, на стеке
  133.         call ffs                        ; остаётся только VOL, а он как раз пригодится нам в F_MOUNT
  134.         ENDM
  135.  
  136. ;/*-----------------------------------------------------------------------*/
  137. ;/* Mount/Unmount a Logical Drive                                         */
  138. ;/*-----------------------------------------------------------------------*/
  139. ;
  140. ;FRESULT f_mount (
  141. ;       BYTE vol,               /* Logical drive number to be mounted/unmounted */
  142. ;       FATFS *fs               /* Pointer to new file system object (NULL for unmount)*/
  143. ;)
  144.         MACRO F_MOUNT _VOL,_FS  ;vol - уже лежит на стеке.
  145.         ld e,_VOL
  146.         LD bc,_FS
  147.         F_MNT
  148.         ENDM
  149.         MACRO F_MNT
  150.         LD A,0
  151.         call ffs
  152.         ENDM
  153.  
  154.  
  155. ; FRESULT f_open (
  156.         ; FIL *fp,                      /* Pointer to the blank file object */
  157.         ; TCHAR *path,  /* Pointer to the file name */
  158.         ; BYTE mode                     /* Access mode and file open mode flags */
  159. ; )
  160.         MACRO F_OPEN _FP,_PATH,_MODE
  161.         LD de,_FP
  162.         LD bc,_PATH
  163.         LD HL,_MODE
  164.         F_OP
  165.         ENDM
  166.         MACRO F_OP
  167.         PUSH HL
  168.         LD A,1
  169.         call ffs
  170.         POP BC
  171.         ENDM
  172.  
  173.  
  174. ;seek
  175. ;FRESULT f_lseek (
  176. ;       FIL *fp,                /* Pointer to the file object */
  177. ;       DWORD ofs               /* File pointer from top of file */
  178. ;)
  179. ;fp = de
  180. ;ofs in stack
  181.         MACRO F_LSEEK
  182.         LD A,3
  183.         call ffs
  184.         ENDM
  185.  
  186.        
  187. ;/*-----------------------------------------------------------------------*/
  188. ;/* Read File                                                             */
  189. ;/*-----------------------------------------------------------------------*/
  190. ;FRESULT f_read
  191. ;       FIL *fp,                /* Pointer to the file object */
  192. ;       void *buff,             /* Pointer to data buffer */
  193. ;       UINT btr,               /* Number of bytes to read */
  194. ;       UINT *br                /* Pointer to number of bytes read */
  195.         MACRO F_READ
  196.         LD A,2
  197.         call ffs
  198.         ENDM
  199.        
  200.        
  201. ;/*-----------------------------------------------------------------------*/
  202. ;/* Write File                                                            */
  203. ;/*-----------------------------------------------------------------------*/
  204. ;FRESULT f_write
  205. ;       FIL *fp,                        /* Pointer to the file object */
  206. ;       const void *buff,       /* Pointer to the data to be written */
  207. ;       UINT btw,                       /* Number of bytes to write */
  208. ;       UINT *bw                        /* Pointer to number of bytes written */
  209.         MACRO F_WRITE
  210.         LD A,8
  211.         call ffs
  212.         ENDM
  213.  
  214. ; /*-----------------------------------------------------------------------*/
  215. ; /* Close File                                                            */
  216. ; /*-----------------------------------------------------------------------*/
  217. ; FRESULT f_close (
  218.         ; FIL *fp               /* Pointer to the file object to be closed */)
  219.         MACRO F_CLOSE _FP
  220.         ld de,_FP
  221.         F_CLOS
  222.         ENDM
  223.         MACRO F_CLOS
  224.         LD A,4
  225.         call ffs
  226.         ENDM
  227.  
  228.  
  229.  
  230. ; /*-----------------------------------------------------------------------*/
  231. ; /* Create a Directory Object                                             */
  232. ; /*-----------------------------------------------------------------------*/
  233. ; FRESULT f_opendir
  234.         ; DIR *dj,                      /* Pointer to directory object to create */
  235.         ; TCHAR *path   /* Pointer to the directory path */
  236.         MACRO F_OPENDIR _DJ;,_PATH
  237.                 LD de,_DJ
  238.                 ;LD bc,_PATH
  239.                 F_OPDIR
  240.         ENDM
  241.         MACRO F_OPDIR
  242.         LD A,5
  243.         call ffs
  244.         ENDM
  245.  
  246. ; /*-----------------------------------------------------------------------*/
  247. ; /* Read Directory Entry in Sequence                                      */
  248. ; /*-----------------------------------------------------------------------*/
  249.  
  250. ; FRESULT f_readdir (
  251.         ; DIR *dj,                      /* Pointer to the open directory object */
  252.         ; FILINFO *fno          /* Pointer to file information to return */
  253. ; )
  254.         MACRO F_READDIR _DJ,_FNO
  255.                 LD de,_DJ
  256.                 LD bc,_FNO
  257.                 F_RDIR
  258.         ENDM
  259.         MACRO F_RDIR
  260.         LD A,6
  261.         call ffs
  262.         ENDM
  263.  
  264.  
  265. ;/*-----------------------------------------------------------------------*/
  266. ;/* Delete a File or Directory                                            */
  267. ;/*-----------------------------------------------------------------------*/
  268. ;FRESULT f_unlink
  269. ;       const TCHAR *path               /* Pointer to the file or directory path */
  270.         MACRO F_UNLINK
  271.         LD A,12
  272.         call ffs
  273.         ENDM
  274. ;/*-----------------------------------------------------------------------*/
  275. ;/* Create a Directory                                                    */
  276. ;/*-----------------------------------------------------------------------*/
  277. ;FRESULT f_mkdir
  278. ;       const TCHAR *path               /* Pointer to the directory path */
  279.         MACRO F_MKDIR
  280.         LD A,13
  281.         call ffs
  282.         ENDM
  283. ;/*-----------------------------------------------------------------------*/
  284. ;/* Rename File/Directory                                                 */
  285. ;/*-----------------------------------------------------------------------*/
  286. ;FRESULT f_rename
  287. ;       const TCHAR *path_old,  /* Pointer to the old name */
  288. ;       const TCHAR *path_new   /* Pointer to the new name */
  289.  
  290.         MACRO F_RENAME
  291.         LD A,16
  292.         call ffs
  293.         ENDM
  294.  
  295. ;/*-----------------------------------------------------------------------*/
  296. ;/* Current Drive/Directory Handlings                                     */
  297. ;/*-----------------------------------------------------------------------*/
  298. ;FRESULT f_chdrive
  299. ;       BYTE drv                /* Drive number */
  300.         MACRO F_CHDR
  301.         LD A,17
  302.         call ffs
  303.         ENDM
  304.         MACRO F_CHDRIVE _DRV
  305.         LD e,_DRV
  306.         F_CHDR
  307.         ENDM
  308.  
  309. ; FRESULT f_chdir (
  310.         ; TCHAR *path   /* Pointer to the directory path */
  311. ; )
  312.         MACRO F_CHDIR
  313.         LD A,18
  314.         call ffs
  315.         ENDM
  316.  
  317. ;FRESULT f_getcwd (
  318. ;       DE=TCHAR *path, /* Pointer to the directory path */ буфер
  319. ;       BC=UINT sz_path /* Size of path */) размер буфера
  320.  
  321.         MACRO F_GETCWD
  322.         LD A,19
  323.         call ffs
  324.         ENDM
  325.  
  326.         ;MACRO F_MUL _ARG
  327.         ;LD DE,0
  328.         ;PUSH DE
  329.         ;LD de,_ARG
  330.         ;push de
  331.         ;LD A,20;19
  332.         ;call ffs
  333.         ;endm
  334.  
  335.  
  336. ;FRESULT f_utime (const TCHAR*, WORD fdate, WORD ftime); /* Change timestamp of the file/dir */
  337. ;de=name
  338. ;bc=date
  339. ;stack=time
  340.         MACRO F_UTIME
  341.         LD A,15
  342.         call ffs
  343.         ENDM
  344.  
  345. ;FRESULT f_getutime (const TCHAR*, WORD *ftimedate); /* Get timestamp of the file/dir */
  346. ;de=name
  347. ;bc=pointer to time,date
  348.         MACRO F_GETUTIME
  349.         LD A,20
  350.         call ffs
  351.         ENDM
  352.  
  353.  
  354. ; /* File function return code (FRESULT) */
  355.  
  356. ; typedef enum {
  357.         ; FR_OK = 0,                            /* (0) Succeeded */
  358.         ; FR_DISK_ERR,                  /* (1) A hard error occured in the low level disk I/O layer */
  359.         ; FR_INT_ERR,                           /* (2) Assertion failed */
  360.         ; FR_NOT_READY,                 /* (3) The physical drive cannot work */
  361.         ; FR_NO_FILE,                           /* (4) Could not find the file */
  362.         ; FR_NO_PATH,                           /* (5) Could not find the path */
  363.         ; FR_INVALID_NAME,              /* (6) The path name format is invalid */
  364.         ; FR_DENIED,                            /* (7) Access denied due to prohibited access or directory full */
  365.         ; FR_EXIST,                             /* (8) Access denied due to prohibited access */
  366.         ; FR_INVALID_OBJECT,            /* (9) The file/directory object is invalid */
  367.         ; FR_WRITE_PROTECTED,           /* (10) The physical drive is write protected */
  368.         ; FR_INVALID_DRIVE,             /* (11) The logical drive number is invalid */
  369.         ; FR_NOT_ENABLED,                       /* (12) The volume has no work area */
  370.         ; FR_NO_FILESYSTEM,             /* (13) There is no valid FAT volume on the physical drive */
  371.         ; FR_MKFS_ABORTED,              /* (14) The f_mkfs() aborted due to any parameter error */
  372.         ; FR_TIMEOUT,                           /* (15) Could not get a grant to access the volume within defined period */
  373.         ; FR_LOCKED,                            /* (16) The operation is rejected according to the file sharing policy */
  374.         ; FR_NOT_ENOUGH_CORE,           /* (17) LFN working buffer could not be allocated */
  375.         ; FR_TOO_MANY_OPEN_FILES        /* (18) Number of open files > _FS_SHARE */
  376. ; } FRESULT;
  377.