From 4f30cc686be6c5cd307d3d260899a3f88dfccf40 Mon Sep 17 00:00:00 2001 From: Diego Parrilla Date: Sun, 29 Oct 2023 19:07:42 +0100 Subject: [PATCH 1/2] removed bool and osbind libraries --- configurator/src/include/config.h | 14 +++++++++----- configurator/src/include/floppydb.h | 3 +-- configurator/src/include/floppyselector.h | 5 ++--- configurator/src/include/helper.h | 17 ++++++++--------- configurator/src/include/network.h | 19 +++++++------------ configurator/src/include/reset.h | 2 +- configurator/src/include/romselector.h | 3 +-- configurator/src/include/storage.h | 17 +++++++---------- 8 files changed, 36 insertions(+), 44 deletions(-) diff --git a/configurator/src/include/config.h b/configurator/src/include/config.h index 5dbb1d9..e094550 100644 --- a/configurator/src/include/config.h +++ b/configurator/src/include/config.h @@ -3,7 +3,6 @@ #include #include -#include #include #include "helper.h" @@ -58,11 +57,16 @@ static ConfigData config_data_example = { }; #endif +#define FALSE 0 +#define TRUE 1 + #define STATUS_STRING_BUFFER_SIZE 80 // Buffer size to display -__uint8_t read_config(); -__uint8_t configuration(); -bool is_delay_option_enabled(); -__uint8_t toggle_delay_option(void); +ConfigEntry *get_config_entry(char *key); +__uint16_t read_config(); +__uint16_t configuration(); +__uint16_t is_delay_option_enabled(); +__uint16_t toggle_delay_option(void); +void init_config(); #endif /* CONFIG_H_ */ \ No newline at end of file diff --git a/configurator/src/include/floppydb.h b/configurator/src/include/floppydb.h index d701062..138ed50 100644 --- a/configurator/src/include/floppydb.h +++ b/configurator/src/include/floppydb.h @@ -3,7 +3,6 @@ #include #include -#include #include "commands.h" #include "screen.h" @@ -11,6 +10,6 @@ #include "storage.h" #include "network.h" -__uint8_t floppy_db(); +__uint16_t floppy_db(); #endif /* FLOPPYDB_H_ */ \ No newline at end of file diff --git a/configurator/src/include/floppyselector.h b/configurator/src/include/floppyselector.h index 691d3a9..5f90169 100644 --- a/configurator/src/include/floppyselector.h +++ b/configurator/src/include/floppyselector.h @@ -3,14 +3,13 @@ #include #include -#include #include "commands.h" #include "screen.h" #include "helper.h" #include "storage.h" -__uint8_t floppy_selector_ro(); -__uint8_t floppy_selector_rw(); +__uint16_t floppy_selector_ro(); +__uint16_t floppy_selector_rw(); #endif /* FLOPPYSELECTOR_H_ */ \ No newline at end of file diff --git a/configurator/src/include/helper.h b/configurator/src/include/helper.h index 2a9b527..be22cb0 100644 --- a/configurator/src/include/helper.h +++ b/configurator/src/include/helper.h @@ -3,7 +3,6 @@ #include #include -#include #include "config.h" @@ -97,16 +96,16 @@ static __uint32_t random_number = 0x0; #define max(a, b) ((a) > (b) ? (a) : (b)) -int get_number_within_range(char *prompt, __uint8_t num_items, __uint8_t first_value, char cancel_char); +int get_number_within_range(char *prompt, __uint16_t num_items, __uint16_t first_value, char cancel_char); int send_async_command(__uint16_t command, void *payload, __uint16_t payload_size); -int send_sync_command(__uint16_t command, void *payload, __uint16_t payload_size, __uint32_t timeout, bool show_spinner); -void please_wait(char *message, __uint8_t seconds); -void please_wait_silent(__uint8_t seconds); -void sleep_seconds(__uint8_t seconds, bool silent); +int send_sync_command(__uint16_t command, void *payload, __uint16_t payload_size, __uint32_t timeout, __uint16_t show_spinner); +void please_wait(char *message, __uint16_t seconds); +void please_wait_silent(__uint16_t seconds); +void sleep_seconds(__uint16_t seconds, __uint16_t silent); void spinner(__uint16_t spinner_update_frequency); -char *read_files_from_memory(__uint8_t *memory_location); -__uint8_t get_file_count(char *file_array); -char *print_file_at_index(char *current_ptr, __uint8_t index, int num_columns); +char *read_files_from_memory(char *memory_location); +__uint16_t get_file_count(char *file_array); +char *print_file_at_index(char *current_ptr, __uint16_t index, int num_columns); int display_paginated_content(char *file_array, int num_files, int page_size, char *item_name, __uint32_t *keypress); void print_centered(const char *str, int screen_width); diff --git a/configurator/src/include/network.h b/configurator/src/include/network.h index 0bcae15..741e250 100644 --- a/configurator/src/include/network.h +++ b/configurator/src/include/network.h @@ -3,7 +3,6 @@ #include #include -#include #include #include "helper.h" @@ -64,11 +63,6 @@ typedef struct connection_data __uint16_t status; // connection status } ConnectionData; -extern ConnectionStatus connection_status; -extern WifiScanData *wifiScanData; -extern ConnectionData *connection_data; -extern __uint16_t previous_connection_status; // Keep track of the previous connection status - #ifdef _DEBUG static WifiScanData wifi_scan_data_example = { .magic = 0x12345678, @@ -112,11 +106,12 @@ static ConnectionData connection_data_example = { .status = CONNECTED_WIFI_IP}; #endif -bool check_latest_release(); -__uint8_t wifi_menu(); -__uint16_t force_connection_status(bool show_bar); -__uint16_t get_connection_status(bool show_bar); -__uint8_t roms_from_network_selector(); -__uint8_t check_network_connection(); +__uint16_t check_latest_release(); +__uint16_t wifi_menu(); +__uint16_t force_connection_status(__uint16_t show_bar); +__uint16_t get_connection_status(__uint16_t show_bar); +__uint16_t roms_from_network_selector(); +__uint16_t check_network_connection(); +void init_connection_status(); #endif // NETWORK_H diff --git a/configurator/src/include/reset.h b/configurator/src/include/reset.h index 4ceb0d0..1e7b851 100644 --- a/configurator/src/include/reset.h +++ b/configurator/src/include/reset.h @@ -8,6 +8,6 @@ #include "screen.h" #include "commands.h" -__uint8_t reset(); +__uint16_t reset(); #endif /*RESET_H_*/ \ No newline at end of file diff --git a/configurator/src/include/romselector.h b/configurator/src/include/romselector.h index 498d057..60ace0b 100644 --- a/configurator/src/include/romselector.h +++ b/configurator/src/include/romselector.h @@ -3,13 +3,12 @@ #include #include -#include #include "commands.h" #include "screen.h" #include "helper.h" #include "storage.h" -__uint8_t rom_selector(); +__uint16_t rom_selector(); #endif /* ROMSELECTOR_H_ */ \ No newline at end of file diff --git a/configurator/src/include/storage.h b/configurator/src/include/storage.h index f19cfa5..b50b2b5 100644 --- a/configurator/src/include/storage.h +++ b/configurator/src/include/storage.h @@ -3,7 +3,6 @@ #include #include -#include #include #include "helper.h" @@ -30,7 +29,7 @@ typedef enum HARDDISKS_FOLDER_NOTFOUND, // Hard disks folder error } StorageStatus; -#define MAX_FOLDER_LENGTH 128 // Max length of the folder names +#define MAX_FOLDER_LENGTH 128 // Max length of the folder names typedef struct sd_data { char roms_folder[MAX_FOLDER_LENGTH]; // ROMs folder name @@ -47,8 +46,6 @@ typedef struct sd_data __uint16_t harddisks_folder_status; // Hard disks folder status } SdCardData; -extern SdCardData *sd_card_data; - #ifdef _DEBUG static SdCardData sdCardDataExample = { .roms_folder = "/roms", @@ -65,11 +62,11 @@ static SdCardData sdCardDataExample = { .status = SD_CARD_MOUNTED}; #endif -//__uint8_t storage_menu(); -__uint16_t get_storage_status(bool show_bar); -__uint8_t check_folder_roms(); -__uint8_t check_folder_floppies(); -__uint8_t check_folder_floppies_db(); -__uint8_t check_folder_harddisks(); +__uint16_t get_storage_status(__uint16_t show_bar); +__uint16_t check_folder_roms(); +__uint16_t check_folder_floppies(); +__uint16_t check_folder_floppies_db(); +__uint16_t check_folder_harddisks(); +void init_storage(); #endif // STORAGE_H From ee15d9f48688f300808cde81b7453f618df2277e Mon Sep 17 00:00:00 2001 From: Diego Parrilla Date: Sun, 29 Oct 2023 19:08:48 +0100 Subject: [PATCH 2/2] Fence the memory access to each modules --- configurator/src/config.c | 91 +++++++++++++++++++------------ configurator/src/floppydb.c | 24 ++++---- configurator/src/floppyselector.c | 14 ++--- configurator/src/helper.c | 88 +++++++++++------------------- configurator/src/main.c | 13 +++-- configurator/src/network.c | 55 +++++++++++-------- configurator/src/reset.c | 4 +- configurator/src/romselector.c | 10 ++-- configurator/src/storage.c | 56 ++++++++++++------- 9 files changed, 189 insertions(+), 166 deletions(-) diff --git a/configurator/src/config.c b/configurator/src/config.c index 6bcc290..a8e2638 100644 --- a/configurator/src/config.c +++ b/configurator/src/config.c @@ -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) { @@ -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) { @@ -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) @@ -190,7 +213,7 @@ static char *read_input(__uint16_t type) return result; } -__uint8_t configuration() +__uint16_t configuration() { PRINT_APP_HEADER(VERSION); @@ -198,20 +221,20 @@ __uint8_t configuration() 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) { @@ -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); @@ -255,11 +278,11 @@ __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) { @@ -267,25 +290,25 @@ __uint8_t read_config() 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); @@ -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); diff --git a/configurator/src/floppydb.c b/configurator/src/floppydb.c index f30c133..8674574 100644 --- a/configurator/src/floppydb.c +++ b/configurator/src/floppydb.c @@ -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) { @@ -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) @@ -56,7 +56,7 @@ __uint8_t floppy_db() print_alphanum_bar(); if (first_time) { - first_time = false; + first_time = FALSE; } else { @@ -78,7 +78,7 @@ __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"); } @@ -86,7 +86,7 @@ __uint8_t floppy_db() } 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"); } @@ -94,14 +94,14 @@ __uint8_t floppy_db() } 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) { @@ -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)); @@ -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; } } } diff --git a/configurator/src/floppyselector.c b/configurator/src/floppyselector.c index c08f360..3759046 100644 --- a/configurator/src/floppyselector.c +++ b/configurator/src/floppyselector.c @@ -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); @@ -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) { @@ -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); } diff --git a/configurator/src/helper.c b/configurator/src/helper.c index 23d8a73..6ed6c22 100644 --- a/configurator/src/helper.c +++ b/configurator/src/helper.c @@ -3,31 +3,6 @@ static __uint16_t spinner_loop = 0; static char spinner_chars[] = {'\\', '|', '/', '-'}; -#ifdef _DEBUG -#include - -void dump_hex(const void *data, size_t size) -{ - locate(0, 24); - const unsigned char *byte_data = data; - // Print hex values - for (size_t i = 0; i < size; i++) - { - printf("%02x ", byte_data[i]); - } - - printf(" "); // Some space between hex and chars - - // Print characters - for (size_t i = 0; i < size; i++) - { - // Check if the character is printable; if not, print a dot - char ch = isprint(byte_data[i]) ? byte_data[i] : '.'; - printf("%c", ch); - } -} -#endif - // Function to convert a character to lowercase char to_lowercase(char c) { @@ -38,7 +13,7 @@ char to_lowercase(char c) return c; } -int get_number_within_range(char *prompt, __uint8_t num_items, __uint8_t first_value, char cancel_char) +int get_number_within_range(char *prompt, __uint16_t num_items, __uint16_t first_value, char cancel_char) { char input[3]; int number; @@ -101,7 +76,7 @@ int send_async_command(__uint16_t command, void *payload, __uint16_t payload_siz { payload_size++; } - __uint32_t rom3_address = ROM3_MEMORY_START; + __uint32_t rom3_address = (__uint32_t)ROM3_MEMORY_START; __uint8_t command_header = *((volatile __uint8_t *)(rom3_address + PROTOCOL_HEADER)); __uint8_t command_code = *((volatile __uint8_t *)(rom3_address + command)); __uint8_t command_payload_size = *((volatile __uint8_t *)(rom3_address + payload_size)); // Always even! @@ -128,14 +103,14 @@ int send_async_command(__uint16_t command, void *payload, __uint16_t payload_siz return 0; } -int send_sync_command(__uint16_t command, void *payload, __uint16_t payload_size, __uint32_t timeout, bool show_spinner) +int send_sync_command(__uint16_t command, void *payload, __uint16_t payload_size, __uint32_t timeout, __uint16_t show_spinner) { if (payload_size % 2 != 0) { payload_size++; } __uint32_t random_seed = *((volatile __uint32_t *)RANDOM_SEED_ADDRESS); - __uint32_t rom3_address = ROM3_MEMORY_START; + __uint32_t rom3_address = (__uint32_t)ROM3_MEMORY_START; __uint8_t command_header = *((volatile __uint8_t *)(rom3_address + PROTOCOL_HEADER)); __uint8_t command_code = *((volatile __uint8_t *)(rom3_address + command)); __uint8_t command_payload_size = *((volatile __uint8_t *)(rom3_address + payload_size + RANDOM_NUMBER_SIZE)); // Always even! @@ -190,12 +165,12 @@ int send_sync_command(__uint16_t command, void *payload, __uint16_t payload_size return (active_wait == 0); } -void sleep_seconds(__uint8_t seconds, bool silent) +void sleep_seconds(__uint16_t seconds, __uint16_t silent) { - for (__uint8_t j = 0; j < seconds; j++) + for (__uint16_t j = 0; j < seconds; j++) { // Assuming PAL system; for NTSC, use 60. - for (__uint8_t i = 0; i < 50; i++) + for (__uint16_t i = 0; i < 50; i++) { if (!silent) spinner(1); @@ -213,21 +188,21 @@ void spinner(__uint16_t spinner_update_frequency) spinner_loop++; } -void please_wait(char *message, __uint8_t seconds) +void please_wait(char *message, __uint16_t seconds) { printf(message); // Show the message printf(" "); // Leave a space for the spinner - sleep_seconds(seconds, false); + sleep_seconds(seconds, FALSE); } -void please_wait_silent(__uint8_t seconds) +void please_wait_silent(__uint16_t seconds) { - sleep_seconds(seconds, true); + sleep_seconds(seconds, TRUE); } -char *read_files_from_memory(__uint8_t *memory_location) +char *read_files_from_memory(char *memory_location) { - __uint8_t *current_ptr = memory_location; + char *current_ptr = memory_location; __uint16_t total_size = 0; // Calculate total size required @@ -261,9 +236,9 @@ char *read_files_from_memory(__uint8_t *memory_location) return output_array; } -__uint8_t get_file_count(char *file_array) +__uint16_t get_file_count(char *file_array) { - __uint8_t count = 0; + __uint16_t count = 0; char *current_ptr = file_array; while (*current_ptr) @@ -279,15 +254,14 @@ __uint8_t get_file_count(char *file_array) return count; } -char *print_file_at_index(char *current_ptr, __uint8_t index, int num_columns) +char *print_file_at_index(char *current_ptr, __uint16_t index, int num_columns) { - __uint8_t current_index = 0; + __uint16_t current_index = 0; while (*current_ptr) { // As long as we don't hit the double null terminator if (current_index == index) { - printf("\033K"); // Erase to end of line (VT52) int chars_printed = 0; // To keep track of how many characters are printed while (*current_ptr) @@ -298,13 +272,13 @@ char *print_file_at_index(char *current_ptr, __uint8_t index, int num_columns) } // If num_columns is provided, fill the rest of the line with spaces - // if (num_columns > 0) - // { - // for (; chars_printed < num_columns; chars_printed++) - // { - // putchar(' '); - // } - // } + if (num_columns > 0) + { + for (; chars_printed < num_columns; chars_printed++) + { + putchar(' '); + } + } putchar('\r'); putchar('\n'); @@ -325,7 +299,7 @@ char *print_file_at_index(char *current_ptr, __uint8_t index, int num_columns) int display_paginated_content(char *file_array, int num_files, int page_size, char *item_name, __uint32_t *keypress) { - void highlight_and_print(char *file_array, __uint8_t index, __uint8_t offset, int current_line, int num_columns, bool highlight) + void highlight_and_print(char *file_array, __uint16_t index, __uint16_t offset, int current_line, int num_columns, __uint16_t highlight) { locate(0, current_line + 2 + index - offset); if (highlight) @@ -395,10 +369,10 @@ int display_paginated_content(char *file_array, int num_files, int page_size, ch } long key; - bool change_page = false; + __uint16_t change_page = FALSE; while ((selected_rom < 0) && (!change_page)) { - highlight_and_print(file_array, current_index, page_size * page_number, current_line, 80, true); + highlight_and_print(file_array, current_index, page_size * page_number, current_line, 80, TRUE); key = Crawcin(); if (keypress != NULL) { @@ -409,7 +383,7 @@ int display_paginated_content(char *file_array, int num_files, int page_size, ch case KEY_UP_ARROW: if (current_index > start_index) { - highlight_and_print(file_array, current_index, page_size * page_number, current_line, 80, false); + highlight_and_print(file_array, current_index, page_size * page_number, current_line, 80, FALSE); current_index = current_index - 1; } break; @@ -417,7 +391,7 @@ int display_paginated_content(char *file_array, int num_files, int page_size, ch case KEY_DOWN_ARROW: if (current_index < end_index) { - highlight_and_print(file_array, current_index, page_size * page_number, current_line, 80, false); + highlight_and_print(file_array, current_index, page_size * page_number, current_line, 80, FALSE); current_index = current_index + 1; } break; @@ -426,7 +400,7 @@ int display_paginated_content(char *file_array, int num_files, int page_size, ch { page_number--; current_index = page_number * page_size; - change_page = true; + change_page = TRUE; } break; case KEY_RIGHT_ARROW: @@ -434,7 +408,7 @@ int display_paginated_content(char *file_array, int num_files, int page_size, ch { page_number++; current_index = page_number * page_size; - change_page = true; + change_page = TRUE; } break; case KEY_ENTER: diff --git a/configurator/src/main.c b/configurator/src/main.c index c4e7d88..5bea514 100644 --- a/configurator/src/main.c +++ b/configurator/src/main.c @@ -61,7 +61,7 @@ static MenuItem menuItems[] = { {EXIT_OPTION, EXIT_OPTION_LINE, "Exit"}}; // Flag if loading from ROM boot or GEM program -static bool is_rom_boot = false; +static __uint16_t is_rom_boot = FALSE; // Modify if more items added before this selector static __int8_t delay_toogle_selector_index = 5; @@ -105,11 +105,11 @@ static __int8_t get_number_active_wait(CallbackFunction networkCallback, Callbac // The order of the call matters. Check the usage of the memory if (storageCallback != NULL) { - storageCallback(true); + storageCallback(TRUE); } if (networkCallback != NULL) { - networkCallback(true); + networkCallback(TRUE); } // Change the DELAY_TOGGLE_SELECTOR_OPTION description according to the value of is_delay_option_enabled() if (is_delay_option_enabled()) @@ -180,9 +180,12 @@ static int run() initMedResolution(palette); } - __uint8_t err = read_config(); + init_config(); + init_connection_status(); + init_storage(); + __uint16_t err = read_config(); - __uint8_t feature = err; // If the config is not loaded, exit the program. Otherwise, show the menu + __uint16_t feature = err; // If the config is not loaded, exit the program. Otherwise, show the menu while (feature == 0) { feature = menu(); diff --git a/configurator/src/network.c b/configurator/src/network.c index edb5888..8ede76d 100644 --- a/configurator/src/network.c +++ b/configurator/src/network.c @@ -1,11 +1,10 @@ #include "include/network.h" -ConnectionStatus connection_status = DISCONNECTED; -WifiScanData *wifiScanData = NULL; -// ConnectionData *connection_data = (ConnectionData *)(CONNECTION_STATUS_START_ADDRESS + sizeof(__uint32_t)); -ConnectionData *connection_data = NULL; -bool poll_latest_release = true; -bool latest_release_available = false; +static ConnectionData *connection_data = NULL; +static ConnectionStatus connection_status = DISCONNECTED; + +static __uint16_t poll_latest_release = TRUE; +static __uint16_t latest_release_available = FALSE; static void read_networks_from_memory(char *ssids, WifiNetworkInfo networks[], __uint16_t total_size) { @@ -21,9 +20,9 @@ static void read_networks_from_memory(char *ssids, WifiNetworkInfo networks[], _ current_ssid_position[0] = '\0'; // Null terminate the list of ssids } -static __uint8_t get_network_count(char *file_array) +static __uint16_t get_network_count(char *file_array) { - __uint8_t count = 0; + __uint16_t count = 0; char *current_ptr = file_array; while (*current_ptr) @@ -93,10 +92,16 @@ char *get_status_str(ConnectionStatus status) return status_str; } -__uint16_t get_connection_status(bool show_bar) +void init_connection_status() +{ + connection_data = malloc(sizeof(ConnectionData)); + memset(connection_data, 0, sizeof(ConnectionData)); +} + +__uint16_t get_connection_status(__uint16_t show_bar) { char buffer[STATUS_STRING_BUFFER_SIZE]; - int err = send_sync_command(GET_IP_DATA, NULL, (__uint16_t)0, 5, false); + int err = send_sync_command(GET_IP_DATA, NULL, (__uint16_t)0, 5, FALSE); if (err != 0) { @@ -132,8 +137,9 @@ __uint16_t get_connection_status(bool show_bar) return err; } -bool check_latest_release() +__uint16_t check_latest_release() { + latest_release_available = FALSE; // Check if the latest release is available if (connection_data != NULL) { @@ -141,8 +147,8 @@ bool check_latest_release() { if (poll_latest_release) { - poll_latest_release = false; - int err = send_sync_command(GET_LATEST_RELEASE, NULL, (__uint16_t)0, 10, false); + poll_latest_release = FALSE; + int err = send_sync_command(GET_LATEST_RELEASE, NULL, 0, 10, FALSE); if (err == 0) { char *latest_release = (char *)(LATEST_RELEASE_START_ADDRESS + sizeof(__uint32_t)); @@ -155,7 +161,7 @@ bool check_latest_release() return latest_release_available; } -__uint8_t check_network_connection() +__uint16_t check_network_connection() { if ((connection_data != NULL) && (connection_data->status != CONNECTED_WIFI_IP)) { @@ -167,7 +173,7 @@ __uint8_t check_network_connection() return 0; // ok } -__uint8_t network_selector() +__uint16_t network_selector() { PRINT_APP_HEADER(VERSION); @@ -175,19 +181,20 @@ __uint8_t network_selector() locate(0, 2); printf("Scanning the network..."); - send_sync_command(LAUNCH_SCAN_NETWORKS, NULL, (__uint16_t)0, 10, true); + send_sync_command(LAUNCH_SCAN_NETWORKS, NULL, (__uint16_t)0, 10, TRUE); printf("\r\033KRetrieving networks..."); - send_sync_command(GET_SCANNED_NETWORKS, NULL, (__uint16_t)0, 10, true); + send_sync_command(GET_SCANNED_NETWORKS, NULL, (__uint16_t)0, 10, TRUE); printf("\r\n"); int num_networks = -1; - __uint32_t network_list_mem = NETWORK_START_ADDRESS + sizeof(__uint32_t); + __uint32_t network_list_mem = (__uint32_t)(NETWORK_START_ADDRESS + sizeof(__uint32_t)); #ifdef _DEBUG printf("Reading network list from memory address: 0x%08X\r\n", network_list_mem); #endif + WifiScanData *wifiScanDataBuff = (WifiScanData *)network_list_mem; char *network_array = malloc(MAX_SSID_LENGTH * wifiScanDataBuff->count + 1); read_networks_from_memory(network_array, wifiScanDataBuff->networks, wifiScanDataBuff->count); @@ -274,7 +281,7 @@ __uint8_t network_selector() return 0; // Return 0 to avoid to force a reset } -__uint8_t roms_from_network_selector() +__uint16_t roms_from_network_selector() { PRINT_APP_HEADER(VERSION); @@ -291,14 +298,14 @@ __uint8_t roms_from_network_selector() __uint16_t *network_file_list_mem = (__uint16_t *)(NETWORK_FILE_LIST_START_ADDRESS + sizeof(__uint32_t)); printf("Getting ROMs list..."); - send_sync_command(GET_ROMS_JSON_FILE, NULL, (__uint16_t)0, 10, true); + send_sync_command(GET_ROMS_JSON_FILE, NULL, (__uint16_t)0, 10, TRUE); printf("\r\n"); #ifdef _DEBUG printf("Reading file list from memory address: 0x%08X\r\n", network_file_list_mem); #endif - char *file_array = read_files_from_memory((__uint8_t *)network_file_list_mem); + char *file_array = read_files_from_memory((char *)network_file_list_mem); if (!file_array) { @@ -323,14 +330,14 @@ __uint8_t roms_from_network_selector() printf("Downloading ROM. Wait until the led in the board blinks a 'E' or 'D' in morse..."); - send_sync_command(DOWNLOAD_ROM, &rom_number, 2, 30, true); + send_sync_command(DOWNLOAD_ROM, &rom_number, 2, 30, TRUE); printf("\r\033KROM file downloaded. "); return 1; // Positive is OK } -__uint8_t wifi_menu() +__uint16_t wifi_menu() { if (connection_data->status == DISCONNECTED) { @@ -355,7 +362,7 @@ __uint8_t wifi_menu() if (Cconis()) { int fullkey = Crawcin(); - __uint8_t key = fullkey & 0xFF; + __uint16_t key = fullkey & 0xFF; if (fullkey == KEY_ESC) { // Back to main menu diff --git a/configurator/src/reset.c b/configurator/src/reset.c index 7ee7dd4..8b57ebf 100644 --- a/configurator/src/reset.c +++ b/configurator/src/reset.c @@ -1,6 +1,6 @@ #include "include/reset.h" -__uint8_t reset() +__uint16_t reset() { char userResponse; @@ -14,7 +14,7 @@ __uint8_t reset() // Check if the input is 'Y' or 'y' if (userResponse == 'Y' || userResponse == 'y') { - send_sync_command(RESET_DEVICE, NULL, 0, 10, false); + send_sync_command(RESET_DEVICE, NULL, 0, 10, FALSE); please_wait("\r\033KResetting SidecarT...", 10); printf("\r\033KSidecarT has been reset.\r\n"); } diff --git a/configurator/src/romselector.c b/configurator/src/romselector.c index d1a280d..2d87021 100644 --- a/configurator/src/romselector.c +++ b/configurator/src/romselector.c @@ -3,7 +3,7 @@ //================================================================ // ROM selector -__uint8_t rom_selector() +__uint16_t rom_selector() { PRINT_APP_HEADER(VERSION); @@ -17,17 +17,17 @@ __uint8_t rom_selector() printf("Loading available ROM images..."); - send_sync_command(LIST_ROMS, NULL, 0, 10, true); + send_sync_command(LIST_ROMS, 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) { @@ -52,7 +52,7 @@ __uint8_t rom_selector() printf("Loading ROM. Wait until the led in the board blinks a 'E' or 'D' in morse..."); - send_sync_command(LOAD_ROM, &rom_number, 2, 30, true); + send_sync_command(LOAD_ROM, &rom_number, 2, 30, TRUE); printf("\r\033KROM file loaded. "); diff --git a/configurator/src/storage.c b/configurator/src/storage.c index 4e97221..9cda532 100644 --- a/configurator/src/storage.c +++ b/configurator/src/storage.c @@ -1,24 +1,30 @@ #include "include/storage.h" -// SdCardData *sd_card_data = (SdCardData *)SD_CARD_DATA_START_ADDRESS + sizeof(__uint32_t); -SdCardData *sd_card_data = NULL; +static SdCardData *sd_card_data = NULL; -__uint16_t get_storage_status(bool show_bar) +void init_storage() +{ + sd_card_data = malloc(sizeof(SdCardData)); + memset(sd_card_data, 0, sizeof(SdCardData)); +} + +__uint16_t get_storage_status(__uint16_t show_bar) { char buffer[STATUS_STRING_BUFFER_SIZE]; - int err = send_sync_command(GET_SD_DATA, NULL, (__uint16_t)0, 5, false); + int err = send_sync_command(GET_SD_DATA, NULL, 0, 5, FALSE); if (err != 0) { snprintf(buffer, STATUS_STRING_BUFFER_SIZE, "Cannot read microSD status. Is the SidecarT connected?"); } else { + if (sd_card_data == NULL) { // We don't know the size of the structure, but 1K should be more than enough sd_card_data = malloc(sizeof(SdCardData)); } - memcpy(sd_card_data, (SdCardData *)(SD_CARD_DATA_START_ADDRESS + sizeof(__uint32_t)), sizeof(SdCardData)); + memcpy(sd_card_data, (SdCardData *)(SD_CARD_DATA_START_ADDRESS + sizeof(__uint32_t)), 512); if (show_bar) { @@ -37,9 +43,13 @@ __uint16_t get_storage_status(bool show_bar) } else { + ConfigEntry *roms_folder = get_config_entry("ROMS_FOLDER"); + ConfigEntry *floppies_folder = get_config_entry("FLOPPIES_FOLDER"); + char *roms_folder_value = roms_folder != NULL ? roms_folder->value : "/roms"; + char *floppies_folder_value = floppies_folder != NULL ? floppies_folder->value : "/floppies"; snprintf(buffer, STATUS_STRING_BUFFER_SIZE, "No microSD card found. Insert one with the folders '%s' and '%s'", - sd_card_data->roms_folder, - sd_card_data->floppies_folder); + roms_folder_value, + floppies_folder_value); } } } @@ -55,23 +65,29 @@ __uint16_t get_storage_status(bool show_bar) return err; } -static __uint8_t check_folder(__uint16_t service) +static __uint16_t check_folder(__uint16_t service) { - __uint8_t status = 0; // 0 is ok + __uint16_t status = 0; // 0 is ok + ConfigEntry *roms_folder = get_config_entry("ROMS_FOLDER"); + ConfigEntry *floppies_folder = get_config_entry("FLOPPIES_FOLDER"); + ConfigEntry *harddisks_folder = get_config_entry("HARDDISKS_FOLDER"); + char *roms_folder_value = roms_folder != NULL ? roms_folder->value : "/roms"; + char *floppies_folder_value = floppies_folder != NULL ? floppies_folder->value : "/floppies"; + char *harddisks_folder_value = harddisks_folder != NULL ? harddisks_folder->value : "/harddisks"; if ((sd_card_data == NULL) || (sd_card_data->status != SD_CARD_MOUNTED)) { printf("No microSD card found. Insert one with the folder "); switch (service) { case ROMS: - printf("'%s'", sd_card_data->roms_folder); + printf("'%s'", roms_folder_value); break; case FLOPPIES: case FLOPPIES_DB: - printf("'%s'", sd_card_data->floppies_folder); + printf("'%s'", floppies_folder_value); break; case HARDDISKS: - printf("'%s'", sd_card_data->harddisks_folder); + printf("'%s'", harddisks_folder_value); break; default: break; @@ -83,7 +99,7 @@ static __uint8_t check_folder(__uint16_t service) { if (sd_card_data->roms_folder_status != ROMS_FOLDER_OK) { - printf("The folder '%s' was not found in the microSD card.\r\nCreate one and put your ROMS inside.\r\n", sd_card_data->roms_folder); + printf("The folder '%s' was not found in the microSD card.\r\nCreate one and put your ROMS inside.\r\n", roms_folder_value); status = 2; } } @@ -91,7 +107,7 @@ static __uint8_t check_folder(__uint16_t service) { if (sd_card_data->floppies_folder_status != FLOPPIES_FOLDER_OK) { - printf("The folder '%s' was not found in the microSD card.\r\nCreate one and put your floppy images inside.\r\n", sd_card_data->floppies_folder); + printf("The folder '%s' was not found in the microSD card.\r\nCreate one and put your floppy images inside.\r\n", floppies_folder_value); status = 3; } } @@ -99,7 +115,7 @@ static __uint8_t check_folder(__uint16_t service) { if (sd_card_data->floppies_folder_status != FLOPPIES_FOLDER_OK) { - printf("The folder '%s' was not found in the microSD card.\r\nPlease create one, SidecarT will download the floppy images inside.\r\n", sd_card_data->floppies_folder); + printf("The folder '%s' was not found in the microSD card.\r\nPlease create one, SidecarT will download the floppy images inside.\r\n", floppies_folder_value); status = 4; } } @@ -107,7 +123,7 @@ static __uint8_t check_folder(__uint16_t service) { if (sd_card_data->harddisks_folder_status != HARDDISKS_FOLDER_OK) { - printf("The folder '%s' was not found in the microSD card.\r\nCreate one and put your hard disk images inside.\r\n", sd_card_data->harddisks_folder); + printf("The folder '%s' was not found in the microSD card.\r\nCreate one and put your hard disk images inside.\r\n", harddisks_folder_value); status = 5; } } @@ -119,22 +135,22 @@ static __uint8_t check_folder(__uint16_t service) return status; } -__uint8_t check_folder_roms() +__uint16_t check_folder_roms() { return check_folder(ROMS); } -__uint8_t check_folder_floppies() +__uint16_t check_folder_floppies() { return check_folder(FLOPPIES); } -__uint8_t check_folder_floppies_db() +__uint16_t check_folder_floppies_db() { return check_folder(FLOPPIES_DB); } -__uint8_t check_folder_harddisks() +__uint16_t check_folder_harddisks() { return check_folder(HARDDISKS); }