forked from oliverschmidt/a2retronet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_cache.c
More file actions
424 lines (332 loc) · 10.2 KB
/
block_cache.c
File metadata and controls
424 lines (332 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
MIT License
Copyright (c) 2025 Michael Neil (Far Left Lane)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "block_cache.h"
#include <ff.h>
#include <string.h>
#include <diskio.h>
#include <stdbool.h>
#if IO_STATS
#include <stdio.h>
#include "hw_config.h"
uint32_t s_block_cache_read_count = 0;
uint32_t s_block_cache_read_ahead_count = 0;
uint32_t s_block_cache_read_hit_count = 0;
uint32_t s_block_cache_read_free_count = 0;
uint32_t s_block_cache_read_evict_count = 0;
uint32_t s_block_cache_read_miss_count = 0;
uint32_t s_block_cache_write_count = 0;
uint32_t s_block_cache_write_hit_count = 0;
uint32_t s_block_cache_write_free_count = 0;
uint32_t s_block_cache_write_evict_count = 0;
uint32_t s_block_cache_write_flush_count = 0;
#endif
#define BLOCK_SIZE 512
#define CACHE_SIZE 128 // Number of cache entries, 128 = 64K bytes
#define HASH_SIZE 257 // Prime number bucket count
/* -------------------------------
cache_entry
block identifies and state
hash chain
lru double linked list
block data
------------------------------- */
typedef struct cache_entry
{
LBA_t sector; // Sector (can be 64-bit or 32-bit based on FF_LBA64 from ff.h)
BYTE pdrv; // Drive number
bool dirty; // Needs write-back
bool valid; // Entry active
struct cache_entry *hash_next; // hash chain
struct cache_entry *lru_prev; // LRU doubly-linked list
struct cache_entry *lru_next;
uint8_t data[BLOCK_SIZE]; // Cached contents
} cache_entry;
// cache storage
static cache_entry s_cache[CACHE_SIZE];
static cache_entry *s_hash_table[HASH_SIZE];
static bool s_dirty_blocks = true;
// LRU list: entry at head = most recently used. tail = eviction candidate
static cache_entry *s_lru_head = NULL;
static cache_entry *s_lru_tail = NULL;
// Free list
static cache_entry *s_free_head = NULL;
// LRU functions
static void lru_remove(cache_entry *e)
{
if (e->lru_prev) e->lru_prev->lru_next = e->lru_next;
if (e->lru_next) e->lru_next->lru_prev = e->lru_prev;
if (s_lru_head == e) s_lru_head = e->lru_next;
if (s_lru_tail == e) s_lru_tail = e->lru_prev;
e->lru_prev = e->lru_next = NULL;
}
static void lru_insert_front(cache_entry *e)
{
e->lru_next = s_lru_head;
e->lru_prev = NULL;
if (s_lru_head) s_lru_head->lru_prev = e;
s_lru_head = e;
if (!s_lru_tail)
s_lru_tail = e;
}
// Move entry to front on page hit
static void lru_touch(cache_entry *e)
{
if (s_lru_head == e) return;
lru_remove(e);
lru_insert_front(e);
}
// free functions, we re-use the hash pointers
static cache_entry *free_get(void)
{
if (s_free_head == NULL)
return NULL;
cache_entry *result = s_free_head;;
s_free_head = result->hash_next;
return result;
}
static void free_insert(cache_entry *e)
{
e->valid = false;
e->hash_next = s_free_head;
s_free_head = e;
}
// Hash table functions
static uint32_t hash_key(BYTE pdrv, LBA_t sector)
{
// Need to colapse 5 or 9 bytes into 4 for hashing
#if FF_LBA64
uint32_t value = (sector & 0xFFFFFFFF) ^ (sector >> 32) ^ (pdrv << (32 - sizeof(BYTE)));
#else
uint32_t value = (sector) ^ (pdrv << (32 - sizeof(BYTE)));
#endif
return (value * 2654435761u) % HASH_SIZE; // Simple, fast multiplicative hash
}
static cache_entry *hash_lookup(BYTE pdrv, LBA_t sector)
{
cache_entry *e = s_hash_table[hash_key(pdrv, sector)];
while (e)
{
if ((e->valid) && (e->sector == sector) && (e->pdrv == pdrv))
return e;
e = e->hash_next;
}
return NULL;
}
static void hash_insert(cache_entry *e)
{
uint32_t h = hash_key(e->pdrv, e->sector);
e->hash_next = s_hash_table[h];
s_hash_table[h] = e;
}
static void hash_remove(cache_entry *e)
{
uint32_t h = hash_key(e->pdrv, e->sector);
cache_entry **pp = &s_hash_table[h];
while (*pp)
{
if (*pp == e)
{
*pp = e->hash_next;
e->hash_next = NULL;
return;
}
pp = &(*pp)->hash_next;
}
}
// Cache functions
static DRESULT s_evict_error = RES_OK;
static cache_entry *evict_entry(void)
{
cache_entry *e = s_lru_tail;
if (!e)
{
s_evict_error = RES_PARERR;
return NULL;
}
// Write back if dirty
if (e->valid && e->dirty)
{
s_evict_error = disk_write_no_cache (e->pdrv, e->data, e->sector, 1); // 1 is a 512 byte sector
if (s_evict_error != RES_OK)
{
return NULL;
}
}
// Remove from cache
hash_remove(e);
lru_remove(e);
e->valid = false;
e->dirty = false;
return e;
}
// Public functions
void block_cache_init(void)
{
memset(s_cache, 0, sizeof(s_cache));
memset(s_hash_table, 0, sizeof(s_hash_table));
s_lru_head = NULL; // LRU list initially empty; entries inserted when used
s_lru_tail = NULL;
for (int i=0; i<CACHE_SIZE; i++) // Populate the free list
{
if (!s_cache[i].valid)
{
free_insert(&s_cache[i]);
}
}
}
DRESULT block_cache_read_block(BYTE pdrv, LBA_t sector, BYTE *out_data)
{
#if IO_STATS
if (out_data == NULL)
s_block_cache_read_ahead_count++;
else
s_block_cache_read_count++;
#endif
cache_entry *e = hash_lookup(pdrv, sector);
if (e)
{
// Cache hit
lru_touch(e);
if(out_data != NULL)
memcpy(out_data, e->data, BLOCK_SIZE);
#if IO_STATS
if (out_data != NULL)
s_block_cache_read_hit_count++;
#endif
return 0;
}
// Cache miss → find free entry or evict
cache_entry *free_entry = free_get();
#if IO_STATS
if (free_entry != NULL)
s_block_cache_read_free_count++;
#endif
if (!free_entry)
{
free_entry = evict_entry();
#if IO_STATS
s_block_cache_read_evict_count++;
#endif
}
if (!free_entry)
{
return s_evict_error;
}
#if IO_STATS
if (out_data != NULL)
s_block_cache_read_miss_count++;
#endif
// Load block from device
DRESULT result = disk_read_no_cache(pdrv, free_entry->data, sector, 1);
if (result != RES_OK)
return result;
free_entry->sector = sector;
free_entry->pdrv = pdrv;
free_entry->dirty = false;
free_entry->valid = true;
// Add to hash + LRU
hash_insert(free_entry);
lru_insert_front(free_entry);
if(out_data != NULL)
memcpy(out_data, free_entry->data, BLOCK_SIZE);
return RES_OK;
}
DRESULT block_cache_write_block(BYTE pdrv, LBA_t sector, const BYTE *in_data)
{
#if IO_STATS
s_block_cache_write_count++;
#endif
cache_entry *e = hash_lookup(pdrv, sector);
if (e)
{
// Cache hit
memcpy(e->data, in_data, BLOCK_SIZE);
e->dirty = true;
s_dirty_blocks = true;
lru_touch(e);
#if IO_STATS
s_block_cache_write_hit_count++;
#endif
return 0;
}
// Cache miss → free or evict
cache_entry *free_entry = free_get();
#if IO_STATS
if (free_entry != NULL)
s_block_cache_write_free_count++;
#endif
if (!free_entry)
{
free_entry = evict_entry();
#if IO_STATS
s_block_cache_write_evict_count++;
#endif
}
if (!free_entry)
{
return s_evict_error;
}
// No need to read from device for write
memcpy(free_entry->data, in_data, BLOCK_SIZE);
free_entry->sector = sector;
free_entry->pdrv = pdrv;
free_entry->dirty = true;
free_entry->valid = true;
s_dirty_blocks = true;
// Add to structures
hash_insert(free_entry);
lru_insert_front(free_entry);
return 0;
}
DRESULT block_cache_flush(bool flush_all, bool invalidate_all)
{
if (s_dirty_blocks == false) // Optimization
return RES_OK;
for (int i=0; i<CACHE_SIZE; i++)
{
if (s_cache[i].valid && s_cache[i].dirty)
{
DRESULT result = disk_write_no_cache (s_cache[i].pdrv, s_cache[i].data, s_cache[i].sector, 1); // 1 is a 512 byte sector
if (result != RES_OK)
{
return result;
}
#if IO_STATS
s_block_cache_write_flush_count++;
#endif
s_cache[i].dirty = false;
if (flush_all == false)
return RES_OK; // Flush only one
}
if (invalidate_all) // Flush all of the cache entries
s_cache[i].valid = false;
}
s_dirty_blocks = false; // Clear the flag
return RES_OK;
}
void block_cache_print_stats(void)
{
#if IO_STATS
printf("block_cache:, Reads, %d, Ahead, %d, Hit, %d, Free, %d, Evict, %d, Miss, %d, ---, Writes, %d, Hit, %d, Free, %d, Evict, %d, Flush, %d,\n",
s_block_cache_read_count, s_block_cache_read_ahead_count, s_block_cache_read_hit_count, s_block_cache_read_free_count, s_block_cache_read_evict_count, s_block_cache_read_miss_count,
s_block_cache_write_count, s_block_cache_write_hit_count, s_block_cache_write_free_count, s_block_cache_write_evict_count, s_block_cache_write_flush_count
);
#endif
}