Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / disk_cache.cpp
blob5553aedf35ba6e0a4ccdead98a3a059283fcadfb
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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.
21 #include <string.h>
22 #include "opentx.h"
24 #if defined(SIMU) && !defined(SIMU_DISKIO)
25 #define __disk_read(...) (RES_OK)
26 #define __disk_write(...) (RES_OK)
27 #endif
29 #if 0 // set to 1 to enable traces
30 #define TRACE_DISK_CACHE(...) TRACE(__VA_ARGS__)
31 #else
32 #define TRACE_DISK_CACHE(...)
33 #endif
35 DiskCache diskCache;
37 DiskCacheBlock::DiskCacheBlock():
38 startSector(0),
39 endSector(0)
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);
48 return true;
50 return false;
53 DRESULT DiskCacheBlock::fill(BYTE drv, BYTE * buff, DWORD sector, UINT count)
55 DRESULT res = __disk_read(drv, data, sector, DISK_CACHE_BLOCK_SECTORS);
56 if (res != RES_OK) {
57 return res;
59 startSector = sector;
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);
63 return RES_OK;
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);
70 endSector = 0;
74 void DiskCacheBlock::free()
76 endSector = 0;
79 bool DiskCacheBlock::empty() const
81 return (endSector == 0);
84 DiskCache::DiskCache():
85 lastBlock(0)
87 stats.noHits = 0;
88 stats.noMisses = 0;
89 stats.noWrites = 0;
90 blocks = new DiskCacheBlock[DISK_CACHE_BLOCKS_NUM];
93 void DiskCache::clear()
95 lastBlock = 0;
96 stats.noHits = 0;
97 stats.noMisses = 0;
98 stats.noWrites = 0;
99 for (int n=0; n<DISK_CACHE_BLOCKS_NUM; ++n) {
100 blocks[n].free();
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) {
108 // ++stats.noMisses;
109 // return __disk_read(drv, buff, sector, count);
110 // }
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)) {
126 ++stats.noHits;
127 return RES_OK;
131 ++stats.noMisses;
133 // find free block
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) {
144 lastBlock = 0;
147 return blocks[lastBlock].fill(drv, buff, sector, count);
150 DRESULT DiskCache::write(BYTE drv, const BYTE* buff, DWORD sector, UINT count)
152 ++stats.noWrites;
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
161 return stats;
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);