Skip to content

Commit

Permalink
Memory: Rework for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 committed Nov 23, 2021
1 parent 11d3067 commit 057c5a3
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "usb64_conf.h"
#include "printf.h"
#include "fileio.h"
#include "memory.h"

void fileio_init()
{
Expand Down Expand Up @@ -52,7 +53,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++;
}
Expand Down
47 changes: 12 additions & 35 deletions src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
5 changes: 4 additions & 1 deletion src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#ifndef _MEMORY_H
#define _MEMORY_H

#include <Arduino.h>
#include "usb64_conf.h"

#define MEMORY_READ_WRITE 0
Expand All @@ -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
5 changes: 3 additions & 2 deletions src/n64_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
62 changes: 62 additions & 0 deletions src/teensy41/memory_t4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020, Ryan Wendland, usb64
// SPDX-License-Identifier: MIT

#include <Arduino.h>
#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;
}
4 changes: 2 additions & 2 deletions src/tft/tft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down

0 comments on commit 057c5a3

Please sign in to comment.