[270] | 1 | /*-----------------------------------------------------------------------*/
|
---|
| 2 | /* PFF - Low level disk control module for Win32 (C)ChaN, 2014 */
|
---|
| 3 | /*-----------------------------------------------------------------------*/
|
---|
| 4 |
|
---|
| 5 | #include <string.h>
|
---|
| 6 | #include "ffconf.h"
|
---|
| 7 | #include "diskio.h"
|
---|
| 8 |
|
---|
| 9 | /*--------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | Module Private Functions
|
---|
| 12 |
|
---|
| 13 | ---------------------------------------------------------------------------*/
|
---|
| 14 |
|
---|
| 15 | #define MAX_DRIVES 1 /* Max number of physical drives to be used */
|
---|
| 16 |
|
---|
[279] | 17 | extern unsigned char RamDisk[];
|
---|
[270] | 18 |
|
---|
| 19 | typedef struct {
|
---|
| 20 | DSTATUS status;
|
---|
| 21 | WORD sz_sector;
|
---|
| 22 | DWORD wofs;
|
---|
| 23 | DWORD wip;
|
---|
| 24 | DWORD n_sectors;
|
---|
| 25 | } RD_STAT;
|
---|
| 26 |
|
---|
| 27 | static volatile
|
---|
| 28 | RD_STAT Stat[MAX_DRIVES];
|
---|
| 29 |
|
---|
| 30 | static
|
---|
| 31 | BYTE Buffer[SS_RAMDISK];
|
---|
| 32 |
|
---|
| 33 | static
|
---|
| 34 | int get_status(volatile RD_STAT *stat) {
|
---|
| 35 | stat->sz_sector = SS_RAMDISK;
|
---|
[434] | 36 | if(stat->sz_sector < FF_MIN_SS || stat->sz_sector > FF_MAX_SS) return 0;
|
---|
[270] | 37 | stat->n_sectors = SZ_RAMDISK * 1024 / SS_RAMDISK;
|
---|
| 38 | stat->status = 0;
|
---|
| 39 | return 1;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /*-----------------------------------------------------------------------*/
|
---|
| 43 | /* Initialize Disk Drive */
|
---|
| 44 | /*-----------------------------------------------------------------------*/
|
---|
| 45 | DSTATUS ramdisk_initialize (void)
|
---|
| 46 | {
|
---|
| 47 | DSTATUS sta;
|
---|
| 48 |
|
---|
| 49 | get_status(&Stat[0]);
|
---|
| 50 | sta = Stat[0].status;
|
---|
| 51 | Stat[0].wip = 0;
|
---|
| 52 |
|
---|
| 53 | return sta;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /*-----------------------------------------------------------------------*/
|
---|
| 57 | /* Get Disk Status */
|
---|
| 58 | /*-----------------------------------------------------------------------*/
|
---|
| 59 | DSTATUS ramdisk_get_status (void)
|
---|
| 60 | {
|
---|
| 61 | return Stat[0].status;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /*-----------------------------------------------------------------------*/
|
---|
| 65 | /* Read Partial Sector */
|
---|
| 66 | /*-----------------------------------------------------------------------*/
|
---|
| 67 | DRESULT ramdisk_read (
|
---|
| 68 | BYTE* buff, /* Data read buffer */
|
---|
| 69 | DWORD sector, /* Sector number (LBA) */
|
---|
| 70 | UINT count /* Number of sectors to read (1..128) */
|
---|
| 71 | )
|
---|
| 72 | {
|
---|
| 73 | DRESULT res;
|
---|
| 74 | int dofs;
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | if(Stat[0].status & STA_NOINIT) return RES_NOTRDY;
|
---|
| 78 |
|
---|
| 79 | if(sector >= Stat[0].n_sectors || !count || (sector + count) > Stat[0].n_sectors/* || Stat[0].wip*/) {
|
---|
| 80 | res = RES_PARERR;
|
---|
| 81 | }
|
---|
| 82 | else {
|
---|
| 83 | if(buff) {
|
---|
| 84 | dofs = sector * Stat[0].sz_sector;
|
---|
| 85 | memcpy(buff, &RamDisk[dofs], count * Stat[0].sz_sector);
|
---|
| 86 | res = RES_OK;
|
---|
| 87 | }
|
---|
| 88 | else {
|
---|
| 89 | res = RES_OK;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return res;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /*-----------------------------------------------------------------------*/
|
---|
| 97 | /* Write Partial Sector */
|
---|
| 98 | /*-----------------------------------------------------------------------*/
|
---|
| 99 | DRESULT ramdisk_write (
|
---|
| 100 | const BYTE* buff, /* Pointer to the write data */
|
---|
| 101 | DWORD sc, /* Sector number (LBA), Number of bytes to send */
|
---|
| 102 | UINT count /* Number of sectors to read (1..128) */
|
---|
| 103 | )
|
---|
| 104 | {
|
---|
| 105 | DRESULT res;
|
---|
| 106 | int dofs;
|
---|
| 107 |
|
---|
| 108 | if(Stat[0].status & STA_NOINIT) return RES_NOTRDY;
|
---|
| 109 |
|
---|
| 110 | if(!buff) {
|
---|
| 111 | if(sc) {
|
---|
| 112 | Stat[0].wip = sc;
|
---|
| 113 | Stat[0].wofs = 0;
|
---|
| 114 | memset(Buffer, 0, sizeof Buffer);
|
---|
| 115 | res = RES_OK;
|
---|
| 116 | }
|
---|
| 117 | else {
|
---|
| 118 | if(Stat[0].wip) {
|
---|
| 119 | dofs = Stat[0].wip * Stat[0].sz_sector;
|
---|
| 120 | memcpy(RamDisk + dofs, Buffer, Stat[0].sz_sector);
|
---|
| 121 | Stat[0].wip = 0;
|
---|
| 122 | res = RES_OK;
|
---|
| 123 | }
|
---|
| 124 | else {
|
---|
| 125 | res = RES_OK;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | else {
|
---|
| 130 | if(Stat[0].wofs + sc > Stat[0].sz_sector) sc = Stat[0].sz_sector - Stat[0].wofs;
|
---|
| 131 | memcpy(&Buffer[Stat[0].wofs], buff, sc);
|
---|
| 132 | Stat[0].wofs += sc;
|
---|
| 133 | res = RES_OK;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return res;
|
---|
| 137 | }
|
---|