ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
cache_store_demo.c
Go to the documentation of this file.
1
18
19#include "cache_store_demo.h"
20
21#include <stdint.h>
22#include <string.h>
23
24#include "ra8_attributes.h"
25#include "ra8_cache_store.h"
26#include "ra8_check.h"
27#include "ra8_err.h"
28
36typedef enum : uint32_t {
37 k_demo_key_cover = 0xC07E5001U,
38 k_demo_key_tile_a = 0x11E0A002U,
39 k_demo_key_tile_b = 0x11E0B003U,
40 k_demo_key_tile_c = 0x11E0C004U,
41 k_demo_key_tile_d = 0x11E0D005U,
43
49typedef enum : uint32_t {
56
62typedef enum : uint8_t {
69
76typedef enum : uint32_t {
79
95RA8_INTERNAL static void internal_demo_fill(uint8_t* buf, uint32_t len, uint8_t seed)
96{
97 if (buf == nullptr) {
98 return;
99 }
100 if (len == 0U) {
101 return;
102 }
103 for (uint32_t i = 0U; i < len; i++) {
104 buf[i] = (uint8_t)(seed + (uint8_t)i);
105 }
106}
107
125RA8_INTERNAL static bool internal_demo_matches(const uint8_t* buf, uint32_t len, uint8_t seed)
126{
127 if (buf == nullptr) {
128 return false;
129 }
130 if (len == 0U) {
131 return true;
132 }
133 for (uint32_t i = 0U; i < len; i++) {
134 if (buf[i] != (uint8_t)(seed + (uint8_t)i)) {
135 return false;
136 }
137 }
138 return true;
139}
140
160{
161 RA8_CHECK_NULL_PTR(in, "rcs_demo", "cfg is NULL");
162 RA8_CHECK_NULL_PTR(out, "rcs_demo", "out cfg is NULL");
163 *out = (ra8_cache_store_cfg_t){
164 .nor_flash = in->nor_flash,
165 .nor_driver_init = in->nor_driver_init,
166 .name = "rcs_demo",
167 .index = in->index,
168 .staging = in->staging,
169 .staging_bytes = in->staging_bytes,
170 .logical_sectors = in->logical_sectors,
171 .index_cap = in->index_cap,
172 .overprovision_pct = 0U, /* 0 => the store's default GC headroom margin. */
173 .format = format,
174 };
175 return k_ra8_ok;
176}
177
201 uint32_t key,
202 uint8_t seed,
203 uint32_t len,
204 const cache_store_demo_cfg_t* cfg)
205{
206 RA8_CHECK_NULL_PTR(st, "rcs_demo", "store is NULL");
207 RA8_CHECK_NULL_PTR(cfg, "rcs_demo", "cfg is NULL");
208 if (len > cfg->scratch_bytes) {
210 }
211 internal_demo_fill(cfg->scratch, len, seed);
212 ra8_err_t rc = ra8_cache_store_put(st, key, cfg->scratch, len);
213 if (rc != k_ra8_ok) {
214 return rc;
215 }
217 rc = ra8_cache_store_get(st, key, &rd);
218 if (rc != k_ra8_ok) {
219 return rc;
220 }
221 if (rd.byte_len != len) {
223 }
224 (void)memset(cfg->scratch, 0, (size_t)len);
225 rc = ra8_cache_store_read(&rd, 0U, cfg->scratch, len);
226 if (rc != k_ra8_ok) {
227 return rc;
228 }
229 if (!internal_demo_matches(cfg->scratch, len, seed)) {
231 }
232 return k_ra8_ok;
233}
234
257 uint32_t key,
258 uint8_t seed,
259 uint32_t len,
260 const cache_store_demo_cfg_t* cfg)
261{
262 RA8_CHECK_NULL_PTR(st, "rcs_demo", "store is NULL");
263 RA8_CHECK_NULL_PTR(cfg, "rcs_demo", "cfg is NULL");
265 ra8_err_t rc = ra8_cache_store_get(st, key, &rd);
266 if (rc != k_ra8_ok) {
267 return rc;
268 }
269 if (rd.byte_len != len) {
271 }
272 (void)memset(cfg->scratch, 0, (size_t)len);
273 rc = ra8_cache_store_read(&rd, 0U, cfg->scratch, len);
274 if (rc != k_ra8_ok) {
275 return rc;
276 }
277 if (!internal_demo_matches(cfg->scratch, len, seed)) {
279 }
280 return k_ra8_ok;
281}
282
301 const cache_store_demo_cfg_t* cfg)
302{
303 RA8_CHECK_NULL_PTR(st, "rcs_demo", "store is NULL");
304 RA8_CHECK_NULL_PTR(cfg, "rcs_demo", "cfg is NULL");
305 ra8_err_t rc =
307 if (rc != k_ra8_ok) {
308 return rc;
309 }
310 rc =
312 if (rc != k_ra8_ok) {
313 return rc;
314 }
315 rc =
317 if (rc != k_ra8_ok) {
318 return rc;
319 }
324 cfg);
325}
326
348{
349 RA8_CHECK_NULL_PTR(st, "rcs_demo", "store is NULL");
351 if (rc != k_ra8_ok) {
352 return rc;
353 }
355 if (rc != k_ra8_ok) {
356 return rc;
357 }
360 return k_ra8_err_invalid_state; /* evicted entry must be a miss */
361 }
363 return k_ra8_err_invalid_state; /* survivor must resolve */
364 }
366 return k_ra8_err_invalid_state; /* survivor must resolve */
367 }
369 return k_ra8_err_invalid_state; /* pinned entry must refuse eviction */
370 }
371 return k_ra8_ok;
372}
373
392 const cache_store_demo_cfg_t* cfg)
393{
394 RA8_CHECK_NULL_PTR(st, "rcs_demo", "store is NULL");
395 RA8_CHECK_NULL_PTR(cfg, "rcs_demo", "cfg is NULL");
396 ra8_err_t rc =
398 if (rc != k_ra8_ok) {
399 return rc;
400 }
401 rc =
403 if (rc != k_ra8_ok) {
404 return rc;
405 }
406 rc =
408 if (rc != k_ra8_ok) {
409 return rc;
410 }
415 cfg);
416}
417
442{
443 RA8_CHECK_NULL_PTR(cfg, "rcs_demo", "cfg is NULL");
444 RA8_CHECK_NULL_PTR(out, "rcs_demo", "out is NULL");
445 /* Reboot: control state is lost, the media survives. */
446 (void)memset(cfg->index, 0, (size_t)cfg->index_cap * sizeof(cfg->index[0]));
447 (void)memset(cfg->staging, 0, (size_t)cfg->staging_bytes);
448 ra8_cache_store_cfg_t cs2 = {};
449 if (internal_demo_build_cfg(cfg, false, &cs2) != k_ra8_ok) {
452 }
453 ra8_cache_store_t st2 = {};
454 if (ra8_cache_store_init(&st2, &cs2) != k_ra8_ok) {
457 }
459 if (internal_demo_verify_survivors(&st2, cfg) != k_ra8_ok) {
462 }
468 }
469 out->evicted_stayed_gone = true;
473 }
474 out->pin_survived = true;
475 (void)ra8_cache_store_close(&st2);
476 return k_ra8_ok;
477}
478
480{
481 RA8_CHECK_NULL_PTR(cfg, "rcs_demo", "cfg is NULL");
482 RA8_CHECK_NULL_PTR(out, "rcs_demo", "out is NULL");
485 .last_stage = k_cache_store_demo_stage_none,
486 .survivors = 0U,
487 .evicted_key = k_demo_key_tile_b,
488 .evicted_stayed_gone = false,
489 .pin_survived = false,
490 };
491
492 ra8_cache_store_cfg_t cs = {};
493 if (internal_demo_build_cfg(cfg, true, &cs) != k_ra8_ok) {
496 }
497 ra8_cache_store_t st = {};
498 if (ra8_cache_store_init(&st, &cs) != k_ra8_ok) {
501 }
503
504 if (internal_demo_phase_seed(&st, cfg) != k_ra8_ok) {
507 }
509
513 }
515
516 /* Reuse: the sectors tile_b freed now hold tile_d. */
521 cfg) != k_ra8_ok) {
524 }
526
527 if (ra8_cache_store_close(&st) != k_ra8_ok) {
530 }
532
533 if (internal_demo_phase_remount(cfg, out) != k_ra8_ok) {
534 return k_ra8_err_invalid_state; /* internal_demo_phase_remount set out->status */
535 }
538 return k_ra8_ok;
539}
static ra8_err_t internal_demo_put_and_verify(ra8_cache_store_t *st, uint32_t key, uint8_t seed, uint32_t len, const cache_store_demo_cfg_t *cfg)
Put a blob, then read it back whole and verify it byte-identical.
static ra8_err_t internal_demo_phase_remount(const cache_store_demo_cfg_t *cfg, cache_store_demo_result_t *out)
Simulate a reboot: re-mount over the same media and confirm survival.
static ra8_err_t internal_demo_get_and_verify(const ra8_cache_store_t *st, uint32_t key, uint8_t seed, uint32_t len, const cache_store_demo_cfg_t *cfg)
Read a sealed blob back and verify it byte-identical (no put).
demo_key_t
Content keys for the demo's cached blobs (source CRC-32 stand-ins).
@ k_demo_key_tile_c
Render tile C (survivor).
@ k_demo_key_tile_a
Render tile A (survivor).
@ k_demo_key_cover
Open-book cover glyph atlas (pinned).
@ k_demo_key_tile_d
Render tile D (re-put after eviction).
@ k_demo_key_tile_b
Render tile B (forcibly evicted).
demo_count_t
Expected live-entry count after the remount.
@ k_demo_survivors
Entries expected live after the remount.
static ra8_err_t internal_demo_phase_evict(ra8_cache_store_t *st)
Pin one entry, force an eviction, and prove the invariants hold.
static ra8_err_t internal_demo_build_cfg(const cache_store_demo_cfg_t *in, bool format, ra8_cache_store_cfg_t *out)
Build an ra8_cache_store init config from the demo config.
static ra8_err_t internal_demo_verify_survivors(const ra8_cache_store_t *st, const cache_store_demo_cfg_t *cfg)
Read-verify the four entries expected to survive the remount.
ra8_err_t cache_store_demo_run(const cache_store_demo_cfg_t *cfg, cache_store_demo_result_t *out)
Run the full ra8_cache_store on-media cache demonstration, end to end.
static void internal_demo_fill(uint8_t *buf, uint32_t len, uint8_t seed)
Fill buf with the demo's deterministic byte ramp.
static bool internal_demo_matches(const uint8_t *buf, uint32_t len, uint8_t seed)
Test whether buf holds the demo's deterministic byte ramp.
static ra8_err_t internal_demo_phase_seed(ra8_cache_store_t *st, const cache_store_demo_cfg_t *cfg)
Seed the store: put + read-verify the four demo blobs.
demo_len_t
Payload lengths (bytes) for the demo blobs; several cross a sector.
@ k_demo_len_tile_a
Tile A length.
@ k_demo_len_tile_d
Tile D length.
@ k_demo_len_tile_c
Tile C length.
@ k_demo_len_tile_b
Tile B length.
@ k_demo_len_cover
Cover atlas length.
demo_seed_t
Per-blob deterministic fill seeds (a distinct byte ramp per key).
@ k_demo_seed_tile_b
Tile B seed.
@ k_demo_seed_tile_c
Tile C seed.
@ k_demo_seed_cover
Cover atlas seed.
@ k_demo_seed_tile_d
Tile D seed.
@ k_demo_seed_tile_a
Tile A seed.
Hardware-free driver for the ra8_cache_store on-media cache demo (#257).
@ k_cache_store_demo_stage_closed
Checkpointed + clean-closed.
@ k_cache_store_demo_stage_none
Nothing ran yet.
@ k_cache_store_demo_stage_persisted
Survivors + pin confirmed on media.
@ k_cache_store_demo_stage_reused
Re-put into the reclaimed sectors.
@ k_cache_store_demo_stage_mounted
Store formatted + mounted.
@ k_cache_store_demo_stage_seeded
Entries put + read-verified.
@ k_cache_store_demo_stage_remounted
Re-opened over the same media.
@ k_cache_store_demo_stage_evicted
Pinned one, forced an eviction.
@ k_cache_store_demo_err_remount
Re-mount over the same media failed.
@ k_cache_store_demo_err_evict
Eviction / survivor / pin-guard failed.
@ k_cache_store_demo_err_seed
A put or its read-back verify failed.
@ k_cache_store_demo_err_persist
A survivor / eviction / pin did not.
@ k_cache_store_demo_err_arg
NULL config / result / buffer.
@ k_cache_store_demo_ok
All stages passed.
@ k_cache_store_demo_err_mount
Format + mount failed.
@ k_cache_store_demo_err_reuse
Re-put into reclaimed sectors failed.
@ k_cache_store_demo_err_close
Checkpoint + close failed.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Persistent key(CRC32)->blob cache for compiled .rabook containers,built on the vendored,...
ra8_err_t ra8_cache_store_close(ra8_cache_store_t *store)
Checkpoint, set the clean-shutdown marker, and close the store.
ra8_err_t ra8_cache_store_init(ra8_cache_store_t *store, const ra8_cache_store_cfg_t *cfg)
Bind a LevelX NOR flash instance and mount (or format) the store.
ra8_err_t ra8_cache_store_pin(ra8_cache_store_t *store, uint32_t key, bool pin)
Pin or unpin an entry (pinned entries are never evicted).
ra8_err_t ra8_cache_store_read(void *ctx, uint64_t offset, uint8_t *buf, uint32_t len)
ra8_vsource_read_fn-shaped random read over an open entry.
ra8_err_t ra8_cache_store_get(const ra8_cache_store_t *store, uint32_t key, ra8_cache_store_reader_t *out_reader)
Open a sealed entry for random reads through ra8_cache_store_read.
ra8_err_t ra8_cache_store_put(ra8_cache_store_t *store, uint32_t key, const uint8_t *data, uint32_t len)
Seal a new entry once: append data under key, atomically.
ra8_err_t ra8_cache_store_evict(ra8_cache_store_t *store, uint32_t key)
Drop an entry and reclaim its sectors (no write-back).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_busy
Resource busy – blocking operation cannot proceed.
Definition ra8_err.h:195
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_not_found
Requested item not found (lookup / search missed).
Definition ra8_err.h:173
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
Injected dependencies + caller-owned buffers for cache_store_demo_run.
ra8_cache_store_entry_t * index
Caller-owned index array.
uint32_t scratch_bytes
Bytes at scratch.
struct LX_NOR_FLASH_STRUCT * nor_flash
Caller-owned LevelX control block.
uint16_t index_cap
Index slots (max live entries).
uint8_t * scratch
Read-back scratch (>= largest blob).
ra8_cache_store_nor_init_fn nor_driver_init
Injected physical-flash bind.
uint32_t staging_bytes
Bytes at staging (>= sector).
uint32_t logical_sectors
Usable LevelX logical-sector span.
uint8_t * staging
Caller-owned >=1-sector scratch.
Outcome + observable facts of one demo run (for the banner / asserts).
bool pin_survived
Pinned entry still refuses eviction.
cache_store_demo_status_t status
Overall verdict.
bool evicted_stayed_gone
Evicted key still absent post-remount.
uint32_t survivors
Live entries after the remount.
cache_store_demo_stage_t last_stage
Furthest stage reached.
Injected dependencies + geometry for ra8_cache_store_init.
Handle to an open entry that streams through ra8_cache_store_read.
uint32_t byte_len
Payload length (== vsource size).
Caller-owned store handle.