diskio.c 5.2 KB

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