Advertisement
DimkaM

Untitled

Jan 21st, 2021
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.63 KB | None | 0 0
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2016        */
  3. /*-----------------------------------------------------------------------*/
  4. /* If a working storage control module is available, it should be        */
  5. /* attached to the FatFs via a glue function rather than modifying it.   */
  6. /* This is an example of glue functions to attach various exsisting      */
  7. /* storage control modules to the FatFs module with a defined API.       */
  8. /*-----------------------------------------------------------------------*/
  9.  
  10. #include "fatfs/ff.h"           /* Obtains integer types */
  11. #include "fatfs/diskio.h"       /* Declarations of disk functions */
  12. #include <Intrz80.h>
  13. #include <oscalls.h>
  14. //                      RESERV      RESERV      DEV_ATMIDE  DEV_DIVIDE
  15. DSTATUS disks_stat[4]={STA_NOINIT, STA_NOINIT, STA_NOINIT, STA_NOINIT};
  16.  
  17.  
  18. extern unsigned char tmpbuf[512];
  19.  
  20. #define IDE_MASTER 0xe0
  21. #define IDE_SLAVE 0xf0
  22.  
  23. #define NEMO_DATA   0x10
  24. #define NEMO_COUNT  0x50
  25. #define NEMO_LBA0   0x70
  26. #define NEMO_LBA1   0x90
  27. #define NEMO_LBA2   0xB0
  28. #define NEMO_LBA3   0xD0
  29. #define NEMO_MS     0xD0
  30. #define NEMO_CMD    0xF0
  31. #define NEMO_STAT   0xF0
  32.  
  33. #define ATM_DATA_LOW    0x000f
  34. #define ATM_DATA_HI     0x010f
  35. #define ATM_COUNT   0x004f
  36. #define ATM_LBA0    0x006f
  37. #define ATM_LBA1    0x008f
  38. #define ATM_LBA2    0x00af
  39. #define ATM_LBA3    0x00cf
  40. #define ATM_MS      0x00cf
  41. #define ATM_CMD     0x00ef
  42. #define ATM_STAT    0x00ef
  43.  
  44. #define ATA_STAT_ERR    0x01
  45. #define ATA_STAT_IDX    0x02
  46. #define ATA_STAT_CORR   0x04
  47. #define ATA_STAT_DRQ    0x08
  48. #define ATA_STAT_DSC    0x10
  49. #define ATA_STAT_DWF    0x20
  50. #define ATA_STAT_DRDY   0x40
  51. #define ATA_STAT_BSY    0x80
  52.  
  53. #define ATA_CMD_READ_SECTORS 0x20
  54. #define ATA_CMD_WRITE_SECTORS 0x30
  55. #define ATA_CMD_INIT_DRIVE_PARAM 0x91
  56. #define ATA_CMD_IDENTIFY_DRIVE 0xec
  57. /*-----------------------------------------------------------------------*/
  58. /* Get Drive Status                                                      */
  59. /*-----------------------------------------------------------------------*/
  60.  
  61. DSTATUS disk_status (
  62.     BYTE pdrv       /* Physical drive nmuber to identify the drive */
  63. )
  64. {
  65.     return disks_stat[pdrv];
  66. }
  67.  
  68.  
  69.  
  70. /*-----------------------------------------------------------------------*/
  71. /* Inidialize a Drive                                                    */
  72. /*-----------------------------------------------------------------------*/
  73.  
  74. DSTATUS disk_initialize (
  75.     BYTE pdrv               /* Physical drive nmuber to identify the drive */
  76. )
  77. {
  78.     unsigned int i = 26;
  79.     unsigned char d;
  80.    
  81.     if(disks_stat[pdrv] == RES_OK) return RES_OK;
  82.    
  83.     if(DEV_NEMO_M == (pdrv & 0xfe)){
  84.         output8(NEMO_MS, (pdrv & 0x01) ? IDE_SLAVE : IDE_MASTER);
  85.         while(--i){
  86.             YIELD();
  87.             d=input8(NEMO_STAT);
  88.             if((d & ATA_STAT_BSY) == 0x00) break;
  89.         }
  90.         if(i == 0x00) return STA_NODISK;
  91.         if(d == 0x00) return STA_NODISK;
  92.         //if(d == 0xff) return STA_NODISK;
  93.         output8(NEMO_LBA2, 0x00);
  94.         output8(NEMO_LBA1, 0x00);
  95.         output8(NEMO_CMD, ATA_CMD_IDENTIFY_DRIVE);
  96.         while(1){
  97.             d = input8(NEMO_STAT);
  98.             if(d == 0x00 || d == 0xff) return STA_NODISK;
  99.             if(d & ATA_STAT_ERR) break;
  100.             if((d  & (ATA_STAT_BSY | ATA_STAT_DRQ)) == ATA_STAT_DRQ) break;
  101.         }
  102.         if(input8(NEMO_LBA2) | input8(NEMO_LBA1)) return STA_NODISK; //is CD-ROM
  103.         input_block_inc(NEMO_DATA, tmpbuf, 256);
  104.         input_block_inc(NEMO_DATA, tmpbuf+256, 256);
  105.         if((tmpbuf[49*2+1] & 0x02) == 0x00) return STA_NODISK; //no LBA
  106.         //                  \/Number of sectors per track
  107.         output8(NEMO_COUNT,tmpbuf[12]);
  108.         d = (tmpbuf[6] - 1) | IDE_MASTER;
  109.         if(pdrv & 0x01) d |= DEV_MS_BIT;
  110.         output8(NEMO_MS,tmpbuf[6]);
  111.         output8(NEMO_CMD, ATA_CMD_INIT_DRIVE_PARAM);
  112.         i = 1000;
  113.         while(--i){
  114.             if(input8(NEMO_STAT) & ATA_STAT_BSY) continue;
  115.             disks_stat[pdrv] = RES_OK;
  116.             break;
  117.         }  
  118.     }else if(DEV_ATM_M == (pdrv & 0xfe)){
  119.         output(ATM_MS, (pdrv & 0x01) ? IDE_SLAVE : IDE_MASTER);
  120.         while(--i){
  121.             YIELD();
  122.             d=input(ATM_STAT);
  123.             if((d & ATA_STAT_BSY) == 0x00) break;
  124.         }
  125.         if(i == 0x00) return STA_NODISK;
  126.         if(d == 0x00) return STA_NODISK;
  127.         //if(d == 0xff) return STA_NODISK;
  128.         output(ATM_LBA2, 0x00);
  129.         output(ATM_LBA1, 0x00);
  130.         output(ATM_CMD, ATA_CMD_IDENTIFY_DRIVE);
  131.         while(1){
  132.             d = input(ATM_STAT);
  133.             if(d == 0x00 || d == 0xff) return STA_NODISK;
  134.             if(d & ATA_STAT_ERR) break;
  135.             if((d  & (ATA_STAT_BSY | ATA_STAT_DRQ)) == ATA_STAT_DRQ) break;
  136.         }
  137.         if(input(ATM_LBA2) | input(ATM_LBA1)) return STA_NODISK; //is CD-ROM
  138.         input_block_inc(ATM_DATA_LOW, tmpbuf, 256);
  139.         input_block_inc(ATM_DATA_LOW, tmpbuf+256, 256);
  140.         if((tmpbuf[49*2+1] & 0x02) == 0x00) return STA_NODISK; //no LBA
  141.         //                  \/Number of sectors per track
  142.         output(ATM_COUNT,tmpbuf[12]);
  143.         d = (tmpbuf[6] - 1) | IDE_MASTER;
  144.         if(pdrv & 0x01) d |= DEV_MS_BIT;
  145.         output(ATM_MS,tmpbuf[6]);
  146.         output(ATM_CMD, ATA_CMD_INIT_DRIVE_PARAM);
  147.         i = 1000;
  148.         while(--i){
  149.             if(input(ATM_STAT) & ATA_STAT_BSY) continue;
  150.             disks_stat[pdrv] = RES_OK;
  151.             break;
  152.         }  
  153.     }
  154.     return disks_stat[pdrv];
  155. }
  156.  
  157.  
  158.  
  159. /*-----------------------------------------------------------------------*/
  160. /* Read Sector(s)                                                        */
  161. /*-----------------------------------------------------------------------*/
  162.  
  163. DRESULT disk_read (
  164.     BYTE pdrv,      /* Physical drive nmuber to identify the drive */
  165.     BYTE *buff,     /* Data buffer to store read data */
  166.     DWORD sector,   /* Start sector in LBA */
  167.     UINT count      /* Number of sectors to read */
  168. )
  169. {
  170.     unsigned char c = count;
  171.     if(DEV_NEMO_M == (pdrv & 0xfe)){
  172.         if(pdrv & 0x01)
  173.             output8(NEMO_LBA3,((unsigned char*)&sector)[3] | IDE_SLAVE);
  174.         else
  175.             output8(NEMO_LBA3,((unsigned char*)&sector)[3] | IDE_MASTER);
  176.         while((input8(NEMO_STAT) & ATA_STAT_BSY) != 0);
  177.         output8(NEMO_LBA2,((unsigned char*)&sector)[2]);
  178.         output8(NEMO_LBA1,((unsigned char*)&sector)[1]);
  179.         output8(NEMO_LBA0,((unsigned char*)&sector)[0]);
  180.         output8(NEMO_COUNT,c);
  181.         output8(NEMO_CMD, ATA_CMD_READ_SECTORS);
  182.         while((input8(NEMO_STAT) & (ATA_STAT_BSY | ATA_STAT_DRQ)) != 8);
  183.         while(c--){
  184.             //unsigned int i = 512;
  185.             input_block_inc(NEMO_DATA, buff, 256);
  186.             buff += 256;
  187.             input_block_inc(NEMO_DATA, buff, 256);
  188.             buff += 256;
  189.             //while(i--){
  190.             //  *buff = input8(NEMO_DATA);
  191.             //  buff++;
  192.             //}
  193.             while((input8(NEMO_STAT) & ATA_STAT_BSY) != 0);
  194.         }
  195.         return RES_OK;
  196.     }else if(DEV_ATM_M == (pdrv & 0xfe)){
  197.         if(pdrv & 0x01)
  198.             output(ATM_LBA3,((unsigned char*)&sector)[3] | IDE_SLAVE);
  199.         else
  200.             output(ATM_LBA3,((unsigned char*)&sector)[3] | IDE_MASTER);
  201.         while((input(ATM_STAT) & ATA_STAT_BSY) != 0);
  202.         output(ATM_LBA2,((unsigned char*)&sector)[2]);
  203.         output(ATM_LBA1,((unsigned char*)&sector)[1]);
  204.         output(ATM_LBA0,((unsigned char*)&sector)[0]);
  205.         output(ATM_COUNT,c);
  206.         output(ATM_CMD, ATA_CMD_READ_SECTORS);
  207.         while((input(ATM_STAT) & (ATA_STAT_BSY | ATA_STAT_DRQ)) != 8);
  208.         while(c--){
  209.             //unsigned int i = 512;
  210.             input_block_inc(ATM_DATA_LOW, buff, 256);
  211.             buff += 256;
  212.             input_block_inc(ATM_DATA_LOW, buff, 256);
  213.             buff += 256;
  214.             //while(i--){
  215.             //  *buff = input8(NEMO_DATA);
  216.             //  buff++;
  217.             //}
  218.             while((input(ATM_STAT) & ATA_STAT_BSY) != 0);
  219.         }
  220.         return RES_OK;
  221.     }
  222.  
  223.     return RES_PARERR;
  224. }
  225.  
  226.  
  227.  
  228. /*-----------------------------------------------------------------------*/
  229. /* Write Sector(s)                                                       */
  230. /*-----------------------------------------------------------------------*/
  231.  
  232. #if FF_FS_READONLY == 0
  233.  
  234. DRESULT disk_write (
  235.     BYTE pdrv,          /* Physical drive nmuber to identify the drive */
  236.     const BYTE *buff,   /* Data to be written */
  237.     DWORD sector,       /* Start sector in LBA */
  238.     UINT count          /* Number of sectors to write */
  239. )
  240. {
  241.     unsigned char c = count;
  242.     if(DEV_NEMO_M == (pdrv & 0xfe)){
  243.         if(pdrv & 0x01)
  244.             output8(NEMO_LBA3,((unsigned char*)&sector)[3] | IDE_SLAVE);
  245.         else
  246.             output8(NEMO_LBA3,((unsigned char*)&sector)[3] | IDE_MASTER);
  247.         while((input8(NEMO_STAT) & ATA_STAT_BSY) != 0);
  248.         output8(NEMO_LBA2,((unsigned char*)&sector)[2]);
  249.         output8(NEMO_LBA1,((unsigned char*)&sector)[1]);
  250.         output8(NEMO_LBA0,((unsigned char*)&sector)[0]);
  251.         output8(NEMO_COUNT,c);
  252.         output8(NEMO_CMD, ATA_CMD_WRITE_SECTORS);
  253.         while((input8(NEMO_STAT) & (ATA_STAT_BSY | ATA_STAT_DRQ)) != 8);
  254.         while(c--){
  255.             output_block_inc(NEMO_DATA, (void*)buff, 256);
  256.             buff += 256;
  257.             output_block_inc(NEMO_DATA, (void*)buff, 256);
  258.             buff += 256;
  259.             /*
  260.             unsigned int i = 512;
  261.             while(i--){
  262.                 output8(NEMO_DATA,*buff);
  263.                 buff++;
  264.             }
  265.             */
  266.             while((input8(NEMO_STAT) & ATA_STAT_BSY) != 0);
  267.         }
  268.         return RES_OK;
  269.     }else if(DEV_ATM_M == (pdrv & 0xfe)){
  270.         if(pdrv & 0x01)
  271.             output(ATM_LBA3,((unsigned char*)&sector)[3] | IDE_SLAVE);
  272.         else
  273.             output(ATM_LBA3,((unsigned char*)&sector)[3] | IDE_MASTER);
  274.         while((input(ATM_STAT) & ATA_STAT_BSY) != 0);
  275.         output(ATM_LBA2,((unsigned char*)&sector)[2]);
  276.         output(ATM_LBA1,((unsigned char*)&sector)[1]);
  277.         output(ATM_LBA0,((unsigned char*)&sector)[0]);
  278.         output(ATM_COUNT,c);
  279.         output(ATM_CMD, ATA_CMD_WRITE_SECTORS);
  280.         while((input(ATM_STAT) & (ATA_STAT_BSY | ATA_STAT_DRQ)) != 8);
  281.         while(c--){
  282.             BYTE * i = (BYTE *)buff + 512;
  283.             while(buff != i){
  284.                 output(ATM_DATA_HI, buff[1]);
  285.                 output(ATM_DATA_LOW, buff[0]);
  286.                 buff += 2;
  287.             }
  288.             while((input(ATM_STAT) & ATA_STAT_BSY) != 0);
  289.         }
  290.         return RES_OK;
  291.     }
  292.  
  293.     return RES_PARERR;
  294. }
  295.  
  296. #endif
  297.  
  298.  
  299. /*-----------------------------------------------------------------------*/
  300. /* Miscellaneous Functions                                               */
  301. /*-----------------------------------------------------------------------*/
  302.  
  303. DRESULT disk_ioctl (
  304.     BYTE pdrv,      /* Physical drive nmuber (0..) */
  305.     BYTE cmd,       /* Control code */
  306.     void *buff      /* Buffer to send/receive control data */
  307. )
  308. {
  309.     if((pdrv == DEV_NEMO_M) && (cmd == GET_SECTOR_COUNT)){
  310.         if(pdrv == DEV_NEMO_M){
  311.             output8(NEMO_LBA3,0xe0);
  312.             output8(NEMO_CMD, ATA_CMD_IDENTIFY_DRIVE);
  313.             while((input8(NEMO_STAT) & (ATA_STAT_BSY | ATA_STAT_DRQ)) != 8);
  314.            
  315.             input_block_inc(NEMO_DATA, tmpbuf, 256);
  316.             input_block_inc(NEMO_DATA, tmpbuf+256, 256);
  317.             while((input8(NEMO_STAT) & ATA_STAT_BSY) != 0);
  318.             *((unsigned long*)buff)=*((unsigned long*)(tmpbuf+120));
  319.             return RES_OK;
  320.         }else if(pdrv == DEV_ATM_M){
  321.             BYTE * i = (BYTE *)tmpbuf;
  322.             output(ATM_LBA3,0xe0);
  323.             output(ATM_CMD, ATA_CMD_IDENTIFY_DRIVE);
  324.             while((input(ATM_STAT) & (ATA_STAT_BSY | ATA_STAT_DRQ)) != 8);
  325.             {
  326.                 while((tmpbuf+512) != i){
  327.                     output(ATM_DATA_HI, i[1]);
  328.                     output(ATM_DATA_LOW, i[0]);
  329.                     i += 2;
  330.                 }
  331.             }
  332.             while((input(ATM_STAT) & ATA_STAT_BSY) != 0);
  333.             *((unsigned long*)buff)=*((unsigned long*)(i+120));
  334.             return RES_OK;
  335.         }
  336.         return RES_PARERR;
  337.     }else if(CTRL_SYNC == cmd){
  338.         return RES_OK;
  339.     }
  340.  
  341.     return RES_PARERR;
  342. }
  343.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement