fileops.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. fileops.c: simple file access functions
  19. */
  20. #include "config.h"
  21. #include "uart.h"
  22. #include "ff.h"
  23. #include "fileops.h"
  24. #include "diskio.h"
  25. /*
  26. WCHAR ff_convert(WCHAR w, UINT dir) {
  27. return w;
  28. }*/
  29. int newcard;
  30. void file_init()
  31. {
  32. file_res = f_mount( 0, &fatfs );
  33. newcard = 0;
  34. }
  35. void file_reinit( void )
  36. {
  37. disk_init();
  38. file_init();
  39. }
  40. FRESULT dir_open_by_filinfo( DIR *dir, FILINFO *fno )
  41. {
  42. return l_opendirbycluster( &fatfs, dir, ( TCHAR * )"", fno->clust );
  43. }
  44. void file_open_by_filinfo( FILINFO *fno )
  45. {
  46. file_res = l_openfilebycluster( &fatfs, &file_handle, ( TCHAR * )"", fno->clust, fno->fsize );
  47. }
  48. void file_open( const uint8_t *filename, BYTE flags )
  49. {
  50. if ( disk_state == DISK_CHANGED )
  51. {
  52. file_reinit();
  53. newcard = 1;
  54. }
  55. file_res = f_open( &file_handle, ( TCHAR * )filename, flags );
  56. file_block_off = sizeof( file_buf );
  57. file_block_max = sizeof( file_buf );
  58. file_status = file_res ? FILE_ERR : FILE_OK;
  59. }
  60. void file_close()
  61. {
  62. file_res = f_close( &file_handle );
  63. }
  64. void file_seek( uint32_t offset )
  65. {
  66. file_res = f_lseek( &file_handle, ( DWORD )offset );
  67. }
  68. UINT file_read()
  69. {
  70. UINT bytes_read;
  71. file_res = f_read( &file_handle, file_buf, sizeof( file_buf ), &bytes_read );
  72. return bytes_read;
  73. }
  74. UINT file_write()
  75. {
  76. UINT bytes_written;
  77. file_res = f_write( &file_handle, file_buf, sizeof( file_buf ), &bytes_written );
  78. if ( bytes_written < sizeof( file_buf ) )
  79. {
  80. printf( "wrote less than expected - card full?\n" );
  81. }
  82. return bytes_written;
  83. }
  84. UINT file_readblock( void *buf, uint32_t addr, uint16_t size )
  85. {
  86. UINT bytes_read;
  87. file_res = f_lseek( &file_handle, addr );
  88. if ( file_handle.fptr != addr )
  89. {
  90. return 0;
  91. }
  92. file_res = f_read( &file_handle, buf, size, &bytes_read );
  93. return bytes_read;
  94. }
  95. UINT file_writeblock( void *buf, uint32_t addr, uint16_t size )
  96. {
  97. UINT bytes_written;
  98. file_res = f_lseek( &file_handle, addr );
  99. if ( file_res )
  100. {
  101. return 0;
  102. }
  103. file_res = f_write( &file_handle, buf, size, &bytes_written );
  104. return bytes_written;
  105. }
  106. uint8_t file_getc()
  107. {
  108. if ( file_block_off == file_block_max )
  109. {
  110. file_block_max = file_read();
  111. if ( file_block_max == 0 )
  112. {
  113. file_status = FILE_EOF;
  114. }
  115. file_block_off = 0;
  116. }
  117. return file_buf[file_block_off++];
  118. }