Skip to content

Commit

Permalink
Memory allocation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoparrilla authored Oct 29, 2023
2 parents a80362b + ee15d9f commit 8c38b2b
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 210 deletions.
91 changes: 57 additions & 34 deletions configurator/src/config.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#include "include/config.h"

static bool is_delay_option = false;
static __uint16_t is_delay_option = FALSE;

static ConfigData *configData = NULL;

ConfigEntry *get_config_entry(char *key)
{
for (size_t i = 0; i < configData->count; i++)
{
if (strcmp(configData->entries[i].key, key) == 0)
{
return &configData->entries[i];
}
}
return NULL;
}

static void print_table(ConfigData *configData)
{
Expand Down Expand Up @@ -47,14 +61,24 @@ static void print_table(ConfigData *configData)
printf("+----+----------------------+------------------------------------------+----------+\r\n");
}

ConfigData load_all_entries()
void init_config()
{
__uint8_t count = 0;
__uint32_t currentAddress = CONFIG_START_ADDRESS + sizeof(__uint32_t);
ConfigData configData = {
.magic = *((volatile __uint32_t *)currentAddress),
.count = 0,
};
// Dynamically allocate memory for ConfigData
configData = malloc(sizeof(ConfigData));
if (configData != NULL)
{
configData->magic = 0;
configData->count = 0;
}
}

void load_all_entries()
{
__uint16_t count = 0;
__uint32_t currentAddress = (__uint32_t)(CONFIG_START_ADDRESS + sizeof(__uint32_t));
// Dynamically allocate memory for ConfigData
configData->magic = *((volatile __uint32_t *)currentAddress);
configData->count = 0;
currentAddress += sizeof(__uint32_t);
while (1)
{
Expand All @@ -68,10 +92,9 @@ ConfigData load_all_entries()
{
break; // Exit the loop if we encounter a key length of 0 (end of entries)
}
configData.entries[configData.count] = entry;
configData.count++;
configData->entries[configData->count] = entry;
configData->count++;
}
return configData;
}

static char *read_input(__uint16_t type)
Expand Down Expand Up @@ -190,28 +213,28 @@ static char *read_input(__uint16_t type)
return result;
}

