Advertisement
Eeems

GetFilePointer

Jun 12th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; FAT
  2. FILE_START      equ 4000h   ; Start of FAT
  3. FILE_NULL       equ 0
  4. FILE_DELETED_F      equ 1
  5. FILE_DELETED_D      equ 2
  6. ;FILE_DELETED_S     equ 3
  7. FILE_FILE       equ 4
  8. FILE_DIRECTORY      equ 5
  9. ;FILE_SYMLINK       equ 6
  10. FILE_END        equ 7
  11. ; Error codes
  12. SUCCCESS        equ 0
  13. FIND_FOUND      equ 0
  14. ALLOCATE_SUCCESS    equ 0
  15. ERROR           equ 1
  16. FIND_MISSING        equ 1
  17. ALLOCATE_OUT_OF_MEM equ 1
  18. CORRUPT         equ 2
  19. MEMORY_CORRUPT      equ 2
  20. FIND_MEMORY_CORRUPT equ 2
  21.  
  22. ; returns the pointer to the file from the path in de
  23. ; outputs   de = pointer to file
  24. ;       bc = length of file
  25. ;       a  = status code
  26. ;       h  = flash page
  27. GetFilePointer:
  28.     ld a, 1 ; Set flash page 1 in bank 2.
  29.     out (6), a
  30.     push de
  31.     ld hl,(FILE_START)
  32.     ld de,FILE_START
  33.     or a
  34.     sbc hl,de
  35.         jr z,_ ; if they are the same no corruption
  36.     ; corruption error here
  37.     ld a,FIND_MEMORY_CORRUPT
  38.     pop de
  39.     ret
  40.    
  41. _   pop de ; memory is fine here
  42.     ld hl,FILE_START
  43. _DirLoop:
  44.     call ContainsSlash
  45. ;       jr nz, ; Directory in path
  46.         jr nz, _FileSearch ; no directory in path
  47.     ;handle directory searching here
  48.     ld a, (hl)
  49.     cp FILE_END ; End of table
  50.         ret z ; Not found
  51.     cp FILE_DIRECTORY ; Directory
  52.         jr z, _CheckDirectory
  53.     cp FILE_FILE
  54.         ret nz ; don't handle anything else for now
  55.     call SkipFileEntry
  56.     jr _DirLoop
  57. _CheckDirectory:
  58.     inc hl
  59.     call DirectoryCompare
  60.         jr nz, _DirNotFound
  61.     ; Directory found :D
  62.     ; Move DE past the directory, and move on
  63.     inc de
  64.     jr _DirLoop
  65. _DirNotFound:
  66.     call SkipDirEntry
  67.     jr _DirLoop
  68. _FileSearch:
  69.     ld a,(hl)   ;get File type
  70.     cp FILE_END ; End of table
  71.         ret z ; Not found
  72.     cp FILE_FILE ; File
  73.         jr z, _FileCheck
  74.     cp FILE_DIR
  75.         ret nz ; don't handle other types for now
  76.     call SkipDirEntry
  77.     jr _FileSearch
  78. _FileCheck:
  79.     push de
  80.         call CmpStrings
  81.     pop de
  82.     jr z, _
  83.     call SkipFileEntry
  84.     jr _FileSearch
  85. _   call SkipEntryName  ; Found file
  86.     inc hl
  87.     ld a, (hl) ; Flash page into A
  88.     inc hl
  89.     ld e, (hl)
  90.     inc hl
  91.     ld d, (hl) ; Location in DE
  92.     inc hl
  93.     ld c, (hl)
  94.     inc hl
  95.     ld b, (hl) ; Size in BC
  96.     out (6), a ; Set flash page
  97.     ld h,a
  98.     ld a,FIND_FOUND ; Give the right Return code
  99.     ret
  100. ;
  101. SkipDirEntry:
  102.     call SkipEntryName
  103.     inc hl \ inc hl ; pass the Dir ID
  104.     ret
  105. ;
  106. SkipFileEntry:
  107.     call SkipEntryName
  108.     push de \ ld de,7 \ add hl,de \ pop de  ; skip the rest of the data
  109.     ret
  110. SkipEntryName:
  111.     ld a, 0
  112.     push bc
  113.         ld bc, 0FFFFh
  114.         cpir    ; Move HL to the end of the name
  115.     pop bc
  116.     ret
  117. ; inputs    de = Pointer to path
  118. ; outputs   z  = true
  119. ;       nz = false
  120. ContainsSlash:
  121.     push de
  122. _       ld a, (de)
  123.         cp '/'
  124.         jr z, ++_
  125.         or a
  126.         jr z, _
  127.         inc de
  128.         jr -_
  129. _   cp 1
  130. _   pop de
  131.     ret
  132. ; checks to see if the directory exists
  133. DirectoryCompare:
  134.         ld a, (de)
  135.         cp 0
  136.         jr z,_
  137.         cp '/'
  138.             jr z,_
  139.         cp (hl)
  140.             ret nz
  141.         inc hl
  142.         inc de
  143.         jr DirectoryCompare
  144. _   ld a, (hl)
  145.     cp 0
  146.         ret z
  147.     cp 1
  148.     ret
  149. ; Input: HL and DE are strings
  150. ; Output: Z if equal, NZ otherwise
  151. CmpStrings:
  152.     ld a, (de)
  153.     cp 0
  154.     jr z, CmpStringsEoS
  155.     cp (hl)
  156.     ret nz
  157.     inc hl
  158.     inc de
  159.     jr CmpStrings
  160. CmpStringsEoS:
  161.     ld a, (hl)
  162.     cp 0
  163.     ret
  164.  
  165. FATTable:
  166.     dw FILE_START           ; Type ID = Start of flash
  167.    
  168.     db FILE_DIRECTORY       ; Type ID = Folder
  169.     db "root",0         ; Folder name
  170.     dw 0                ; Directory ID (word)
  171.  
  172.     db FILE_FILE            ; Type ID = File
  173.     db "boot.txt", 0        ; File name
  174.     db 01h              ; Flash page
  175.     dw BootText         ; Pointer to data (word)
  176.     dw BootTextEnd-BootText     ; Length (word)
  177.     dw 0                ; Directory ID (word)
  178.    
  179.     db FILE_END         ;end of FAT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement