mirror of
https://github.com/opentx/opentx.git
synced 2025-07-19 06:15:10 +03:00
Fixes #3374: added SD card disk cache for Horus
* SIMU_DISKIO option available again * new option DISK_CACHE defaults to ON for Horus * printf("%f") fixed in CLI
This commit is contained in:
parent
ee9bb73ef9
commit
868133371c
14 changed files with 334 additions and 20 deletions
153
radio/src/disk_cache.cpp
Normal file
153
radio/src/disk_cache.cpp
Normal file
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "opentx.h"
|
||||
|
||||
#if defined(SIMU) && !defined(SIMU_DISKIO)
|
||||
#define __disk_read(...) (RES_OK)
|
||||
#define __disk_write(...) (RES_OK)
|
||||
#else
|
||||
extern DRESULT __disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
|
||||
extern DRESULT __disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
|
||||
#endif
|
||||
|
||||
#if 0 // set to 1 to enable traces
|
||||
#define TRACE_DISK_CACHE(...) TRACE(__VA_ARGS__)
|
||||
#else
|
||||
#define TRACE_DISK_CACHE(...)
|
||||
#endif
|
||||
|
||||
DiskCache diskCache;
|
||||
|
||||
DiskCacheBlock::DiskCacheBlock()
|
||||
: startSector(0),
|
||||
endSector(0)
|
||||
{
|
||||
}
|
||||
|
||||
bool DiskCacheBlock::read(BYTE* buff, DWORD sector, UINT count)
|
||||
{
|
||||
if (sector >= startSector && (sector+count) <= endSector) {
|
||||
TRACE_DISK_CACHE("\tcache read(%u, %u) from %p", (uint32_t)sector, (uint32_t)count, this);
|
||||
memcpy(buff, data + ((sector - startSector) * BLOCK_SIZE), count * BLOCK_SIZE);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
DRESULT DiskCacheBlock::fill(BYTE drv, BYTE* buff, DWORD sector, UINT count)
|
||||
{
|
||||
// TODO: check if trying to read beyond the end of disk
|
||||
DRESULT res = __disk_read(drv, data, sector, DISK_CACHE_BLOCK_SECTORS);
|
||||
if (res != RES_OK) {
|
||||
return res;
|
||||
}
|
||||
startSector = sector;
|
||||
endSector = sector + DISK_CACHE_BLOCK_SECTORS;
|
||||
memcpy(buff, data, count * BLOCK_SIZE);
|
||||
TRACE_DISK_CACHE("\tcache %p FILLED from read(%u, %u)", this, (uint32_t)sector, (uint32_t)count);
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
void DiskCacheBlock::free(DWORD sector, UINT count)
|
||||
{
|
||||
if (sector < endSector && (sector+count) > startSector) {
|
||||
TRACE_DISK_CACHE("\tINVALIDATING disk cache block %p (%u)", this, startSector);
|
||||
endSector = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool DiskCacheBlock::empty() const
|
||||
{
|
||||
return (endSector == 0);
|
||||
}
|
||||
|
||||
DiskCache::DiskCache()
|
||||
: lastBlock(0)
|
||||
{
|
||||
stats.noHits = 0;
|
||||
stats.noMisses = 0;
|
||||
blocks = new DiskCacheBlock[DISK_CACHE_BLOCKS_NUM];
|
||||
}
|
||||
|
||||
DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count)
|
||||
{
|
||||
// TODO: check if not caching first sectors would improve anything
|
||||
// if (sector < 1000) {
|
||||
// ++stats.noMisses;
|
||||
// return __disk_read(drv, buff, sector, count);
|
||||
// }
|
||||
|
||||
for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
|
||||
if (blocks[n].read(buff, sector, count)) {
|
||||
++stats.noHits;
|
||||
return RES_OK;
|
||||
}
|
||||
}
|
||||
|
||||
++stats.noMisses;
|
||||
|
||||
// find free block
|
||||
for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
|
||||
if (blocks[n].empty()) {
|
||||
TRACE_DISK_CACHE("\t\t using free block");
|
||||
return blocks[n].fill(drv, buff, sector, count);
|
||||
}
|
||||
}
|
||||
|
||||
// use next block (round robin)
|
||||
// TODO: use better strategy to select which used block gets used here
|
||||
if (++lastBlock >= DISK_CACHE_BLOCKS_NUM) {
|
||||
lastBlock = 0;
|
||||
}
|
||||
return blocks[lastBlock].fill(drv, buff, sector, count);
|
||||
}
|
||||
|
||||
DRESULT DiskCache::write(BYTE drv, const BYTE* buff, DWORD sector, UINT count)
|
||||
{
|
||||
for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
|
||||
blocks[n].free(sector, count);
|
||||
}
|
||||
return __disk_write(drv, buff, sector, count);
|
||||
}
|
||||
|
||||
const DiskCacheStats & DiskCache::getStats() const
|
||||
{
|
||||
return stats;
|
||||
}
|
||||
|
||||
int DiskCache::getHitRate() const
|
||||
{
|
||||
uint32_t all = stats.noHits + stats.noMisses;
|
||||
if (all == 0) return 0;
|
||||
return (stats.noHits * 1000) / all;
|
||||
}
|
||||
|
||||
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
return diskCache.read(drv, buff, sector, count);
|
||||
}
|
||||
|
||||
|
||||
DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
return diskCache.write(drv, buff, sector, count);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue