ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
sweep_block_payload.c
Go to the documentation of this file.
1
13#include <string.h>
14
16
17typedef enum : uint32_t {
19 k_cbs_no_word = UINT32_MAX,
21
28
29typedef enum : uint64_t {
30 k_cbs_payload_seed = 0x9E3779B97F4A7C15ULL,
32
38
39static const char* const s_payload_words[] = {
40 "the", "quick", "reader", "turns", "another", "page", "while", "morning",
41 "light", "settles", "across", "quiet", "margins", "and", "chapter", "headings",
42 "gather", "small", "notes", "between", "lines", "of", "steady", "prose",
43 "carried", "through", "paper", "towns", "by", "patient", "hands", "again",
44};
45
61static uint64_t internal_payload_rng(uint64_t* state)
62{
63 uint64_t value = *state;
64 value ^= value << (uint8_t)k_cbs_payload_shift_a;
65 value ^= value >> (uint8_t)k_cbs_payload_shift_b;
66 value ^= value << (uint8_t)k_cbs_payload_shift_c;
67 *state = value;
68 return value;
69}
70
87{
88 if (state->phase == (uint8_t)k_cbs_phase_dot) {
89 state->phase = (uint8_t)k_cbs_phase_newline;
90 return (uint8_t)'.';
91 }
92 if (state->phase == (uint8_t)k_cbs_phase_newline) {
93 state->phase = (uint8_t)k_cbs_phase_word;
94 state->word_index = (uint32_t)k_cbs_no_word;
95 return (uint8_t)'\n';
96 }
97 if (state->phase == (uint8_t)k_cbs_phase_space) {
98 state->phase = (uint8_t)k_cbs_phase_word;
99 state->word_index = (uint32_t)k_cbs_no_word;
100 return (uint8_t)' ';
101 }
102 if (state->word_index == (uint32_t)k_cbs_no_word) {
103 const uint32_t count = (uint32_t)(sizeof(s_payload_words) / sizeof(s_payload_words[0]));
104 state->word_index = (uint32_t)(internal_payload_rng(&state->rng) % count);
105 state->word_offset = 0U;
106 }
107 const char* word = s_payload_words[state->word_index];
108 if (word[state->word_offset] != '\0') {
109 const uint8_t result = (uint8_t)word[state->word_offset];
110 state->word_offset++;
111 return result;
112 }
113 state->sentence_words++;
114 if (state->sentence_words == (uint32_t)k_cbs_words_per_dot) {
115 state->sentence_words = 0U;
116 state->phase = (uint8_t)k_cbs_phase_newline;
117 return (uint8_t)'.';
118 }
119 state->phase = (uint8_t)k_cbs_phase_word;
120 state->word_index = (uint32_t)k_cbs_no_word;
121 return (uint8_t)' ';
122}
123
126{
127 const size_t count = ((size_t)k_cbs_blob_bytes / k_cbs_payload_checkpoint_bytes) + 1U;
128 return count * sizeof(cbs_payload_state_t);
129}
130
132int priv_payload_init(cbs_payload_t* payload, void* workspace, size_t capacity)
133{
134 const size_t required = priv_payload_workspace_required();
135 if ((payload == nullptr) || (workspace == nullptr) ||
136 (((uintptr_t)workspace % alignof(cbs_payload_state_t)) != 0U) || (capacity < required)) {
137 return 1;
138 }
139 cbs_payload_state_t* checkpoints = (cbs_payload_state_t*)workspace;
140 cbs_payload_state_t state = {.rng = (uint64_t)k_cbs_payload_seed,
141 .word_index = (uint32_t)k_cbs_no_word};
142 const uint32_t count = (uint32_t)(required / sizeof(cbs_payload_state_t));
143 for (uint32_t index = 0U; index < count; ++index) {
144 checkpoints[index] = state;
145 if ((index + 1U) < count) {
146 for (uint32_t i = 0U; i < (uint32_t)k_cbs_payload_checkpoint_bytes; ++i) {
147 (void)internal_payload_next(&state);
148 }
149 }
150 }
151 *payload = (cbs_payload_t){.checkpoints = checkpoints,
152 .checkpoint_count = count,
153 .cursor_state = checkpoints[0]};
154 return 0;
155}
156
158ra8_err_t priv_payload_read(void* ctx, uint64_t offset, uint8_t* buffer, uint32_t length)
159{
160 cbs_payload_t* payload = (cbs_payload_t*)ctx;
161 if ((payload == nullptr) || (buffer == nullptr)) {
162 return k_ra8_err_null_ptr;
163 }
164 if ((offset + length) > (uint64_t)k_cbs_blob_bytes) {
166 }
167 cbs_payload_state_t state = payload->cursor_state;
168 if (offset != payload->cursor_offset) {
169 const uint32_t checkpoint = (uint32_t)(offset / k_cbs_payload_checkpoint_bytes);
170 if (checkpoint >= payload->checkpoint_count) {
172 }
173 state = ((const cbs_payload_state_t*)payload->checkpoints)[checkpoint];
174 const uint32_t skip = (uint32_t)(offset % k_cbs_payload_checkpoint_bytes);
175 for (uint32_t i = 0U; i < skip; ++i) {
176 (void)internal_payload_next(&state);
177 }
178 }
179 for (uint32_t i = 0U; i < length; ++i) {
180 buffer[i] = internal_payload_next(&state);
181 }
182 payload->cursor_offset = offset + length;
183 payload->cursor_state = state;
184 return k_ra8_ok;
185}
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
Definition ra8_err.h:337
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Serializable pseudo-text generator cursor.
uint64_t rng
PRNG state.
uint32_t word_index
Selected word or sentinel.
uint32_t word_offset
Byte position in the selected word.
uint8_t phase
Word/separator phase.
uint32_t sentence_words
Words emitted in the current sentence.
Resettable exact pseudo-text byte source with bounded checkpoints.
uint64_t cursor_offset
End offset cached for sequential reads.
const void * checkpoints
Caller-owned generator-state index.
cbs_payload_state_t cursor_state
State at cursor_offset.
uint32_t checkpoint_count
Entries in the index.
Module-private seams shared by the #208 sweep translation units.
@ k_cbs_blob_bytes
8 MiB payload (2^23; all sizes divide it).
@ k_cbs_words_per_dot
Words per sentence in the text filler.
RA8_PRIV int priv_payload_init(cbs_payload_t *payload, void *workspace, size_t capacity)
Build the payload source index into caller-owned storage.
cbs_payload_limit_t
@ k_cbs_no_word
Generator needs a new word.
@ k_cbs_payload_checkpoint_bytes
Maximum seek regeneration.
static RA8_INTERNAL uint8_t internal_payload_next(cbs_payload_state_t *state)
Emit one byte from the deterministic pseudo-text state machine.
RA8_PRIV ra8_err_t priv_payload_read(void *ctx, uint64_t offset, uint8_t *buffer, uint32_t length)
Read exact historical pseudo-text bytes at any bounded offset.
cbs_payload_seed_t
@ k_cbs_payload_seed
Initial generator state.
static RA8_INTERNAL uint64_t internal_payload_rng(uint64_t *state)
Advance the deterministic payload pseudo-random generator.
static const char *const s_payload_words[]
RA8_PRIV size_t priv_payload_workspace_required(void)
Return exact workspace bytes required by the pseudo-text source index.
cbs_payload_shift_t
@ k_cbs_payload_shift_a
First xorshift distance.
@ k_cbs_payload_shift_c
Final xorshift distance.
@ k_cbs_payload_shift_b
Middle xorshift distance.
cbs_payload_phase_t
@ k_cbs_phase_dot
Emit a sentence period.
@ k_cbs_phase_word
Emit a word byte.
@ k_cbs_phase_newline
Emit a line break.
@ k_cbs_phase_space
Emit a word separator.