diskio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. printfc("SNES::disk_initialize called drv=%i stat=%i\n",drv,Stat);
  29. if (drv) return STA_NOINIT; /* Supports only single drive */
  30. Stat |= STA_NOINIT;
  31. printfc("SNES::disk_initialize stat=%i\n",Stat);
  32. *(byte*) MMIO_RETVAL = STA_VOID;
  33. *(byte*) MMIO_CMD = CMD_INIT;
  34. printfc("SNES::disk_initialize poll retval\n");
  35. while((retval = *(byte*) MMIO_RETVAL) == STA_VOID);
  36. Stat &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */
  37. printfc("SNES::disk_initialize done Stat=%i\n",Stat);
  38. return Stat;
  39. }
  40. /*-----------------------------------------------------------------------*/
  41. /* Return Disk Status */
  42. /*-----------------------------------------------------------------------*/
  43. DSTATUS disk_status (BYTE drv){
  44. if (drv) return STA_NOINIT; /* Supports only single drive */
  45. return Stat;
  46. }
  47. /*-----------------------------------------------------------------------*/
  48. /* Read Sector(s) */
  49. /*-----------------------------------------------------------------------*/
  50. DRESULT disk_read (
  51. BYTE drv, /* Physical drive nmuber (0) */
  52. BYTE *buff, /* Data buffer to store read data */
  53. DWORD sector, /* Sector number (LBA) */
  54. BYTE count /* Sector count (1..255) */
  55. )
  56. {
  57. DWORD offset;
  58. INT size;
  59. byte retval;
  60. word i;
  61. printfc("SNES::disk_read called sector=%li count=%i\n",sector,count);
  62. if (drv || !count) return RES_PARERR;
  63. printfc("SNES::disk_read drv ok\n");
  64. if (Stat & STA_NOINIT) return RES_NOTRDY;
  65. printfc("SNES::disk_read sta ok\n");
  66. *(byte*) MMIO_RETVAL = STA_VOID;
  67. *(byte*) MMIO_CMD = CMD_READ;
  68. *(byte*) MMIO_SECTOR01 = (sector >> 24) & 0xff;
  69. *(byte*) MMIO_SECTOR02 = (sector >> 16) & 0xff;
  70. *(byte*) MMIO_SECTOR03 = (sector >> 8) & 0xff;
  71. *(byte*) MMIO_SECTOR04 = (sector) & 0xff;
  72. *(byte*) MMIO_COUNT = count;
  73. printfc("SNES::disk_read poll retval\n");
  74. while((retval = *(byte*) MMIO_RETVAL) == STA_VOID);
  75. printfc("SNES::disk_read copy buffer to %06lx\n",SHARED_ADDR);
  76. for (i=0;i<(count*512);i++){
  77. buff[i] = *(byte*)(SHARED_ADDR+i);
  78. if ( i < 8)
  79. printfc("0x%02x ",buff[i]);
  80. }
  81. printfc("\n");
  82. return retval;
  83. }
  84. /*-----------------------------------------------------------------------*/
  85. /* Write Sector(s) */
  86. /*-----------------------------------------------------------------------*/
  87. #if _READONLY == 0
  88. DRESULT disk_write (
  89. BYTE drv, /* Physical drive nmuber (0) */
  90. const BYTE *buff, /* Data to be written */
  91. DWORD sector, /* Sector number (LBA) */
  92. BYTE count /* Sector count (1..255) */
  93. )
  94. {
  95. DWORD offset;
  96. INT size;
  97. if (drv || !count) return RES_PARERR;
  98. if (Stat & STA_NOINIT) return RES_NOTRDY;
  99. offset = sector * 512;
  100. size = count * 512;
  101. return RES_OK;
  102. }
  103. #endif /* _READONLY */
  104. /*-----------------------------------------------------------------------*/
  105. /* Miscellaneous Functions */
  106. /*-----------------------------------------------------------------------*/
  107. #if _USE_IOCTL != 0
  108. DRESULT disk_ioctl (
  109. BYTE drv, /* Physical drive nmuber (0) */
  110. BYTE ctrl, /* Control code */
  111. void *buff /* Buffer to send/receive data block */
  112. )
  113. {
  114. BYTE n, w, ofs, dl, dh, *ptr = (BYTE*)buff;
  115. if (drv) return RES_PARERR;
  116. if (Stat & STA_NOINIT) return RES_NOTRDY;
  117. switch (ctrl) {
  118. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  119. ofs = 60; w = 2; n = 0;
  120. break;
  121. case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
  122. *(WORD*)buff = 512;
  123. return RES_OK;
  124. case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */
  125. *(DWORD*)buff = 32;
  126. return RES_OK;
  127. default:
  128. return RES_PARERR;
  129. }
  130. return RES_OK;
  131. }
  132. #endif /* _USE_IOCTL != 0 */