1 | /*-----------------------------------------------------------------------*/
|
---|
2 | /* PFF - Low level disk control module for Win32 (C)ChaN, 2014 */
|
---|
3 | /*-----------------------------------------------------------------------*/
|
---|
4 |
|
---|
5 | #include <string.h>
|
---|
6 | #include "pffconf.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 |
|
---|
17 | #define SZ_RAMDISK 768 /* Size of RAM disk [kB] */
|
---|
18 | #define SS_RAMDISK 512 /* Sector size of RAM disk [byte] */
|
---|
19 |
|
---|
20 | #ifndef _MSC_VER
|
---|
21 | #define RamDisk ((unsigned char *)0xfff00000)
|
---|
22 | #else
|
---|
23 | unsigned char RamDisk[SZ_RAMDISK * 1024];
|
---|
24 | extern void win_disk_initialize(void *ramDisk, int size);
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | typedef struct {
|
---|
28 | DSTATUS status;
|
---|
29 | WORD sz_sector;
|
---|
30 | DWORD wofs;
|
---|
31 | DWORD wip;
|
---|
32 | DWORD n_sectors;
|
---|
33 | } RD_STAT;
|
---|
34 |
|
---|
35 | static volatile
|
---|
36 | RD_STAT Stat[MAX_DRIVES];
|
---|
37 |
|
---|
38 | static
|
---|
39 | BYTE Buffer[SS_RAMDISK];
|
---|
40 |
|
---|
41 | static
|
---|
42 | int get_status(volatile RD_STAT *stat) {
|
---|
43 | stat->sz_sector = SS_RAMDISK;
|
---|
44 | if(stat->sz_sector < _MIN_SS || stat->sz_sector > _MAX_SS) return 0;
|
---|
45 | stat->n_sectors = SZ_RAMDISK * 1024 / SS_RAMDISK;
|
---|
46 | stat->status = 0;
|
---|
47 | return 1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /*-----------------------------------------------------------------------*/
|
---|
51 | /* Initialize Disk Drive */
|
---|
52 | /*-----------------------------------------------------------------------*/
|
---|
53 | DSTATUS ramdisk_initialize (void)
|
---|
54 | {
|
---|
55 | DSTATUS sta;
|
---|
56 | #ifdef _MSC_VER
|
---|
57 | win_disk_initialize(RamDisk, sizeof(RamDisk));
|
---|
58 | #endif
|
---|
59 | get_status(&Stat[0]);
|
---|
60 | sta = Stat[0].status;
|
---|
61 | Stat[0].wip = 0;
|
---|
62 |
|
---|
63 | return sta;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*-----------------------------------------------------------------------*/
|
---|
67 | /* Get Disk Status */
|
---|
68 | /*-----------------------------------------------------------------------*/
|
---|
69 | DSTATUS ramdisk_get_status (void)
|
---|
70 | {
|
---|
71 | return Stat[0].status;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*-----------------------------------------------------------------------*/
|
---|
75 | /* Read Partial Sector */
|
---|
76 | /*-----------------------------------------------------------------------*/
|
---|
77 | DRESULT ramdisk_readp (
|
---|
78 | BYTE* buff, /* Data read buffer */
|
---|
79 | DWORD sector, /* Sector number (LBA) */
|
---|
80 | UINT offset, /* Offset in the sector */
|
---|
81 | UINT count /* Byte count */
|
---|
82 | )
|
---|
83 | {
|
---|
84 | DRESULT res;
|
---|
85 | int dofs;
|
---|
86 |
|
---|
87 |
|
---|
88 | if(Stat[0].status & STA_NOINIT) return RES_NOTRDY;
|
---|
89 |
|
---|
90 | if(sector >= Stat[0].n_sectors || !count || offset + count > Stat[0].sz_sector/* || Stat[0].wip*/) {
|
---|
91 | res = RES_PARERR;
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | if(buff) {
|
---|
95 | dofs = sector * Stat[0].sz_sector;
|
---|
96 | memcpy(buff, &RamDisk[dofs + offset], count);
|
---|
97 | res = RES_OK;
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | res = RES_OK;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | return res;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*-----------------------------------------------------------------------*/
|
---|
108 | /* Write Partial Sector */
|
---|
109 | /*-----------------------------------------------------------------------*/
|
---|
110 | DRESULT ramdisk_writep (
|
---|
111 | const BYTE* buff, /* Pointer to the write data */
|
---|
112 | DWORD sc /* Sector number (LBA), Number of bytes to send */
|
---|
113 | )
|
---|
114 | {
|
---|
115 | DRESULT res;
|
---|
116 | int dofs;
|
---|
117 |
|
---|
118 | if(Stat[0].status & STA_NOINIT) return RES_NOTRDY;
|
---|
119 |
|
---|
120 | if(!buff) {
|
---|
121 | if(sc) {
|
---|
122 | Stat[0].wip = sc;
|
---|
123 | Stat[0].wofs = 0;
|
---|
124 | memset(Buffer, 0, sizeof Buffer);
|
---|
125 | res = RES_OK;
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | if(Stat[0].wip) {
|
---|
129 | dofs = Stat[0].wip * Stat[0].sz_sector;
|
---|
130 | memcpy(RamDisk + dofs, Buffer, Stat[0].sz_sector);
|
---|
131 | Stat[0].wip = 0;
|
---|
132 | res = RES_OK;
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | res = RES_OK;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | if(Stat[0].wofs + sc > Stat[0].sz_sector) sc = Stat[0].sz_sector - Stat[0].wofs;
|
---|
141 | memcpy(&Buffer[Stat[0].wofs], buff, sc);
|
---|
142 | Stat[0].wofs += sc;
|
---|
143 | res = RES_OK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | return res;
|
---|
147 | }
|
---|