diff --git a/src/fileio.cpp b/src/fileio.cpp index 8c0088de..f2e1d765 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -4,6 +4,7 @@ #include "usb64_conf.h" #include "printf.h" #include "fileio.h" +#include "memory.h" void fileio_init() { @@ -50,7 +51,7 @@ uint32_t fileio_list_directory(char **list, uint32_t max) } debug_print_fatfs("Found file: %s\n", filename); - list[file_count] = (char *)malloc(strlen(filename) + 1); + list[file_count] = (char *)memory_dev_malloc(strlen(filename) + 1); strcpy(list[file_count], filename); file_count++; } diff --git a/src/memory.cpp b/src/memory.cpp index 29eb43dd..701121fd 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -17,20 +17,18 @@ #include "fileio.h" #include "printf.h" -extern uint8_t external_psram_size; //in MB. Set in startup.c -EXTMEM uint8_t ext_ram[1]; //Just to get the start of EXTMEM -static uint32_t internal_size = 32768; //Smaller than this will malloc to internal RAM instead static sram_storage sram[32] = {0}; void memory_init() { - if (external_psram_size == 0) - return; - - debug_print_memory("[MEMORY] External memory initialised\n"); - debug_print_memory("[MEMORY] Detected %uMB\n", external_psram_size); - debug_print_memory("[MEMORY] Heap start: %08x\n", (uint32_t)ext_ram); - debug_print_memory("[MEMORY] Heap end: %08x\n", (uint32_t)ext_ram + external_psram_size * 1024 * 1024); + if (!memory_dev_init()) + { + debug_print_memory("[MEMORY] Warning: External memory not initialised\n"); + } + else + { + debug_print_memory("[MEMORY] External memory initialised\n"); + } } //This function allocates and manages SRAM for mempak and gameboy roms (tpak) for the system. @@ -58,13 +56,7 @@ uint8_t *memory_alloc_ram(const char *name, uint32_t alloc_len, uint32_t read_on debug_print_error("[MEMORY] ERROR: SRAM malloced memory isnt right, resetting memory\n"); //Allocated length isnt long enough. Reset it to be memory safe - if(sram[i].data != NULL) - { - if (sram[i].data >= ext_ram) - extmem_free(sram[i].data); - else - free(sram[i].data); - } + memory_dev_free(sram[i].data); sram[i].data = NULL; sram[i].len = 0; } @@ -74,16 +66,9 @@ uint8_t *memory_alloc_ram(const char *name, uint32_t alloc_len, uint32_t read_on { if (sram[i].len == 0) { - //Smaller blocks are RAM are mallocs internally for better performance. Teensy has a reasonable - //amount of internal RAM :) - (alloc_len <= internal_size || external_psram_size == 0) ? (sram[i].data = (uint8_t *)malloc(alloc_len)) : - (sram[i].data = (uint8_t *)extmem_malloc(alloc_len)); - - //If failed to malloc to internal RAM, try external RAM - if (sram[i].data == NULL && alloc_len <= internal_size && external_psram_size > 0) - sram[i].data = (uint8_t *)extmem_malloc(alloc_len); + sram[i].data = (uint8_t *)memory_dev_malloc(alloc_len); - //If it still failed, no RAM left? + //If failed, no RAM left? if (sram[i].data == NULL) break; @@ -116,10 +101,7 @@ void memory_free_item(void *ptr) if (sram[i].data == ptr) { debug_print_memory("[MEMORY] Freeing %s at 0x%08x\n", sram[i].name, sram[i].data); - if (sram[i].data >= ext_ram) - extmem_free(sram[i].data); - else - free(sram[i].data); + memory_dev_free(sram[i].data); sram[i].name[0] = '\0'; sram[i].data = NULL; sram[i].len = 0; @@ -162,8 +144,3 @@ void memory_mark_dirty(void *ptr) } debug_print_error("[MEMORY] ERROR: Could not find 0x%08x\n", ptr); } - -uint8_t memory_get_ext_ram_size() -{ - return external_psram_size; -} \ No newline at end of file diff --git a/src/memory.h b/src/memory.h index 55efb9a5..63071d8f 100644 --- a/src/memory.h +++ b/src/memory.h @@ -4,7 +4,6 @@ #ifndef _MEMORY_H #define _MEMORY_H -#include #include "usb64_conf.h" #define MEMORY_READ_WRITE 0 @@ -26,4 +25,8 @@ void memory_free_item(void *ptr); void memory_mark_dirty(void *ptr); uint8_t memory_get_ext_ram_size(); +bool memory_dev_init(); +void *memory_dev_malloc(uint32_t len); +void memory_dev_free(void *add); + #endif diff --git a/src/n64_wrapper.cpp b/src/n64_wrapper.cpp index ae901b7d..c6d48f43 100644 --- a/src/n64_wrapper.cpp +++ b/src/n64_wrapper.cpp @@ -7,6 +7,7 @@ #include "memory.h" #include "usb64_conf.h" #include "fileio.h" +#include "memory.h" /* * Function: Reads a hardware realtime clock and populates day,h,m,s. @@ -189,13 +190,13 @@ uint32_t n64hal_list_gb_roms(char **gb_list, uint32_t max) { if (rom_count < max) { - gb_list[rom_count] = (char *)malloc(strlen(file_list[i]) + 1); + gb_list[rom_count] = (char *)memory_dev_malloc(strlen(file_list[i]) + 1); strcpy(gb_list[rom_count], file_list[i]); rom_count++; } } //Free file list as we go - free(file_list[i]); + memory_dev_free(file_list[i]); } return rom_count; } diff --git a/src/teensy41/memory_t4.cpp b/src/teensy41/memory_t4.cpp new file mode 100644 index 00000000..9e25e3d1 --- /dev/null +++ b/src/teensy41/memory_t4.cpp @@ -0,0 +1,62 @@ +// Copyright 2020, Ryan Wendland, usb64 +// SPDX-License-Identifier: MIT + +#include +#include "memory.h" +#include "printf.h" + +static uint32_t internal_size = 32768; //Smaller than this will malloc to internal RAM instead +extern uint8_t external_psram_size; //in MB. Set in startup.c +EXTMEM uint8_t ext_ram[1]; //Just to get the start of EXTMEM + +bool memory_dev_init() +{ + if (external_psram_size == 0) + return false; + + debug_print_memory("[MEMORY] Detected %uMB\n", external_psram_size); + debug_print_memory("[MEMORY] Heap start: %08x\n", (uint32_t)ext_ram); + debug_print_memory("[MEMORY] Heap end: %08x\n", (uint32_t)ext_ram + external_psram_size * 1024 * 1024); + return true; +} + +void *memory_dev_malloc(uint32_t len) +{ + void *add = NULL; + + //Try internal RAM for smaller files for best performance + if (len <= internal_size || external_psram_size == 0) + { + add = malloc(len); + } + + //If failed to malloc to internal RAM, try external RAM + if (add == NULL) + { + add = (uint8_t *)extmem_malloc(len); + } + + return add; +} + +void memory_dev_free(void *add) +{ + if (add == NULL) + { + return; + } + //If the address range is in the external RAM memory map, call the correct free function. + if (add >= ext_ram) + { + extmem_free(add); + } + else + { + free(add); + } +} + +uint8_t memory_get_ext_ram_size() +{ + return external_psram_size; +} diff --git a/src/tft/tft.cpp b/src/tft/tft.cpp index 97d3227c..adab410e 100644 --- a/src/tft/tft.cpp +++ b/src/tft/tft.cpp @@ -300,7 +300,7 @@ void tft_add_log(char c) if (c == '\n') { tft_log[tft_log_pos] = '\0'; - _tft_log_text_lines[tft_log_line_num] = (char *)malloc(strlen(tft_log) + 1); + _tft_log_text_lines[tft_log_line_num] = (char *)memory_dev_malloc(strlen(tft_log) + 1); strcpy(_tft_log_text_lines[tft_log_line_num], tft_log); tft_log_line_num++; tft_log_pos = 0; @@ -310,7 +310,7 @@ void tft_add_log(char c) //Exceeded max lines, remove oldest line and shift lines up by one if (tft_log_line_num >= _tft_log_max_lines) { - free(_tft_log_text_lines[0]); + memory_dev_free(_tft_log_text_lines[0]); for (uint32_t i = 0; i < _tft_log_max_lines - 1; i++) { _tft_log_text_lines[i] = _tft_log_text_lines[i + 1];