home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / m / makedrawf / Source / s / lowfile next >
Text File  |  1997-01-13  |  6KB  |  220 lines

  1. ; >s.lowfile
  2. ;
  3. ; functions for low-level file handling under RISC OS
  4. ;
  5. ; This code occupies about 200 bytes.
  6. ;
  7. ; It provides functions to:
  8. ;   open and close files
  9. ;   read or write a block of bytes
  10. ;   read or set the pointer
  11. ;   test for EOF
  12. ;   read the extent (size) of a file
  13. ;
  14. ; It does NOT give access to OS_BGet and OS_BPut because they are
  15. ; horribly slow. It's almost always much better to use OS_GBPB.
  16. ; Since the only reason for using these routines is efficiency, it doesn't
  17. ; make sense to provide inefficient things that can be done with
  18. ; the standard C calls.
  19. ;
  20. ; All these functions obey APCS-R. On entry, lr is saved on the
  21. ; stack; it's safe to use these in supervisor mode.
  22. ;
  23. ; NOTE: I've also put in the save_file() function, because decdrawf
  24. ;       needs it. This adds 40 bytes to the code size.
  25.  
  26.         GET h.asmregs
  27.  
  28. XOS_Bit    EQU 0x020000
  29. OS_File EQU 0x000008
  30. OS_Args    EQU 0x000009
  31. OS_GBPB    EQU 0x00000C
  32. OS_Find    EQU 0x00000D
  33.  
  34.  
  35.         AREA |A$$code|, CODE, READONLY
  36.  
  37.         EXPORT low_open
  38.         EXPORT low_close
  39.         EXPORT low_eof
  40.         EXPORT low_read
  41.         EXPORT low_write
  42.         EXPORT low_ptr
  43.         EXPORT low_setptr
  44.         EXPORT low_seek
  45.         EXPORT low_extent
  46.     EXPORT save_file
  47.  
  48. ; int low_open(char *name, int mode);
  49. ; mode: only bits 4,7 matter.
  50. ;       bit 7 indicates "write"
  51. ;       bit 6 indicates "existing"
  52. ;       having both clear is a mistake
  53. ; returns file handle if OK, 0 if not OK
  54.  
  55. low_open
  56.         STMFD   sp!,{lr}
  57.         AND     r1,r1,#&C0
  58.         ORR     r1,r1,#&07
  59.         MOV     r2,r1       ;
  60.         MOV     r1,r0       ; swap r0,r1
  61.         MOV     r0,r2       ;
  62.         SWI     XOS_Bit+OS_Find
  63.         MOVVS   r0,#0       ; if error
  64.         LDMFD   sp!,{pc}^   ; return, restoring flags
  65.  
  66. ; void low_close(int handle);
  67. ; if handle==0 this doesn't do anything. (This is not the same
  68. ; as raw OS_Find 0.)
  69.  
  70. low_close
  71.         STMFD   sp!,{lr}
  72.         CMP     r0,#0
  73.         MOVNE   r1,r0
  74.         MOVNE   r0,#0
  75.         SWINE   XOS_Bit+OS_Find
  76.         LDMFD   sp!,{pc}^
  77.  
  78. ; int low_eof(int handle);
  79. ; returns 0 if not at EOF, 1 if at EOF.
  80. ; undefined behaviour if arg isn't handle of an open file.
  81.  
  82. low_eof STMFD   sp!,{lr}
  83.         MOV     r1,r0
  84.         MOV     r0,#5
  85.         SWI     XOS_Bit+OS_Args
  86.         CMP     r2,#0       ; annoying. We need to return 0 or 1.
  87.         MOVEQ   r0,#0
  88.         MOVNE   r0,#1
  89.         LDMFD   sp!,{pc}^
  90.  
  91. ; int low_read(int handle, void *buffer, int nbytes);
  92. ; returns number of bytes actually read; 0 if there was an error.
  93.  
  94. low_read
  95.         STMFD   sp!,{r4,r5,lr}
  96.         MOV     r3,r2       ; number of bytes
  97.         MOV     r2,r1       ; buffer address
  98.         MOV     r1,r0       ; handle
  99.         MOV     r0,#4       ; read, using current file pointer
  100.         MOV     r5,r2       ; save for later use
  101.         SWI     XOS_Bit+OS_GBPB
  102.         MOVVS   r0,#0
  103.         SUBVC   r0,r2,r5    ; next_byte-first_byte
  104.         LDMFD   sp!,{r4,r5,pc}^
  105.  
  106. ; void low_write(int handle, void *buffer, int nbytes);
  107. ; doesn't return anything as the corresponding OS_GBPB call doesn't
  108. ; return any useful information.
  109.  
  110. low_write
  111.         STMFD   sp!,{r4,r5,lr}
  112.         MOV     r3,r2       ; number of bytes
  113.         MOV     r2,r1       ; address
  114.         MOV     r1,r0       ; handle
  115.         MOV     r0,#2       ; write, at current position
  116.         SWI     XOS_Bit+OS_GBPB
  117.         LDMFD   sp!,{r4,r5,pc}^
  118.  
  119. ; int low_ptr(int handle);
  120. ; returns file pointer
  121. ; an invalid handle will result in garbage
  122.  
  123. low_ptr STMFD   sp!,{lr}
  124.         MOV     r1,r0
  125.         MOV     r0,#0
  126.         SWI     XOS_Bit+OS_Args
  127.         MOV     r0,r2
  128.         LDMFD   sp!,{pc}^
  129.  
  130. ; void low_setptr(int handle, int ptr);
  131. ; sets the file pointer. Again, there isn't anything useful to return.
  132.  
  133. low_setptr
  134.         STMFD   sp!,{lr}
  135.         MOV     r2,r1
  136.         MOV     r1,r0
  137.         MOV     r0,#1
  138.         SWI     XOS_Bit+OS_Args
  139.         LDMFD   sp!,{pc}^
  140.  
  141. ; int low_seek(int handle, int n, int whence);
  142. ; sets pointer, but now
  143. ;  - if whence==0 does same as setptr
  144. ;  - if whence==1 moves on by n
  145. ;  - if whence==2 moves to end-n
  146. ; I'm not sure what correct behaviour is if we go past EOF or
  147. ; something. I'll use the RISC OS conventions.
  148. ; We return the new pointer position, or -1 for failure. (This
  149. ; appears to be standard for Unix lseek.)
  150.  
  151. low_seek
  152.         STMFD   sp!,{lr}
  153.         CMP     r0,#0
  154.         BEQ     low_seek_fail    ; 0 is never a valid handle
  155.         CMP     r2,#1
  156.         BGT     low_seek_end
  157.         BEQ     low_seek_advance
  158.         CMP     r1,#0
  159.         BLT     low_seek_fail
  160.         MOV     r2,r1
  161.         MOV     r1,r0
  162. low_seek_doit
  163.         MOV     r0,#1
  164.         SWI     XOS_Bit+OS_Args
  165.         BVS     low_seek_fail
  166.         MOV     r0,#0
  167.         LDMFD   sp!,{pc}^
  168. low_seek_advance
  169.         MOV     r3,r1    ; save
  170.         MOV     r1,r0
  171.         MOV     r0,#0
  172.         SWI     XOS_Bit+OS_Args
  173.         ADDS    r2,r2,r3 ; new pointer
  174.         BGE     low_seek_doit
  175. low_seek_fail
  176.         MVN     r0,#0
  177.         LDMFD   sp!,{pc}^
  178. low_seek_end
  179.         MOV     r3,r1    ; save
  180.         MOV     r1,r0
  181.         MOV     r0,#2
  182.         SWI     XOS_Bit+OS_Args
  183.         SUBS    r2,r2,r3 ; new pointer
  184.         BGE     low_seek_doit
  185.         B       low_seek_fail
  186.  
  187. ; int low_extent(int handle);
  188. ; reads extent of an open file. Returns a negative number if problems.
  189.  
  190. low_extent
  191.         STMFD   sp!,{lr}
  192.         MOVS    r1,r0
  193.         BEQ     low_ext_fail
  194.         MOV     r0,#2
  195.         SWI     XOS_Bit+OS_Args
  196.         BVS     low_ext_fail
  197.         MOV     r0,r2
  198.         LDMFD   sp!,{pc}^
  199. low_ext_fail
  200.         MVN     r0,#0
  201.         LDMFD   sp!,{pc}^
  202.  
  203. ; int save_file(char *name, void *address, int nbytes, int filetype);
  204. ; returns 0 for success, -1 for failure.
  205.  
  206. save_file
  207.     STMFD sp!,{r4,r5,lr}
  208.     ADD r5,r1,r2    ; r5: end address (excl) = start + length
  209.     MOV r4,r1    ; r4: start address
  210.             ; r3: unused
  211.     MOV r2,r3    ; r2: filetype
  212.     MOV r1,r0    ; r1: filename
  213.     MOV r0,#10    ; save file, with filetype etc
  214.     SWI XOS_Bit+OS_File
  215.     MVNVS r0,#0
  216.     MOVVC r0,#0
  217.     LDMFD sp!,{r4,r5,pc}^
  218.  
  219.     END
  220.