39 lines
1,007 B
C++
39 lines
1,007 B
C++
#ifndef _CACHE_H_DEF
|
|
#define _CACHE_H_DEF
|
|
#include "norflash.h"
|
|
|
|
template<typename T, const unsigned N> class Cache {
|
|
T data [N];
|
|
unsigned begin_address;
|
|
unsigned ofset_current;
|
|
NorFlash nor;
|
|
public:
|
|
explicit Cache () noexcept : begin_address(0u), ofset_current(0u), nor() {}
|
|
Cache & operator= (const unsigned addr) {
|
|
begin_address = addr;
|
|
ofset_current = 0u;
|
|
reload ();
|
|
return * this;
|
|
}
|
|
operator bool () { if (begin_address == 0u) return false; return true; }
|
|
T * operator++ (int) {
|
|
check_validity ();
|
|
T * tmp = data + ofset_current;
|
|
ofset_current += 1;
|
|
return tmp;
|
|
}
|
|
protected:
|
|
void check_validity () {
|
|
if (ofset_current >= N) {
|
|
begin_address += N * sizeof(T);
|
|
reload ();
|
|
ofset_current = 0u;
|
|
}
|
|
}
|
|
void reload () {
|
|
unsigned char * ptr = reinterpret_cast<unsigned char *>(data);
|
|
nor.ReadBlock (begin_address, ptr, N * sizeof(T));
|
|
}
|
|
};
|
|
|
|
#endif // _CACHE_H_DEF
|