__uint8_t configuration()
__uint16_t configuration()
{
PRINT_APP_HEADER(VERSION);

printf("\r\n");

printf("Loading configuration...");

send_sync_command(GET_CONFIG, NULL, 0, 10, true);
send_sync_command(GET_CONFIG, NULL, 0, 10, TRUE);

printf("\r\n");

ConfigData configData = load_all_entries();
load_all_entries();

while (1)
{
print_table(&configData);
print_table(configData);

char *prompt;
asprintf(&prompt, "Enter the parameter to modify (1 to %d) or [C]ancel: ", configData.count);
asprintf(&prompt, "Enter the parameter to modify (1 to %d) or [C]ancel: ", configData->count);

int param_index = get_number_within_range(prompt, configData.count, 1, 'C');
int param_index = get_number_within_range(prompt, configData->count, 1, 'C');

if (param_index <= 0)
{
Expand All @@ -221,32 +244,32 @@ __uint8_t configuration()

param_index = param_index - 1;

printf("\r\n%s = %s\r\r\n", configData.entries[param_index].key, configData.entries[param_index].value);
char *input = read_input(configData.entries[param_index].dataType);
printf("\r\n%s = %s\r\r\n", configData->entries[param_index].key, configData->entries[param_index].value);
char *input = read_input(configData->entries[param_index].dataType);
printf("\r\n");

printf("The input is: %s\r\n", input);
strncpy(configData.entries[param_index].value, input, MAX_STRING_VALUE_LENGTH);
strncpy(configData->entries[param_index].value, input, MAX_STRING_VALUE_LENGTH);

printf("Saving configuration...");

switch (configData.entries[param_index].dataType)
switch (configData->entries[param_index].dataType)
{
case TYPE_INT:
send_sync_command(PUT_CONFIG_INTEGER, &configData.entries[param_index], sizeof(ConfigEntry), 10, false);
send_sync_command(PUT_CONFIG_INTEGER, &configData->entries[param_index], sizeof(ConfigEntry), 10, FALSE);
break;
case TYPE_BOOL:
send_sync_command(PUT_CONFIG_BOOL, &configData.entries[param_index], sizeof(ConfigEntry), 10, false);
send_sync_command(PUT_CONFIG_BOOL, &configData->entries[param_index], sizeof(ConfigEntry), 10, FALSE);
break;
case TYPE_STRING:
send_sync_command(PUT_CONFIG_STRING, &configData.entries[param_index], sizeof(ConfigEntry), 10, false);
send_sync_command(PUT_CONFIG_STRING, &configData->entries[param_index], sizeof(ConfigEntry), 10, FALSE);
break;
default:
printf("Unknown data type.\r\n");
break;
}

send_sync_command(SAVE_CONFIG, NULL, 0, 10, true);
send_sync_command(SAVE_CONFIG, NULL, 0, 10, TRUE);
printf("\r\n");

free(input);
Expand All @@ -255,37 +278,37 @@ __uint8_t configuration()
}
}

__uint8_t read_config()
__uint16_t read_config()
{

#ifndef _DEBUG
int err = send_sync_command(GET_CONFIG, NULL, 0, 10, false);
int err = send_sync_command(GET_CONFIG, NULL, 0, 10, FALSE);

if (err != 0)
{
printf("Cannot read configuration. Is the SidecarT connected?\r\nTry to reset the SidecarT and try again.\r\n");
return 1;
}

ConfigData configData = load_all_entries();
load_all_entries();

for (size_t i = 0; i < configData.count; i++)
for (size_t i = 0; i < configData->count; i++)
{
if (strcmp(configData.entries[i].key, "DELAY_ROM_EMULATION") == 0)
if (strcmp(configData->entries[i].key, "DELAY_ROM_EMULATION") == 0)
{
is_delay_option = strcmp(configData.entries[i].value, "true") == 0;
is_delay_option = strcmp(configData->entries[i].value, "true") == 0;
}
}
#endif
return 0;
}

bool is_delay_option_enabled(void)
__uint16_t is_delay_option_enabled(void)
{
return is_delay_option;
}

__uint8_t toggle_delay_option(void)
__uint16_t toggle_delay_option(void)
{
PRINT_APP_HEADER(VERSION);

Expand All @@ -297,7 +320,7 @@ __uint8_t toggle_delay_option(void)
strncpy(entry->value, is_delay_option ? "true" : "false", MAX_STRING_VALUE_LENGTH);
entry->dataType = TYPE_BOOL;

send_sync_command(PUT_CONFIG_BOOL, entry, sizeof(ConfigEntry), 10, false);
send_sync_command(PUT_CONFIG_BOOL, entry, sizeof(ConfigEntry), 10, FALSE);

free(entry);

Expand Down
24 changes: 12 additions & 12 deletions configurator/src/floppydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//================================================================
// Floppy selector

__uint8_t floppy_db()
__uint16_t floppy_db()
{
__uint8_t alphanum_bar_y_pos = 4;
__uint16_t alphanum_bar_y_pos = 4;

void erase_table(int y_pos, int num_lines)
{
Expand Down Expand Up @@ -45,8 +45,8 @@ __uint8_t floppy_db()
print_alphanum_bar();
int padding = 2;
char nav_arrow_keys[36] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
__uint8_t nav_arrow_keys_index = 0;
bool first_time = true;
__uint16_t nav_arrow_keys_index = 0;
__uint16_t first_time = TRUE;
char key = 'A';
long fullkey = 0;
while (1)
Expand All @@ -56,7 +56,7 @@ __uint8_t floppy_db()
print_alphanum_bar();
if (first_time)
{
first_time = false;
first_time = FALSE;
}
else
{
Expand All @@ -78,30 +78,30 @@ __uint8_t floppy_db()
locate(0, alphanum_bar_y_pos);
if ((key >= 65) && (key <= 90))
{
for (__uint8_t i = 0; i < padding + (key - 65); i++)
for (__uint16_t i = 0; i < padding + (key - 65); i++)
{
printf("\033C\033C");
}
nav_arrow_keys_index = key - 65;
}
else
{
for (__uint8_t i = 0; i < padding + 26 + (key - 48); i++)
for (__uint16_t i = 0; i < padding + 26 + (key - 48); i++)
{
printf("\033C\033C");
}
nav_arrow_keys_index = 26 + key - 48;
}
printf("\033p%c\033q\r", key);
__uint16_t key16 = ((__uint16_t)key) & 0xFF;
send_sync_command(QUERY_FLOPPY_DB, &key16, 2, 1, false);
send_sync_command(QUERY_FLOPPY_DB, &key16, 2, 1, FALSE);

int num_files = -1;
__uint32_t db_file_list_mem = DB_FILES_LIST_START_ADDRESS;
__uint32_t db_file_list_mem = (__uint32_t)DB_FILES_LIST_START_ADDRESS;
#ifdef _DEBUG
printf("Reading db file list from memory address: 0x%08X\r\n", db_file_list_mem);
#endif
char *file_array = read_files_from_memory((__uint8_t *)db_file_list_mem);
char *file_array = read_files_from_memory((char *)db_file_list_mem);

if (!file_array)
{
Expand All @@ -123,7 +123,7 @@ __uint8_t floppy_db()

printf("Loading floppy. Wait until the led in the board blinks a 'F' in morse...");

send_sync_command(DOWNLOAD_FLOPPY, &app_number, 2, 30, true);
send_sync_command(DOWNLOAD_FLOPPY, &app_number, 2, 30, TRUE);

__uint16_t download_status = *((__uint16_t *)(DB_FILES_LIST_START_ADDRESS));

Expand All @@ -147,7 +147,7 @@ __uint8_t floppy_db()
erase_table(alphanum_bar_y_pos + 1, 18);
key = last_key_pressed & 0xFF;
fullkey = last_key_pressed;
first_time = true;
first_time = TRUE;
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions configurator/src/floppyselector.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//================================================================
// Floppy selector

static __uint8_t floppy_selector(mode_t floppy_command)
static __uint16_t floppy_selector(mode_t floppy_command)
{
PRINT_APP_HEADER(VERSION);

Expand All @@ -17,17 +17,17 @@ static __uint8_t floppy_selector(mode_t floppy_command)

printf("Loading available Floppy images...");

send_sync_command(LIST_FLOPPIES, NULL, 0, 10, true);
send_sync_command(LIST_FLOPPIES, NULL, 0, 10, TRUE);

printf("\r\n");

int num_files = -1;
__uint32_t file_list_mem = FILE_LIST_START_ADDRESS + sizeof(__uint32_t);
__uint32_t file_list_mem = (__uint32_t)(FILE_LIST_START_ADDRESS + sizeof(__uint32_t));

#ifdef _DEBUG
printf("Reading file list from memory address: 0x%08X\r\n", file_list_mem);
#endif
char *file_array = read_files_from_memory((__uint8_t *)file_list_mem);
char *file_array = read_files_from_memory((char *)file_list_mem);

if (!file_array)
{
Expand All @@ -52,19 +52,19 @@ static __uint8_t floppy_selector(mode_t floppy_command)

printf("\r\033KLoading floppy. Wait until the led in the board blinks a 'F' in morse...");

send_sync_command(floppy_command, &floppy_number, 2, 30, true);
send_sync_command(floppy_command, &floppy_number, 2, 30, TRUE);

printf("\r\033KFloppy image file loaded. ");

return 1; // Positive is OK
}

__uint8_t floppy_selector_ro(void)
__uint16_t floppy_selector_ro(void)
{
return floppy_selector(LOAD_FLOPPY_RO);
}

__uint8_t floppy_selector_rw(void)
__uint16_t floppy_selector_rw(void)
{
return floppy_selector(LOAD_FLOPPY_RW);
}
Loading

0 comments on commit 8c38b2b

Please sign in to comment.