5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
24 #if defined(SIMU) && !defined(SIMU_DISKIO)
25 #define __disk_read(...) (RES_OK)
26 #define __disk_write(...) (RES_OK)
29 #if 0 // set to 1 to enable traces
30 #define TRACE_DISK_CACHE(...) TRACE(__VA_ARGS__)
32 #define TRACE_DISK_CACHE(...)
37 DiskCacheBlock::DiskCacheBlock():
43 bool DiskCacheBlock::read(BYTE
* buff
, DWORD sector
, UINT count
)
45 if (sector
>= startSector
&& (sector
+count
) <= endSector
) {
46 TRACE_DISK_CACHE("\tcache read(%u, %u) from %p", (uint32_t)sector
, (uint32_t)count
, this);
47 memcpy(buff
, data
+ ((sector
- startSector
) * BLOCK_SIZE
), count
* BLOCK_SIZE
);
53 DRESULT
DiskCacheBlock::fill(BYTE drv
, BYTE
* buff
, DWORD sector
, UINT count
)
55 DRESULT res
= __disk_read(drv
, data
, sector
, DISK_CACHE_BLOCK_SECTORS
);
60 endSector
= sector
+ DISK_CACHE_BLOCK_SECTORS
;
61 memcpy(buff
, data
, count
* BLOCK_SIZE
);
62 TRACE_DISK_CACHE("\tcache %p FILLED from read(%u, %u)", this, (uint32_t)sector
, (uint32_t)count
);
66 void DiskCacheBlock::free(DWORD sector
, UINT count
)
68 if (sector
< endSector
&& (sector
+count
) > startSector
) {
69 TRACE_DISK_CACHE("\tINVALIDATING disk cache block %p (%u)", this, startSector
);
74 void DiskCacheBlock::free()
79 bool DiskCacheBlock::empty() const
81 return (endSector
== 0);
84 DiskCache::DiskCache():
90 blocks
= new DiskCacheBlock
[DISK_CACHE_BLOCKS_NUM
];
93 void DiskCache::clear()
99 for (int n
=0; n
<DISK_CACHE_BLOCKS_NUM
; ++n
) {
104 DRESULT
DiskCache::read(BYTE drv
, BYTE
* buff
, DWORD sector
, UINT count
)
106 // TODO: check if not caching first sectors would improve anything
107 // if (sector < 1000) {
109 // return __disk_read(drv, buff, sector, count);
112 // if read is bigger than cache block, then read it directly without using cache
113 if (count
> DISK_CACHE_BLOCK_SECTORS
) {
114 TRACE_DISK_CACHE("\t\t big read(%u, %u)", (uint32_t)sector
, (uint32_t)count
);
115 return __disk_read(drv
, buff
, sector
, count
);
118 // if block + cache block size is beyond the end of the disk, then read it directly without using cache
119 if (sector
+DISK_CACHE_BLOCK_SECTORS
>= sdGetNoSectors()) {
120 TRACE_DISK_CACHE("\t\t cache would be beyond end of disk %u (%u)", (uint32_t)sector
, sdGetNoSectors());
121 return __disk_read(drv
, buff
, sector
, count
);
124 for (int n
=0; n
<DISK_CACHE_BLOCKS_NUM
; ++n
) {
125 if (blocks
[n
].read(buff
, sector
, count
)) {
134 for (int n
=0; n
<DISK_CACHE_BLOCKS_NUM
; ++n
) {
135 if (blocks
[n
].empty()) {
136 TRACE_DISK_CACHE("\t\t using free block");
137 return blocks
[n
].fill(drv
, buff
, sector
, count
);
141 // use next block (round robin)
142 // TODO: use better strategy to select which used block gets used here
143 if (++lastBlock
>= DISK_CACHE_BLOCKS_NUM
) {
147 return blocks
[lastBlock
].fill(drv
, buff
, sector
, count
);
150 DRESULT
DiskCache::write(BYTE drv
, const BYTE
* buff
, DWORD sector
, UINT count
)
153 for(int n
=0; n
< DISK_CACHE_BLOCKS_NUM
; ++n
) {
154 blocks
[n
].free(sector
, count
);
156 return __disk_write(drv
, buff
, sector
, count
);
159 const DiskCacheStats
& DiskCache::getStats() const
164 int DiskCache::getHitRate() const
166 uint32_t all
= stats
.noHits
+ stats
.noMisses
;
167 if (all
== 0) return 0;
168 return (stats
.noHits
* 1000) / all
;
171 DRESULT
disk_read(BYTE drv
, BYTE
* buff
, DWORD sector
, UINT count
)
173 return diskCache
.read(drv
, buff
, sector
, count
);
177 DRESULT
disk_write(BYTE drv
, const BYTE
* buff
, DWORD sector
, UINT count
)
179 return diskCache
.write(drv
, buff
, sector
, count
);