diskio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "integer.h"
  2. #include "diskio.h"
  3. #include "config.h"
  4. #include "data.h"
  5. #include "debug.h"
  6. /*
  7. * Interface ** Scratch Buffer addr 3 byte size 1 byte
  8. *
  9. * ** Call Interface cmd 1 byte sector 4 bytes count 1 byte return 1 byte
  10. *
  11. * ** Commands * disk_init * disk_read * disk_write
  12. */
  13. /*-----------------------------------------------------------------------*/
  14. /*
  15. * Initialize Disk Drive
  16. */
  17. /*-----------------------------------------------------------------------*/
  18. #include <string.h>
  19. // TODO: Figure out gobal vars ?!?
  20. DSTATUS Stat = 0; // STA_NOINIT; /* Disk status */
  21. DSTATUS disk_initialize(BYTE drv)
  22. {
  23. byte retval;
  24. #ifdef MMIO_DEBUG
  25. printfc("SNES::disk_initialize: called drv=%i stat=%i\n", drv, Stat);
  26. #endif
  27. if (drv)
  28. return STA_NOINIT; /* Supports only single drive */
  29. Stat |= STA_NOINIT;
  30. #ifdef MMIO_DEBUG
  31. printfc("SNES::disk_initialize: stat=%i\n", Stat);
  32. #endif
  33. *(byte *) MMIO_RETVAL = STA_VOID;
  34. *(byte *) MMIO_CMD = CMD_INIT;
  35. #ifdef MMIO_DEBUG
  36. printfc("SNES::disk_initialize: poll retval\n");
  37. #endif
  38. while ((retval = *(byte *) MMIO_RETVAL) == STA_VOID);
  39. Stat &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */
  40. #ifdef MMIO_DEBUG
  41. printfc("SNES::disk_initialize: done Stat=%i\n", Stat);
  42. #endif
  43. return Stat;
  44. }
  45. /*-----------------------------------------------------------------------*/
  46. /*
  47. * Return Disk Status
  48. */
  49. /*-----------------------------------------------------------------------*/
  50. DSTATUS disk_status(BYTE drv)
  51. {
  52. if (drv)
  53. return STA_NOINIT; /* Supports only single drive */
  54. return Stat;
  55. }
  56. /*-----------------------------------------------------------------------*/
  57. /*
  58. * Read Sector(s)
  59. */
  60. /*-----------------------------------------------------------------------*/
  61. DRESULT disk_read(BYTE drv, /* Physical drive nmuber (0) */
  62. BYTE * buff, /* Data buffer to store read data */
  63. DWORD sector, /* Sector number (LBA) */
  64. BYTE count /* Sector count (1..255) */
  65. )
  66. {
  67. DWORD offset;
  68. INT size;
  69. byte retval;
  70. word i;
  71. #ifdef MMIO_DEBUG
  72. printfc("SNES::disk_read: sector=%li count=%i\n", sector, count);
  73. #endif
  74. if (drv || !count)
  75. return RES_PARERR;
  76. #ifdef MMIO_DEBUG
  77. printfc("SNES::disk_read: drv ok\n");
  78. #endif
  79. if (Stat & STA_NOINIT)
  80. return RES_NOTRDY;
  81. #ifdef MMIO_DEBUG
  82. printfc("SNES::disk_read: sta ok\n");
  83. #endif
  84. *(byte *) MMIO_RETVAL = STA_VOID;
  85. *(byte *) MMIO_CMD = CMD_READ;
  86. *(byte *) MMIO_SECTOR01 = (sector >> 24) & 0xff;
  87. *(byte *) MMIO_SECTOR02 = (sector >> 16) & 0xff;
  88. *(byte *) MMIO_SECTOR03 = (sector >> 8) & 0xff;
  89. *(byte *) MMIO_SECTOR04 = (sector) & 0xff;
  90. *(byte *) MMIO_COUNT = count;
  91. #ifdef MMIO_DEBUG
  92. printfc("SNES::disk_read: poll retval\n");
  93. #endif
  94. while ((retval = *(byte *) MMIO_RETVAL) == STA_VOID);
  95. #ifdef MMIO_DEBUG
  96. printfc("SNES::disk_read: copy buffer to %lx\n", SHARED_ADDR);
  97. #endif
  98. for (i = 0; i < (count * 512); i++) {
  99. buff[i] = *(byte *) (SHARED_ADDR + i);
  100. #ifdef MMIO_DEBUG
  101. if (i < 4)
  102. printfc("0x%x ", buff[i]);
  103. #endif
  104. }
  105. #ifdef MMIO_DEBUG
  106. printfc("\n");
  107. #endif
  108. return retval;
  109. }
  110. /*-----------------------------------------------------------------------*/
  111. /*
  112. * Write Sector(s)
  113. */
  114. /*-----------------------------------------------------------------------*/
  115. #if _READONLY == 0
  116. DRESULT disk_write(BYTE drv, /* Physical drive nmuber (0) */
  117. const BYTE * buff, /* Data to be written */
  118. DWORD sector, /* Sector number (LBA) */
  119. BYTE count /* Sector count (1..255) */
  120. )
  121. {
  122. DWORD offset;
  123. INT size;
  124. if (drv || !count)
  125. return RES_PARERR;
  126. if (Stat & STA_NOINIT)
  127. return RES_NOTRDY;
  128. offset = sector * 512;
  129. size = count * 512;
  130. return RES_OK;
  131. }
  132. #endif /* _READONLY */
  133. /*-----------------------------------------------------------------------*/
  134. /*
  135. * Miscellaneous Functions
  136. */
  137. /*-----------------------------------------------------------------------*/
  138. #if _USE_IOCTL != 0
  139. DRESULT disk_ioctl(BYTE drv, /* Physical drive nmuber (0) */
  140. BYTE ctrl, /* Control code */
  141. void *buff /* Buffer to send/receive data block */
  142. )
  143. {
  144. BYTE n, w, ofs, dl, dh, *ptr = (BYTE *) buff;
  145. if (drv)
  146. return RES_PARERR;
  147. if (Stat & STA_NOINIT)
  148. return RES_NOTRDY;
  149. switch (ctrl) {
  150. case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
  151. ofs = 60;
  152. w = 2;
  153. n = 0;
  154. break;
  155. case GET_SECTOR_SIZE: /* Get sectors on the disk (WORD) */
  156. *(WORD *) buff = 512;
  157. return RES_OK;
  158. case GET_BLOCK_SIZE: /* Get erase block size in sectors (DWORD) */
  159. *(DWORD *) buff = 32;
  160. return RES_OK;
  161. default:
  162. return RES_PARERR;
  163. }
  164. return RES_OK;
  165. }
  166. #endif /* _USE_IOCTL != 0 */