From af92a4303b5475407cbacc9e040e8446c8a7f709 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 20 Mar 2026 16:22:34 +0100 Subject: [PATCH] memory compile flags for runner for bare metal updated --- src/coparun/mem_man.c | 34 ++++++++++++++++++++++++---------- src/coparun/mem_man.h | 7 +++++-- src/coparun/runmem.c | 22 ++++++++++++++++++---- src/coparun/runmem.h | 6 +++++- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/coparun/mem_man.c b/src/coparun/mem_man.c index 5fab4b3..51e55ed 100644 --- a/src/coparun/mem_man.c +++ b/src/coparun/mem_man.c @@ -9,37 +9,51 @@ #include -#if defined DATA_MEMORY_ADDR || defined EXECUTABLE_MEMORY_ADDR +#if defined DATA_MEMORY_LEN || defined EXECUTABLE_MEMORY_LEN /* Bare metal implementations */ -#if not defined(EXECUTABLE_MEMORY_ADDR) || not defined(DATA_MEMORY_ADDR) - #error "For bare metal, you must define DATA_MEMORY_ADDR and DATA_EXECUTABLE_MEMORY_ADDR." +#if !defined(EXECUTABLE_MEMORY_LEN) || !defined(DATA_MEMORY_LEN) + #error "For bare metal, you must define DATA_MEMORY_LEN and DATA_EXECUTABLE_MEMORY_LEN." #endif -uint8_t *allocate_executable_memory(uint32_t num_bytes) { - return (uint8_t*)EXECUTABLE_MEMORY_ADDR; +__attribute__((section(".mem_exec_itcm"), used)) +volatile uint8_t executable_memory_pool[EXECUTABLE_MEMORY_LEN]; + +__attribute__((section(".mem_data_itcm"), used)) +volatile uint8_t data_memory_pool[DATA_MEMORY_LEN]; + +volatile uint8_t *allocate_executable_memory(uint32_t num_bytes) { + if (num_bytes > EXECUTABLE_MEMORY_LEN) return 0; + return executable_memory_pool; } -uint8_t *allocate_data_memory(uint32_t num_bytes) { - return (uint8_t*)DATA_MEMORY_ADDR; +volatile uint8_t *allocate_data_memory(uint32_t num_bytes) { + if (num_bytes > DATA_MEMORY_LEN) return 0; + return data_memory_pool; } int mark_mem_executable(uint8_t *memory, uint32_t memory_len) { /* No-op for bare metal */ + (void)memory; // Mark as used + (void)memory_len; // Mark as used return 1; } void deallocate_memory(uint8_t *memory, uint32_t memory_len) { + (void)memory; // Mark as used + (void)memory_len; // Mark as used /* No-op for bare metal */ } -void memcpy(void *dest, const void *src, size_t n) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" +void memcpy(void *dest, const void *src, unsigned int n) { uint8_t *d = (uint8_t*)dest; const uint8_t *s = (const uint8_t*)src; - for (size_t i = 0; i < n; i++) { + for (unsigned int i = 0; i < n; i++) { d[i] = s[i]; } - return 0; } +#pragma GCC diagnostic pop #elif defined _WIN32 diff --git a/src/coparun/mem_man.h b/src/coparun/mem_man.h index 4767f33..6cc23d3 100644 --- a/src/coparun/mem_man.h +++ b/src/coparun/mem_man.h @@ -7,8 +7,11 @@ uint8_t *allocate_buffer_memory(uint32_t num_bytes); int mark_mem_executable(uint8_t *memory, uint32_t memory_len); void deallocate_memory(uint8_t *memory, uint32_t memory_len); -#ifdef DATA_MEMORY_ADDR -void memcpy(void *dest, const void *src, size_t n); +#ifdef DATA_MEMORY_LEN +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" +void memcpy(void *dest, const void *src, unsigned int n); +#pragma GCC diagnostic pop #else #include #endif diff --git a/src/coparun/runmem.c b/src/coparun/runmem.c index 5028733..75d0c97 100644 --- a/src/coparun/runmem.c +++ b/src/coparun/runmem.c @@ -8,6 +8,21 @@ #include "runmem.h" #include "mem_man.h" +void runmem_init(runmem_t *context) +{ + context->data_memory = NULL; + context->data_memory_len = 0; + context->executable_memory = NULL; + context->executable_memory_len = 0; + context->data_offs = 0; + context->entr_point = NULL; + context->rx_state = RX_STATE_IDLE; + context->state_flag = STATE_FLAG_NONE; + context->data_src = NULL; + context->data_dest = NULL; + context->data_size = 0; +} + void patch(uint8_t *patch_addr, uint32_t patch_mask, int32_t value) { uint32_t *val_ptr = (uint32_t*)patch_addr; uint32_t original = *val_ptr; @@ -119,9 +134,9 @@ void free_memory(runmem_t *context) { deallocate_memory(context->data_memory, context->data_memory_len); context->executable_memory_len = 0; context->data_memory_len = 0; - context->executable_memory = NULL; - context->data_memory = NULL; - context->entr_point = NULL; + context->executable_memory = 0; + context->data_memory = 0; + context->entr_point = 0; context->data_offs = 0; } @@ -129,7 +144,6 @@ int update_data_offs(runmem_t *context) { if (context->data_memory && context->executable_memory && (context->data_memory - context->executable_memory > 0x7FFFFFFF || context->executable_memory - context->data_memory > 0x7FFFFFFF)) { - perror("Error: code and data memory to far apart"); return 0; } context->data_offs = (int)(context->data_memory - context->executable_memory); diff --git a/src/coparun/runmem.h b/src/coparun/runmem.h index cd74053..2497581 100644 --- a/src/coparun/runmem.h +++ b/src/coparun/runmem.h @@ -9,8 +9,9 @@ #define RUNMEM_H #include +#include -#ifdef DATA_MEMORY_ADDR +#ifdef DATA_MEMORY_LEN #define PRINTF(...) #else #include @@ -82,6 +83,9 @@ typedef struct runmem_s { an error flag (0 on success according to current code) */ int parse_commands(runmem_t *context, uint8_t *bytes, uint32_t lengths); +/* Initialize runmem context to default/empty state */ +void runmem_init(runmem_t *context); + /* Free program and data memory */ void free_memory(runmem_t *context);